Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,13 @@ let package = Package(
dependencies: ["XCRemoteCache"]
),
.target(
name: "xclibtool",
name: "xclibtoolSupport",
dependencies: ["XCRemoteCache"]
),
.target(
name: "xclibtool",
dependencies: ["XCRemoteCache", "xclibtoolSupport"]
),
.target(
name: "xcpostbuild",
dependencies: ["XCRemoteCache"]
Expand Down Expand Up @@ -65,5 +69,9 @@ let package = Package(
dependencies: ["XCRemoteCache"],
resources: [.copy("TestData")]
),
.testTarget(
name: "xclibtoolSupportTests",
dependencies: ["xclibtoolSupport"]
),
]
)
2 changes: 1 addition & 1 deletion Sources/XCRemoteCache/Commands/Libtool/XCLibtool.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import Foundation

/// Represents a mode that libtool was called
public enum XCLibtoolMode {
public enum XCLibtoolMode: Equatable {
/// Creating a static library (ar format) from a set of .o input files
case createLibrary(output: String, filelist: String, dependencyInfo: String)
/// Creating a universal library (multiple-architectures) from a set of input .a static libraries
Expand Down
47 changes: 9 additions & 38 deletions Sources/xclibtool/XCLibtoolMain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,53 +18,24 @@
// under the License.

import Foundation
import xclibtoolSupport
import XCRemoteCache

public enum XCLibtoolMainError: Error {
case missingOutput
case unsupportedMode
}

/// Wrapper for a `libtool` program that copies the build executable (e.g. .a) from a cached-downloaded location
/// Fallbacks to a standard `libtool` when the Ramote cache is not applicable (e.g. modified sources)
public class XCLibtoolMain {
public init() { }

public func main() {
let args = ProcessInfo().arguments
var output: String?
// all input arguments library '.a'. Used to create an universal binary
var inputLibraries: [String] = []
var filelist: String?
var dependencyInfo: String?
var i = 0
while i < args.count {
switch args[i] {
case "-o":
output = args[i + 1]
i += 1
case "-filelist":
filelist = args[i + 1]
i += 1
case "-dependency_info":
dependencyInfo = args[i + 1]
i += 1
case let input where input.hasSuffix(".a"):
inputLibraries.append(input)
default:
break
}
i += 1
}
guard let outputInput = output else {
exit(1, "Missing 'output' argument. Args: \(args)")
}

let mode: XCLibtoolMode
if let filelistInput = filelist, let dependencyInfoInput = dependencyInfo {
// libtool is creating a library
mode = .createLibrary(output: outputInput, filelist: filelistInput, dependencyInfo: dependencyInfoInput)
} else if !inputLibraries.isEmpty {
// multiple input libraries suggest creating an universal binary
mode = .createUniversalBinary(output: outputInput, inputs: inputLibraries)
} else {
// unknown mode
exit(1, "Unsupported mode. Args: \(args)")
}
do {
let mode = try XCLibtoolHelper.buildMode(args: args)
try XCLibtool(mode).run()
} catch {
exit(1, "Failed with: \(error). Args: \(args)")
Expand Down
72 changes: 72 additions & 0 deletions Sources/xclibtoolSupport/XCLibtoolHelper.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// Copyright (c) 2023 Spotify AB.
//
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you 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 Foundation
import XCRemoteCache

public enum XCLibtoolHelperError: Error {
case missingOutput
case unsupportedMode
}

public class XCLibtoolHelper {
public static func buildMode(args: [String]) throws -> XCLibtoolMode {
var output: String?
// all input arguments library '.a'. Used to create an universal binary
var inputLibraries: [String] = []
var filelist: String?
var dependencyInfo: String?
var i = 0
while i < args.count {
switch args[i] {
case "-o":
output = args[i + 1]
i += 1
case "-filelist":
filelist = args[i + 1]
i += 1
case "-dependency_info":
dependencyInfo = args[i + 1]
i += 1
case let input where input.hasSuffix(".a"):
// Support for
inputLibraries.append(input)
default:
break
}
i += 1
}
guard let outputInput = output else {
throw XCLibtoolHelperError.missingOutput
}

let mode: XCLibtoolMode
if let filelistInput = filelist, let dependencyInfoInput = dependencyInfo {
// libtool is creating a library
mode = .createLibrary(output: outputInput, filelist: filelistInput, dependencyInfo: dependencyInfoInput)
} else if !inputLibraries.isEmpty {
// multiple input libraries suggest creating an universal binary
mode = .createUniversalBinary(output: outputInput, inputs: inputLibraries)
} else {
// unknown mode
throw XCLibtoolHelperError.unsupportedMode
}
return mode
}
}
66 changes: 66 additions & 0 deletions Tests/xclibtoolSupportTests/XCLibtoolHelperTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Copyright (c) 2023 Spotify AB.
//
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you 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.

@testable import xclibtoolSupport
import XCTest

class XCLibtoolHelperTests: XCTestCase {
// func testStaticFrameworkUniversalBinary() throws {
// let mode = try XCLibtoolHelper.buildMode(
// args: ["-o", "/universal/static", "/arch1/static", "arch2/static"]
// )
//
// XCTAssertEqual(mode, .createUniversalBinary(
// output: "/universal/static",
// inputs: ["/arch1/static", "arch2/static"]
// ))
// }
//
// func testStaticLibraryUniversalBinary() throws {
// let mode = try XCLibtoolHelper.buildMode(
// args: ["-o", "/universal/static.a", "/arch1/static.a", "arch2/static.a"]
// )
//
// XCTAssertEqual(mode, .createUniversalBinary(
// output: "/universal/static.a",
// inputs: ["/arch1/static.a", "arch2/static.a"]
// ))
// }
//
// func testUnknownExtensionInputThrowsUnsupportedMode() throws {
// XCTAssertThrowsError(try XCLibtoolHelper.buildMode(
// args: ["-o", "/universal/static.a", "/arch1/static.unknown"])) { error in
// switch error {
// case XCLibtoolHelperError.unsupportedMode: break
// default:
// XCTFail("Not expected error")
// }
// }
// }
//
// func testMissingOutputThrowsMissingOutput() throws {
// XCTAssertThrowsError(try XCLibtoolHelper.buildMode(args: ["/arch1/static"])) { error in
// switch error {
// case XCLibtoolHelperError.missingOutput: break
// default:
// XCTFail("Not expected error")
// }
// }
// }
}
Loading