Skip to content
This repository was archived by the owner on Mar 4, 2026. It is now read-only.

Commit b929289

Browse files
authored
CMake overhaul (#12)
* Non-effective CMake code removed This change should not affect functionality. * Compilation of examples made optional * Boost made mandatory, better include_dir settings * Install and cmake package instructions added * Indentation fixes * benchmark: submodule -> cmake ExternalProject_Add Unnecessary download of benchmark can be avoided with CMake. During CMake configure, benchmark is downloaded if needed and not found. benchmark is then built and installed during CMake configure step. We can then use find_package with REQUIRED to use benchmark. This is cleaner and is the suggested method by google. https://github.com/google/googletest/blob/master/googletest/README.md Settings for benchmark build can be set in: benchmark_download.cmake.in. This file is configured so use of variables from main build is possible. * yomm2 given ARCHIVE DESTINATION for install This fixes an issue that appeared in Travis CI * Options revamped Now using CMake's `Option` command to define options. This allows them to be cached and have default values. Examples are set to on by default again. YOMM2_ENABLE_BENCHMARKS only available if tests are built. * Dependency downloading macro improved * Readme updated * Linking instructions added to README.md * Boost should be PUBLIC, not INTERFACE dependency! * Make yomm2.a STATIC explicitly (clearer!) * Unneeded parameter removed
1 parent 825e0fa commit b929289

File tree

11 files changed

+139
-49
lines changed

11 files changed

+139
-49
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
.rtags
22
build/
33
**/#*#
4+
5+
extern

.gitmodules

Lines changed: 0 additions & 3 deletions
This file was deleted.

CMakeLists.txt

Lines changed: 46 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,10 @@ cmake_minimum_required (VERSION 3.0)
99

1010
project (YOMM2 VERSION 1.0)
1111

12-
find_package(Boost)
13-
14-
include_directories ("${YOMM2_SOURCE_DIR}/include")
15-
link_directories ("${YOMM2_BINARY_DIR}/src")
16-
12+
# Find Boost dependency
13+
find_package(Boost 1.53 REQUIRED)
1714
message(STATUS "Using Boost libraries from ${Boost_INCLUDE_DIRS}")
1815

19-
include_directories("${Boost_INCLUDE_DIR}")
20-
2116
if(CMAKE_COMPILER_IS_GNUCXX OR "${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
2217
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17")
2318
if (CMAKE_BUILD_TYPE STREQUAL "Debug")
@@ -29,34 +24,37 @@ if(MSVC)
2924
set(CMAKE_CXX_FLAGS "/std:c++17 -D_SCL_SECURE_NO_WARNINGS /EHsc")
3025
endif()
3126

32-
set(YOMM2_BUILD_TYPE ${CMAKE_BUILD_TYPE})
33-
34-
if("${YOMM2_BUILD_TYPE}" STREQUAL "DebugRelease")
27+
# Only allow Debug or Release
28+
if("${CMAKE_BUILD_TYPE}" STREQUAL "DebugRelease")
3529
set(CMAKE_BUILD_TYPE "Debug")
3630
endif()
3731

38-
if("${YOMM2_BUILD_TYPE}" STREQUAL "ReleaseDebug")
32+
if("${CMAKE_BUILD_TYPE}" STREQUAL "ReleaseDebug")
3933
set(CMAKE_BUILD_TYPE "Release")
4034
endif()
4135

4236
add_subdirectory (src)
4337

44-
if("${YOMM2_BUILD_TYPE}" STREQUAL "DebugRelease")
45-
set(CMAKE_BUILD_TYPE "Release")
46-
endif()
47-
48-
if("${YOMM2_BUILD_TYPE}" STREQUAL "ReleaseDebug")
49-
set(CMAKE_BUILD_TYPE "Debug")
50-
endif()
51-
52-
add_subdirectory (examples)
53-
54-
if (NOT DEFINED(YOMM2_ENABLE_TESTS))
55-
set(YOMM2_ENABLE_TESTS CACHE BOOL ON)
38+
option(YOMM2_ENABLE_EXAMPLES "Set to ON to build examples" ON)
39+
if (${YOMM2_ENABLE_EXAMPLES})
40+
message(STATUS "Examples enabled")
41+
add_subdirectory (examples)
5642
endif()
5743

44+
option(YOMM2_ENABLE_TESTS "Set to ON to build tests" OFF)
45+
include(CMakeDependentOption)
46+
CMAKE_DEPENDENT_OPTION(YOMM2_ENABLE_BENCHMARKS
47+
"Set to ON to build benchmarks" OFF
48+
"YOMM2_ENABLE_TESTS" OFF
49+
)
5850
if (${YOMM2_ENABLE_TESTS})
5951
message(STATUS "Tests enabled")
52+
if (${YOMM2_ENABLE_BENCHMARKS})
53+
message(STATUS "Benchmarks enabled")
54+
list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake")
55+
include(find_or_download_package)
56+
find_or_download_package(benchmark)
57+
endif()
6058
include(CTest)
6159
add_subdirectory (tests)
6260
enable_testing()
@@ -72,16 +70,30 @@ if (${YOMM2_ENABLE_TESTS})
7270
add_test (containers examples/containers/containers)
7371
endif()
7472

75-
if (NOT DEFINED(YOMM2_ENABLE_BENCHMARKS))
76-
set(YOMM2_ENABLE_BENCHMARKS CACHE BOOL ON)
77-
endif()
78-
79-
if (${YOMM2_ENABLE_BENCHMARKS})
80-
message(STATUS "Benchmarks enabled")
81-
set(BENCHMARK_ENABLE_TESTING CACHE BOOL OFF)
82-
set(BENCHMARK_ENABLE_GTEST_TESTS CACHE BOOL OFF)
83-
add_subdirectory(extern/benchmark)
84-
include_directories(extern/benchmark/include)
85-
endif()
73+
## Install instruction
74+
# Create version file for cmake package
75+
include(CMakePackageConfigHelpers)
76+
write_basic_package_version_file(
77+
YOMM2ConfigVersion.cmake
78+
VERSION ${PACKAGE_VERSION}
79+
COMPATIBILITY SameMajorVersion
80+
)
81+
# Create targets file of yomm2
82+
install(EXPORT YOMM2Targets
83+
FILE YOMM2Targets.cmake
84+
NAMESPACE YOMM2::
85+
DESTINATION ${CMAKE_INSTALL_PREFIX}/lib/cmake/YOMM2
86+
)
87+
# Configure package config (tells using code about dependencies)
88+
configure_package_config_file(
89+
cmake/Config.cmake.in ${CMAKE_CURRENT_BINARY_DIR}/YOMM2Config.cmake
90+
INSTALL_DESTINATION ${CMAKE_INSTALL_PREFIX}/lib/cmake/YOMM2
91+
)
92+
# Copy config files to install directory
93+
install(FILES
94+
"${CMAKE_CURRENT_BINARY_DIR}/YOMM2Config.cmake"
95+
"${CMAKE_CURRENT_BINARY_DIR}/YOMM2ConfigVersion.cmake"
96+
DESTINATION ${CMAKE_INSTALL_PREFIX}/lib/cmake/YOMM2
97+
)
8698

8799
INSTALL (DIRECTORY include/yorel DESTINATION include)

README.md

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -178,13 +178,6 @@ git clone https://github.com/jll63/yomm2.git
178178
cd yomm2
179179
```
180180

181-
If you want to run the benchmarks, fetch the Google Benchmark submodule:
182-
183-
```
184-
git submodule init
185-
git submodule update
186-
```
187-
188181
Create a build directory and run cmake then make:
189182

190183
```
@@ -208,6 +201,9 @@ build):
208201
cmake .. -DYOMM2_ENABLE_TESTS=1 -DYOMM2_ENABLE_BENCHMARKS=1 -DCMAKE_BUILD_TYPE=Release
209202
make && tests/benchmarks # wow it's fast!
210203
```
204+
This will automatically download the dependency
205+
[benchmark](https://github.com/google/benchmark), build it and finally install
206+
it to `./extern` within the root directory of yomm2.
211207

212208
Finally, if you like it and you want to install it:
213209

@@ -217,6 +213,13 @@ sudo make install
217213
# or:
218214
make install DESTDIR=/path/to/my/libs
219215
```
216+
This will install the library and headers, as well as a CMake package
217+
configuration.
218+
Make sure to add the install location to `CMAKE_PREFIX_PATH` so that you can use
219+
`find_package(YOMM2)` from your including project. For linking, the use
220+
`target_link_library(<your_target> YOMM2::yomm2)`. This will automatically add
221+
the necessary include directories, so this should be all you need to do to link
222+
to yomm2.
220223
221224
## Going Further
222225

cmake/Config.cmake.in

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
include(CMakeFindDependencyMacro)
2+
3+
# Tell library users about the Boost dependency
4+
find_dependency(Boost 1.53)
5+
6+
# Add the targets file
7+
include("${CMAKE_CURRENT_LIST_DIR}/YOMM2Targets.cmake")

cmake/benchmark_download.cmake.in

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
cmake_minimum_required(VERSION 3.5)
2+
project(benchmarkDownload)
3+
include(ExternalProject)
4+
5+
ExternalProject_Add(benchmark
6+
GIT_REPOSITORY https://github.com/google/benchmark.git
7+
GIT_TAG master
8+
GIT_PROGRESS True
9+
SOURCE_DIR "${CMAKE_SOURCE_DIR}/extern/benchmark"
10+
BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/benchmark-build"
11+
INSTALL_DIR ${CMAKE_SOURCE_DIR}/extern
12+
CMAKE_ARGS -DCMAKE_INSTALL_PREFIX=<INSTALL_DIR>
13+
-DCMAKE_BUILD_TYPE=Release
14+
-DBENCHMARK_ENABLE_TESTING=OFF
15+
-DBENCHMARK_ENABLE_GTEST_TESTS=OFF
16+
)
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
macro(find_or_download_package PACKAGE)
2+
find_package(${PACKAGE} QUIET)
3+
if(NOT ${${PACKAGE}_FOUND})
4+
message(STATUS "Package \"${PACKAGE}\" not found in system.")
5+
message(STATUS
6+
"Downloading dependency \"${PACKAGE}\" and building from source."
7+
)
8+
9+
# Prepare download instructions for dependency
10+
configure_file(
11+
${CMAKE_SOURCE_DIR}/cmake/${PACKAGE}_download.cmake.in
12+
${CMAKE_CURRENT_BINARY_DIR}/${PACKAGE}-download/CMakeLists.txt
13+
)
14+
15+
# Download dependency
16+
execute_process(
17+
COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" .
18+
RESULT_VARIABLE result
19+
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/${PACKAGE}-download
20+
OUTPUT_QUIET
21+
)
22+
if(result)
23+
message(FATAL_ERROR "Download of dependency ${PACKAGE} failed: ${result}")
24+
endif()
25+
26+
# Build dependency
27+
execute_process(
28+
COMMAND ${CMAKE_COMMAND} --build .
29+
RESULT_VARIABLE result
30+
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/${PACKAGE}-download
31+
)
32+
if(result)
33+
message(FATAL_ERROR "Build of dependency ${PACKAGE} failed: ${result}")
34+
endif()
35+
36+
# Update search path and use regular find_package to add dependency
37+
# TODO Use same directory here as for configure_file up there and inside
38+
# download instructions!
39+
list(
40+
APPEND CMAKE_PREFIX_PATH "${CMAKE_SOURCE_DIR}/extern/lib/cmake/${PACKAGE}"
41+
)
42+
find_package(${PACKAGE} REQUIRED)
43+
endif()
44+
endmacro()

examples/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ if(NOT MSVC)
3434
set(CMAKE_SKIP_BUILD_RPATH TRUE)
3535
add_executable(dl_main dl_main.cpp)
3636
add_library(dl_shared SHARED dl_shared.cpp)
37+
get_target_property(YOMM2_INCLUDE_DIRS yomm2 INTERFACE_INCLUDE_DIRECTORIES)
38+
target_include_directories(dl_shared PUBLIC ${YOMM2_INCLUDE_DIRS})
3739
add_dependencies(dl_main dl_shared)
3840
if(APPLE)
3941
set_target_properties(dl_main PROPERTIES LINK_FLAGS "-Wl,-export_dynamic")

extern/benchmark

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/CMakeLists.txt

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,16 @@
33
# See accompanying file LICENSE_1_0.txt
44
# or copy at http://www.boost.org/LICENSE_1_0.txt)
55

6-
add_library(yomm2 yomm2.cpp)
6+
add_library(yomm2 STATIC yomm2.cpp)
77

8-
INSTALL(TARGETS yomm2
9-
DESTINATION lib
8+
target_include_directories(yomm2 PUBLIC
9+
$<BUILD_INTERFACE:${YOMM2_SOURCE_DIR}/include>
10+
$<INSTALL_INTERFACE:include>
11+
)
12+
target_include_directories(yomm2 SYSTEM PUBLIC ${Boost_INCLUDE_DIR})
13+
14+
install(TARGETS yomm2
15+
EXPORT YOMM2Targets
16+
LIBRARY DESTINATION lib
17+
ARCHIVE DESTINATION lib
1018
)

0 commit comments

Comments
 (0)