Repository navigation
Expand file tree
/
Copy pathCMakeLists.txt
More file actions
256 lines (217 loc) · 10.2 KB
/
Copy pathCMakeLists.txt
File metadata and controls
256 lines (217 loc) · 10.2 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# Morris - Nine Men's Morris
#
# Configure: cmake -S . -B build
# Build: cmake --build build
# Install: cmake --install build (as root for a system prefix)
# Tarball: cmake --build build --target package_source
cmake_minimum_required(VERSION 3.16)
project(morris
VERSION 0.4
DESCRIPTION "Nine Men's Morris board game with rule variants and a learning AI"
HOMEPAGE_URL "https://nine-mens-morris.net/"
LANGUAGES CXX)
set(PACKAGE_BUGREPORT "dirk.farin@gmail.com")
include(GNUInstallDirs)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
option(ENABLE_NLS
"Native language support: build and install the gettext translations" ON)
option(MORRIS_COMPILE_SCHEMAS
"Run glib-compile-schemas after installation (skipped when DESTDIR or GSETTINGS_DISABLE_SCHEMAS_COMPILE is set)" ON)
option(MORRIS_ENABLE_ASSERTS
"Keep assert() active in every build type, as the previous build did" ON)
# The autotools build defaulted to "-g -O2" unless the user supplied CXXFLAGS;
# RelWithDebInfo is the equivalent, applied only when no flags were given.
if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES AND NOT CMAKE_CXX_FLAGS)
set(CMAKE_BUILD_TYPE RelWithDebInfo CACHE STRING "Build type" FORCE)
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS Debug Release RelWithDebInfo MinSizeRel)
endif()
# ---------------------------------------------------------------------------
# Dependencies
# ---------------------------------------------------------------------------
# The search uses std::jthread and <stop_token>. target_compile_features()
# only asks for the standard, so an older toolchain still configures happily
# and then fails deep in the build; check for the facility itself instead.
include(CheckCXXSourceCompiles)
set(CMAKE_REQUIRED_FLAGS "-std=c++20")
check_cxx_source_compiles("
#include <stop_token>
#include <thread>
int main() { std::stop_source s; std::jthread t; }"
MORRIS_HAVE_JTHREAD)
unset(CMAKE_REQUIRED_FLAGS)
if(NOT MORRIS_HAVE_JTHREAD)
message(FATAL_ERROR
"morris needs a C++20 standard library with <stop_token> and std::jthread: "
"GCC 10 or newer, or Clang with libstdc++ 10 or newer. "
"libc++ needs version 20, or an older one built with -fexperimental-library.")
endif()
find_package(PkgConfig REQUIRED)
pkg_check_modules(GTK REQUIRED IMPORTED_TARGET
glib-2.0
gthread-2.0
gio-2.0
gtk+-3.0)
if(ENABLE_NLS)
find_package(Intl REQUIRED) # libintl; part of libc on GNU systems
find_package(Gettext) # msgfmt / msgmerge for the message catalogs
if(NOT GETTEXT_FOUND)
message(WARNING "gettext tools not found: translations will not be built or installed")
endif()
endif()
find_program(GLIB_COMPILE_SCHEMAS_EXECUTABLE glib-compile-schemas)
# ---------------------------------------------------------------------------
# Program
# ---------------------------------------------------------------------------
# The reverse-DNS application id, used by the program, the desktop entry, the
# icons and the metainfo. It must not contain hyphens, so the domain
# nine-mens-morris.net becomes nine_mens_morris; the GSettings schema keeps
# its own name so that nobody loses their stored settings.
set(APP_ID net.nine_mens_morris.Morris)
add_subdirectory(src)
# ---------------------------------------------------------------------------
# Tests: they link morris_core and need no display
# ---------------------------------------------------------------------------
include(CTest) # defines BUILD_TESTING, default ON
if(BUILD_TESTING)
add_subdirectory(test)
endif()
# ---------------------------------------------------------------------------
# Translations (po/ exports MORRIS_LINGUAS and MORRIS_PO_FILES)
# ---------------------------------------------------------------------------
set(MORRIS_LINGUAS "")
set(MORRIS_PO_FILES "")
if(ENABLE_NLS AND GETTEXT_FOUND)
add_subdirectory(po)
endif()
# ---------------------------------------------------------------------------
# Desktop entry: merge the translated Name/Comment into the template
# (this is what intltool-merge used to do)
# ---------------------------------------------------------------------------
set(DESKTOP_TEMPLATE ${CMAKE_CURRENT_SOURCE_DIR}/${APP_ID}.desktop.in)
set(DESKTOP_FILE ${CMAKE_CURRENT_BINARY_DIR}/${APP_ID}.desktop)
if(MORRIS_PO_FILES)
add_custom_command(
OUTPUT ${DESKTOP_FILE}
COMMAND ${GETTEXT_MSGFMT_EXECUTABLE} --desktop
--template=${DESKTOP_TEMPLATE}
-d ${CMAKE_CURRENT_SOURCE_DIR}/po
-o ${DESKTOP_FILE}
DEPENDS ${DESKTOP_TEMPLATE} ${CMAKE_CURRENT_SOURCE_DIR}/po/LINGUAS ${MORRIS_PO_FILES}
COMMENT "Merging translations into morris.desktop"
VERBATIM)
add_custom_target(desktop-file ALL DEPENDS ${DESKTOP_FILE})
else()
configure_file(${DESKTOP_TEMPLATE} ${DESKTOP_FILE} COPYONLY)
endif()
install(FILES ${DESKTOP_FILE} DESTINATION ${CMAKE_INSTALL_DATADIR}/applications)
# Version 0.3 installed the desktop entry and the icon under other names
# (morris.desktop, pixmaps/morris.png). Left in place beside the new files
# they would show the game twice in the menu, so an install over an old
# source install removes them. Packages never see this: dpkg does the same.
if(IS_ABSOLUTE "${CMAKE_INSTALL_DATADIR}")
set(_data_dir "${CMAKE_INSTALL_DATADIR}")
else()
# Left unexpanded so that "cmake --install --prefix" is honoured.
set(_data_dir "\${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_DATADIR}")
endif()
install(CODE "
foreach(_old \"\$ENV{DESTDIR}${_data_dir}/applications/morris.desktop\"
\"\$ENV{DESTDIR}${_data_dir}/pixmaps/morris.png\")
if(EXISTS \"\${_old}\")
message(STATUS \"Removing the 0.3 file \${_old}\")
file(REMOVE \"\${_old}\")
endif()
endforeach()
")
# ---------------------------------------------------------------------------
# GSettings schema: validated at build time, compiled after installation
# ---------------------------------------------------------------------------
set(GSCHEMA_FILE net.nine-mens-morris.gschema.xml)
set(GSCHEMA_INSTALL_DIR ${CMAKE_INSTALL_DATADIR}/glib-2.0/schemas)
if(GLIB_COMPILE_SCHEMAS_EXECUTABLE)
add_custom_command(
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${GSCHEMA_FILE}.valid
COMMAND ${GLIB_COMPILE_SCHEMAS_EXECUTABLE} --strict --dry-run
--schema-file=${CMAKE_CURRENT_SOURCE_DIR}/${GSCHEMA_FILE}
COMMAND ${CMAKE_COMMAND} -E touch ${CMAKE_CURRENT_BINARY_DIR}/${GSCHEMA_FILE}.valid
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${GSCHEMA_FILE}
COMMENT "Validating ${GSCHEMA_FILE}"
VERBATIM)
add_custom_target(validate-gschema ALL
DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${GSCHEMA_FILE}.valid)
endif()
install(FILES ${GSCHEMA_FILE} DESTINATION ${GSCHEMA_INSTALL_DIR})
if(MORRIS_COMPILE_SCHEMAS AND GLIB_COMPILE_SCHEMAS_EXECUTABLE)
if(IS_ABSOLUTE "${GSCHEMA_INSTALL_DIR}")
set(_schema_dir "${GSCHEMA_INSTALL_DIR}")
else()
# Left unexpanded so that "cmake --install --prefix" is honoured.
set(_schema_dir "\${CMAKE_INSTALL_PREFIX}/${GSCHEMA_INSTALL_DIR}")
endif()
install(CODE "
if(NOT \"\$ENV{DESTDIR}\" STREQUAL \"\")
message(STATUS \"DESTDIR is set: not compiling GSettings schemas\")
elseif(NOT \"\$ENV{GSETTINGS_DISABLE_SCHEMAS_COMPILE}\" STREQUAL \"\")
message(STATUS \"GSETTINGS_DISABLE_SCHEMAS_COMPILE is set: not compiling GSettings schemas\")
else()
message(STATUS \"Compiling GSettings schemas in ${_schema_dir}\")
execute_process(COMMAND \"${GLIB_COMPILE_SCHEMAS_EXECUTABLE}\" \"${_schema_dir}\"
RESULT_VARIABLE _rc)
if(NOT _rc EQUAL 0)
message(FATAL_ERROR \"glib-compile-schemas failed with status \${_rc}\")
endif()
endif()
")
endif()
# ---------------------------------------------------------------------------
# Uninstall (replacement for "make uninstall"): removes the files listed in
# install_manifest.txt and recompiles the schema directory afterwards
# ---------------------------------------------------------------------------
if(NOT TARGET uninstall)
add_custom_target(uninstall
COMMAND ${CMAKE_COMMAND}
-DMANIFEST=${CMAKE_BINARY_DIR}/install_manifest.txt
-DGLIB_COMPILE_SCHEMAS=${GLIB_COMPILE_SCHEMAS_EXECUTABLE}
-DSCHEMA_DIR=${CMAKE_INSTALL_FULL_DATADIR}/glib-2.0/schemas
-P ${CMAKE_CURRENT_SOURCE_DIR}/cmake/uninstall.cmake
COMMENT "Uninstalling the files listed in install_manifest.txt")
endif()
# ---------------------------------------------------------------------------
# Icon and manual page
# ---------------------------------------------------------------------------
# Scalable first, with the existing bitmap for the small sizes.
install(FILES pixmaps/${APP_ID}.svg
DESTINATION ${CMAKE_INSTALL_DATADIR}/icons/hicolor/scalable/apps)
install(FILES pixmaps/morris.png
DESTINATION ${CMAKE_INSTALL_DATADIR}/icons/hicolor/48x48/apps
RENAME ${APP_ID}.png)
install(FILES ${APP_ID}.metainfo.xml
DESTINATION ${CMAKE_INSTALL_DATADIR}/metainfo)
install(FILES man/morris.6 DESTINATION ${CMAKE_INSTALL_MANDIR}/man6)
# ---------------------------------------------------------------------------
# Source tarball (replacement for "make dist")
# ---------------------------------------------------------------------------
set(CPACK_SOURCE_GENERATOR "TGZ;TBZ2")
set(CPACK_SOURCE_PACKAGE_FILE_NAME "${PROJECT_NAME}-${PROJECT_VERSION}")
# CPack matches these regexes against the ABSOLUTE path of every file, so the
# directory patterns are anchored to the source tree; otherwise a checkout
# below e.g. /build/ would produce an empty tarball.
string(REGEX REPLACE "([][+.*()^$])" "\\\\\\1" _src_re "${CMAKE_SOURCE_DIR}")
# A literal dot is written [.] rather than \.: CPack copies these patterns
# into its own configuration file, where \. is an invalid escape sequence.
set(CPACK_SOURCE_IGNORE_FILES
"^${_src_re}/[.]git/"
"^${_src_re}/[.]gitignore$"
"^${_src_re}/[.]claude/"
"^${_src_re}/CLAUDE[.]md$"
"^${_src_re}/build[^/]*/"
"^${_src_re}/cmake-build-[^/]*/"
"^${_src_re}/po/${PROJECT_NAME}[.]pot$"
"[.]tar[.](gz|bz2|xz)$"
"~$"
"[.]swp$")
set(CPACK_PACKAGE_VERSION_MAJOR ${PROJECT_VERSION_MAJOR})
set(CPACK_PACKAGE_VERSION_MINOR ${PROJECT_VERSION_MINOR})
set(CPACK_PACKAGE_VERSION_PATCH 0)
set(CPACK_PACKAGE_CONTACT "${PACKAGE_BUGREPORT}")
include(CPack)