Skip to content

Commit 024a409

Browse files
Merge bitcoin-core/secp256k1#1240: cmake: Improve and document compiler flag checks
a8d059f cmake, doc: Document compiler flags (Hennadii Stepanov) 6ece150 cmake, refactor: Rename `try_add_compile_option` to `try_append_cflags` (Hennadii Stepanov) 19516ed cmake: Use `add_compile_options()` in `try_add_compile_option()` (Hennadii Stepanov) Pull request description: This PR: - drops tinkering with the `COMPILE_OPTIONS` directory property in `try_add_compile_option()` and renames it to `try_append_cflags()` - copies related comments from `configure.ac` ACKs for top commit: theuni: ACK bitcoin-core/secp256k1@a8d059f . Tree-SHA512: 7ac011c135e12a65c45f4feb7cd74fd2d961ed77252afecf3a66e2af1d57facab446120c63696507b5ecd5bdb3eee1521760a53028b914c429652d00d03a4462
2 parents 4b84f4b + a8d059f commit 024a409

File tree

3 files changed

+43
-40
lines changed

3 files changed

+43
-40
lines changed

CMakeLists.txt

+19-17
Original file line numberDiff line numberDiff line change
@@ -191,25 +191,27 @@ else()
191191
endif()
192192
endif()
193193

194-
include(TryAddCompileOption)
194+
include(TryAppendCFlags)
195195
if(MSVC)
196-
try_add_compile_option(/W2)
197-
try_add_compile_option(/wd4146)
196+
# Keep the following commands ordered lexicographically.
197+
try_append_c_flags(/W2) # Moderate warning level.
198+
try_append_c_flags(/wd4146) # Disable warning C4146 "unary minus operator applied to unsigned type, result still unsigned".
198199
else()
199-
try_add_compile_option(-pedantic)
200-
try_add_compile_option(-Wall)
201-
try_add_compile_option(-Wcast-align)
202-
try_add_compile_option(-Wcast-align=strict)
203-
try_add_compile_option(-Wconditional-uninitialized)
204-
try_add_compile_option(-Wextra)
205-
try_add_compile_option(-Wnested-externs)
206-
try_add_compile_option(-Wno-long-long)
207-
try_add_compile_option(-Wno-overlength-strings)
208-
try_add_compile_option(-Wno-unused-function)
209-
try_add_compile_option(-Wreserved-identifier)
210-
try_add_compile_option(-Wshadow)
211-
try_add_compile_option(-Wstrict-prototypes)
212-
try_add_compile_option(-Wundef)
200+
# Keep the following commands ordered lexicographically.
201+
try_append_c_flags(-pedantic)
202+
try_append_c_flags(-Wall) # GCC >= 2.95 and probably many other compilers.
203+
try_append_c_flags(-Wcast-align) # GCC >= 2.95.
204+
try_append_c_flags(-Wcast-align=strict) # GCC >= 8.0.
205+
try_append_c_flags(-Wconditional-uninitialized) # Clang >= 3.0 only.
206+
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.
207+
try_append_c_flags(-Wnested-externs)
208+
try_append_c_flags(-Wno-long-long) # GCC >= 3.0, -Wlong-long is implied by -pedantic.
209+
try_append_c_flags(-Wno-overlength-strings) # GCC >= 4.2, -Woverlength-strings is implied by -pedantic.
210+
try_append_c_flags(-Wno-unused-function) # GCC >= 3.0, -Wunused-function is implied by -Wall.
211+
try_append_c_flags(-Wreserved-identifier) # Clang >= 13.0 only.
212+
try_append_c_flags(-Wshadow)
213+
try_append_c_flags(-Wstrict-prototypes)
214+
try_append_c_flags(-Wundef)
213215
endif()
214216

215217
set(CMAKE_C_VISIBILITY_PRESET hidden)

cmake/TryAddCompileOption.cmake

-23
This file was deleted.

cmake/TryAppendCFlags.cmake

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
include(CheckCCompilerFlag)
2+
3+
function(secp256k1_check_c_flags_internal flags output)
4+
string(MAKE_C_IDENTIFIER "${flags}" result)
5+
string(TOUPPER "${result}" result)
6+
set(result "C_SUPPORTS_${result}")
7+
if(NOT MSVC)
8+
set(CMAKE_REQUIRED_FLAGS "-Werror")
9+
endif()
10+
11+
# This avoids running a linker.
12+
set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
13+
check_c_compiler_flag("${flags}" ${result})
14+
15+
set(${output} ${${result}} PARENT_SCOPE)
16+
endfunction()
17+
18+
# Append flags to the COMPILE_OPTIONS directory property if CC accepts them.
19+
macro(try_append_c_flags)
20+
secp256k1_check_c_flags_internal("${ARGV}" result)
21+
if(result)
22+
add_compile_options(${ARGV})
23+
endif()
24+
endmacro()

0 commit comments

Comments
 (0)