From 3133b4ca1d45e2ca6cce02e8644e2796e84efcdb Mon Sep 17 00:00:00 2001 From: Simon Hausmann Date: Wed, 28 Aug 2024 16:03:14 +0200 Subject: [PATCH] Make it possible to configure a CMake build with the slint-compiler to be downloaded at app configure time --- .github/workflows/upgrade_version.yaml | 1 + api/cpp/CMakeLists.txt | 10 +++++--- api/cpp/cmake/SlintConfig.cmake.in | 35 +++++++++++++++++++++++++- 3 files changed, 42 insertions(+), 4 deletions(-) diff --git a/.github/workflows/upgrade_version.yaml b/.github/workflows/upgrade_version.yaml index e996ca0eade..1717fd474e1 100644 --- a/.github/workflows/upgrade_version.yaml +++ b/.github/workflows/upgrade_version.yaml @@ -24,6 +24,7 @@ jobs: # Update the version in CmakeLists.txt sed -i 's/ VERSION [0-9]*\.[0-9]*\.[0-9]*$/ VERSION ${{ github.event.inputs.new_version }}/' api/cpp/CMakeLists.txt + sed -i 's/SLINT_VERSION [0-9]*\.[0-9]*\.[0-9]*$/SLINT_VERSION ${{ github.event.inputs.new_version }}/' api/cpp/CMakeLists.txt sed -i "s/(CPACK_PACKAGE_VERSION_MAJOR [0-9]*)/(CPACK_PACKAGE_VERSION_MAJOR `echo ${{ github.event.inputs.new_version }} | sed "s/\([0-9]*\)\.\([0-9]*\).\([0-9]*\)/\1/"`)/" api/cpp/CMakeLists.txt sed -i "s/(CPACK_PACKAGE_VERSION_MINOR [0-9]*)/(CPACK_PACKAGE_VERSION_MINOR `echo ${{ github.event.inputs.new_version }} | sed "s/\([0-9]*\)\.\([0-9]*\).\([0-9]*\)/\2/"`)/" api/cpp/CMakeLists.txt sed -i "s/(CPACK_PACKAGE_VERSION_PATCH [0-9]*)/(CPACK_PACKAGE_VERSION_PATCH `echo ${{ github.event.inputs.new_version }} | sed "s/\([0-9]*\)\.\([0-9]*\).\([0-9]*\)/\3/"`)/" api/cpp/CMakeLists.txt diff --git a/api/cpp/CMakeLists.txt b/api/cpp/CMakeLists.txt index d16da6a407a..0ef8f32e6e3 100644 --- a/api/cpp/CMakeLists.txt +++ b/api/cpp/CMakeLists.txt @@ -26,7 +26,7 @@ add_feature_info(SLINT_FEATURE_COMPILER SLINT_FEATURE_COMPILER "Enable support f option(SLINT_BUILD_RUNTIME "Actually build the Slint runtime libraries (Disable that to only build the compiler)" ON) add_feature_info(SLINT_BUILD_RUNTIME SLINT_BUILD_RUNTIME "Actually build the Slint runtime libraries (Disable that to only build the compiler)") -set(SLINT_COMPILER "" CACHE FILEPATH "Path to the slint-compiler executable. When unset, it the compiler will be build as part of the build process") +set(SLINT_COMPILER "" CACHE STRING "Path to the slint-compiler executable. When unset, it the compiler will be build as part of the build process. When set to 'download', the compiler will be downloaded from GitHub.") set(SLINT_LIBRARY_CARGO_FLAGS "" CACHE STRING "Flags to pass to cargo when building the Slint runtime library") @@ -431,8 +431,12 @@ if(SLINT_BUILD_RUNTIME) endforeach() get_property(_SLINT_STYLE GLOBAL PROPERTY SLINT_STYLE) - get_target_property(_slint_compiler_location slint-compiler IMPORTED_LOCATION) - cmake_path(GET _slint_compiler_location FILENAME SLINT_COMPILER_FILE_NAME) + if (TARGET slint-compiler) + get_target_property(_slint_compiler_location slint-compiler IMPORTED_LOCATION) + cmake_path(GET _slint_compiler_location FILENAME SLINT_COMPILER_FILE_NAME) + endif() + + set(SLINT_VERSION 1.8.0) configure_package_config_file("cmake/SlintConfig.cmake.in" "${CMAKE_CURRENT_BINARY_DIR}/lib/cmake/Slint/SlintConfig.cmake" INSTALL_DESTINATION lib/cmake/Slint) endfunction() diff --git a/api/cpp/cmake/SlintConfig.cmake.in b/api/cpp/cmake/SlintConfig.cmake.in index aec4e1e6d98..73f1764bedf 100644 --- a/api/cpp/cmake/SlintConfig.cmake.in +++ b/api/cpp/cmake/SlintConfig.cmake.in @@ -14,8 +14,41 @@ endif() add_library(@slint_cpp_impl@ @cmake_lib_type@ IMPORTED) set_target_properties(@slint_cpp_impl@ PROPERTIES @SLINT_LIB_PROPERTIES@) -set(SLINT_COMPILER @SLINT_COMPILER@ CACHE FILEPATH "Path to the slint-compiler executable") +function(_slint_download_compiler_and_cache) + set(SLINT_GITHUB_RELEASE @SLINT_VERSION@ CACHE STRING "GitHub Release to use for Slint Binary Artifact Downloads") + + if (CMAKE_HOST_WIN32) + set(compiler_exe_suffix ".exe") + set(platform_suffix "windows") + else() + set(platform_suffix "${CMAKE_HOST_SYSTEM_NAME}-${CMAKE_HOST_SYSTEM_PROCESSOR}") + endif() + + set(prebuilt_archive_filename "slint-compiler-${platform_suffix}.tar.gz") + set(download_url "https://github.com/slint-ui/slint/releases/download/${SLINT_GITHUB_RELEASE}/${prebuilt_archive_filename}") + set(download_directory "${CMAKE_BINARY_DIR}/slint-prebuilt") + message(STATUS "Downloading pre-built Slint compiler binary from ${download_url}") + file(DOWNLOAD "${download_url}" "${download_directory}/${prebuilt_archive_filename}" STATUS download_status) + list(GET download_status 0 download_code) + if (NOT download_code EQUAL 0) + list(GET download_status 1 download_message) + message(FATAL_ERROR "Download of Slint compiler binary package failed: ${download_message}") + else() + file(ARCHIVE_EXTRACT INPUT "${download_directory}/${prebuilt_archive_filename}" DESTINATION "${download_directory}") + file(REMOVE "${download_directory}/${prebuilt_archive_filename}") + + set(SLINT_COMPILER "${download_directory}/slint-compiler${compiler_exe_suffix}") + set(SLINT_COMPILER "${SLINT_COMPILER}" CACHE STRING "Path to the slint-compiler executable" FORCE) + set(SLINT_COMPILER "${SLINT_COMPILER}" PARENT_SCOPE) + endif() +endfunction() + +set(SLINT_COMPILER @SLINT_COMPILER@ CACHE STRING "Path to the slint-compiler executable") if (SLINT_COMPILER) + if (SLINT_COMPILER STREQUAL "download") + _slint_download_compiler_and_cache(_) + endif() + add_executable(Slint::slint-compiler IMPORTED GLOBAL) set_target_properties(Slint::slint-compiler PROPERTIES IMPORTED_LOCATION ${SLINT_COMPILER}) include("${CMAKE_CURRENT_LIST_DIR}/SlintMacro.cmake")