From: Simon Marchi Date: Tue, 5 Nov 2024 17:10:51 +0000 (-0500) Subject: bt2/setup.py.in: assume environment variables always exist X-Git-Url: http://drtracing.org/?a=commitdiff_plain;h=18bcd8bdd30f29196eb9c17db2d528e4f0437b16;p=babeltrace.git bt2/setup.py.in: assume environment variables always exist `setup.py` is always called from our Makefile, where we control which environment variables are set. Remove the code paths that handle the cases where environment variables CC, LDFLAGS, CFLAGS and CPPFLAGS may not exist. This simplifies the number of cases to worry about, making the code easier to reason about. This requires passing the build flags to setup.py when using the install command, but I don't think that's a problem. Change-Id: I380d113824bddf6a423e34a681d904a844175100 Signed-off-by: Simon Marchi Reviewed-on: https://review.lttng.org/c/babeltrace/+/13491 Reviewed-by: Michael Jeanson Tested-by: jenkins Reviewed-by: Philippe Proulx --- diff --git a/src/bindings/python/bt2/Makefile.am b/src/bindings/python/bt2/Makefile.am index 0c68ea11..718486fc 100644 --- a/src/bindings/python/bt2/Makefile.am +++ b/src/bindings/python/bt2/Makefile.am @@ -143,6 +143,8 @@ BUILD_FLAGS=CC="$(CC)" \ CPPFLAGS="$(AM_CPPFLAGS) $(CPPFLAGS) -I$(srcdir)/bt2" \ LDFLAGS="$(AM_LDFLAGS) $(LDFLAGS) $(GLIB_LIBS) $(PYTHON_LDFLAGS) $(LIBS)" +SETUP_PY=$(BUILD_FLAGS) $(PYTHON) $(builddir)/setup.py + all-local: build-python-bindings.stamp copy-static-deps.stamp: $(addprefix $(srcdir)/, $(STATIC_BINDINGS_DEPS)) @@ -155,7 +157,7 @@ copy-static-deps.stamp: $(addprefix $(srcdir)/, $(STATIC_BINDINGS_DEPS)) build-python-bindings.stamp: copy-static-deps.stamp $(GENERATED_BINDINGS_DEPS) \ $(STATIC_NATIVE_LIB_SRCS) $(STATIC_LIBRARIES_DEPS) - $(BUILD_FLAGS) $(PYTHON) $(builddir)/setup.py build --force + $(SETUP_PY) build --force touch $@ swig_verbose = $(swig_verbose_@AM_V@) @@ -185,7 +187,7 @@ install-exec-local: build-python-bindings.stamp if [ "$(DESTDIR)" != "" ]; then \ opts="$$opts --root=$(DESTDIR)"; \ fi; \ - $(PYTHON) $(builddir)/setup.py install $(PY_INSTALL_OPTS) $$opts; + $(SETUP_PY) install $(PY_INSTALL_OPTS) $$opts; clean-local: rm -rf $(builddir)/build diff --git a/src/bindings/python/bt2/setup.py.in b/src/bindings/python/bt2/setup.py.in index 3c81447e..47f28506 100644 --- a/src/bindings/python/bt2/setup.py.in +++ b/src/bindings/python/bt2/setup.py.in @@ -52,13 +52,15 @@ following command to your .bashrc/.zshrc: original_get_config_vars = sysconfig.get_config_vars -def get_cflags(): - cflags = os.environ.get("CFLAGS") +# Get the value of environment variable `name` and assert that it exists. +def getenv_assert(name: str): + value = os.getenv(name) + assert value is not None + return value - if cflags is None: - [cflags] = original_get_config_vars("CFLAGS") - return cflags +def get_cflags(): + return getenv_assert("CFLAGS") # distutils performs a similar transformation step on LDSHARED on @@ -70,22 +72,15 @@ def get_cflags(): # by either the Python interprter's CC or the overridden CC don't cause a # build failure. def get_ldshared(): - cc = os.environ.get("CC") - ldflags = os.environ.get("LDFLAGS") + cc = getenv_assert("CC") + ldflags = getenv_assert("LDFLAGS") [py_cc] = original_get_config_vars("CC") [py_ldshared] = original_get_config_vars("LDSHARED") if not py_ldshared.startswith(py_cc): return py_ldshared - if cc and ldflags: - return "{} -shared {}".format(cc, ldflags) - elif cc: - return cc + py_ldshared[len(py_cc) :] - elif ldflags: - return py_cc + py_ldshared[len(py_cc) :] - else: - return py_ldshared + return "{} -shared {}".format(cc, ldflags) def our_get_config_vars(*args): @@ -135,16 +130,15 @@ def cygpath_m(path: str): # (starts with '-I/') and convert them to valid Windows paths using cygpath. if is_mingw(): for flagvar in ["CFLAGS", "CPPFLAGS"]: - cur_flags = os.getenv(flagvar) - if cur_flags != None: - new_flags = "" - for flag in cur_flags.split(): - if flag.startswith("-I/"): - flag = "-I{}".format(cygpath_m(flag[2:])) + cur_flags = getenv_assert(flagvar) + new_flags = "" + for flag in cur_flags.split(): + if flag.startswith("-I/"): + flag = "-I{}".format(cygpath_m(flag[2:])) - new_flags += " {}".format(flag) + new_flags += " {}".format(flag) - os.environ[flagvar] = new_flags + os.environ[flagvar] = new_flags def main():