Skip to content

Commit 1dcb61b

Browse files
committed
CMake: dynamically detect compiler options
1 parent d88b673 commit 1dcb61b

File tree

1 file changed

+29
-29
lines changed

1 file changed

+29
-29
lines changed

CMakeLists.txt

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ cmake_minimum_required(VERSION 3.9)
22

33
project(quickjs LANGUAGES C)
44

5+
include(CheckCCompilerFlag)
56
include(GNUInstallDirs)
67

78
# TODO:
@@ -11,44 +12,43 @@ set(CMAKE_C_STANDARD_REQUIRED ON)
1112
set(CMAKE_C_EXTENSIONS ON)
1213
set(CMAKE_C_STANDARD 11)
1314

14-
add_compile_options(
15-
-Wall
16-
-Werror
17-
)
18-
if(CMAKE_C_COMPILER_ID MATCHES "AppleClang|Clang")
19-
add_compile_options(
20-
-Wextra
21-
-Wno-sign-compare
22-
-Wno-missing-field-initializers
23-
-Wno-unused-parameter
24-
-Wno-unused-variable
25-
-Wno-unused-but-set-variable
26-
-funsigned-char
27-
)
28-
else()
29-
add_compile_options(
30-
-Wno-array-bounds
31-
-Wno-format-truncation
32-
-Wno-unused-variable
33-
-Wno-unused-but-set-variable
34-
)
35-
endif()
36-
3715
if(NOT CMAKE_BUILD_TYPE)
3816
message(STATUS "No build type selected, default to Release")
3917
set(CMAKE_BUILD_TYPE "Release")
4018
endif()
4119

4220
message(STATUS "Building in ${CMAKE_BUILD_TYPE} mode")
4321
message(STATUS "Building with ${CMAKE_C_COMPILER_ID} ${CMAKE_C_COMPILER_VERSION} on ${CMAKE_SYSTEM}")
22+
23+
macro(xcheck_add_c_compiler_flag FLAG)
24+
string(REPLACE "-" "" FLAG_NO_HYPHEN ${FLAG})
25+
check_c_compiler_flag(${FLAG} COMPILER_SUPPORTS_${FLAG_NO_HYPHEN})
26+
if(COMPILER_SUPPORTS_${FLAG_NO_HYPHEN})
27+
add_compile_options(${FLAG})
28+
endif()
29+
endmacro()
30+
31+
xcheck_add_c_compiler_flag(-Wall)
32+
xcheck_add_c_compiler_flag(-Werror)
33+
# -Wextra is too spartan on GCC
34+
if(CMAKE_C_COMPILER_ID MATCHES "AppleClang|Clang")
35+
add_compile_options(-Wextra)
36+
endif()
37+
xcheck_add_c_compiler_flag(-Wno-sign-compare)
38+
xcheck_add_c_compiler_flag(-Wno-missing-field-initializers)
39+
xcheck_add_c_compiler_flag(-Wno-unused-parameter)
40+
xcheck_add_c_compiler_flag(-Wno-unused-variable)
41+
xcheck_add_c_compiler_flag(-Wno-unused-but-set-variable)
42+
xcheck_add_c_compiler_flag(-Wno-array-bounds)
43+
xcheck_add_c_compiler_flag(-Wno-format-truncation)
44+
xcheck_add_c_compiler_flag(-funsigned-char)
45+
4446
if(CMAKE_BUILD_TYPE MATCHES "Debug")
45-
add_compile_options(
46-
-ggdb
47-
-O0
48-
-fno-omit-frame-pointer
49-
)
47+
add_compile_options(-O0)
48+
xcheck_add_c_compiler_flag(-ggdb)
49+
xcheck_add_c_compiler_flag(-fno-omit-frame-pointer)
5050
else()
51-
add_compile_options(-g)
51+
xcheck_add_c_compiler_flag(-g)
5252
endif()
5353

5454
macro(xoption OPTION_NAME OPTION_TEXT OPTION_DEFAULT)

0 commit comments

Comments
 (0)