Ver código fonte

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.
Andrew Kaster 10 meses atrás
pai
commit
c5153cb398

+ 1 - 1
AK/AK+Swift.swift

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

+ 2 - 5
AK/CMakeLists.txt

@@ -62,14 +62,11 @@ swizzle_target_properties_for_swift(simdutf::simdutf)
 target_link_libraries(AK PRIVATE simdutf::simdutf)
 
 if (ENABLE_SWIFT)
-    generate_clang_module_map(
-       AK
+    generate_clang_module_map(AK
        GENERATED_FILES
         "${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)
+    add_swift_target_properties(AK)
 endif()

+ 28 - 0
Meta/CMake/Swift/swift-settings.cmake

@@ -23,6 +23,8 @@ if (APPLE)
     set(CMAKE_Swift_COMPILER_TARGET "${CMAKE_SYSTEM_PROCESSOR}-apple-macosx${CMAKE_OSX_DEPLOYMENT_TARGET}")
 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
 # For now, we'll just manually massage the flags.
 function(swizzle_target_properties_for_swift target_name)
@@ -35,3 +37,29 @@ function(swizzle_target_properties_for_swift target_name)
     endforeach()
     set_property(TARGET ${target_name} PROPERTY INTERFACE_COMPILE_OPTIONS ${munged_properties})
 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()

+ 10 - 2
Meta/CMake/code_generators.cmake

@@ -29,14 +29,22 @@ function(generate_clang_module_map target_name)
         set(MODULE_MAP_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}")
     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(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)
     # FIXME: Make this depend on the public headers of the target
     add_custom_command(
         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
         DEPENDS "${SerenityOS_SOURCE_DIR}/Meta/generate_clang_module_map.py"
     )

+ 3 - 1
Meta/generate_clang_module_map.py

@@ -27,6 +27,7 @@ def main():
                  formatter_class=argparse.RawDescriptionHelpFormatter)
     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('-n', '--module-name', help='top-level module name')
     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')
     args = parser.parse_args()
@@ -36,9 +37,10 @@ def main():
         print(f"Error: {args.directory} is not a directory", file=sys.stderr)
         return 1
     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()]
-    module_name = root.name
+    module_name = args.module_name if args.module_name else root.name
 
     module_map = f"module {module_name} {{\n"
     for header_file in header_files:

+ 1 - 2
Tests/LibWeb/TestHTMLTokenizerSwift.swift

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

+ 1 - 1
Tests/LibWeb/TestLibWebSwiftBindings.swift

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

+ 1 - 24
Userland/Libraries/LibGfx/CMakeLists.txt

@@ -116,32 +116,9 @@ target_link_libraries(LibGfx PRIVATE PkgConfig::WOFF2 JPEG::JPEG PkgConfig::Jxl
 
 if (ENABLE_SWIFT)
     generate_clang_module_map(LibGfx GENERATED_FILES ${generated_headers})
-
-    target_compile_features(LibGfx PUBLIC cxx_std_23)
-
     target_sources(LibGfx PRIVATE
         Color.swift
     )
-    target_compile_definitions(LibGfx PRIVATE LIBGFX_USE_SWIFT)
     target_link_libraries(LibGfx PRIVATE AK)
-    set_target_properties(LibGfx PROPERTIES Swift_MODULE_NAME "SwiftLibGfx")
-
-    # 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()
+    add_swift_target_properties(LibGfx LAGOM_LIBRARIES AK)
 endif()

+ 1 - 1
Userland/Libraries/LibGfx/Color.cpp

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

+ 2 - 2
Userland/Libraries/LibGfx/Color.swift

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

+ 1 - 26
Userland/Libraries/LibWeb/CMakeLists.txt

@@ -800,36 +800,11 @@ if (ENABLE_SWIFT)
 
     generate_clang_module_map(LibWeb GENERATED_FILES ${LIBWEB_ALL_GENERATED_HEADERS})
 
-    target_compile_features(LibWeb PUBLIC cxx_std_23)
-
     target_sources(LibWeb PRIVATE
         HTML/Parser/HTMLToken.swift
         HTML/Parser/HTMLTokenizer.swift
         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)
-
-    # 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()
+    add_swift_target_properties(LibWeb LAGOM_LIBRARIES AK LibGfx)
 endif()

+ 15 - 13
Userland/Libraries/LibWeb/HTML/Parser/HTMLToken.swift

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

+ 2 - 2
Userland/Libraries/LibWeb/HTML/Parser/HTMLTokenizer.swift

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