Meta+Libraries+AK: Append Cxx to imported library module names in swift

At the same time, simplify CMakeLists magic for libraries that want to
include Swift code in the library. The Lib-less name of the library is
now always the module name for the library with any Swift additions,
extensions, etc. All vfs overlays now live in a common location to make
finding them easier from CMake functions. A new pattern is needed for
the Lib-less modules to re-export their Cxx counterparts.
This commit is contained in:
Andrew Kaster 2024-08-26 19:04:05 -06:00 committed by Andrew Kaster
parent f27d638e0a
commit c5153cb398
Notes: github-actions[bot] 2024-08-27 23:23:23 +00:00
13 changed files with 68 additions and 80 deletions

View file

@ -4,7 +4,7 @@
* SPDX-License-Identifier: BSD-2-Clause * SPDX-License-Identifier: BSD-2-Clause
*/ */
import AK @_exported import AKCxx
import Foundation import Foundation
public extension Foundation.Data { public extension Foundation.Data {

View file

@ -62,14 +62,11 @@ swizzle_target_properties_for_swift(simdutf::simdutf)
target_link_libraries(AK PRIVATE simdutf::simdutf) target_link_libraries(AK PRIVATE simdutf::simdutf)
if (ENABLE_SWIFT) if (ENABLE_SWIFT)
generate_clang_module_map( generate_clang_module_map(AK
AK
GENERATED_FILES GENERATED_FILES
"${CMAKE_CURRENT_BINARY_DIR}/Backtrace.h" "${CMAKE_CURRENT_BINARY_DIR}/Backtrace.h"
"${CMAKE_CURRENT_BINARY_DIR}/Debug.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) target_sources(AK PRIVATE AK+Swift.swift)
add_swift_target_properties(AK)
endif() endif()

View file

@ -23,6 +23,8 @@ if (APPLE)
set(CMAKE_Swift_COMPILER_TARGET "${CMAKE_SYSTEM_PROCESSOR}-apple-macosx${CMAKE_OSX_DEPLOYMENT_TARGET}") set(CMAKE_Swift_COMPILER_TARGET "${CMAKE_SYSTEM_PROCESSOR}-apple-macosx${CMAKE_OSX_DEPLOYMENT_TARGET}")
endif() endif()
set(VFS_OVERLAY_DIRECTORY "${CMAKE_BINARY_DIR}/vfs_overlays" CACHE PATH "Directory to put VFS overlays in")
# FIXME: https://gitlab.kitware.com/cmake/cmake/-/issues/26195 # FIXME: https://gitlab.kitware.com/cmake/cmake/-/issues/26195
# For now, we'll just manually massage the flags. # For now, we'll just manually massage the flags.
function(swizzle_target_properties_for_swift target_name) function(swizzle_target_properties_for_swift target_name)
@ -35,3 +37,29 @@ function(swizzle_target_properties_for_swift target_name)
endforeach() endforeach()
set_property(TARGET ${target_name} PROPERTY INTERFACE_COMPILE_OPTIONS ${munged_properties}) set_property(TARGET ${target_name} PROPERTY INTERFACE_COMPILE_OPTIONS ${munged_properties})
endfunction() endfunction()
function(add_swift_target_properties target_name)
cmake_parse_arguments(PARSE_ARGV 1 SWIFT_TARGET "" "" "LAGOM_LIBRARIES")
target_compile_features(${target_name} PUBLIC cxx_std_${CMAKE_CXX_STANDARD})
string(REPLACE "Lib" "" module_name ${target_name})
string(TOUPPER ${target_name} TARGET_NAME_UPPER)
target_compile_definitions(${target_name} PRIVATE "${TARGET_NAME_UPPER}_USE_SWIFT")
set_target_properties(${target_name} PROPERTIES Swift_MODULE_NAME ${module_name})
# FIXME: These should be pulled automatically from interface compile options for the target
set(VFS_OVERLAY_OPTIONS "-Xcc" "-ivfsoverlay${VFS_OVERLAY_DIRECTORY}/${target_name}_vfs_overlay.yaml")
foreach(internal_library IN LISTS SWIFT_TARGET_LAGOM_LIBRARIES)
list(APPEND VFS_OVERLAY_OPTIONS "-Xcc" "-ivfsoverlay${VFS_OVERLAY_DIRECTORY}/${internal_library}_vfs_overlay.yaml")
endforeach()
get_target_property(_NATIVE_DIRS ${target_name} INCLUDE_DIRECTORIES)
list(APPEND _NATIVE_DIRS ${CMAKE_Swift_MODULE_DIRECTORY})
_swift_generate_cxx_header(${target_name} "${target_name}-Swift.h"
SEARCH_PATHS ${_NATIVE_DIRS}
COMPILE_OPTIONS ${VFS_OVERLAY_OPTIONS}
)
endfunction()

View file

@ -29,14 +29,22 @@ function(generate_clang_module_map target_name)
set(MODULE_MAP_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}") set(MODULE_MAP_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}")
endif() endif()
string(REPLACE "Lib" "" module_name ${target_name})
set(module_name "${module_name}Cxx")
set(module_map_file "${CMAKE_CURRENT_BINARY_DIR}/module/module.modulemap") set(module_map_file "${CMAKE_CURRENT_BINARY_DIR}/module/module.modulemap")
set(vfs_overlay_file "${CMAKE_CURRENT_BINARY_DIR}/vfs_overlay.yaml") set(vfs_overlay_file "${VFS_OVERLAY_DIRECTORY}/${target_name}_vfs_overlay.yaml")
find_package(Python3 REQUIRED COMPONENTS Interpreter) find_package(Python3 REQUIRED COMPONENTS Interpreter)
# FIXME: Make this depend on the public headers of the target # FIXME: Make this depend on the public headers of the target
add_custom_command( add_custom_command(
OUTPUT "${module_map_file}" OUTPUT "${module_map_file}"
COMMAND "${Python3_EXECUTABLE}" "${SerenityOS_SOURCE_DIR}/Meta/generate_clang_module_map.py" "${MODULE_MAP_DIRECTORY}" --module-map "${module_map_file}" --vfs-map ${vfs_overlay_file} ${MODULE_MAP_GENERATED_FILES} COMMAND "${Python3_EXECUTABLE}" "${SerenityOS_SOURCE_DIR}/Meta/generate_clang_module_map.py"
"${MODULE_MAP_DIRECTORY}"
--module-name "${module_name}"
--module-map "${module_map_file}"
--vfs-map ${vfs_overlay_file}
${MODULE_MAP_GENERATED_FILES}
VERBATIM VERBATIM
DEPENDS "${SerenityOS_SOURCE_DIR}/Meta/generate_clang_module_map.py" DEPENDS "${SerenityOS_SOURCE_DIR}/Meta/generate_clang_module_map.py"
) )

View file

@ -27,6 +27,7 @@ def main():
formatter_class=argparse.RawDescriptionHelpFormatter) formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument('directory', help='source directory to generate module map for') parser.add_argument('directory', help='source directory to generate module map for')
parser.add_argument('generated_files', nargs='+', help='extra files to include in the module map') parser.add_argument('generated_files', nargs='+', help='extra files to include in the module map')
parser.add_argument('-n', '--module-name', help='top-level module name')
parser.add_argument('-m', '--module-map', required=True, help='output module map file') parser.add_argument('-m', '--module-map', required=True, help='output module map file')
parser.add_argument('-v', '--vfs-map', required=True, help='output VFS map file') parser.add_argument('-v', '--vfs-map', required=True, help='output VFS map file')
args = parser.parse_args() args = parser.parse_args()
@ -36,9 +37,10 @@ def main():
print(f"Error: {args.directory} is not a directory", file=sys.stderr) print(f"Error: {args.directory} is not a directory", file=sys.stderr)
return 1 return 1
pathlib.Path(args.module_map).parent.mkdir(parents=True, exist_ok=True) pathlib.Path(args.module_map).parent.mkdir(parents=True, exist_ok=True)
pathlib.Path(args.vfs_map).parent.mkdir(parents=True, exist_ok=True)
header_files = [f for f in root.rglob('**/*.h') if f.is_file()] header_files = [f for f in root.rglob('**/*.h') if f.is_file()]
module_name = root.name module_name = args.module_name if args.module_name else root.name
module_map = f"module {module_name} {{\n" module_map = f"module {module_name} {{\n"
for header_file in header_files: for header_file in header_files:

View file

@ -5,8 +5,7 @@
*/ */
import AK import AK
import LibWeb import Web
import SwiftLibWeb
import Foundation import Foundation
class StandardError: TextOutputStream { class StandardError: TextOutputStream {

View file

@ -5,7 +5,7 @@
*/ */
import AK import AK
import LibWeb import Web
import Foundation import Foundation
class StandardError: TextOutputStream { class StandardError: TextOutputStream {

View file

@ -116,32 +116,9 @@ target_link_libraries(LibGfx PRIVATE PkgConfig::WOFF2 JPEG::JPEG PkgConfig::Jxl
if (ENABLE_SWIFT) if (ENABLE_SWIFT)
generate_clang_module_map(LibGfx GENERATED_FILES ${generated_headers}) generate_clang_module_map(LibGfx GENERATED_FILES ${generated_headers})
target_compile_features(LibGfx PUBLIC cxx_std_23)
target_sources(LibGfx PRIVATE target_sources(LibGfx PRIVATE
Color.swift Color.swift
) )
target_compile_definitions(LibGfx PRIVATE LIBGFX_USE_SWIFT)
target_link_libraries(LibGfx PRIVATE AK) target_link_libraries(LibGfx PRIVATE AK)
set_target_properties(LibGfx PROPERTIES Swift_MODULE_NAME "SwiftLibGfx") add_swift_target_properties(LibGfx LAGOM_LIBRARIES AK)
# FIXME: These should be pulled automatically from interface compile options for the target
set(VFS_OVERLAY_OPTIONS
-Xcc -ivfsoverlay${CMAKE_CURRENT_BINARY_DIR}/vfs_overlay.yaml
-Xcc -ivfsoverlay${Lagom_BINARY_DIR}/AK/vfs_overlay.yaml
)
get_target_property(LIBGFX_NATIVE_DIRS LibGfx INCLUDE_DIRECTORIES)
list(APPEND LIBGFX_NATIVE_DIRS ${CMAKE_Swift_MODULE_DIRECTORY})
_swift_generate_cxx_header(LibGfx "LibGfx-Swift.h"
SEARCH_PATHS ${LIBGFX_NATIVE_DIRS}
COMPILE_OPTIONS ${VFS_OVERLAY_OPTIONS}
)
# FIXME: https://gitlab.kitware.com/cmake/cmake/-/issues/26175
if (APPLE)
add_custom_command(TARGET LibGfx POST_BUILD
COMMAND install_name_tool -id @rpath/liblagom-gfx.0.dylib "$<TARGET_FILE:LibGfx>"
)
endif()
endif() endif()

View file

@ -270,7 +270,7 @@ Optional<Color> Color::from_named_css_color_string(StringView string)
#if defined(LIBGFX_USE_SWIFT) #if defined(LIBGFX_USE_SWIFT)
static Optional<Color> hex_string_to_color(StringView string) static Optional<Color> hex_string_to_color(StringView string)
{ {
auto color = SwiftLibGfx::parseHexString(string); auto color = parseHexString(string);
if (color.getCount() == 0) if (color.getCount() == 0)
return {}; return {};
return color[0]; return color[0];

View file

@ -4,8 +4,8 @@
* SPDX-License-Identifier: BSD-2-Clause * SPDX-License-Identifier: BSD-2-Clause
*/ */
import SwiftAK import AK
import LibGfx @_exported import GfxCxx
// FIXME: Do this without extending String with an index operation that was explicitly deleted :^) // FIXME: Do this without extending String with an index operation that was explicitly deleted :^)
extension Swift.String { extension Swift.String {

View file

@ -800,36 +800,11 @@ if (ENABLE_SWIFT)
generate_clang_module_map(LibWeb GENERATED_FILES ${LIBWEB_ALL_GENERATED_HEADERS}) generate_clang_module_map(LibWeb GENERATED_FILES ${LIBWEB_ALL_GENERATED_HEADERS})
target_compile_features(LibWeb PUBLIC cxx_std_23)
target_sources(LibWeb PRIVATE target_sources(LibWeb PRIVATE
HTML/Parser/HTMLToken.swift HTML/Parser/HTMLToken.swift
HTML/Parser/HTMLTokenizer.swift HTML/Parser/HTMLTokenizer.swift
HTML/Parser/HTMLTokenizerHelpers.cpp HTML/Parser/HTMLTokenizerHelpers.cpp
) )
target_compile_definitions(LibWeb PRIVATE LIBWEB_USE_SWIFT)
set_target_properties(LibWeb PROPERTIES Swift_MODULE_NAME "SwiftLibWeb")
target_link_libraries(LibWeb PRIVATE AK Collections) target_link_libraries(LibWeb PRIVATE AK Collections)
add_swift_target_properties(LibWeb LAGOM_LIBRARIES AK LibGfx)
# FIXME: These should be pulled automatically from interface compile options for the target
set(VFS_OVERLAY_OPTIONS
-Xcc -ivfsoverlay${CMAKE_CURRENT_BINARY_DIR}/vfs_overlay.yaml
-Xcc -ivfsoverlay${CMAKE_CURRENT_BINARY_DIR}/../LibGfx/vfs_overlay.yaml
-Xcc -ivfsoverlay${Lagom_BINARY_DIR}/AK/vfs_overlay.yaml
)
get_target_property(LIBWEB_NATIVE_DIRS LibWeb INCLUDE_DIRECTORIES)
list(APPEND LIBWEB_NATIVE_DIRS ${CMAKE_Swift_MODULE_DIRECTORY})
_swift_generate_cxx_header(LibWeb "LibWeb-Swift.h"
SEARCH_PATHS ${LIBWEB_NATIVE_DIRS}
COMPILE_OPTIONS ${VFS_OVERLAY_OPTIONS}
)
# FIXME: https://gitlab.kitware.com/cmake/cmake/-/issues/26175
if (APPLE)
add_custom_command(TARGET LibWeb POST_BUILD
COMMAND install_name_tool -id @rpath/liblagom-web.0.dylib "$<TARGET_FILE:LibWeb>"
)
endif()
endif() endif()

View file

@ -4,6 +4,8 @@
* SPDX-License-Identifier: BSD-2-Clause * SPDX-License-Identifier: BSD-2-Clause
*/ */
@_exported import WebCxx
public class HTMLToken { public class HTMLToken {
public struct Position { public struct Position {
var line = UInt() var line = UInt()
@ -12,10 +14,10 @@ public class HTMLToken {
} }
public struct Attribute { public struct Attribute {
var prefix: String? var prefix: Swift.String?
var localName: String var localName: Swift.String
var namespace_: String? var namespace_: Swift.String?
var value: String var value: Swift.String
var nameStartPosition: Position var nameStartPosition: Position
var nameEndPosition: Position var nameEndPosition: Position
var valueStartPosition: Position var valueStartPosition: Position
@ -25,21 +27,21 @@ public class HTMLToken {
public enum TokenType { public enum TokenType {
case Invalid case Invalid
case DOCTYPE( case DOCTYPE(
name: String?, name: Swift.String?,
publicIdentifier: String?, publicIdentifier: Swift.String?,
systemIdentifier: String?, systemIdentifier: Swift.String?,
forceQuirksMode: Bool) forceQuirksMode: Bool)
case StartTag( case StartTag(
tagName: String, tagName: Swift.String,
selfClosing: Bool, selfClosing: Bool,
selfClosingAcknowledged: Bool, selfClosingAcknowledged: Bool,
attributes: [Attribute]) attributes: [Attribute])
case EndTag( case EndTag(
tagName: String, tagName: Swift.String,
selfClosing: Bool, selfClosing: Bool,
selfClosingAcknowledged: Bool, selfClosingAcknowledged: Bool,
attributes: [Attribute]) attributes: [Attribute])
case Comment(data: String) case Comment(data: Swift.String)
case Character(codePoint: Character) case Character(codePoint: Character)
case EndOfFile case EndOfFile
} }
@ -78,14 +80,14 @@ public class HTMLToken {
} }
extension HTMLToken.Position: Equatable, CustomStringConvertible { extension HTMLToken.Position: Equatable, CustomStringConvertible {
public var description: String { public var description: Swift.String {
return "\(self.line):\(self.column)" return "\(self.line):\(self.column)"
} }
} }
extension HTMLToken.TokenType: CustomStringConvertible { extension HTMLToken.TokenType: CustomStringConvertible {
// FIXME: Print attributes for start/end tags // FIXME: Print attributes for start/end tags
public var description: String { public var description: Swift.String {
switch self { switch self {
case .Invalid: case .Invalid:
return "Invalid" return "Invalid"
@ -106,7 +108,7 @@ extension HTMLToken.TokenType: CustomStringConvertible {
} }
extension HTMLToken: CustomStringConvertible { extension HTMLToken: CustomStringConvertible {
public var description: String { public var description: Swift.String {
if (self.startPosition == Position()) { if (self.startPosition == Position()) {
return "HTMLToken(type: \(self.type))" return "HTMLToken(type: \(self.type))"
} }

View file

@ -4,10 +4,10 @@
* SPDX-License-Identifier: BSD-2-Clause * SPDX-License-Identifier: BSD-2-Clause
*/ */
import AK
import Collections import Collections
import Foundation import Foundation
import LibWeb @_exported import WebCxx
import SwiftAK
extension Swift.String { extension Swift.String {
public init?(decoding: AK.StringView, as: AK.StringView) { public init?(decoding: AK.StringView, as: AK.StringView) {