Skip to content
This repository
tag: v1155
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 296 lines (266 sloc) 7.928 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
(*
Copyright © 2011 MLstate

This file is part of OPA.

OPA is free software: you can redistribute it and/or modify it under the
terms of the GNU Affero General Public License, version 3, as published by
the Free Software Foundation.

OPA is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
more details.

You should have received a copy of the GNU Affero General Public License
along with OPA. If not, see <http://www.gnu.org/licenses/>.
*)
(* depends *)
module Arg = BaseArg
module Format = BaseFormat
module String = BaseString

(* - *)

type properties = {
  cclib : string list ;
  ccopt : string list ;
  mlcopt : string list ;
  mllopt : string list ;
  mlinclude : string list ;
  mllibs : string list ;
}

let pp_list fmt list = Format.pp_list " ; " Format.pp_print_string fmt list

let pp_if word fmt list =
  if list <> []
  then
    Format.fprintf fmt "@\n%s: [ %a ]" word pp_list list

let pp_properties fmt properties =
  Format.fprintf fmt "@[<2>{" ;
  pp_if "cclib" fmt properties.cclib ;
  pp_if "ccopt" fmt properties.ccopt ;
  pp_if "mlcopt" fmt properties.mlcopt ;
  pp_if "mllopt" fmt properties.mllopt ;
  pp_if "mlinclude" fmt properties.mlinclude ;
  pp_if "mllibs" fmt properties.mllibs ;
  Format.fprintf fmt "@]@\n}"

(*
<!> Beware, this type is marshaled during the bsl preprocess
*)
type conf = {
  all_platform: properties ;
  linux: properties option ;
  mac:properties option ;
  windows: properties option ;
  cygwin: properties option ;
}

let pp_if word fmt opt =
  match opt with
  | None -> ()
  | Some props ->
      Format.fprintf fmt "@\n%s: %a" word pp_properties props

let pp fmt conf =
  Format.fprintf fmt "@[<2>{" ;
  pp_if "all-platform" fmt (Some conf.all_platform) ;
  pp_if "linux" fmt conf.linux ;
  pp_if "mac" fmt conf.mac ;
  pp_if "windows" fmt conf.windows ;
  pp_if "cygwin" fmt conf.cygwin ;
  Format.fprintf fmt "@]@\n}"

type t = conf

let default_properties = {
  cclib = [] ;
  ccopt = [] ;
  mlcopt = [] ;
  mllopt = [] ;
  mlinclude = [] ;
  mllibs = [] ;
}

let is_default_properties def =
     def.cclib = []
  && def.ccopt = []
  && def.mlcopt = []
  && def.mllopt = []
  && def.mlinclude = []
  && def.mllibs = []

let is_default_properties_opt = function
  | None -> true
  | Some def -> is_default_properties def

let default = {
  all_platform = default_properties ;
  linux = None ;
  mac = None ;
  windows = None ;
  cygwin = None ;
}

let default_conf = default

let is_default def =
  true
  && is_default_properties def.all_platform
  && is_default_properties_opt def.linux
  && is_default_properties_opt def.windows
  && is_default_properties_opt def.cygwin
  && is_default_properties_opt def.mac

(*
Parsing the file.
A line can be a new property, with an optional platform,
or contents
*)
let regexp_property = Str.regexp "\\[\\([^]:]*\\)\\(:\\([^]:]*\\)\\)?\\]$"
let property_platform line =
  if Str.string_match regexp_property line 0
  then
    let property = Str.matched_group 1 line in
    let platform = try Some (Str.matched_group 3 line) with Not_found -> None in
    Some (property, platform)
  else
    None

let all_properties = [
  "mlcopt" ;
  "mllopt" ;
  "cclib" ;
  "ccopt" ;
  "mlinclude" ;
  "mllibs" ;
]

let all_platform = [
  "linux" ;
  "windows" ;
  "cygwin" ;
  "mac" ;
]

let check_property_platform pos property platform =
  let (!!) fmt = OManager.error ("%a@\n"^^fmt) FilePos.pp pos in
  let property =
    match String.trim property with
    | "mllopt" -> `mllopt
    | "mlcopt" -> `mlcopt
    | "cclib" -> `cclib
    | "ccopt" -> `ccopt
    | "mlinclude" -> `mlinclude
    | "mllibs" -> `mllibs
    | unknown -> !! "Invalid property %S@\n%a" unknown (HintUtils.pp_suggestion all_properties) unknown
  in
  let platform =
    match platform with
    | None -> `all
    | Some platform -> (
        match String.trim platform with
        | "linux" -> `linux
        | "max" -> `mac
        | "windows" -> `windows
        | "cygwin" -> `cygwin
        | unknown -> !! "Invalid platform %S@\n%a" unknown (HintUtils.pp_suggestion all_properties) unknown
      )
  in
  property, platform

(*
Convention: everything lists are reversed during the fold
*)

let add_args acc line =
  let args = Arg.split line in
  List.rev_append args acc

let add_property properties property line =
  match property with
  | `cclib -> {
      properties with
        cclib = add_args properties.cclib line
    }
  | `ccopt -> {
      properties with
        ccopt = add_args properties.ccopt line
    }
  | `mlcopt -> {
      properties with
        mlcopt = add_args properties.mlcopt line
    }
  | `mllopt -> {
      properties with
        mllopt = add_args properties.mllopt line
    }
  | `mlinclude -> {
      properties with
        mlinclude = add_args properties.mlinclude line
    }
  | `mllibs -> {
      properties with
        mllibs = add_args properties.mllibs line
    }

let add property platform t line =
  match platform with
  | `all -> {
      t with
        all_platform = add_property t.all_platform property line
    }
  | `linux ->
      let properties = Option.default default_properties t.linux in
      { t with linux = Some (add_property properties property line) }
  | `windows ->
      let properties = Option.default default_properties t.windows in
      { t with windows = Some (add_property properties property line) }
  | `cygwin ->
      let properties = Option.default default_properties t.cygwin in
      { t with cygwin = Some (add_property properties property line) }
  | `mac ->
      let properties = Option.default default_properties t.mac in
      { t with mac = Some (add_property properties property line) }

let fold ~filename t =
  let () =
    if not (File.is_regular filename) then
      OManager.error "I/O error: @{<bright>%S@} -> No such file" filename
  in
  let () =
    let content =
      match File.content_opt filename with
      | Some content -> content
      | None ->
          OManager.error "I/O error: cannot read conf file @{<bright>%S@}" filename
    in
    FilePos.add_file filename content
  in
  let fold_line (((property, platform) as props, t) as acc) line line_number =
    let line = String.trim line in
    if String.length line = 0 || line.[0] = '#' then acc else
    let pos = FilePos.make_pos_from_line filename line_number in
    match property_platform line with
    | Some (property, platform) ->
        let props = check_property_platform pos property platform in
        props, t
    | None -> (
        match property with
        | `none ->
            OManager.error "%a@\nSyntax error. The conf file should start with a property." FilePos.pp pos
        | (
            `cclib
          | `ccopt
          | `mlcopt
          | `mllopt
          | `mlinclude
          | `mllibs
          ) as prop ->
            let t = add prop platform t line in
            props, t
      )
  in
  let _, t = File.lines_foldi fold_line ((`none, `all), t) filename in
  let () =
    #<If:BSL_REGISTER $contains "conf">
      OManager.printf "@[<2>@{<cyan>BslConf:@} folding file %S:@\n %a@]@\n" filename pp t ;
    #<End>
  in
  t

let export t =
  let rev = List.rev in
  let rev_prop prop =
    let { cclib ; ccopt ; mlcopt ; mllopt ; mlinclude ; mllibs } = prop in
    {
      cclib = rev cclib ;
      ccopt = rev ccopt ;
      mlcopt = rev mlcopt ;
      mllopt = rev mllopt ;
      mlinclude = rev mlinclude ;
      mllibs = rev mllibs ;
    } in
  let rev_prop_opt = Option.map rev_prop in
  let { all_platform ; linux ; mac ; windows ; cygwin } = t in
  {
    all_platform = rev_prop all_platform ;
    linux = rev_prop_opt linux ;
    mac = rev_prop_opt mac ;
    windows = rev_prop_opt windows ;
    cygwin = rev_prop_opt cygwin ;
  }
Something went wrong with that request. Please try again.