[Dune-devel] [Dune-Commit] [Commit] dune-common - 15057f0: [Cmake][feature] allow overloading of compiler flags on the command line via a compiler script. This feature is disabled by default and can be enabled by -DALLOW_CXXFLAGS_OVERWRITE=ON.

Dominic Kempf dominic.r.kempf at gmail.com
Wed Jul 1 11:56:24 CEST 2015


Dear Robert,

I can see that your solution to this is much cleaner and more failsafe, so
I appreciate it.

I do have some technical remarks:
* exec_program is deprecated, you should use execute_process instead.
* the part in finalize, where you write the script looks much like you want
to use the configure_file command with a template.

More importantly, I consider it bad style to:
* push it to master AND release branch without ever even letting somebody
know.
* changing the name of the switch without consulting anybody.

Best,
Dominic

On Wed, Jul 1, 2015 at 11:45 AM, Robert K <robertk at posteo.org> wrote:

> New commit, appeared at Wed Jul  1 11:45:15 2015 +0200
> as part of the following ref changes:
>
>     branch refs/heads/releases/2.4    updated from b3d8b8f -> 5704c3d
>
> Browsable version:
> http://cgit.dune-project.org/repositories/dune-common/commit/?id=15057f0af4f2379cdaeef9ed0ef80c26339950db
>
> ======================================================================
>
> commit 15057f0af4f2379cdaeef9ed0ef80c26339950db
> Author: Robert K <robertk at posteo.org>
> Date:   Wed Jul 1 10:54:24 2015 +0200
>
>     [Cmake][feature] allow overloading of compiler flags on the command
> line via a compiler
>     script. This feature is disabled by default and can be enabled by
>     -DALLOW_CXXFLAGS_OVERWRITE=ON.
>
>  cmake/modules/DuneMacros.cmake            | 15 +++----
>  cmake/modules/OverloadCompilerFlags.cmake | 69
> +++++++++++++++++++++++++++++++
>  2 files changed, 77 insertions(+), 7 deletions(-)
>  create mode 100644 cmake/modules/OverloadCompilerFlags.cmake
>
>
>
> diff --git a/cmake/modules/DuneMacros.cmake
> b/cmake/modules/DuneMacros.cmake
> index 39b7ff5..1372881 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 0000000..c5783e8
> --- /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()
>
> _______________________________________________
> Dune-Commit mailing list
> Dune-Commit at dune-project.org
> http://lists.dune-project.org/mailman/listinfo/dune-commit
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.dune-project.org/pipermail/dune-devel/attachments/20150701/a86de5aa/attachment.htm>


More information about the Dune-devel mailing list