-
Notifications
You must be signed in to change notification settings - Fork 4
/
CMakeLists.txt
48 lines (36 loc) · 1.6 KB
/
CMakeLists.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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 CC=${CMAKE_C_COMPILER} CXX=${CMAKE_CXX_COMPILER} LD=${CMAKE_CXX_COMPILER} cmake_info
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
OUTPUT_VARIABLE MAKE_OUTPUT
)
function(extract_var SEPARATOR DEST_VARIABLE MAKE_OUTPUT)
string(REGEX MATCH "${SEPARATOR} .* ${SEPARATOR}" TMP "${MAKE_OUTPUT}")
string(REGEX REPLACE "${SEPARATOR} (.*) ${SEPARATOR}" "\\1" TMP "${TMP}")
separate_arguments(TMP)
set(${DEST_VARIABLE} ${TMP} PARENT_SCOPE)
endfunction()
extract_var(SEPARATOR_SRC ABC_SRC ${MAKE_OUTPUT})
extract_var(SEPARATOR_LIBS ABC_LIBS ${MAKE_OUTPUT})
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_filtered(abc PRIVATE ${ABC_CFLAGS} -Wno-unused-but-set-variable )
target_link_libraries(abc PRIVATE ${ABC_LIBS})