ref: ca5b0928075a22693d6bf07e7a5dbab41e3ca286
parent: 6be5aac70d20bbd752a310b1dc14f44715c3a3f7
author: Clownacy <[email protected]>
date: Wed Apr 1 16:14:44 EDT 2020
Add static-linkage support to CMake file Well, kind of. It uses pkg-config and GCC's `-static' flag. It's very tied the Linux way of doing things.
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -18,8 +18,9 @@
set(BACKEND_RENDERER "SDLTexture" CACHE STRING "Which renderer the game should use: 'OpenGL3' for an OpenGL 3.2 renderer, 'OpenGLES2' for an OpenGL ES 2.0 renderer, 'SDLTexture' for SDL2's hardware-accelerated Texture API, 'SDLSurface' for SDL2's software-rendered Surface API, or 'Software' for a handwritten software renderer")
set(BACKEND_AUDIO "SDL2" CACHE STRING "Which audio backend the game should use: 'SDL2' or 'miniaudio'")
+option(STATIC_LINKAGE "On platforms with pkg-config, static-link the dependencies" OFF)
option(LTO "Enable link-time optimisation" OFF)
-option(MSVC_LINK_STATIC_RUNTIME "Link the static MSVC runtime library" OFF)
+option(MSVC_LINK_STATIC_RUNTIME "Link the static MSVC runtime library (Visual Studio only)" OFF)
option(FORCE_LOCAL_LIBS "Compile the built-in versions of SDL2, FreeType, and FLTK instead of using the system-provided ones" OFF)
@@ -258,6 +259,10 @@
target_compile_definitions(CSE2 PRIVATE DEBUG_SAVE)
endif()
+if(STATIC_LINKAGE)
+ target_link_options(CSE2 PRIVATE "-static")
+endif()
+
if(LTO)
include(CheckIPOSupported)
@@ -372,10 +377,29 @@
################
if(NOT FORCE_LOCAL_LIBS)
+ find_package(PkgConfig QUIET)
+endif()
+
+if(NOT FORCE_LOCAL_LIBS)
find_package(SDL2)
+
+ if (PKG_CONFIG_FOUND)
+ pkg_check_modules(sdl2 QUIET sdl2)
+ endif()
endif()
-if(TARGET SDL2::SDL2)
+if(sdl2_FOUND)
+ # pkg-config
+ if (STATIC_LINKAGE)
+ message(STATUS "Using system SDL2 (pkg-config, static)")
+ target_compile_options(CSE2 PRIVATE ${sdl2_STATIC_CFLAGS})
+ target_link_libraries(CSE2 PRIVATE ${sdl2_STATIC_LIBRARIES})
+ else()
+ message(STATUS "Using system SDL2 (pkg-config, dynamic)")
+ target_compile_options(CSE2 PRIVATE ${sdl2_CFLAGS})
+ target_link_libraries(CSE2 PRIVATE ${sdl2_LIBRARIES})
+ endif()
+elseif(TARGET SDL2::SDL2)
# CMake-generated config (Arch, vcpkg, Raspbian)
message(STATUS "Using system SDL2 (CMake, dynamic)")
target_link_libraries(CSE2 PRIVATE SDL2::SDL2 SDL2::SDL2main)
@@ -401,10 +425,25 @@
if(NOT FORCE_LOCAL_LIBS)
find_package(Freetype)
+
+ if (PKG_CONFIG_FOUND)
+ pkg_check_modules(freetype2 QUIET freetype2)
+ endif()
endif()
-if(FREETYPE_FOUND)
- message(STATUS "Using system FreeType")
+if(freetype2_FOUND)
+ # pkg-config
+ if (STATIC_LINKAGE)
+ message(STATUS "Using system FreeType (pkg-config, static)")
+ target_compile_options(CSE2 PRIVATE ${freetype2_STATIC_CFLAGS})
+ target_link_libraries(CSE2 PRIVATE ${freetype2_STATIC_LIBRARIES})
+ else()
+ message(STATUS "Using system FreeType (pkg-config, dynamic)")
+ target_compile_options(CSE2 PRIVATE ${freetype2_CFLAGS})
+ target_link_libraries(CSE2 PRIVATE ${freetype2_LIBRARIES})
+ endif()
+elseif(FREETYPE_FOUND)
+ message(STATUS "Using system FreeType (CMake)")
target_include_directories(CSE2 PRIVATE ${FREETYPE_INCLUDE_DIRS})
target_link_libraries(CSE2 PRIVATE ${FREETYPE_LIBRARIES})
else()