Skip to content

Commit 7756251

Browse files
committed
Add CMake build support (header-only lib, samples, and tests)
1 parent 6b3f945 commit 7756251

4 files changed

Lines changed: 108 additions & 0 deletions

File tree

CMakeLists.txt

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
cmake_minimum_required(VERSION 3.10)
2+
project(pcg-cpp VERSION 0.9.1 LANGUAGES CXX)
3+
4+
include(GNUInstallDirs)
5+
include(CMakePackageConfigHelpers)
6+
7+
# Header-only library
8+
add_library(pcg-cpp INTERFACE)
9+
add_library(pcg_cpp::pcg_cpp ALIAS pcg-cpp)
10+
11+
target_include_directories(pcg-cpp INTERFACE
12+
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
13+
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
14+
)
15+
16+
target_compile_features(pcg-cpp INTERFACE cxx_std_11)
17+
18+
# Options
19+
option(PCG_CPP_BUILD_SAMPLES "Build samples" ON)
20+
option(PCG_CPP_BUILD_TESTS "Build tests" ON)
21+
22+
if(PCG_CPP_BUILD_TESTS)
23+
enable_testing()
24+
endif()
25+
26+
if(PCG_CPP_BUILD_SAMPLES)
27+
add_subdirectory(sample)
28+
endif()
29+
30+
if(PCG_CPP_BUILD_TESTS)
31+
add_subdirectory(test-high)
32+
endif()
33+
34+
# Installation
35+
install(DIRECTORY include/ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
36+
37+
install(TARGETS pcg-cpp EXPORT pcg-cppTargets)
38+
install(EXPORT pcg-cppTargets
39+
FILE pcg-cppTargets.cmake
40+
NAMESPACE pcg_cpp::
41+
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/pcg-cpp
42+
)
43+
44+
write_basic_package_version_file(
45+
"${CMAKE_CURRENT_BINARY_DIR}/pcg-cppConfigVersion.cmake"
46+
VERSION ${PROJECT_VERSION}
47+
COMPATIBILITY SameMajorVersion
48+
)
49+
50+
configure_package_config_file(
51+
"${CMAKE_CURRENT_SOURCE_DIR}/cmake/pcg-cppConfig.cmake.in"
52+
"${CMAKE_CURRENT_BINARY_DIR}/pcg-cppConfig.cmake"
53+
INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/pcg-cpp
54+
)
55+
56+
install(FILES
57+
"${CMAKE_CURRENT_BINARY_DIR}/pcg-cppConfig.cmake"
58+
"${CMAKE_CURRENT_BINARY_DIR}/pcg-cppConfigVersion.cmake"
59+
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/pcg-cpp
60+
)

cmake/pcg-cppConfig.cmake.in

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
@PACKAGE_INIT@
2+
3+
include("${CMAKE_CURRENT_LIST_DIR}/pcg-cppTargets.cmake")
4+
5+
check_required_components(pcg-cpp)

sample/CMakeLists.txt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
set(SAMPLE_TARGETS pcg-demo codebook cppref-sample make-partytrick)
2+
3+
foreach(sample ${SAMPLE_TARGETS})
4+
add_executable(${sample} ${sample}.cpp)
5+
target_link_libraries(${sample} PRIVATE pcg_cpp::pcg_cpp)
6+
endforeach()
7+
8+
# spew and use-partytrick use POSIX-specific <unistd.h> and write()
9+
if(NOT MSVC)
10+
set(POSIX_SAMPLES spew use-partytrick)
11+
foreach(sample ${POSIX_SAMPLES})
12+
if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/${sample}.cpp")
13+
add_executable(${sample} ${sample}.cpp)
14+
target_link_libraries(${sample} PRIVATE pcg_cpp::pcg_cpp)
15+
endif()
16+
endforeach()
17+
endif()

test-high/CMakeLists.txt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
set(TEST_TARGETS
2+
check-pcg8_once_insecure check-pcg8_oneseq_once_insecure
3+
check-pcg16_once_insecure check-pcg16_oneseq_once_insecure check-pcg32
4+
check-pcg32_c1024 check-pcg32_c1024_fast check-pcg32_c64
5+
check-pcg32_c64_fast check-pcg32_c64_oneseq check-pcg32_fast
6+
check-pcg32_k1024 check-pcg32_k1024_fast check-pcg32_k16384
7+
check-pcg32_k16384_fast check-pcg32_k2 check-pcg32_k2_fast
8+
check-pcg32_k64 check-pcg32_k64_fast check-pcg32_k64_oneseq
9+
check-pcg32_once_insecure check-pcg32_oneseq
10+
check-pcg32_oneseq_once_insecure check-pcg32_unique check-pcg64
11+
check-pcg64_c1024 check-pcg64_c1024_fast check-pcg64_c32
12+
check-pcg64_c32_fast check-pcg64_c32_oneseq check-pcg64_fast
13+
check-pcg64_k1024 check-pcg64_k1024_fast check-pcg64_k32
14+
check-pcg64_k32_fast check-pcg64_k32_oneseq check-pcg64_once_insecure
15+
check-pcg64_oneseq check-pcg64_oneseq_once_insecure check-pcg64_unique
16+
check-pcg128_once_insecure check-pcg128_oneseq_once_insecure
17+
)
18+
19+
20+
foreach(test_exe ${TEST_TARGETS})
21+
add_executable(${test_exe} ${test_exe}.cpp)
22+
target_link_libraries(${test_exe} PRIVATE pcg_cpp::pcg_cpp)
23+
# The tests in this project compare actual output with expected output.
24+
# For now, we just add them as basic regression tests.
25+
add_test(NAME ${test_exe} COMMAND ${test_exe})
26+
endforeach()

0 commit comments

Comments
 (0)