diff options
| author | hristo <[email protected]> | 2021-01-14 00:10:02 +0200 |
|---|---|---|
| committer | GitHub <[email protected]> | 2021-01-13 23:10:02 +0100 |
| commit | 9821725c6bafeaf4153ab8604e692dfe13cbff09 (patch) | |
| tree | 5dc264f722473f8f5f6c48dcfb4d1ada5761f501 /cmake | |
| parent | 3d22709808fd9ba2843e6cad5e106bcd90eecaca (diff) | |
| download | raylib-9821725c6bafeaf4153ab8604e692dfe13cbff09.tar.gz raylib-9821725c6bafeaf4153ab8604e692dfe13cbff09.zip | |
Big cmake changes (#1514)
* Delete emscripten.cmake
This file is not needed at this point. EMSDK provides a toolchain file that has a lot more things in it and is better supported. Project currently works fine with the documentation provided in Emscripten SDK on how to build projects.
* First pass file separation.
The main two files are cleaner now. Only important things can be seen. Major changes include:
- raylib_static is now the alias instead of raylib
- Repeating segments are removed and pulled into separate files into <root>/cmake
- File is reordered to make more sense
- Installs are better structured
- Library is build into an output directory "raylib" instead of "src"
- All public header files are now set as a public header file
- Source files need to be listed (it is a bad practice to capture them using wildcards and file globs)
- CMakeLists are better commented
* Second pass on the example dirs.
They are quite complex so I'm more hesitant to do major changes. Also it works pretty well. Noticed that I forgot one of the seperated files and added it into src/CMakeLists.txt.
* Returned the header copy as it was convenient to have the public headers copied.
* A better description to the variable RAYLIB_IS_MAIN
Co-authored-by: Rob Loach <[email protected]>
* Remove debug message
Co-authored-by: Rob Loach <[email protected]>
* Improvements based on review.
* Simplify the install condition to not be platform specific as it was before.
Co-authored-by: Alexander Neumann <[email protected]>
* Remove some CMAKE variables as they don't affect the build in any way
Co-authored-by: Alexander Neumann <[email protected]>
Co-authored-by: Rob Loach <[email protected]>
Co-authored-by: Alexander Neumann <[email protected]>
Diffstat (limited to 'cmake')
| -rw-r--r-- | cmake/BuildOptions.cmake | 39 | ||||
| -rw-r--r-- | cmake/BuildType.cmake | 43 | ||||
| -rw-r--r-- | cmake/CompilerFlags.cmake | 33 | ||||
| -rw-r--r-- | cmake/GlfwImport.cmake | 29 | ||||
| -rw-r--r-- | cmake/InstallConfigurations.cmake | 29 | ||||
| -rw-r--r-- | cmake/LibraryConfigurations.cmake | 109 | ||||
| -rw-r--r-- | cmake/PackConfigurations.cmake | 13 | ||||
| -rw-r--r-- | cmake/emscripten.cmake | 23 |
8 files changed, 252 insertions, 66 deletions
diff --git a/cmake/BuildOptions.cmake b/cmake/BuildOptions.cmake new file mode 100644 index 00000000..00586c23 --- /dev/null +++ b/cmake/BuildOptions.cmake @@ -0,0 +1,39 @@ +if(NOT (STATIC OR SHARED)) + message(FATAL_ERROR "Nothing to do if both -DSHARED=OFF and -DSTATIC=OFF...") +endif() + +if (DEFINED BUILD_SHARED_LIBS) + set(SHARED ${BUILD_SHARED_LIBS}) + if (${BUILD_SHARED_LIBS}) + set(STATIC OFF) + else() + set(STATIC ON) + endif() +endif() +if(DEFINED SHARED_RAYLIB) + set(SHARED ${SHARED_RAYLIB}) + message(DEPRECATION "-DSHARED_RAYLIB is deprecated. Please use -DSHARED instead.") +endif() +if(DEFINED STATIC_RAYLIB) + set(STATIC ${STATIC_RAYLIB}) + message(DEPRECATION "-DSTATIC_RAYLIB is deprecated. Please use -DSTATIC instead.") +endif() + +if(${PLATFORM} MATCHES "Desktop" AND APPLE) + if(MACOS_FATLIB) + if (CMAKE_OSX_ARCHITECTURES) + message(FATAL_ERROR "User supplied -DCMAKE_OSX_ARCHITECTURES overrides -DMACOS_FATLIB=ON") + else() + set(CMAKE_OSX_ARCHITECTURES "x86_64;i386") + endif() + endif() +endif() + +# This helps support the case where emsdk toolchain file is used +# either by setting it with -DCMAKE_TOOLCHAIN_FILE=<path_to_emsdk>/upstream/emscripten/cmake/Modules/Platform/Emscripten.cmake +# or by using "emcmake cmake -B build -S ." as described in https://emscripten.org/docs/compiling/Building-Projects.html +if(EMSCRIPTEN) + SET(PLATFORM Web CACHE STRING "Forcing PLATFORM_WEB because EMSCRIPTEN was detected") +endif() + +# vim: ft=cmake diff --git a/cmake/BuildType.cmake b/cmake/BuildType.cmake deleted file mode 100644 index 80ccdee2..00000000 --- a/cmake/BuildType.cmake +++ /dev/null @@ -1,43 +0,0 @@ -# Set a default build type if none was specified -set(default_build_type "Release") -if(EXISTS "${CMAKE_SOURCE_DIR}/.git") - set(default_build_type "Debug") -endif() - -if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES) - message(STATUS "Setting build type to '${default_build_type}' as none was specified.") - set(CMAKE_BUILD_TYPE "${default_build_type}" CACHE - STRING "Choose the type of build." FORCE) - # Set the possible values of build type for cmake-gui - set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release" - "MinSizeRel" "RelWithDebInfo") -endif() - -# Taken from the https://github.com/OpenChemistry/tomviz project -# Copyright (c) 2014-2017, Kitware, Inc. -# All rights reserved. -# -# Redistribution and use in source and binary forms, with or without -# modification, are permitted provided that the following conditions are met: -# -# 1. Redistributions of source code must retain the above copyright notice, this -# list of conditions and the following disclaimer. -# -# 2. Redistributions in binary form must reproduce the above copyright notice, -# this list of conditions and the following disclaimer in the documentation -# and/or other materials provided with the distribution. -# -# 3. Neither the name of the copyright holder nor the names of its contributors -# may be used to endorse or promote products derived from this software -# without specific prior written permission. -# -# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND -# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED -# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE -# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL -# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR -# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER -# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR -# TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF -# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/cmake/CompilerFlags.cmake b/cmake/CompilerFlags.cmake new file mode 100644 index 00000000..41fee692 --- /dev/null +++ b/cmake/CompilerFlags.cmake @@ -0,0 +1,33 @@ +include(AddIfFlagCompiles) + +add_if_flag_compiles(-Werror=pointer-arith CMAKE_C_FLAGS) +add_if_flag_compiles(-Werror=implicit-function-declaration CMAKE_C_FLAGS) +# src/external/jar_xm.h does shady stuff +add_if_flag_compiles(-fno-strict-aliasing CMAKE_C_FLAGS) + +if (ENABLE_ASAN) + add_if_flag_compiles(-fno-omit-frame-pointer CMAKE_C_FLAGS CMAKE_LINKER_FLAGS) + add_if_flag_compiles(-fsanitize=address CMAKE_C_FLAGS CMAKE_LINKER_FLAGS) +endif() +if (ENABLE_UBSAN) + add_if_flag_compiles(-fno-omit-frame-pointer CMAKE_C_FLAGS CMAKE_LINKER_FLAGS) + add_if_flag_compiles(-fsanitize=undefined CMAKE_C_FLAGS CMAKE_LINKER_FLAGS) +endif() +if (ENABLE_MSAN) + add_if_flag_compiles(-fno-omit-frame-pointer CMAKE_C_FLAGS CMAKE_LINKER_FLAGS) + add_if_flag_compiles(-fsanitize=memory CMAKE_C_FLAGS CMAKE_LINKER_FLAGS) +endif() + +if (ENABLE_MSAN AND ENABLE_ASAN) + MESSAGE(WARNING "Compiling with both AddressSanitizer and MemorySanitizer is not recommended") +endif() + +add_definitions("-DRAYLIB_CMAKE=1") + +if(CMAKE_VERSION VERSION_LESS "3.1") + if(CMAKE_C_COMPILER_ID STREQUAL "GNU") + add_if_flag_compiles(-std=gnu99 CMAKE_C_FLAGS) + endif() +else() + set (CMAKE_C_STANDARD 99) +endif() diff --git a/cmake/GlfwImport.cmake b/cmake/GlfwImport.cmake new file mode 100644 index 00000000..12dbfeb9 --- /dev/null +++ b/cmake/GlfwImport.cmake @@ -0,0 +1,29 @@ + +if(USE_EXTERNAL_GLFW STREQUAL "ON") + find_package(glfw3 3.2.1 REQUIRED) +elseif(USE_EXTERNAL_GLFW STREQUAL "IF_POSSIBLE") + find_package(glfw3 3.2.1 QUIET) +endif() +if (glfw3_FOUND) + set(LIBS_PRIVATE ${LIBS_PRIVATE} glfw) +endif() + +# Explicitly check against "ON", because USE_EXTERNAL_GLFW is a tristate option +# Also adding only on desktop (web also uses glfw but it is more limited and is added using an emcc linker flag) +if(NOT glfw3_FOUND AND NOT USE_EXTERNAL_GLFW STREQUAL "ON" AND "${PLATFORM}" MATCHES "Desktop") + MESSAGE(STATUS "Using raylib's GLFW") + set(GLFW_BUILD_DOCS OFF CACHE BOOL "" FORCE) + set(GLFW_BUILD_TESTS OFF CACHE BOOL "" FORCE) + set(GLFW_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE) + set(GLFW_INSTALL OFF CACHE BOOL "" FORCE) + set(BUILD_SHARED_LIBS OFF CACHE BOOL " " FORCE) + set(GLFW_USE_WAYLAND ${USE_WAYLAND} CACHE BOOL "" FORCE) + + add_subdirectory(external/glfw) + + list(APPEND raylib_sources $<TARGET_OBJECTS:glfw_objlib>) + include_directories(BEFORE SYSTEM external/glfw/include) +else() + MESSAGE(STATUS "Using external GLFW") + set(GLFW_PKG_DEPS glfw3) +endif()
\ No newline at end of file diff --git a/cmake/InstallConfigurations.cmake b/cmake/InstallConfigurations.cmake new file mode 100644 index 00000000..a26fedf5 --- /dev/null +++ b/cmake/InstallConfigurations.cmake @@ -0,0 +1,29 @@ +install( + TARGETS raylib EXPORT raylib-targets + ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}" + LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}" + RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}" + PUBLIC_HEADER DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}" +) + +# PKG_CONFIG_LIBS_PRIVATE is used in raylib.pc.in +if (STATIC) + include(LibraryPathToLinkerFlags) + library_path_to_linker_flags(__PKG_CONFIG_LIBS_PRIVATE "${LIBS_PRIVATE}") + set(PKG_CONFIG_LIBS_PRIVATE ${__PKG_CONFIG_LIBS_PRIVATE} ${GLFW_PKG_LIBS}) + string(REPLACE ";" " " PKG_CONFIG_LIBS_PRIVATE "${PKG_CONFIG_LIBS_PRIVATE}") +elseif (SHARED) + set(PKG_CONFIG_LIBS_EXTRA "") +endif () + +join_paths(libdir_for_pc_file "\${exec_prefix}" "${CMAKE_INSTALL_LIBDIR}") +join_paths(includedir_for_pc_file "\${prefix}" "${CMAKE_INSTALL_INCLUDEDIR}") +configure_file(../raylib.pc.in raylib.pc @ONLY) +configure_file(../cmake/raylib-config-version.cmake raylib-config-version.cmake @ONLY) +install(FILES ${CMAKE_CURRENT_BINARY_DIR}/raylib.pc DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig") +install(FILES ${CMAKE_CURRENT_BINARY_DIR}/raylib-config-version.cmake DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/raylib") +install(FILES ${PROJECT_SOURCE_DIR}/../cmake/raylib-config.cmake DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/raylib") + +# populates raylib_{FOUND, INCLUDE_DIRS, LIBRARIES, LDFLAGS, DEFINITIONS} +include(PopulateConfigVariablesLocally) +populate_config_variables_locally(raylib) diff --git a/cmake/LibraryConfigurations.cmake b/cmake/LibraryConfigurations.cmake new file mode 100644 index 00000000..8cca2464 --- /dev/null +++ b/cmake/LibraryConfigurations.cmake @@ -0,0 +1,109 @@ + +### Config options ### +# Translate the config options to what raylib wants +configure_file(config.h.in ${CMAKE_BINARY_DIR}/cmake/config.h) + +if(${PLATFORM} MATCHES "Desktop") + set(PLATFORM_CPP "PLATFORM_DESKTOP") + + if(APPLE) + # Need to force OpenGL 3.3 on OS X + # See: https://github.com/raysan5/raylib/issues/341 + set(GRAPHICS "GRAPHICS_API_OPENGL_33") + find_library(OPENGL_LIBRARY OpenGL) + set(LIBS_PRIVATE ${OPENGL_LIBRARY}) + link_libraries("${LIBS_PRIVATE}") + if (NOT CMAKE_SYSTEM STRLESS "Darwin-18.0.0") + add_definitions(-DGL_SILENCE_DEPRECATION) + MESSAGE(AUTHOR_WARNING "OpenGL is deprecated starting with macOS 10.14 (Mojave)!") + endif() + elseif(WIN32) + add_definitions(-D_CRT_SECURE_NO_WARNINGS) + set(LIBS_PRIVATE ${LIBS_PRIVATE} winmm) + else() + find_library(pthread NAMES pthread) + find_package(OpenGL QUIET) + if ("${OPENGL_LIBRARIES}" STREQUAL "") + set(OPENGL_LIBRARIES "GL") + endif() + + if ("${CMAKE_SYSTEM_NAME}" MATCHES "(Net|Open)BSD") + find_library(OSS_LIBRARY ossaudio) + endif() + + set(LIBS_PRIVATE m pthread ${OPENGL_LIBRARIES} ${OSS_LIBRARY}) + endif() + +elseif(${PLATFORM} MATCHES "Web") + set(PLATFORM_CPP "PLATFORM_WEB") + set(GRAPHICS "GRAPHICS_API_OPENGL_ES2") + set(CMAKE_C_FLAGS "-s USE_GLFW=3 -s ASSERTIONS=1 --profiling") + set(CMAKE_STATIC_LIBRARY_SUFFIX ".a") + +elseif(${PLATFORM} MATCHES "Android") + set(PLATFORM_CPP "PLATFORM_ANDROID") + set(GRAPHICS "GRAPHICS_API_OPENGL_ES2") + include(AddIfFlagCompiles) + add_if_flag_compiles(-ffunction-sections CMAKE_C_FLAGS) + add_if_flag_compiles(-funwind-tables CMAKE_C_FLAGS) + add_if_flag_compiles(-fstack-protector-strong CMAKE_C_FLAGS) + set(CMAKE_POSITION_INDEPENDENT_CODE ON) + add_if_flag_compiles(-Wa,--noexecstack CMAKE_C_FLAGS) + add_if_flag_compiles(-no-canonical-prefixes CMAKE_C_FLAGS) + add_definitions(-DANDROID -D__ANDROID_API__=21) + include_directories(external/android/native_app_glue) + set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,--exclude-libs,libatomic.a -Wl,--build-id -Wl,-z,noexecstack -Wl,-z,relro -Wl,-z,now -Wl,--warn-shared-textrel -Wl,--fatal-warnings -uANativeActivity_onCreate") + + find_library(OPENGL_LIBRARY OpenGL) + set(LIBS_PRIVATE m log android EGL GLESv2 OpenSLES atomic c) + +elseif(${PLATFORM} MATCHES "Raspberry Pi") + set(PLATFORM_CPP "PLATFORM_RPI") + set(GRAPHICS "GRAPHICS_API_OPENGL_ES2") + + add_definitions(-D_DEFAULT_SOURCE) + + find_library(GLESV2 brcmGLESv2 HINTS /opt/vc/lib) + find_library(EGL brcmEGL HINTS /opt/vc/lib) + find_library(BCMHOST bcm_host HINTS /opt/vc/lib) + include_directories(/opt/vc/include /opt/vc/include/interface/vmcs_host/linux /opt/vc/include/interface/vcos/pthreads) + link_directories(/opt/vc/lib) + set(LIBS_PRIVATE ${GLESV2} ${EGL} ${BCMHOST} pthread rt m dl) + +elseif(${PLATFORM} MATCHES "DRM") + set(PLATFORM_CPP "PLATFORM_DRM") + set(GRAPHICS "GRAPHICS_API_OPENGL_ES2") + + add_definitions(-D_DEFAULT_SOURCE) + add_definitions(-DEGL_NO_X11) + add_definitions(-DPLATFORM_DRM) + + find_library(GLESV2 GLESv2) + find_library(EGL EGL) + find_library(DRM drm) + find_library(GBM gbm) + + include_directories(/usr/include/libdrm) + set(LIBS_PRIVATE ${GLESV2} ${EGL} ${DRM} ${GBM} pthread m dl) + +endif() + +if (${OPENGL_VERSION}) + set(${SUGGESTED_GRAPHICS} "${GRAPHICS}") + if (${OPENGL_VERSION} MATCHES "3.3") + set(GRAPHICS "GRAPHICS_API_OPENGL_33") + elseif (${OPENGL_VERSION} MATCHES "2.1") + set(GRAPHICS "GRAPHICS_API_OPENGL_21") + elseif (${OPENGL_VERSION} MATCHES "1.1") + set(GRAPHICS "GRAPHICS_API_OPENGL_11") + elseif (${OPENGL_VERSION} MATCHES "ES 2.0") + set(GRAPHICS "GRAPHICS_API_OPENGL_ES2") + endif() + if ("${SUGGESTED_GRAPHICS}" AND NOT "${SUGGESTED_GRAPHICS}" STREQUAL "${GRAPHICS}") + message(WARNING "You are overriding the suggested GRAPHICS=${SUGGESTED_GRAPHICS} with ${GRAPHICS}! This may fail") + endif() +endif() + +if(NOT GRAPHICS) + set(GRAPHICS "GRAPHICS_API_OPENGL_33") +endif()
\ No newline at end of file diff --git a/cmake/PackConfigurations.cmake b/cmake/PackConfigurations.cmake new file mode 100644 index 00000000..74eded02 --- /dev/null +++ b/cmake/PackConfigurations.cmake @@ -0,0 +1,13 @@ +# Packaging +SET(CPACK_PACKAGE_NAME "raylib") +SET(CPACK_PACKAGE_DESCRIPTION_SUMMARY "Simple and easy-to-use library to enjoy videogames programming") +SET(CPACK_PACKAGE_VERSION "${PROJECT_VERSION}") +SET(CPACK_PACKAGE_VERSION_MAJOR "${PROJECT_VERSION_MAJOR}") +SET(CPACK_PACKAGE_VERSION_MINOR "${PROJECT_VERSION_MINOR}") +SET(CPACK_PACKAGE_VERSION_PATCH "${PROJECT_VERSION_PATCH}") +SET(CPACK_PACKAGE_DESCRIPTION_FILE "${PROJECT_SOURCE_DIR}/../README.md") +SET(CPACK_RESOURCE_FILE_WELCOME "${PROJECT_SOURCE_DIR}/../README.md") +SET(CPACK_RESOURCE_FILE_LICENSE "${PROJECT_SOURCE_DIR}/../LICENSE") +SET(CPACK_PACKAGE_FILE_NAME "raylib-${PROJECT_VERSION}$ENV{RAYLIB_PACKAGE_SUFFIX}") +SET(CPACK_GENERATOR "ZIP;TGZ") # Remove this, if you want the NSIS installer on Windows +include(CPack)
\ No newline at end of file diff --git a/cmake/emscripten.cmake b/cmake/emscripten.cmake deleted file mode 100644 index 1780dee9..00000000 --- a/cmake/emscripten.cmake +++ /dev/null @@ -1,23 +0,0 @@ -SET(CMAKE_SYSTEM_NAME Linux) -SET(CMAKE_SYSTEM_PROCESSOR x86) - -if (CMAKE_HOST_WIN32) - SET(EMSCRIPTEN_EXTENSION ".bat") -else () - SET(EMSCRIPTEN_EXTENSION "") -endif() - -SET(CMAKE_C_COMPILER emcc${EMSCRIPTEN_EXTENSION}) -SET(CMAKE_CXX_COMPILER em++${EMSCRIPTEN_EXTENSION}) - -if(NOT DEFINED CMAKE_AR) - find_program(CMAKE_AR NAMES emar${EMSCRIPTEN_EXTENSION}) -endif() -if(NOT DEFINED CMAKE_RANLIB) - find_program(CMAKE_RANLIB NAMES emranlib${EMSCRIPTEN_EXTENSION}) -endif() - -set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER) -set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY) -set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE BOTH) -set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY) |
