Skip to content

Commit

Permalink
Modify Makefile/build.ml to install cmdliner helper
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrey Popp committed Jun 8, 2024
1 parent 5fd865b commit b4b9547
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 18 deletions.
19 changes: 11 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
-include $(shell ocamlc -where)/Makefile.config

PREFIX=/usr
BINDIR=$(DESTDIR)$(PREFIX)/bin
LIBDIR=$(DESTDIR)$(PREFIX)/lib/ocaml/cmdliner
DOCDIR=$(DESTDIR)$(PREFIX)/share/doc/cmdliner
NATIVE=$(shell ocamlopt -version > /dev/null 2>&1 && echo true)
Expand All @@ -21,7 +22,7 @@ NATIVE=$(shell ocamlopt -version > /dev/null 2>&1 && echo true)

INSTALL=install
B=_build
BASE=$(B)/cmdliner
BASE=$(B)/src/cmdliner

ifeq ($(NATIVE),true)
BUILD-TARGETS=build-byte build-native
Expand Down Expand Up @@ -53,27 +54,29 @@ build-byte:

build-native:
ocaml build.ml cmxa
ocaml build.ml exe

build-native-dynlink:
ocaml build.ml cmxs

create-libdir:
$(INSTALL) -d "$(LIBDIR)"
prepare-prefix:
$(INSTALL) -d "$(BINDIR)" "$(LIBDIR)"

install-common: create-libdir
install-common: prepare-prefix
$(INSTALL) pkg/META $(BASE).mli $(BASE).cmi $(BASE).cmti "$(LIBDIR)"
$(INSTALL) cmdliner.opam "$(LIBDIR)/opam"

install-byte: create-libdir
install-byte: prepare-prefix
$(INSTALL) $(BASE).cma "$(LIBDIR)"

install-native: create-libdir
install-native: prepare-prefix
$(INSTALL) $(BASE).cmxa $(BASE)$(EXT_LIB) $(wildcard $(B)/cmdliner*.cmx) \
"$(LIBDIR)"
$(INSTALL) -m 755 $(B)/bin/cmdliner.exe "$(BINDIR)/cmdliner"

install-native-dynlink: create-libdir
install-native-dynlink: prepare-prefix
$(INSTALL) $(BASE).cmxs "$(LIBDIR)"

.PHONY: all install install-doc clean build-byte build-native \
build-native-dynlink create-libdir install-common install-byte \
build-native-dynlink prepare-prefix install-common install-byte \
install-native install-dynlink
41 changes: 32 additions & 9 deletions build.ml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@
(* Usage: ocaml build.ml [cma|cmxa|cmxs|clean] *)

let root_dir = Sys.getcwd ()
let build_dir = "_build"
let root_build_dir = Filename.concat root_dir "_build"
let src_dir = "src"

type unit = Lib | Bin

let unit_dir = function Lib -> "src" | Bin -> "bin"
let build_dir u = Filename.concat root_build_dir (unit_dir u)

let base_ocaml_opts =
[ "-g"; "-bin-annot";
"-safe-string"; (* Remove once we require >= 4.06 *) ]
Expand Down Expand Up @@ -89,18 +94,26 @@ let read_cmd args =

(* Create and delete directories *)

let mkdir dir =
let rec mkdir dir =
let parent = Filename.dirname dir in
if String.equal dir parent then ()
else mkdir (Filename.dirname dir);
try match Sys.file_exists dir with
| true -> ()
| false -> run_cmd ["mkdir"; dir]
with
| Sys_error e -> err "%s: %s" dir e

let rmdir dir =
let rec rmdir dir =
try match Sys.file_exists dir with
| false -> ()
| true ->
let rm f = Sys.remove (fpath ~dir f) in
let rm f =
let p = fpath ~dir f in
if Sys.is_directory p
then rmdir p
else Sys.remove (fpath ~dir f)
in
Array.iter rm (Sys.readdir dir);
run_cmd ["rmdir"; dir]
with
Expand All @@ -125,6 +138,13 @@ let sort_srcs srcs =

let common srcs = base_ocaml_opts @ sort_srcs srcs

let exe src =
let lib = build_dir Lib in
["-I"; lib; "cmdliner.cmxa"] @ common src

let build_exe srcs =
run_cmd ([ocamlopt ()] @ exe srcs @ ["-o"; "cmdliner.exe"])

let build_cma srcs =
run_cmd ([ocamlc ()] @ common srcs @ ["-a"; "-o"; "cmdliner.cma"])

Expand All @@ -134,19 +154,22 @@ let build_cmxa srcs =
let build_cmxs srcs =
run_cmd ([ocamlopt ()] @ common srcs @ ["-shared"; "-o"; "cmdliner.cmxs"])

let clean () = rmdir build_dir
let clean () = rmdir root_build_dir

let in_build_dir f =
let in_build_dir u f =
let src_dir = unit_dir u in
let build_dir = build_dir u in
let srcs = ml_srcs src_dir in
let cp src = cp (fpath ~dir:src_dir src) (fpath ~dir:build_dir src) in
mkdir build_dir;
List.iter cp srcs;
Sys.chdir build_dir; f srcs; Sys.chdir root_dir

let main () = match Array.to_list Sys.argv with
| _ :: [ "cma" ] -> in_build_dir build_cma
| _ :: [ "cmxa" ] -> in_build_dir build_cmxa
| _ :: [ "cmxs" ] -> in_build_dir build_cmxs
| _ :: [ "exe" ] -> in_build_dir Bin build_exe
| _ :: [ "cma" ] -> in_build_dir Lib build_cma
| _ :: [ "cmxa" ] -> in_build_dir Lib build_cmxa
| _ :: [ "cmxs" ] -> in_build_dir Lib build_cmxs
| _ :: [ "clean" ] -> clean ()
| [] | [_] -> err "Missing argument: cma, cmxa, cmxs or clean\n";
| cmd :: args ->
Expand Down
2 changes: 1 addition & 1 deletion cmdliner.opam
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ depends: [
]
build: [make "all" "PREFIX=%{prefix}%"]
install: [
[make "install" "LIBDIR=%{_:lib}%" "DOCDIR=%{_:doc}%"]
[make "install" "BINDIR=%{_:bin}%" "LIBDIR=%{_:lib}%" "DOCDIR=%{_:doc}%"]
[make "install-doc" "LIBDIR=%{_:lib}%" "DOCDIR=%{_:doc}%"]
]
dev-repo: "git+https://erratique.ch/repos/cmdliner.git"

0 comments on commit b4b9547

Please sign in to comment.