ecmwf / ecbuild

A CMake-based build system, consisting of a collection of CMake macros and functions that ease the managing of software build systems
https://ecbuild.readthedocs.io
Apache License 2.0
26 stars 25 forks source link

Avoid ecbuild's default compiler flags being used #45

Open climbfuji opened 2 years ago

climbfuji commented 2 years ago

When I build a project with ecbuild, the software automatically adds its own compiler flags into the project, for example https://github.com/ecmwf/ecbuild/blob/develop/cmake/compiler_flags/Intel_Fortran.cmake

I don't want that, in fact, I think ecbuild should never impose its own flags. Projects are written around the default cmake flags, depending on the build type, and only the software itself knows which flags are appropriate and needed.

For the above example Intel/Fortran, there is a -check all flag. Not every projects wants that, and there is no way to get rid of it except manually editing an ecbuild installation (which not everyone has the rights to do so) and commenting out those flags.

Question: Is there a way to disable using the ecbuild default flags using a cmake macro/flag to pass to ecbuild? I looked in https://github.com/ecmwf/ecbuild/blob/develop/bin/ecbuild but didn't see anything.

Ultimately, I think ecbuild should not add any additional flags and purely rely on the default flags for the cmake build type and what the developers add as appropriate for their own code.

wdeconinck commented 2 years ago

Hello thanks for your suggestions. After some internal discussions we do agree that some flags, e.g. '-check all' may be problematic for legacy projects especially. We will review all flags soon. We however don't intend to remove them all.

ecBuild does however allow to override the default behaviour. The procedure is as follows:

  1. ecBuild does not set CMAKE_<lang>_FLAGS i.e. the user can set these via -D on the CMake cache and these will be the "base" flags.
  2. 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 -D or the CMake cache has no effect.
  3. Any value the user provides via ECBUILD_<lang>_FLAGS or ECBUILD_<lang>_FLAGS_<bytpe> overrides the corresponding CMAKE_<lang>_FLAGS or CMAKE_<lang>_FLAGS_<btype> without being written to the CMake cache.
climbfuji commented 2 years ago

Thanks, @wdeconinck for the explanation and for your willingness to review the flags. It's good that there is a workaround for the time being. Please close this issue or keep it open until the review was done, up to you.

antoine-morvan commented 1 year ago

Hello,

I see this issue still open, so I'll add my 2 cents

I'm building fiat and estrans projects, where I want to specify custom flags to the compiler. I have tried so far, with a deliberate example :

export MYCFLAGS="-O0"

## Test 1
-DCMAKE_BUILD_TYPE="Release" \
-DCMAKE_C_FLAGS="$MYCFLAGS"
# Produces in the CMake summary : 'flags      : -O0 -O3 -DNDEBUG'

## Test 2
-DCMAKE_BUILD_TYPE="Release" \
-DCMAKE_C_FLAGS_RELEASE="$MYCFLAGS"
# Produces in the CMake summary : 'flags      : -O0 -O3 -DNDEBUG'

-O0 -O3 -DNDEBUG
## Test 3
-DCMAKE_BUILD_TYPE="Release" \
-DECBUILD_C_FLAGS="$MYCFLAGS"
# Produces in the CMake summary : 'flags      : -O0 -O3 -DNDEBUG'

## Test 4
-DCMAKE_BUILD_TYPE="Release" \
-DECBUILD_C_FLAGS_RELEASE="$MYCFLAGS"
# Produces in the CMake summary : 'flags      : -O0'

From this, we can observe that the flags can effectively be overridden. However, only the ECBUILD_C_FLAGS_RELEASE is effective. Indeed, compilers interpret the command line argument in order, the later ones having higher priority. -O1 -O0 -O3 is similar to -O3.

Giving room for the developper to play with flags, with working (semanticaly) defaults, and using CMake standard would be great :)

tlmquintino commented 1 year ago

I think this issue stems from a misunderstanding of the role of the build type. Release has a meaning -- it means optimise my code, with whatever flags the build system thinks are appropriate.

So if you want to fully control the flags, you are basically saying "I know better, this is what I want to do". This is what you seem to suggest from your example -- you want "-O0"

So then the mechanism is not to use the CMAKE_BUILD_TYPE="Release".

Just set DCMAKE_BUILD_TYPE=None and set whatever flags you want using CMAKE_C_FLAGS Example: cmake <source_dir> -DCMAKE_BUILD_TYPE=None -DCMAKE_C_FLAGS="-O0" -DCMAKE_CXX_FLAGS="-O0"