Skip to content

Commit

Permalink
[SCons] Fix sample SConstruct and Makefiles for package_build option
Browse files Browse the repository at this point in the history
  • Loading branch information
ischoegl committed Aug 25, 2022
1 parent 8c18c01 commit 7f309cd
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 33 deletions.
2 changes: 1 addition & 1 deletion samples/cxx/Makefile.in
Expand Up @@ -17,4 +17,4 @@ clean:
$(RM) $(OBJS) @tmpl_progname@

dist-clean: clean
$(RM) *~
$(RM) *~
70 changes: 48 additions & 22 deletions samples/cxx/SConscript
Expand Up @@ -52,22 +52,41 @@ set(CMAKE_EXE_LINKER_FLAGS ${CMAKE_EXE_LINKER_FLAGS} ${OpenMP_EXE_LINKER_FLAGS})
# Note: These Makefiles and SConstruct files are automatically installed
# by the "RecursiveInstall" that grabs everything in the cxx directory.

incdirs = (localenv['ct_incroot'], localenv['sundials_include'],
localenv['boost_inc_dir']) + tuple(localenv['extra_inc_dirs'])
libdirs = ((localenv['ct_libdir'], localenv['sundials_libdir'],
localenv['blas_lapack_dir']) + tuple(localenv['extra_lib_dirs']))
# Remove sysroot and macOS min version flags in templated output files
# Users should compile against their local SDKs, which should be backwards
# compatible with the SDK used for building. This only applies to the
# conda package for now.
if env['OS'] == 'Darwin' and os.environ.get('CONDA_BUILD', False):
ccFlags = []
for flag in localenv['CCFLAGS'] + localenv['CXXFLAGS']:
if not flag.startswith(('-isysroot', '-mmacosx', '/App')):
ccFlags.append(flag)
incdirs = [localenv["ct_incroot"]]
libdirs = [localenv["ct_libdir"]]
if localenv["package_build"]:
# Remove sysroot flags in templated output files. This only applies to the
# conda package for now.
# Users should compile against their local SDKs, which should be backwards
# compatible with the SDK used for building.
def split_compiler_flags(flags, excludes=[]):
"""
Separate concatenated compiler flags in flag list.
Entries listed in 'exclude' are omitted.
"""
flags = " ".join(flags)
flags = f" {flags}".split(" -")[1:]
# split concatenated entries
flags = [f"-{flag}" for flag in flags]
cc_flags = []
for flag in flags:
if not flag.startswith(excludes) and flag not in cc_flags:
cc_flags.append(flag)
return cc_flags

excludes = (
"-isysroot", "-mmacosx", "-march", "-mtune", "-fdebug-prefix-map")
cc_flags = split_compiler_flags(localenv["CCFLAGS"] + localenv["CXXFLAGS"],
excludes)
else:
ccFlags = localenv['CCFLAGS'] + localenv['CXXFLAGS']
localenv['tmpl_compiler_flags'] = repr(ccFlags)
incdirs.extend([localenv["sundials_include"], localenv["boost_inc_dir"]])
incdirs.extend(localenv["extra_inc_dirs"])
libdirs.extend([localenv["sundials_libdir"], localenv["blas_lapack_dir"]])
libdirs.extend(localenv["extra_lib_dirs"])

cc_flags = localenv["CCFLAGS"] + localenv["CXXFLAGS"]

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])
localenv['cmake_cantera_incdirs'] = ' '.join(quoted(x) for x in incdirs if x)
Expand All @@ -90,12 +109,19 @@ set(CMAKE_EXE_LINKER_FLAGS ${CMAKE_EXE_LINKER_FLAGS} ${OpenMP_EXE_LINKER_FLAGS})
env_args.append('MSVC_VERSION={0!r}'.format(localenv['MSVC_VERSION']))
localenv['tmpl_env_args'] = ', '.join(env_args)

# If this is a conda build on macOS, we do not want to specify the conda
# compilers from the build environment, because those won't be installed
# on the user's system.
if env['OS'] == 'Darwin' and os.environ.get('CONDA_BUILD', False):
localenv['CXX'] = 'clang++'
localenv['CC'] = 'clang'
if localenv["package_build"]:
# We do not want to specify the conda compilers from the build environment,
# because those won't be installed on the user's system.
if localenv["OS"] == "Darwin":
localenv["CXX"] = "clang++"
localenv["CC"] = "clang"
elif localenv["OS"] == "Windows":
# compilation needs the Visual Studio Command Prompt
localenv["CXX"] = "cl"
localenv["CC"] = "cl"
else:
localenv["CXX"] = "g++"
localenv["CC"] = "gcc"

sconstruct = localenv.SubstFile(pjoin(subdir, 'SConstruct'), 'SConstruct.in')
install(pjoin('$inst_sampledir', 'cxx', subdir), sconstruct)
Expand All @@ -106,7 +132,7 @@ set(CMAKE_EXE_LINKER_FLAGS ${CMAKE_EXE_LINKER_FLAGS} ${OpenMP_EXE_LINKER_FLAGS})

## Generate Makefiles to be installed
mak_path = pjoin(localenv['ct_incroot'], 'cantera', 'Cantera.mak')
localenv['mak_compiler_flags'] = ' '.join(ccFlags)
localenv["mak_compiler_flags"] = " ".join(cc_flags)
if ' ' in mak_path:
# There is no reasonable way to handle spaces in Makefile 'include'
# statement, so we fall back to using the relative path instead
Expand Down
4 changes: 2 additions & 2 deletions samples/f77/Makefile.in
@@ -1,6 +1,6 @@
include @tmpl_Cantera_dot_mak@

FC=@F77@
F77=@F77@
RM=rm -f
FFLAGS=-g
LDFLAGS=
Expand All @@ -12,7 +12,7 @@ OBJS=isentropic.o demo_ftnlib.o
all: isentropic

isentropic: $(OBJS)
$(F77) $(LDFLAGS) -o isentropic $(OBJS) $(LDLIBS)
$(F77) $(LDFLAGS) -o isentropic $(OBJS) $(LDLIBS) $(CANTERA_LIBS)

clean:
$(RM) $(OBJS)
Expand Down
26 changes: 22 additions & 4 deletions samples/f77/SConscript
Expand Up @@ -23,11 +23,15 @@ for program_name, fortran_sources in samples:
LINK='$FORTRAN_LINK')

# Generate SConstruct file to be installed
incdirs = (localenv['ct_incroot'], localenv['sundials_include'],
localenv['boost_inc_dir']) + tuple(localenv['extra_inc_dirs'])
incdirs = [localenv["ct_incroot"]]
libdirs = [localenv["ct_libdir"]]
if not localenv["package_build"]:
incdirs.extend([localenv["sundials_include"], localenv["boost_inc_dir"]])
incdirs.extend(localenv["extra_inc_dirs"])
libdirs.extend([localenv["sundials_libdir"], localenv["blas_lapack_dir"]])
libdirs.extend(localenv["extra_lib_dirs"])

libs = ['cantera_fortran'] + localenv['cantera_libs'] + env['cxx_stdlib']
libdirs = ((localenv['ct_libdir'], localenv['sundials_libdir'],
localenv['blas_lapack_dir']) + tuple(localenv['extra_lib_dirs']))
linkflags = ('-g', localenv['thread_flags'])

mak_path = pjoin(localenv['ct_incroot'], 'cantera', 'Cantera.mak')
Expand All @@ -43,6 +47,20 @@ localenv['tmpl_cantera_libdirs'] = repr([x for x in libdirs if x])
localenv['tmpl_cantera_linkflags'] = repr([x for x in linkflags if x])
localenv['tmpl_cantera_frameworks'] = repr(localenv['FRAMEWORKS'])

if localenv["package_build"]:
# We do not want to specify the conda compilers from the build environment,
# because those won't be installed on the user's system.
if localenv["OS"] == "Darwin":
localenv["F77"] = "gfortran"
localenv["CC"] = "clang"
elif env["OS"] == "Windows":
# compilation needs the Visual Studio Command Prompt
localenv["F77"] = "ifx"
localenv["CC"] = "cl"
else:
localenv["F77"] = "gfortran"
localenv["CC"] = "gcc"

sconstruct = localenv.SubstFile('SConstruct', 'SConstruct.in')

# Generate Makefile to be installed
Expand Down
2 changes: 1 addition & 1 deletion samples/f90/Makefile.in
Expand Up @@ -13,7 +13,7 @@ OBJS=$(subst .f90,.o,$(SRCS))
all: @make_target@

@make_target@: $(OBJS)
$(F90) $(LDFLAGS) -o @make_target@ $(OBJS) $(LDLIBS)
$(F90) $(LDFLAGS) -o @make_target@ $(OBJS) $(LDLIBS) $(CANTERA_LIBS)

%.o : %.f90
$(F90) -c $< @FORTRANMODDIRPREFIX@$(F90MODDIR) $(F90FLAGS)
Expand Down
11 changes: 8 additions & 3 deletions samples/f90/SConscript
Expand Up @@ -18,10 +18,15 @@ for programName, sources in samples:
LINK='$FORTRAN_LINK')

# Generate SConstruct files to be installed
incdirs = [pjoin(localenv['ct_incroot'], 'cantera')] + localenv['extra_inc_dirs']
incdirs = [pjoin(localenv["ct_incroot"], "cantera")] # path to fortran .mod
libdirs = [localenv["ct_libdir"]]
if not localenv["package_build"]:
incdirs.extend([localenv["sundials_include"], localenv["boost_inc_dir"]])
incdirs.extend(localenv["extra_inc_dirs"])
libdirs.extend([localenv["sundials_libdir"], localenv["blas_lapack_dir"]])
libdirs.extend(localenv["extra_lib_dirs"])

libs = ['cantera_fortran'] + localenv['cantera_libs'] + env['cxx_stdlib']
libdirs = ((localenv['ct_libdir'], localenv['sundials_libdir'],
localenv['blas_lapack_dir']) + tuple(localenv['extra_lib_dirs']))
linkflags = ('-g', localenv['thread_flags'])

mak_path = pjoin(localenv['ct_incroot'], 'cantera', 'Cantera.mak')
Expand Down

0 comments on commit 7f309cd

Please sign in to comment.