-
Notifications
You must be signed in to change notification settings - Fork 0
/
CMakeLists.txt
108 lines (85 loc) · 3.58 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
# CMake simple example
cmake_minimum_required(VERSION 3.1...3.26)
project(
ModernCMakeExample
VERSION 1.0
LANGUAGES CXX)
# If you set any CMAKE_ variables, that can go here. (But usually don't do this, except maybe for C++ standard)
# Set default cmake-variables if they are not passed/set from CLI. see in `CMakeCache.txt`
# (When using `option(), these variables must not be set as a normal or cache variable)
# set(BUILD_SHARED_LIBS ON CACHE BOOL "Build libfoo shared library")
option(BUILD_SHARED_LIBS "Default BUILD_SHARED_LIBS to ON" ON)
# Set default `CMAKE_INSTALL_PREFIX` to ${PROJECT_SOURCE_DIR}/install as this is an application-based standalone project.
# (`option()` will not work as `CMAKE_INSTALL_PREFIX` is already cached!)
if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
set(CMAKE_INSTALL_PREFIX ${PROJECT_SOURCE_DIR}/install CACHE PATH "Force path to set CMAKE_INSTALL_PREFIX" FORCE)
endif()
if(MSVC)
# Create dlls on Windows
option(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS "Default CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS to ON" ON)
endif()
if(APPLE)
option(CMAKE_MACOSX_BUNDLE "Default CMAKE_MACOSX_BUNDLE to ON" ON)
set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib")
set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
endif()
message(STATUS "BUILD_SHARED_LIBS=${BUILD_SHARED_LIBS}")
message(STATUS "CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS=${CMAKE_WINDOWS_EXPORT_ALL_SYMBOsLS}")
message(STATUS "CMAKE_MACOSX_BUNDLE=${CMAKE_MACOSX_BUNDLE}")
message(STATUS "PROJECT_BINARY_DIR=${PROJECT_BINARY_DIR}")
message(STATUS "CMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX}")
# Find packages go here.
# You should usually split this into folders, but this is a simple example
# Pass library type in CLI(STATIC, SHARED, and MODULE)
# Add library
add_library(MyLibExample)
target_include_directories(MyLibExample PRIVATE ${PROJECT_SOURCE_DIR})
target_sources(MyLibExample
PRIVATE simple_lib.cpp
PUBLIC FILE_SET HEADERS # For copy header files to `include/`
BASE_DIRS ${PROJECT_SOURCE_DIR}
FILES simple_lib.hpp
)
install(TARGETS MyLibExample
LIBRARY DESTINATION lib # No need actually
ARCHIVE DESTINATION lib # No need actually
RUNTIME DESTINATION bin # No need actually
FILE_SET HEADERS DESTINATION include # Key need actually
)
# Add executable
add_executable(MyExample simple_example.cpp)
target_link_libraries(MyExample MyLibExample)
# Install
install(TARGETS MyExample
LIBRARY DESTINATION lib
ARCHIVE DESTINATION lib
RUNTIME DESTINATION bin
BUNDLE DESTINATION .
)
# Detect `.app` bundles on OSX and make it standalone after install
if(APPLE)
message(STATUS "CMAKE_INSTALL_NAME_TOOL=${CMAKE_INSTALL_NAME_TOOL}")
install(CODE [[
include(BundleUtilities)
# Note Mac specific extension .app
set(APPS ${CMAKE_INSTALL_PREFIX}/MyExample.app)
# Directories to look for dependencies
set(DIRS ${CMAKE_INSTALL_PREFIX}/lib)
message(STATUS "APPS=${APPS}")
message(STATUS "DIRS=${DIRS}")
# NOTE: No need `${DIRS}` when already setting rpath in exectuable in install by `set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib")`
# fixup_bundle(${APPS} "" ${DIRS})
fixup_bundle(${APPS} "" "")
]])
endif()
# add CPack to project
# set(CPACK_PACKAGE_NAME "MyExample")
# set(CPACK_PACKAGE_VENDOR "CMake.org")
# set(CPACK_PACKAGE_DESCRIPTION_SUMMARY
# "MyExample - CPack Component Installation Example")
# set(CPACK_PACKAGE_VERSION "1.0.0")
# set(CPACK_PACKAGE_VERSION_MAJOR "1")
# set(CPACK_PACKAGE_VERSION_MINOR "0")
# set(CPACK_PACKAGE_VERSION_PATCH "0")
# Package management
include(CPack)