package oasis

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file OCamlbuildCommon.ml

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
(******************************************************************************)
(* OASIS: architecture for building OCaml libraries and applications          *)
(*                                                                            *)
(* Copyright (C) 2011-2016, Sylvain Le Gall                                   *)
(* Copyright (C) 2008-2011, OCamlCore SARL                                    *)
(*                                                                            *)
(* This library is free software; you can redistribute it and/or modify it    *)
(* under the terms of the GNU Lesser General Public License as published by   *)
(* the Free Software Foundation; either version 2.1 of the License, or (at    *)
(* your option) any later version, with the OCaml static compilation          *)
(* exception.                                                                 *)
(*                                                                            *)
(* This library 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 file COPYING for more         *)
(* details.                                                                   *)
(*                                                                            *)
(* You should have received a copy of the GNU Lesser General Public License   *)
(* along with this library; if not, write to the Free Software Foundation,    *)
(* Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA              *)
(******************************************************************************)


(** Functions common to OCamlbuild build and doc plugin
*)


open OASISGettext
open BaseEnv
open BaseStandardVar
open OASISTypes


type args =
  {
    plugin_tags: string option;
    extra: string list;
  }


let ocamlbuild_clean_ev = "ocamlbuild-clean"


let ocamlbuildflags =
  var_define
    ~short_desc:(fun () -> "OCamlbuild additional flags")
    "ocamlbuildflags"
    (fun () -> "")


(** Fix special arguments depending on environment *)
let fix_args args extra_argv =
  List.flatten
    [
      if (os_type ()) = "Win32" then
        [
          "-classic-display";
          "-no-log";
          "-no-links";
        ]
      else
        [];

      if OASISVersion.comparator_apply
          (OASISVersion.version_of_string (ocaml_version ()))
          (OASISVersion.VLesser (OASISVersion.version_of_string "3.11.1")) then
        [
          "-install-lib-dir";
          (Filename.concat (standard_library ()) "ocamlbuild")
        ]
      else
        [];

      if not (bool_of_string (is_native ())) || (os_type ()) = "Win32" then
        [
          "-byte-plugin"
        ]
      else
        [];

      args.extra;

      begin
        match args.plugin_tags with
        | Some t -> ["-plugin-tag"; Filename.quote t]
        | None -> []
      end;

      if bool_of_string (debug ()) then
        ["-tag"; "debug"]
      else
        [];

      if bool_of_string (tests ()) then
        ["-tag"; "tests"]
      else
        [];

      if bool_of_string (profile ()) then
        ["-tag"; "profile"]
      else
        [];

      OASISString.nsplit (ocamlbuildflags ()) ' ';

      Array.to_list extra_argv;
    ]


(** Run 'ocamlbuild -clean' if not already done *)
let run_clean ~ctxt extra_argv =
  let extra_cli =
    String.concat " " (Array.to_list extra_argv)
  in
  (* Run if never called with these args *)
  if not (BaseLog.exists ~ctxt ocamlbuild_clean_ev extra_cli) then
    begin
      OASISExec.run
        ~ctxt (ocamlbuild ())
        (fix_args {extra = ["-clean"]; plugin_tags = None} extra_argv);
      BaseLog.register ~ctxt ocamlbuild_clean_ev extra_cli;
      at_exit
        (fun () ->
           try
             BaseLog.unregister ~ctxt ocamlbuild_clean_ev extra_cli
           with _ -> ())
    end


(** Run ocamlbuild, unregister all clean events *)
let run_ocamlbuild ~ctxt args extra_argv =
  (* TODO: enforce that target in args must be UNIX encoded i.e. toto/index.html
  *)
  OASISExec.run ~ctxt (ocamlbuild ()) (fix_args args extra_argv);
  (* Remove any clean event, we must run it again *)
  List.iter
    (fun (e, d) -> BaseLog.unregister ~ctxt e d)
    (BaseLog.filter ~ctxt [ocamlbuild_clean_ev])


(** Determine real build directory *)
let build_dir extra_argv =
  let rec search_args dir =
    function
      | "-build-dir" :: dir :: tl ->
        search_args dir tl
      | _ :: tl ->
        search_args dir tl
      | [] ->
        dir
  in
  search_args "_build" (fix_args {extra = []; plugin_tags = None} extra_argv)


(* END EXPORT *)

open OASISValues

let fix_build_tools tool pkg =
  let fix_build_tools' _ bs =
    if not (List.mem tool bs.bs_build_tools) then
      {bs with bs_build_tools = tool :: bs.bs_build_tools}
    else
      bs
  in

  let sections =
    List.fold_left
      (fun acc sct ->
         let sct =
           match sct with
             | Executable (cs, bs, exec) ->
               let bs = fix_build_tools' sct bs in
               Executable (cs, bs, exec)

             | Library (cs, bs, lib) ->
               let bs = fix_build_tools' sct bs in
               Library (cs, bs, lib)

             | Object (cs, bs, obj) ->
               let bs = fix_build_tools' sct bs in
               Object (cs, bs, obj)

             | Flag _ | SrcRepo _ | Test _ | Doc _ as sct ->
               sct
         in
         sct :: acc)
      []
      pkg.sections
  in
  {pkg with sections = List.rev sections}


module Tag =
struct
  (** [filename_concat fn1 fn2] Concat filename, using semantic of _tags
      [fn1] must be a real filename whereas fn2 can contains wildcards.
  *)
  let filename_concat fn1 fn2 =
    OASISUnixPath.concat (OASISUnixPath.reduce fn1) fn2
end

(** Check OCaml version constraint defined in _oasis. *)
let check_ocaml_version version pkg =
  OASISVersion.StringVersion.comparator_ge version pkg.ocaml_version


let ocamlbuild_more_args =
  OASISFeatures.create "ocamlbuild_more_args"
    OASISFeatures.alpha
    (fun () ->
       s_ "Allow to pass arguments to ocamlbuild.")


let ocamlbuild_supports_ocamlfind = check_ocaml_version "3.12.1"
let ocamlbuild_supports_plugin_tags = check_ocaml_version "4.01"


let ocamlbuild_common_generator pivot_data schm id =
  let new_field nm = OASISSchema.new_field schm id nm in
  let plugin_tags =
    new_field
      "PluginTags"
      ~default:None
      ~feature:ocamlbuild_more_args
      (opt string_not_empty)
      (fun () -> s_ "Gives the plugin tags to ocambuild through \
                     '-plugin-tags' (OCaml >= 4.01 only)")
      pivot_data (fun _ args -> args.plugin_tags)
  in
  let extra_args =
    new_field
      "ExtraArgs"
      ~default:[]
      ~feature:ocamlbuild_more_args
      command_line_options
      (fun () -> s_ "Gives extra arguments to ocamlbuild")
      pivot_data (fun _ args -> args.extra)
  in
  fun data ->
    {
      extra = extra_args data;
      plugin_tags = plugin_tags data;
    }


let args_ocamlbuild_common ctxt pkg args =
  let supports_plugins = ocamlbuild_supports_plugin_tags pkg in
  let plugin_tags, ctxt =
    if args.plugin_tags <> None &&
       (not supports_plugins) then begin
      None,
      OASISPlugin.set_error
        (not supports_plugins)
        (s_ "'XOCamlbuildPluginTags' in only available for OCaml >= 4.01. \
             Please restrict your requirements with 'OCamlVersion: >= 4.01'")
        ctxt
    end else begin
      args.plugin_tags, ctxt
    end
  in
  let extra =
    if ocamlbuild_supports_ocamlfind pkg then
      "-use-ocamlfind" :: args.extra
    else
      args.extra
  in
  {plugin_tags; extra}, ctxt


let odn_of_args (args:args) =
  OASISDataNotation.REC
    ("OCamlbuildCommon",
     [
       "plugin_tags",
       (OASISDataNotation.of_option OASISDataNotation.of_string)
         (args:args).plugin_tags;
       "extra",
       (OASISDataNotation.of_list OASISDataNotation.of_string) args.extra
     ])
OCaml

Innovation. Community. Security.