1 # When enabling SDL2 by default on macOS, don't forget to change
2 # "NSHighResolutionCapable" to true in "Info.plist".
7 option(USE_SDL2 "Use the SDL2 backend" ${DEFAULT_SDL2})
11 if(CMAKE_BUILD_TYPE STREQUAL "Debug")
12 add_compile_definitions(_DEBUG)
14 set(CMAKE_POSITION_INDEPENDENT_CODE TRUE)
16 if(CMAKE_CXX_COMPILER_ID MATCHES "^(GNU|Clang|AppleClang)$")
17 set(CMAKE_CXX_FLAGS_RELEASE "-O3")
18 set(CMAKE_CXX_FLAGS_DEBUG "-g")
20 add_compile_options(-Wall -pipe)
22 # Enable SSE for floating point math on 32-bit x86 by default
23 # reasoning see minetest issue #11810 and https://gcc.gnu.org/wiki/FloatingPointMath
24 if(CMAKE_SIZEOF_VOID_P EQUAL 4)
25 include(CheckCXXSourceCompiles)
26 check_cxx_source_compiles("#ifndef __i686__\n#error\n#endif\nint main(){}" IS_I686)
28 message(STATUS "Detected Intel x86: using SSE instead of x87 FPU")
29 add_compile_options(-mfpmath=sse -msse)
34 string(APPEND CMAKE_CXX_STANDARD_LIBRARIES " msvcrt.lib") # ???? fuck off
36 add_compile_options(/Zl)
38 # Enable SSE for floating point math on 32-bit x86 by default
39 # reasoning see minetest issue #11810 and https://gcc.gnu.org/wiki/FloatingPointMath
40 if(CMAKE_SIZEOF_VOID_P EQUAL 4)
41 add_compile_options(/arch:SSE)
45 # Platform-independent configuration (hard-coded currently)
46 add_compile_definitions(IRR_ENABLE_BUILTIN_FONT)
48 # Platform-specific configuration
50 if(CMAKE_SYSTEM_NAME STREQUAL "SunOS")
57 add_compile_definitions(_IRR_WINDOWS_ _IRR_WINDOWS_API_)
60 add_compile_definitions(_IRR_OSX_PLATFORM_)
63 add_compile_definitions(_IRR_ANDROID_PLATFORM_)
65 message(FATAL_ERROR "The Android build requires SDL2")
68 add_compile_definitions(_IRR_EMSCRIPTEN_PLATFORM_ _IRR_COMPILE_WITH_EGL_MANAGER_)
69 set(LINUX_PLATFORM TRUE)
72 add_compile_definitions(_IRR_SOLARIS_PLATFORM_ _IRR_POSIX_API_)
75 add_compile_definitions(_IRR_POSIX_API_)
76 set(LINUX_PLATFORM TRUE)
81 add_compile_definitions(_IRR_LINUX_PLATFORM_)
85 add_compile_definitions(_CRT_SECURE_NO_WARNINGS)
90 elseif(DEVICE STREQUAL "SDL")
91 message(FATAL_ERROR "SDL was used but not enabled?!")
94 add_compile_definitions("_IRR_COMPILE_WITH_${DEVICE}_DEVICE_")
98 if(DEVICE STREQUAL "X11")
99 option(USE_X11 "Use X11" TRUE)
104 if(LINUX_PLATFORM AND USE_X11)
105 option(USE_XINPUT2 "Use XInput2" TRUE)
106 option(USE_XCURSOR "Use XCursor" FALSE)
108 set(USE_XINPUT2 FALSE)
109 set(USE_XCURSOR FALSE)
114 if(NOT (BSD OR SOLARIS OR EMSCRIPTEN))
115 add_compile_definitions(_IRR_COMPILE_WITH_JOYSTICK_EVENTS_)
122 set(DEFAULT_OPENGL3 TRUE)
125 set(DEFAULT_OPENGL3 FALSE)
127 option(ENABLE_OPENGL3 "Enable OpenGL 3+" ${DEFAULT_OPENGL3})
129 if(ANDROID OR EMSCRIPTEN)
130 set(ENABLE_OPENGL FALSE)
132 option(ENABLE_OPENGL "Enable OpenGL" TRUE)
136 set(ENABLE_GLES2 FALSE)
137 set(ENABLE_WEBGL1 FALSE)
139 if(ANDROID OR EMSCRIPTEN)
140 set(DEFAULT_GLES2 TRUE)
143 set(DEFAULT_WEBGL1 TRUE)
145 option(ENABLE_GLES2 "Enable OpenGL ES 2+" ${DEFAULT_GLES2})
146 #option(ENABLE_WEBGL1 "Enable WebGL (requires GLES2)" ${DEFAULT_WEBGL1})
147 set(ENABLE_WEBGL1 FALSE) # not working currently
149 set(ENABLE_GLES2 TRUE)
153 if(ENABLE_OPENGL OR (ENABLE_OPENGL3 AND NOT USE_SDL2))
155 add_compile_definitions(_IRR_COMPILE_WITH_OPENGL_)
156 set(OPENGL_DIRECT_LINK TRUE) # driver relies on this
158 if(DEVICE STREQUAL "WINDOWS")
159 add_compile_definitions(_IRR_COMPILE_WITH_WGL_MANAGER_)
160 elseif(DEVICE STREQUAL "X11")
161 add_compile_definitions(_IRR_COMPILE_WITH_GLX_MANAGER_)
162 elseif(DEVICE STREQUAL "OSX")
163 add_compile_definitions(_IRR_COMPILE_WITH_NSOGL_MANAGER_)
168 if(DEVICE STREQUAL "WINDOWS")
170 elseif(DEVICE STREQUAL "X11")
172 elseif (NOT USE_SDL2)
173 message(FATAL_ERROR "OpenGL 3 driver requires SDL2")
178 add_compile_definitions(_IRR_COMPILE_WITH_OGLES2_)
179 if(DEVICE MATCHES "^(WINDOWS|X11)$" OR EMSCRIPTEN)
180 add_compile_definitions(_IRR_COMPILE_WITH_EGL_MANAGER_)
185 add_compile_definitions(_IRR_COMPILE_WITH_WEBGL1_)
190 include(TestBigEndian)
191 TEST_BIG_ENDIAN(BIG_ENDIAN)
193 add_compile_definitions(__BIG_ENDIAN__)
196 # Configuration report
198 message(STATUS "Device: ${DEVICE}")
199 message(STATUS "OpenGL: ${ENABLE_OPENGL}")
200 message(STATUS "OpenGL 3: ${ENABLE_OPENGL3}")
202 message(STATUS "OpenGL ES 2: ON (unified)")
204 message(STATUS "OpenGL ES 2: OFF")
206 #message(STATUS "WebGL: ${ENABLE_WEBGL1}")
210 include(CheckCXXSourceCompiles)
212 find_package(ZLIB REQUIRED)
213 find_package(JPEG REQUIRED)
214 find_package(PNG REQUIRED)
217 find_package(OpenGLES2 REQUIRED)
220 find_package(OpenGL REQUIRED)
224 find_package(SDL2 REQUIRED)
226 # provided by AndroidLibs.cmake
228 message(STATUS "Found SDL2: ${SDL2_LIBRARIES}")
230 # unfortunately older SDL does not provide its version to cmake, so check header.
231 set(CMAKE_REQUIRED_INCLUDES ${SDL2_INCLUDE_DIRS})
232 unset(CHECK_SDL_VERSION CACHE)
233 check_cxx_source_compiles("#include <SDL_version.h>\n\
234 #if !SDL_VERSION_ATLEAST(2, 0, 10)\n\
237 int main() {}" CHECK_SDL_VERSION)
238 if(NOT CHECK_SDL_VERSION)
239 message(FATAL_ERROR "SDL2 is too old, required is at least 2.0.10!")
242 # ...no target either.
243 if(NOT TARGET SDL2::SDL2)
244 add_library(SDL2::SDL2 SHARED IMPORTED)
245 set_target_properties(SDL2::SDL2 PROPERTIES
246 IMPORTED_LOCATION "${SDL2_LIBRARIES}")
250 # More special config
252 if(ENABLE_OPENGL AND DEVICE STREQUAL "SDL")
253 # The legacy GL driver requires some symbols from GL 4.5 to compile,
254 # which SDL only provides since 2.26.0 (Nov 2022).
255 # We have a fallback in case this isn't satisfied so test for it.
256 set(CMAKE_REQUIRED_INCLUDES ${SDL2_INCLUDE_DIRS})
257 unset(CHECK_GL_VERSION_4_5 CACHE)
258 check_cxx_source_compiles("#include <SDL_opengl.h>\n\
259 #ifndef GL_VERSION_4_5\n\
262 int main() {}" CHECK_GL_VERSION_4_5)
263 if(CHECK_GL_VERSION_4_5)
264 add_compile_definitions(IRR_PREFER_SDL_GL_HEADER)
268 # Platform-specific libs
273 find_library(COCOA_LIB Cocoa REQUIRED)
274 find_library(IOKIT_LIB IOKit REQUIRED)
276 add_compile_definitions(GL_SILENCE_DEPRECATION)
279 find_package(X11 REQUIRED)
280 if(USE_XINPUT2 AND NOT X11_Xi_FOUND)
281 message(FATAL_ERROR "XInput not found")
286 "${PROJECT_SOURCE_DIR}/include"
287 "${CMAKE_CURRENT_SOURCE_DIR}"
289 "${ZLIB_INCLUDE_DIR}"
290 "${JPEG_INCLUDE_DIR}"
292 "$<$<BOOL:${USE_SDL2}>:${SDL2_INCLUDE_DIRS}>"
294 ${OPENGL_INCLUDE_DIR}
295 ${OPENGLES2_INCLUDE_DIR}
298 "$<$<BOOL:${USE_X11}>:${X11_INCLUDE_DIR}>"
304 CB3DMeshFileLoader.cpp
305 CGLTFMeshFileLoader.cpp
306 COBJMeshFileLoader.cpp
310 add_library(IRRMESHOBJ OBJECT
314 CAnimatedMeshSceneNode.cpp
318 target_link_libraries(IRRMESHOBJ PUBLIC tiniergltf::tiniergltf)
329 # the two legacy drivers
334 COpenGLCacheHandler.cpp
336 COpenGLSLMaterialRenderer.cpp
337 COpenGLExtensionHandler.cpp
341 # the unified drivers
343 if(ENABLE_OPENGL3 OR ENABLE_GLES2)
347 OpenGL/ExtensionHandler.cpp
348 OpenGL/FixedPipelineRenderer.cpp
349 OpenGL/MaterialRenderer.cpp
350 OpenGL/Renderer2D.cpp
378 add_library(IRRVIDEOOBJ OBJECT
384 add_library(IRRIOOBJ OBJECT
395 add_library(IRROTHEROBJ OBJECT
407 target_compile_definitions(IRROTHEROBJ PRIVATE ENABLE_OPENGL3)
411 # Build all IRROTHEROBJ sources as objc++, including the .cpp's
412 set_target_properties(IRROTHEROBJ PROPERTIES COMPILE_OPTIONS "-xobjective-c++")
413 target_sources(IRROTHEROBJ PRIVATE
420 target_compile_definitions(IRROTHEROBJ PRIVATE _IRR_COMPILE_WITH_X11_)
424 target_compile_definitions(IRROTHEROBJ PRIVATE _IRR_LINUX_X11_XINPUT2_)
428 target_compile_definitions(IRROTHEROBJ PRIVATE _IRR_LINUX_XCURSOR_)
431 add_library(IRRGUIOBJ OBJECT
437 CGUIFileOpenDialog.cpp
451 # There have to be some sources in IrrlichtMt to workaround Cmake Xcode generator bug
452 add_library(IrrlichtMt STATIC
453 CBillboardSceneNode.cpp
455 CDummyTransformationSceneNode.cpp
458 CSceneCollisionManager.cpp
463 IRRMESHOBJ IRRVIDEOOBJ
464 IRRIOOBJ IRROTHEROBJ IRRGUIOBJ)
465 # Set include directories for object library compilation
466 target_include_directories(${object_lib} PRIVATE ${link_includes})
467 if(CMAKE_GENERATOR STREQUAL "Xcode")
468 # Workaround for Cmake Xcode project generator
469 target_link_libraries(IrrlichtMt PRIVATE ${object_lib})
471 # Add objects from object library to main library
472 target_sources(IrrlichtMt PRIVATE $<TARGET_OBJECTS:${object_lib}>)
476 target_link_libraries(${object_lib} PRIVATE Tracy::TracyClient)
480 # Alias target provides add_submodule compatibility
481 add_library(IrrlichtMt::IrrlichtMt ALIAS IrrlichtMt)
483 target_include_directories(IrrlichtMt
485 "$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include/>"
487 "$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>"
491 # this needs to be here and not in a variable (like link_includes) due to issues
492 # with the generator expressions on at least CMake 3.22, but not 3.28 or later
493 target_link_libraries(IrrlichtMt PRIVATE
494 tiniergltf::tiniergltf
498 "$<$<BOOL:${USE_SDL2}>:SDL2::SDL2>"
500 "$<$<BOOL:${OPENGL_DIRECT_LINK}>:${OPENGL_LIBRARIES}>"
503 # incl. transitive SDL2 dependencies for static linking
504 "$<$<PLATFORM_ID:Android>:-landroid -llog -lGLESv2 -lGLESv1_CM -lOpenSLES>"
507 "$<$<PLATFORM_ID:Windows>:gdi32>"
508 "$<$<PLATFORM_ID:Windows>:winmm>"
509 "$<$<BOOL:${USE_X11}>:${X11_X11_LIB}>"
510 "$<$<BOOL:${USE_X11}>:${X11_Xi_LIB}>"
514 target_compile_definitions(IrrlichtMt INTERFACE _IRR_WINDOWS_API_) # used in _IRR_DEBUG_BREAK_IF definition in a public header
516 if(CMAKE_BUILD_TYPE STREQUAL "Debug")
517 target_compile_definitions(IrrlichtMt INTERFACE _DEBUG) # same
519 if(APPLE OR ANDROID OR EMSCRIPTEN)
520 target_compile_definitions(IrrlichtMt PUBLIC IRR_MOBILE_PATHS)