ecbuild_compiler_flags
Set compiler specific default compilation flags for a given language.
ecbuild_compiler_flags( <lang> )
The procedure is as follows:
ecBuild does not set
CMAKE_<lang>_FLAGSi.e. the user can set these via-Dor the CMake cache and these will be the “base” flags.ecBuild overwrites
CMAKE_<lang>_FLAGS_<btype>in the CMake cache for all build types with compiler specific defaults for the currently loaded compiler i.e. any value set by the user via-Dor the CMake cache has no effect.Any value the user provides via
ECBUILD_<lang>_FLAGSorECBUILD_<lang>_FLAGS_<btype>overrides the correspondingCMAKE_<lang>_FLAGSorCMAKE_<lang>_FLAGS_<btype>without being written to the CMake cache.
ecbuild_linker_flags
Apply user or toolchain specified linker flag overrides per object type (NOT written to cache)
ecbuild_linker_flags()
ecbuild_override_compiler_flags
Purge existing CMAKE_<lang>_FLAGS flags and trigger the use of per
source file overrideable flags (see Using custom compilation flags for
an explanation).
- ecbuild_override_compiler_flags( [WARN] [INHERIT_ECBUILD_FLAGS]
[COMPILE_FLAGS <file>] [SOURCE_FLAGS <file>] )
Options:
- WARN
Emit a warning when compiler flags are purged.
- INHERIT_ECBUILD_FLAGS
Only applies together with
COMPILE_FLAGS. Copies anyECBUILD_<lang>_FLAGSandECBUILD_<lang>_FLAGS_<btype>values to project specific<PNAME>_<lang>_FLAGSand<PNAME>_<lang>_FLAGS_<btype>variables before loading the compile flags file. This allows per sourceCOMPILE_FLAGSrules to build on ecBuild level overrides even thoughCMAKE_<lang>_FLAGSare purged.This option does not apply to
SOURCE_FLAGS.
Using custom compilation flags
If compilation flags need to be controlled on a per source file basis, ecBuild supports defining custom rules in a CMake or JSON file.
When using this approach, default compilation flags are NOT loaded!
Overriding compilation flags on a per source file basis using CMake rules
Compiler flags can be overridden on a per source file basis by setting the
CMake variable ECBUILD_COMPILE_FLAGS to the full path of a CMake file
defining the override rules. If set, <PNAME>_ECBUILD_COMPILE_FLAGS
takes precendence and ECBUILD_COMPILE_FLAGS is ignored, allowing for
rules that only apply to a subproject (e.g. in a bundle).
If ecbuild_override_compiler_flags(INHERIT_ECBUILD_FLAGS COMPILE_FLAGS
<file>) is used, existing ECBUILD_<lang>_FLAGS and
ECBUILD_<lang>_FLAGS_<btype> values are copied to project specific
<PNAME>_<lang>_FLAGS and <PNAME>_<lang>_FLAGS_<btype> variables
before the compile flags file is loaded. This is only supported for
COMPILE_FLAGS.
Flags can be overridden in 3 different ways:
By defining project specific flags for a language and (optionally) build type e.g.
set(<PNAME>_Fortran_FLAGS "...") # common flags for all build types set(<PNAME>_Fortran_FLAGS_DEBUG "...") # only for DEBUG build type
By defining source file specific flags which are combined with the project and target specific flags
set_source_files_properties(<source> PROPERTIES COMPILE_FLAGS "..." # common flags for all build types COMPILE_FLAGS_DEBUG "...") # only for DEBUG build type
By defining source file specific flags which override the project and target specific flags
set_source_files_properties(<source> PROPERTIES OVERRIDE_COMPILE_FLAGS "..." OVERRIDE_COMPILE_FLAGS_DEBUG "...")
See examples/override-compile-flags in the ecBuild source tree for a
complete example using this technique.
Overriding compilation flags on a per source file basis using JSON rules
Compiler flags can be overridden on a per source file basis by setting the
CMake variable ECBUILD_SOURCE_FLAGS to the full path of a JSON file
defining the override rules. If set, <PNAME>_ECBUILD_SOURCE_FLAGS
takes precendence and ECBUILD_SOURCE_FLAGS is ignored, allowing for
rules that only apply to a subproject (e.g. in a bundle).
The JSON file lists shell glob patterns and the rule to apply to each source
file matching the pattern, defined as an array [op, flag1, ...]
containing an operator followed by one or more flags. Valid operators are:
- +:
Add the flags to the default compilation flags for matching files
- =:
Set the flags for matching files, disregarding default compilation flags
- /:
Remove the flags from the default compilation flags for matching files
Rules can be nested to e.g. only apply to a subdirectory by setting the rule to a dictionary, which will only apply to source files matching its pattern.
An example JSON file demonstrating different rule types is given below:
{
"*" : [ "+", "-g0" ],
"*.cxx" : [ "+", "-cxx11" ],
"*.f90" : [ "+", "-pipe" ],
"foo.c" : [ "+", "-O0" ],
"foo.cc" : [ "+", "-O2", "-pipe" ],
"bar/*": {
"*.f90" : [ "=", "-O1" ]
},
"baz/*": {
"*.f90" : [ "/", "-pipe" ],
"*.f90" : [ "/", "-O2" ],
"*.f90" : [ "+", "-O3" ]
}
}
See examples/override-compile-flags in the ecBuild source tree for a
complete example using this technique.