git repos / PlatformFolders

blame: CMakeLists.txt

normal view · raw

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