package oasis

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

Source file OCamlbuildPlugin.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
(******************************************************************************)
(* 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              *)
(******************************************************************************)


(** Build using ocamlbuild
    @author Sylvain Le Gall
  *)


open OASISTypes
open OASISGettext
open OASISUtils
open OASISString
open BaseEnv
open OCamlbuildCommon
open BaseStandardVar


let cond_targets_hook = ref (fun lst -> lst)


let build ~ctxt args pkg argv =
  (* Return the filename in build directory *)
  let in_build_dir fn =
    Filename.concat
      (build_dir argv)
      fn
  in

  (* Return the unix filename in host build directory *)
  let in_build_dir_of_unix fn =
    in_build_dir (OASISHostPath.of_unix fn)
  in

  let cond_targets =
    List.fold_left
      (fun acc ->
         function
           | Library (cs, bs, lib) when var_choose bs.bs_build ->
               begin
                 let evs, unix_files =
                   BaseBuilt.of_library
                     in_build_dir_of_unix
                     (cs, bs, lib)
                 in

                 let tgts =
                   List.flatten
                     (List.filter
                        (fun l -> l <> [])
                        (List.map
                           (List.filter
                              (fun fn ->
                               ends_with ~what:".cma" fn
                               || ends_with ~what:".cmxs" fn
                               || ends_with ~what:".cmxa" fn
                               || ends_with ~what:(ext_lib ()) fn
                               || ends_with ~what:(ext_dll ()) fn))
                           unix_files))
                 in

                   match tgts with
                     | _ :: _ ->
                         (evs, tgts) :: acc
                     | [] ->
                         failwithf
                           (f_ "No possible ocamlbuild targets for library %s")
                           cs.cs_name
               end

           | Object (cs, bs, obj) when var_choose bs.bs_build ->
               begin
                 let evs, unix_files =
                   BaseBuilt.of_object
                     in_build_dir_of_unix
                     (cs, bs, obj)
                 in

                 let tgts =
                   List.flatten
                     (List.filter
                        (fun l -> l <> [])
                        (List.map
                           (List.filter
                              (fun fn ->
                               ends_with ~what:".cmo" fn
                               || ends_with ~what:".cmx" fn))
                           unix_files))
                 in

                   match tgts with
                     | _ :: _ ->
                         (evs, tgts) :: acc
                     | [] ->
                         failwithf
                           (f_ "No possible ocamlbuild targets for object %s")
                           cs.cs_name
               end

           | Executable (cs, bs, exec) when var_choose bs.bs_build ->
               begin
                 let evs, _, _ =
                   BaseBuilt.of_executable in_build_dir_of_unix (cs, bs, exec)
                 in

                 let target ext =
                   let unix_tgt =
                     (OASISUnixPath.concat
                        bs.bs_path
                        (OASISUnixPath.chop_extension
                           exec.exec_main_is))^ext
                   in
                   let evs =
                     (* Fix evs, we want to use the unix_tgt, without copying *)
                     List.map
                       (function
                          | BaseBuilt.BExec, nm, _ when nm = cs.cs_name ->
                              BaseBuilt.BExec, nm,
                              [[in_build_dir_of_unix unix_tgt]]
                          | ev ->
                              ev)
                       evs
                   in
                     evs, [unix_tgt]
                 in

                 (* Add executable *)
                 let acc =
                   match bs.bs_compiled_object with
                     | Native ->
                         (target ".native") :: acc
                     | Best when bool_of_string (is_native ()) ->
                         (target ".native") :: acc
                     | Byte
                     | Best ->
                         (target ".byte") :: acc
                 in
                   acc
               end

           | Library _ | Object _ | Executable _ | Test _
           | SrcRepo _ | Flag _ | Doc _ ->
               acc)
      []
      (* Keep the pkg.sections ordered *)
      (List.rev pkg.sections);
  in

  (* Check and register built files *)
  let check_and_register (bt, bnm, lst) =
    List.iter
      (fun fns ->
         if not (List.exists OASISFileUtil.file_exists_case fns) then
           failwithf
             (fn_
                "Expected built file %s doesn't exist."
                "None of expected built files %s exists."
                (List.length fns))
             (String.concat (s_ " or ") (List.map (Printf.sprintf "'%s'") fns)))
      lst;
      (BaseBuilt.register ~ctxt bt bnm lst)
  in

  (* Run the hook *)
  let cond_targets = !cond_targets_hook cond_targets in

  (* Run a list of target... *)
  run_ocamlbuild
    ~ctxt
    {args with extra = List.flatten (List.map snd cond_targets) @ args.extra}
    argv;
  (* ... and register events *)
  List.iter check_and_register (List.flatten (List.map fst cond_targets))


let clean ~ctxt pkg args  =
  run_clean ~ctxt args;
  List.iter
    (function
       | Library (cs, _, _) ->
           BaseBuilt.unregister ~ctxt BaseBuilt.BLib cs.cs_name
       | Executable (cs, _, _) ->
           BaseBuilt.unregister ~ctxt BaseBuilt.BExec cs.cs_name;
           BaseBuilt.unregister ~ctxt BaseBuilt.BExecLib cs.cs_name
       | _ ->
           ())
    pkg.sections


(* END EXPORT *)


open OASISFileTemplate
open OASISUtils
open OASISMessage
open OASISGettext
open OASISPlugin
open OASISTypes
open MyOCamlbuildBase
open Ocamlbuild_plugin
open OCamlbuildId


let plugin =
  `Build, name, Some version


let self_id, all_id =
  Build.create plugin


let pure_interface_test =
  OASISFeatures.package_test
    (OASISFeatures.create "pure_interface" ~plugin
       OASISFeatures.alpha
       (fun () ->
          s_ "Allow to have module with only .mli file."))


let pivot_data =
  data_new_property plugin


let only_h_files lst =
  List.filter
    (fun fn -> OASISUnixPath.check_extension fn "h")
    lst


let only_c_files lst =
  List.filter
    (fun fn -> OASISUnixPath.check_extension fn "c")
    lst


let add_tags tag_t tgts tags =
  let quote_target fn =
    let test_char =
      String.contains fn
    in
      if test_char '*' ||
         test_char '?' ||
         test_char '{' ||
         test_char '[' then
        "<"^fn^">"
      else
        "\""^fn^"\""
  in

    List.fold_left
      (fun tag_t tgt ->
         List.fold_left
           (fun tag_t tag ->
              ((quote_target tgt)^": "^tag) :: tag_t)
           tag_t
           tags)
      tag_t
      tgts


let prepend_bs_path bs fn =
  Tag.filename_concat bs.bs_path fn


let bs_paths bs files =
  let subdirs =
    List.rev_map OASISUnixPath.dirname
      (List.rev_map (prepend_bs_path bs) files)
  in
    (* Unique elements *)
    SetString.elements
      (SetString.of_list
         (List.rev_map OASISUnixPath.reduce (bs.bs_path :: subdirs)))


let bs_tags pkg sct cs bs src_dirs src_internal_dirs link_tgt ctxt tag_t
      myocamlbuild_t =

  let link_pkg =
    (* Only link findlib package with executable *)
    match sct with
      | Executable _ -> true
      | Library _ | Object _ | Flag _ | Test _ | SrcRepo _ | Doc _ -> false
  in

  let src_tgts =
    List.rev_append
      (* .ml files *)
      (List.rev_map
         (fun dn -> Tag.filename_concat dn "*.ml{,i,y}")
         (src_dirs @ src_internal_dirs))
      (* .c files *)
      (List.map
         (prepend_bs_path bs)
         (only_c_files bs.bs_c_sources))
  in

  let clib_tgts =
    if bs.bs_c_sources <> [] then
      (List.rev_map
         (fun fmt -> (prepend_bs_path bs) (Printf.sprintf fmt cs.cs_name))
         [
           (* Unix *)
           "dll%s_stubs.so";
           "lib%s_stubs.a";
           (* Win32 *)
           "dll%s_stubs.dll";
           "lib%s_stubs.lib";
         ])
    else
      []
  in

  (* Manipulate ocamlbuild's spec *)
  let atom s = A s
  in
  let path s = P s
  in
  let mkspec pre_arg tr_arg lst =
    S (List.fold_left
         (fun acc arg ->
            match pre_arg with
              | Some s -> s :: arg :: acc
              | None -> arg :: acc)
         []
         (List.rev_map tr_arg lst))
  in

  (* Create flag for extra command line option *)
  let ctxt, tag_t, myocamlbuild_t =
    List.fold_left
      (fun ((ctxt, tag_t, myocamlbuild_t) as acc)
             (basename, tgts, tags, pre_arg, tr_arg, args_cond) ->
         if args_cond <> [OASISExpr.EBool true, []] then
           begin
             let tag_name =
               (* e.g. oasis_library_foo_ccopt *)
               varname_concat
                 "oasis_"
                 (varname_concat
                    (varname_of_string
                       (OASISSection.string_of_section sct))
                    basename)
             in
             let all_tags =
               tag_name :: tags
             in
             let cond_specs =
               List.map
                 (fun (cond, lst) ->
                    cond, mkspec pre_arg tr_arg lst)
                 args_cond
             in
             let flags =
               (all_tags, cond_specs) :: myocamlbuild_t.flags
             in
             let tag_t =
               add_tags tag_t tgts [tag_name]
             in
               ctxt,
               tag_t,
               {myocamlbuild_t with flags = flags}
           end
         else
           begin
             acc
           end)

      (ctxt, tag_t, myocamlbuild_t)

      ([
        "ccopt",
        src_tgts, ["compile"],
        Some (A"-ccopt"),
        atom,
        bs.bs_ccopt;

        "cclib",
        [link_tgt],
        ["link"],
        Some (A"-cclib"),
        atom,
        bs.bs_cclib;

        "cclib",
        clib_tgts,
        ["ocamlmklib"; "c"],
        None,
        atom, bs.bs_cclib;

        "dlllib",
        [link_tgt],
        ["link"; "byte"],
        Some (A"-dllib"),
        path,
        bs.bs_dlllib;

        "dllpath",
        [link_tgt],
        ["link"; "byte"],
        Some (A"-dllpath"),
        path,
        bs.bs_dllpath;

        "dllpath",
        clib_tgts,
        ["ocamlmklib"; "c"],
        Some(A"-dllpath"),
        path,
        bs.bs_dllpath;
      ]
      @
      (List.fold_left
         (fun acc flg ->
            ("byte",
             link_tgt :: src_tgts,
             ["ocaml"; flg; "byte"],
             None,
             atom,
             bs.bs_byteopt)
            ::
            ("native",
             link_tgt :: src_tgts,
             ["ocaml"; flg; "native"],
             None,
             atom,
             bs.bs_nativeopt)
            ::
            acc)
         []
         ["compile";
          "ocamldep";
          "link"]))
  in

  (* Add tag for dependency on C part of the library *)
  let ctxt, tag_t, myocamlbuild_t =
    if bs.bs_c_sources <> [] then
      begin
        (* Generate .clib files *)
        let fn_clib =
          OASISHostPath.add_extension
            (prepend_bs_path bs ("lib"^(nm_libstubs cs.cs_name)))
            "clib"
        in
          add_file
            (template_make
               fn_clib
               comment_ocamlbuild
               []
               (List.map
                  (fun fn -> (Filename.chop_extension fn)^".o")
                  (only_c_files bs.bs_c_sources))
               [])
            ctxt,

          add_tags
            tag_t
            [link_tgt]
            [tag_libstubs cs.cs_name],

          {myocamlbuild_t with
               lib_c =
                 ((cs.cs_name,
                   bs.bs_path,
                   List.map
                     (fun fn -> prepend_bs_path bs fn)
                     (only_h_files bs.bs_c_sources))
                 ::
                  myocamlbuild_t.lib_c)}
      end
    else
      ctxt, tag_t, myocamlbuild_t
  in

  (* Add build depends tags *)
  let tag_t =
    let mp =
      OASISBuildSection.transitive_build_depends pkg
    in
    let supports_ocamlfind = ocamlbuild_supports_ocamlfind pkg in
      add_tags
        tag_t
        (if link_pkg then
           link_tgt :: src_tgts
         else
           src_tgts)
        (List.sort String.compare
          (List.fold_left
             (fun acc ->
                function
                  | FindlibPackage (findlib_pkg, _) ->
                      (if supports_ocamlfind then
                         ("package("^findlib_pkg^")")
                       else
                         ("pkg_"^findlib_pkg)
                      )
                      :: acc
                  | InternalLibrary nm ->
                      ("use_"^nm) :: acc)
             []
             (OASISSection.MapSection.find sct mp)))
  in

  let ctxt =
    (* TODO: merge with qstr_cmplt *)
    set_error
      (not (List.mem (ExternalTool "ocamlbuild") bs.bs_build_tools))
      (Printf.sprintf
         (f_ "ocamlbuild in field BuildTools of %s is mandatory")
         (OASISSection.string_of_section sct))
      ctxt
  in

    ctxt, tag_t, myocamlbuild_t


module MapDirs =
  Map.Make
    (struct
       type t = [`Library of string | `Object of string | `Executable of string]
       let compare t1 t2 =
         match t1, t2 with
           | `Library _, `Executable _
           | `Library _, `Object _
           | `Object _, `Executable _ ->
               -1
           | `Object _, `Library _
           | `Executable _, `Library _
           | `Executable _, `Object _ ->
               1
           | `Library s1, `Library s2
           | `Object s1, `Object s2
           | `Executable s1, `Executable s2 ->
               String.compare s1 s2
     end)


let compute_map_dirs pkg =
  let add k dirs internal_dirs mp =
    let src_dirs, src_internal_dirs =
      try
        MapDirs.find k mp
      with Not_found ->
        SetString.empty, SetString.empty
    in
    let add_dirs st dirs = SetString.add_list st dirs in
      MapDirs.add k
        (add_dirs src_dirs dirs,
         add_dirs src_internal_dirs internal_dirs)
        mp
  in
  let map_dirs =
    List.fold_left
      (fun mp ->
         function
           | Library (cs, bs, lib) ->
               add (`Library cs.cs_name)
                 (* All paths accessed from within the library *)
                 (bs_paths bs lib.lib_modules)
                 (* All paths accessed only by the library *)
                 (bs_paths bs lib.lib_internal_modules)
                 mp

           | Object (cs, bs, obj) ->
               add (`Object cs.cs_name)
                 (* All paths accessed from within the (potentially packed)
                    object *)
                 (bs_paths bs obj.obj_modules)
                 [] (* No internal paths *)
                 mp

           | Executable (cs, bs, exec) ->
               add (`Executable cs.cs_name)
                 (bs_paths bs [exec.exec_main_is])
                 [] (* No internal paths *)
                 mp
           | Flag _ | SrcRepo _ | Test _ | Doc _ ->
               mp)
      MapDirs.empty
      pkg.sections
  in
    (* Now get rid of internal paths that are also non internal. *)
    MapDirs.map
      (fun (src_dirs, src_internal_dirs) ->
         SetString.elements src_dirs,
         SetString.elements (SetString.diff src_internal_dirs src_dirs))
      map_dirs


let compute_includes map_dirs pkg =
  let add_includes dir set_dirs includes =
    (* Not self-dependent *)
    let set_dirs = SetString.diff set_dirs (SetString.singleton dir) in
    let pre_dirs =
      try
        MapString.find dir includes
      with Not_found ->
        SetString.empty
    in
      MapString.add dir (SetString.union set_dirs pre_dirs) includes
  in

  let add_map_dirs k bs includes =
    let dep_dirs =
      (* Source dirs of dependent libraries *)
      List.fold_left
        (fun set ->
           function
             | InternalLibrary nm ->
                let src_dirs, _ =
                   try MapDirs.find (`Library nm) map_dirs
                   with Not_found -> MapDirs.find (`Object nm) map_dirs
                 in
                   SetString.add_list set src_dirs

             | FindlibPackage _ ->
                 set)
        SetString.empty
        bs.bs_build_depends
    in
    let self_dirs =
      (* Source dirs *)
      let src_dirs, src_internal_dirs = MapDirs.find k map_dirs in
        SetString.add_list (SetString.of_list src_dirs) src_internal_dirs
    in
    let all_dirs = SetString.union dep_dirs self_dirs in
    let all_dirs =
      (* No need to include the current dir. *)
      SetString.filter
        (fun dn -> not (OASISUnixPath.is_current_dir dn))
        all_dirs
    in

      (* All self_dirs depends on all_dirs *)
      SetString.fold
        (fun dir includes ->
           add_includes dir all_dirs includes)
        self_dirs
        includes
  in

  let includes =
    List.fold_left
      (fun includes ->
         function
           | Library (cs, bs, _) ->
               add_map_dirs (`Library cs.cs_name) bs includes
           | Object (cs, bs, _) ->
               add_map_dirs (`Object cs.cs_name) bs includes
           | Executable (cs, bs, _) ->
               add_map_dirs (`Executable cs.cs_name) bs includes
           | Flag _ | SrcRepo _ | Test _ | Doc _ ->
               includes)
      MapString.empty
      pkg.sections
  in
    MapString.fold
      (fun dir include_dirs acc ->
         if SetString.empty <> include_dirs then
           (dir, SetString.elements include_dirs) :: acc
         else
           acc)
      includes
      []


(* Check if the given files list only contains .mli. *)
let is_pure_interface (base_fn, fn_lst) =
  let rec is_pure_interface_aux =
    (* TODO: this needs to be refine because sometime we don't have the .ml file
     * because it is generated (BaseData.ml) but we have the .mli.
     *)
    function
      | [fn] ->
          OASISString.ends_with ~what:".mli" fn
      | fn :: tl ->
          OASISString.ends_with ~what:".mli" fn && is_pure_interface_aux tl
      | [] -> false
  in
    is_pure_interface_aux fn_lst


let add_ocamlbuild_files ctxt pkg =

  let map_dirs =
    compute_map_dirs pkg
  in

  let ctxt, tag_t, myocamlbuild_t =
    List.fold_left
      (fun (ctxt, tag_t, myocamlbuild_t) ->
         function
           | Library (cs, bs, lib) as sct ->
               begin
                 (* Extract content for libraries *)

                 (* All paths of the library *)
                 let src_dirs, src_internal_dirs =
                   MapDirs.find (`Library cs.cs_name) map_dirs
                 in

                 (* Generated library *)
                 let target_lib =
                   let ext =
                     match bs.bs_compiled_object with
                       | Best ->
                           "{cma,cmxa}"
                       | Byte ->
                           "cma"
                       | Native ->
                           "cmxa"
                   in
                     prepend_bs_path bs
                       (OASISUnixPath.add_extension cs.cs_name ext)
                 in

                 (* Start comment *)
                 let tag_t =
                   (Printf.sprintf "# Library %s" cs.cs_name) :: tag_t
                 in

                 (* Sources of the library. *)
                 let sources =
                   OASISLibrary.source_unix_files
                     ~ctxt:ctxt.ctxt
                     (cs, bs, lib)
                     (fun ufn ->
                        OASISFileUtil.file_exists_case
                          (OASISHostPath.of_unix ufn))
                 in

                 let intf_module_list, impl_module_list =
                   let to_module (base_fn, _) =
                     OASISString.capitalize_ascii (Filename.basename base_fn)
                   in
                   let intf_module_list =
                     if pure_interface_test pkg then
                       List.map to_module
                         (List.filter is_pure_interface sources)
                     else
                       []
                   in
                     List.partition
                       (fun modul -> List.mem modul intf_module_list)
                       (lib.lib_modules @ lib.lib_internal_modules)
                 in

                 (* Add dependency of cmxs to their own library: used
                    at link time when there is C code *)
                 let tag_t =
                   add_tags
                     tag_t
                     [prepend_bs_path
                        bs
                        (OASISUnixPath.add_extension cs.cs_name "cmxs")]
                     ["use_"^cs.cs_name]
                 in

                 let tag_t =
                   if lib.lib_pack then
                     add_tags
                       tag_t
                       (List.rev_map
                          (fun (base_fn, _) ->
                             OASISUnixPath.add_extension base_fn "cmx")
                          sources)
                       ["for-pack("^OASISString.capitalize_ascii cs.cs_name^")"]
                   else
                     tag_t
                 in

                 let ctxt, tag_t, myocamlbuild_t =
                   bs_tags
                     pkg sct cs bs
                     src_dirs
                     src_internal_dirs
                     target_lib
                     ctxt
                     tag_t
                     myocamlbuild_t
                 in

                 let myocamlbuild_t =
                   {myocamlbuild_t with
                        lib_ocaml =
                          (cs.cs_name,
                           List.filter
                             (fun fn -> not (OASISUnixPath.is_current fn))
                             src_dirs,
                           intf_module_list)
                          :: myocamlbuild_t.lib_ocaml}
                 in

                 let () =
                   if lib.lib_modules = [] then
                     warning
                       ~ctxt:ctxt.ctxt
                       (f_ "No exported module defined for library %s")
                       cs.cs_name;
                 in

                 let ctxt =
                   (* Generate .mllib *)
                   let fn_base = prepend_bs_path bs cs.cs_name in
                   let mllib = OASISHostPath.add_extension fn_base "mllib" in
                   let mldylib =
                     OASISHostPath.add_extension fn_base "mldylib"
                   in
                   let mlpack = OASISHostPath.add_extension fn_base "mlpack" in
                   let mllib_template_lines =
                     (* mllib contains either the name of the pack or the list
                      * of modules. *)
                     if lib.lib_pack then
                       [ OASISString.capitalize_ascii cs.cs_name ]
                     else
                       impl_module_list
                   in
                   let ctxt =
                     add_file
                       (template_make mllib
                          comment_ocamlbuild
                          []
                          mllib_template_lines
                          [])
                       ctxt
                   in
                   let ctxt =
                     add_file
                       (template_make mldylib
                          comment_ocamlbuild
                          []
                          mllib_template_lines
                          [])
                       ctxt
                   in
                   if lib.lib_pack then begin
                     (* generate .mlpack for packed libraries *)
                     add_file
                       (template_make mlpack
                          comment_ocamlbuild
                          []
                          impl_module_list
                          [])
                       ctxt
                   end else begin
                     (* make sure there is no conflicting mlpack file *)
                     {ctxt with
                          other_actions =
                            (fun ()->
                              if OASISFileUtil.file_exists_case mlpack then
                                OASISMessage.error ~ctxt:ctxt.ctxt
                                  (f_ "Conflicting file '%s' and '%s' \
                                      exists, remove '%s'.")
                                  mllib mlpack mlpack)
                          :: ctxt.other_actions}
                   end
                 in
                 ctxt, tag_t, myocamlbuild_t
               end

           | Object (cs, bs, obj) as sct ->
               begin
                 (* Extract content for objects *)

                 (* All paths of the library *)
                 let src_dirs, src_internal_dirs =
                   MapDirs.find (`Object cs.cs_name) map_dirs
                 in

                 (* Generated library *)
                 let target_lib =
                   let ext =
                     match bs.bs_compiled_object with
                       | Best ->
                           "{cmo,cmx}"
                       | Byte ->
                           "cmo"
                       | Native ->
                           "cmx"
                   in
                     prepend_bs_path bs
                       (OASISUnixPath.add_extension cs.cs_name ext)
                 in

                 (* Start comment *)
                 let tag_t =
                   (Printf.sprintf "# Object %s" cs.cs_name) :: tag_t
                 in

                 let tag_t =
                   if List.length obj.obj_modules <> 1 then
                     let base_sources =
                       OASISObject.source_unix_files
                         ~ctxt:ctxt.ctxt
                         (cs, bs, obj)
                         (fun ufn ->
                            OASISFileUtil.file_exists_case
                              (OASISHostPath.of_unix ufn))
                     in
                     add_tags
                       tag_t
                       (List.rev_map
                          (fun (base_fn, _) ->
                             OASISUnixPath.add_extension base_fn "cmx")
                          base_sources)
                       ["for-pack("^OASISString.capitalize_ascii cs.cs_name^")"]
                   else
                     tag_t
                 in

                 let ctxt, tag_t, myocamlbuild_t =
                   bs_tags
                     pkg sct cs bs
                     src_dirs
                     src_internal_dirs
                     target_lib
                     ctxt
                     tag_t
                     myocamlbuild_t
                 in

                 let myocamlbuild_t =
                   {myocamlbuild_t with
                        lib_ocaml =
                          (cs.cs_name,
                           List.filter
                             (fun fn -> not (OASISUnixPath.is_current fn))
                             src_dirs,
                           []) :: myocamlbuild_t.lib_ocaml}
                 in

                 let () =
                   if obj.obj_modules = [] then
                     warning
                       ~ctxt:ctxt.ctxt
                       (f_ "No exported module defined for object %s")
                       cs.cs_name;
                 in

                 let ctxt =
                   match obj.obj_modules with
                     | [ _ ] -> ctxt
                     | _ -> (* generate mlpack file *)
                         let fn_base = prepend_bs_path bs cs.cs_name in
                         let fn =
                           OASISHostPath.add_extension fn_base "mlpack"
                         and not_fn =
                           OASISHostPath.add_extension fn_base "ml"
                         in
                         let ctxt =
                           add_file
                             (template_make
                                fn
                                comment_ocamlbuild
                                []
                                obj.obj_modules
                                [])
                             ctxt
                         in
                           {ctxt with
                                other_actions =
                                  (fun () ->
                                     if OASISFileUtil.file_exists_case not_fn
                                     then
                                       OASISMessage.error ~ctxt:ctxt.ctxt
                                         (f_ "Conflicting file '%s' and '%s' \
                                            exists, remove '%s'.")
                                         fn not_fn not_fn)
                                :: ctxt.other_actions}
                 in
                   ctxt, tag_t, myocamlbuild_t
               end

           | Executable (cs, bs, exec) as sct ->
               begin
                 (* Extract content for executables *)
                 let src_dirs, src_internal_dirs =
                   MapDirs.find (`Executable cs.cs_name) map_dirs
                 in

                 let target_exec =
                   let ext =
                     match bs.bs_compiled_object with
                       | Best ->
                           "{native,byte}"
                       | Byte ->
                           "byte"
                       | Native ->
                           "native"
                   in
                     prepend_bs_path bs
                       (OASISUnixPath.replace_extension
                          (exec.exec_main_is) ext)
                 in

                 let tag_t =
                   (Printf.sprintf "# Executable %s" cs.cs_name) :: tag_t
                 in

                 let ctxt, tag_t, myocamlbuild_t =
                   bs_tags
                     pkg sct cs bs
                     src_dirs
                     src_internal_dirs
                     target_exec
                     ctxt
                     tag_t
                     myocamlbuild_t
                 in

                 let tag_t =
                   if exec.exec_custom then
                     add_tags tag_t [target_exec] ["custom"]
                   else
                     tag_t
                 in

                   ctxt, tag_t, myocamlbuild_t
               end

           | Flag _ | SrcRepo _ | Test _ | Doc _ ->
               ctxt, tag_t, myocamlbuild_t)
      (ctxt, [], {lib_ocaml = []; lib_c = []; flags = []; includes = []})
      pkg.sections
  in

  (* Filter duplicate and reverse content in tag_t. *)
  let tag_t =
    snd
      (List.fold_left
         (fun (prev_tag, acc) tag ->
            (* Don't remove comment but remove already seen tag. *)
            if (String.length tag > 0 && tag.[0] = '#') ||
               not (SetString.mem tag prev_tag) then
              (
                SetString.add tag prev_tag,
                tag :: acc
              )
            else
                (prev_tag, acc))
         (SetString.empty, [])
         tag_t)
  in

  let myocamlbuild_t =
    (* Fix section order *)
    {
      lib_ocaml = List.rev myocamlbuild_t.lib_ocaml;
      lib_c     = List.rev myocamlbuild_t.lib_c;
      flags     = List.rev myocamlbuild_t.flags;
      includes  = compute_includes map_dirs pkg;
    }
  in

  let tag_t =
    "# Ignore VCS directories, you can use the same kind of rule outside"
    ::
    "# OASIS_START/STOP if you want to exclude directories that contains"
    ::
    "# useless stuff for the build process"
    ::
    "true: annot, bin_annot"
    ::
    add_tags
      tag_t
      ["_darcs"; ".git"; ".hg"; ".bzr"; "**/.svn"]
      ["not_hygienic"; "-traverse"]
  in


  let ctxt =
    List.fold_left
      (fun ctxt tmpl -> add_file tmpl ctxt)
      ctxt
      [
        (* Generate _tags *)
        template_make
          "_tags"
          comment_ocamlbuild
          []
          tag_t
          [];

        (* Generate myocamlbuild.ml *)
        template_of_mlfile
         "myocamlbuild.ml"
          []
          [
            OASISData.oasissyslight_ml;
            BaseData.basesysenvironment_ml;
            OCamlbuildData.myocamlbuild_ml;
            "open Ocamlbuild_plugin;;";
            (
              Format.fprintf Format.str_formatter
                "@[<hv2>let package_default =@ %a@,@];;"
                (OASISDataNotation.pp_odn ~opened_modules:["Ocamlbuild_plugin"])
                (MyOCamlbuildBase.odn_of_t myocamlbuild_t);
              Format.flush_str_formatter ()
            );
            "";
            Printf.sprintf
              "let conf = {MyOCamlbuildFindlib.no_automatic_syntax = %b}"
              (OASISFeatures.package_test OASISFeatures.no_automatic_syntax pkg);
            "";
            "let dispatch_default = \
                   MyOCamlbuildBase.dispatch_default conf package_default;;";
            "";
          ]
          ["Ocamlbuild_plugin.dispatch dispatch_default;;"];
      ]
  in

    ctxt


let generator =
  ocamlbuild_common_generator pivot_data OASISPackage.schema all_id

let doit ctxt pkg =
  let args, ctxt =
    args_ocamlbuild_common ctxt pkg
      (generator pkg.schema_data)
  in
  let ctxt = add_ocamlbuild_files ctxt pkg in
    ctxt,
    {
      chng_moduls       = [OCamlbuildData.ocamlbuildsys_ml];
      chng_main         = OASISDataNotation.func_with_arg_ctxt build
                            "OCamlbuildPlugin.build"
                            args (OCamlbuildCommon.odn_of_args);
      chng_clean        = Some (OASISDataNotation.func clean "OCamlbuildPlugin.clean");
      chng_distclean    = None;
    }


let qstrt_completion pkg =
  fix_build_tools (ExternalTool "ocamlbuild") pkg


let init () =
  OCamlbuildId.init ();
  Build.register_act self_id doit;
  register_quickstart_completion all_id qstrt_completion;
  register_generator_package all_id pivot_data generator
OCaml

Innovation. Community. Security.