Skip to content

Commit

Permalink
Refactor add_package to a macro, do aliases, move to separate file
Browse files Browse the repository at this point in the history
  • Loading branch information
Swagtoy committed Oct 31, 2024
1 parent 67395f5 commit 9866b82
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 53 deletions.
75 changes: 22 additions & 53 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -140,51 +140,20 @@ if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
endif()

## Add lots of dependencies to compiler switches

# TODO Move this to its own file
find_package(PkgConfig)
function(add_package pkg_target)
cmake_parse_arguments(PARSE_ARGV 1 add_package
"CONFIG;REQUIRED" "PROVIDES;PKG" "PKG_CONFIG"
)

set(fp_args)
# Note: We don't pass "REQUIRED" here because we choose to fallback if it doesn't exist.
# Later, however, we do choose to throw an error based on this flag.
if (${add_package_CONFIG})
list(APPEND fp_args CONFIG)
endif()

find_package(${add_package_PKG} ${fp_args})

if (NOT ${add_package_PKG}_FOUND)
message(STATUS "Package \"${add_package_PKG}\" doesn't exist, so falling back to PkgConfig")

if (PkgConfig_FOUND)
set(pkg_config_args)
if (${add_package_REQUIRED})
list(APPEND pkg_config_args REQUIRED)
endif()
pkg_check_modules(${pkg_target} ${pkg_config_args} ${add_package_PKG_CONFIG})
elseif(add_package_REQUIRED)
# TODO look in Provides
message(FATAL_ERROR "Package \"${pkg_target}\" couldn't be found. I don't know what to do.")
endif()
endif()
endfunction(add_package)
include(SuperTux/AddPackage)

# Find dependencies
add_package(SDL2::SDL2 PKG SDL2 CONFIG REQUIRED PKG_CONFIG sdl2)
#find_package(PNG CONFIG REQUIRED)
add_package(SDL2::SDL2_image PKG SDL2_image CONFIG REQUIRED PKG_CONFIG sdl2_image)
add_package(SDL2::SDL2_ttf PKG SDL2_ttf CONFIG REQUIRED PKG_CONFIG sdl2_ttf)
add_package(CURL PKG CURL PKG_CONFIG libcurl)
add_package(OpenAL PKG OpenAL CONFIG REQUIRED PKG_CONFIG OpenAL)
add_package(Ogg PKG Ogg CONFIG REQUIRED PKG_CONFIG ogg)
add_package(Vorbis PKG Vorbis CONFIG REQUIRED PKG_CONFIG vorbis vorbisfile)
add_package(glm PKG glm CONFIG REQUIRED PKG_CONFIG glm)
add_package(fmt PKG fmt CONFIG REQUIRED PKG_CONFIG fmt)
add_package(PhysFS PKG PhysFS CONFIG REQUIRED PKG_CONFIG PhysFS PROVIDES ProvidePhysfs)
add_package(TARGET SDL2 PKG SDL2 PKG_USE SDL2::SDL2 CONFIG REQUIRED PKG_CONFIG sdl2)
add_package(TARGET SDL2_image PKG SDL2_image PKG_USE SDL2_image::SDL2_image CONFIG REQUIRED PKG_CONFIG sdl2_image)
add_package(TARGET SDL2_ttf PKG SDL2_ttf PKG_USE SDL2_ttf::SDL2_ttf CONFIG REQUIRED PKG_CONFIG sdl2_ttf)
add_package(TARGET CURL PKG CURL PKG_USE CURL::libcurl PKG_CONFIG libcurl)
add_package(TARGET OpenAL PKG OpenAL PKG_USE OpenAL::OpenAL CONFIG REQUIRED PKG_CONFIG OpenAL)
add_package(TARGET Ogg PKG Ogg PKG_USE Ogg::ogg CONFIG REQUIRED PKG_CONFIG ogg)
add_package(TARGET Vorbis PKG Vorbis PKG_USE Vorbis::vorbis CONFIG REQUIRED PKG_CONFIG vorbis)
add_package(TARGET VorbisFile PKG Vorbis PKG_USE Vorbis::vorbisfile CONFIG REQUIRED PKG_CONFIG vorbisfile)
add_package(TARGET glm PKG glm PKG_USE glm::glm CONFIG REQUIRED PKG_CONFIG glm)
add_package(TARGET fmt PKG fmt PKG_USE fmt::fmt CONFIG REQUIRED PKG_CONFIG fmt)
add_package(TARGET PhysFS PKG PhysFS PKG_USE PhysFS::PhysFS CONFIG REQUIRED PKG_CONFIG physfs PROVIDES ProvidePhysfs)

include(SuperTux/ProvideSavePNG)
include(SuperTux/ProvideSimpleSquirrel)
Expand Down Expand Up @@ -266,26 +235,26 @@ target_link_libraries(supertux2 PUBLIC LibTinygettext)
target_link_libraries(supertux2 PUBLIC LibSexp)
target_link_libraries(supertux2 PUBLIC LibSavePNG)
target_link_libraries(supertux2 PUBLIC LibPartioZip)
target_link_libraries(supertux2 PUBLIC OpenAL::OpenAL)
target_link_libraries(supertux2 PUBLIC glm::glm)
target_link_libraries(supertux2 PUBLIC OpenAL)
target_link_libraries(supertux2 PUBLIC glm)
target_compile_definitions(supertux2 PUBLIC GLM_ENABLE_EXPERIMENTAL)
target_link_libraries(supertux2 PUBLIC fmt::fmt)
target_link_libraries(supertux2 PUBLIC $<IF:$<TARGET_EXISTS:PhysFS::PhysFS>,PhysFS::PhysFS,PhysFS::PhysFS-static>)
target_link_libraries(supertux2 PUBLIC fmt)
target_link_libraries(supertux2 PUBLIC PhysFS)

if(NOT EMSCRIPTEN)
target_link_libraries(supertux2 PUBLIC
# SDL2_image
$<IF:$<TARGET_EXISTS:SDL2_image::SDL2_image>,SDL2_image::SDL2_image,SDL2_image::SDL2_image-static>
SDL2_image
# SDL2_ttf
$<IF:$<TARGET_EXISTS:SDL2_ttf::SDL2_ttf>,SDL2_ttf::SDL2_ttf,SDL2_ttf::SDL2_ttf-static>
SDL2_ttf
# SDL2 main (windows?)
$<TARGET_NAME_IF_EXISTS:SDL2::SDL2main>
# SDL2
$<IF:$<TARGET_EXISTS:SDL2::SDL2>,SDL2::SDL2,SDL2::SDL2-static>)
SDL2)
target_link_libraries(supertux2 PUBLIC
$<IF:$<TARGET_EXISTS:Ogg::ogg>,Ogg:ogg,PkgConfig::Ogg>
$<IF:$<TARGET_EXISTS:Vorbis::vorbis>,Vorbis::vorbis Vorbis::vorbisfile,
PkgConfig::Vorbis>
Ogg
Vorbis
VorbisFile
)
target_link_libraries(supertux2 PUBLIC CURL::libcurl)
endif()
Expand Down
83 changes: 83 additions & 0 deletions mk/cmake/SuperTux/AddPackage.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# AddPackage.cmake - Portable find_package approach
#
# Copyright (C) 2024 Hyland B. <[email protected]>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#

# This monstrosity essentially adds a complex package wrapping
# function, but it's honestly impressive. It essentially tries to
# default to find_package, however, this may not always work,
# especially on *NIX machines or anything weird like that. So, being
# clever, it then tries to go to PkgConfig, which (generally speaking)
# tends to produce more accurate results on *NIX
# machines. Unfortunately, this may not always work (and sometimes by
# design), so then it finally falls back to Supertux's old, weird
# Provide files. And if that fails, then that means the package
# doesn't exist or wasn't installed.

# Usage:
#
# add_package(TARGET SDL2 <-- The output target, a "target alias" (sometimes)
# PKG SDL2 <-- The find_package package, in this case it gets SDL2
# (might look similar to the output target)
# PKG_USE SDL2::SDL2 <-- Specific "namespace" from the package we want to alias
# CONFIG <-- (optional) Passed to find_package if its a CONFIG
# REQUIRED <-- (optional) NOT passed to find_package, just a check to
# throw an error if we've exhausted all options.
# PKG_CONFIG sdl2 sdl2_ttf <-- (optional, recommended) List of packages for PkgConfig
# PROVIDES ProvideSDL2 <-- (optional) Fallback to just look at the provided file.
# ) Undecided if I should fall back to a FindXXXX.cmake yet

find_package(PkgConfig)
macro(add_package)
cmake_parse_arguments(addpackage_args
"CONFIG;REQUIRED" "TARGET;PROVIDES;PKG;PKG_USE" "PKG_CONFIG"
${ARGN}
)

# Note: We don't pass "REQUIRED" here because we choose to fallback if it doesn't exist.
# Later, however, we do choose to throw an error based on this flag.
set(addpackage_fp_args "")
if (${addpackage_args_CONFIG})
string(APPEND addpackage_fp_args "CONFIG")
endif()

find_package(${addpackage_args_PKG} ${addpackage_fp_args})

if(${addpackage_args_PKG}_FOUND)
# See if its an alias (Is this needed?)
get_target_property(addpackage_pkg_alias_check ${addpackage_args_PKG_USE} ALIASED_TARGET)
if (addpackage_pkg_alias_check STREQUAL "addpackage_pkg_alias_check-NOTFOUND")
add_library(${addpackage_args_TARGET} ALIAS ${addpackage_args_PKG_USE})
else()
message(STATUS "Package \"${addpackage_args_PKG}\" is an alias. Realiasing it.")
# "unalias" it, aka just export the "alias" as the new target, so a re-alias, really...
get_target_property(${addpackage_args_TARGET} ${addpackage_args_PKG_USE} ALIASED_TARGET)
endif()
else()
#message(STATUS "CMake Package \"${addpackage_args_PKG}\" doesn't exist, so falling back to PkgConfig")

if (PkgConfig_FOUND)
if (${addpackage_args_REQUIRED})
list(APPEND addpackage_args_pkg_config_args REQUIRED)
endif()
pkg_check_modules(${addpackage_args_TARGET} ${addpackage_args_pkg_config_args} ${addpackage_args_PKG_CONFIG})
elseif(addpackage_args_REQUIRED)
# TODO look in Provides
message(FATAL_ERROR "Package \"${addpackage_args_TARGET}\" couldn't be found, but it's required.\nI don't know what to do. Is it installed?")
endif()
endif()
endmacro(add_package)

0 comments on commit 9866b82

Please sign in to comment.