From b4b95479d889032dc31e22febdb6e6647918421f Mon Sep 17 00:00:00 2001 From: Andrey Popp Date: Sat, 8 Jun 2024 18:37:17 +0400 Subject: [PATCH] Modify Makefile/build.ml to install cmdliner helper --- Makefile | 19 +++++++++++-------- build.ml | 41 ++++++++++++++++++++++++++++++++--------- cmdliner.opam | 2 +- 3 files changed, 44 insertions(+), 18 deletions(-) diff --git a/Makefile b/Makefile index a3921d7..896c7ea 100644 --- a/Makefile +++ b/Makefile @@ -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) @@ -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 @@ -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 diff --git a/build.ml b/build.ml index e0d9a3c..2911ece 100755 --- a/build.ml +++ b/build.ml @@ -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 *) ] @@ -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 @@ -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"]) @@ -134,9 +154,11 @@ 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; @@ -144,9 +166,10 @@ let in_build_dir f = 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 -> diff --git a/cmdliner.opam b/cmdliner.opam index ac7b447..cbee459 100644 --- a/cmdliner.opam +++ b/cmdliner.opam @@ -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"