From aeb508a9f0388a8605bacd5363c3c6efbb343bf1 Mon Sep 17 00:00:00 2001 From: Max Desiatov Date: Thu, 5 Oct 2023 15:44:48 +0100 Subject: [PATCH 1/2] LLBuildManifest: add `WriteAuxiliary.EntitlementPlist` This new auxiliary file type allows writing an entitlements plist file during the build process. Related to rdar://112065568. --- .../xcschemes/SwiftPM-Package.xcscheme | 10 +++++ Package.swift | 4 ++ Sources/LLBuildManifest/BuildManifest.swift | 41 ++++++++++++++++++- Sources/LLBuildManifest/Node.swift | 6 +++ Sources/LLBuildManifest/Tools.swift | 2 +- .../LLBuildManifestTests.swift | 35 +++++++++++++--- 6 files changed, 90 insertions(+), 8 deletions(-) rename Tests/{BuildTests => LLBuildManifestTests}/LLBuildManifestTests.swift (75%) diff --git a/.swiftpm/xcode/xcshareddata/xcschemes/SwiftPM-Package.xcscheme b/.swiftpm/xcode/xcshareddata/xcschemes/SwiftPM-Package.xcscheme index 91aaea3c058..780e7e3820f 100644 --- a/.swiftpm/xcode/xcshareddata/xcschemes/SwiftPM-Package.xcscheme +++ b/.swiftpm/xcode/xcshareddata/xcschemes/SwiftPM-Package.xcscheme @@ -829,6 +829,16 @@ ReferencedContainer = "container:"> + + + + [Node] { + [.virtual(Self.name), .virtual(entitlement)] + } + + public static func getFileContents(inputs: [Node]) throws -> String { + guard let entitlementName = inputs.last?.extractedVirtualNodeName else { + throw Error.undefinedEntitlementName + } + let encoder = PropertyListEncoder() + encoder.outputFormat = .xml + let result = try encoder.encode([entitlementName: true]) + + let contents = String(decoding: result, as: UTF8.self) + return contents + } + + private enum Error: Swift.Error { + case undefinedEntitlementName + } + } public struct LinkFileList: AuxiliaryFileType { public static let name = "link-file-list" @@ -108,7 +138,7 @@ public enum WriteAuxiliary { } public static func getFileContents(inputs: [Node]) throws -> String { - guard let principalClass = inputs.last?.name.dropFirst().dropLast() else { + guard let principalClass = inputs.last?.extractedVirtualNodeName else { throw Error.undefinedPrincipalClass } @@ -206,6 +236,13 @@ public struct BuildManifest { commands[name] = Command(name: name, tool: tool) } + public mutating func addEntitlementPlistCommand(entitlement: String, outputPath: AbsolutePath) { + let inputs = WriteAuxiliary.EntitlementPlist.computeInputs(entitlement: entitlement) + let tool = WriteAuxiliaryFile(inputs: inputs, outputFilePath: outputPath) + let name = outputPath.pathString + commands[name] = Command(name: name, tool: tool) + } + public mutating func addWriteLinkFileListCommand( objects: [AbsolutePath], linkFileListPath: AbsolutePath diff --git a/Sources/LLBuildManifest/Node.swift b/Sources/LLBuildManifest/Node.swift index 9800cb34e64..f8ecfca2123 100644 --- a/Sources/LLBuildManifest/Node.swift +++ b/Sources/LLBuildManifest/Node.swift @@ -30,6 +30,12 @@ public struct Node: Hashable, Codable { self.name = name self.kind = kind } + + /// Extracts `name` property if this node was constructed as `Node//virtual`. + public var extractedVirtualNodeName: String { + precondition(kind == .virtual) + return String(self.name.dropFirst().dropLast()) + } public static func virtual(_ name: String) -> Node { precondition(name.first != "<" && name.last != ">", "<> will be inserted automatically") diff --git a/Sources/LLBuildManifest/Tools.swift b/Sources/LLBuildManifest/Tools.swift index 7b641d539d9..b4ea180f4fa 100644 --- a/Sources/LLBuildManifest/Tools.swift +++ b/Sources/LLBuildManifest/Tools.swift @@ -155,7 +155,7 @@ public struct ShellTool: ToolProtocol { } } -public struct WriteAuxiliaryFile: ToolProtocol { +public struct WriteAuxiliaryFile: Equatable, ToolProtocol { public static let name: String = "write-auxiliary-file" public let inputs: [Node] diff --git a/Tests/BuildTests/LLBuildManifestTests.swift b/Tests/LLBuildManifestTests/LLBuildManifestTests.swift similarity index 75% rename from Tests/BuildTests/LLBuildManifestTests.swift rename to Tests/LLBuildManifestTests/LLBuildManifestTests.swift index becb6aacbb5..248c3de56be 100644 --- a/Tests/BuildTests/LLBuildManifestTests.swift +++ b/Tests/LLBuildManifestTests/LLBuildManifestTests.swift @@ -2,7 +2,7 @@ // // This source file is part of the Swift open source project // -// Copyright (c) 2014-2021 Apple Inc. and the Swift project authors +// Copyright (c) 2020 Apple Inc. and the Swift project authors // Licensed under Apache License v2.0 with Runtime Library Exception // // See http://swift.org/LICENSE.txt for license information @@ -10,14 +10,39 @@ // //===----------------------------------------------------------------------===// -import Basics -import LLBuildManifest +import struct Basics.AbsolutePath +import class Foundation.PropertyListDecoder +@testable import LLBuildManifest +import SPMTestSupport +import class TSCBasic.InMemoryFileSystem import XCTest -import class TSCBasic.InMemoryFileSystem -// FIXME: This should be in its own test target. +private let testEntitlement = "test-entitlement" + final class LLBuildManifestTests: XCTestCase { + func testEntitlementsPlist() throws { + let FileType = WriteAuxiliary.EntitlementPlist.self + let inputs = FileType.computeInputs(entitlement: testEntitlement) + XCTAssertEqual(inputs, [.virtual(FileType.name), .virtual(testEntitlement)]) + + let contents = try FileType.getFileContents(inputs: inputs) + let decoder = PropertyListDecoder() + let decodedEntitlements = try decoder.decode([String: Bool].self, from: .init(contents.utf8)) + XCTAssertEqual(decodedEntitlements, [testEntitlement: true]) + + var manifest = BuildManifest() + let outputPath = AbsolutePath("/test.plist") + manifest.addEntitlementPlistCommand(entitlement: testEntitlement, outputPath: outputPath) + + let commandName = outputPath.pathString + XCTAssertEqual(manifest.commands.count, 1) + + let command = try XCTUnwrap(manifest.commands[commandName]?.tool as? WriteAuxiliaryFile) + + XCTAssertEqual(command, .init(inputs: inputs, outputFilePath: outputPath)) + } + func testBasics() throws { var manifest = BuildManifest() From dcb3b001f59d1b8e84f1a9dd1157931cf0a7b45e Mon Sep 17 00:00:00 2001 From: Max Desiatov Date: Thu, 5 Oct 2023 15:49:42 +0100 Subject: [PATCH 2/2] Update copyright years --- Sources/LLBuildManifest/BuildManifest.swift | 2 +- Sources/LLBuildManifest/Node.swift | 2 +- Sources/LLBuildManifest/Tools.swift | 2 +- Tests/LLBuildManifestTests/LLBuildManifestTests.swift | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Sources/LLBuildManifest/BuildManifest.swift b/Sources/LLBuildManifest/BuildManifest.swift index e72c0c056b9..4a84be4c0f9 100644 --- a/Sources/LLBuildManifest/BuildManifest.swift +++ b/Sources/LLBuildManifest/BuildManifest.swift @@ -2,7 +2,7 @@ // // This source file is part of the Swift open source project // -// Copyright (c) 2014-2021 Apple Inc. and the Swift project authors +// Copyright (c) 2014-2023 Apple Inc. and the Swift project authors // Licensed under Apache License v2.0 with Runtime Library Exception // // See http://swift.org/LICENSE.txt for license information diff --git a/Sources/LLBuildManifest/Node.swift b/Sources/LLBuildManifest/Node.swift index f8ecfca2123..82b5bfbe19e 100644 --- a/Sources/LLBuildManifest/Node.swift +++ b/Sources/LLBuildManifest/Node.swift @@ -2,7 +2,7 @@ // // This source file is part of the Swift open source project // -// Copyright (c) 2014-2019 Apple Inc. and the Swift project authors +// Copyright (c) 2014-2023 Apple Inc. and the Swift project authors // Licensed under Apache License v2.0 with Runtime Library Exception // // See http://swift.org/LICENSE.txt for license information diff --git a/Sources/LLBuildManifest/Tools.swift b/Sources/LLBuildManifest/Tools.swift index b4ea180f4fa..b9bf571cbf8 100644 --- a/Sources/LLBuildManifest/Tools.swift +++ b/Sources/LLBuildManifest/Tools.swift @@ -2,7 +2,7 @@ // // This source file is part of the Swift open source project // -// Copyright (c) 2014-2021 Apple Inc. and the Swift project authors +// Copyright (c) 2014-2023 Apple Inc. and the Swift project authors // Licensed under Apache License v2.0 with Runtime Library Exception // // See http://swift.org/LICENSE.txt for license information diff --git a/Tests/LLBuildManifestTests/LLBuildManifestTests.swift b/Tests/LLBuildManifestTests/LLBuildManifestTests.swift index 248c3de56be..24b5be4bf6e 100644 --- a/Tests/LLBuildManifestTests/LLBuildManifestTests.swift +++ b/Tests/LLBuildManifestTests/LLBuildManifestTests.swift @@ -2,7 +2,7 @@ // // This source file is part of the Swift open source project // -// Copyright (c) 2020 Apple Inc. and the Swift project authors +// Copyright (c) 2014-2023 Apple Inc. and the Swift project authors // Licensed under Apache License v2.0 with Runtime Library Exception // // See http://swift.org/LICENSE.txt for license information