From c1c7e5ff3e47b78b86a777fcbb5949e8eb910577 Mon Sep 17 00:00:00 2001 From: Andrew Kaster Date: Fri, 23 Aug 2024 12:22:26 -0600 Subject: [PATCH] AK+Meta: Add SwiftAK module to add helpers to construct swift Strings This allows constructing Foundation.Data and Swift.String without unnecessary copies from AK.StringView and AK.String respectively. --- AK/AK+Swift.swift | 25 ++++++++++++++++++++++ AK/CMakeLists.txt | 4 ++++ Meta/CMake/Swift/GenerateSwiftHeader.cmake | 2 ++ Meta/CMake/lagom_install_options.cmake | 4 ++++ 4 files changed, 35 insertions(+) create mode 100644 AK/AK+Swift.swift diff --git a/AK/AK+Swift.swift b/AK/AK+Swift.swift new file mode 100644 index 0000000000..6175aee59d --- /dev/null +++ b/AK/AK+Swift.swift @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2024, Andrew Kaster + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +import AK +import Foundation + +public extension Foundation.Data { + init(_ string: AK.StringView) { + let bytes = string.bytes() + self.init(bytesNoCopy: UnsafeMutableRawPointer(mutating: bytes.data()), count: bytes.size(), deallocator: .none) + } +} + +public extension Swift.String { + init?(_ string: AK.String) { + self.init(data: Foundation.Data(string.__bytes_as_string_viewUnsafe()), encoding: .utf8) + } + + init?(_ string: AK.StringView) { + self.init(data: Foundation.Data(string), encoding: .utf8) + } +} diff --git a/AK/CMakeLists.txt b/AK/CMakeLists.txt index 02cc2c9d42..cfa16efdf5 100644 --- a/AK/CMakeLists.txt +++ b/AK/CMakeLists.txt @@ -68,4 +68,8 @@ if (ENABLE_SWIFT) "${CMAKE_CURRENT_BINARY_DIR}/Backtrace.h" "${CMAKE_CURRENT_BINARY_DIR}/Debug.h" ) + + target_compile_features(AK PUBLIC cxx_std_23) + set_target_properties(AK PROPERTIES Swift_MODULE_NAME SwiftAK) + target_sources(AK PRIVATE AK+Swift.swift) endif() diff --git a/Meta/CMake/Swift/GenerateSwiftHeader.cmake b/Meta/CMake/Swift/GenerateSwiftHeader.cmake index 0182fa52a0..b7d09dac97 100644 --- a/Meta/CMake/Swift/GenerateSwiftHeader.cmake +++ b/Meta/CMake/Swift/GenerateSwiftHeader.cmake @@ -36,10 +36,12 @@ function(_swift_generate_cxx_header target header) if(APPLE) set(SDK_FLAGS "-sdk" "${CMAKE_OSX_SYSROOT}") + list(APPEND SDK_FLAGS "-target" "${CMAKE_Swift_COMPILER_TARGET}") elseif(WIN32) set(SDK_FLAGS "-sdk" "$ENV{SDKROOT}") elseif(DEFINED ${CMAKE_SYSROOT}) set(SDK_FLAGS "-sdk" "${CMAKE_SYSROOT}") + list(APPEND SDK_FLAGS "-target" "${CMAKE_Swift_COMPILER_TARGET}") endif() cmake_path(APPEND CMAKE_CURRENT_BINARY_DIR include diff --git a/Meta/CMake/lagom_install_options.cmake b/Meta/CMake/lagom_install_options.cmake index 2e60a0ff75..ea0d526d92 100644 --- a/Meta/CMake/lagom_install_options.cmake +++ b/Meta/CMake/lagom_install_options.cmake @@ -15,6 +15,10 @@ set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/${IN_BUILD_PREFIX}${CMAK set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/${IN_BUILD_PREFIX}${CMAKE_INSTALL_LIBDIR}") set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/${IN_BUILD_PREFIX}${CMAKE_INSTALL_LIBDIR}") +# FIXME: Stop setting this when we have a good way to retrieve the directory that has the swift module +# file for use by the swift frontend's header generator +set(CMAKE_Swift_MODULE_DIRECTORY "${CMAKE_BINARY_DIR}/${IN_BUILD_PREFIX}swift") + set(CMAKE_SKIP_BUILD_RPATH FALSE) set(CMAKE_BUILD_WITH_INSTALL_RPATH TRUE) # See slide 100 of the following ppt :^)