Move SMaterial std::hash impl to its header
[minetest.git] / irr / src / CMakeLists.txt
bloba0560783057ebdbb93d567d8d0c99181d35fb24b
1 # When enabling SDL2 by default on macOS, don't forget to change
2 # "NSHighResolutionCapable" to true in "Info.plist".
3 if(NOT APPLE)
4         set(DEFAULT_SDL2 ON)
5 endif()
7 option(USE_SDL2 "Use the SDL2 backend" ${DEFAULT_SDL2})
9 # Compiler flags
11 if(CMAKE_BUILD_TYPE STREQUAL "Debug")
12         add_compile_definitions(_DEBUG)
13 endif()
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)
27                 if(IS_I686)
28                         message(STATUS "Detected Intel x86: using SSE instead of x87 FPU")
29                         add_compile_options(-mfpmath=sse -msse)
30                 endif()
31         endif()
33 elseif(MSVC)
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)
42         endif()
43 endif()
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")
51         set(SOLARIS TRUE)
52 endif()
54 # Device
56 if(WIN32)
57         add_compile_definitions(_IRR_WINDOWS_ _IRR_WINDOWS_API_)
58         set(DEVICE "WINDOWS")
59 elseif(APPLE)
60         add_compile_definitions(_IRR_OSX_PLATFORM_)
61         set(DEVICE "OSX")
62 elseif(ANDROID)
63         add_compile_definitions(_IRR_ANDROID_PLATFORM_)
64         if(NOT USE_SDL2)
65                 message(FATAL_ERROR "The Android build requires SDL2")
66         endif()
67 elseif(EMSCRIPTEN)
68         add_compile_definitions(_IRR_EMSCRIPTEN_PLATFORM_ _IRR_COMPILE_WITH_EGL_MANAGER_)
69         set(LINUX_PLATFORM TRUE)
70         set(DEVICE "SDL")
71 elseif(SOLARIS)
72         add_compile_definitions(_IRR_SOLARIS_PLATFORM_ _IRR_POSIX_API_)
73         set(DEVICE "X11")
74 else()
75         add_compile_definitions(_IRR_POSIX_API_)
76         set(LINUX_PLATFORM TRUE)
77         set(DEVICE "X11")
78 endif()
80 if(LINUX_PLATFORM)
81         add_compile_definitions(_IRR_LINUX_PLATFORM_)
82 endif()
84 if(MSVC)
85         add_compile_definitions(_CRT_SECURE_NO_WARNINGS)
86 endif()
88 if(USE_SDL2)
89         set(DEVICE "SDL")
90 elseif(DEVICE STREQUAL "SDL")
91         message(FATAL_ERROR "SDL was used but not enabled?!")
92 endif()
94 add_compile_definitions("_IRR_COMPILE_WITH_${DEVICE}_DEVICE_")
96 # X11
98 if(DEVICE STREQUAL "X11")
99         option(USE_X11 "Use X11" TRUE)
100 else()
101         set(USE_X11 FALSE)
102 endif()
104 if(LINUX_PLATFORM AND USE_X11)
105         option(USE_XINPUT2 "Use XInput2" TRUE)
106         option(USE_XCURSOR "Use XCursor" FALSE)
107 else()
108         set(USE_XINPUT2 FALSE)
109         set(USE_XCURSOR FALSE)
110 endif()
112 # Joystick
114 if(NOT (BSD OR SOLARIS OR EMSCRIPTEN))
115         add_compile_definitions(_IRR_COMPILE_WITH_JOYSTICK_EVENTS_)
116 endif()
118 # OpenGL
120 if(USE_SDL2)
121         if(NOT ANDROID)
122                 set(DEFAULT_OPENGL3 TRUE)
123         endif()
124 else()
125         set(DEFAULT_OPENGL3 FALSE)
126 endif()
127 option(ENABLE_OPENGL3 "Enable OpenGL 3+" ${DEFAULT_OPENGL3})
129 if(ANDROID OR EMSCRIPTEN)
130         set(ENABLE_OPENGL FALSE)
131 else()
132         option(ENABLE_OPENGL "Enable OpenGL" TRUE)
133 endif()
135 if(APPLE)
136         set(ENABLE_GLES2 FALSE)
137         set(ENABLE_WEBGL1 FALSE)
138 else()
139         if(ANDROID OR EMSCRIPTEN)
140                 set(DEFAULT_GLES2 TRUE)
141         endif()
142         if(EMSCRIPTEN)
143                 set(DEFAULT_WEBGL1 TRUE)
144         endif()
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
148         if(ENABLE_WEBGL1)
149                 set(ENABLE_GLES2 TRUE)
150         endif()
151 endif()
153 if(ENABLE_OPENGL OR (ENABLE_OPENGL3 AND NOT USE_SDL2))
154         if(ENABLE_OPENGL)
155                 add_compile_definitions(_IRR_COMPILE_WITH_OPENGL_)
156                 set(OPENGL_DIRECT_LINK TRUE) # driver relies on this
157         endif()
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_)
164         endif()
165 endif()
167 if(ENABLE_OPENGL3)
168         if(DEVICE STREQUAL "WINDOWS")
169                 # supported
170         elseif(DEVICE STREQUAL "X11")
171                 # supported
172         elseif (NOT USE_SDL2)
173                 message(FATAL_ERROR "OpenGL 3 driver requires SDL2")
174         endif()
175 endif()
177 if(ENABLE_GLES2)
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_)
181         endif()
182 endif()
184 if(ENABLE_WEBGL1)
185         add_compile_definitions(_IRR_COMPILE_WITH_WEBGL1_)
186 endif()
188 # Misc
190 include(TestBigEndian)
191 TEST_BIG_ENDIAN(BIG_ENDIAN)
192 if(BIG_ENDIAN)
193         add_compile_definitions(__BIG_ENDIAN__)
194 endif()
196 # Configuration report
198 message(STATUS "Device: ${DEVICE}")
199 message(STATUS "OpenGL: ${ENABLE_OPENGL}")
200 message(STATUS "OpenGL 3: ${ENABLE_OPENGL3}")
201 if (ENABLE_GLES2)
202         message(STATUS "OpenGL ES 2: ON (unified)")
203 else()
204         message(STATUS "OpenGL ES 2: OFF")
205 endif()
206 #message(STATUS "WebGL: ${ENABLE_WEBGL1}")
208 # Required libs
210 include(CheckCXXSourceCompiles)
212 find_package(ZLIB REQUIRED)
213 find_package(JPEG REQUIRED)
214 find_package(PNG REQUIRED)
216 if(ENABLE_GLES2)
217         find_package(OpenGLES2 REQUIRED)
218 endif()
219 if(ENABLE_OPENGL)
220         find_package(OpenGL REQUIRED)
221 endif()
222 if(USE_SDL2)
223         if(NOT ANDROID)
224                 find_package(SDL2 REQUIRED)
225         else()
226                 # provided by AndroidLibs.cmake
227         endif()
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\
235 #error\n\
236 #endif\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!")
240         endif()
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}")
247         endif()
248 endif()
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\
260 #error\n\
261 #endif\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)
265         endif()
266 endif()
268 # Platform-specific libs
270 if(ANDROID)
271         enable_language(C)
272 elseif(APPLE)
273         find_library(COCOA_LIB Cocoa REQUIRED)
274         find_library(IOKIT_LIB IOKit REQUIRED)
276         add_compile_definitions(GL_SILENCE_DEPRECATION)
277 elseif(NOT USE_SDL2)
278         # Unix probably
279         find_package(X11 REQUIRED)
280         if(USE_XINPUT2 AND NOT X11_Xi_FOUND)
281                 message(FATAL_ERROR "XInput not found")
282         endif()
283 endif()
285 set(link_includes
286         "${PROJECT_SOURCE_DIR}/include"
287         "${CMAKE_CURRENT_SOURCE_DIR}"
289         "${ZLIB_INCLUDE_DIR}"
290         "${JPEG_INCLUDE_DIR}"
291         "${PNG_INCLUDE_DIR}"
292         "$<$<BOOL:${USE_SDL2}>:${SDL2_INCLUDE_DIRS}>"
294         ${OPENGL_INCLUDE_DIR}
295         ${OPENGLES2_INCLUDE_DIR}
296         ${EGL_INCLUDE_DIR}
298         "$<$<BOOL:${USE_X11}>:${X11_INCLUDE_DIR}>"
301 # Source files
303 set(IRRMESHLOADER
304         CB3DMeshFileLoader.cpp
305         CGLTFMeshFileLoader.cpp
306         COBJMeshFileLoader.cpp
307         CXMeshFileLoader.cpp
310 add_library(IRRMESHOBJ OBJECT
311         CSkinnedMesh.cpp
312         CBoneSceneNode.cpp
313         CMeshSceneNode.cpp
314         CAnimatedMeshSceneNode.cpp
315         ${IRRMESHLOADER}
318 target_link_libraries(IRRMESHOBJ PUBLIC tiniergltf::tiniergltf)
320 set(IRRDRVROBJ
321         CNullDriver.cpp
322         CGLXManager.cpp
323         CWGLManager.cpp
324         CEGLManager.cpp
325         CSDLManager.cpp
326         mt_opengl_loader.cpp
329 # the two legacy drivers
331 if(ENABLE_OPENGL)
332         set(IRRDRVROBJ
333                 ${IRRDRVROBJ}
334                 COpenGLCacheHandler.cpp
335                 COpenGLDriver.cpp
336                 COpenGLSLMaterialRenderer.cpp
337                 COpenGLExtensionHandler.cpp
338         )
339 endif()
341 # the unified drivers
343 if(ENABLE_OPENGL3 OR ENABLE_GLES2)
344         set(IRRDRVROBJ
345                 ${IRRDRVROBJ}
346                 OpenGL/Driver.cpp
347                 OpenGL/ExtensionHandler.cpp
348                 OpenGL/FixedPipelineRenderer.cpp
349                 OpenGL/MaterialRenderer.cpp
350                 OpenGL/Renderer2D.cpp
351         )
352 endif()
354 if(ENABLE_OPENGL3)
355         set(IRRDRVROBJ
356                 ${IRRDRVROBJ}
357                 OpenGL3/Driver.cpp
358         )
359 endif()
361 if(ENABLE_GLES2)
362         set(IRRDRVROBJ
363                 ${IRRDRVROBJ}
364                 OpenGLES2/Driver.cpp
365         )
366 endif()
368 set(IRRIMAGEOBJ
369         CColorConverter.cpp
370         CImage.cpp
371         CImageLoaderJPG.cpp
372         CImageLoaderPNG.cpp
373         CImageLoaderTGA.cpp
374         CImageWriterJPG.cpp
375         CImageWriterPNG.cpp
378 add_library(IRRVIDEOOBJ OBJECT
379         CFPSCounter.cpp
380         ${IRRDRVROBJ}
381         ${IRRIMAGEOBJ}
384 add_library(IRRIOOBJ OBJECT
385         CFileList.cpp
386         CFileSystem.cpp
387         CLimitReadFile.cpp
388         CMemoryFile.cpp
389         CReadFile.cpp
390         CWriteFile.cpp
391         CZipReader.cpp
392         CAttributes.cpp
395 add_library(IRROTHEROBJ OBJECT
396         CIrrDeviceSDL.cpp
397         CIrrDeviceLinux.cpp
398         CIrrDeviceStub.cpp
399         CIrrDeviceWin32.cpp
400         CLogger.cpp
401         COSOperator.cpp
402         Irrlicht.cpp
403         os.cpp
406 if(ENABLE_OPENGL3)
407         target_compile_definitions(IRROTHEROBJ PRIVATE ENABLE_OPENGL3)
408 endif()
410 if(APPLE)
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
414                 CIrrDeviceOSX.mm
415                 CNSOGLManager.mm
416         )
417 endif()
419 if(USE_X11)
420         target_compile_definitions(IRROTHEROBJ PRIVATE _IRR_COMPILE_WITH_X11_)
421 endif()
423 if(USE_XINPUT2)
424         target_compile_definitions(IRROTHEROBJ PRIVATE _IRR_LINUX_X11_XINPUT2_)
425 endif()
427 if(USE_XCURSOR)
428         target_compile_definitions(IRROTHEROBJ PRIVATE _IRR_LINUX_XCURSOR_)
429 endif()
431 add_library(IRRGUIOBJ OBJECT
432         CGUIButton.cpp
433         CGUICheckBox.cpp
434         CGUIComboBox.cpp
435         CGUIEditBox.cpp
436         CGUIEnvironment.cpp
437         CGUIFileOpenDialog.cpp
438         CGUIFont.cpp
439         CGUIImage.cpp
440         CGUIListBox.cpp
441         CGUIScrollBar.cpp
442         CGUISkin.cpp
443         CGUIStaticText.cpp
444         CGUITabControl.cpp
445         CGUISpriteBank.cpp
446         CGUIImageList.cpp
449 # Library
451 # There have to be some sources in IrrlichtMt to workaround Cmake Xcode generator bug
452 add_library(IrrlichtMt STATIC
453         CBillboardSceneNode.cpp
454         CCameraSceneNode.cpp
455         CDummyTransformationSceneNode.cpp
456         CEmptySceneNode.cpp
457         CMeshManipulator.cpp
458         CSceneCollisionManager.cpp
459         CSceneManager.cpp
460         CMeshCache.cpp
462 foreach(object_lib
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})
470         else()
471                 # Add objects from object library to main library
472                 target_sources(IrrlichtMt PRIVATE $<TARGET_OBJECTS:${object_lib}>)
473         endif()
475         if(BUILD_WITH_TRACY)
476                 target_link_libraries(${object_lib} PRIVATE Tracy::TracyClient)
477         endif()
478 endforeach()
480 # Alias target provides add_submodule compatibility
481 add_library(IrrlichtMt::IrrlichtMt ALIAS IrrlichtMt)
483 target_include_directories(IrrlichtMt
484         PUBLIC
485                 "$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include/>"
486         PRIVATE
487                 "$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>"
488                 ${link_includes}
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
495         ${ZLIB_LIBRARY}
496         ${JPEG_LIBRARY}
497         ${PNG_LIBRARY}
498         "$<$<BOOL:${USE_SDL2}>:SDL2::SDL2>"
500         "$<$<BOOL:${OPENGL_DIRECT_LINK}>:${OPENGL_LIBRARIES}>"
501         ${EGL_LIBRARY}
503         # incl. transitive SDL2 dependencies for static linking
504         "$<$<PLATFORM_ID:Android>:-landroid -llog -lGLESv2 -lGLESv1_CM -lOpenSLES>"
505         ${COCOA_LIB}
506         ${IOKIT_LIB}
507         "$<$<PLATFORM_ID:Windows>:gdi32>"
508         "$<$<PLATFORM_ID:Windows>:winmm>"
509         "$<$<BOOL:${USE_X11}>:${X11_X11_LIB}>"
510         "$<$<BOOL:${USE_X11}>:${X11_Xi_LIB}>"
513 if(WIN32)
514         target_compile_definitions(IrrlichtMt INTERFACE _IRR_WINDOWS_API_) # used in _IRR_DEBUG_BREAK_IF definition in a public header
515 endif()
516 if(CMAKE_BUILD_TYPE STREQUAL "Debug")
517         target_compile_definitions(IrrlichtMt INTERFACE _DEBUG) # same
518 endif()
519 if(APPLE OR ANDROID OR EMSCRIPTEN)
520         target_compile_definitions(IrrlichtMt PUBLIC IRR_MOBILE_PATHS)
521 endif()