|
| 1 | +# ----------------------------------------------------------------------------- |
| 2 | +# Codam Coding College, Amsterdam @ 2022-2023 by W2Wizard. |
| 3 | +# See README in the root project for more information. |
| 4 | +# ----------------------------------------------------------------------------- |
| 5 | + |
| 6 | +# CMake specifications |
| 7 | +# ----------------------------------------------------------------------------- |
| 8 | +cmake_minimum_required (VERSION 3.18.0) |
| 9 | +project(mlx42 VERSION 2.3.3) |
| 10 | +message(STATUS "MLX42 @ ${CMAKE_PROJECT_VERSION}") |
| 11 | + |
| 12 | +# Variables |
| 13 | +# ----------------------------------------------------------------------------- |
| 14 | +set(SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/src) |
| 15 | +set(TOOLS_DIR ${CMAKE_CURRENT_SOURCE_DIR}/tools) |
| 16 | +set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}) |
| 17 | +set(CMAKE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/cmake) |
| 18 | +set(CMAKE_C_STANDARD 11) |
| 19 | +set(CMAKE_C_EXTENSIONS OFF) |
| 20 | +set(CMAKE_C_STANDARD_REQUIRED ON) |
| 21 | +set(CMAKE_POSITION_INDEPENDENT_CODE ON) |
| 22 | +set_property(GLOBAL PROPERTY USE_FOLDERS ON) |
| 23 | +list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake") |
| 24 | + |
| 25 | +# Options |
| 26 | +set(DEBUG OFF CACHE BOOL "Build MLX42 in debug mode, enabling assertions") |
| 27 | +set(GLFW_FETCH ON CACHE BOOL "Clone and install GLFW") |
| 28 | +set(BUILD_TESTS OFF CACHE BOOL "Build the tests to verify the integrity of the lib") |
| 29 | + |
| 30 | +# Compile Options |
| 31 | +# ----------------------------------------------------------------------------- |
| 32 | + |
| 33 | +# Reduce the size of LodePNG, we don't need these things. |
| 34 | +add_definitions(-D LODEPNG_NO_COMPILE_ENCODER) |
| 35 | +add_definitions(-D LODEPNG_NO_COMPILE_ANCILLARY_CHUNKS) |
| 36 | + |
| 37 | +if(UNIX) |
| 38 | + set(CCSHADER ${PROJECT_SOURCE_DIR}/tools/compile_shader.sh) |
| 39 | + add_compile_options( |
| 40 | + -Wextra |
| 41 | + -Wall |
| 42 | + -Werror |
| 43 | + -Wunreachable-code |
| 44 | + |
| 45 | + # Some low priority warnings that are annoying. |
| 46 | + -Wno-char-subscripts |
| 47 | + -Wno-sign-compare |
| 48 | + -Wno-unused-parameter |
| 49 | + -Wno-missing-field-initializers |
| 50 | + ) |
| 51 | + if(DEBUG) |
| 52 | + message(STATUS "Building in DEBUG mode") |
| 53 | + add_compile_options(-g) |
| 54 | + else() |
| 55 | + message(STATUS "Building in RELEASE mode") |
| 56 | + add_definitions(-D NDEBUG) |
| 57 | + add_compile_options(-Ofast) |
| 58 | + endif(DEBUG) |
| 59 | +else() |
| 60 | + # TODO: Figure out what we need for windows. |
| 61 | + set(CCSHADER ${PROJECT_SOURCE_DIR}/tools/compile_shader.bat) |
| 62 | +endif() |
| 63 | + |
| 64 | +# Build specific files |
| 65 | +# @see https://cmake.org/cmake/help/latest/command/add_custom_command.html |
| 66 | +# ----------------------------------------------------------------------------- |
| 67 | + |
| 68 | +add_custom_command( |
| 69 | + COMMENT "Building fragment shader" |
| 70 | + DEPENDS ${PROJECT_SOURCE_DIR}/shaders/default.frag |
| 71 | + OUTPUT mlx_frag_shader.c |
| 72 | + COMMAND ${CCSHADER} ${PROJECT_SOURCE_DIR}/shaders/default.frag > mlx_frag_shader.c |
| 73 | + VERBATIM |
| 74 | + PRE_BUILD |
| 75 | + USES_TERMINAL |
| 76 | +) |
| 77 | + |
| 78 | +add_custom_command( |
| 79 | + COMMENT "Building vertex shader" |
| 80 | + DEPENDS ${PROJECT_SOURCE_DIR}/shaders/default.vert |
| 81 | + OUTPUT mlx_vert_shader.c |
| 82 | + COMMAND ${CCSHADER} ${PROJECT_SOURCE_DIR}/shaders/default.vert > mlx_vert_shader.c |
| 83 | + VERBATIM |
| 84 | + PRE_BUILD |
| 85 | + USES_TERMINAL |
| 86 | +) |
| 87 | + |
| 88 | +# Sources |
| 89 | +# ----------------------------------------------------------------------------- |
| 90 | +add_library(mlx42 STATIC |
| 91 | + |
| 92 | + # Root |
| 93 | + ${SOURCE_DIR}/mlx_cursor.c |
| 94 | + ${SOURCE_DIR}/mlx_exit.c |
| 95 | + ${SOURCE_DIR}/mlx_images.c |
| 96 | + ${SOURCE_DIR}/mlx_init.c |
| 97 | + ${SOURCE_DIR}/mlx_keys.c |
| 98 | + ${SOURCE_DIR}/mlx_loop.c |
| 99 | + ${SOURCE_DIR}/mlx_monitor.c |
| 100 | + ${SOURCE_DIR}/mlx_mouse.c |
| 101 | + ${SOURCE_DIR}/mlx_put_pixel.c |
| 102 | + ${SOURCE_DIR}/mlx_window.c |
| 103 | + |
| 104 | + # Utils |
| 105 | + ${SOURCE_DIR}/utils/mlx_error.c |
| 106 | + ${SOURCE_DIR}/utils/mlx_list.c |
| 107 | + ${SOURCE_DIR}/utils/mlx_utils.c |
| 108 | + ${SOURCE_DIR}/utils/mlx_compare.c |
| 109 | + |
| 110 | + # Textures |
| 111 | + ${SOURCE_DIR}/font/mlx_font.c |
| 112 | + ${SOURCE_DIR}/textures/mlx_png.c |
| 113 | + ${SOURCE_DIR}/textures/mlx_texture.c |
| 114 | + ${SOURCE_DIR}/textures/mlx_xpm42.c |
| 115 | + |
| 116 | + # Libs |
| 117 | + lib/png/lodepng.c |
| 118 | + lib/glad/glad.c |
| 119 | + |
| 120 | + mlx_vert_shader.c |
| 121 | + mlx_frag_shader.c |
| 122 | +) |
| 123 | +target_include_directories(mlx42 PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include) |
| 124 | + |
| 125 | +# Dependencies |
| 126 | +# ----------------------------------------------------------------------------- |
| 127 | + |
| 128 | +find_package(glfw3) |
| 129 | +find_package(OpenGL REQUIRED) |
| 130 | + |
| 131 | +target_link_libraries(mlx42 OpenGL::GL) |
| 132 | +if (NOT glfw3_FOUND AND GLFW_FETCH) |
| 133 | + message(STATUS "Install GLFW to suppress this message") |
| 134 | + message(STATUS "Please wait, fetching GLFW ...") |
| 135 | + include(${CMAKE_DIR}/LinkGLFW.cmake) |
| 136 | + LinkGLFW(mlx42) |
| 137 | +elseif(NOT glfw3_FOUND AND NOT GLFW_FETCH) |
| 138 | + message(FATAL_ERROR "Unable to build: GLFW can't be found nor fetched.") |
| 139 | +endif() |
| 140 | + |
| 141 | +if (glfw3_FOUND) |
| 142 | + target_link_libraries(mlx42 ${GLFW3_LIBRARY}) |
| 143 | +endif() |
| 144 | +if(APPLE) |
| 145 | + target_link_libraries(mlx42 "-framework Cocoa" "-framework IOKit") |
| 146 | +endif() |
| 147 | + |
| 148 | +# Testing |
| 149 | +# ----------------------------------------------------------------------------- |
| 150 | +# Only build tests if we are the main project or explicitly told to, make sure |
| 151 | +# tests are not built when mlx42 is included as a subproject, use MLX42_BUILD_TESTS to overwrite this |
| 152 | +# use cmake -DBUILD_TESTS=ON/-DMLX42_BUILD_TESTS=ON to build tests |
| 153 | + |
| 154 | +if ((PROJECT_NAME STREQUAL CMAKE_PROJECT_NAME AND BUILD_TESTS) OR MLX42_BUILD_TESTS) |
| 155 | + add_subdirectory(tests) |
| 156 | + enable_testing() |
| 157 | +endif() |
| 158 | + |
| 159 | +# Installation |
| 160 | +# ----------------------------------------------------------------------------- |
| 161 | +# Convenience feature to install the library and headers to the system. |
| 162 | +# Use cmake -DCMAKE_INSTALL_PREFIX=/usr/local for example to install to /usr/local |
| 163 | +# or any other directory that you want to install to. |
| 164 | +# |
| 165 | +# This only really useful if you are a system administrator and want to install |
| 166 | +# the library to the system, if you are a developer you should just use the |
| 167 | +# library as a subproject as you probably don't have (nor really should) have any |
| 168 | +# ambitions to use this for anything other than your own school projects. |
| 169 | + |
| 170 | +install( |
| 171 | + DIRECTORY ./include/MLX42 DESTINATION ${CMAKE_INSTALL_PREFIX}/include |
| 172 | + FILES_MATCHING PATTERN MLX42.h |
| 173 | +) |
| 174 | + |
| 175 | +install(TARGETS mlx42 |
| 176 | + EXPORT mlx42Targets |
| 177 | + RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}" |
| 178 | + ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}" |
| 179 | + LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}" |
| 180 | +) |
0 commit comments