Skip to content

Commit 13f95d0

Browse files
author
Fytch
committed
CMake: add target "ProgramOptionsHxx" so that users can link against the library when they've added it via add_subdirectory
1 parent e82a49b commit 13f95d0

File tree

1 file changed

+55
-42
lines changed

1 file changed

+55
-42
lines changed

CMakeLists.txt

Lines changed: 55 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,69 @@
1-
cmake_minimum_required( VERSION 3.1 )
2-
project( ProgramOptions )
1+
cmake_minimum_required( VERSION 3.5 )
2+
3+
if( NOT DEFINED PROJECT_NAME )
4+
set( PROGRAMOPTIONS_NOT_SUBPROJECT ON )
5+
endif()
6+
7+
project( ProgramOptions LANGUAGES CXX VERSION 1.0.0 )
38
set( CMAKE_CXX_STANDARD 11 )
49
set( CMAKE_CXX_STANDARD_REQUIRED ON )
510
set( CMAKE_CXX_EXTENSIONS OFF )
611

7-
option( PROGRAMOPTIONS_NO_EXCEPTIONS "disable exceptions" OFF )
8-
option( PROGRAMOPTIONS_NO_COLORS "disable colored output" OFF )
9-
option( PROGRAMOPTIONS_BUILD_TEST "build test" ON )
10-
option( PROGRAMOPTIONS_BUILD_EXAMPLES "build examples" ON )
11-
12-
if( CMAKE_CXX_COMPILER_ID MATCHES "Clang" OR
13-
CMAKE_CXX_COMPILER_ID MATCHES "GNU" OR
14-
CMAKE_CXX_COMPILER_ID MATCHES "Intel" )
15-
set( PROGRAMOPTIONS_GNU_OPTIONS TRUE )
16-
elseif( CMAKE_CXX_COMPILER_ID MATCHES "MSVC" )
17-
set( PROGRAMOPTIONS_MS_OPTIONS TRUE )
18-
endif()
12+
add_library( ProgramOptionsHxx INTERFACE )
13+
target_include_directories( ProgramOptionsHxx INTERFACE "${CMAKE_CURRENT_LIST_DIR}/include" )
1914

20-
include_directories( "${CMAKE_CURRENT_LIST_DIR}/include/" )
15+
if( PROGRAMOPTIONS_NOT_SUBPROJECT )
16+
option( PROGRAMOPTIONS_NO_EXCEPTIONS "disable exceptions" OFF )
17+
option( PROGRAMOPTIONS_NO_COLORS "disable colored output" OFF )
18+
option( PROGRAMOPTIONS_BUILD_TEST "build test" ON )
19+
option( PROGRAMOPTIONS_BUILD_EXAMPLES "build examples" ON )
2120

22-
if( PROGRAMOPTIONS_GNU_OPTIONS )
23-
set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Wpedantic -Wno-deprecated -fno-rtti" )
24-
set( CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -g -ggdb -fno-omit-frame-pointer" )
25-
set( CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O3 -march=native -DNDEBUG -s -flto" )
26-
endif()
21+
if( CMAKE_CXX_COMPILER_ID MATCHES "Clang" OR
22+
CMAKE_CXX_COMPILER_ID MATCHES "GNU" OR
23+
CMAKE_CXX_COMPILER_ID MATCHES "Intel" )
24+
set( PROGRAMOPTIONS_GNU_OPTIONS TRUE )
25+
elseif( CMAKE_CXX_COMPILER_ID MATCHES "MSVC" )
26+
set( PROGRAMOPTIONS_MS_OPTIONS TRUE )
27+
endif()
2728

28-
if( PROGRAMOPTIONS_NO_EXCEPTIONS )
29-
add_definitions( -DPROGRAMOPTIONS_NO_EXCEPTIONS )
3029
if( PROGRAMOPTIONS_GNU_OPTIONS )
31-
set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-exceptions" )
32-
elseif( PROGRAMOPTIONS_MS_OPTIONS )
33-
set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /EHsc" )
30+
set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Wpedantic -Wno-deprecated -fno-rtti" )
31+
set( CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -g -ggdb -fno-omit-frame-pointer" )
32+
set( CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O3 -march=native -DNDEBUG -s -flto" )
3433
endif()
35-
endif()
3634

37-
if( PROGRAMOPTIONS_NO_COLORS )
38-
add_definitions( -DPROGRAMOPTIONS_NO_COLORS )
39-
endif()
35+
if( PROGRAMOPTIONS_NO_EXCEPTIONS )
36+
add_definitions( -DPROGRAMOPTIONS_NO_EXCEPTIONS )
37+
if( PROGRAMOPTIONS_GNU_OPTIONS )
38+
set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-exceptions" )
39+
elseif( PROGRAMOPTIONS_MS_OPTIONS )
40+
set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /EHsc" )
41+
endif()
42+
endif()
4043

41-
if( PROGRAMOPTIONS_BUILD_TEST )
42-
add_subdirectory( "${CMAKE_CURRENT_LIST_DIR}/ext/Catch2" )
44+
if( PROGRAMOPTIONS_NO_COLORS )
45+
add_definitions( -DPROGRAMOPTIONS_NO_COLORS )
46+
endif()
4347

44-
file( GLOB_RECURSE test_src test/*.cxx )
45-
add_executable( Test ${test_src} )
46-
target_link_libraries( Test Catch2::Catch2 )
47-
endif()
48+
if( PROGRAMOPTIONS_BUILD_TEST )
49+
add_subdirectory( "${CMAKE_CURRENT_LIST_DIR}/ext/Catch2" )
4850

49-
if( PROGRAMOPTIONS_BUILD_EXAMPLES )
50-
file( GLOB_RECURSE examples_src examples/*.cxx )
51-
foreach( src_name ${examples_src} )
52-
# CMake's regex engine doesn't support lazy evaluation (?), hence the hack below
53-
string( REGEX REPLACE "^.*[^a-zA-Z_]([a-zA-Z_]+)\\.cxx$" "\\1" exe_name ${src_name} )
54-
add_executable( ${exe_name} ${src_name} )
55-
endforeach( src_name ${examples_src} )
51+
file( GLOB_RECURSE test_src test/*.cxx )
52+
add_executable( Test ${test_src} )
53+
target_link_libraries( Test
54+
Catch2::Catch2
55+
ProgramOptionsHxx
56+
)
57+
endif()
58+
59+
if( PROGRAMOPTIONS_BUILD_EXAMPLES )
60+
file( GLOB_RECURSE examples_src examples/*.cxx )
61+
foreach( src_name ${examples_src} )
62+
# CMake's regex engine doesn't support lazy evaluation (?), hence the hack below
63+
string( REGEX REPLACE "^.*[^a-zA-Z_]([a-zA-Z_]+)\\.cxx$" "\\1" exe_name ${src_name} )
64+
add_executable( ${exe_name} ${src_name} )
65+
66+
target_link_libraries( ${exe_name} ProgramOptionsHxx )
67+
endforeach( src_name ${examples_src} )
68+
endif()
5669
endif()

0 commit comments

Comments
 (0)