cmake: remove SDL_STATIC_PIC cmake option.

Use CMAKE_POSITION_INDEPENDENT_CODE instead
This commit is contained in:
Anonymous Maarten 2024-06-10 16:39:30 +02:00
parent 4ce4fc575a
commit dd0bb25f66
6 changed files with 16 additions and 15 deletions

View File

@ -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 \

View File

@ -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 }}

View File

@ -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})

View File

@ -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",

View File

@ -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)

View File

@ -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).