CMake: only add compiler flags that are actually compatible with the compiler being used - the compiler detection logic in the Makefile is not perfect and that sometime results in weird conflicts

This commit is contained in:
Baruch Sterin 2015-10-23 14:44:17 -07:00
parent 9322493e90
commit 0f64f3b7ab
1 changed files with 20 additions and 2 deletions

View File

@ -1,9 +1,27 @@
cmake_minimum_required(VERSION 2.8.12)
include(CMakeParseArguments)
include(CheckCCompilerFlag)
# filter out flags that are not appropriate for the compiler used
function(target_compile_options_filtered target visibility)
foreach( flag ${ARGN} )
if( flag MATCHES "^-D.*" )
target_compile_options( ${target} ${visibility} ${flag} )
else()
check_c_compiler_flag( ${flag} COMPILER_SUPPORTS__${flag} )
if( COMPILER_SUPPORTS__${flag} )
target_compile_options( ${target} ${visibility} ${flag} )
endif()
endif()
endforeach()
endfunction()
project(abc)
# run make to extract compiler options, linker options and list of source files
execute_process(
COMMAND make ABC_MAKE_NO_DEPS=1 cmake_info
COMMAND make ABC_MAKE_NO_DEPS=1 CC=${CMAKE_C_COMPILER} CXX=${CMAKE_CXX_COMPILER} LD=${CMAKE_CXX_COMPILER} cmake_info
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
OUTPUT_VARIABLE MAKE_OUTPUT
)
@ -26,5 +44,5 @@ extract_var(SEPARATOR_CFLAGS ABC_CFLAGS ${MAKE_OUTPUT})
add_executable(abc ${ABC_SRC})
target_include_directories(abc PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src )
target_compile_options(abc PRIVATE ${ABC_CFLAGS} )
target_compile_options_filtered(abc PRIVATE ${ABC_CFLAGS} -Wno-unused-but-set-variable )
target_link_libraries(abc PRIVATE ${ABC_LIBS})