Skip to content

Commit 55f7ee1

Browse files
committed
CMake: check compiler flags are supported before adding them
Signed-off-by: Steve Lhomme <robux4@ycbcr.xyz>
1 parent bcc4287 commit 55f7ee1

2 files changed

Lines changed: 37 additions & 6 deletions

File tree

CMakeLists.txt

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ set(CMAKE_C_STANDARD 99)
104104
set(CMAKE_C_STANDARD_REQUIRED ON)
105105
set(CMAKE_C_EXTENSIONS OFF)
106106

107+
include(CheckCCompilerFlag)
108+
107109
# Set compiler flags and options.
108110
if(MSVC)
109111
message("Not supported yet!")
@@ -121,8 +123,27 @@ elseif(UNIX OR MINGW)
121123
set(OPT_DBG "-DNDEBUG") # disable assert
122124
endif()
123125

124-
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OPT_DBG} -${OPT_LV} -fomit-frame-pointer")
125-
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wno-unused-function -Wno-pointer-sign -Wno-pointer-to-int-cast")
126+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OPT_DBG} -${OPT_LV}")
127+
check_c_compiler_flag("-fomit-frame-pointer" HAS_FLAG_OMIT_FRAME_POINTER)
128+
if(HAS_FLAG_OMIT_FRAME_POINTER)
129+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fomit-frame-pointer")
130+
endif()
131+
check_c_compiler_flag("-Wall" HAS_FLAG_WARNING_ALL)
132+
if(HAS_FLAG_WARNING_ALL)
133+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall")
134+
endif()
135+
check_c_compiler_flag("-Wno-unused-function" HAS_FLAG_NO_UNUSED_FUNCTION)
136+
if(HAS_FLAG_NO_UNUSED_FUNCTION)
137+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-unused-function")
138+
endif()
139+
check_c_compiler_flag("-Wno-pointer-sign" HAS_FLAG_NO_POINTER_SIGN)
140+
if(HAS_FLAG_NO_POINTER_SIGN)
141+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-pointer-sign")
142+
endif()
143+
check_c_compiler_flag("-Wno-pointer-to-int-cast" HAS_FLAG_NO_POINTER_INT_CAST)
144+
if(HAS_FLAG_NO_POINTER_INT_CAST)
145+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-pointer-to-int-cast")
146+
endif()
126147
if(NOT WIN32)
127148
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -pthread")
128149
endif()

src/CMakeLists.txt

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ file(GLOB LIB_AVX_INC "../src/avx/oapv_*.h" )
1414
include(GenerateExportHeader)
1515
include_directories("${CMAKE_BINARY_DIR}/include")
1616

17+
include(CheckCCompilerFlag)
18+
check_c_compiler_flag("-flax-vector-conversions" HAS_ARM_VECTOR_CONVERSION)
19+
check_c_compiler_flag("-msse4.1" HAS_SSE41)
20+
check_c_compiler_flag("-mavx2" HAS_AVX2)
1721

1822
message("SYSTEM_PROCESSOR=${CMAKE_SYSTEM_PROCESSOR}")
1923

@@ -107,10 +111,16 @@ if(MSVC)
107111
endif()
108112
elseif(UNIX OR MINGW)
109113
if (ARM)
110-
set_property(SOURCE ${NEON} APPEND PROPERTY COMPILE_FLAGS "-flax-vector-conversions")
111-
elseif (X86 OR UNIVERSAL)
112-
set_property(SOURCE ${SSE} APPEND PROPERTY COMPILE_FLAGS "-msse4.1")
113-
set_property(SOURCE ${AVX} APPEND PROPERTY COMPILE_FLAGS " -mavx2")
114+
if(HAS_ARM_VECTOR_CONVERSION)
115+
set_property(SOURCE ${NEON} APPEND PROPERTY COMPILE_FLAGS "-flax-vector-conversions")
116+
endif()
117+
elseif (X86 OR UNIVERSAL)
118+
if(HAS_SSE41)
119+
set_property(SOURCE ${SSE} APPEND PROPERTY COMPILE_FLAGS "-msse4.1")
120+
endif()
121+
if(HAS_AVX2)
122+
set_property(SOURCE ${AVX} APPEND PROPERTY COMPILE_FLAGS "-mavx2")
123+
endif()
114124
endif()
115125

116126
if(OAPV_BUILD_SHARED_LIB)

0 commit comments

Comments
 (0)