1
- # CMake script for testsweeper framework
2
- # needed to build BLASPP/test, LAPACK /test, SLATE/test
1
+ # CMake script for TestSweeper framework
2
+ # needed to build BLASPP/test, LAPACKPP /test, SLATE/test
3
3
# repo: http://bitbucket.org/icl/testsweeper
4
4
5
- cmake_minimum_required (VERSION 3.2)
5
+ cmake_minimum_required ( VERSION 3.8 )
6
+ # 3.1 target_compile_features
7
+ # 3.8 target_compile_features( cxx_std_11 )
8
+ # 3.14 install( LIBRARY DESTINATION lib ) default
9
+ # 3.15 $<$COMPILE_LANG_AND_ID # optional
6
10
7
- project (TESTSWEEPER
11
+ project (
12
+ testsweeper
13
+ VERSION 2020.06.0
8
14
LANGUAGES CXX
9
15
)
10
16
11
- option (BUILD_TESTS "build and run tests" ON )
17
+ #-------------------------------------------------------------------------------
18
+ # Options
19
+ option ( BUILD_SHARED_LIBS "Build shared libraries" true )
20
+ option ( BUILD_TESTS "Build and run tests" true )
21
+ option ( COLOR "Use ANSI color output" true )
22
+
23
+ # Default prefix=/opt/slate
24
+ if (CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
25
+ set ( CMAKE_INSTALL_PREFIX "/opt/slate"
26
+ CACHE PATH
27
+ "Install path prefix, prepended onto install directories."
28
+ FORCE
29
+ )
30
+ message ( STATUS "Setting CMAKE_INSTALL_PREFIX = ${CMAKE_INSTALL_PREFIX} " )
31
+ endif ()
12
32
13
- string (TOLOWER "${CMAKE_CURRENT_SOURCE_DIR} " SOURCE_DIR_LOWER)
14
- string (TOLOWER "${CMAKE_CURRENT_BINARY_DIR} " BINARY_DIR_LOWER )
15
- if (SOURCE_DIR_LOWER STREQUAL BINARY_DIR_LOWER)
16
- message (FATAL_ERROR "Compiling TESTSWEEPER with CMake requires an out-of-source build. To proceed:
33
+ # Provide menu of options. (Why doesn't CMake do this?)
34
+ set_property ( CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS
35
+ None Debug Release RelWithDebInfo MinSizeRel )
36
+
37
+ #-------------------------------------------------------------------------------
38
+ # Require build directory.
39
+ string ( TOLOWER "${CMAKE_CURRENT_SOURCE_DIR} " source_dir_lower )
40
+ string ( TOLOWER "${CMAKE_CURRENT_BINARY_DIR} " binary_dir_lower )
41
+ if (source_dir_lower STREQUAL binary_dir_lower)
42
+ message ( FATAL_ERROR
43
+ "Compiling TestSweeper with CMake requires an out-of-source build. To proceed:
17
44
rm -rf CMakeCache.txt CMakeFiles/ # delete files in ${CMAKE_CURRENT_SOURCE_DIR}
18
45
mkdir build
19
46
cd build
20
47
cmake ..
21
- make" )
22
- endif ()
23
-
24
- if (CMAKE_HOST_APPLE )
25
- set (CMAKE_MACOSX_RPATH 1)
48
+ make"
49
+ )
26
50
endif ()
27
51
28
- add_library (testsweeper SHARED
52
+ #-------------------------------------------------------------------------------
53
+ # Build library.
54
+ add_library (
55
+ testsweeper
29
56
testsweeper.cc
30
57
version .cc
31
58
)
32
59
33
- target_include_directories (testsweeper PUBLIC
34
- $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR} >
60
+ # Include directory.
61
+ # During build it's source dir; after install it's {prefix}/include.
62
+ target_include_directories (
63
+ testsweeper
64
+ PUBLIC
65
+ "$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR} >"
66
+ "$<INSTALL_INTERFACE:include>"
35
67
)
36
68
37
- # TODO: ensure that hg command exists as well
38
- if (EXISTS ${CMAKE_CURRENT_SOURCE_DIR} /.hg)
39
- execute_process (COMMAND hg id -i ${CMAKE_CURRENT_SOURCE_DIR} OUTPUT_VARIABLE testsweeper_id)
40
- string (STRIP ${testsweeper_id} testsweeper_id)
41
- target_compile_definitions (testsweeper PUBLIC
42
- TESTSWEEPER_ID="${testsweeper_id} "
43
- )
69
+ # Get hg id.
70
+ # todo: replace with `git rev-parse --short HEAD`.
71
+ if (EXISTS "${CMAKE_CURRENT_SOURCE_DIR} /.hg" )
72
+ execute_process ( COMMAND hg id -i ${CMAKE_CURRENT_SOURCE_DIR}
73
+ OUTPUT_VARIABLE testsweeper_id )
74
+ string ( STRIP "${testsweeper_id} " testsweeper_id )
75
+ message ( STATUS "testsweeper_id = ${testsweeper_id} " )
76
+ target_compile_definitions (
77
+ testsweeper PRIVATE TESTSWEEPER_ID="${testsweeper_id} " )
44
78
endif ()
45
79
46
- set_target_properties (testsweeper PROPERTIES
47
- CXX_STANDARD 11
48
- CXX_STANDARD_REQUIRED YES
49
- CXX_EXTENSIONS NO
50
- )
80
+ # Use and export -std=c++11; don't allow -std=gnu++11 extensions.
81
+ target_compile_features ( testsweeper PUBLIC cxx_std_11 )
82
+ set_target_properties ( testsweeper PROPERTIES CXX_EXTENSIONS false )
51
83
52
- if (NO_COLOR)
53
- target_compile_definitions (testsweeper PUBLIC
54
- NO_COLOR
55
- )
84
+ if (CMAKE_VERSION VERSION_GREATER_EQUAL 3.15)
85
+ # Conditionally add -Wall. See CMake tutorial.
86
+ set ( gcc_like_cxx "$<COMPILE_LANG_AND_ID:CXX,ARMClang,AppleClang,Clang,GNU>" )
87
+ target_compile_options (
88
+ testsweeper PRIVATE "$<${gcc_like_cxx} :$<BUILD_INTERFACE:-Wall>>" )
56
89
endif ()
57
90
58
- if (CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
59
- set (CMAKE_INSTALL_PREFIX "/opt/slate"
60
- CACHE PATH
61
- "Install path prefix, prepended onto install directories."
62
- FORCE
63
- )
91
+ if (NOT COLOR)
92
+ target_compile_definitions ( testsweeper PUBLIC "NO_COLOR" )
64
93
endif ()
65
94
66
- #message("cmake install prefix: " ${CMAKE_INSTALL_PREFIX})
67
- install (TARGETS testsweeper
95
+ #-------------------------------------------------------------------------------
96
+ # Install libtestsweeper, and add to testsweeperTargets.cmake
97
+ install (
98
+ TARGETS testsweeper
68
99
EXPORT testsweeperTargets
69
- LIBRARY DESTINATION lib
70
- ARCHIVE DESTINATION lib
71
- INCLUDES DESTINATION include
100
+ LIBRARY DESTINATION "lib" # no default before 3.14
72
101
)
73
102
74
- install (
75
- EXPORT testsweeperTargets
76
- DESTINATION lib/testsweeper
103
+ # Install header file
104
+ install (
105
+ FILES "testsweeper.hh"
106
+ DESTINATION "include"
77
107
)
78
108
109
+ # Install <package>Targets.cmake
79
110
install (
80
- FILES testsweeper.hh
81
- DESTINATION include
111
+ EXPORT testsweeperTargets
112
+ DESTINATION "lib/testsweeper"
82
113
)
83
114
84
- configure_file (testsweeperConfig.cmake.in
85
- testsweeperConfig.cmake
86
- COPYONLY
115
+ # Also export <package>Targets.cmake in build directory
116
+ export (
117
+ EXPORT testsweeperTargets
118
+ FILE "testsweeperTargets.cmake"
87
119
)
88
120
89
- install (
90
- FILES ${CMAKE_CURRENT_BINARY_DIR} /testsweeperConfig.cmake
91
- DESTINATION lib/testsweeper
121
+ # Install <package>Config.cmake and <package>ConfigVersion.cmake,
122
+ # to enable find_package( <package> ).
123
+ include ( CMakePackageConfigHelpers )
124
+ configure_package_config_file(
125
+ "testsweeperConfig.cmake.in"
126
+ "testsweeperConfig.cmake"
127
+ INSTALL_DESTINATION "lib/testsweeper"
92
128
)
93
-
94
- export (
95
- EXPORT testsweeperTargets
96
- FILE testsweeperTargets.cmake
129
+ write_basic_package_version_file(
130
+ "testsweeperConfigVersion.cmake"
131
+ VERSION "${testsweeper_VERSION} "
132
+ COMPATIBILITY AnyNewerVersion
133
+ )
134
+ install (
135
+ FILES "${CMAKE_CURRENT_BINARY_DIR} /testsweeperConfig.cmake"
136
+ "${CMAKE_CURRENT_BINARY_DIR} /testsweeperConfigVersion.cmake"
137
+ DESTINATION "lib/testsweeper"
97
138
)
98
139
99
- if (BUILD_TESTS)
100
- add_executable (example example.cc)
101
- set_target_properties (example PROPERTIES
102
- CXX_STANDARD 11
103
- CXX_STANDARD_REQUIRED YES
104
- CXX_EXTENSIONS NO
105
- )
106
- target_include_directories (example PUBLIC
107
- ${CMAKE_CURRENT_SOURCE_DIR}
108
- )
109
- target_link_libraries (example testsweeper)
140
+ #-------------------------------------------------------------------------------
141
+ if (BUILD_TESTS)
142
+ # Compile example.
143
+ add_executable ( example example.cc )
144
+ target_link_libraries ( example testsweeper )
145
+ set_target_properties ( example PROPERTIES CXX_EXTENSIONS false )
110
146
111
147
enable_testing ()
112
148
113
- add_test (NAME run_test
114
- COMMAND "example"
115
- )
116
- set_tests_properties (run_test
117
- PROPERTIES
118
- PASS_REGULAR_EXPRESSION
119
- "(Usage:)"
120
- )
121
- add_test (NAME run_test_foo
122
- COMMAND "example" "foo"
149
+ add_test ( NAME example COMMAND example )
150
+ set_tests_properties (
151
+ example PROPERTIES PASS_REGULAR_EXPRESSION "Usage:" )
152
+
153
+ add_test ( NAME example_foo COMMAND example foo )
154
+ set_tests_properties (
155
+ example_foo PROPERTIES PASS_REGULAR_EXPRESSION "All tests passed" )
156
+
157
+ # Copy test script to build directory. Used in Jenkins.
158
+ add_custom_command (
159
+ TARGET testsweeper POST_BUILD
160
+ COMMAND
161
+ cp -a ${CMAKE_CURRENT_SOURCE_DIR} /test
162
+ ${CMAKE_CURRENT_BINARY_DIR} /test
123
163
)
124
- set_tests_properties (run_test_foo
125
- PROPERTIES
126
- PASS_REGULAR_EXPRESSION
127
- "(All tests passed)"
128
- )
129
- endif (BUILD_TESTS)
130
-
131
- # Move test script to build directory
132
- add_custom_command (TARGET testsweeper POST_BUILD
133
- COMMAND
134
- cp -a ${CMAKE_CURRENT_SOURCE_DIR} /test /.
135
- ${CMAKE_CURRENT_BINARY_DIR} /test
136
- )
164
+ endif ()
165
+
166
+ #-------------------------------------------------------------------------------
167
+ # To make this more user friendly, add 'make lib' and 'make tester' targets.
168
+ add_custom_target ( lib DEPENDS testsweeper )
169
+ add_custom_target ( tester DEPENDS example )
0 commit comments