From 4e48fd2efd95e67e373708b010bf817f966ac7cb Mon Sep 17 00:00:00 2001 From: Nick Gasson Date: Sat, 9 Jul 2022 13:03:57 +0100 Subject: [PATCH] Add --dest argument to --install --- contrib/functions.sh | 2 +- nvc.1 | 13 +++++++++++++ src/nvc.c | 19 +++++++++++++++---- src/util.c | 16 +++++++++++++++- src/util.h | 1 + 5 files changed, 45 insertions(+), 6 deletions(-) diff --git a/contrib/functions.sh b/contrib/functions.sh index 53c720f0..a1d38bb6 100644 --- a/contrib/functions.sh +++ b/contrib/functions.sh @@ -8,7 +8,7 @@ _safe () { _nvc () { local _work=${WORK:-work} - local _dest=${DEST:-$HOME/.nvc/lib} + local _dest=${NVC_INSTALL_DEST:-$HOME/.nvc/lib} local _opts="--std=${STD:-1993} --work=$_dest/$_work -L$_dest $GLOBAL_OPTS" [ -d $_dest ] || _safe mkdir -p $_dest _safe ${NVC:-nvc} $_opts $* diff --git a/nvc.1 b/nvc.1 index 9060a91f..72251d7f 100644 --- a/nvc.1 +++ b/nvc.1 @@ -333,6 +333,19 @@ The generated makefile will work with any POSIX compliant make. Otherwise the output may use extensions specific to GNU make. .El .\" ------------------------------------------------------------ +.\" Install options +.\" ------------------------------------------------------------ +.Ss Install options +.Bl -tag -width Ds +.\" --dest +.It Fl -dest Ar dir +Compile libraries into directory +.Ar dir +instead of the default +.Ql $HOME/.nvc/lib . +.\" --posix +.El +.\" ------------------------------------------------------------ .\" Libraries .\" ------------------------------------------------------------ .Sh LIBRARIES diff --git a/src/nvc.c b/src/nvc.c index 812df187..d4b61e74 100644 --- a/src/nvc.c +++ b/src/nvc.c @@ -86,9 +86,9 @@ static int scan_cmd(int start, int argc, char **argv) static void bad_option(const char *what, char **argv) { if (optopt == 0) - fatal("unrecognised %s option %s", what, argv[optind - 1]); + fatal("unrecognised %s option $bold$%s$$", what, argv[optind - 1]); else - fatal("unrecognised %s option -%c", what, optopt); + fatal("unrecognised %s option $bold$-%c$$", what, optopt); } static int analyse(int argc, char **argv) @@ -663,6 +663,7 @@ static void list_packages(void) static int install_cmd(int argc, char **argv) { static struct option long_options[] = { + { "dest", required_argument, 0, 'd' }, { 0, 0, 0, 0 } }; @@ -675,7 +676,10 @@ static int install_cmd(int argc, char **argv) // Set a flag break; case '?': - bad_option("init", argv); + bad_option("install", argv); + break; + case 'd': + setenv("NVC_INSTALL_DEST", optarg , 1); break; } } @@ -686,8 +690,12 @@ static int install_cmd(int argc, char **argv) return EXIT_FAILURE; } + LOCAL_TEXT_BUF tb = tb_new(); + if (get_exe_path(tb)) + setenv("NVC", tb_get(tb), 1); + for (int i = optind; i < next_cmd; i++) { - LOCAL_TEXT_BUF tb = tb_new(); + tb_rewind(tb); get_libexec_dir(tb); tb_printf(tb, "/install-%s.sh", argv[i]); @@ -897,6 +905,9 @@ static void usage(void) "Make options:\n" " --deps-only\tOutput dependencies without actions\n" " --posix\t\tStrictly POSIX compliant makefile\n" + "\n" + "Install options:\n" + " --dest=DIR\t\tCompile libraries into directory DEST\n" "\n", PACKAGE, opt_get_int(OPT_STOP_DELTA)); diff --git a/src/util.c b/src/util.c index 72aed5ba..08e8bae6 100644 --- a/src/util.c +++ b/src/util.c @@ -1287,8 +1287,9 @@ void tb_catn(text_buf_t *tb, const char *str, size_t nchars) tb->buf = xrealloc(tb->buf, tb->alloc); } - memcpy(tb->buf + tb->len, str, nchars + 1); + memcpy(tb->buf + tb->len, str, nchars); tb->len += nchars; + tb->buf[tb->len] = '\0'; } void tb_cat(text_buf_t *tb, const char *str) @@ -1709,6 +1710,19 @@ char *search_path(const char *name) return xstrdup(name); } +bool get_exe_path(text_buf_t *tb) +{ +#if defined __linux__ + char buf[PATH_MAX]; + ssize_t nchars = readlink("/proc/self/exe", buf, sizeof(buf)); + if (nchars > 0) { // Does not append '\0' + tb_catn(tb, buf, nchars); + return true; + } +#endif + return false; +} + void get_libexec_dir(text_buf_t *tb) { #if defined DEBUG && defined __linux__ && 0 diff --git a/src/util.h b/src/util.h index c385f719..3bdfd30f 100644 --- a/src/util.h +++ b/src/util.h @@ -245,6 +245,7 @@ void unmap_file(void *ptr, size_t size); void make_dir(const char *path); char *search_path(const char *name); void get_libexec_dir(text_buf_t *tb); +bool get_exe_path(text_buf_t *tb); struct cpu_state; void capture_registers(struct cpu_state *cpu); -- 2.39.2