From 4ca0cdf939113ef1d1e585bdc630f462b7a625f1 Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Sun, 20 Mar 2022 12:14:35 +0900 Subject: [PATCH] Add -Xswiftc option for each build commands (#300) --- Dockerfile | 3 ++- Sources/CartonCLI/Commands/Bundle.swift | 5 +++- Sources/CartonCLI/Commands/Dev.swift | 6 ++++- Sources/CartonCLI/Commands/Options.swift | 24 +++++++++++++++++++ Sources/CartonCLI/Commands/Test.swift | 5 +++- Sources/SwiftToolchain/BuildFlavor.swift | 9 ++++++- Sources/SwiftToolchain/Toolchain.swift | 8 +++++++ .../BundleCommandTests.swift | 20 ++++++++++++++++ 8 files changed, 75 insertions(+), 5 deletions(-) create mode 100644 Sources/CartonCLI/Commands/Options.swift diff --git a/Dockerfile b/Dockerfile index ccb4f3e..4d3d786 100644 --- a/Dockerfile +++ b/Dockerfile @@ -10,8 +10,9 @@ RUN export DEBIAN_FRONTEND=noninteractive DEBCONF_NONINTERACTIVE_SEEN=true && ap libncurses5 \ libsqlite3-0 \ libsqlite3-dev \ + libxkbcommon0 \ curl unzip \ - && export WASMER_DIR=/usr/local && curl https://get.wasmer.io -sSfL | sh && \ + && export WASMER_DIR=/usr/local && curl https://get.wasmer.io -sSfL | sh -s "2.2.1" && \ rm -r /var/lib/apt/lists/* ENV CARTON_ROOT=/root/.carton diff --git a/Sources/CartonCLI/Commands/Bundle.swift b/Sources/CartonCLI/Commands/Bundle.swift index f84ef0d..6dcc9ec 100644 --- a/Sources/CartonCLI/Commands/Bundle.swift +++ b/Sources/CartonCLI/Commands/Bundle.swift @@ -39,6 +39,9 @@ struct Bundle: AsyncParsableCommand { @Flag(help: "When specified, build in the debug mode.") var debug = false + @OptionGroup() + var buildOptions: BuildOptions + static let configuration = CommandConfiguration( abstract: "Produces an optimized app bundle for distribution." ) @@ -46,7 +49,7 @@ struct Bundle: AsyncParsableCommand { func buildFlavor() -> BuildFlavor { BuildFlavor( isRelease: !debug, environment: .browser, - sanitize: nil + sanitize: nil, swiftCompilerFlags: buildOptions.swiftCompilerFlags ) } diff --git a/Sources/CartonCLI/Commands/Dev.swift b/Sources/CartonCLI/Commands/Dev.swift index e442dc9..4b21dfb 100644 --- a/Sources/CartonCLI/Commands/Dev.swift +++ b/Sources/CartonCLI/Commands/Dev.swift @@ -54,6 +54,9 @@ struct Dev: AsyncParsableCommand { @Flag(name: .long, help: "Skip automatically opening app in system browser.") var skipAutoOpen = false + @OptionGroup() + var buildOptions: BuildOptions + static let configuration = CommandConfiguration( abstract: "Watch the current directory, host the app, rebuild on change." ) @@ -62,7 +65,8 @@ struct Dev: AsyncParsableCommand { let defaultSanitize: SanitizeVariant? = release ? nil : .stackOverflow return BuildFlavor( isRelease: release, environment: .browser, - sanitize: sanitize ?? defaultSanitize + sanitize: sanitize ?? defaultSanitize, + swiftCompilerFlags: buildOptions.swiftCompilerFlags ) } diff --git a/Sources/CartonCLI/Commands/Options.swift b/Sources/CartonCLI/Commands/Options.swift new file mode 100644 index 0000000..159dbf6 --- /dev/null +++ b/Sources/CartonCLI/Commands/Options.swift @@ -0,0 +1,24 @@ +// Copyright 2022 Carton contributors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +import ArgumentParser + +struct BuildOptions: ParsableArguments { + @Option(name: .customLong("Xswiftc", withSingleDash: true), + parsing: .unconditionalSingleValue, + help: "Pass flag through to all Swift compiler invocations") + var swiftCompilerFlags: [String] = [] + + init() {} +} diff --git a/Sources/CartonCLI/Commands/Test.swift b/Sources/CartonCLI/Commands/Test.swift index 51ffbff..796cd27 100644 --- a/Sources/CartonCLI/Commands/Test.swift +++ b/Sources/CartonCLI/Commands/Test.swift @@ -66,10 +66,13 @@ struct Test: AsyncParsableCommand { ) var host = "127.0.0.1" + @OptionGroup() + var buildOptions: BuildOptions + func buildFlavor() -> BuildFlavor { BuildFlavor( isRelease: release, environment: environment.destination, - sanitize: sanitize + sanitize: sanitize, swiftCompilerFlags: buildOptions.swiftCompilerFlags ) } diff --git a/Sources/SwiftToolchain/BuildFlavor.swift b/Sources/SwiftToolchain/BuildFlavor.swift index 337f906..53c5da6 100644 --- a/Sources/SwiftToolchain/BuildFlavor.swift +++ b/Sources/SwiftToolchain/BuildFlavor.swift @@ -20,10 +20,17 @@ public struct BuildFlavor { public var isRelease: Bool public var environment: DestinationEnvironment public var sanitize: SanitizeVariant? + public var swiftCompilerFlags: [String] - public init(isRelease: Bool, environment: DestinationEnvironment, sanitize: SanitizeVariant?) { + public init( + isRelease: Bool, + environment: DestinationEnvironment, + sanitize: SanitizeVariant?, + swiftCompilerFlags: [String] + ) { self.isRelease = isRelease self.environment = environment self.sanitize = sanitize + self.swiftCompilerFlags = swiftCompilerFlags } } diff --git a/Sources/SwiftToolchain/Toolchain.swift b/Sources/SwiftToolchain/Toolchain.swift index a6b4e44..e70eef7 100644 --- a/Sources/SwiftToolchain/Toolchain.swift +++ b/Sources/SwiftToolchain/Toolchain.swift @@ -265,6 +265,10 @@ public final class Toolchain { builderArguments.append(contentsOf: ["-Xlinker", "-licuuc", "-Xlinker", "-licui18n"]) } + builderArguments.append(contentsOf: flavor.swiftCompilerFlags.flatMap { + ["-Xswiftc", $0] + }) + try await Builder( arguments: builderArguments, mainWasmPath: mainWasmPath, @@ -317,6 +321,10 @@ public final class Toolchain { builderArguments.append(contentsOf: ["-Xlinker", "-licuuc", "-Xlinker", "-licui18n"]) } + builderArguments.append(contentsOf: flavor.swiftCompilerFlags.flatMap { + ["-Xswiftc", $0] + }) + try await Builder( arguments: builderArguments, mainWasmPath: testBundlePath, diff --git a/Tests/CartonCommandTests/BundleCommandTests.swift b/Tests/CartonCommandTests/BundleCommandTests.swift index 8e9a9a4..404e820 100644 --- a/Tests/CartonCommandTests/BundleCommandTests.swift +++ b/Tests/CartonCommandTests/BundleCommandTests.swift @@ -57,4 +57,24 @@ final class BundleCommandTests: XCTestCase { try bundleDirectory.delete() try packageDirectory.appending(component: ".build").delete() } + + func testWithXswiftc() throws { + let package = "Milk" + let packageDirectory = testFixturesDirectory.appending(component: package) + + let bundle = "Bundle" + let bundleDirectory = packageDirectory.appending(component: bundle) + + // it's ok if there is nothing to delete + do { try bundleDirectory.delete() } catch {} + + AssertExecuteCommand( + command: "carton bundle -Xswiftc --fake-swiftc-options", + cwd: packageDirectory.url, + expected: "error: unknown argument: '--fake-swiftc-options'", + expectedContains: true + ) + + try packageDirectory.appending(component: ".build").delete() + } }