/
CMakeLists.txt
135 lines (100 loc) · 4.41 KB
/
CMakeLists.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# Structure inspired by https://github.com/pabloariasal/modern-cmake-sample
# Assumptions:
# - We cannot go higher than CMake 3.22.1 while we support Ubuntu 22.04.
# - Prefer static libraries. Users should link Gosu into their games.
# - This is not (yet?) going to be used for building Ruby/Gosu.
# - This is not (yet?) going to replace the Visual Studio project for Windows.
cmake_minimum_required(VERSION 3.22.1...3.26.4 FATAL_ERROR)
project(Gosu VERSION 2.0.0)
##############################################
# Global options
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
##############################################
# Declare dependencies
find_package(SDL2 REQUIRED)
find_package(OpenGL REQUIRED)
if (APPLE)
find_library(COCOA_LIBRARY Cocoa)
find_library(AUDIO_TOOLBOX_LIBRARY AudioToolbox)
find_library(CORE_TEXT_LIBRARY CoreText)
find_library(ICONV_LIBRARY iconv)
else ()
find_package(PkgConfig REQUIRED)
pkg_check_modules(FONTCONFIG REQUIRED fontconfig)
endif ()
##############################################
# Create target and set properties
file(GLOB_RECURSE C_FILES ${CMAKE_CURRENT_SOURCE_DIR}/dependencies/*.c)
file(GLOB HEADER_FILES ${CMAKE_CURRENT_SOURCE_DIR}/include/Gosu/*.hpp)
file(GLOB SOURCE_FILES ${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp)
add_library(gosu ${C_FILES} ${HEADER_FILES} ${SOURCE_FILES})
# Gosu uses C++20 in its public interface, so CMake projects using Gosu must support it too.
target_compile_features(gosu PUBLIC cxx_std_20)
# This is necessary so that a static libgosu.a can be used in e.g. a dynamic libgosu-ffi.so.
set_property(TARGET gosu PROPERTY POSITION_INDEPENDENT_CODE ON)
# Ignore deprecation warnings from within Gosu while compiling Gosu itself.
target_compile_definitions(gosu PRIVATE -DGOSU_DEPRECATED=)
# Compile all C++ files as Objective-C++ on macOS so that we can use ObjC APIs.
if (APPLE)
set_source_files_properties(${SOURCE_FILES} PROPERTIES
COMPILE_FLAGS "-x objective-c++")
endif ()
target_include_directories(gosu PUBLIC
$<INSTALL_INTERFACE:include>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>)
# Use target_include_directories(... SYSTEM ...) to suppress warnings in these headers.
target_include_directories(gosu SYSTEM PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/dependencies/mojoAL/AL
${CMAKE_CURRENT_SOURCE_DIR}/dependencies/SDL_sound
${CMAKE_CURRENT_SOURCE_DIR}/dependencies/stb
${CMAKE_CURRENT_SOURCE_DIR}/dependencies/utf8proc)
target_link_libraries(gosu PRIVATE
SDL2::SDL2
OpenGL::GL
# Only defined on macOS
${COCOA_LIBRARY}
${AUDIO_TOOLBOX_LIBRARY}
${CORE_TEXT_LIBRARY}
${ICONV_LIBRARY}
# Only defined on Linux
${FONTCONFIG_LIBRARIES})
##############################################
# Installation instructions
include(GNUInstallDirs)
set(INSTALL_CONFIGDIR ${CMAKE_INSTALL_LIBDIR}/cmake/Gosu)
install(TARGETS gosu
EXPORT gosu-targets
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})
set_target_properties(gosu PROPERTIES EXPORT_NAME Gosu)
install(DIRECTORY include/ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
# Export the targets to a script
install(EXPORT gosu-targets
FILE GosuTargets.cmake
NAMESPACE Gosu::
DESTINATION ${INSTALL_CONFIGDIR})
include(CMakePackageConfigHelpers)
configure_package_config_file(${CMAKE_CURRENT_LIST_DIR}/cmake/GosuConfig.cmake.in
${CMAKE_CURRENT_BINARY_DIR}/GosuConfig.cmake
INSTALL_DESTINATION ${INSTALL_CONFIGDIR})
install(FILES
${CMAKE_CURRENT_BINARY_DIR}/GosuConfig.cmake
DESTINATION ${INSTALL_CONFIGDIR})
export(EXPORT gosu-targets
FILE ${CMAKE_CURRENT_BINARY_DIR}/GosuTargets.cmake
NAMESPACE Gosu::)
##############################################
# FFI interface, examples, and tests
# Alias our library as Gosu::Gosu so that examples and submodules can depend
# on it by its normal name.
add_library(Gosu::Gosu ALIAS gosu)
# Only build the FFI interface, examples, and tests if this is the top-level CMake module.
if (CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
add_subdirectory(ffi)
add_subdirectory(examples)
# If GoogleTest is not available as a CMake module, this is a no-op.
# (enable_testing() MUST come before add_subdirectory: https://stackoverflow.com/a/30264765)
enable_testing()
add_subdirectory(test)
endif ()