diff --git a/Ladybird/AppKit/CMakeLists.txt b/Ladybird/AppKit/CMakeLists.txt index b1f7ee39a1b..6a945c72491 100644 --- a/Ladybird/AppKit/CMakeLists.txt +++ b/Ladybird/AppKit/CMakeLists.txt @@ -17,7 +17,6 @@ add_library(ladybird_impl STATIC ) target_include_directories(ladybird_impl PUBLIC $) -target_compile_options(ladybird_impl PRIVATE "SHELL:$<$:-Xcc -std=c++23 -cxx-interoperability-mode=default>") target_compile_options(ladybird_impl PUBLIC $<$:-fobjc-arc> $<$:-Wno-deprecated-anon-enum-enum-conversion> # Required for CGImageCreate @@ -25,19 +24,6 @@ target_compile_options(ladybird_impl PUBLIC target_compile_features(ladybird_impl PUBLIC cxx_std_23) if (ENABLE_SWIFT) - enable_language(Swift) - if (CMAKE_Swift_COMPILER_VERSION VERSION_LESS 6.0) - message(FATAL_ERROR - "Swift 6.0 or newer is required to parse AK C++ headers in C++23 mode" - ) - endif() - if (NOT CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang") - message(FATAL_ERROR - "Swift files must use Clang that was bundled with swiftc" - ) - endif() - include(Swift/GenerateSwiftHeader) - target_sources(ladybird_impl PRIVATE UI/TaskManager.swift UI/TaskManagerController.swift diff --git a/Meta/CMake/Swift/InitializeSwift.cmake b/Meta/CMake/Swift/InitializeSwift.cmake new file mode 100644 index 00000000000..3f8f92e3e4e --- /dev/null +++ b/Meta/CMake/Swift/InitializeSwift.cmake @@ -0,0 +1,99 @@ +# This source file is part of the Swift open source project +# +# Copyright (c) 2023 Apple Inc. and the Swift project authors. +# Licensed under Apache License v2.0 with Runtime Library Exception +# +# See https://swift.org/LICENSE.txt for license information + +# Compute the name of the architecture directory on Windows from the CMake +# system processor name. +function(_swift_windows_arch_name output_variable_name target_arch) + if(NOT WIN32) + return() + endif() + + if("${target_arch}" STREQUAL "AMD64") + set("${output_variable_name}" "x86_64" PARENT_SCOPE) + elseif("${target_arch}" STREQUAL "ARM64") + set("${output_variable_name}" "aarch64" PARENT_SCOPE) + else() + message(FATAL_ERROR "Unknown windows architecture: ${target_arch}") + endif() +endfunction() + +# Compute flags and search paths +# NOTE: This logic will eventually move to CMake +function(_setup_swift_paths) + # If we haven't set the swift library search paths, do that now + if(NOT SWIFT_LIBRARY_SEARCH_PATHS OR NOT SWIFT_INCLUDE_PATHS) + if(APPLE) + set(SDK_FLAGS "-sdk" "${CMAKE_OSX_SYSROOT}") + endif() + + # Note: This does not handle cross-compiling correctly. + # To handle it correctly, we would need to pass the target triple and + # flags to this compiler invocation. + execute_process( + COMMAND ${CMAKE_Swift_COMPILER} ${SDK_FLAGS} -print-target-info + OUTPUT_VARIABLE SWIFT_TARGET_INFO + ) + + # extract search paths from swift driver response + string(JSON SWIFT_TARGET_PATHS GET ${SWIFT_TARGET_INFO} "paths") + + string(JSON SWIFT_TARGET_LIBRARY_PATHS GET ${SWIFT_TARGET_PATHS} "runtimeLibraryPaths") + string(JSON SWIFT_TARGET_LIBRARY_PATHS_LENGTH LENGTH ${SWIFT_TARGET_LIBRARY_PATHS}) + math(EXPR SWIFT_TARGET_LIBRARY_PATHS_LENGTH "${SWIFT_TARGET_LIBRARY_PATHS_LENGTH} - 1 ") + + string(JSON SWIFT_TARGET_LIBRARY_IMPORT_PATHS GET ${SWIFT_TARGET_PATHS} "runtimeLibraryImportPaths") + string(JSON SWIFT_TARGET_LIBRARY_IMPORT_PATHS_LENGTH LENGTH ${SWIFT_TARGET_LIBRARY_IMPORT_PATHS}) + math(EXPR SWIFT_TARGET_LIBRARY_IMPORT_PATHS_LENGTH "${SWIFT_TARGET_LIBRARY_IMPORT_PATHS_LENGTH} - 1 ") + + string(JSON SWIFT_SDK_IMPORT_PATH ERROR_VARIABLE errno GET ${SWIFT_TARGET_PATHS} "sdkPath") + + foreach(JSON_ARG_IDX RANGE ${SWIFT_TARGET_LIBRARY_PATHS_LENGTH}) + string(JSON SWIFT_LIB GET ${SWIFT_TARGET_LIBRARY_PATHS} ${JSON_ARG_IDX}) + list(APPEND SWIFT_SEARCH_PATHS ${SWIFT_LIB}) + endforeach() + + foreach(JSON_ARG_IDX RANGE ${SWIFT_TARGET_LIBRARY_IMPORT_PATHS_LENGTH}) + string(JSON SWIFT_LIB GET ${SWIFT_TARGET_LIBRARY_IMPORT_PATHS} ${JSON_ARG_IDX}) + list(APPEND SWIFT_SEARCH_PATHS ${SWIFT_LIB}) + endforeach() + + if(SWIFT_SDK_IMPORT_PATH) + list(APPEND SWIFT_SEARCH_PATHS ${SWIFT_SDK_IMPORT_PATH}) + endif() + + # Save the swift library search paths + set(SWIFT_LIBRARY_SEARCH_PATHS ${SWIFT_SEARCH_PATHS} CACHE FILEPATH "Swift driver search paths") + + string(JSON SWIFT_RUNTIME_RESOURCE_PATH GET ${SWIFT_TARGET_PATHS} "runtimeResourcePath") + set(SWIFT_TOOLCHAIN_INCLUDE_DIR "${SWIFT_RUNTIME_RESOURCE_PATH}/../../include") + cmake_path(ABSOLUTE_PATH SWIFT_TOOLCHAIN_INCLUDE_DIR NORMALIZE) + if (NOT IS_DIRECTORY "${SWIFT_TOOLCHAIN_INCLUDE_DIR}") + message(WARNING "Expected toolchain include dir ${SWIFT_TOOLCHAIN_INCLUDE_DIR} does not exist") + endif() + set(SWIFT_INCLUDE_PATHS ${SWIFT_TOOLCHAIN_INCLUDE_DIR} CACHE FILEPATH "Swift interop include paths") + endif() + + link_directories(${SWIFT_LIBRARY_SEARCH_PATHS}) + include_directories(${SWIFT_INCLUDE_PATHS}) + + if(WIN32) + _swift_windows_arch_name(SWIFT_WIN_ARCH_DIR "${CMAKE_SYSTEM_PROCESSOR}") + set(SWIFT_SWIFTRT_FILE "$ENV{SDKROOT}/usr/lib/swift/windows/${SWIFT_WIN_ARCH_DIR}/swiftrt.obj") + add_link_options("$<$:${SWIFT_SWIFTRT_FILE}>") + elseif(NOT APPLE) + find_file(SWIFT_SWIFTRT_FILE + swiftrt.o + PATHS ${SWIFT_LIBRARY_SEARCH_PATHS} + NO_CACHE + REQUIRED + NO_DEFAULT_PATH) + add_link_options("$<$:${SWIFT_SWIFTRT_FILE}>") + endif() +endfunction() + +_setup_swift_paths() + diff --git a/Meta/CMake/Swift/swift-settings.cmake b/Meta/CMake/Swift/swift-settings.cmake new file mode 100644 index 00000000000..36cef4a9483 --- /dev/null +++ b/Meta/CMake/Swift/swift-settings.cmake @@ -0,0 +1,19 @@ +enable_language(Swift) + +if (CMAKE_Swift_COMPILER_VERSION VERSION_LESS 6.0) + message(FATAL_ERROR + "Swift 6.0 or newer is required to parse C++ headers in C++23 mode" + ) +endif() + +# FIXME: How to verify this on non-Apple? +if (APPLE AND NOT CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang") + message(FATAL_ERROR + "Swift files must use Clang that was bundled with swiftc" + ) +endif() + +include(${CMAKE_CURRENT_LIST_DIR}/InitializeSwift.cmake) +include(${CMAKE_CURRENT_LIST_DIR}/GenerateSwiftHeader.cmake) + +add_compile_options("SHELL:$<$:-Xcc -std=c++23 -cxx-interoperability-mode=default>") diff --git a/Meta/CMake/common_options.cmake b/Meta/CMake/common_options.cmake index e6cdc0d3df7..52d2fd3a4fd 100644 --- a/Meta/CMake/common_options.cmake +++ b/Meta/CMake/common_options.cmake @@ -34,6 +34,9 @@ serenity_option(ENABLE_CLANG_PLUGINS OFF CACHE BOOL "Enable building with the Cl serenity_option(ENABLE_CLANG_PLUGINS_INVALID_FUNCTION_MEMBERS OFF CACHE BOOL "Enable detecting invalid function types as members of GC-allocated objects") serenity_option(ENABLE_GUI_TARGETS ON CACHE BOOL "Enable building GUI targets") +serenity_option(ENABLE_INSTALL_HEADERS ON CACHE BOOL "Enable installing headers") serenity_option(ENABLE_SWIFT OFF CACHE BOOL "Enable building Swift files") -serenity_option(ENABLE_INSTALL_HEADERS ON CACHE BOOL "Enable installing headers") +if (ENABLE_SWIFT) + include(${CMAKE_CURRENT_LIST_DIR}/Swift/swift-settings.cmake) +endif()