forked from codam-coding-college/MLX42
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCMakeLists.txt
180 lines (155 loc) · 5.62 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
# -----------------------------------------------------------------------------
# Codam Coding College, Amsterdam @ 2022-2023 by W2Wizard.
# See README in the root project for more information.
# -----------------------------------------------------------------------------
# CMake specifications
# -----------------------------------------------------------------------------
cmake_minimum_required (VERSION 3.18.0)
project(mlx42 VERSION 2.3.2)
message(STATUS "MLX42 @ ${CMAKE_PROJECT_VERSION}")
# Variables
# -----------------------------------------------------------------------------
set(SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/src)
set(TOOLS_DIR ${CMAKE_CURRENT_SOURCE_DIR}/tools)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})
set(CMAKE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
set(CMAKE_C_STANDARD 11)
set(CMAKE_C_EXTENSIONS OFF)
set(CMAKE_C_STANDARD_REQUIRED ON)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake")
# Options
set(DEBUG OFF CACHE BOOL "Build MLX42 in debug mode, enabling assertions")
set(GLFW_FETCH ON CACHE BOOL "Clone and install GLFW")
set(BUILD_TESTS OFF CACHE BOOL "Build the tests to verify the integrity of the lib")
# Compile Options
# -----------------------------------------------------------------------------
# Reduce the size of LodePNG, we don't need these things.
add_definitions(-D LODEPNG_NO_COMPILE_ENCODER)
add_definitions(-D LODEPNG_NO_COMPILE_ANCILLARY_CHUNKS)
if(UNIX)
set(CCSHADER ${PROJECT_SOURCE_DIR}/tools/compile_shader.sh)
add_compile_options(
-Wextra
-Wall
-Werror
-Wunreachable-code
# Some low priority warnings that are annoying.
-Wno-char-subscripts
-Wno-sign-compare
-Wno-unused-parameter
-Wno-missing-field-initializers
)
if(DEBUG)
message(STATUS "Building in DEBUG mode")
add_compile_options(-g)
else()
message(STATUS "Building in RELEASE mode")
add_definitions(-D NDEBUG)
add_compile_options(-Ofast)
endif(DEBUG)
else()
# TODO: Figure out what we need for windows.
set(CCSHADER ${PROJECT_SOURCE_DIR}/tools/compile_shader.bat)
endif()
# Build specific files
# @see https://cmake.org/cmake/help/latest/command/add_custom_command.html
# -----------------------------------------------------------------------------
add_custom_command(
COMMENT "Building fragment shader"
DEPENDS ${PROJECT_SOURCE_DIR}/shaders/default.frag
OUTPUT mlx_frag_shader.c
COMMAND ${CCSHADER} ${PROJECT_SOURCE_DIR}/shaders/default.frag > mlx_frag_shader.c
VERBATIM
PRE_BUILD
USES_TERMINAL
)
add_custom_command(
COMMENT "Building vertex shader"
DEPENDS ${PROJECT_SOURCE_DIR}/shaders/default.vert
OUTPUT mlx_vert_shader.c
COMMAND ${CCSHADER} ${PROJECT_SOURCE_DIR}/shaders/default.vert > mlx_vert_shader.c
VERBATIM
PRE_BUILD
USES_TERMINAL
)
# Sources
# -----------------------------------------------------------------------------
add_library(mlx42 STATIC
# Root
${SOURCE_DIR}/mlx_cursor.c
${SOURCE_DIR}/mlx_exit.c
${SOURCE_DIR}/mlx_images.c
${SOURCE_DIR}/mlx_init.c
${SOURCE_DIR}/mlx_keys.c
${SOURCE_DIR}/mlx_loop.c
${SOURCE_DIR}/mlx_monitor.c
${SOURCE_DIR}/mlx_mouse.c
${SOURCE_DIR}/mlx_put_pixel.c
${SOURCE_DIR}/mlx_window.c
# Utils
${SOURCE_DIR}/utils/mlx_error.c
${SOURCE_DIR}/utils/mlx_list.c
${SOURCE_DIR}/utils/mlx_utils.c
${SOURCE_DIR}/utils/mlx_compare.c
# Textures
${SOURCE_DIR}/font/mlx_font.c
${SOURCE_DIR}/textures/mlx_png.c
${SOURCE_DIR}/textures/mlx_texture.c
${SOURCE_DIR}/textures/mlx_xpm42.c
# Libs
lib/png/lodepng.c
lib/glad/glad.c
mlx_vert_shader.c
mlx_frag_shader.c
)
target_include_directories(mlx42 PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include)
# Dependencies
# -----------------------------------------------------------------------------
find_package(glfw3)
find_package(OpenGL REQUIRED)
target_link_libraries(mlx42 OpenGL::GL)
if (NOT glfw3_FOUND AND GLFW_FETCH)
message(STATUS "Install GLFW to suppress this message")
message(STATUS "Please wait, fetching GLFW ...")
include(${CMAKE_DIR}/LinkGLFW.cmake)
LinkGLFW(mlx42)
elseif(NOT glfw3_FOUND AND NOT GLFW_FETCH)
message(FATAL_ERROR "Unable to build: GLFW can't be found nor fetched.")
endif()
if (glfw3_FOUND)
target_link_libraries(mlx42 ${GLFW3_LIBRARY})
endif()
if(APPLE)
target_link_libraries(mlx42 "-framework Cocoa" "-framework IOKit")
endif()
# Testing
# -----------------------------------------------------------------------------
# Only build tests if we are the main project or explicitly told to, make sure
# tests are not built when mlx42 is included as a subproject, use MLX42_BUILD_TESTS to overwrite this
# use cmake -DBUILD_TESTS=ON/-DMLX42_BUILD_TESTS=ON to build tests
if ((PROJECT_NAME STREQUAL CMAKE_PROJECT_NAME AND BUILD_TESTS) OR MLX42_BUILD_TESTS)
add_subdirectory(tests)
enable_testing()
endif()
# Installation
# -----------------------------------------------------------------------------
# Convenience feature to install the library and headers to the system.
# Use cmake -DCMAKE_INSTALL_PREFIX=/usr/local for example to install to /usr/local
# or any other directory that you want to install to.
#
# This only really useful if you are a system administrator and want to install
# the library to the system, if you are a developer you should just use the
# library as a subproject as you probably don't have (nor really should) have any
# amibitons to use this for anything other than your own school projects.
install(
DIRECTORY ./include/MLX42 DESTINATION ${CMAKE_INSTALL_PREFIX}/include
FILES_MATCHING PATTERN MLX42.h
)
install(TARGETS mlx42
EXPORT mlx42Targets
RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}"
LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}"
)