diff --git a/.github/workflows/android.yml b/.github/workflows/android.yml index bc6ce3fa9..037121c18 100644 --- a/.github/workflows/android.yml +++ b/.github/workflows/android.yml @@ -70,7 +70,7 @@ jobs: -DANDROID_ABI=${{ matrix.platform.android_abi }} \ -DSDL_SHARED=ON \ -DSDL_STATIC=ON \ - -DSDL_STATIC_PIC=ON \ + -DCMAKE_POSITION_INDEPENDENT_CODE=ON \ -DSDL_VENDOR_INFO="Github Workflow" \ -DCMAKE_INSTALL_PREFIX=prefix \ -DCMAKE_BUILD_TYPE=Release \ diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 46cc370a8..758dbc527 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -111,6 +111,7 @@ jobs: -DSDL_CLANG_TIDY=ON \ -DSDL_DISABLE_INSTALL_DOCS=${{ !matrix.platform.no-perl }} \ -DCMAKE_INSTALL_PREFIX=cmake_prefix \ + -DCMAKE_POSITION_INDEPENDENT_CODE=ON \ -DCMAKE_BUILD_TYPE=Release \ ${{ matrix.platform.cmake-platform }} \ ${{ matrix.platform.cmake }} diff --git a/CMakeLists.txt b/CMakeLists.txt index 35d95ce0e..6a31ed54f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -367,7 +367,6 @@ cmake_dependent_option(SDL_SHARED "Build a shared version of the library" ${SDL_ cmake_dependent_option(SDL_STATIC "Build a static version of the library" ${SDL_STATIC_DEFAULT} ${SDL_STATIC_AVAILABLE} OFF) option(SDL_TEST_LIBRARY "Build the SDL3_test library" ON) -dep_option(SDL_STATIC_PIC "Static version of the library should be built with Position Independent Code" "${CMAKE_POSITION_INDEPENDENT_CODE}" "SDL_STATIC" OFF) dep_option(SDL_TESTS "Build the test directory" OFF SDL_TEST_LIBRARY OFF) dep_option(SDL_INSTALL_TESTS "Install test-cases" OFF "NOT SDL_DISABLE_INSTALL;NOT SDL_FRAMEWORK;NOT WINDOWS_STORE" OFF) dep_option(SDL_TESTS_LINK_SHARED "link tests to shared SDL library" "${SDL_SHARED}" "SDL_SHARED;SDL_STATIC" "${SDL_SHARED}") @@ -378,8 +377,6 @@ if(VITA) set_option(VIDEO_VITA_PVR "Build with PSVita PVR gles/gles2 support" OFF) endif() -set(HAVE_STATIC_PIC "${SDL_STATIC_PIC}") - if(NOT (SDL_SHARED OR SDL_STATIC)) message(FATAL_ERROR "SDL_SHARED and SDL_STATIC cannot both be disabled") endif() @@ -3334,7 +3331,6 @@ endif() if(SDL_STATIC) set_target_properties(SDL3-static PROPERTIES OUTPUT_NAME "${sdl_static_libname}" - POSITION_INDEPENDENT_CODE "${SDL_STATIC_PIC}" ) target_compile_definitions(SDL3-static PRIVATE SDL_STATIC_LIB) target_link_libraries(SDL3-static PRIVATE ${SDL_CMAKE_DEPENDS}) diff --git a/build-scripts/build-release.py b/build-scripts/build-release.py index 4f8983954..42863c209 100755 --- a/build-scripts/build-release.py +++ b/build-scripts/build-release.py @@ -630,10 +630,9 @@ class Releaser: f"-DCMAKE_TOOLCHAIN_FILE={cmake_toolchain_file}", f"-DANDROID_PLATFORM={android_api}", f"-DANDROID_ABI={android_abi}", - f"-DCMAKE_POSITION_INDEPENDENT_CODE=ON", + "-DCMAKE_POSITION_INDEPENDENT_CODE=ON", "-DSDL_SHARED=ON", "-DSDL_STATIC=ON", - "-DSDL_STATIC_PIC=ON", "-DSDL_TEST_LIBRARY=ON", "-DSDL_DISABLE_ANDROID_JAR=OFF", "-DSDL_TESTS=OFF", diff --git a/cmake/test/CMakeLists.txt b/cmake/test/CMakeLists.txt index f25936d57..caf99e15a 100644 --- a/cmake/test/CMakeLists.txt +++ b/cmake/test/CMakeLists.txt @@ -81,14 +81,12 @@ if(TEST_STATIC) add_executable(gui-static WIN32 main_gui.c) target_link_libraries(gui-static PRIVATE SDL3::SDL3-static) - option(SDL_STATIC_PIC "SDL static library has been built with PIC") - if(SDL_STATIC_PIC OR WIN32) - add_library(sharedlib-static SHARED main_lib.c) - target_link_libraries(sharedlib-static PRIVATE SDL3::SDL3-static) - generate_export_header(sharedlib-static EXPORT_MACRO_NAME MYLIBRARY_EXPORT) - target_compile_definitions(sharedlib-static PRIVATE "EXPORT_HEADER=\"${CMAKE_CURRENT_BINARY_DIR}/sharedlib-static_export.h\"") - set_target_properties(sharedlib-static PROPERTIES C_VISIBILITY_PRESET "hidden") - endif() + # Assume SDL library has been built with `set(CMAKE_POSITION_INDEPENDENT_CODE ON)` + add_library(sharedlib-static SHARED main_lib.c) + target_link_libraries(sharedlib-static PRIVATE SDL3::SDL3-static) + generate_export_header(sharedlib-static EXPORT_MACRO_NAME MYLIBRARY_EXPORT) + target_compile_definitions(sharedlib-static PRIVATE "EXPORT_HEADER=\"${CMAKE_CURRENT_BINARY_DIR}/sharedlib-static_export.h\"") + set_target_properties(sharedlib-static PROPERTIES C_VISIBILITY_PRESET "hidden") if(TEST_TEST) add_executable(sdltest-static sdltest.c) diff --git a/docs/README-cmake.md b/docs/README-cmake.md index 2b96eb2e4..eb65c0ca7 100644 --- a/docs/README-cmake.md +++ b/docs/README-cmake.md @@ -306,6 +306,13 @@ if(WIN32) endif() ``` +### Linking against a static SDL library fails due to relocation errors + +On unix platforms, all code that ends up in shared libraries needs to be built as relocatable (=position independent) code. +However, by default CMake builds static libraries as non-relocatable. +Configuring SDL with `-DCMAKE_POSITION_INDEPENDENT_CODE=ON` will result in a static `libSDL3.a` library +which you can link against to create a shared library. + ## Help, it doesn't work! Below, a SDL3 CMake project can be found that builds 99.9% of time (assuming you have internet connectivity).