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
d483775b sum01 2018-04-16 03:05 8
add_library(${PROJECT_NAME}
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
d483775b sum01 2018-04-16 03:05 12
# Where to search for the header
d483775b sum01 2018-04-16 03:05 13
# The BUILD/INSTALL interface expressions are for exporting
d483775b sum01 2018-04-16 03:05 14
target_include_directories(${PROJECT_NAME} PRIVATE
d483775b sum01 2018-04-16 03:05 15
	$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/sago>
d483775b sum01 2018-04-16 03:05 16
	$<INSTALL_INTERFACE:sago>
d483775b sum01 2018-04-16 03:05 17
)
d483775b sum01 2018-04-16 03:05 18
d483775b sum01 2018-04-16 03:05 19
# Define the header as public for installation
d483775b sum01 2018-04-16 03:05 20
set_target_properties(${PROJECT_NAME} PROPERTIES
d483775b sum01 2018-04-16 03:05 21
	PUBLIC_HEADER "sago/platform_folders.h"
d483775b sum01 2018-04-16 03:05 22
)
d483775b sum01 2018-04-16 03:05 23
3e5ce8ff sum01 2018-04-16 21:01 24
# Minimum C++11 for "nullptr" and other stuff
3e5ce8ff sum01 2018-04-16 21:01 25
target_compile_features(${PROJECT_NAME} PRIVATE
3e5ce8ff sum01 2018-04-16 21:01 26
	cxx_std_11
3e5ce8ff sum01 2018-04-16 21:01 27
	cxx_nullptr
3e5ce8ff sum01 2018-04-16 21:01 28
)
3e5ce8ff sum01 2018-04-16 21:01 29
d483775b sum01 2018-04-16 03:05 30
# Apple requires linking to CoreServices
d483775b sum01 2018-04-16 03:05 31
# Check sys name instead of "APPLE" for cross-compilation
d483775b sum01 2018-04-16 03:05 32
if(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
d483775b sum01 2018-04-16 03:05 33
	# Find the framework
d483775b sum01 2018-04-16 03:05 34
	# ref: https://developer.apple.com/library/content/documentation/MacOSX/Conceptual/OSX_Technology_Overview/SystemFrameworks/SystemFrameworks.html
d483775b sum01 2018-04-16 03:05 35
	find_library(_CoreServices_FRAMEWORK
d483775b sum01 2018-04-16 03:05 36
		NAMES "CoreServices.framework" "CoreServices"
d483775b sum01 2018-04-16 03:05 37
		PATH_SUFFIXES "CoreServices"
d483775b sum01 2018-04-16 03:05 38
		PATHS "/System/Library/Frameworks"
d483775b sum01 2018-04-16 03:05 39
	)
d483775b sum01 2018-04-16 03:05 40
	# Make sure it was found
d483775b sum01 2018-04-16 03:05 41
	if(NOT _CoreServices_FRAMEWORK)
d483775b sum01 2018-04-16 03:05 42
		message(FATAL_ERROR "Could not find the CoreServices framework!")
d483775b sum01 2018-04-16 03:05 43
	endif()
d483775b sum01 2018-04-16 03:05 44
	# Link to the CoreServices framework. This also sets the correct linking options
d483775b sum01 2018-04-16 03:05 45
	# "If the library file is in a Mac OSX framework, the Headers directory of the framework will also be processed as a usage requirement."
d483775b sum01 2018-04-16 03:05 46
	target_link_libraries(${PROJECT_NAME} PRIVATE "${_CoreServices_FRAMEWORK}")
d483775b sum01 2018-04-16 03:05 47
endif()
d483775b sum01 2018-04-16 03:05 48
d483775b sum01 2018-04-16 03:05 49
# Defines standardized defaults for install paths
d483775b sum01 2018-04-16 03:05 50
include(GNUInstallDirs)
d483775b sum01 2018-04-16 03:05 51
# For the config and configversion macros
d483775b sum01 2018-04-16 03:05 52
include(CMakePackageConfigHelpers)
d483775b sum01 2018-04-16 03:05 53
d483775b sum01 2018-04-16 03:05 54
# Controls what dir to prefix for the lib, so <sago/platform_folders.h>
d483775b sum01 2018-04-16 03:05 55
set(_PROJECT_INSTALL_PREFIX_DIR "sago")
d483775b sum01 2018-04-16 03:05 56
# Controls where the exports, config, and configversion files install to
d483775b sum01 2018-04-16 03:05 57
set(_PROJECT_INSTALL_CMAKE_DIR "${CMAKE_INSTALL_FULL_LIBDIR}/cmake/${_PROJECT_INSTALL_PREFIX_DIR}")
d483775b sum01 2018-04-16 03:05 58
d483775b sum01 2018-04-16 03:05 59
# Gives "Make install" esque operations a location to install to...
d483775b sum01 2018-04-16 03:05 60
# and creates a .cmake file to be exported
d483775b sum01 2018-04-16 03:05 61
install(TARGETS ${PROJECT_NAME}
d483775b sum01 2018-04-16 03:05 62
	EXPORT "${PROJECT_NAME}-targets"
d483775b sum01 2018-04-16 03:05 63
	LIBRARY DESTINATION "${CMAKE_INSTALL_FULL_LIBDIR}/${_PROJECT_INSTALL_PREFIX_DIR}"
d483775b sum01 2018-04-16 03:05 64
	ARCHIVE DESTINATION "${CMAKE_INSTALL_FULL_LIBDIR}/${_PROJECT_INSTALL_PREFIX_DIR}"
d483775b sum01 2018-04-16 03:05 65
	# Tells it where to put your headers if any set by set_target_properties
d483775b sum01 2018-04-16 03:05 66
	PUBLIC_HEADER DESTINATION "${CMAKE_INSTALL_FULL_INCLUDEDIR}/${_PROJECT_INSTALL_PREFIX_DIR}"
d483775b sum01 2018-04-16 03:05 67
	# Tells export where your includes folder is | Note that the private include path is not needed here
d483775b sum01 2018-04-16 03:05 68
	INCLUDES DESTINATION "${CMAKE_INSTALL_FULL_INCLUDEDIR}/${_PROJECT_INSTALL_PREFIX_DIR}"
d483775b sum01 2018-04-16 03:05 69
)
d483775b sum01 2018-04-16 03:05 70
d483775b sum01 2018-04-16 03:05 71
# "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 72
# Installs a cmake file which external projects can import.
d483775b sum01 2018-04-16 03:05 73
install(EXPORT "${PROJECT_NAME}-targets"
d483775b sum01 2018-04-16 03:05 74
	DESTINATION "${_PROJECT_INSTALL_CMAKE_DIR}"
d483775b sum01 2018-04-16 03:05 75
)
d483775b sum01 2018-04-16 03:05 76
d483775b sum01 2018-04-16 03:05 77
# "The export command is used to generate a file exporting targets from a project build tree"
d483775b sum01 2018-04-16 03:05 78
# Creates an import file for external projects which are aware of the build tree.
d483775b sum01 2018-04-16 03:05 79
# May be useful for cross-compiling
d483775b sum01 2018-04-16 03:05 80
export(TARGETS ${PROJECT_NAME}
d483775b sum01 2018-04-16 03:05 81
	FILE "${PROJECT_NAME}-exports.cmake"
d483775b sum01 2018-04-16 03:05 82
)
d483775b sum01 2018-04-16 03:05 83
d483775b sum01 2018-04-16 03:05 84
configure_package_config_file("${PROJECT_NAME}Config.cmake.in"
d483775b sum01 2018-04-16 03:05 85
	"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
d483775b sum01 2018-04-16 03:05 86
	# Tells the config file where it will be installed, so it can be correctly imported
d483775b sum01 2018-04-16 03:05 87
	INSTALL_DESTINATION "${_PROJECT_INSTALL_CMAKE_DIR}"
d483775b sum01 2018-04-16 03:05 88
	# This passes the variables to the cmake.in file, which uses them
d483775b sum01 2018-04-16 03:05 89
	PATH_VARS PROJECT_NAME
d483775b sum01 2018-04-16 03:05 90
)
d483775b sum01 2018-04-16 03:05 91
d483775b sum01 2018-04-16 03:05 92
# Creates the project's ConfigVersion.cmake file
d483775b sum01 2018-04-16 03:05 93
# This allows for find_package() to use a version in the call
d483775b sum01 2018-04-16 03:05 94
write_basic_package_version_file("${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake"
d483775b sum01 2018-04-16 03:05 95
	# This'll require versioning in the project() call
d483775b sum01 2018-04-16 03:05 96
	VERSION ${CMAKE_PROJECT_VERSION}
d483775b sum01 2018-04-16 03:05 97
	# Just assuming Semver is followed
d483775b sum01 2018-04-16 03:05 98
	COMPATIBILITY SameMajorVersion
d483775b sum01 2018-04-16 03:05 99
)
d483775b sum01 2018-04-16 03:05 100
d483775b sum01 2018-04-16 03:05 101
# Install the ConfigVersion file, which is located in the build dir
d483775b sum01 2018-04-16 03:05 102
install(FILES
d483775b sum01 2018-04-16 03:05 103
		"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
d483775b sum01 2018-04-16 03:05 104
		"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake"
d483775b sum01 2018-04-16 03:05 105
	DESTINATION "${_PROJECT_INSTALL_CMAKE_DIR}"
d483775b sum01 2018-04-16 03:05 106
)
d483775b sum01 2018-04-16 03:05 107
d483775b sum01 2018-04-16 03:05 108
# A module for testing the library
d483775b sum01 2018-04-16 03:05 109
include(CTest)
d483775b sum01 2018-04-16 03:05 110
# BUILD_TESTING is defined (default ON) in CTest
d483775b sum01 2018-04-16 03:05 111
if(BUILD_TESTING)
d483775b sum01 2018-04-16 03:05 112
	set(_PROJECT_TEST_NAME "${PROJECT_NAME}_test")
d483775b sum01 2018-04-16 03:05 113
	add_executable(${_PROJECT_TEST_NAME} platform_folders.cpp)
d483775b sum01 2018-04-16 03:05 114
d483775b sum01 2018-04-16 03:05 115
	target_link_libraries(${_PROJECT_TEST_NAME} PRIVATE ${PROJECT_NAME})
d483775b sum01 2018-04-16 03:05 116
d483775b sum01 2018-04-16 03:05 117
	# Since tests aren't installed, no reason to give it an INSTALL_INTERFACE
d483775b sum01 2018-04-16 03:05 118
	target_include_directories(${_PROJECT_TEST_NAME} PRIVATE
d483775b sum01 2018-04-16 03:05 119
		$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/sago>
d483775b sum01 2018-04-16 03:05 120
	)
d483775b sum01 2018-04-16 03:05 121
d483775b sum01 2018-04-16 03:05 122
	# Creates the "MyTest" test that runs the test executable created above
d483775b sum01 2018-04-16 03:05 123
	# This is triggered by things like "make test"
d483775b sum01 2018-04-16 03:05 124
	add_test(NAME MyTest COMMAND ${_PROJECT_TEST_NAME})
d483775b sum01 2018-04-16 03:05 125
endif()
1970-01-01 00:00 126