From 2820749d0fb32798ba3eda58c8ac9d5df2cb37a7 Mon Sep 17 00:00:00 2001 From: Ray Speth Date: Sun, 20 Aug 2023 17:42:41 -0400 Subject: [PATCH] [SCons] Fix cleanup of -isysroot flags in package builds Also filter out paths referring to conda build environment --- platform/posix/SConscript | 4 ++-- samples/cxx/SConscript | 11 ++++++----- samples/f77/SConscript | 8 ++++---- site_scons/buildutils.py | 39 ++++++++++++++++++++++++++++++--------- 4 files changed, 42 insertions(+), 20 deletions(-) diff --git a/platform/posix/SConscript b/platform/posix/SConscript index 6ebb9885f..4367aff05 100644 --- a/platform/posix/SConscript +++ b/platform/posix/SConscript @@ -51,8 +51,8 @@ if localenv["package_build"]: # compatible with the SDK used for building. excludes = ( "-isysroot", "-mmacosx", "-march", "-mtune", "-fdebug-prefix-map") - localenv["CCFLAGS"] = compiler_flag_list(localenv["CCFLAGS"], excludes) - localenv["CXXFLAGS"] = compiler_flag_list(localenv["CXXFLAGS"], excludes) + localenv["CCFLAGS"] = compiler_flag_list(localenv["CCFLAGS"], env["CC"], excludes) + localenv["CXXFLAGS"] = compiler_flag_list(localenv["CXXFLAGS"], env["CC"], excludes) # Generate cantera.pc for use with pkg-config localenv["pc_prefix"] = localenv["prefix"] diff --git a/samples/cxx/SConscript b/samples/cxx/SConscript index dad7b979f..63ee37d29 100644 --- a/samples/cxx/SConscript +++ b/samples/cxx/SConscript @@ -52,7 +52,7 @@ set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${OpenMP_EXE_LINKER_FLAGS} # Note: These CMakeLists.txt and SConstruct files are automatically installed # by the "RecursiveInstall" that grabs everything in the cxx directory. - flag_excludes = ["$(", "/TP", "$)", "/nologo"] + flag_excludes = [r"\$\(", "/TP", r"\$\)", "/nologo"] incdirs = [localenv["ct_incroot"]] libdirs = [localenv["ct_libdir"]] if localenv["package_build"]: @@ -60,8 +60,8 @@ set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${OpenMP_EXE_LINKER_FLAGS} # conda package for now. # Users should compile against their local SDKs, which should be backwards # compatible with the SDK used for building. - flag_excludes.extend(["-isysroot", "-mmacosx", "-march", "-mtune" - "-fdebug-prefix-map"]) + flag_excludes.extend(["-isysroot", "-mmacosx", "-march", "-mtune", + "-fdebug-prefix-map", ".*/_build_env/"]) else: incdirs.extend([localenv["sundials_include"], localenv["boost_inc_dir"]]) incdirs.append(localenv["hdf_include"]) @@ -78,7 +78,8 @@ set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${OpenMP_EXE_LINKER_FLAGS} for d in libdirs]) cc_flags = compiler_flag_list(localenv["CCFLAGS"] + localenv["CXXFLAGS"], - flag_excludes) + env["CC"], flag_excludes) + link_flags = compiler_flag_list(localenv["LINKFLAGS"], env["CC"], flag_excludes) localenv["tmpl_compiler_flags"] = repr(cc_flags) localenv['tmpl_cantera_frameworks'] = repr(localenv['FRAMEWORKS']) localenv['tmpl_cantera_incdirs'] = repr([x for x in incdirs if x]) @@ -90,7 +91,7 @@ set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${OpenMP_EXE_LINKER_FLAGS} localenv['cmake_cantera_incdirs'] += ' "/usr/local/include"' localenv['tmpl_cantera_libdirs'] = repr([x for x in libdirs if x]) localenv['cmake_cantera_libdirs'] = ' '.join(quoted(x) for x in libdirs if x) - localenv['tmpl_cantera_linkflags'] = repr(localenv['LINKFLAGS']) + localenv['tmpl_cantera_linkflags'] = repr(link_flags) localenv['tmpl_progname'] = name localenv['tmpl_sourcename'] = name + '.cpp' env_args = [] diff --git a/samples/f77/SConscript b/samples/f77/SConscript index aa1a55253..f73e120a9 100644 --- a/samples/f77/SConscript +++ b/samples/f77/SConscript @@ -24,7 +24,7 @@ for program_name, fortran_sources in samples: # Generate SConstruct file to be installed linkflags = ["-g", localenv["thread_flags"]] -flag_excludes = ["$(", "/TP", "$)", "/nologo"] +flag_excludes = [r"\$\(", "/TP", r"\$\)", "/nologo"] incdirs = [localenv["ct_incroot"]] libdirs = [localenv["ct_libdir"]] if localenv["package_build"]: @@ -32,8 +32,8 @@ if localenv["package_build"]: # conda package for now. # Users should compile against their local SDKs, which should be backwards # compatible with the SDK used for building. - flag_excludes.extend(["-isysroot", "-mmacosx", "-march", "-mtune", - "-fdebug-prefix-map"]) + flag_excludes.extend(["-mmacosx", "-march", "-mtune", "-fdebug-prefix-map", + "-isysroot", ".*/_build_env/"]) else: linkflags.append(f"-Wl,-rpath,{localenv['ct_shlibdir']}") @@ -50,7 +50,7 @@ else: libs = ["cantera_fortran"] + localenv["cantera_libs"] + localenv["cxx_stdlib"] cc_flags = compiler_flag_list(localenv["CCFLAGS"] + localenv["CXXFLAGS"], - flag_excludes) + env["CC"], flag_excludes) localenv["tmpl_compiler_flags"] = repr(cc_flags) localenv['tmpl_cantera_incdirs'] = repr([x for x in incdirs if x]) localenv['tmpl_cantera_libs'] = repr(libs) diff --git a/site_scons/buildutils.py b/site_scons/buildutils.py index 9da33d83e..03ed55c8b 100644 --- a/site_scons/buildutils.py +++ b/site_scons/buildutils.py @@ -1098,24 +1098,45 @@ def add_RegressionTest(env: "SCEnvironment") -> None: def compiler_flag_list( flags: "Union[str, Iterable]", - excludes: "Optional[Iterable]" = [] + compiler: str, + excludes: "Optional[Iterable]" = (), ) -> "List[str]": """ Separate concatenated compiler flags in ``flags``. - Entries listed in ``excludes`` are omitted. + + ``compiler`` is either ``"cl"`` for MSVC or anything else for a different + compiler. + + Entries starting with the regular expression patterns in ``excludes`` are omitted. """ if not isinstance(flags, str): flags = " ".join(flags) - # split concatenated entries. Options can start with "-", "/", or "$" - flags = re.findall(r"""(?:^|\ +) # start of string or leading whitespace - ([-/\$].+?) # capture start of option - (?=\ +[-/\$]|\ *$) # start of next option or end of string - """, flags, re.VERBOSE) - cc_flags = [] + + if compiler == "cl": + # Options can start with "/", or "$" + expr = r"""(?:^|\ +) # start of string or leading whitespace + ([/\$].+?) # capture start of option + (?=\ +[-/\$]|\ *$) # start of next option or end of string + """ + else: + # Options can start with "-" + expr = r"""(?:^|\ +) # start of string or leading whitespace + (-.+?) # capture start of option + (?=\ +-|\ *$) # start of next option or end of string + """ + + # split concatenated entries + flags = re.findall(expr, flags, re.VERBOSE) + + # Remove duplicates and excluded items excludes = tuple(excludes) + cc_flags = [] for flag in flags: - if not flag.startswith(excludes) and flag not in cc_flags: + if flag in cc_flags: + continue + if not any(re.match(exclude, flag) for exclude in excludes): cc_flags.append(flag) + return cc_flags