c-blosc2 icon indicating copy to clipboard operation
c-blosc2 copied to clipboard

CFLAGS from environment are not respected

Open mgorny opened this issue 3 years ago • 2 comments

Describe the bug Normally, CMake defaults the C compiler flags to the value of CFLAGS environment variable. However, c-blosc2 overrides CMAKE_C_FLAGS and prevents CMake from respecting that.

To Reproduce

CFLAGS="-frecord-gcc-switches" cmake .. -G Ninja
ninja -v |& grep record-gcc-switches

Expected behavior -frecord-gcc-switches should be found on the compiler command-line.

Logs n/a

System information:

  • OS: Gentoo Linux amd64
  • Compiler: gcc 12.2.1
  • Version: v2.6.1

Additional context I suppose adding something like this on top would work. However, I'm not sure if there isn't a cleaner solution:

if(NOT CMAKE_C_FLAGS)
  set(CMAKE_C_FLAGS "$ENV{CFLAGS}")
endif()

mgorny avatar Dec 28 '22 14:12 mgorny

Indeed CGLAGS seems to be the traditional standard approach to modify C options locally outside the build chain, but also inside the build chain. For example, in configure.ac you would write:

CFLAGS="$CFLAGS -std=c11"

Therefore, I wonder whether CMake should manipulate CFLAGS instead of directly modifying CMAKE_C_FLAGS:

CMake uses this environment variable value, in combination with its own builtin default flags for the toolchain, to initialize and store the CMAKE_C_FLAGS cache entry. [...] For any configuration run (including the first), the environment variable will be ignored if the CMAKE_C_FLAGS variable is already defined.

However, that approach might require some extra changes, for example here:

# Set the "-msse2" build flag only if the CMAKE_C_FLAGS is not already set.
# Probably "-msse2" should be appended to CMAKE_C_FLAGS_RELEASE.
if(CMAKE_C_COMPILER_ID STREQUAL GNU OR CMAKE_C_COMPILER_ID STREQUAL Clang OR CMAKE_C_COMPILER_ID STREQUAL Intel)
    if(NOT CMAKE_C_FLAGS AND COMPILER_SUPPORT_SSE2)
        set(CMAKE_C_FLAGS -msse2 CACHE STRING "C flags." FORCE)
    endif()
endif()

DimitriPapadopoulos avatar Feb 15 '23 20:02 DimitriPapadopoulos