This repository has been archived by the owner on Jul 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
CMakeLists.txt
364 lines (298 loc) · 11.7 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
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
cmake_minimum_required(VERSION 3.10)
project(shadertoy LANGUAGES CXX)
set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/modules")
include(CMakeDependentOption)
include(GNUInstallDirs)
include(GenerateExportHeader)
include(CMakePackageConfigHelpers)
# Load lib version from debian/substvars
file(STRINGS ${CMAKE_CURRENT_SOURCE_DIR}/debian/changelog DEBIAN_CHANGELOG
REGEX "^libshadertoy \\(.*\\) ")
list(GET DEBIAN_CHANGELOG 0 DEBIAN_CHANGELOG_HEAD)
string(REGEX REPLACE "^.*\\(([0-9]+\\.[0-9]+\\.[0-9]+)([-~].*)?\\).*$" "\\1" ST_VERSION ${DEBIAN_CHANGELOG_HEAD})
string(REGEX REPLACE "^.*\\(([0-9]+\\.[0-9]+\\.[0-9]+)([-~].*)?\\).*$" "\\1\\2"
ST_VERSION_FULL ${DEBIAN_CHANGELOG_HEAD})
# Parse the version string into major, minor and patch
string(REPLACE "." ";" ST_VERSION_LIST ${ST_VERSION})
list(GET ST_VERSION_LIST 0 VERSION_MAJOR)
list(GET ST_VERSION_LIST 1 VERSION_MINOR)
list(GET ST_VERSION_LIST 2 VERSION_PATCH)
set(VERSION "${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}")
mark_as_advanced(VERSION_MAJOR VERSION_MINOR VERSION_PATCH VERSION)
# Check if we are running in standalone build or in-source
if (${CMAKE_CURRENT_SOURCE_DIR} STREQUAL ${CMAKE_SOURCE_DIR})
option(SHADERTOY_STANDALONE "Set to ON when libshadertoy is being built from source instead of in a project" ON)
else()
option(SHADERTOY_STANDALONE "Set to ON when libshadertoy is being built from source instead of in a project" OFF)
endif()
if (SHADERTOY_STANDALONE)
set(SHADERTOY_VERSION_STANDALONE "Standalone build")
set(SHADERTOY_DEFAULT_TARGET_ARG "")
else()
set(SHADERTOY_VERSION_STANDALONE "In-source build")
set(SHADERTOY_DEFAULT_TARGET_ARG EXCLUDE_FROM_ALL)
endif()
# Print a message with the version number
message(STATUS "shadertoy v${ST_VERSION_FULL} (${SHADERTOY_VERSION_STANDALONE})")
# Project directories
set(INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/include)
set(SRC_DIR ${CMAKE_CURRENT_SOURCE_DIR}/src)
# Export compile commands for autocomplete
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
# Required packages
find_package(PkgConfig REQUIRED)
# Find GLM using CMake
find_package(glm REQUIRED)
# libepoxy extension loader
find_package(Epoxy REQUIRED)
# Use bundled spdlog
set(SPDLOG_BUILD_EXAMPLES OFF)
set(SPDLOG_BUILD_TESTING OFF)
set(SPDLOG_DIR ${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/spdlog)
add_subdirectory(${SPDLOG_DIR} EXCLUDE_FROM_ALL)
# spdlog requires a thread init library
find_package(Threads REQUIRED)
# Copy headers to include directory for build
set(SPDLOG_MAIN_INCLUDE ${CMAKE_CURRENT_BINARY_DIR}/shadertoy/spdlog/spdlog.h)
add_custom_command(OUTPUT ${SPDLOG_MAIN_INCLUDE}
MAIN_DEPENDENCY ${SPDLOG_DIR}/include/spdlog/spdlog.h
COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_CURRENT_BINARY_DIR}/shadertoy/spdlog
COMMAND ${CMAKE_COMMAND} -E copy_directory ${SPDLOG_DIR}/include/spdlog ${CMAKE_CURRENT_BINARY_DIR}/shadertoy/spdlog)
# uri.hh
set(URI_LIBRARY_DIR ${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/uri-library)
set(URI_LIBRARY_MAIN_INCLUDE ${CMAKE_CURRENT_BINARY_DIR}/shadertoy/uri.hpp)
add_custom_command(OUTPUT ${URI_LIBRARY_MAIN_INCLUDE}
MAIN_DEPENDENCY ${URI_LIBRARY_DIR}/uri.hh
COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_CURRENT_BINARY_DIR}/shadertoy
COMMAND ${CMAKE_COMMAND} -E copy ${URI_LIBRARY_DIR}/uri.hh ${URI_LIBRARY_MAIN_INCLUDE})
# libsoil
find_package(SOIL)
# libjpeg
find_package(JPEG)
# libopenexr
find_package(OpenEXR)
# Copy shaders to source
file(GLOB RESOURCES_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/shaders/*)
add_custom_command(
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/resources.cpp
${CMAKE_CURRENT_BINARY_DIR}/resources.hpp
DEPENDS ${RESOURCES_SOURCES}
COMMAND ${CMAKE_COMMAND}
-DRESOURCES_INPUT=${CMAKE_CURRENT_SOURCE_DIR}/shaders
-DRESOURCES_OUTPUT=${CMAKE_CURRENT_BINARY_DIR}/resources.cpp
-DRESOURCES_H_OUTPUT=${CMAKE_CURRENT_BINARY_DIR}/resources.hpp
-DRESOURCES_H_ID=_SHADERTOY_RESOURCES_HPP_
-P ${CMAKE_MODULE_PATH}/resources.cmake)
set(shadertoy_INCLUDE_DIRS
$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}> # for Resources.h
$<BUILD_INTERFACE:${INCLUDE_DIR}>
$<INSTALL_INTERFACE:include>)
file(GLOB_RECURSE SHADERTOY_LIBRARY_HEADERS ${INCLUDE_DIR}/*.hpp ${INCLUDE_DIR}/*.ipp)
file(GLOB_RECURSE SHADERTOY_LIBRARY_SOURCES ${SRC_DIR}/*.cpp)
add_library(shadertoy-objects OBJECT EXCLUDE_FROM_ALL
${SHADERTOY_LIBRARY_HEADERS}
${SHADERTOY_LIBRARY_SOURCES}
${CMAKE_CURRENT_BINARY_DIR}/resources.cpp
${POLYMORPHIC_VALUE_INCLUDE}
${SPDLOG_MAIN_INCLUDE}
${URI_LIBRARY_MAIN_INCLUDE})
# Enable warnings
if(MSVC)
target_compile_options(shadertoy-objects PRIVATE /W2 /wd4275 /wd4251)
else()
target_compile_options(shadertoy-objects PRIVATE -Wall -Wno-missing-braces -Wno-attributes -pedantic)
endif()
# Include directories
target_include_directories(shadertoy-objects PRIVATE
${shadertoy_INCLUDE_DIRS}
${OPENGL_INCLUDE_DIRS}
${Epoxy_INCLUDE_DIRS}
${GLM_INCLUDE_DIRS})
# The vcpkg port of SOIL requires OpenGL to be linked manually
if (WIN32 AND SOIL_FOUND)
find_package(OpenGL QUIET)
endif()
if (SOIL_FOUND AND (NOT WIN32 OR OpenGL_FOUND))
message(STATUS "Building with SOIL support")
target_include_directories(shadertoy-objects PRIVATE
${SOIL_INCLUDE_DIRS})
target_compile_definitions(shadertoy-objects PRIVATE
LIBSHADERTOY_SOIL=1)
else()
message(STATUS "Building without SOIL support")
target_compile_definitions(shadertoy-objects PRIVATE
LIBSHADERTOY_SOIL=0)
endif()
if (JPEG_FOUND)
message(STATUS "Building with JPEG support")
target_include_directories(shadertoy-objects PRIVATE
${JPEG_INCLUDE_DIRS})
target_compile_definitions(shadertoy-objects PRIVATE
LIBSHADERTOY_JPEG=1)
else()
message(STATUS "Building without JPEG support")
target_compile_definitions(shadertoy-objects PRIVATE
LIBSHADERTOY_JPEG=0)
endif()
if (OpenEXR_FOUND)
message(STATUS "Building with OpenEXR support")
target_include_directories(shadertoy-objects PRIVATE
${OpenEXR_INCLUDE_DIRS})
target_compile_definitions(shadertoy-objects PRIVATE
LIBSHADERTOY_OPENEXR=1)
# https://github.com/openexr/openexr/issues/235
set_property(SOURCE ${SRC_DIR}/inputs/exr_input.cpp PROPERTY COMPILE_FLAGS -std=c++14)
else()
message(STATUS "Building without OpenEXR support")
target_compile_definitions(shadertoy-objects PRIVATE
LIBSHADERTOY_OPENEXR=0)
endif()
# C++17
target_compile_features(shadertoy-objects PUBLIC cxx_std_17)
# -fPIC
set_property(TARGET shadertoy-objects PROPERTY POSITION_INDEPENDENT_CODE ON)
# Build static and shared libraries
set(LIBRARY_TYPES "SHARED;STATIC")
foreach(LIBRARY_TYPE ${LIBRARY_TYPES})
set(TARGET_NAME "shadertoy-${LIBRARY_TYPE}")
string(TOLOWER ${TARGET_NAME} TARGET_NAME)
message(STATUS "Creating target ${TARGET_NAME}")
# Main libshadertoy
add_library(${TARGET_NAME} ${LIBRARY_TYPE} ${SHADERTOY_DEFAULT_TARGET_ARG}
$<TARGET_OBJECTS:shadertoy-objects>)
# Link dependencies
target_link_libraries(${TARGET_NAME} PUBLIC
${Epoxy_LIBRARIES}
${JPEG_LIBRARIES}
${SOIL_LIBRARIES}
${CMAKE_THREAD_LIBS_INIT})
if (CMAKE_CXX_COMPILER_ID STREQUAL GNU)
target_link_libraries(${TARGET_NAME} PUBLIC stdc++fs)
endif()
if (SOIL_FOUND AND (NOT WIN32 OR OpenGL_FOUND))
target_link_libraries(${TARGET_NAME} PUBLIC ${SOIL_LIBRARIES})
# See find_package(SOIL) handling
if (WIN32)
target_link_libraries(${TARGET_NAME} PUBLIC ${OPENGL_LIBRARY})
endif()
endif()
if (JPEG_FOUND)
target_link_libraries(${TARGET_NAME} PUBLIC ${JPEG_LIBRARIES})
endif()
if (OpenEXR_FOUND)
target_link_libraries(${TARGET_NAME} PUBLIC ${OpenEXR_LIBRARIES})
endif()
if (NOT MSVC)
target_compile_options(${TARGET_NAME} PUBLIC -Wno-attributes)
endif()
# Include directories for install
target_include_directories(${TARGET_NAME} INTERFACE
${shadertoy_INCLUDE_DIRS}
${GLM_INCLUDE_DIRS})
# Set library file version
set_target_properties(${TARGET_NAME} PROPERTIES
SOVERSION ${VERSION_MAJOR}
VERSION ${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH})
if (SHADERTOY_STANDALONE)
# Install target
install(TARGETS ${TARGET_NAME}
DESTINATION ${CMAKE_INSTALL_LIBDIR}
EXPORT shadertoy)
endif()
endforeach()
set_target_properties(shadertoy-shared PROPERTIES OUTPUT_NAME shadertoy)
# Export header for shared
generate_export_header(shadertoy-shared
BASE_NAME shadertoy
EXPORT_MACRO_NAME shadertoy_EXPORT
EXPORT_FILE_NAME ${CMAKE_CURRENT_BINARY_DIR}/shadertoy/shadertoy_export.h)
target_compile_definitions(shadertoy-objects PUBLIC shadertoy_shared_EXPORTS)
if (SHADERTOY_STANDALONE)
# Export targets
export(EXPORT shadertoy FILE ${CMAKE_CURRENT_BINARY_DIR}/shadertoy.cmake)
# Export build package
export(PACKAGE shadertoy)
# Install exports
install(EXPORT shadertoy
COMPONENT shadertoy-dev
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/shadertoy)
# Configure the -config.cmake file
configure_file(shadertoy-config.cmake.in ${CMAKE_CURRENT_BINARY_DIR}/shadertoy-config.cmake @ONLY)
# Configure -version.cmake file
write_basic_package_version_file(${CMAKE_CURRENT_BINARY_DIR}/shadertoy-config-version.cmake
VERSION ${VERSION}
COMPATIBILITY SameMajorVersion)
# Install cmake config files
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/shadertoy-config.cmake
${CMAKE_CURRENT_BINARY_DIR}/shadertoy-config-version.cmake
COMPONENT shadertoy-dev
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/shadertoy)
# Install the examples
install(DIRECTORY examples
COMPONENT shadertoy-examples
DESTINATION ${CMAKE_INSTALL_DATADIR}/shadertoy
PATTERN ".idea" EXCLUDE
PATTERN "build" EXCLUDE
PATTERN "*.dump" EXCLUDE)
# Install headers
install(FILES ${INCLUDE_DIR}/shadertoy.hpp
COMPONENT shadertoy-dev
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
file(GLOB SHADERTOY_HEADERS ${INCLUDE_DIR}/shadertoy/*.hpp ${INCLUDE_DIR}/shadertoy/*.ipp)
list(APPEND SHADERTOY_HEADERS
${CMAKE_CURRENT_BINARY_DIR}/shadertoy/shadertoy_export.h)
install(FILES ${SHADERTOY_HEADERS}
COMPONENT shadertoy-dev
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/shadertoy)
file(GLOB SHADERTOY_HEADERS_DIRS RELATIVE ${INCLUDE_DIR}/shadertoy ${INCLUDE_DIR}/shadertoy/*)
foreach(SHADERTOY_HEADERS_ITEM ${SHADERTOY_HEADERS_DIRS})
if(IS_DIRECTORY ${INCLUDE_DIR}/shadertoy/${SHADERTOY_HEADERS_ITEM})
file(GLOB SHADERTOY_HEADER_FILES
${INCLUDE_DIR}/shadertoy/${SHADERTOY_HEADERS_ITEM}/*.hpp
${INCLUDE_DIR}/shadertoy/${SHADERTOY_HEADERS_ITEM}/*.ipp)
install(FILES ${SHADERTOY_HEADER_FILES}
COMPONENT shadertoy-dev
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/shadertoy/${SHADERTOY_HEADERS_ITEM})
endif()
endforeach()
install(DIRECTORY ${SPDLOG_DIR}/include/spdlog
COMPONENT shadertoy-dev
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/shadertoy)
endif()
# Documentation
find_package(Doxygen)
cmake_dependent_option(SHADERTOY_BUILD_DOCUMENTATION
"Build the HTML docs" ON
"DOXYGEN_FOUND" OFF)
if(SHADERTOY_BUILD_DOCUMENTATION)
if(NOT DOXYGEN_FOUND)
message(FATAL_ERROR "Doxygen is needed to build the documentation.")
endif()
#set(doxy_main_page ${CMAKE_CURRENT_SOURCE_DIR}/README.md)
set(doxy_main_page ${CMAKE_CURRENT_SOURCE_DIR}/README.md)
set(doxyfile_in ${CMAKE_CURRENT_SOURCE_DIR}/docs/Doxyfile.in)
set(doxyfile ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile)
configure_file(${doxyfile_in} ${doxyfile} @ONLY)
add_custom_target(shadertoy_doc
COMMAND ${DOXYGEN_EXECUTABLE} ${doxyfile}
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
COMMENT "Generating API documentation with Doxygen"
VERBATIM)
if (LIBSHADERTOY_BUILD_DOCUMENTATION)
install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/html
COMPONENT shadertoy-docs
DESTINATION ${CMAKE_INSTALL_DOCDIR})
endif()
endif()
if(SHADERTOY_BUILD_EXAMPLES)
add_subdirectory(examples)
endif()
# Package building
set(CPACK_GENERATOR "7Z")
execute_process(COMMAND git rev-parse --short HEAD
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
OUTPUT_VARIABLE GIT_REVISION
OUTPUT_STRIP_TRAILING_WHITESPACE
ERROR_QUIET)
set(CPACK_PACKAGE_FILE_NAME "libshadertoy-${ST_VERSION_FULL}-${GIT_REVISION}")
include(CPack)