This commit is contained in:
Angel Ortigosa Perez
2026-02-12 17:45:06 +01:00
parent 6579882198
commit 2707e12133
80 changed files with 185 additions and 1489 deletions

113
lib/mlx/CMakeLists.txt Executable file → Normal file
View File

@@ -5,8 +5,8 @@
# CMake specifications
# -----------------------------------------------------------------------------
cmake_minimum_required (VERSION 3.16.0)
project(mlx42 VERSION 2.4.2)
cmake_minimum_required (VERSION 3.18.0)
project(mlx42 VERSION 2.3.3)
message(STATUS "MLX42 @ ${CMAKE_PROJECT_VERSION}")
# Variables
@@ -23,11 +23,9 @@ set_property(GLOBAL PROPERTY USE_FOLDERS ON)
list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake")
# Options
# -----------------------------------------------------------------------------
option(BUILD_SHARED_LIBS "Build mlx42 as a shared library." OFF)
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")
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
# -----------------------------------------------------------------------------
@@ -36,13 +34,14 @@ set(BUILD_TESTS OFF CACHE BOOL "Build the tests to verify the integrity of the l
add_definitions(-D LODEPNG_NO_COMPILE_ENCODER)
add_definitions(-D LODEPNG_NO_COMPILE_ANCILLARY_CHUNKS)
if(UNIX AND NOT EMSCRIPTEN)
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
@@ -55,55 +54,40 @@ if(UNIX AND NOT EMSCRIPTEN)
else()
message(STATUS "Building in RELEASE mode")
add_definitions(-D NDEBUG)
add_compile_options(-O3)
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
# -----------------------------------------------------------------------------
if (UNIX)
set(CCSHADER ${TOOLS_DIR}/compile_shader.sh)
else()
set(CCSHADER ${TOOLS_DIR}/compile_shader.bat)
endif()
if(EMSCRIPTEN)
set(EMSCRIPTEN_VALUE 1)
else()
set(EMSCRIPTEN_VALUE 0)
endif()
# Add custom command for fragment shader
add_custom_command(
COMMENT "Building fragment shader"
DEPENDS ${PROJECT_SOURCE_DIR}/shaders/default.frag
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/mlx_frag_shader.c
COMMAND ${CCSHADER} ${PROJECT_SOURCE_DIR}/shaders/default.frag ${EMSCRIPTEN_VALUE} > ${CMAKE_CURRENT_BINARY_DIR}/mlx_frag_shader.c
VERBATIM
PRE_BUILD
USES_TERMINAL
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 for vertex shader
add_custom_command(
COMMENT "Building vertex shader"
DEPENDS ${PROJECT_SOURCE_DIR}/shaders/default.vert
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/mlx_vert_shader.c
COMMAND ${CCSHADER} ${PROJECT_SOURCE_DIR}/shaders/default.vert ${EMSCRIPTEN_VALUE} > ${CMAKE_CURRENT_BINARY_DIR}/mlx_vert_shader.c
VERBATIM
PRE_BUILD
USES_TERMINAL
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
# -----------------------------------------------------------------------------
# The library type is now determined by the BUILD_SHARED_LIBS option.
# By default, it builds a STATIC library.
add_library(mlx42 # Changed from "add_library(mlx42 STATIC"
add_library(mlx42 STATIC
# Root
${SOURCE_DIR}/mlx_cursor.c
@@ -138,45 +122,32 @@ add_library(mlx42 # Changed from "add_library(mlx42 STATIC"
)
target_include_directories(mlx42 PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include)
# Add this block to control symbol visibility for shared library builds
if(BUILD_SHARED_LIBS)
set_target_properties(mlx42 PROPERTIES
C_VISIBILITY_PRESET hidden
CXX_VISIBILITY_PRESET hidden
)
target_compile_definitions(mlx42 PRIVATE MLX42_BUILD_SHARED)
endif()
# Dependencies
# -----------------------------------------------------------------------------
find_package(glfw3)
find_package(OpenGL REQUIRED)
if(EMSCRIPTEN)
target_link_libraries(mlx42 "-s USE_GLFW=3" "-s FULL_ES3=1")
else()
target_link_libraries(mlx42 OpenGL::GL)
find_package(glfw3)
if (glfw3_FOUND)
target_link_libraries(mlx42 ${GLFW3_LIBRARY})
endif()
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(APPLE)
target_link_libraries(mlx42 "-framework Cocoa" "-framework IOKit")
endif()
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
# 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