| 1 |
# For target_compile_features |
| 2 |
cmake_minimum_required(VERSION 3.5.1...4.1.0) |
| 3 |
|
| 4 |
set(PLATFORMFOLDERS_MAIN_PROJECT OFF) |
| 5 |
if(CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR) |
| 6 |
set(PLATFORMFOLDERS_MAIN_PROJECT ON) |
| 7 |
endif() |
| 8 |
|
| 9 |
project(platform_folders VERSION 4.2.0 LANGUAGES CXX) |
| 10 |
|
| 11 |
# BUILD_SHARED_LIBS is off by default, the library will be static by default |
| 12 |
option(PLATFORMFOLDERS_BUILD_SHARED_LIBS "Build platform_folders shared library" ${BUILD_SHARED_LIBS}) |
| 13 |
option(PLATFORMFOLDERS_BUILD_TESTING "Build platform_folders tests" ${PLATFORMFOLDERS_MAIN_PROJECT}) |
| 14 |
option(PLATFORMFOLDERS_ENABLE_INSTALL "Enable platform_folders INSTALL target" ${PLATFORMFOLDERS_MAIN_PROJECT}) |
| 15 |
|
| 16 |
set(PLATFORMFOLDERS_TYPE STATIC) |
| 17 |
if(PLATFORMFOLDERS_BUILD_SHARED_LIBS) |
| 18 |
set(PLATFORMFOLDERS_TYPE SHARED) |
| 19 |
endif() |
| 20 |
|
| 21 |
add_library(platform_folders ${PLATFORMFOLDERS_TYPE} |
| 22 |
sago/platform_folders.cpp |
| 23 |
) |
| 24 |
|
| 25 |
set_target_properties(platform_folders PROPERTIES DEBUG_POSTFIX "${CMAKE_DEBUG_POSTFIX}") |
| 26 |
|
| 27 |
# Creates an alias so that people building in-tree (instead of using find_package)... |
| 28 |
# can still link against the same target |
| 29 |
add_library(sago::platform_folders ALIAS platform_folders) |
| 30 |
|
| 31 |
# Defines standardized defaults for install paths |
| 32 |
include(GNUInstallDirs) |
| 33 |
# Where to search for the header while building |
| 34 |
target_include_directories(platform_folders PUBLIC |
| 35 |
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/sago> |
| 36 |
# Controls where #include <sago/platform_folders.h> starts to look from |
| 37 |
# So /usr/include/<sago/platform_folders.h> |
| 38 |
# or C:\Program Files\platform_folders\include\<sago/platform_folders.h> |
| 39 |
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}> |
| 40 |
) |
| 41 |
|
| 42 |
# Define the header as public for installation |
| 43 |
set_target_properties(platform_folders PROPERTIES |
| 44 |
PUBLIC_HEADER "sago/platform_folders.h" |
| 45 |
) |
| 46 |
|
| 47 |
# cxx_std_11 requires v3.8 |
| 48 |
if(CMAKE_VERSION VERSION_LESS "3.8.0") |
| 49 |
# Use old method of forcing C++11 |
| 50 |
set(CMAKE_CXX_STANDARD 11) |
| 51 |
set(CMAKE_CXX_STANDARD_REQUIRED true) |
| 52 |
else() |
| 53 |
# Require (minimum) C++11 when using header |
| 54 |
# PRIVATE means only at compile time |
| 55 |
target_compile_features(platform_folders PUBLIC cxx_std_11) |
| 56 |
endif() |
| 57 |
|
| 58 |
# cxx_nullptr exists in v3.1 |
| 59 |
target_compile_features(platform_folders PRIVATE cxx_nullptr) |
| 60 |
|
| 61 |
# Cmake's find_package search path is different based on the system |
| 62 |
# See https://cmake.org/cmake/help/latest/command/find_package.html for the list |
| 63 |
if(CMAKE_SYSTEM_NAME STREQUAL "Windows") |
| 64 |
# You can pass these when calling Cmake, so don't override if the user does |
| 65 |
if(NOT _WIN32_WINNT AND NOT WINVER) |
| 66 |
target_compile_definitions(platform_folders PRIVATE |
| 67 |
_WIN32_WINNT=0x0601 |
| 68 |
WINVER=0x0601 |
| 69 |
) |
| 70 |
endif() |
| 71 |
# Controls where the exports, config, and configversion files install to |
| 72 |
set(_PROJECT_INSTALL_CMAKE_DIR "${CMAKE_INSTALL_PREFIX}/cmake") |
| 73 |
else() |
| 74 |
# When calling find_package(<name>) |
| 75 |
# it looks for /usr/lib/cmake/<name>/<name>Config.cmake |
| 76 |
set(_PROJECT_INSTALL_CMAKE_DIR "${CMAKE_INSTALL_LIBDIR}/cmake/platform_folders") |
| 77 |
endif() |
| 78 |
|
| 79 |
if(PLATFORMFOLDERS_ENABLE_INSTALL) |
| 80 |
# Gives "Make install" esque operations a location to install to... |
| 81 |
# and creates a .cmake file to be exported |
| 82 |
install(TARGETS platform_folders |
| 83 |
EXPORT "platform_foldersConfig" |
| 84 |
LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}" |
| 85 |
ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}" |
| 86 |
# Tells it where to put the header files |
| 87 |
PUBLIC_HEADER DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/sago" |
| 88 |
) |
| 89 |
|
| 90 |
# "The install(TARGETS) and install(EXPORT) commands work together to install a target and a file to help import it" |
| 91 |
# Installs a cmake file which external projects can import. |
| 92 |
install(EXPORT "platform_foldersConfig" |
| 93 |
NAMESPACE sago:: |
| 94 |
DESTINATION "${_PROJECT_INSTALL_CMAKE_DIR}" |
| 95 |
) |
| 96 |
|
| 97 |
# "The export command is used to generate a file exporting targets from a project build tree" |
| 98 |
# Creates an import file for external projects which are aware of the build tree. |
| 99 |
# May be useful for cross-compiling |
| 100 |
export(TARGETS platform_folders |
| 101 |
FILE "platform_folders-exports.cmake" |
| 102 |
) |
| 103 |
|
| 104 |
# For the config and configversion macros |
| 105 |
include(CMakePackageConfigHelpers) |
| 106 |
|
| 107 |
# Creates the project's ConfigVersion.cmake file |
| 108 |
# This allows for find_package() to use a version in the call |
| 109 |
write_basic_package_version_file("${CMAKE_CURRENT_BINARY_DIR}/platform_foldersConfigVersion.cmake" |
| 110 |
# This'll require versioning in the project() call |
| 111 |
VERSION ${CMAKE_PROJECT_VERSION} |
| 112 |
# Just assuming Semver is followed |
| 113 |
COMPATIBILITY SameMajorVersion |
| 114 |
) |
| 115 |
|
| 116 |
# Install the ConfigVersion file, which is located in the build dir |
| 117 |
install(FILES |
| 118 |
"${CMAKE_CURRENT_BINARY_DIR}/platform_foldersConfigVersion.cmake" |
| 119 |
DESTINATION "${_PROJECT_INSTALL_CMAKE_DIR}" |
| 120 |
) |
| 121 |
endif() |
| 122 |
|
| 123 |
if(PLATFORMFOLDERS_BUILD_TESTING) |
| 124 |
enable_testing() |
| 125 |
add_subdirectory(test) |
| 126 |
add_executable(platform_folders_sample platform_folders.cpp) |
| 127 |
target_link_libraries(platform_folders_sample PRIVATE platform_folders) |
| 128 |
endif() |