From 853b33b67caca3607d40e301a054d8bbecd26c1d Mon Sep 17 00:00:00 2001 From: Calvin Rose Date: Sun, 14 Jun 2020 14:04:23 -0500 Subject: [PATCH] On nix platforms, patch jpm with path information. This means we no longer need to guess paths after install. Custom directory layouts can now be better supported at install time without need for environment variables. --- Makefile | 8 ++++++-- jpm | 18 +++++++++++++----- meson.build | 11 ++++++++++- tools/patch-jpm.janet | 33 +++++++++++++++++++++++++++++++++ 4 files changed, 62 insertions(+), 8 deletions(-) create mode 100644 tools/patch-jpm.janet diff --git a/Makefile b/Makefile index 662a83ca..fbbe91d2 100644 --- a/Makefile +++ b/Makefile @@ -233,6 +233,10 @@ build/doc.html: $(JANET_TARGET) tools/gendoc.janet ##### Installation ##### ######################## +build/jpm: jpm $(JANET_TARGET) + $(JANET_TARGET) tools/patch-jpm.janet jpm build/jpm "--libpath=$(LIBDIR)" "--headerpath=$(INCLUDEDIR)/janet" "--binpath=$(BINDIR)" + chmod +x build/jpm + .INTERMEDIATE: build/janet.pc build/janet.pc: $(JANET_TARGET) echo 'prefix=$(PREFIX)' > $@ @@ -248,7 +252,7 @@ build/janet.pc: $(JANET_TARGET) echo 'Libs: -L$${libdir} -ljanet' >> $@ echo 'Libs.private: $(CLIBS)' >> $@ -install: $(JANET_TARGET) build/janet.pc +install: $(JANET_TARGET) build/janet.pc build/jpm mkdir -p '$(DESTDIR)$(BINDIR)' cp $(JANET_TARGET) '$(DESTDIR)$(BINDIR)/janet' mkdir -p '$(DESTDIR)$(INCLUDEDIR)/janet' @@ -259,7 +263,7 @@ install: $(JANET_TARGET) build/janet.pc cp $(JANET_STATIC_LIBRARY) '$(DESTDIR)$(LIBDIR)/libjanet.a' ln -sf $(SONAME) '$(DESTDIR)$(LIBDIR)/libjanet.so' ln -sf libjanet.so.$(shell $(JANET_TARGET) -e '(print janet/version)') $(DESTDIR)$(LIBDIR)/$(SONAME) - cp -rf jpm '$(DESTDIR)$(BINDIR)' + cp -rf build/jpm '$(DESTDIR)$(BINDIR)' mkdir -p '$(DESTDIR)$(MANPATH)' cp janet.1 '$(DESTDIR)$(MANPATH)' cp jpm.1 '$(DESTDIR)$(MANPATH)' diff --git a/jpm b/jpm index 926d582f..72790f72 100755 --- a/jpm +++ b/jpm @@ -26,21 +26,29 @@ (def i (last (string/find-all sep exe))) (slice exe 0 i))) -(def JANET_MODPATH (or (os/getenv "JANET_MODPATH") (dyn :syspath))) +###START### +# Overriden on some installs. +(def- install-paths + {:headerpath (os/realpath (string exe-dir "/../include/janet")) + :libpath (os/realpath (string exe-dir "/../lib")) + :binpath exe-dir}) +###END### # Default based on janet binary location (def JANET_HEADERPATH (or (os/getenv "JANET_HEADERPATH") - (string exe-dir "/../include/janet"))) + (get install-paths :headerpath))) (def JANET_LIBPATH (or (os/getenv "JANET_LIBPATH") - (string exe-dir "/../lib"))) - + (get install-paths :libpath))) # We want setting JANET_PATH to contain installed binaries. However, it is convenient # to have globally installed binaries got to the same place as jpm itself, which is on # the $PATH. (def JANET_BINPATH (or (os/getenv "JANET_BINPATH") (if-let [mp (os/getenv "JANET_MODPATH")] (string mp "/bin")) (if-let [mp (os/getenv "JANET_PATH")] (string mp "/bin")) - exe-dir)) + (get install-paths :binpath))) + +# modpath should only be derived from the syspath being used or an environment variable. +(def JANET_MODPATH (or (os/getenv "JANET_MODPATH") (dyn :syspath))) # # Utilities diff --git a/meson.build b/meson.build index ee68cb56..7b545801 100644 --- a/meson.build +++ b/meson.build @@ -247,5 +247,14 @@ pkg.generate(libjanet, install_man('janet.1') install_man('jpm.1') install_headers(['src/include/janet.h', jconf], subdir: 'janet') -install_data(sources : ['jpm'], install_dir : get_option('bindir')) +patched_jpm = custom_target('patched-jpm', + input : ['tools/patch-jpm.janet', 'jpm'], + install : true, + install_dir : get_option('bindir'), + build_by_default : true, + output : ['jpm'], + command : [janet_nativeclient, '@INPUT@', '@OUTPUT@', + '--binpath=' + join_paths(get_option('prefix'), get_option('bindir')), + '--libpath=' + join_paths(get_option('prefix'), get_option('libdir'), 'janet'), + '--headerpath=' + join_paths(get_option('prefix'), get_option('includedir'))]) install_data(sources : ['tools/.keep'], install_dir : join_paths(get_option('libdir'), 'janet')) diff --git a/tools/patch-jpm.janet b/tools/patch-jpm.janet new file mode 100644 index 00000000..741280fd --- /dev/null +++ b/tools/patch-jpm.janet @@ -0,0 +1,33 @@ +# Patch jpm to have the correct paths for the current install. +# usage: janet patch-jpm.janet output --libdir=/usr/local/lib/x64-linux/ --binpath + +(def- argpeg + (peg/compile + '(* "--" '(to "=") "=" '(any 1)))) + +(def- args (tuple/slice (dyn :args) 3)) +(def- len (length args)) +(var i :private 0) + +(def install-paths @{}) + +# Get flags +(each a args + (if-let [m (peg/match argpeg a)] + (let [[key value] m] + (put install-paths (keyword key) value)))) + +(def- replace-peg + (peg/compile + ~(% (* '(to "###START###") + (constant ,(string/format "# Inserted by tools/patch-jpm.janet\n(def install-paths %j)" install-paths)) + (thru "###END###") + '(any 1))))) + +(def source (slurp ((dyn :args) 1))) +(def newsource (0 (peg/match replace-peg source))) + +(spit ((dyn :args) 2) newsource) + +(unless (= :windows (os/which)) + (os/shell (string `chmod +x "` ((dyn :args) 2) `"`)))