-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCMakeLists.txt
192 lines (146 loc) · 6.96 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
cmake_minimum_required(VERSION 3.1)
project(sample_shared_lib)
# set file name
set(LIB_NAME sample_lib)
##
# Warning user on doing compilation inside the source
if(${CMAKE_SOURCE_DIR} STREQUAL ${CMAKE_BINARY_DIR})
message(WARNING "*** Recommendation: Use separate build folder ***")
endif()
##-------------------- CMAKE_BUILD_TYPE -- START ------------------
# From commandline, one can set it via -DCMAKE_BUILD_TYPE=Release
# Set it if it is not set from commandline.
if(NOT CMAKE_BUILD_TYPE)
# NOTE: `CACHE STRING "Choose the type of build" FORCE` is to make sure that
# CMAKE_BUILD_TYPE has been set here.
set(CMAKE_BUILD_TYPE "Release" CACHE STRING "Choose the type of build" FORCE)
# Set build types for cmake-gui otherwise cmake-gui will have empty line
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release")
endif()
##-------------------- Validate build type ------------------
# Error if build type is not properly setup
if(NOT CMAKE_BUILD_TYPE MATCHES "release|Release|RELEASE|debug|Debug|DEBUG")
message("*** CMAKE_BUILD_TYPE is not set to known value:")
message("*** Possible values: Release;Debug")
message(FATAL_ERROR "*** CMAKE_BUILD_TYPE is not set properly")
endif()
##-------------------- Normalize build type ------------------
# Let's normalized user passed build type for ease of management
if((CMAKE_BUILD_TYPE MATCHES "DEBUG") OR (CMAKE_BUILD_TYPE MATCHES "debug"))
set(CMAKE_BUILD_TYPE "Debug")
endif()
if((CMAKE_BUILD_TYPE MATCHES "RELEASE") OR (CMAKE_BUILD_TYPE MATCHES "release"))
set(CMAKE_BUILD_TYPE "Release")
endif()
##-------------------- Customize build parameters ------------------
# Customize compiler flags as per build type
if(CMAKE_BUILD_TYPE MATCHES "Release")
add_definitions(-DBUILD_TYPE="Release")
endif()
if(CMAKE_BUILD_TYPE MATCHES "Debug")
add_definitions(-DBUILD_TYPE="Debug")
endif()
# Set default values of CXX flags for different builds.
# By default, cmake have following configuration
# - CMAKE_CXX_FLAGS:
# - CMAKE_CXX_FLAGS_DEBUG: -g
# - CMAKE_CXX_FLAGS_RELEASE: -O3 -DNDEBUG
set(CMAKE_CXX_FLAGS "-pipe" CACHE STRING "Default CXX flags" FORCE)
set(CMAKE_CXX_FLAGS_DEBUG "-O0 -g -ggdb" CACHE STRING "Default CXX flags for Debug build" FORCE)
set(CMAKE_CXX_FLAGS_RELEASE "-O2 -DNDEBUG" CACHE STRING "Default CXX flags for Release build" FORCE)
##-------------------- CMAKE_BUILD_TYPE -- END ------------------
##------------------ CMake file generation -- START ---------------
# Set version
set(VERSION_MAJOR 1)
set(VERSION_MINOR 0)
set(VERSION_PATCH 0)
# Make version string
set(VERSION_STRING ${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_MINOR})
# Configure a header file to have cmake version variables
configure_file(
"${PROJECT_SOURCE_DIR}/sample_lib_config.h.in"
"${CMAKE_BINARY_DIR}/sample_lib_config.h"
@ONLY
)
##------------------ CMake file generation -- END ---------------
##----------------- CXX related configuration -- START -------------
# Set CXX standard
set(CMAKE_CXX_STANDARD 14)
# Drop out if required standard is not supported
set(CMAKE_CXX_STANDARD_REQUIRED ON)
# CXX extensions
set(CMAKE_CXX_EXTENSIONS OFF)
# Append extra flags
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -pedantic -Wextra")
##----------------- CXX related configuration -- END -------------
##
# Show general info
message("-----------------------------------------------------------")
message("Project name: : ${PROJECT_NAME}")
message("Lib name : lib${LIB_NAME}")
message("Lib version : ${VERSION_STRING}")
message("Compiler Id : ${CMAKE_CXX_COMPILER_ID}")
message("Compiler (c) version : ${CMAKE_C_COMPILER_VERSION}")
message("Compiler (cxx) version : ${CMAKE_CXX_COMPILER_VERSION}")
message("-----------------------------------------------------------")
##----------------- Setup target - START -------------
# Add sources to the library
add_library(sample_lib SHARED ${PROJECT_SOURCE_DIR}/sample_lib.cc)
# Follow .so naming convention
set_target_properties(sample_lib PROPERTIES
VERSION ${VERSION_STRING}
SOVERSION ${VERSION_MAJOR})
# Set include directories
# NOTE: Cmake was not happy with target_include_directories
include_directories("${CMAKE_SOURCE_DIR}")
include_directories("${CMAKE_BINARY_DIR}")
##----------------- Setup target - END -------------
##----------------- Setup .pc file -- START -------------
# Set env for text replacement in .pc template
set(prefix ${CMAKE_INSTALL_PREFIX})
set(target_lib ${LIB_NAME})
set(verison ${VERSION_STRING})
# Generate .pc file from template
configure_file(
"${PROJECT_SOURCE_DIR}/${LIB_NAME}.pc.in"
"${CMAKE_BINARY_DIR}/${LIB_NAME}.pc"
@ONLY
)
# Install
install(FILES ${CMAKE_BINARY_DIR}/${LIB_NAME}.pc DESTINATION lib/pkgconfig)
##----------------- Setup .pc file -- END -------------
##----------------- Setup .cmake file -- START -------------
# Install targets and also specify EXPORT ${LIB_NAME}-targets to generate cmake targets
install(TARGETS ${LIB_NAME} DESTINATION lib EXPORT ${LIB_NAME}-targets)
install(FILES "sample_lib.h" DESTINATION include)
# Generate .ConfigVersion.cmake file from template
configure_file(
"${PROJECT_SOURCE_DIR}/${LIB_NAME}-configVersion.cmake.in"
"${CMAKE_BINARY_DIR}/${LIB_NAME}-configVersion.cmake"
@ONLY
)
install(FILES ${CMAKE_BINARY_DIR}/${LIB_NAME}-configVersion.cmake DESTINATION lib/cmake)
# Generate .Config.cmake file from template
include(CMakePackageConfigHelpers)
configure_package_config_file(
"${LIB_NAME}-config.cmake.in"
"${LIB_NAME}-config.cmake"
INSTALL_DESTINATION "${CMAKE_BINARY_DIR}"
)
install(FILES ${CMAKE_BINARY_DIR}/${LIB_NAME}-config.cmake DESTINATION lib/cmake)
# Generate .cmake target
install(EXPORT ${LIB_NAME}-targets DESTINATION lib/cmake EXPORT_LINK_INTERFACE_LIBRARIES)
# install(EXPORT ${LIB_NAME}-targets DESTINATION lib/cmake)
# Expose meta info of the target # TODO; Cmake does not like following
# set_property(TARGET ${LIB_NAME} PROPERTY INTERFACE_CXX_STANDARD ${CMAKE_CXX_STANDARD})
# set_property(TARGET ${LIB_NAME} APPEND PROPERTY COMPATIBLE_INTERFACE_STRING CXX_STANDARD)
# set_property(TARGET ${LIB_NAME} PROPERTY INTERFACE_CXX_STANDARD_REQUIRED ${CMAKE_CXX_STANDARD_REQUIRED})
# set_property(TARGET ${LIB_NAME} APPEND PROPERTY COMPATIBLE_INTERFACE_BOOL CXX_STANDARD_REQUIRED)
# set_property(TARGET ${LIB_NAME} PROPERTY INTERFACE_CXX_EXTENSIONS ${CMAKE_CXX_EXTENSIONS})
# set_property(TARGET ${LIB_NAME} APPEND PROPERTY COMPATIBLE_INTERFACE_STRING CXX_EXTENSIONS)
# set_property(TARGET ${LIB_NAME} PROPERTY INTERFACE_CXX_COMPILE_FEATURES ${CMAKE_CXX_COMPILE_FEATURES})
# set_property(TARGET ${LIB_NAME} APPEND PROPERTY COMPATIBLE_INTERFACE_STRING CXX_COMPILE_FEATURES)
target_include_directories(${LIB_NAME} PUBLIC $<INSTALL_INTERFACE:include>)
target_link_directories(${LIB_NAME} PUBLIC $<INSTALL_INTERFACE:lib>)
# target_link_libraries(${LIB_NAME} PUBLIC lib${LIB_NAME}) # TODO; Cmake does not like
##----------------- Setup .cmake file -- START -------------