Skip to content

Commit

Permalink
CMake overhaul (#12)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
derpda authored Apr 28, 2020
1 parent 825e0fa commit b929289
Show file tree
Hide file tree
Showing 11 changed files with 139 additions and 49 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
.rtags
build/
**/#*#

extern
3 changes: 0 additions & 3 deletions .gitmodules

This file was deleted.

80 changes: 46 additions & 34 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,10 @@ cmake_minimum_required (VERSION 3.0)

project (YOMM2 VERSION 1.0)

find_package(Boost)

include_directories ("${YOMM2_SOURCE_DIR}/include")
link_directories ("${YOMM2_BINARY_DIR}/src")

# Find Boost dependency
find_package(Boost 1.53 REQUIRED)
message(STATUS "Using Boost libraries from ${Boost_INCLUDE_DIRS}")

include_directories("${Boost_INCLUDE_DIR}")

if(CMAKE_COMPILER_IS_GNUCXX OR "${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17")
if (CMAKE_BUILD_TYPE STREQUAL "Debug")
Expand All @@ -29,34 +24,37 @@ if(MSVC)
set(CMAKE_CXX_FLAGS "/std:c++17 -D_SCL_SECURE_NO_WARNINGS /EHsc")
endif()

set(YOMM2_BUILD_TYPE ${CMAKE_BUILD_TYPE})

if("${YOMM2_BUILD_TYPE}" STREQUAL "DebugRelease")
# Only allow Debug or Release
if("${CMAKE_BUILD_TYPE}" STREQUAL "DebugRelease")
set(CMAKE_BUILD_TYPE "Debug")
endif()

if("${YOMM2_BUILD_TYPE}" STREQUAL "ReleaseDebug")
if("${CMAKE_BUILD_TYPE}" STREQUAL "ReleaseDebug")
set(CMAKE_BUILD_TYPE "Release")
endif()

add_subdirectory (src)

if("${YOMM2_BUILD_TYPE}" STREQUAL "DebugRelease")
set(CMAKE_BUILD_TYPE "Release")
endif()

if("${YOMM2_BUILD_TYPE}" STREQUAL "ReleaseDebug")
set(CMAKE_BUILD_TYPE "Debug")
endif()

add_subdirectory (examples)

if (NOT DEFINED(YOMM2_ENABLE_TESTS))
set(YOMM2_ENABLE_TESTS CACHE BOOL ON)
option(YOMM2_ENABLE_EXAMPLES "Set to ON to build examples" ON)
if (${YOMM2_ENABLE_EXAMPLES})
message(STATUS "Examples enabled")
add_subdirectory (examples)
endif()

option(YOMM2_ENABLE_TESTS "Set to ON to build tests" OFF)
include(CMakeDependentOption)
CMAKE_DEPENDENT_OPTION(YOMM2_ENABLE_BENCHMARKS
"Set to ON to build benchmarks" OFF
"YOMM2_ENABLE_TESTS" OFF
)
if (${YOMM2_ENABLE_TESTS})
message(STATUS "Tests enabled")
if (${YOMM2_ENABLE_BENCHMARKS})
message(STATUS "Benchmarks enabled")
list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake")
include(find_or_download_package)
find_or_download_package(benchmark)
endif()
include(CTest)
add_subdirectory (tests)
enable_testing()
Expand All @@ -72,16 +70,30 @@ if (${YOMM2_ENABLE_TESTS})
add_test (containers examples/containers/containers)
endif()

if (NOT DEFINED(YOMM2_ENABLE_BENCHMARKS))
set(YOMM2_ENABLE_BENCHMARKS CACHE BOOL ON)
endif()

if (${YOMM2_ENABLE_BENCHMARKS})
message(STATUS "Benchmarks enabled")
set(BENCHMARK_ENABLE_TESTING CACHE BOOL OFF)
set(BENCHMARK_ENABLE_GTEST_TESTS CACHE BOOL OFF)
add_subdirectory(extern/benchmark)
include_directories(extern/benchmark/include)
endif()
## Install instruction
# Create version file for cmake package
include(CMakePackageConfigHelpers)
write_basic_package_version_file(
YOMM2ConfigVersion.cmake
VERSION ${PACKAGE_VERSION}
COMPATIBILITY SameMajorVersion
)
# Create targets file of yomm2
install(EXPORT YOMM2Targets
FILE YOMM2Targets.cmake
NAMESPACE YOMM2::
DESTINATION ${CMAKE_INSTALL_PREFIX}/lib/cmake/YOMM2
)
# Configure package config (tells using code about dependencies)
configure_package_config_file(
cmake/Config.cmake.in ${CMAKE_CURRENT_BINARY_DIR}/YOMM2Config.cmake
INSTALL_DESTINATION ${CMAKE_INSTALL_PREFIX}/lib/cmake/YOMM2
)
# Copy config files to install directory
install(FILES
"${CMAKE_CURRENT_BINARY_DIR}/YOMM2Config.cmake"
"${CMAKE_CURRENT_BINARY_DIR}/YOMM2ConfigVersion.cmake"
DESTINATION ${CMAKE_INSTALL_PREFIX}/lib/cmake/YOMM2
)

INSTALL (DIRECTORY include/yorel DESTINATION include)
17 changes: 10 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,13 +178,6 @@ git clone https://github.com/jll63/yomm2.git
cd yomm2
```

If you want to run the benchmarks, fetch the Google Benchmark submodule:

```
git submodule init
git submodule update
```

Create a build directory and run cmake then make:

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

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

Expand All @@ -217,6 +213,13 @@ sudo make install
# or:
make install DESTDIR=/path/to/my/libs
```
This will install the library and headers, as well as a CMake package
configuration.
Make sure to add the install location to `CMAKE_PREFIX_PATH` so that you can use
`find_package(YOMM2)` from your including project. For linking, the use
`target_link_library(<your_target> YOMM2::yomm2)`. This will automatically add
the necessary include directories, so this should be all you need to do to link
to yomm2.
## Going Further
Expand Down
7 changes: 7 additions & 0 deletions cmake/Config.cmake.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
include(CMakeFindDependencyMacro)

# Tell library users about the Boost dependency
find_dependency(Boost 1.53)

# Add the targets file
include("${CMAKE_CURRENT_LIST_DIR}/YOMM2Targets.cmake")
16 changes: 16 additions & 0 deletions cmake/benchmark_download.cmake.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
cmake_minimum_required(VERSION 3.5)
project(benchmarkDownload)
include(ExternalProject)

ExternalProject_Add(benchmark
GIT_REPOSITORY https://github.com/google/benchmark.git
GIT_TAG master
GIT_PROGRESS True
SOURCE_DIR "${CMAKE_SOURCE_DIR}/extern/benchmark"
BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/benchmark-build"
INSTALL_DIR ${CMAKE_SOURCE_DIR}/extern
CMAKE_ARGS -DCMAKE_INSTALL_PREFIX=<INSTALL_DIR>
-DCMAKE_BUILD_TYPE=Release
-DBENCHMARK_ENABLE_TESTING=OFF
-DBENCHMARK_ENABLE_GTEST_TESTS=OFF
)
44 changes: 44 additions & 0 deletions cmake/find_or_download_package.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
macro(find_or_download_package PACKAGE)
find_package(${PACKAGE} QUIET)
if(NOT ${${PACKAGE}_FOUND})
message(STATUS "Package \"${PACKAGE}\" not found in system.")
message(STATUS
"Downloading dependency \"${PACKAGE}\" and building from source."
)

# Prepare download instructions for dependency
configure_file(
${CMAKE_SOURCE_DIR}/cmake/${PACKAGE}_download.cmake.in
${CMAKE_CURRENT_BINARY_DIR}/${PACKAGE}-download/CMakeLists.txt
)

# Download dependency
execute_process(
COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" .
RESULT_VARIABLE result
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/${PACKAGE}-download
OUTPUT_QUIET
)
if(result)
message(FATAL_ERROR "Download of dependency ${PACKAGE} failed: ${result}")
endif()

# Build dependency
execute_process(
COMMAND ${CMAKE_COMMAND} --build .
RESULT_VARIABLE result
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/${PACKAGE}-download
)
if(result)
message(FATAL_ERROR "Build of dependency ${PACKAGE} failed: ${result}")
endif()

# Update search path and use regular find_package to add dependency
# TODO Use same directory here as for configure_file up there and inside
# download instructions!
list(
APPEND CMAKE_PREFIX_PATH "${CMAKE_SOURCE_DIR}/extern/lib/cmake/${PACKAGE}"
)
find_package(${PACKAGE} REQUIRED)
endif()
endmacro()
2 changes: 2 additions & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ if(NOT MSVC)
set(CMAKE_SKIP_BUILD_RPATH TRUE)
add_executable(dl_main dl_main.cpp)
add_library(dl_shared SHARED dl_shared.cpp)
get_target_property(YOMM2_INCLUDE_DIRS yomm2 INTERFACE_INCLUDE_DIRECTORIES)
target_include_directories(dl_shared PUBLIC ${YOMM2_INCLUDE_DIRS})
add_dependencies(dl_main dl_shared)
if(APPLE)
set_target_properties(dl_main PROPERTIES LINK_FLAGS "-Wl,-export_dynamic")
Expand Down
1 change: 0 additions & 1 deletion extern/benchmark
Submodule benchmark deleted from e30cac
14 changes: 11 additions & 3 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,16 @@
# See accompanying file LICENSE_1_0.txt
# or copy at http://www.boost.org/LICENSE_1_0.txt)

add_library(yomm2 yomm2.cpp)
add_library(yomm2 STATIC yomm2.cpp)

INSTALL(TARGETS yomm2
DESTINATION lib
target_include_directories(yomm2 PUBLIC
$<BUILD_INTERFACE:${YOMM2_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>
)
target_include_directories(yomm2 SYSTEM PUBLIC ${Boost_INCLUDE_DIR})

install(TARGETS yomm2
EXPORT YOMM2Targets
LIBRARY DESTINATION lib
ARCHIVE DESTINATION lib
)
2 changes: 1 addition & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ target_link_libraries (namespaces yomm2 ${CMAKE_THREAD_LIBS_INIT})
if (${YOMM2_ENABLE_BENCHMARKS})
set_source_files_properties(benchmarks_vfuncs.cpp PROPERTIES COMPILE_FLAGS "-save-temps")
add_executable(benchmarks benchmarks.cpp benchmarks_vfuncs.cpp )
target_link_libraries (benchmarks yomm2 benchmark)
target_link_libraries (benchmarks yomm2 benchmark::benchmark)
endif()

0 comments on commit b929289

Please sign in to comment.