tech-pkg archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

CMake compiler flags handling



Hi,

I just, again, stumbled over the CMake interaction with environment
CFLAGS, FFLAGS, etc. Since CMake insists on appending its defaults
_after_ the environment variables, this crops up from time to time over
the decades, like

	https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=640214
	https://bugzilla.redhat.com/show_bug.cgi?id=875954#c2
	https://github.com/android/ndk/issues/699
	https://github.com/android/ndk/issues/378
.
Apparently, nobody ever (successfully?) approached CMake upstream about
joining the ranks of reasonable build system authors and put its
default flags _before_ CFLAGS from the environment. Sad. Anyone got
stories to share? Did just nobody try?

So pkgsrc does such here, exclusively for GNU compilers (so a build
with Intel or clang will still do the other thing):

 if(CMAKE_BOOTSTRAP OR CMAKE_PKGSRC_BUILD_FLAGS)
    string(APPEND CMAKE_${lang}_FLAGS_MINSIZEREL_INIT " -DNDEBUG")
    string(APPEND CMAKE_${lang}_FLAGS_RELEASE_INIT " -DNDEBUG")
    string(APPEND CMAKE_${lang}_FLAGS_RELWITHDEBINFO_INIT " -g -DNDEBUG")
  else()
    string(APPEND CMAKE_${lang}_FLAGS_MINSIZEREL_INIT " -Os -DNDEBUG")
    string(APPEND CMAKE_${lang}_FLAGS_RELEASE_INIT " -O3 -DNDEBUG")
    string(APPEND CMAKE_${lang}_FLAGS_RELWITHDEBINFO_INIT " -O2 -g -DNDEBUG")
  endif()

It drops the overall CMake default optimization levels to use what we
have set up in the environment instead, while keeping expected things
like -DNDEBUG, which upstream projects might relyon, there. This seems
to be a nice trick.

But it's awfully specific to only this toolchain. I wonder … should we
extend that to others hat might possibly be relevant? GNU-Fortran comes
to mind, which does

# No -DNDEBUG for Fortran.
string(APPEND CMAKE_Fortran_FLAGS_MINSIZEREL_INIT " -Os")
string(APPEND CMAKE_Fortran_FLAGS_RELEASE_INIT " -O3")

and Intel and PGI on top … the former I have used in the past to build
a small pkgsrc tree.

Why exactly don't we rather change things at the root? The flag
appending behaviour is intended documented behaviour for CMake:

	https://cmake.org/cmake/help/latest/variable/CMAKE_LANG_FLAGS_INIT.html

But maybe they'd be open to introducing a switch similar to
CMAKE_PKGSRC_BUILD_FLAGS so that you can tell stock cmake to behave
like we want. I'm talking of the switch effecting this change:

--- Modules/CMakeCInformation.cmake.orig	2021-06-09 23:03:17.143678846 +0000
+++ Modules/CMakeCInformation.cmake
@@ -100,7 +100,7 @@ if(NOT CMAKE_MODULE_EXISTS)
   set(CMAKE_SHARED_MODULE_CREATE_C_FLAGS ${CMAKE_SHARED_LIBRARY_CREATE_C_FLAGS})
 endif()
 
-set(CMAKE_C_FLAGS_INIT "$ENV{CFLAGS} ${CMAKE_C_FLAGS_INIT}")
+set(CMAKE_C_FLAGS_INIT "${CMAKE_C_FLAGS_INIT} $ENV{CFLAGS}")
 
 cmake_initialize_per_config_variable(CMAKE_C_FLAGS "Flags used by the C compiler")

(see attachment for the obviously relevant places)

That way, -O2 or similar will still be default, but it's bettern than
nothing with gcc anyway. And any -O that is set in our flags rules.

Has this been attempted before, rejected for a specific reason? A
switch that makes cmake append CFLAGS to its internal stuff seems to be
something every packager wants. Totally overriding all flags for a
CMAKE_BUILD_TYPE might not be what works.


Alrighty then,

Thomas

-- 
Dr. Thomas Orgis
HPC @ Universität Hamburg
$NetBSD$

--- Modules/CMakeASMInformation.cmake.orig	2021-06-09 23:03:17.139679042 +0000
+++ Modules/CMakeASMInformation.cmake
@@ -65,7 +65,7 @@ endif()
 
 
 # Support for CMAKE_ASM${ASM_DIALECT}_FLAGS_INIT and friends:
-set(CMAKE_ASM${ASM_DIALECT}_FLAGS_INIT "$ENV{ASM${ASM_DIALECT}FLAGS} ${CMAKE_ASM${ASM_DIALECT}_FLAGS_INIT}")
+set(CMAKE_ASM${ASM_DIALECT}_FLAGS_INIT "${CMAKE_ASM${ASM_DIALECT}_FLAGS_INIT} $ENV{ASM${ASM_DIALECT}FLAGS}")
 
 cmake_initialize_per_config_variable(CMAKE_ASM${ASM_DIALECT}_FLAGS "Flags used by the ASM${ASM_DIALECT} compiler")
 
$NetBSD$

--- Modules/CMakeCInformation.cmake.orig	2021-06-09 23:03:17.143678846 +0000
+++ Modules/CMakeCInformation.cmake
@@ -100,7 +100,7 @@ if(NOT CMAKE_MODULE_EXISTS)
   set(CMAKE_SHARED_MODULE_CREATE_C_FLAGS ${CMAKE_SHARED_LIBRARY_CREATE_C_FLAGS})
 endif()
 
-set(CMAKE_C_FLAGS_INIT "$ENV{CFLAGS} ${CMAKE_C_FLAGS_INIT}")
+set(CMAKE_C_FLAGS_INIT "${CMAKE_C_FLAGS_INIT} $ENV{CFLAGS}")
 
 cmake_initialize_per_config_variable(CMAKE_C_FLAGS "Flags used by the C compiler")
 
$NetBSD$

--- Modules/CMakeCSharpInformation.cmake.orig	2021-06-09 23:03:17.147678651 +0000
+++ Modules/CMakeCSharpInformation.cmake
@@ -43,7 +43,7 @@ endif()
 # on the initial values computed in the platform/*.cmake files
 # use _INIT variables so that this only happens the first time
 # and you can set these flags in the cmake cache
-set(CMAKE_CSharp_FLAGS_INIT "$ENV{CSFLAGS} ${CMAKE_CSharp_FLAGS_INIT}")
+set(CMAKE_CSharp_FLAGS_INIT "${CMAKE_CSharp_FLAGS_INIT} $ENV{CSFLAGS}")
 
 cmake_initialize_per_config_variable(CMAKE_CSharp_FLAGS "Flags used by the C# compiler")
 
$NetBSD$

--- Modules/CMakeCUDAInformation.cmake.orig	2021-06-09 23:03:17.151678455 +0000
+++ Modules/CMakeCUDAInformation.cmake
@@ -85,7 +85,7 @@ endif()
 # on the initial values computed in the platform/*.cmake files
 # use _INIT variables so that this only happens the first time
 # and you can set these flags in the cmake cache
-set(CMAKE_CUDA_FLAGS_INIT "$ENV{CUDAFLAGS} ${CMAKE_CUDA_FLAGS_INIT}")
+set(CMAKE_CUDA_FLAGS_INIT "${CMAKE_CUDA_FLAGS_INIT} $ENV{CUDAFLAGS}")
 
 cmake_initialize_per_config_variable(CMAKE_CUDA_FLAGS "Flags used by the CUDA compiler")
 
$NetBSD$

--- Modules/CMakeCXXInformation.cmake.orig	2021-06-09 23:03:17.151678455 +0000
+++ Modules/CMakeCXXInformation.cmake
@@ -197,7 +197,7 @@ endforeach()
 # on the initial values computed in the platform/*.cmake files
 # use _INIT variables so that this only happens the first time
 # and you can set these flags in the cmake cache
-set(CMAKE_CXX_FLAGS_INIT "$ENV{CXXFLAGS} ${CMAKE_CXX_FLAGS_INIT}")
+set(CMAKE_CXX_FLAGS_INIT "${CMAKE_CXX_FLAGS_INIT} $ENV{CXXFLAGS}")
 
 cmake_initialize_per_config_variable(CMAKE_CXX_FLAGS "Flags used by the CXX compiler")
 
$NetBSD$

--- Modules/CMakeFortranInformation.cmake.orig	2021-06-09 23:03:17.159678064 +0000
+++ Modules/CMakeFortranInformation.cmake
@@ -159,7 +159,7 @@ endif()
 
 set(CMAKE_VERBOSE_MAKEFILE FALSE CACHE BOOL "If this value is on, makefiles will be generated without the .SILENT directive, and all commands will be echoed to the console during the make.  This is useful for debugging only. With Visual Studio IDE projects all commands are done without /nologo.")
 
-set(CMAKE_Fortran_FLAGS_INIT "$ENV{FFLAGS} ${CMAKE_Fortran_FLAGS_INIT}")
+set(CMAKE_Fortran_FLAGS_INIT "${CMAKE_Fortran_FLAGS_INIT} $ENV{FFLAGS}")
 
 cmake_initialize_per_config_variable(CMAKE_Fortran_FLAGS "Flags used by the Fortran compiler")
 
$NetBSD$

--- Modules/CMakeISPCInformation.cmake.orig	2021-06-09 23:03:17.159678064 +0000
+++ Modules/CMakeISPCInformation.cmake
@@ -26,7 +26,7 @@ endif()
 # on the initial values computed in the platform/*.cmake files
 # use _INIT variables so that this only happens the first time
 # and you can set these flags in the cmake cache
-set(CMAKE_ISPC_FLAGS_INIT "$ENV{ISPCFLAGS} ${CMAKE_ISPC_FLAGS_INIT}")
+set(CMAKE_ISPC_FLAGS_INIT "${CMAKE_ISPC_FLAGS_INIT} $ENV{ISPCFLAGS}")
 
 cmake_initialize_per_config_variable(CMAKE_ISPC_FLAGS "Flags used by the ISPC compiler")
 
$NetBSD$

--- Modules/CMakeOBJCInformation.cmake.orig	2021-06-09 23:03:17.163677868 +0000
+++ Modules/CMakeOBJCInformation.cmake
@@ -100,7 +100,7 @@ if(NOT CMAKE_MODULE_EXISTS)
   set(CMAKE_SHARED_MODULE_CREATE_OBJC_FLAGS ${CMAKE_SHARED_LIBRARY_CREATE_OBJC_FLAGS})
 endif()
 
-set(CMAKE_OBJC_FLAGS_INIT "$ENV{OBJCFLAGS} ${CMAKE_OBJC_FLAGS_INIT}")
+set(CMAKE_OBJC_FLAGS_INIT "${CMAKE_OBJC_FLAGS_INIT} $ENV{OBJCFLAGS}")
 
 cmake_initialize_per_config_variable(CMAKE_OBJC_FLAGS "Flags used by the Objective-C compiler")
 
$NetBSD$

--- Modules/CMakeOBJCXXInformation.cmake.orig	2021-06-09 23:03:17.163677868 +0000
+++ Modules/CMakeOBJCXXInformation.cmake
@@ -193,7 +193,7 @@ endforeach()
 # on the initial values computed in the platform/*.cmake files
 # use _INIT variables so that this only happens the first time
 # and you can set these flags in the cmake cache
-set(CMAKE_OBJCXX_FLAGS_INIT "$ENV{OBJCXXFLAGS} ${CMAKE_OBJCXX_FLAGS_INIT}")
+set(CMAKE_OBJCXX_FLAGS_INIT "${CMAKE_OBJCXX_FLAGS_INIT} $ENV{OBJCXXFLAGS}")
 
 cmake_initialize_per_config_variable(CMAKE_OBJCXX_FLAGS "Flags used by the Objective-C++ compiler")
 
$NetBSD$

--- Modules/CMakeRCInformation.cmake.orig	2021-06-09 23:03:17.163677868 +0000
+++ Modules/CMakeRCInformation.cmake
@@ -28,7 +28,7 @@ if(CMAKE_USER_MAKE_RULES_OVERRIDE)
   set(CMAKE_USER_MAKE_RULES_OVERRIDE "${_override}")
 endif()
 
-set(CMAKE_RC_FLAGS_INIT "$ENV{RCFLAGS} ${CMAKE_RC_FLAGS_INIT}")
+set(CMAKE_RC_FLAGS_INIT "${CMAKE_RC_FLAGS_INIT} $ENV{RCFLAGS}")
 
 cmake_initialize_per_config_variable(CMAKE_RC_FLAGS "Flags for Windows Resource Compiler")
 


Home | Main Index | Thread Index | Old Index