cmake: add flags to only build the compiler

... and to use an external compiler

For example, this is how one only build the compiler:

```
cmake .. -DSLINT_BUILD_RUNTIME=OFF -DCMAKE_INSTALL_PREFIX=/tmp/slint_compiler
make install
```

And this only build the runtime
```
cmake .. -DSLINT_FEATURE_COMPILER=OFF -DCMAKE_INSTALL_PREFIX=/tmp/install_runtime
make install
```

And then this can be used in a project like so:
```
cmake .. -DCMAKE_PREFIX_PATH=/tmp/install_runtime/ -DSLINT_COMPILER=/tmp/install_compiler/bin/slint-compiler
```
This commit is contained in:
Olivier Goffart 2023-09-15 14:42:52 +02:00 committed by GitHub
parent d299f0bf3e
commit 14f7fe4ba2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 265 additions and 243 deletions

View file

@ -23,6 +23,7 @@ All notable changes to this project are documented in this file.
### C++
- Removed the need for C++ exceptions in generated code.
- Added ability to only build the Slint compiler or use an external compiler.
### LSP

View file

@ -38,7 +38,7 @@ add_feature_info(SLINT_BUILD_EXAMPLES SLINT_BUILD_EXAMPLES "configure whether to
if(SLINT_BUILD_EXAMPLES)
add_subdirectory(examples)
endif()
if(SLINT_BUILD_TESTING)
if(SLINT_BUILD_TESTING AND (SLINT_FEATURE_COMPILER OR SLINT_COMPILER))
add_subdirectory(docs/tutorial/cpp/src/)
endif()

View file

@ -20,63 +20,13 @@ find_package(Rust 1.70 REQUIRED MODULE)
option(BUILD_SHARED_LIBS "Build Slint as shared library" ON)
option(SLINT_FEATURE_COMPILER "Enable support for compiling .slint files to C++ ahead of time" ON)
add_feature_info(SLINT_FEATURE_COMPILER SLINT_FEATURE_COMPILER "Enable support for compiling .slint files to C++ ahead of time")
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)")
if(SLINT_FEATURE_COMPILER)
set(slint_compiler_crate "slint-compiler")
endif()
if(BUILD_SHARED_LIBS)
set(rustc_lib_type "cdylib")
set(slint_cpp_impl "slint-cpp-shared")
set(cmake_lib_type "SHARED")
else()
set(rustc_lib_type "staticlib")
set(slint_cpp_impl "slint-cpp-static")
set(cmake_lib_type "STATIC")
endif()
corrosion_import_crate(MANIFEST_PATH "${CMAKE_CURRENT_SOURCE_DIR}/../../Cargo.toml"
CRATES slint-cpp ${slint_compiler_crate} CRATE_TYPES bin ${rustc_lib_type})
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")
set(SLINT_LIBRARY_CARGO_FLAGS "" CACHE STRING
"Flags to pass to cargo when building the Slint runtime library")
if(SLINT_LIBRARY_CARGO_FLAGS)
corrosion_set_cargo_flags(slint-cpp ${SLINT_LIBRARY_CARGO_FLAGS})
endif()
# When doing "make install" package builds, set install_name to rpath, so that the installed
# binaries don't have a load-command pointing back to their build directory.
# Don't do this when Slint is used via FetchContent. While we could set CMAKE_BUILD_RPATH to
# include the binary dir and thus our examples would have the correct rpath set, binaries
# outside (i.e. applications using Slint via FetchContent) would not and the BUILD_RPATH
# target property doesn't propagate :(
if (APPLE AND SLINT_IS_TOPLEVEL_BUILD AND BUILD_SHARED_LIBS)
# corrosion could provide the Cargo.toml package version as a CMake target property.
corrosion_add_target_local_rustflags(slint-cpp -Clink-arg=-Wl,-install_name,@rpath/libslint_cpp.dylib,-current_version,${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR},-compatibility_version,${PROJECT_VERSION_MAJOR}.0)
# Set this one to false again explicitely because Corrosion will starting setting this property to true by default.
set_target_properties(slint-cpp-shared PROPERTIES IMPORTED_NO_SONAME 0)
set_target_properties(slint-cpp-shared PROPERTIES IMPORTED_SONAME libslint_cpp.dylib)
endif()
set_property(
TARGET slint-cpp
APPEND
PROPERTY CORROSION_ENVIRONMENT_VARIABLES
"SLINT_GENERATED_INCLUDE_DIR=${CMAKE_CURRENT_BINARY_DIR}/generated_include/"
)
if(SLINT_FEATURE_COMPILER)
corrosion_set_hostbuild(slint-compiler)
endif()
add_library(Slint INTERFACE)
add_library(Slint::Slint ALIAS Slint)
target_link_libraries(Slint INTERFACE slint-cpp)
target_compile_features(Slint INTERFACE cxx_std_20)
if (MSVC)
target_compile_options(Slint INTERFACE /bigobj)
endif()
function(define_cargo_feature cargo-feature description default)
# turn foo-bar into SLINT_FEATURE_FOO_BAR
@ -148,27 +98,88 @@ define_cargo_dependent_feature(backend-linuxkms "Enable support for the backend
define_cargo_dependent_feature(gettext "Enable support of translations using gettext" OFF "NOT SLINT_FEATURE_FREESTANDING")
define_cargo_dependent_feature(accessibility "Enable integration with operating system provided accessibility APIs" ON "NOT SLINT_FEATURE_FREESTANDING")
if (SLINT_FEATURE_FREESTANDING)
if (SLINT_BUILD_RUNTIME)
if(SLINT_FEATURE_COMPILER AND NOT SLINT_COMPILER)
set(slint_compiler_crate "slint-compiler")
endif()
if(BUILD_SHARED_LIBS)
set(rustc_lib_type "cdylib")
set(slint_cpp_impl "slint-cpp-shared")
set(cmake_lib_type "SHARED")
else()
set(rustc_lib_type "staticlib")
set(slint_cpp_impl "slint-cpp-static")
set(cmake_lib_type "STATIC")
endif()
corrosion_import_crate(MANIFEST_PATH "${CMAKE_CURRENT_SOURCE_DIR}/../../Cargo.toml"
CRATES slint-cpp ${slint_compiler_crate} CRATE_TYPES bin ${rustc_lib_type})
elseif(SLINT_FEATURE_COMPILER AND NOT SLINT_COMPILER)
corrosion_import_crate(MANIFEST_PATH "${CMAKE_CURRENT_SOURCE_DIR}/../../Cargo.toml"
CRATES slint-compiler CRATE_TYPES bin)
endif()
if(SLINT_FEATURE_COMPILER AND NOT SLINT_COMPILER)
corrosion_set_hostbuild(slint-compiler)
endif()
if (SLINT_BUILD_RUNTIME)
# When doing "make install" package builds, set install_name to rpath, so that the installed
# binaries don't have a load-command pointing back to their build directory.
# Don't do this when Slint is used via FetchContent. While we could set CMAKE_BUILD_RPATH to
# include the binary dir and thus our examples would have the correct rpath set, binaries
# outside (i.e. applications using Slint via FetchContent) would not and the BUILD_RPATH
# target property doesn't propagate :(
if (APPLE AND SLINT_IS_TOPLEVEL_BUILD AND BUILD_SHARED_LIBS)
# corrosion could provide the Cargo.toml package version as a CMake target property.
corrosion_add_target_local_rustflags(slint-cpp -Clink-arg=-Wl,-install_name,@rpath/libslint_cpp.dylib,-current_version,${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR},-compatibility_version,${PROJECT_VERSION_MAJOR}.0)
# Set this one to false again explicitely because Corrosion will starting setting this property to true by default.
set_target_properties(slint-cpp-shared PROPERTIES IMPORTED_NO_SONAME 0)
set_target_properties(slint-cpp-shared PROPERTIES IMPORTED_SONAME libslint_cpp.dylib)
endif()
add_library(Slint INTERFACE)
add_library(Slint::Slint ALIAS Slint)
target_link_libraries(Slint INTERFACE slint-cpp)
target_compile_features(Slint INTERFACE cxx_std_20)
if (MSVC)
target_compile_options(Slint INTERFACE /bigobj)
endif()
if (SLINT_FEATURE_FREESTANDING)
if (ESP_PLATFORM)
list(APPEND features "esp-backtrace/${IDF_TARGET}")
endif()
else (SLINT_FEATURE_FREESTANDING)
else (SLINT_FEATURE_FREESTANDING)
list(APPEND features std)
endif()
endif()
set_property(
set_property(
TARGET slint-cpp
APPEND
PROPERTY CORROSION_ENVIRONMENT_VARIABLES
"SLINT_GENERATED_INCLUDE_DIR=${CMAKE_CURRENT_BINARY_DIR}/generated_include/"
)
set_property(
TARGET slint-cpp
PROPERTY CORROSION_FEATURES
${features}
)
set_property(
)
set_property(
TARGET slint-cpp
PROPERTY CORROSION_NO_DEFAULT_FEATURES
ON
)
)
if(SLINT_FEATURE_BACKEND_QT)
if(SLINT_LIBRARY_CARGO_FLAGS)
corrosion_set_cargo_flags(slint-cpp ${SLINT_LIBRARY_CARGO_FLAGS})
endif()
if(SLINT_FEATURE_BACKEND_QT)
# For the CMake build don't rely on qmake being in PATH but use CMake to locate Qt. This
# means usually CMAKE_PREFIX_PATH is set.
find_package(Qt6 6.2 QUIET COMPONENTS Core Widgets)
@ -176,9 +187,9 @@ if(SLINT_FEATURE_BACKEND_QT)
if(NOT TARGET Qt::qmake)
find_package(Qt5 5.15 QUIET COMPONENTS Core Widgets)
endif()
endif(SLINT_FEATURE_BACKEND_QT)
endif(SLINT_FEATURE_BACKEND_QT)
if(TARGET Qt::qmake)
if(TARGET Qt::qmake)
set_property(
TARGET slint-cpp
APPEND
@ -186,7 +197,7 @@ if(TARGET Qt::qmake)
QMAKE=$<TARGET_PROPERTY:Qt::qmake,LOCATION>
)
set(SLINT_STYLE_DEFAULT "native")
else()
else()
set_property(
TARGET slint-cpp
APPEND
@ -194,17 +205,17 @@ else()
SLINT_NO_QT=1
)
set(SLINT_STYLE_DEFAULT "fluent")
endif()
endif()
set(SLINT_STYLE "" CACHE STRING "The Slint Widget Style")
set(SLINT_STYLE "" CACHE STRING "The Slint Widget Style")
if(SLINT_STYLE)
if(SLINT_STYLE)
set_property(GLOBAL PROPERTY SLINT_STYLE ${SLINT_STYLE})
else(SLINT_STYLE)
else(SLINT_STYLE)
set_property(GLOBAL PROPERTY SLINT_STYLE ${SLINT_STYLE_DEFAULT})
endif(SLINT_STYLE)
endif(SLINT_STYLE)
if(SLINT_FEATURE_RENDERER_SKIA OR SLINT_FEATURE_RENDERER_SKIA_OPENGL OR SLINT_FEATURE_RENDERER_SKIA_VULKAN)
if(SLINT_FEATURE_RENDERER_SKIA OR SLINT_FEATURE_RENDERER_SKIA_OPENGL OR SLINT_FEATURE_RENDERER_SKIA_VULKAN)
find_program(CLANGCC clang)
find_program(CLANGCXX clang++)
if(CLANGCC AND CLANGCXX)
@ -256,16 +267,16 @@ if(SLINT_FEATURE_RENDERER_SKIA OR SLINT_FEATURE_RENDERER_SKIA_OPENGL OR SLINT_FE
)
endif()
endif()
endif()
endif()
file(GLOB api_headers RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}/include/"
file(GLOB api_headers RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}/include/"
"${CMAKE_CURRENT_SOURCE_DIR}/include/*.h")
foreach(header IN LISTS api_headers)
foreach(header IN LISTS api_headers)
set_property(TARGET Slint APPEND PROPERTY PUBLIC_HEADER include/${header})
endforeach()
endforeach()
set(generated_headers
set(generated_headers
${CMAKE_CURRENT_BINARY_DIR}/generated_include/slint_internal.h
${CMAKE_CURRENT_BINARY_DIR}/generated_include/slint_enums_internal.h
${CMAKE_CURRENT_BINARY_DIR}/generated_include/slint_enums.h
@ -281,55 +292,61 @@ set(generated_headers
${CMAKE_CURRENT_BINARY_DIR}/generated_include/slint_qt_internal.h
${CMAKE_CURRENT_BINARY_DIR}/generated_include/slint_platform_internal.h
${CMAKE_CURRENT_BINARY_DIR}/generated_include/slint_generated_public.h
)
)
if(SLINT_FEATURE_INTERPRETER)
if(SLINT_FEATURE_INTERPRETER)
list(APPEND generated_headers
${CMAKE_CURRENT_BINARY_DIR}/generated_include/slint_interpreter_internal.h
${CMAKE_CURRENT_BINARY_DIR}/generated_include/slint_interpreter_generated_public.h
)
endif()
endif()
foreach(header IN LISTS generated_headers)
foreach(header IN LISTS generated_headers)
set_property(TARGET Slint APPEND PROPERTY PUBLIC_HEADER ${header})
endforeach()
endforeach()
target_include_directories(Slint INTERFACE
target_include_directories(Slint INTERFACE
$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/generated_include>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include/slint>
)
)
if(SLINT_FEATURE_COMPILER)
if(SLINT_FEATURE_COMPILER AND NOT SLINT_COMPILER)
add_executable(Slint::slint-compiler ALIAS slint-compiler)
include(${CMAKE_CURRENT_LIST_DIR}/cmake/SlintMacro.cmake)
endif()
endif()
export(TARGETS Slint slint-cpp
export(TARGETS Slint slint-cpp
NAMESPACE Slint:: FILE "${CMAKE_BINARY_DIR}/lib/cmake/Slint/SlintTargets.cmake")
install(EXPORT SlintTargets NAMESPACE Slint:: DESTINATION lib/cmake/Slint)
install(TARGETS Slint slint-cpp
install(EXPORT SlintTargets NAMESPACE Slint:: DESTINATION lib/cmake/Slint)
install(TARGETS Slint slint-cpp
EXPORT SlintTargets LIBRARY DESTINATION lib PUBLIC_HEADER DESTINATION include/slint)
install(FILES $<TARGET_FILE:${slint_cpp_impl}> TYPE LIB)
if(WIN32)
install(FILES $<TARGET_LINKER_FILE:${slint_cpp_impl}> TYPE LIB)
endif()
endif(SLINT_BUILD_RUNTIME)
include(CMakePackageConfigHelpers)
include(GNUInstallDirs)
install(FILES $<TARGET_FILE:${slint_cpp_impl}> TYPE LIB)
if(WIN32)
install(FILES $<TARGET_LINKER_FILE:${slint_cpp_impl}> TYPE LIB)
endif()
if(SLINT_FEATURE_COMPILER)
if(SLINT_FEATURE_COMPILER AND NOT SLINT_COMPILER)
install(PROGRAMS $<TARGET_FILE:slint-compiler> TYPE BIN)
endif()
# Corrosion sets the `IMPORTED` locations late to allow us to set OUTPUT_DIRECTORY
# target properties. This function must be deferred until after Corrosion set the
# locations. Since we are writing to a config file Generator expressions are not
# an option.
function(_slint_write_configure_file)
if(SLINT_BUILD_RUNTIME)
# Corrosion sets the `IMPORTED` locations late to allow us to set OUTPUT_DIRECTORY
# target properties. This function must be deferred until after Corrosion set the
# locations. Since we are writing to a config file Generator expressions are not
# an option.
function(_slint_write_configure_file)
foreach(prop
IMPORTED_LOCATION IMPORTED_LOCATION_DEBUG IMPORTED_LOCATION_RELEASE
IMPORTED_LOCATION_RELWITHDEBINFO IMPORTED_LOCATION_MINSIZEREL
@ -345,32 +362,26 @@ function(_slint_write_configure_file)
get_property(_SLINT_STYLE GLOBAL PROPERTY SLINT_STYLE)
configure_package_config_file("cmake/SlintConfig.cmake.in" "${CMAKE_CURRENT_BINARY_DIR}/lib/cmake/Slint/SlintConfig.cmake" INSTALL_DESTINATION lib/cmake/Slint)
endfunction()
endfunction()
cmake_language(DEFER CALL _slint_write_configure_file)
cmake_language(DEFER CALL _slint_write_configure_file)
write_basic_package_version_file(
write_basic_package_version_file(
${CMAKE_CURRENT_BINARY_DIR}/lib/cmake/Slint/SlintConfigVersion.cmake
VERSION 1.3.0
COMPATIBILITY SameMinorVersion
)
)
install(FILES
install(FILES
"${CMAKE_CURRENT_BINARY_DIR}/lib/cmake/Slint/SlintConfig.cmake"
"${CMAKE_CURRENT_BINARY_DIR}/lib/cmake/Slint/SlintConfigVersion.cmake"
DESTINATION lib/cmake/Slint
)
if(SLINT_FEATURE_COMPILER)
install(FILES
"${CMAKE_CURRENT_LIST_DIR}/cmake/SlintMacro.cmake"
DESTINATION lib/cmake/Slint
)
endif()
option(SLINT_PACKAGE_BUNDLE_QT "Internal setting to install Qt binary in the packages" OFF)
option(SLINT_PACKAGE_BUNDLE_QT "Internal setting to install Qt binary in the packages" OFF)
if(SLINT_PACKAGE_BUNDLE_QT)
if(SLINT_PACKAGE_BUNDLE_QT)
if(WIN32)
find_package(Qt6 6.0 COMPONENTS Core Gui Widgets Svg)
install(
@ -397,7 +408,9 @@ if(SLINT_PACKAGE_BUNDLE_QT)
include(InstallRequiredSystemLibraries)
install(FILES ${CMAKE_INSTALL_SYSTEM_RUNTIME_LIBS} TYPE LIB)
endif()
endif(SLINT_PACKAGE_BUNDLE_QT)
endif(SLINT_PACKAGE_BUNDLE_QT)
endif(SLINT_BUILD_RUNTIME)
set(CPACK_PACKAGE_NAME "Slint-cpp")
set(CPACK_PACKAGE_VENDOR "Slint")

View file

@ -14,7 +14,13 @@ endif()
add_library(@slint_cpp_impl@ @cmake_lib_type@ IMPORTED)
set_target_properties(@slint_cpp_impl@ PROPERTIES @SLINT_LIB_PROPERTIES@)
if (@SLINT_FEATURE_COMPILER@)
set(SLINT_COMPILER @SLINT_COMPILER@ CACHE STRING "Path to the slint-compiler executable")
if (SLINT_COMPILER)
set(SLINT_STYLE @_SLINT_STYLE@ CACHE STRING "The Slint widget style")
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")
elseif (@SLINT_FEATURE_COMPILER@)
add_executable(Slint::slint-compiler IMPORTED GLOBAL)
set_target_properties(Slint::slint-compiler PROPERTIES IMPORTED_LOCATION "${_IMPORT_PREFIX}/@CMAKE_INSTALL_BINDIR@/slint-compiler${CMAKE_EXECUTABLE_SUFFIX}")
include("${CMAKE_CURRENT_LIST_DIR}/SlintMacro.cmake")

View file

@ -45,7 +45,9 @@ if(NOT SLINT_FEATURE_FREESTANDING)
endif()
slint_test(models)
add_subdirectory(multiple-includes)
if(SLINT_FEATURE_COMPILER OR SLINT_COMPILER)
add_subdirectory(multiple-includes)
endif()
slint_test(platform_eventloop)
target_link_libraries(test_platform_eventloop PRIVATE Threads::Threads)