Skip to content

Commit e4af41c

Browse files
Merge bitcoin-core/secp256k1#1249: cmake: Add SECP256K1_LATE_CFLAGS configure option
42f8c51 cmake: Add `SECP256K1_LATE_CFLAGS` configure option (Hennadii Stepanov) Pull request description: This PR enables users to override compiler flags that have been set by the CMake-based build system, such as warning flags. The Autotools-based build system has the same feature out-of-the-box. See more details [here](bitcoin-core/secp256k1#1235 (comment)). Here are some examples of the new option usage: ``` cmake -S . -B build -DSECP256K1_LATE_CFLAGS="-Wno-extra -Wlong-long" ``` ``` cmake -S . -B build -DSECP256K1_BUILD_EXAMPLES=ON -DSECP256K1_LATE_CFLAGS=-O1 cmake --build build ... In function ‘secp256k1_ecmult_strauss_wnaf’, inlined from ‘secp256k1_ecmult’ at /home/hebasto/git/secp256k1/src/ecmult_impl.h:353:5: /home/hebasto/git/secp256k1/src/ecmult_impl.h:291:5: warning: ‘aux’ may be used uninitialized [-Wmaybe-uninitialized] 291 | secp256k1_ge_table_set_globalz(ECMULT_TABLE_SIZE(WINDOW_A) * no, state->pre_a, state->aux); | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ In file included from /home/hebasto/git/secp256k1/src/secp256k1.c:29: /home/hebasto/git/secp256k1/src/ecmult_impl.h: In function ‘secp256k1_ecmult’: /home/hebasto/git/secp256k1/src/group_impl.h:174:13: note: by argument 3 of type ‘const secp256k1_fe *’ to ‘secp256k1_ge_table_set_globalz’ declared here 174 | static void secp256k1_ge_table_set_globalz(size_t len, secp256k1_ge *a, const secp256k1_fe *zr) { | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ In file included from /home/hebasto/git/secp256k1/src/secp256k1.c:30: /home/hebasto/git/secp256k1/src/ecmult_impl.h:345:18: note: ‘aux’ declared here 345 | secp256k1_fe aux[ECMULT_TABLE_SIZE(WINDOW_A)]; | ^~~ ... ``` Please note that in the last case providing `env CFLAGS=-O1` or `-DCMAKE_C_FLAGS=-O1` won't work. ACKs for top commit: real-or-random: ACK 42f8c51 Tree-SHA512: 2b152e420a4a8ffd5f67857de03ae5ba9f2223e535ac01a867c1025e0619180d8255fdd1e5fb8279b290f0a1c96bcc874043ef968fcd99b1ff4e13041a91b1e1
2 parents 3bf4d68 + 42f8c51 commit e4af41c

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

CMakeLists.txt

+8
Original file line numberDiff line numberDiff line change
@@ -265,9 +265,14 @@ if(SECP256K1_BUILD_BENCHMARK OR SECP256K1_BUILD_TESTS OR SECP256K1_BUILD_EXHAUST
265265
enable_testing()
266266
endif()
267267

268+
set(SECP256K1_LATE_CFLAGS "" CACHE STRING "Compiler flags that are added to the command line after all other flags added by the build system.")
269+
include(AllTargetsCompileOptions)
270+
268271
add_subdirectory(src)
272+
all_targets_compile_options(src "${SECP256K1_LATE_CFLAGS}")
269273
if(SECP256K1_BUILD_EXAMPLES)
270274
add_subdirectory(examples)
275+
all_targets_compile_options(examples "${SECP256K1_LATE_CFLAGS}")
271276
endif()
272277

273278
message("\n")
@@ -341,6 +346,9 @@ else()
341346
message(" - LDFLAGS for executables ............ ${CMAKE_EXE_LINKER_FLAGS_DEBUG}")
342347
message(" - LDFLAGS for shared libraries ....... ${CMAKE_SHARED_LINKER_FLAGS_DEBUG}")
343348
endif()
349+
if(SECP256K1_LATE_CFLAGS)
350+
message("SECP256K1_LATE_CFLAGS ................. ${SECP256K1_LATE_CFLAGS}")
351+
endif()
344352
message("\n")
345353
if(SECP256K1_EXPERIMENTAL)
346354
message(

cmake/AllTargetsCompileOptions.cmake

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Add compile options to all targets added in the subdirectory.
2+
function(all_targets_compile_options dir options)
3+
get_directory_property(targets DIRECTORY ${dir} BUILDSYSTEM_TARGETS)
4+
separate_arguments(options)
5+
set(compiled_target_types STATIC_LIBRARY SHARED_LIBRARY OBJECT_LIBRARY EXECUTABLE)
6+
foreach(target ${targets})
7+
get_target_property(type ${target} TYPE)
8+
if(type IN_LIST compiled_target_types)
9+
target_compile_options(${target} PRIVATE ${options})
10+
endif()
11+
endforeach()
12+
endfunction()

0 commit comments

Comments
 (0)