Skip to content

Commit

Permalink
Use pkg-config to help finding mostly static libraries
Browse files Browse the repository at this point in the history
The FindSDL2*.cmake modules create the *::*-static imported targets
like VCPKG does. This way we can reuse the same names and reduce the
complexity in the main and src CMakeLists.txt.
  • Loading branch information
alef committed Jun 12, 2023
1 parent 98baf56 commit bdd1c57
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 88 deletions.
65 changes: 22 additions & 43 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ include(CheckCXXCompilerFlag)
#SET(CMAKE_SHARED_LIBRARY_C_FLAGS "${CMAKE_SHARED_LIBRARY_C_FLAGS} -m32")
#SET(CMAKE_SHARED_LIBRARY_CXX_FLAGS "${CMAKE_SHARED_LIBRARY_CXX_FLAGS} -m32")

find_package(PkgConfig)
if (NOT DYNAMIC_LINKING)
if(NOT MSVC)
set(CMAKE_FIND_LIBRARY_SUFFIXES ".a;.dll.a")
Expand Down Expand Up @@ -258,31 +259,26 @@ find_package(ZLIB REQUIRED)
if (TILES)
# Find SDL, SDL_ttf & SDL_image for graphical install
message(STATUS "Searching for SDL2 library --")
find_package(SDL2)
if (NOT (SDL2_FOUND OR TARGET SDL2::SDL2))
if (NOT CMAKE_FIND_PACKAGE_PREFER_CONFIG)
set(CMAKE_FIND_PACKAGE_PREFER_CONFIG ON) # Use sdl2-config.cmake provided by the system or VCPKG
find_package(SDL2)
if(NOT SDL2_FOUND)
# Use our CMakeModules/Find/FindSDL2.cmake
set(CMAKE_FIND_PACKAGE_PREFER_CONFIG OFF)
find_package(SDL2)
endif()
set(CMAKE_FIND_PACKAGE_PREFER_CONFIG OFF)
endif()
if (NOT (SDL2_FOUND OR TARGET SDL2::SDL2 OR TARGET SDL2::SDL2-static))
message(FATAL_ERROR
"This project requires SDL2 to be installed to compile in graphical mode. \
Please install the SDL2 development libraries, \
or try compiling without -DTILES=1 for a text-only compilation. \
See doc/COMPILING/COMPILING-CMAKE.md for details and more info.")
endif ()

if (NOT DYNAMIC_LINKING)
# SDL, SDL_Image, SDL_ttf deps are required for static build
message(STATUS "Searching for SDL deps libraries --")
find_package(Freetype REQUIRED)
find_package(PNG REQUIRED)
find_package(JPEG REQUIRED)
find_package(ZLIB REQUIRED)
find_package(BZip2 REQUIRED)
endif ()

message(STATUS "Searching for SDL2_TTF library --")
if (NOT VCPKG_MANIFEST_MODE)
find_package(SDL2_ttf)
else()
find_package(SDL2_ttf)
endif()
find_package(SDL2_ttf)
if (NOT (SDL2_TTF_FOUND OR TARGET SDL2::SDL2_ttf OR TARGET SDL2_ttf::SDL2_ttf-static))
message(FATAL_ERROR
"This project requires SDL2_ttf to be installed to compile in graphical mode. \
Expand All @@ -292,12 +288,8 @@ if (TILES)
endif ()

message(STATUS "Searching for SDL2_image library --")
if(VCPKG_MANIFEST_MODE)
find_package(SDL2_image)
else()
find_package(SDL2_image)
endif()
if (NOT (SDL2_IMAGE_FOUND OR TARGET SDL2_image::SDL2_image-static))
find_package(SDL2_image)
if (NOT (SDL2_IMAGE_FOUND OR TARGET SDL2::SDL2_image OR TARGET SDL2_image::SDL2_image-static))
message(FATAL_ERROR
"This project requires SDL2_image to be installed to compile in graphical mode. \
Please install the SDL2_image development libraries, \
Expand Down Expand Up @@ -335,26 +327,13 @@ if (SOUND)
endif ()

# Sound requires SDL_mixer library
if(VCPKG_MANIFEST_MODE)
message(STATUS "Searching for sdl2-mixer library --")
find_package(Ogg REQUIRED)
find_package(SDL2_mixer)
if (NOT (TARGET SDL2_mixer::SDL2_mixer OR TARGET SDL2_mixer::SDL2_mixer-static))
message(FATAL_ERROR
"You need the sdl2-mixer development library \
to be able to compile with sound enabled. \
See doc/COMPILING/COMPILING-CMAKE.md for details and more info.")
endif()
else()
message(STATUS "Searching for SDL2_mixer library --")
find_package(SDL2_mixer)
# if (NOT TARGET SDL2_mixer::SDL2_mixer-static) # TODO x64-mingw-static ?
if (NOT SDL2_MIXER_FOUND)
message(FATAL_ERROR
"You need the SDL2_mixer development library \
to be able to compile with sound enabled. \
See doc/COMPILING/COMPILING-CMAKE.md for details and more info.")
endif()
message(STATUS "Searching for SDL2_mixer library --")
find_package(SDL2_mixer)
if (NOT (SDL2_MIXER_FOUND OR TARGET SDL2_mixer::SDL2_mixer OR TARGET SDL2_mixer::SDL2_mixer-static))
message(FATAL_ERROR
"You need the SDL2_mixer development library \
to be able to compile with sound enabled. \
See doc/COMPILING/COMPILING-CMAKE.md for details and more info.")
endif()
endif ()

Expand Down
35 changes: 34 additions & 1 deletion CMakeModules/Find/FindSDL2_image.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,37 @@ set(SDL2IMAGE_LIBRARY ${SDL2_IMAGE_LIBRARIES})
set(SDL2IMAGE_INCLUDE_DIR ${SDL2_IMAGE_INCLUDE_DIRS})
set(SDL2IMAGE_FOUND ${SDL2_IMAGE_FOUND})

mark_as_advanced(SDL2_IMAGE_LIBRARY SDL2_IMAGE_INCLUDE_DIR)
mark_as_advanced(SDL2_IMAGE_LIBRARY SDL2_IMAGE_INCLUDE_DIR)

if(NOT DYNAMIC_LINKING AND PKGCONFIG_FOUND)
if (NOT TARGET SDL2_image:SDL2_image-static)
add_library(SDL2_image::SDL2_image-static STATIC IMPORTED)
set_property(TARGET SDL2_image::SDL2_image-static
PROPERTY IMPORTED_LOCATION ${SDL2_IMAGE_LIBRARY}
)
endif()
message(STATUS "Searching for SDL_images deps libraries --")
find_package(JPEG REQUIRED)
find_package(PNG REQUIRED)
find_package(TIFF REQUIRED)
find_library(JBIG jbig REQUIRED)
find_package(LibLZMA REQUIRED)
target_link_libraries(SDL2_image::SDL2_image-static INTERFACE
JPEG::JPEG
PNG::PNG
TIFF::TIFF
${JBIG}
LibLZMA::LibLZMA
${ZSTD}
)
pkg_check_modules(WEBP REQUIRED IMPORTED_TARGET libwebp)
pkg_check_modules(ZIP REQUIRED IMPORTED_TARGET libzip)
pkg_check_modules(ZSTD REQUIRED IMPORTED_TARGET libzstd)
pkg_check_modules(DEFLATE REQUIRED IMPORTED_TARGET libdeflate)
target_link_libraries(SDL2_image::SDL2_image-static INTERFACE
PkgConfig::WEBP
PkgConfig::ZIP
PkgConfig::ZSTD
PkgConfig::DEFLATE
)
endif()
16 changes: 15 additions & 1 deletion CMakeModules/Find/FindSDL2_mixer.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,18 @@ set(SDL2MIXER_LIBRARY ${SDL2_MIXER_LIBRARIES})
set(SDL2MIXER_INCLUDE_DIR ${SDL2_MIXER_INCLUDE_DIRS})
set(SDL2MIXER_FOUND ${SDL2_MIXER_FOUND})

mark_as_advanced(SDL2_MIXER_LIBRARY SDL2_MIXER_INCLUDE_DIR)
mark_as_advanced(SDL2_MIXER_LIBRARY SDL2_MIXER_INCLUDE_DIR)

if(NOT DYNAMIC_LINKING AND PKGCONFIG_FOUND)
if (NOT TARGET SDL2_mixer::SDL2_mixer-static)
add_library(SDL2_mixer::SDL2_mixer-static STATIC IMPORTED)
set_property(TARGET SDL2_mixer::SDL2_mixer-static
PROPERTY IMPORTED_LOCATION ${SDL2_MIXER_LIBRARY}
)
endif()
message(STATUS "Searching for SDL_mixer deps libraries --")
pkg_check_modules(FLAC REQUIRED IMPORTED_TARGET flac)
target_link_libraries(SDL2_mixer::SDL2_mixer-static INTERFACE
PkgConfig::FLAC
)
endif()
22 changes: 21 additions & 1 deletion CMakeModules/Find/FindSDL2_ttf.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,24 @@ set(SDL2TTF_LIBRARY ${SDL2_TTF_LIBRARIES})
set(SDL2TTF_INCLUDE_DIR ${SDL2_TTF_INCLUDE_DIRS})
set(SDL2TTF_FOUND ${SDL2_TTF_FOUND})

mark_as_advanced(SDL2_TTF_LIBRARY SDL2_TTF_INCLUDE_DIR)
mark_as_advanced(SDL2_TTF_LIBRARY SDL2_TTF_INCLUDE_DIR)

if (NOT DYNAMIC_LINKING AND PKGCONFIG_FOUND)
if (NOT TARGET SDL2_ttf::SDL2_ttf-static)
add_library(SDL2_ttf::SDL2_ttf-static STATIC IMPORTED)
set_property(TARGET SDL2_ttf::SDL2_ttf-static
PROPERTY IMPORTED_LOCATION ${SDL2_TTF_LIBRARY}
)
endif()
message(STATUS "Searching for SDL_ttf deps libraries --")
find_package(Freetype REQUIRED)
find_package(Harfbuzz REQUIRED)
target_link_libraries(SDL2_ttf::SDL2_ttf-static INTERFACE
Freetype::Freetype
harfbuzz::harfbuzz
)
pkg_check_modules(BROTLI REQUIRED IMPORTED_TARGET libbrotlidec libbrotlicommon)
target_link_libraries(Freetype::Freetype INTERFACE
PkgConfig::BROTLI
)
endif()
59 changes: 17 additions & 42 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -83,51 +83,26 @@ if (TILES)
endif ()

if (NOT DYNAMIC_LINKING)
# SDL, SDL_Image, SDL_ttf deps are required for static build
target_include_directories(cataclysm-tiles-common PUBLIC
${FREETYPE_INCLUDE_DIRS}
${PNG_INCLUDE_DIRS}
${JPEG_INCLUDE_DIR}
${BZIP2_INCLUDE_DIR})
target_link_libraries(cataclysm-tiles-common PUBLIC
${FREETYPE_LIBRARIES}
${PNG_LIBRARIES}
${JPEG_LIBRARIES}
${BZIP2_LIBRARIES})
target_link_libraries(cataclysm-tiles-common PUBLIC SDL2::SDL2-static)
endif ()
target_include_directories(cataclysm-tiles-common PUBLIC
${SDL2_INCLUDE_DIR}
${SDL2_IMAGE_INCLUDE_DIRS}
${SDL2_TTF_INCLUDE_DIRS}
${ZLIB_INCLUDE_DIRS})
target_link_libraries(cataclysm-tiles-common PUBLIC
${SDL2_LIBRARY}
${SDL2_IMAGE_LIBRARIES}
${SDL2_TTF_LIBRARIES}
${ZLIB_LIBRARIES})
if(TARGET SDL2::SDL2)
target_link_libraries(cataclysm-tiles-common PUBLIC SDL2::SDL2
SDL2::SDL2main
target_link_libraries(cataclysm-tiles-common PUBLIC
SDL2::SDL2-static
SDL2_image::SDL2_image-static
SDL2_ttf::SDL2_ttf-static)
endif()
SDL2_ttf::SDL2_ttf-static
)
else()
target_link_libraries(cataclysm-tiles-common PUBLIC
SDL2::SDL2
SDL2_image::SDL2_image
SDL2_ttf::SDL2_ttf
)
endif ()
if (SOUND)
if (VCPKG_MANIFEST_MODE)
find_package(Ogg REQUIRED)
# SDL2_mixer::SDL2_mixer-static = TODO x64-mingw-static ?
find_package(Vorbis REQUIRED)
target_link_libraries(cataclysm-tiles-common PUBLIC SDL2_mixer::SDL2_mixer-static
Ogg::ogg
Vorbis::vorbis)
if (NOT DYNAMIC_LINKING)
target_link_libraries(cataclysm-tiles-common PUBLIC
SDL2_mixer::SDL2_mixer-static
)
else()
# TODO
endif()
target_compile_definitions(cataclysm-tiles-common PUBLIC SDL_SOUND )
target_include_directories(cataclysm-tiles-common PUBLIC ${OGGVORBIS_INCLUDE_DIR})
target_link_libraries(cataclysm-tiles-common PUBLIC ${OGG_LIBRARY})
target_link_libraries(cataclysm-tiles-common PUBLIC ${VORBIS_LIBRARY})
target_link_libraries(cataclysm-tiles-common PUBLIC ${VORBISFILE_LIBRARY})
target_include_directories(cataclysm-tiles-common PUBLIC ${SDL2_MIXER_INCLUDE_DIRS})
target_link_libraries(cataclysm-tiles-common PUBLIC ${SDL2_MIXER_LIBRARIES})
endif ()

if (WIN32)
Expand Down

0 comments on commit bdd1c57

Please sign in to comment.