- # Append to the variables for the given build type for
- # each language, in the parent scope. We add our new variables at the end, so
- # compiler-specific choices are more likely to override default CMake choices.
- # This is for instance useful for RelWithDebInfo builds, where we want to use the full
- # set of our optimization flags detected in this file, rather than having -O2 override them.
- set(CMAKE_${language}_FLAGS${punctuation}${build_type}
- "${CMAKE_${language}_FLAGS${punctuation}${build_type}} ${GMXC_${language}FLAGS${punctuation}${build_type}}"
- PARENT_SCOPE)
+# Add compiler flags expected for all GROMACS build configurations,
+# and those expected for the current CMake build type (e.g. Release)
+# to TARGET.
+#
+# Most targets (ie. libraries, executables) need compiler flags that
+# are characteristic of the build configuration. This function
+# provides a central point for adding such flags. This approach is
+# more flexible than e.g. setting CMAKE_CXX_FLAGS globally, because
+# that setting will apply globally, which means it applies also to
+# "external" code that the build of GROMACS might also build.
+function(gmx_target_compile_options TARGET)
+ if (NOT GMX_SKIP_DEFAULT_CFLAGS)
+ target_compile_options(${TARGET}
+ PRIVATE
+ $<$<COMPILE_LANGUAGE:C>:${SIMD_C_FLAGS};${MPI_COMPILE_FLAGS};${EXTRA_C_FLAGS};${GMXC_CFLAGS}>
+ $<$<COMPILE_LANGUAGE:CXX>:${SIMD_CXX_FLAGS};${MPI_COMPILE_FLAGS};${EXTRA_CXX_FLAGS};${GMXC_CXXFLAGS}>
+ )
+ foreach(build_type ${build_types_with_explicit_flags})
+ target_compile_options(${TARGET}
+ BEFORE PRIVATE
+ $<$<AND:$<COMPILE_LANGUAGE:C>,$<CONFIG:${build_type}>>:${GMXC_CFLAGS_${build_type}}>
+ $<$<AND:$<COMPILE_LANGUAGE:CXX>,$<CONFIG:${build_type}>>:${GMXC_CXXFLAGS_${build_type}}>
+ )