Skip to content

Commit 823ae66

Browse files
committed
Update CMake
1 parent d1e5882 commit 823ae66

6 files changed

+151
-115
lines changed

CMakeLists.txt

+126-93
Original file line numberDiff line numberDiff line change
@@ -1,136 +1,169 @@
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
33
# repo: http://bitbucket.org/icl/testsweeper
44

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
610

7-
project (TESTSWEEPER
11+
project(
12+
testsweeper
13+
VERSION 2020.06.0
814
LANGUAGES CXX
915
)
1016

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()
1232

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:
1744
rm -rf CMakeCache.txt CMakeFiles/ # delete files in ${CMAKE_CURRENT_SOURCE_DIR}
1845
mkdir build
1946
cd build
2047
cmake ..
21-
make")
22-
endif ()
23-
24-
if (CMAKE_HOST_APPLE)
25-
set (CMAKE_MACOSX_RPATH 1)
48+
make"
49+
)
2650
endif()
2751

28-
add_library (testsweeper SHARED
52+
#-------------------------------------------------------------------------------
53+
# Build library.
54+
add_library(
55+
testsweeper
2956
testsweeper.cc
3057
version.cc
3158
)
3259

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>"
3567
)
3668

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}" )
4478
endif()
4579

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 )
5183

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>>" )
5689
endif()
5790

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" )
6493
endif()
6594

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
6899
EXPORT testsweeperTargets
69-
LIBRARY DESTINATION lib
70-
ARCHIVE DESTINATION lib
71-
INCLUDES DESTINATION include
100+
LIBRARY DESTINATION "lib" # no default before 3.14
72101
)
73102

74-
install (
75-
EXPORT testsweeperTargets
76-
DESTINATION lib/testsweeper
103+
# Install header file
104+
install(
105+
FILES "testsweeper.hh"
106+
DESTINATION "include"
77107
)
78108

109+
# Install <package>Targets.cmake
79110
install(
80-
FILES testsweeper.hh
81-
DESTINATION include
111+
EXPORT testsweeperTargets
112+
DESTINATION "lib/testsweeper"
82113
)
83114

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"
87119
)
88120

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"
92128
)
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"
97138
)
98139

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 )
110146

111147
enable_testing()
112148

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
123163
)
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 )

GNUmakefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ make.inc:
3030

3131
# Defaults if not given in make.inc. GNU make doesn't have defaults for these.
3232
RANLIB ?= ranlib
33-
prefix ?= /usr/local/testsweeper
33+
prefix ?= /opt/slate
3434

3535
# auto-detect OS
3636
# $OSTYPE may not be exported from the shell, so echo it

INSTALL.md

+13-14
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Option 2: CMake
1717

1818
mkdir build && cd build
1919
cmake ..
20-
make && make install
20+
make && make test && make install
2121

2222
Makefile Installation
2323
--------------------------------------------------------------------------------
@@ -94,22 +94,21 @@ test files are in the config directory.
9494
CMake Installation
9595
--------------------------------------------------------------------------------
9696

97-
The CMake script enforces an out of source build. The simplest way to accomplish
98-
this is to create a build directory off the TestSweeper root directory:
97+
The CMake script enforces an out of source build. Create a build
98+
directory under the TestSweeper root directory:
9999

100-
cd /my/testsweeper/dir
100+
cd /my/testsweeper
101101
mkdir build && cd build
102+
cmake ..
103+
make
104+
make test
105+
make install
102106

103-
By default TestSweeper is set to install into `/opt/slate/`. If you wish to
104-
change this, CMake needs to be told where to install the TestSweeper library.
105-
You can do this by defining CMAKE_INSTALL_PREFIX variable via the CMake
106-
command line:
107+
By default TestSweeper installs into `/opt/slate`. To change this,
108+
set `CMAKE_INSTALL_PREFIX`:
107109

108-
# Assuming the working dir is still /my/testsweeper/dir/build
109-
cmake -DCMAKE_INSTALL_PREFIX=/path/to/my/dir ..
110+
cmake -DCMAKE_INSTALL_PREFIX=/usr/local ..
110111

111-
This generates the required makefiles and can be built and installed as normal:
112+
To debug the build, set `VERBOSE`:
112113

113-
# Assuming the working dir is still /my/testsweeper/dir/build
114-
make
115-
make install
114+
make VERBOSE=1

Jenkinsfile

+6-5
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ pipeline {
3535
fi
3636
if [ "${maker}" = "cmake" ]; then
3737
spack load cmake
38-
mkdir -p build
38+
mkdir build
3939
cd build
40-
cmake -DNO_COLOR=TRUE -DCMAKE_CXX_FLAGS="-Werror" ..
40+
cmake -DCOLOR=no -DCMAKE_CXX_FLAGS="-Werror" ..
4141
make -j8
4242
ldd example
4343
cd test
@@ -88,10 +88,11 @@ pipeline {
8888
fi
8989
if [ "${maker}" = "cmake" ]; then
9090
spack load cmake
91-
mkdir -p build
91+
mkdir build
9292
cd build
93-
cmake -DNO_COLOR=TRUE -DCMAKE_CXX_FLAGS="-Werror" ..
94-
make
93+
cmake -DCOLOR=no -DCMAKE_CXX_FLAGS="-Werror" ..
94+
make -j8
95+
ldd example
9596
cd test
9697
./run_tests.py --xml ../../report_cmake.xml
9798
fi

configure.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636

3737
#-------------------------------------------------------------------------------
3838
def main():
39-
config.init( prefix='/usr/local/testsweeper' )
39+
config.init( prefix='/opt/slate' )
4040
config.prog_cxx()
4141
config.prog_cxx_flags([
4242
'-O2', '-std=c++11', '-MMD',

testsweeperConfig.cmake.in

+4-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
1-
include("${CMAKE_CURRENT_LIST_DIR}/testsweeperTargets.cmake")
1+
@PACKAGE_INIT@
2+
3+
# testsweeperTargets.cmake is in same dir as this file (CMAKE_CURRENT_LIST_DIR).
4+
include( "${CMAKE_CURRENT_LIST_DIR}/testsweeperTargets.cmake" )

0 commit comments

Comments
 (0)