bt2/setup.py.in: assume environment variables always exist
authorSimon Marchi <simon.marchi@efficios.com>
Tue, 5 Nov 2024 17:10:51 +0000 (12:10 -0500)
committerSimon Marchi <simon.marchi@efficios.com>
Tue, 5 Nov 2024 19:58:27 +0000 (14:58 -0500)
`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 <simon.marchi@efficios.com>
Reviewed-on: https://review.lttng.org/c/babeltrace/+/13491
Reviewed-by: Michael Jeanson <mjeanson@efficios.com>
Tested-by: jenkins <jenkins@lttng.org>
Reviewed-by: Philippe Proulx <eeppeliteloop@gmail.com>
src/bindings/python/bt2/Makefile.am
src/bindings/python/bt2/setup.py.in

index 0c68ea11620f17361ecf937383189edd3afb28ac..718486fcecd3058018440da996669924b45c3b9c 100644 (file)
@@ -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
index 3c81447ea80a2d8dd928f43aba69feea2bf7f980..47f2850622cbef18f4d8f31d78884832c2c95f32 100644 (file)
@@ -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():
This page took 0.027008 seconds and 4 git commands to generate.