Source

ocaml-shell-dsl / main.ml

Full commit
open Printf;

module S = Shell_dsl;

(**)

open S;

value ( </> ) = Filename.concat;

value base_dir = Sys.getcwd ();
value dep_dir = base_dir </> "_dep";
value tmp_dir = dep_dir </> "tmp";
value bin_dir = dep_dir </> "bin";
value lib_dir = dep_dir </> "lib";
value stublibs_dir = lib_dir </> "stublibs";
value etc_dir = dep_dir </> "etc";
value env_sh = dep_dir </> "env.sh";

value mkdir_p lst =
  S.cmd (S.of_list [s "mkdir"; s "-p" :: List.map s lst])
;

value create_dirs =
  mkdir_p
    [
      base_dir; dep_dir; tmp_dir; bin_dir; lib_dir; etc_dir; stublibs_dir
    ]
;

value seq_list lst = List.fold_right (fun cmd acc -> seq cmd acc) lst nop
;

value env =
          [ (`Prepend, "OCAMLPATH", lib_dir)
          ; (`Prepend, "PATH", bin_dir)
          ; (`Set, "OCAMLFIND_DESTDIR", lib_dir)
          ; (`Prepend, "CAML_LD_LIBRARY_PATH", stublibs_dir)
          ; (`Set, "OCAMLFIND_LDCONF", "ignore")
          ]
;

value ( @> ) f g = fun x -> g (f x)
;

value rec with_list
 : ! 'a 'b . ('a -> ('b -> 'b)) -> list 'a -> 'b -> 'b
 = fun func lst cont ->
  match lst with
  [ [] -> cont
  | [h :: t] -> with_list func t ( (func h) cont )
  ]
;

value with_env cont =
  with_list
    (fun
     [ (`Prepend, n, v) -> with_env_prepend n (s v)
     | (`Set, n, v) -> with_env_set n (s v)
     ]
    )
    env
    cont
;

value ( >> ) = seq;
value ( & ) f x = f x;
value ( @ ) = S.append;

value makefile = object
  method install ~source_dir ~flags ~targets ~patches =
    let () = ignore patches in
    create_dirs >>
    print (S.of_list [s "Starting Makefile build"]) >>
    with_chdir (s source_dir)
    (let do_configure =
       if_ (is_file & s "configure")
         (let_
            ( (* maybe we should always pass --prefix,
                 not only for oasis projects *)
             if_ (is_file & s "_oasis")
               ((of_list [s "--prefix"; s dep_dir]) @ flags)
               flags
            )
            (fun flags ->
               let_
                 (if_ (is_exec & s "configure")
                    (of_list [s "./configure"])
                    (of_list [s "sh" ; s "./configure"])
                 )
                 (fun run_configure ->
                    cmd (v run_configure @ v flags)
                 )
            )
         )
         nop
     and do_make =
       let_
         (getenv ~default:"make" "MAKE")
         (fun make ->
            cmd (of_list [v make] @ targets) >>
            cmd (of_list [v make; s "install"])
         )
     in
     with_env
     (do_configure >> do_make)
    )
  ;
end
;


value () =
  let t : S.t res =
    makefile#install ~source_dir:"/tmp/abc"
      ~flags:S.(of_list [])
      ~targets:S.(of_list [s"all"; s"install"])
      ~patches:()
  in
  print_string (S.script t)
;