From ca5b0928075a22693d6bf07e7a5dbab41e3ca286 Mon Sep 17 00:00:00 2001 From: Clownacy Date: Wed, 1 Apr 2020 20:14:44 +0100 Subject: [PATCH] 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. --- CMakeLists.txt | 49 ++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 44 insertions(+), 5 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index dbeb2665..5337ff22 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -18,8 +18,9 @@ option(DEBUG_SAVE "Re-enable the ability to drag-and-drop save files onto the wi 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 @@ if(DEBUG_SAVE) 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 @@ set_target_properties(CSE2 PROPERTIES ################ if(NOT FORCE_LOCAL_LIBS) - find_package(SDL2) + find_package(PkgConfig QUIET) endif() -if(TARGET SDL2::SDL2) +if(NOT FORCE_LOCAL_LIBS) + find_package(SDL2) + + if (PKG_CONFIG_FOUND) + pkg_check_modules(sdl2 QUIET sdl2) + endif() +endif() + +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 @@ endif() 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()