git repos / PlatformFolders

blame: CMakeLists.txt

normal view · raw

3e5ce8ff sum01 2018-04-16 21:01 1
# For target_compile_features
3e5ce8ff sum01 2018-04-16 21:01 2
cmake_minimum_required(VERSION 3.1.0 FATAL_ERROR)
bc48900a sago007 2018-05-27 13:07 3
project(platform_folders VERSION 3.2.0 LANGUAGES CXX)
d483775b sum01 2018-04-16 03:05 4
d483775b sum01 2018-04-16 03:05 5
# Since it's off, the library will be static by default
d483775b sum01 2018-04-16 03:05 6
option(BUILD_SHARED_LIBS "Build shared instead of static." OFF)
d483775b sum01 2018-04-16 03:05 7
80e52811 sum01 2018-05-04 17:14 8
add_library(platform_folders
d483775b sum01 2018-04-16 03:05 9
	sago/platform_folders.cpp
d483775b sum01 2018-04-16 03:05 10
)
d483775b sum01 2018-04-16 03:05 11
80e52811 sum01 2018-05-04 17:14 12
# Defines standardized defaults for install paths
80e52811 sum01 2018-05-04 17:14 13
include(GNUInstallDirs)
80e52811 sum01 2018-05-04 17:14 14
# Where to search for the header while building
80e52811 sum01 2018-05-04 17:14 15
target_include_directories(platform_folders PUBLIC
d483775b sum01 2018-04-16 03:05 16
	$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/sago>
80e52811 sum01 2018-05-04 17:14 17
	# Controls where #include <sago/platform_folders.h> starts to look from
80e52811 sum01 2018-05-04 17:14 18
	# So /usr/include/<sago/platform_folders.h>
80e52811 sum01 2018-05-04 17:14 19
	# or C:\Program Files\platform_folders\include\<sago/platform_folders.h>
80e52811 sum01 2018-05-04 17:14 20
	$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
d483775b sum01 2018-04-16 03:05 21
)
d483775b sum01 2018-04-16 03:05 22
d483775b sum01 2018-04-16 03:05 23
# Define the header as public for installation
80e52811 sum01 2018-05-04 17:14 24
set_target_properties(platform_folders PROPERTIES
d483775b sum01 2018-04-16 03:05 25
	PUBLIC_HEADER "sago/platform_folders.h"
d483775b sum01 2018-04-16 03:05 26
)
d483775b sum01 2018-04-16 03:05 27
80e52811 sum01 2018-05-04 17:14 28
# Require (minimum) C++11 when using header
80e52811 sum01 2018-05-04 17:14 29
# PRIVATE means only at compile time
80e52811 sum01 2018-05-04 17:14 30
target_compile_features(platform_folders
80e52811 sum01 2018-05-04 17:14 31
	PUBLIC cxx_std_11
80e52811 sum01 2018-05-04 17:14 32
	PRIVATE cxx_nullptr
3e5ce8ff sum01 2018-04-16 21:01 33
)
3e5ce8ff sum01 2018-04-16 21:01 34
d483775b sum01 2018-04-16 03:05 35
# Apple requires linking to CoreServices
d483775b sum01 2018-04-16 03:05 36
# Check sys name instead of "APPLE" for cross-compilation
d483775b sum01 2018-04-16 03:05 37
if(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
d483775b sum01 2018-04-16 03:05 38
	# Find the framework
d483775b sum01 2018-04-16 03:05 39
	# ref: https://developer.apple.com/library/content/documentation/MacOSX/Conceptual/OSX_Technology_Overview/SystemFrameworks/SystemFrameworks.html
d483775b sum01 2018-04-16 03:05 40
	find_library(_CoreServices_FRAMEWORK
d483775b sum01 2018-04-16 03:05 41
		NAMES "CoreServices.framework" "CoreServices"
d483775b sum01 2018-04-16 03:05 42
		PATH_SUFFIXES "CoreServices"
d483775b sum01 2018-04-16 03:05 43
		PATHS "/System/Library/Frameworks"
d483775b sum01 2018-04-16 03:05 44
	)
d483775b sum01 2018-04-16 03:05 45
	# Make sure it was found
d483775b sum01 2018-04-16 03:05 46
	if(NOT _CoreServices_FRAMEWORK)
d483775b sum01 2018-04-16 03:05 47
		message(FATAL_ERROR "Could not find the CoreServices framework!")
d483775b sum01 2018-04-16 03:05 48
	endif()
d483775b sum01 2018-04-16 03:05 49
	# Link to the CoreServices framework. This also sets the correct linking options
d483775b sum01 2018-04-16 03:05 50
	# "If the library file is in a Mac OSX framework, the Headers directory of the framework will also be processed as a usage requirement."
80e52811 sum01 2018-05-04 17:14 51
	target_link_libraries(platform_folders PRIVATE "${_CoreServices_FRAMEWORK}")
d483775b sum01 2018-04-16 03:05 52
endif()
d483775b sum01 2018-04-16 03:05 53
80e52811 sum01 2018-05-04 17:14 54
# Cmake's find_package search path is different based on the system
80e52811 sum01 2018-05-04 17:14 55
# See https://cmake.org/cmake/help/latest/command/find_package.html for the list
80e52811 sum01 2018-05-04 17:14 56
if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
80e52811 sum01 2018-05-04 17:14 57
	# Controls where the exports, config, and configversion files install to
80e52811 sum01 2018-05-04 17:14 58
	set(_PROJECT_INSTALL_CMAKE_DIR "${CMAKE_INSTALL_PREFIX}/cmake")
80e52811 sum01 2018-05-04 17:14 59
else()
80e52811 sum01 2018-05-04 17:14 60
	# When calling find_package(<name>)
80e52811 sum01 2018-05-04 17:14 61
	# it looks for /usr/lib/cmake/<name>/<name>Config.cmake
80e52811 sum01 2018-05-04 17:14 62
	set(_PROJECT_INSTALL_CMAKE_DIR "${CMAKE_INSTALL_LIBDIR}/cmake/platform_folders")
80e52811 sum01 2018-05-04 17:14 63
endif()
d483775b sum01 2018-04-16 03:05 64
d483775b sum01 2018-04-16 03:05 65
# Gives "Make install" esque operations a location to install to...
d483775b sum01 2018-04-16 03:05 66
# and creates a .cmake file to be exported
80e52811 sum01 2018-05-04 17:14 67
install(TARGETS platform_folders
80e52811 sum01 2018-05-04 17:14 68
	EXPORT "platform_folders-targets"
80e52811 sum01 2018-05-04 17:14 69
	LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}"
80e52811 sum01 2018-05-04 17:14 70
	ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}"
80e52811 sum01 2018-05-04 17:14 71
	# Tells it where to put the header files
80e52811 sum01 2018-05-04 17:14 72
	PUBLIC_HEADER DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/sago"
d483775b sum01 2018-04-16 03:05 73
)
d483775b sum01 2018-04-16 03:05 74
d483775b sum01 2018-04-16 03:05 75
# "The install(TARGETS) and install(EXPORT) commands work together to install a target and a file to help import it"
d483775b sum01 2018-04-16 03:05 76
# Installs a cmake file which external projects can import.
80e52811 sum01 2018-05-04 17:14 77
install(EXPORT "platform_folders-targets"
80e52811 sum01 2018-05-04 17:14 78
	NAMESPACE sago::
d483775b sum01 2018-04-16 03:05 79
	DESTINATION "${_PROJECT_INSTALL_CMAKE_DIR}"
d483775b sum01 2018-04-16 03:05 80
)
d483775b sum01 2018-04-16 03:05 81
d483775b sum01 2018-04-16 03:05 82
# "The export command is used to generate a file exporting targets from a project build tree"
d483775b sum01 2018-04-16 03:05 83
# Creates an import file for external projects which are aware of the build tree.
d483775b sum01 2018-04-16 03:05 84
# May be useful for cross-compiling
80e52811 sum01 2018-05-04 17:14 85
export(TARGETS platform_folders
80e52811 sum01 2018-05-04 17:14 86
	FILE "platform_folders-exports.cmake"
d483775b sum01 2018-04-16 03:05 87
)
d483775b sum01 2018-04-16 03:05 88
80e52811 sum01 2018-05-04 17:14 89
# For the config and configversion macros
80e52811 sum01 2018-05-04 17:14 90
include(CMakePackageConfigHelpers)
80e52811 sum01 2018-05-04 17:14 91
80e52811 sum01 2018-05-04 17:14 92
configure_package_config_file("platform_foldersConfig.cmake.in"
80e52811 sum01 2018-05-04 17:14 93
	"${CMAKE_CURRENT_BINARY_DIR}/platform_foldersConfig.cmake"
d483775b sum01 2018-04-16 03:05 94
	# Tells the config file where it will be installed, so it can be correctly imported
d483775b sum01 2018-04-16 03:05 95
	INSTALL_DESTINATION "${_PROJECT_INSTALL_CMAKE_DIR}"
d483775b sum01 2018-04-16 03:05 96
)
d483775b sum01 2018-04-16 03:05 97
d483775b sum01 2018-04-16 03:05 98
# Creates the project's ConfigVersion.cmake file
d483775b sum01 2018-04-16 03:05 99
# This allows for find_package() to use a version in the call
80e52811 sum01 2018-05-04 17:14 100
write_basic_package_version_file("${CMAKE_CURRENT_BINARY_DIR}/platform_foldersConfigVersion.cmake"
d483775b sum01 2018-04-16 03:05 101
	# This'll require versioning in the project() call
d483775b sum01 2018-04-16 03:05 102
	VERSION ${CMAKE_PROJECT_VERSION}
d483775b sum01 2018-04-16 03:05 103
	# Just assuming Semver is followed
d483775b sum01 2018-04-16 03:05 104
	COMPATIBILITY SameMajorVersion
d483775b sum01 2018-04-16 03:05 105
)
d483775b sum01 2018-04-16 03:05 106
d483775b sum01 2018-04-16 03:05 107
# Install the ConfigVersion file, which is located in the build dir
d483775b sum01 2018-04-16 03:05 108
install(FILES
80e52811 sum01 2018-05-04 17:14 109
		"${CMAKE_CURRENT_BINARY_DIR}/platform_foldersConfig.cmake"
80e52811 sum01 2018-05-04 17:14 110
		"${CMAKE_CURRENT_BINARY_DIR}/platform_foldersConfigVersion.cmake"
d483775b sum01 2018-04-16 03:05 111
	DESTINATION "${_PROJECT_INSTALL_CMAKE_DIR}"
d483775b sum01 2018-04-16 03:05 112
)
d483775b sum01 2018-04-16 03:05 113
d483775b sum01 2018-04-16 03:05 114
# A module for testing the library
d483775b sum01 2018-04-16 03:05 115
include(CTest)
d483775b sum01 2018-04-16 03:05 116
# BUILD_TESTING is defined (default ON) in CTest
d483775b sum01 2018-04-16 03:05 117
if(BUILD_TESTING)
452e64cc sum01 2018-04-26 21:25 118
	add_subdirectory(test)
d483775b sum01 2018-04-16 03:05 119
endif()
1970-01-01 00:00 120