tools/CMakeLists.txt
author Simon McVittie <smcv@debian.org>
Mon, 12 Sep 2022 10:40:53 -0400
branch1.0.0
changeset 15859 7b1d6dfa3173
parent 15334 8951eaad20e5
permissions -rw-r--r--
Remove FindSDL2 find-module, use sdl2-config.cmake instead This requires SDL >= 2.0.4. Since <https://bugzilla.libsdl.org/show_bug.cgi?id=2464> was fixed in SDL 2.0.4, SDL behaves as a CMake "config-file package", even if it was not itself built using CMake: it installs a sdl2-config.cmake file to ${libdir}/cmake/SDL2, which tells CMake where to find SDL's headers and library, analogous to a pkg-config .pc file. As a result, we no longer need to copy/paste a "find-module package" to be able to find a system copy of SDL >= 2.0.4 with find_package(SDL2). Find-module packages are now discouraged by the CMake developers, in favour of having upstream projects behave as config-file packages. This results in a small API change: FindSDL2 used to set SDL2_INCLUDE_DIR and SDL2_LIBRARY, but the standard behaviour for config-file packages is to set <name>_INCLUDE_DIRS and <name>_LIBRARIES. Use the CONFIG keyword to make sure we search in config-file package mode, and will not find a FindSDL2.cmake in some other directory that implements the old interface. In addition to deleting redundant code, this avoids some assumptions in FindSDL2 about the layout of a SDL installation. The current libsdl2-dev package in Debian breaks those assumptions; this is considered a bug and will hopefully be fixed soon, but it illustrates how fragile these assumptions can be. We can be more robust against different installation layouts by relying on SDL's own CMake integration. When linking to a copy of CMake in a non-standard location, users can now set the SDL2_DIR or CMAKE_PREFIX_PATH environment variable to point to it; previously, these users would have used the SDL2DIR environment variable. This continues to be unnecessary if using matching system-wide installations of CMake and SDL2, for example both from Debian.

if(NOT APPLE)
    configure_file("${CMAKE_CURRENT_SOURCE_DIR}/cmake_uninstall.cmake.in"
                   "${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake"
                    IMMEDIATE @ONLY)

    add_custom_target(uninstall "${CMAKE_COMMAND}" -P "${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake")
endif()

if(APPLE AND NOT SKIPBUNDLE)
    find_package(Qt5 REQUIRED QUIET COMPONENTS Core Widgets Gui Network)
    find_package(SDL2 REQUIRED CONFIG)
    find_package(SDL2_image 2 REQUIRED)
    find_package(SDL2_net 2 REQUIRED)
    find_package(SDL2_ttf 2 REQUIRED)
    find_package(SDL2_mixer 2 REQUIRED)
    find_package(OggVorbis REQUIRED)
    find_package(PNG REQUIRED)

    if(NOT NOAUTOUPDATE)
        find_package(Sparkle) #needed for SPARKLE_FOUND variable
        #needed because the 'if' clause in the script prints silly policy warnings
        if(SPARKLE_FOUND)
            set(SPARKLE_FOUND 1)
        else()
            set(SPARKLE_FOUND 0)
        endif()
    endif()

    #remove the ";-framework Cocoa" from the SDL2_LIBRARIES variable
    string(REGEX REPLACE "(.*);-.*" "\\1" sdl_library_only "${SDL2_LIBRARIES}")
    #remove the "libSDLmain.a" from the SDL2_LIBRARIES variable
    string(REGEX REPLACE ".*;(.*)" "\\1" sdl_library_only "${sdl_library_only}")

    #get the neme of the library (harmelss if it is static)
    string(REGEX REPLACE ".*/(.*)$" "\\1" PNG_LIBNAME "${PNG_LIBRARY}")
    string(REGEX REPLACE ".*/(.*)$" "\\1" ZLIB_LIBNAME "${ZLIB_LIBRARY}")

    set(frameworks_dir ${CMAKE_INSTALL_PREFIX}/${target_library_install_dir})
    
    if(${BUILD_ENGINE_LIBRARY})
        set(engine_full_path "${frameworks_dir}/${CMAKE_SHARED_LIBRARY_PREFIX}hwengine${CMAKE_SHARED_LIBRARY_SUFFIX}")
    else()
        set(engine_full_path "${CMAKE_INSTALL_PREFIX}/hwengine${CMAKE_EXECUTABLE_SUFFIX}")
    endif()


    #create the .app bundle using BundleUtilities instead of old macdeployqt
    set(APP_DIR "Hedgewars.app")
    set(APP_BASE_DIR "${CMAKE_INSTALL_PREFIX}/../")  # should be, Hedgewars.app/Contents/MacOS/../

    # macro to install qt5 plugins 
    # modified from https://github.com/Kitware/CMake/blob/master/Source/QtDialog/CMakeLists.txt
    macro(install_qt5_plugin _qt_plugin_name _qt_plugins_var)
      get_target_property(_qt_plugin_path "${_qt_plugin_name}" LOCATION)
      if(EXISTS "${_qt_plugin_path}")
        get_filename_component(_qt_plugin_file "${_qt_plugin_path}" NAME)
        get_filename_component(_qt_plugin_type "${_qt_plugin_path}" PATH)
        get_filename_component(_qt_plugin_type "${_qt_plugin_type}" NAME)
        set(_qt_plugin_dir "PlugIns")
        set(_qt_plugin_dest "${_qt_plugin_dir}/${_qt_plugin_type}")
        install(FILES "${_qt_plugin_path}"
          DESTINATION "../${_qt_plugin_dest}" # relative to install dir
          ${COMPONENT})
        list(APPEND ${_qt_plugins_var}
          "\${CMAKE_BINARY_DIR}/${APP_BASE_DIR}/${_qt_plugin_dest}/${_qt_plugin_file}")
      else()
        message(FATAL_ERROR "QT plugin ${_qt_plugin_name} not found")
      endif()
    endmacro()

    # install cocoa plugin and build list to send to fixup_bundle
    install_qt5_plugin("Qt5::QCocoaIntegrationPlugin" QT_PLUGINS)
    file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/qt.conf"
        "[Paths]\nPlugins = ${_qt_plugin_dir}\n")
    install(FILES "${CMAKE_CURRENT_BINARY_DIR}/qt.conf"
        DESTINATION "../Resources"  # relative to install dir
        ${COMPONENT})

    # Build up search directories for fixup_bundle
    set(DIRS "")
    # Add QT bin and lib paths
    if(CMAKE_PREFIX_PATH)
        foreach(dir ${CMAKE_PREFIX_PATH})
            list(APPEND DIRS "${dir}/bin" "${dir}/lib")
        endforeach()
    endif()
    # Add other lib folder from around the system
    list(APPEND DIRS 
        ~/Library/Frameworks
        /Library/Frameworks
        /usr/local/lib
        /opt/local/lib
    )

    # operate on the Hedgewars.app
    set(APPS ${CMAKE_BINARY_DIR}/${APP_DIR})
    
    # debugging
    message(STATUS "APPS: ${APPS}")
    message(STATUS "QT_PLUGINS: ${QT_PLUGINS}")
    message(STATUS "DIRS: ${DIRS}")

    # properly fixup the .app to include all dependencies    
    install(CODE "include(BundleUtilities)
        fixup_bundle(\"${APPS}\" \"${QT_PLUGINS}\" \"${DIRS}\")")
    
    
    #create the .dmg for deployment
    #first make sure .app exists, then remove any old .dmg with same name, finally run the script
    add_custom_target(dmg COMMAND make install
                          COMMAND rm -f ${CMAKE_BINARY_DIR}/Hedgewars-${HEDGEWARS_VERSION}.dmg
                          COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/create-dmg.sh
                                  --volname "Hedgewars ${HEDGEWARS_VERSION}"
                                # --volicon icon.icns
                                  --window-size 600 470
                                  --icon-size 96
                                  --icon "Hedgewars" 190 190
                                  --app-drop-link 410 190
                                  --background "${CMAKE_CURRENT_SOURCE_DIR}/../misc/dmgBackground.png"
                                  ${CMAKE_BINARY_DIR}/Hedgewars-${HEDGEWARS_VERSION}.dmg
                                  ${CMAKE_BINARY_DIR}/${APP_DIR}
                          WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
endif()