This file is indexed.

/usr/lib/ocaml/oasis/StdFilesPlugin.ml is in liboasis-ocaml-dev 0.4.5-1build3.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
(******************************************************************************)
(* OASIS: architecture for building OCaml libraries and applications          *)
(*                                                                            *)
(* Copyright (C) 2011-2013, 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              *)
(******************************************************************************)


(** Generate standard text files: INSTALL.txt, README.txt
    @author Sylvain Le Gall
  *)


open OASISFileTemplate
open OASISPlugin
open OASISTypes
open OASISUtils
open OASISValues
open OASISGettext
open OASISVersion
open OASISPlugin
open OASISSchema
open Format
open FormatExt


let plugin =
  `Extra, "StdFiles", Some OASISConf.version_short


let self_id, all_id =
  Extra.create plugin


let markdown =
  OASISFeatures.create "stdfiles_markdown" ~plugin
    OASISFeatures.alpha
    (fun () ->
       s_ "Use markdown comment and replace .txt extensions of standard files by
           .md.")

type package =
    (* Standalone executable *)
  | LTool of prog
    (* Findlib package *)
  | LFindlibPackage of name * (OASISVersion.comparator option)


let facts =
  [
    LFindlibPackage ("ocaml", None),
    [
      (* Tools shipped with OCaml *)
      "camlp4";
      "camlp4boot";
      "camlp4o";
      "camlp4of";
      "camlp4of.opt";
      "camlp4oof";
      "camlp4oof.opt";
      "camlp4o.opt";
      "camlp4orf";
      "camlp4orf.opt";
      "camlp4prof";
      "camlp4r";
      "camlp4rf";
      "camlp4rf.opt";
      "camlp4r.opt";
      "mkcamlp4";
      "ocaml";
      "ocamlbuild";
      "ocamlbuild.byte";
      "ocamlbuild.native";
      "ocamlc";
      "ocamlc.opt";
      "ocamlcp";
      "ocamldebug";
      "ocamldep";
      "ocamldep.opt";
      "ocamldoc";
      "ocamldoc.opt";
      "ocamllex";
      "ocamllex.opt";
      "ocamlmklib";
      "ocamlmktop";
      "ocamlopt";
      "ocamlopt.opt";
      "ocamlprof";
      "ocamlrun";
      "ocamlyacc";
    ],
    [
      (* Libraries shipped with OCaml *)
      "bigarray";
      "camlp4";
      "dbm";
      "dynlink";
      "graphics";
      "labltk";
      "num";
      "stdlib";
      "str";
      "threads";
      "unix";
    ];

    LFindlibPackage ("findlib", None),
    ["ocamlfind"],
    ["findlib"];
  ]


(** Merge 2 versions constraint *)
let merge_version_opt ver_opt1 ver_opt2 =
 match ver_opt1, ver_opt2 with
   | Some v1, Some v2 -> Some (VAnd (v1, v2))
   | None, v | v, None -> v


(** Associate a tool to a findlib package or a tool *)
let package_of_tool =
  let mp =
    MapString.of_list
      (List.fold_left
         (fun acc (pkg, tools, _) ->
            List.fold_left
              (fun acc tool -> (tool, pkg) :: acc)
              acc
              tools)
         [] facts)
  in
    fun tool ->
      try
        MapString.find tool mp
      with Not_found ->
        LTool tool


(** Associate a library to a findlib package raw data *)
let package_of_library =
  let mp =
    MapString.of_list
      (List.fold_left
         (fun acc (pkg, _, libs) ->
            List.fold_left
              (fun acc lib -> (lib, pkg) :: acc)
              acc
              libs)
         [] facts)
  in
    fun lib ver_opt ->
      try
        MapString.find lib mp
      with Not_found ->
        LFindlibPackage (lib, ver_opt)


type t =
    {
      readme:  unix_filename option;
      install: unix_filename option;
      authors: unix_filename option;
    }


let pivot_data =
  data_new_property plugin


let default_filenames =
  let mp =
    MapString.of_list(["README", "README.txt";
                       "INSTALL", "INSTALL.txt";
                       "AUTHORS", "AUTHORS.txt"])
  in
    fun fn ->
      try
        MapString.find fn mp
      with Not_found ->
        failwithf (f_ "No default filename for file %s.") fn

let generator =
  let fn_enable fn sync =
    let enable =
      new_field
        OASISPackage.schema
        all_id
        fn
        ~default:true
        boolean
        (fun () ->
           Printf.sprintf (f_ "Enable %s file generation.") fn)
        pivot_data
        (fun t t' -> (sync t t') <> None)
    in
    let fn =
      new_field
        OASISPackage.schema
        all_id
        (fn^"Filename")
        ~default:(default_filenames fn)
        string_not_empty
        (fun () ->
           Printf.sprintf (f_ "Real filename to use for file %s.") fn)
        pivot_data
        (fun t t' ->
           match sync t t' with
             | Some fn -> fn
             | None -> raise Not_printable)
    in
      fun data ->
        if enable data then
          Some (fn data)
        else
          None
  in

  let readme =
    fn_enable "README" (fun _ t -> t.readme)
  in

  let install =
    fn_enable "INSTALL" (fun _ t -> t.install)
  in

  let authors =
    fn_enable "AUTHORS" (fun _ t -> t.authors)
  in

    fun data ->
      {
        readme  = readme data;
        install = install data;
        authors = authors data;
      }


module SetSection =
  Set.Make
    (struct type t = section let compare = compare end)


let main ctxt pkg =
  let data = pkg.schema_data in

  (** All sections that contains a build_section *)
  let all_build_sections_set =
    let all_build_sections =
      List.rev
        (List.fold_left
           (fun acc ->
              function
                | Library (_, bs, _)
                | Object (_, bs, _)
                | Executable (_, bs, _) as sct ->
                    (sct, bs) :: acc
                | SrcRepo _ | Flag _ | Test _ | Doc _ ->
                    acc)
           []
           pkg.sections)
    in
      List.fold_left
        (fun acc e -> SetSection.add e acc)
        SetSection.empty
        (List.rev_map fst all_build_sections)
  in

  let pp_print_sections =
    (* If a tool/library applies to all sections that can handle a build_depends
       or build_tools we can say it is a general depends. In this case, return
       is None, otherwise "Some sections".
     *)
    fun fmt sections ->
      let is_all_build_sections, sections =
        if SetSection.subset all_build_sections_set sections then
          true, SetSection.diff sections all_build_sections_set
        else
          false, sections
      in
        if SetSection.is_empty sections then
          ()
        else
          fprintf fmt " for %t%a"
            (fun fmt ->
               if is_all_build_sections then
                 fprintf fmt "all,@ ")
            (pp_print_list
               (fun fmt sct ->
                  fprintf fmt "%s" (OASISSection.string_of_section sct))
               ",@ ")
            (SetSection.elements sections)
  in

  let pp_print_ver_opt fmt =
    may
      (fun ver_cmp ->
         fprintf fmt " (%a)"
           pp_print_string
           (OASISVersion.string_of_comparator
              (OASISVersion.comparator_reduce ver_cmp)))
  in

  let depends =
    let merge_package_section lst (new_pkg, new_sections) =
      try
        let old_pkg, new_pkg, old_sections =
          match new_pkg with
            | LTool _ as tool ->
                tool,
                tool,
                List.assoc tool lst

            | LFindlibPackage (nm1, ver_opt1) ->
                begin
                  let res_opt =
                    List.fold_left
                      (fun acc pkg ->
                         match acc, pkg with
                           | None,
                             ((LFindlibPackage (nm2, ver_opt2))
                                as old_pkg,
                              sections) ->
                               if nm1 = nm2 then
                                 Some
                                   (old_pkg,
                                    LFindlibPackage
                                      (nm1,
                                       merge_version_opt
                                         ver_opt1
                                         ver_opt2),
                                    sections)
                               else
                                 acc
                           | _, _ ->
                               acc)
                      None
                      lst
                  in
                    match res_opt with
                      | Some res ->
                          res
                      | None ->
                          raise Not_found
                end
        in
        let lst =
          try
            List.remove_assoc old_pkg lst
          with Not_found ->
            lst
        in
          (new_pkg, SetSection.union new_sections old_sections) :: lst

      with Not_found ->
        (new_pkg, new_sections) :: lst
    in

    let add_build_tools ssection =
       List.fold_left
         (fun lst ->
            function
              | ExternalTool tool ->
                  (package_of_tool tool, ssection) :: lst
              | InternalExecutable _ ->
                  lst)
    in

    let split_package_section =
      List.fold_left
        (fun lst ->
           function
             | Library (_, bs, _)
             | Object (_, bs, _)
             | Executable (_, bs, _) as section ->
                 begin
                   let ssection =
                     SetSection.singleton section
                   in
                   let lst =
                     (* Add build_depends *)
                     List.fold_left
                       (fun lst ->
                          function
                            | FindlibPackage (fndlb_nm, ver_opt) ->
                                let fndlb_root =
                                  match (OASISString.nsplit fndlb_nm '.') with
                                    | hd :: _ -> hd
                                    | _ -> fndlb_nm
                                in
                                  (package_of_library fndlb_root ver_opt,
                                   ssection) :: lst
                            | InternalLibrary _ ->
                                lst)
                       lst
                       bs.bs_build_depends
                   in
                     (* Add build_tools *)
                     add_build_tools
                       ssection
                       lst
                       bs.bs_build_tools
                 end
             | Test (_, {test_tools = build_tools})
             | Doc (_, {doc_build_tools = build_tools}) as section ->
                 add_build_tools
                   (SetSection.singleton section)
                   lst
                   build_tools
             | Flag _ | SrcRepo _ ->
                 lst)

        (* Basic dependencies *)
        ([
          LFindlibPackage ("findlib", pkg.findlib_version),
          all_build_sections_set;

          LFindlibPackage ("ocaml", pkg.ocaml_version),
          all_build_sections_set]
        @
         (if OASISFeatures.package_test
               OASISFeatures.dynrun_for_release pkg then
            [
              LFindlibPackage ("oasis",
                               Some (OASISVersion.VGreaterEqual
                                       pkg.oasis_version)),
              all_build_sections_set
            ]
          else
            []))


        (* Go through all sections *)
        pkg.sections
    in

      (* Merge everything *)
      List.fold_left
        merge_package_section
        []
        split_package_section
  in

  let add_file ctxt (fn_opt, important, ppf) =
    match fn_opt with
      | Some unix_fn ->
          begin
            let rec remove_eol_eof str =
              List.fold_left
                (fun str eol ->
                   try
                     remove_eol_eof (OASISString.strip_ends_with ~what:eol str)
                   with Not_found ->
                     str)
                str ["\n"; "\r"]
            in
            let content =
              ppf str_formatter;
              (* Trim problematic 2x newline at end of last para, because we
               * don't know if there will a following para.
               *)
              remove_eol_eof (flush_str_formatter ())
            in
            let comment =
              if OASISFeatures.package_test markdown pkg then
                comment_markdown
              else
                comment_ml
            in
              add_file
                {(template_make unix_fn comment
                    []
                    [
                      (* One line before, one line after to avoid mixing content
                       * with comment formatting (it causes e.g. pandoc to think
                       * this is a paragraph.
                       *)
                      "\n"^content^"\n"
                    ]
                    []) with
                     important = important}
                ctxt
          end

      | None ->
          ctxt
  in

  let t = generator data in

  let fix_ext fn real_fn_opt =
    match real_fn_opt with
      | Some real_fn ->
          if OASISFeatures.package_test markdown pkg &&
             real_fn = default_filenames fn then
            Some (fn^".md")
          else
            Some real_fn
      | None -> None
  in

  let t =
    {
      readme = fix_ext "README" t.readme;
      install = fix_ext "INSTALL" t.install;
      authors = fix_ext "AUTHORS" t.authors;
    }
  in

    List.fold_left
      add_file
      ctxt
      [
        (* Generate README.txt *)
        t.readme,
        false,
        (fun fmt ->
           pp_open_vbox fmt 0;
           pp_print_titlef fmt 1 "%s - %s" pkg.name pkg.synopsis;

           may
             (fun descr ->
                OASISText.pp_print fmt descr;
                pp_print_newline fmt ();
                pp_print_newline fmt ())
             pkg.description;

           may
             (fun fn ->
                pp_print_paraf fmt
                  "See the file [%s](%s) for building and installation \
                   instructions." fn fn)
             t.install;

           may (pp_print_paraf fmt "[Home page](%s)") pkg.homepage;

           pp_print_title fmt 2 "Copyright and license";

           List.iter
             (fun str ->
                pp_print_string fmt str;
                pp_print_newline fmt ())
             pkg.copyrights;
           if pkg.copyrights <> [] then
             pp_print_newline fmt ();

           pp_print_para fmt
             (OASISLicense.legal_disclaimer pkg.name pkg.license);

           may
             (fun fn ->
                pp_print_paraf fmt "See [%s](%s) for more information." fn fn)
             pkg.license_file;

           pp_close_box fmt ();
        );

        (* Generate INSTALL.txt *)
        t.install,
        (OASISFeatures.package_test OASISFeatures.dynrun_for_release pkg),
        (fun fmt ->
           pp_open_vbox fmt 0;
           fprintf fmt
             "@[This is the INSTALL file for the %s distribution.@]@,@,"
             pkg.name;

           pp_print_para fmt
             "This package uses OASIS to generate its build system. \
              See section OASIS for full information.";

           pp_print_title fmt 1 "Dependencies";
           fprintf fmt "@[In order to compile this package, you will need:@]@,";
           pp_open_vbox fmt 0;
           pp_print_cut fmt ();
           pp_print_list
             (fun fmt (pkg, sections) ->
                fprintf fmt "* @[%a%a@]"
                  (fun fmt ->
                     function
                       | LTool s ->
                           pp_print_string fmt s
                       | LFindlibPackage (nm, ver_opt) ->
                           fprintf fmt "%s%a"
                             nm
                             pp_print_ver_opt ver_opt)
                  pkg
                  pp_print_sections sections)
             "@,"
             fmt
             depends;

           pp_close_box fmt ();
           pp_print_cut fmt ();

           pp_print_string fmt StdFilesData.install;
           pp_close_box fmt ());

        (* Generate AUTHORS.txt *)
        t.authors,
        false,
        (fun fmt ->
           pp_open_vbox fmt 0;
           fprintf fmt "@[Authors of %s:@]@," pkg.name;
           pp_print_cut fmt ();
           pp_print_list
             (fun fmt str -> pp_print_string fmt ("* "^str))
             "@,"
             fmt
             pkg.OASISTypes.authors;

           if pkg.maintainers <> [] then begin
             pp_print_cut2 fmt ();
             fprintf fmt "@[Current maintainers of %s:@]@," pkg.name;
             pp_print_cut fmt ();
             pp_print_list
               (fun fmt str -> pp_print_string fmt ("* "^str))
               "@,"
               fmt
               pkg.maintainers
           end;

           pp_close_box fmt ());
      ]


let init () =
  register_help plugin
    {(help_default StdFilesData.readme_template_mkd) with
         help_order = 50};
  Extra.register_act self_id main;
  register_generator_package all_id pivot_data generator