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