Skip to content

Commit

Permalink
Support building CSFML as static libraries
Browse files Browse the repository at this point in the history
Some languages can make use of C foreign function interfaces
compiled as static libraries so it's a good idea for CSFML to
support this use case.
  • Loading branch information
ChrisThrasher committed Sep 7, 2024
1 parent e963079 commit 055e695
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 7 deletions.
9 changes: 6 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ defaults:

jobs:
build:
name: ${{ matrix.platform.name }} ${{ matrix.type.name }}
name: ${{ matrix.platform.name }} ${{ matrix.config.name }} ${{ matrix.type.name }}
runs-on: ${{ matrix.platform.os }}

strategy:
Expand All @@ -28,6 +28,9 @@ jobs:
- { name: Linux GCC, os: ubuntu-22.04, flags: -GNinja }
- { name: Linux Clang, os: ubuntu-22.04, flags: -GNinja -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ }
- { name: MacOS, os: macos-14, flags: -GNinja }
config:
- { name: Shared, flags: -DBUILD_SHARED_LIBS=ON, csfml_flags: -DCSFML_LINK_SFML_STATICALLY=OFF }
- { name: Static, flags: -DBUILD_SHARED_LIBS=OFF, csfml_flags: -DCSFML_LINK_SFML_STATICALLY=ON }
type:
- { name: Debug }
- { name: Release }
Expand All @@ -50,7 +53,7 @@ jobs:
path: SFML

- name: Configure SFML CMake
run: cmake -S SFML -B SFML/build -DCMAKE_INSTALL_PREFIX=SFML/install -DBUILD_SHARED_LIBS=TRUE ${{matrix.platform.flags}} -DCMAKE_BUILD_TYPE=${{matrix.type.name}}
run: cmake -S SFML -B SFML/build -DCMAKE_INSTALL_PREFIX=SFML/install ${{matrix.platform.flags}} ${{matrix.config.flags}} -DCMAKE_BUILD_TYPE=${{matrix.type.name}}

- name: Build SFML
run: cmake --build SFML/build --config ${{matrix.type.name}} --target install
Expand All @@ -61,7 +64,7 @@ jobs:
path: CSFML

- name: Configure CSFML CMake
run: cmake --preset dev -S CSFML -B CSFML/build -DCMAKE_INSTALL_PREFIX=CSFML/install -DCSFML_LINK_SFML_STATICALLY=FALSE -DSFML_ROOT=SFML/install -DCMAKE_VERBOSE_MAKEFILE=ON ${{matrix.platform.flags}} -DCMAKE_BUILD_TYPE=${{matrix.type.name}}
run: cmake --preset dev -S CSFML -B CSFML/build -DCMAKE_INSTALL_PREFIX=CSFML/install -DSFML_ROOT=SFML/install -DCMAKE_VERBOSE_MAKEFILE=ON ${{matrix.platform.flags}} ${{matrix.config.flags}} ${{matrix.config.csfml_flags}} -DCMAKE_BUILD_TYPE=${{matrix.type.name}}

- name: Build CSFML
run: cmake --build CSFML/build --config ${{matrix.type.name}} --target install
Expand Down
18 changes: 14 additions & 4 deletions cmake/Macros.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,17 @@ macro(csfml_add_library target)
string(TOUPPER "${NAME_UPPER}" NAME_UPPER)
set_target_properties(${target} PROPERTIES DEFINE_SYMBOL ${NAME_UPPER}_EXPORTS)

if(SFML_OS_WINDOWS)
# include the major version number in Windows shared library names (but not import library names)
if(BUILD_SHARED_LIBS)
set_target_properties(${target} PROPERTIES DEBUG_POSTFIX -d)
set_target_properties(${target} PROPERTIES SUFFIX "-${PROJECT_VERSION_MAJOR}${CMAKE_SHARED_LIBRARY_SUFFIX}")
if(SFML_OS_WINDOWS)
# include the major version number in Windows shared library names (but not import library names)
set_target_properties(${target} PROPERTIES SUFFIX "-${PROJECT_VERSION_MAJOR}${CMAKE_SHARED_LIBRARY_SUFFIX}")
endif()
else()
set_target_properties(${target} PROPERTIES DEBUG_POSTFIX -d)
set_target_properties(${target} PROPERTIES DEBUG_POSTFIX -s-d)
set_target_properties(${target} PROPERTIES RELEASE_POSTFIX -s)
set_target_properties(${target} PROPERTIES MINSIZEREL_POSTFIX -s)
set_target_properties(${target} PROPERTIES RELWITHDEBINFO_POSTFIX -s)
endif()
if (SFML_OS_WINDOWS AND SFML_COMPILER_GCC)
# on Windows/gcc get rid of "lib" prefix for shared libraries,
Expand Down Expand Up @@ -75,4 +80,9 @@ macro(csfml_add_library target)
LIBRARY DESTINATION lib${LIB_SUFFIX} COMPONENT bin
ARCHIVE DESTINATION lib${LIB_SUFFIX} COMPONENT devel)

# define CSFML_STATIC if the build type is not set to 'shared'
if(NOT BUILD_SHARED_LIBS)
target_compile_definitions(${target} PUBLIC CSFML_STATIC)
endif()

endmacro()
10 changes: 10 additions & 0 deletions include/CSFML/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@
////////////////////////////////////////////////////////////
// Define helpers to create portable import / export macros for each module
////////////////////////////////////////////////////////////
#if !defined(CSFML_STATIC)

#if defined(CSFML_SYSTEM_WINDOWS)

// Windows compilers need specific (and different) keywords for export and import
Expand All @@ -104,6 +106,14 @@

#endif

#else

// Static build doesn't need import/export macros
#define CSFML_API_EXPORT extern "C"
#define CSFML_API_IMPORT CSFML_EXTERN_C

#endif

////////////////////////////////////////////////////////////
// Cross-platform warning for deprecated functions and classes
//
Expand Down

0 comments on commit 055e695

Please sign in to comment.