~jprotopopov/public-inbox

kefir : Treat zero-length environment variables as unset v2 APPROVED

remph: 1
 Treat zero-length environment variables as unset

 8 files changed, 37 insertions(+), 29 deletions(-)
Export patchset (mbox)
How do I use this?

Copy & paste the following snippet into your terminal to import this patchset into git:

curl -s https://lists.sr.ht/~jprotopopov/public-inbox/patches/57368/mbox | git am -3
Learn more about email & git

[PATCH kefir v2] Treat zero-length environment variables as unset Export this patch

Idiomatically, environment variables set to '' are treated as unset, as they
are in the shell. This patch fixes behaviour a user may not expect:
* `make CC=kefir KEFIR_LD=': kefir tries to exec an empty string
* `TMPDIR= kefir ...': kefir writes temporary files to current working
  directory, rather than using a default TMPDIR

Also:
* Some tests specified a nonexistent KEFIR_RTINC, which realpath(1) turned
  into an empty string, which kefir previously would have interpreted as
  current directory. Corrected these for the patch to pass tests
* manpage typo
---
 docs/man/kefir.1                              |  2 +-
 source/driver/externals.c                     | 48 +++++++++++--------
 source/platform/tempfile.c                    |  4 +-
 source/tests/external/c-testsuite/Makefile.mk |  2 +-
 source/tests/external/duktape/Makefile.mk     |  2 +-
 source/tests/external/gcc-torture/Makefile.mk |  2 +-
 source/tests/external/lua/Makefile.mk         |  2 +-
 source/tests/external/nginx/Makefile.mk       |  4 +-
 8 files changed, 37 insertions(+), 29 deletions(-)

diff --git a/docs/man/kefir.1 b/docs/man/kefir.1
index 949f991b..63e8a98a 100644
--- a/docs/man/kefir.1
+++ b/docs/man/kefir.1
@@ -628,7 +628,7 @@ Override kefir temporary directory
Normally
.Nm
exits with 0 exit code. In case of any errors in any of compilation stages, all further compilation is aborted and
non-zero exit code retruned.
non-zero exit code returned.
.\"
.Sh STANDARDS
.Nm
diff --git a/source/driver/externals.c b/source/driver/externals.c
index 764cd151..3f11e0de 100644
--- a/source/driver/externals.c
+++ b/source/driver/externals.c
@@ -28,6 +28,14 @@ static void set_if_null(const char **target, const char *value) {
    }
}

static const char *getenv_nonzero(const char *envvar) {
    const char *value = getenv(envvar);
    if (value != NULL && *value == '\0') {
        value = NULL;
    }
    return value;
}

kefir_result_t kefir_driver_external_resources_init_from_env(struct kefir_mem *mem,
                                                             struct kefir_driver_external_resources *externals,
                                                             struct kefir_tempfile_manager *tmpmgr) {
@@ -41,9 +49,9 @@ kefir_result_t kefir_driver_external_resources_init_from_env(struct kefir_mem *m
    externals->default_target = getenv("KEFIR_TARGET");

    externals->assembler_path_explicit = false;
    externals->assembler_path = getenv("KEFIR_AS");
    externals->assembler_path = getenv_nonzero("KEFIR_AS");
    if (externals->assembler_path == NULL) {
        externals->assembler_path = getenv("AS");
        externals->assembler_path = getenv_nonzero("AS");
    }
    if (externals->assembler_path != NULL) {
        externals->assembler_path_explicit = true;
@@ -55,9 +63,9 @@ kefir_result_t kefir_driver_external_resources_init_from_env(struct kefir_mem *m
        externals->assembler_path = "as";
    }

    externals->linker_path = getenv("KEFIR_LD");
    externals->linker_path = getenv_nonzero("KEFIR_LD");
    if (externals->linker_path == NULL) {
        externals->linker_path = getenv("LD");
        externals->linker_path = getenv_nonzero("LD");
    }
#ifdef KEFIR_CONFIG_HOST_LD
    set_if_null(&externals->linker_path, KEFIR_CONFIG_HOST_LD);
@@ -66,22 +74,22 @@ kefir_result_t kefir_driver_external_resources_init_from_env(struct kefir_mem *m
        externals->linker_path = "ld";
    }

    externals->runtime_include = getenv("KEFIR_RTINC");
    externals->musl.include_path = getenv("KEFIR_MUSL_INCLUDE");
    externals->musl.library_path = getenv("KEFIR_MUSL_LIB");
    externals->musl.dynamic_linker = getenv("KEFIR_MUSL_DYNAMIC_LINKER");
    externals->gnu.include_path = getenv("KEFIR_GNU_INCLUDE");
    externals->gnu.library_path = getenv("KEFIR_GNU_LIB");
    externals->gnu.dynamic_linker = getenv("KEFIR_GNU_DYNAMIC_LINKER");
    externals->freebsd.include_path = getenv("KEFIR_FREEBSD_INCLUDE");
    externals->freebsd.library_path = getenv("KEFIR_FREEBSD_LIB");
    externals->freebsd.dynamic_linker = getenv("KEFIR_FREEBSD_DYNAMIC_LINKER");
    externals->openbsd.include_path = getenv("KEFIR_OPENBSD_INCLUDE");
    externals->openbsd.library_path = getenv("KEFIR_OPENBSD_LIB");
    externals->openbsd.dynamic_linker = getenv("KEFIR_OPENBSD_DYNAMIC_LINKER");
    externals->netbsd.include_path = getenv("KEFIR_NETBSD_INCLUDE");
    externals->netbsd.library_path = getenv("KEFIR_NETBSD_LIB");
    externals->netbsd.dynamic_linker = getenv("KEFIR_NETBSD_DYNAMIC_LINKER");
    externals->runtime_include = getenv_nonzero("KEFIR_RTINC");
    externals->musl.include_path = getenv_nonzero("KEFIR_MUSL_INCLUDE");
    externals->musl.library_path = getenv_nonzero("KEFIR_MUSL_LIB");
    externals->musl.dynamic_linker = getenv_nonzero("KEFIR_MUSL_DYNAMIC_LINKER");
    externals->gnu.include_path = getenv_nonzero("KEFIR_GNU_INCLUDE");
    externals->gnu.library_path = getenv_nonzero("KEFIR_GNU_LIB");
    externals->gnu.dynamic_linker = getenv_nonzero("KEFIR_GNU_DYNAMIC_LINKER");
    externals->freebsd.include_path = getenv_nonzero("KEFIR_FREEBSD_INCLUDE");
    externals->freebsd.library_path = getenv_nonzero("KEFIR_FREEBSD_LIB");
    externals->freebsd.dynamic_linker = getenv_nonzero("KEFIR_FREEBSD_DYNAMIC_LINKER");
    externals->openbsd.include_path = getenv_nonzero("KEFIR_OPENBSD_INCLUDE");
    externals->openbsd.library_path = getenv_nonzero("KEFIR_OPENBSD_LIB");
    externals->openbsd.dynamic_linker = getenv_nonzero("KEFIR_OPENBSD_DYNAMIC_LINKER");
    externals->netbsd.include_path = getenv_nonzero("KEFIR_NETBSD_INCLUDE");
    externals->netbsd.library_path = getenv_nonzero("KEFIR_NETBSD_LIB");
    externals->netbsd.dynamic_linker = getenv_nonzero("KEFIR_NETBSD_DYNAMIC_LINKER");

    UNUSED(set_if_null);

diff --git a/source/platform/tempfile.c b/source/platform/tempfile.c
index 2fa69a3f..ee3a6644 100644
--- a/source/platform/tempfile.c
+++ b/source/platform/tempfile.c
@@ -93,10 +93,10 @@ kefir_result_t kefir_tempfile_manager_free(struct kefir_mem *mem, struct kefir_t

static const char *get_tmp_directory(void) {
    const char *tmp_directory = getenv("KEFIR_TMPDIR");
    if (tmp_directory == NULL) {
    if (tmp_directory == NULL || *tmp_directory == '\0') {
        tmp_directory = getenv("TMPDIR");
    }
    if (tmp_directory == NULL) {
    if (tmp_directory == NULL || *tmp_directory == '\0') {
#ifdef P_tmpdir
        tmp_directory = P_tmpdir;
#else
diff --git a/source/tests/external/c-testsuite/Makefile.mk b/source/tests/external/c-testsuite/Makefile.mk
index 30c26514..51f1f086 100644
--- a/source/tests/external/c-testsuite/Makefile.mk
+++ b/source/tests/external/c-testsuite/Makefile.mk
@@ -28,7 +28,7 @@ $(KEFIR_EXTERNAL_TEST_C_TESTSUITE_DIR)/c-tests.log: $(KEFIR_EXTERAL_TEST_C_TESTS
	@echo "Running c-testsuite $(KEFIR_EXTERNAL_TEST_C_TESTSUITE_ARCHIVE_SHA256)..."
	@KEFIRCC="$(realpath $(KEFIR_EXE))" \
		LD_LIBRARY_PATH="$(realpath $(LIB_DIR)):$$LD_LIBRARY_PATH" \
		KEFIR_RTINC="$(realpath $(HEADERS_DIR)/runtime)" \
		KEFIR_RTINC="$(realpath $(HEADERS_DIR)/kefir/runtime)" \
		CFLAGS="$(KEFIR_EXTERNAL_TEST_C_TESTSUITE_CFLAGS)" \
		bash -c "set -o pipefail; cd $(KEFIR_EXTERAL_TEST_C_TESTSUITE); ./single-exec kefir 2>&1" | tee "$@.tmp"
	@mv "$@.tmp" "$@"
diff --git a/source/tests/external/duktape/Makefile.mk b/source/tests/external/duktape/Makefile.mk
index 305875e4..2278deac 100644
--- a/source/tests/external/duktape/Makefile.mk
+++ b/source/tests/external/duktape/Makefile.mk
@@ -24,7 +24,7 @@ $(KEFIR_EXTERAL_TEST_DUKTAPE_EXE): $(KEFIR_EXTERNAL_TEST_DUKTAPE_DIR)/$(KEFIR_EX
	@sed "s/^CCOPTS[ ]*=.*$$/CCOPTS = -O1 -fPIC -pie/g" "$(KEFIR_EXTERNAL_TEST_DUKTAPE_SOURCE_DIR)/Makefile.cmdline" > "$(KEFIR_EXTERNAL_TEST_DUKTAPE_SOURCE_DIR)/Makefile.kefir"
	@cd "$(KEFIR_EXTERNAL_TEST_DUKTAPE_SOURCE_DIR)" && \
		LD_LIBRARY_PATH="$(shell $(REALPATH) $(LIB_DIR)):$$LD_LIBRARY_PATH" \
		KEFIR_RTINC="$(shell $(REALPATH) $(HEADERS_DIR)/runtime)" \
		KEFIR_RTINC="$(shell $(REALPATH) $(HEADERS_DIR)/kefir/runtime)" \
			$(MAKE) -f Makefile.kefir \
			CC="$(shell $(REALPATH) $(KEFIR_EXE))"

diff --git a/source/tests/external/gcc-torture/Makefile.mk b/source/tests/external/gcc-torture/Makefile.mk
index 2d776ac1..58d52f66 100644
--- a/source/tests/external/gcc-torture/Makefile.mk
+++ b/source/tests/external/gcc-torture/Makefile.mk
@@ -32,7 +32,7 @@ $(KEFIR_EXTERNAL_TEST_GCC_TORTURE_DIR)/torture.log: $(KEFIR_EXTERNAL_TEST_GCC_DI
	@TORTURE="$(KEFIR_EXTERNAL_TEST_GCC_DIR)/gcc/testsuite/gcc.c-torture" \
		KEFIRCC="$(realpath $(KEFIR_EXE))" \
		LD_LIBRARY_PATH="$(realpath $(LIB_DIR)):$$LD_LIBRARY_PATH" \
		KEFIR_RTINC="$(realpath $(HEADERS_DIR)/runtime)" \
		KEFIR_RTINC="$(realpath $(HEADERS_DIR)/kefir/runtime)" \
		KEFIR_EXTRAFLAGS="$(KEFIR_EXTERNAL_TEST_GCC_TORTURE_CFLAGS)" \
		"$(SOURCE_DIR)/tests/external/gcc-torture/run_gcc_torture_suite.sh" 2>&1 | tee "$@.tmp"
	@mv "$@.tmp" "$@"
diff --git a/source/tests/external/lua/Makefile.mk b/source/tests/external/lua/Makefile.mk
index 7685a6b2..91723a87 100644
--- a/source/tests/external/lua/Makefile.mk
+++ b/source/tests/external/lua/Makefile.mk
@@ -34,7 +34,7 @@ $(KEFIR_EXTERAL_TEST_LUA_EXE): $(KEFIR_EXTERNAL_TEST_LUA_DIR)/$(KEFIR_EXTERNAL_T
	@cd "$(KEFIR_EXTERNAL_TEST_LUA_DIR)" && tar xvfz "$(KEFIR_EXTERNAL_TEST_LUA_ARCHIVE)"
	@cd "$(KEFIR_EXTERNAL_TEST_LUA_DIR)/lua-$(KEFIR_EXTERNAL_TEST_LUA_VERSION)" && \
		LD_LIBRARY_PATH="$(shell $(REALPATH) $(LIB_DIR)):$$LD_LIBRARY_PATH" \
		KEFIR_RTINC="$(shell $(REALPATH) $(HEADERS_DIR)/runtime)" \
		KEFIR_RTINC="$(shell $(REALPATH) $(HEADERS_DIR)/kefir/runtime)" \
			$(MAKE) -f Makefile \
			CC="$(shell $(REALPATH) $(KEFIR_EXE))" \
			CFLAGS="$(KEFIR_EXTERNAL_TEST_LUA_CFLAGS)" \
diff --git a/source/tests/external/nginx/Makefile.mk b/source/tests/external/nginx/Makefile.mk
index 381eff44..5b7c4176 100644
--- a/source/tests/external/nginx/Makefile.mk
+++ b/source/tests/external/nginx/Makefile.mk
@@ -23,7 +23,7 @@ $(KEFIR_EXTERNAL_TEST_NGINX_DIR)/.configured: $(KEFIR_EXTERNAL_TEST_NGINX_DIR)/$
	@cd "$(KEFIR_EXTERNAL_TEST_NGINX_DIR)" && tar xvfz "$(KEFIR_EXTERNAL_TEST_NGINX_ARCHIVE)"
	@cd "$(KEFIR_EXTERNAL_TEST_NGINX_DIR)/nginx-$(KEFIR_EXTERNAL_TEST_NGINX_VERSION)" && \
		LD_LIBRARY_PATH="$(shell $(REALPATH) $(LIB_DIR)):$$LD_LIBRARY_PATH" \
		KEFIR_RTINC="$(shell $(REALPATH) $(HEADERS_DIR)/runtime)" \
		KEFIR_RTINC="$(shell $(REALPATH) $(HEADERS_DIR)/kefir/runtime)" \
			CC="$(shell $(REALPATH) $(KEFIR_EXE))" \
			CFLAGS="$(KEFIR_EXTERNAL_TEST_NGINX_CFLAGS)" \
			./configure
@@ -33,7 +33,7 @@ $(KEFIR_EXTERAL_TEST_NGINX_EXE): $(KEFIR_EXTERNAL_TEST_NGINX_DIR)/.configured
	@echo "Building Nginx $(KEFIR_EXTERNAL_TEST_NGINX_VERSION)..."
	@cd "$(KEFIR_EXTERNAL_TEST_NGINX_DIR)/nginx-$(KEFIR_EXTERNAL_TEST_NGINX_VERSION)" && \
		LD_LIBRARY_PATH="$(shell $(REALPATH) $(LIB_DIR)):$$LD_LIBRARY_PATH" \
		KEFIR_RTINC="$(shell $(REALPATH) $(HEADERS_DIR)/runtime)" \
		KEFIR_RTINC="$(shell $(REALPATH) $(HEADERS_DIR)/kefir/runtime)" \
			$(MAKE)

$(KEFIR_EXTERNAL_TEST_NGINX_DIR)/test.log: $(KEFIR_EXTERAL_TEST_NGINX_EXE)
-- 
2.48.1
This looks reasonable. I will merge it into the master sometime next week.

Thank you for the patch.