Skip to content

Commit 19516ed

Browse files
committed
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.
1 parent 4b84f4b commit 19516ed

File tree

1 file changed

+18
-17
lines changed

1 file changed

+18
-17
lines changed

cmake/TryAddCompileOption.cmake

+18-17
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
11
include(CheckCCompilerFlag)
22

3-
function(try_add_compile_option option)
4-
string(MAKE_C_IDENTIFIER ${option} result)
5-
string(TOUPPER ${result} result)
6-
set(result "C_SUPPORTS${result}")
7-
set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
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}")
87
if(NOT MSVC)
98
set(CMAKE_REQUIRED_FLAGS "-Werror")
109
endif()
11-
check_c_compiler_flag(${option} ${result})
12-
if(${result})
13-
get_property(compile_options
14-
DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
15-
PROPERTY COMPILE_OPTIONS
16-
)
17-
list(APPEND compile_options "${option}")
18-
set_property(
19-
DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
20-
PROPERTY COMPILE_OPTIONS "${compile_options}"
21-
)
22-
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)
2316
endfunction()
17+
18+
# Append flags to the COMPILE_OPTIONS directory property if CC accepts them.
19+
macro(try_add_compile_option)
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)