From 19516ed3e9efe43b00d75820fb6590dcbed548b3 Mon Sep 17 00:00:00 2001 From: Hennadii Stepanov <32963518+hebasto@users.noreply.github.com> Date: Thu, 27 Apr 2023 14:39:10 +0100 Subject: [PATCH 1/3] cmake: Use `add_compile_options()` in `try_add_compile_option()` This change drops tinkering with the `COMPILE_OPTIONS` directory property. Also `try_add_compile_option()` can handle a list of flags now, if they are required to be checked simultaneously. An explanatory comments have been added as well. --- cmake/TryAddCompileOption.cmake | 35 +++++++++++++++++---------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/cmake/TryAddCompileOption.cmake b/cmake/TryAddCompileOption.cmake index f53c252c..9a99e4f2 100644 --- a/cmake/TryAddCompileOption.cmake +++ b/cmake/TryAddCompileOption.cmake @@ -1,23 +1,24 @@ include(CheckCCompilerFlag) -function(try_add_compile_option option) - string(MAKE_C_IDENTIFIER ${option} result) - string(TOUPPER ${result} result) - set(result "C_SUPPORTS${result}") - set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY) +function(secp256k1_check_c_flags_internal flags output) + string(MAKE_C_IDENTIFIER "${flags}" result) + string(TOUPPER "${result}" result) + set(result "C_SUPPORTS_${result}") if(NOT MSVC) set(CMAKE_REQUIRED_FLAGS "-Werror") endif() - check_c_compiler_flag(${option} ${result}) - if(${result}) - get_property(compile_options - DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}" - PROPERTY COMPILE_OPTIONS - ) - list(APPEND compile_options "${option}") - set_property( - DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}" - PROPERTY COMPILE_OPTIONS "${compile_options}" - ) - endif() + + # This avoids running a linker. + set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY) + check_c_compiler_flag("${flags}" ${result}) + + set(${output} ${${result}} PARENT_SCOPE) endfunction() + +# Append flags to the COMPILE_OPTIONS directory property if CC accepts them. +macro(try_add_compile_option) + secp256k1_check_c_flags_internal("${ARGV}" result) + if(result) + add_compile_options(${ARGV}) + endif() +endmacro() From 6ece1507cb11a897a98052f34a374ec00e83cb86 Mon Sep 17 00:00:00 2001 From: Hennadii Stepanov <32963518+hebasto@users.noreply.github.com> Date: Wed, 22 Mar 2023 10:35:49 +0000 Subject: [PATCH 2/3] cmake, refactor: Rename `try_add_compile_option` to `try_append_cflags` Actually, `try_append_cflags()` can handle a list of flags, and the new name is similar to the one used in `configure.ac`. --- CMakeLists.txt | 34 +++++++++---------- ...pileOption.cmake => TryAppendCFlags.cmake} | 2 +- 2 files changed, 18 insertions(+), 18 deletions(-) rename cmake/{TryAddCompileOption.cmake => TryAppendCFlags.cmake} (95%) diff --git a/CMakeLists.txt b/CMakeLists.txt index fe716c8b..ac18cf83 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -191,25 +191,25 @@ else() endif() endif() -include(TryAddCompileOption) +include(TryAppendCFlags) if(MSVC) - try_add_compile_option(/W2) - try_add_compile_option(/wd4146) + try_append_c_flags(/W2) + try_append_c_flags(/wd4146) else() - try_add_compile_option(-pedantic) - try_add_compile_option(-Wall) - try_add_compile_option(-Wcast-align) - try_add_compile_option(-Wcast-align=strict) - try_add_compile_option(-Wconditional-uninitialized) - try_add_compile_option(-Wextra) - try_add_compile_option(-Wnested-externs) - try_add_compile_option(-Wno-long-long) - try_add_compile_option(-Wno-overlength-strings) - try_add_compile_option(-Wno-unused-function) - try_add_compile_option(-Wreserved-identifier) - try_add_compile_option(-Wshadow) - try_add_compile_option(-Wstrict-prototypes) - try_add_compile_option(-Wundef) + try_append_c_flags(-pedantic) + try_append_c_flags(-Wall) + try_append_c_flags(-Wcast-align) + try_append_c_flags(-Wcast-align=strict) + try_append_c_flags(-Wconditional-uninitialized) + try_append_c_flags(-Wextra) + try_append_c_flags(-Wnested-externs) + try_append_c_flags(-Wno-long-long) + try_append_c_flags(-Wno-overlength-strings) + try_append_c_flags(-Wno-unused-function) + try_append_c_flags(-Wreserved-identifier) + try_append_c_flags(-Wshadow) + try_append_c_flags(-Wstrict-prototypes) + try_append_c_flags(-Wundef) endif() set(CMAKE_C_VISIBILITY_PRESET hidden) diff --git a/cmake/TryAddCompileOption.cmake b/cmake/TryAppendCFlags.cmake similarity index 95% rename from cmake/TryAddCompileOption.cmake rename to cmake/TryAppendCFlags.cmake index 9a99e4f2..1d81a931 100644 --- a/cmake/TryAddCompileOption.cmake +++ b/cmake/TryAppendCFlags.cmake @@ -16,7 +16,7 @@ function(secp256k1_check_c_flags_internal flags output) endfunction() # Append flags to the COMPILE_OPTIONS directory property if CC accepts them. -macro(try_add_compile_option) +macro(try_append_c_flags) secp256k1_check_c_flags_internal("${ARGV}" result) if(result) add_compile_options(${ARGV}) From a8d059f76cb3429381adda1193c3d1976ba3cab4 Mon Sep 17 00:00:00 2001 From: Hennadii Stepanov <32963518+hebasto@users.noreply.github.com> Date: Wed, 22 Mar 2023 10:37:16 +0000 Subject: [PATCH 3/3] cmake, doc: Document compiler flags --- CMakeLists.txt | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index ac18cf83..86ffd931 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -193,20 +193,22 @@ endif() include(TryAppendCFlags) if(MSVC) - try_append_c_flags(/W2) - try_append_c_flags(/wd4146) + # Keep the following commands ordered lexicographically. + try_append_c_flags(/W2) # Moderate warning level. + try_append_c_flags(/wd4146) # Disable warning C4146 "unary minus operator applied to unsigned type, result still unsigned". else() + # Keep the following commands ordered lexicographically. try_append_c_flags(-pedantic) - try_append_c_flags(-Wall) - try_append_c_flags(-Wcast-align) - try_append_c_flags(-Wcast-align=strict) - try_append_c_flags(-Wconditional-uninitialized) - try_append_c_flags(-Wextra) + try_append_c_flags(-Wall) # GCC >= 2.95 and probably many other compilers. + try_append_c_flags(-Wcast-align) # GCC >= 2.95. + try_append_c_flags(-Wcast-align=strict) # GCC >= 8.0. + try_append_c_flags(-Wconditional-uninitialized) # Clang >= 3.0 only. + try_append_c_flags(-Wextra) # GCC >= 3.4, this is the newer name of -W, which we don't use because older GCCs will warn about unused functions. try_append_c_flags(-Wnested-externs) - try_append_c_flags(-Wno-long-long) - try_append_c_flags(-Wno-overlength-strings) - try_append_c_flags(-Wno-unused-function) - try_append_c_flags(-Wreserved-identifier) + try_append_c_flags(-Wno-long-long) # GCC >= 3.0, -Wlong-long is implied by -pedantic. + try_append_c_flags(-Wno-overlength-strings) # GCC >= 4.2, -Woverlength-strings is implied by -pedantic. + try_append_c_flags(-Wno-unused-function) # GCC >= 3.0, -Wunused-function is implied by -Wall. + try_append_c_flags(-Wreserved-identifier) # Clang >= 13.0 only. try_append_c_flags(-Wshadow) try_append_c_flags(-Wstrict-prototypes) try_append_c_flags(-Wundef)