-
Notifications
You must be signed in to change notification settings - Fork 54
/
CMakeLists.txt
120 lines (94 loc) · 2.95 KB
/
CMakeLists.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
cmake_minimum_required(VERSION 3.5)
project(hashmap VERSION 2.0.0 LANGUAGES C)
set(CMAKE_C_STANDARD 99)
##############################################
# Build options
option(HASHMAP_BUILD_TESTS "Build tests" OFF)
option(HASHMAP_BUILD_EXAMPLES "Build examples" OFF)
##############################################
# Set default build to release
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE "Release" CACHE STRING "Choose Release or Debug" FORCE)
endif()
##############################################
# Create target and set properties
add_library(hashmap
src/hashmap.c
)
# Add an alias so that library can be used inside the build tree,
# e.g. when testing
add_library(HashMap::HashMap ALIAS hashmap)
# Set target properties
target_include_directories(hashmap
PUBLIC
$<INSTALL_INTERFACE:include>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/src
)
target_compile_options(hashmap
PRIVATE $<$<C_COMPILER_ID:GNU>:-Wall -Werror>
)
##############################################
# Installation instructions
include(GNUInstallDirs)
set(INSTALL_CONFIGDIR ${CMAKE_INSTALL_LIBDIR}/cmake/HashMap)
install(TARGETS hashmap
EXPORT hashmap-targets
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
)
# Ensure the exported target has the name HashMap and not hashmap
# and if this is linked into a shared library, ensure it is PIC
set_target_properties(hashmap
PROPERTIES
EXPORT_NAME HashMap
POSITION_INDEPENDENT_CODE ON
)
install(DIRECTORY include/ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
# Export the targets to a script
install(EXPORT hashmap-targets
FILE
HashMapTargets.cmake
NAMESPACE
HashMap::
DESTINATION
${INSTALL_CONFIGDIR}
)
# Create a ConfigVersion.cmake file
include(CMakePackageConfigHelpers)
write_basic_package_version_file(
${CMAKE_CURRENT_BINARY_DIR}/HashMapConfigVersion.cmake
VERSION ${PROJECT_VERSION}
COMPATIBILITY AnyNewerVersion
)
configure_package_config_file(
${CMAKE_CURRENT_LIST_DIR}/cmake/HashMapConfig.cmake.in
${CMAKE_CURRENT_BINARY_DIR}/HashMapConfig.cmake
INSTALL_DESTINATION ${INSTALL_CONFIGDIR}
)
# Install the config, configversion and custom find modules
install(FILES
${CMAKE_CURRENT_BINARY_DIR}/HashMapConfig.cmake
${CMAKE_CURRENT_BINARY_DIR}/HashMapConfigVersion.cmake
DESTINATION ${INSTALL_CONFIGDIR}
)
##############################################
# Exporting from the build tree
export(EXPORT hashmap-targets
FILE ${CMAKE_CURRENT_BINARY_DIR}/HashMapTargets.cmake
NAMESPACE HashMap::
)
# Register package in user's package registry
export(PACKAGE HashMap)
##############################################
# Build unit test
if(HASHMAP_BUILD_TESTS)
enable_testing()
add_subdirectory(test)
endif()
##############################################
# Build examples
if(HASHMAP_BUILD_EXAMPLES)
add_subdirectory(examples)
endif()