diff --git a/cmake/modules/DuneMacros.cmake b/cmake/modules/DuneMacros.cmake index 39b7ff5fc1fb09dd31c4a378ec0ce4d29fb94326..1372881e9057c489c9922c00d84ed2a248e53346 100644 --- a/cmake/modules/DuneMacros.cmake +++ b/cmake/modules/DuneMacros.cmake @@ -80,6 +80,7 @@ enable_language(C) # Enable C to skip CXX bindings for some tests. include(FeatureSummary) include(DuneEnableAllPackages) +include(OverloadCompilerFlags) include(DuneSymlinkOrCopy) @@ -546,6 +547,9 @@ macro(dune_project) message(FATAL_ERROR "You need to specify an absolute path to your compiler instead of just the compiler name. cmake >= 3.0 fixes this issue.") endif() + # check if CXX flag overloading has been enabled (see OverloadCompilerFlags.cmake) + initialize_compiler_script() + # extract information from dune.module dune_module_information(${CMAKE_SOURCE_DIR}) set(ProjectName "${DUNE_MOD_NAME}") @@ -677,13 +681,6 @@ macro(dune_project) include(Headercheck) setup_headercheck() - # check whether the user wants to append compile flags upon calling make - if(ALLOW_EXTRA_CXXFLAGS AND (${CMAKE_GENERATOR} MATCHES ".*Unix Makefiles.*")) - file(WRITE ${CMAKE_BINARY_DIR}/compiler.sh - "#!/bin/bash\nif [ -n \"$VERBOSE\" ] ; then\necho 1>&2 \"Additional flags: \$EXTRA_CXXFLAGS\"\nfi\nexec ${CMAKE_CXX_COMPILER} \"\$@\" \$EXTRA_CXXFLAGS") - exec_program(chmod ARGS "+x ${CMAKE_BINARY_DIR}/compiler.sh") - set(CMAKE_CXX_COMPILER ${CMAKE_BINARY_DIR}/compiler.sh) - endif() endmacro(dune_project) # create a new config.h file and overwrite the existing one @@ -886,6 +883,10 @@ endif() include(CPack) feature_summary(WHAT ALL) + + # check if CXX flag overloading has been enabled + # and write compiler script (see OverloadCompilerFlags.cmake) + finalize_compiler_script() endmacro(finalize_dune_project) macro(target_link_dune_default_libraries _target) diff --git a/cmake/modules/OverloadCompilerFlags.cmake b/cmake/modules/OverloadCompilerFlags.cmake new file mode 100644 index 0000000000000000000000000000000000000000..c5783e836928ebb6213a9c0774ecca4c673035e7 --- /dev/null +++ b/cmake/modules/OverloadCompilerFlags.cmake @@ -0,0 +1,69 @@ +# check whether the user wants to overload compile flags upon calling make +# +# Provides the following macros: +# +# initialize_compiler_script() : needs to be called before further flags are added to CMAKE_CXX_FLAGS +# finalize_compiler_script() : needs to be called at the end of the cmake macros, e.g. in finalize_dune_project +# +# By default this feature is disabled. Use -DALLOW_CXXFLAGS_OVERWRITE=ON to activate. +# Then the following is possible: +# +# make CXXFLAGS="your flags" GRIDTYPE="grid type" +# +# Furthermore any CPP variable of the form -DVAR=VALUE can be overloaded on the command line. +# +# Note: If you don't know what this is or what it's good for, don't use it. +# +option(ALLOW_CXXFLAGS_OVERWRITE OFF) + +# check whether the user wants to append compile flags upon calling make +if(ALLOW_EXTRA_CXXFLAGS AND (${CMAKE_GENERATOR} MATCHES ".*Unix Makefiles.*")) + message("ALLOW_EXTRA_CXXFLAGS is deprecated, please use -DALLOW_CXXFLAGS_OVERWRITE=ON") + set(ALLOW_CXXFLAGS_OVERWRITE ON) +endif() + +# init compiler script and store CXX flags +macro(initialize_compiler_script) + if(ALLOW_CXXFLAGS_OVERWRITE AND (${CMAKE_GENERATOR} MATCHES ".*Unix Makefiles.*")) + find_program (BASH_PROGRAM bash) + # set CXXFLAGS as environment variable + set( DEFAULT_CXXFLAGS ${CMAKE_CXX_FLAGS}) + set( CMAKE_CXX_FLAGS "" ) + set( DEFAULT_CXX_COMPILER ${CMAKE_CXX_COMPILER} ) + set( COMPILER_SCRIPT_FILE "#!${BASH_PROGRAM}\nexec ${CMAKE_CXX_COMPILER} \"\$@\"") + file(WRITE ${CMAKE_BINARY_DIR}/compiler.sh "${COMPILER_SCRIPT_FILE}" ) + exec_program(chmod ARGS "+x ${CMAKE_BINARY_DIR}/compiler.sh") + set(CMAKE_CXX_COMPILER ${CMAKE_BINARY_DIR}/compiler.sh) + endif() +endmacro() + +# finalize compiler script and write it +macro(finalize_compiler_script) + if(ALLOW_CXXFLAGS_OVERWRITE AND (${CMAKE_GENERATOR} MATCHES ".*Unix Makefiles.*")) + find_program (GREP_PROGRAM grep) + find_program (SED_PROGRAM sed) + find_program (CUT_PROGRAM cut) + find_program (ENV_PROGRAM env) + find_program (ECHO_PROGRAM echo) + set( COMPILER_SCRIPT_FILE "#!${BASH_PROGRAM}\nSED=${SED_PROGRAM}\nGREP=${GREP_PROGRAM}") + set( COMPILER_SCRIPT_FILE "${COMPILER_SCRIPT_FILE}\nCUT=${CUT_PROGRAM}\nENV=${ENV_PROGRAM}\nECHO=${ECHO_PROGRAM}") + set( COMPILER_SCRIPT_FILE "${COMPILER_SCRIPT_FILE}\n# store flags\nFLAGS=\"\$@\"") + set( COMPILER_SCRIPT_FILE "${COMPILER_SCRIPT_FILE}\nMAKE_EXECUTABLE_NEW=0\n") + set( COMPILER_SCRIPT_FILE "${COMPILER_SCRIPT_FILE}\nif [ \"\$CXXFLAGS\" == \"\" ]; then\n # default CXX flags\n CXXFLAGS=\"${DEFAULT_CXXFLAGS}\"\nfi\n") + set( COMPILER_SCRIPT_FILE "${COMPILER_SCRIPT_FILE}\nGRIDS=\nCONFIG_H=${CMAKE_BINARY_DIR}/config.h") + set( COMPILER_SCRIPT_FILE "${COMPILER_SCRIPT_FILE}\nif [ \"\$GRIDTYPE\" != \"\" ]; then") + set( COMPILER_SCRIPT_FILE "${COMPILER_SCRIPT_FILE}\n GRIDS=`\$GREP \"defined USED_[A-Z_]*_GRIDTYPE\" \$CONFIG_H | \$SED 's/\\(.*defined USED\\_\\)\\(.*\\)\\(\\_GRIDTYPE*\\)/\\2/g'`\nfi\n") + set( COMPILER_SCRIPT_FILE "${COMPILER_SCRIPT_FILE}\nOLDFLAGS=\$FLAGS\nFLAGS=") + set( COMPILER_SCRIPT_FILE "${COMPILER_SCRIPT_FILE}\nfor FLAG in \$OLDFLAGS; do") + set( COMPILER_SCRIPT_FILE "${COMPILER_SCRIPT_FILE}\n NEWFLAG=\$FLAG\n VARNAME=`\$ECHO \$FLAG | \$GREP \"\\-D\" | \$SED 's/-D//g'`") + set( COMPILER_SCRIPT_FILE "${COMPILER_SCRIPT_FILE}\n for GRID in \$GRIDS; do\n if [ \"\$VARNAME\" == \"\$GRID\" ]; then\n NEWFLAG=\"-D\$GRIDTYPE\"\n break\n fi\n done") + set( COMPILER_SCRIPT_FILE "${COMPILER_SCRIPT_FILE}\n VARNAME=`\$ECHO \$VARNAME | \$GREP \"=\" | \$CUT -d \"=\" -f 1`") + set( COMPILER_SCRIPT_FILE "${COMPILER_SCRIPT_FILE}\n if [ \"\$VARNAME\" != \"\" ]; then\n VAR=`\$ENV | \$GREP \$VARNAME`\n if [ \"\$VAR\" != \"\" ]; then") + set( COMPILER_SCRIPT_FILE "${COMPILER_SCRIPT_FILE}\n # add variable from environment to flags list\n NEWFLAG=\"-D\$VARNAME=\${!VARNAME}\"\n fi\n fi") + set( COMPILER_SCRIPT_FILE "${COMPILER_SCRIPT_FILE}\n FLAGS=\"\$FLAGS \$NEWFLAG\"\ndone") + set( COMPILER_SCRIPT_FILE "${COMPILER_SCRIPT_FILE}\n\$ECHO \"${DEFAULT_CXX_COMPILER} \$CXXFLAGS \$FLAGS\"") + set( COMPILER_SCRIPT_FILE "${COMPILER_SCRIPT_FILE}\nexec ${DEFAULT_CXX_COMPILER} \$CXXFLAGS \$FLAGS") + file(WRITE ${CMAKE_BINARY_DIR}/compiler.sh "${COMPILER_SCRIPT_FILE}" ) + exec_program(chmod ARGS "+x ${CMAKE_BINARY_DIR}/compiler.sh") + endif() +endmacro()