Skip to content

Support building with Xcode #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Nov 14, 2023
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
builders
  • Loading branch information
kabiroberai committed Nov 13, 2023
commit f6e0c8904c9c3a160b2952fc0b7c31481f53bf61
47 changes: 30 additions & 17 deletions src/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,43 @@ export type BuildMode = "release" | "debug";

export type ConfigFlags = string | string[];

export interface XcodeConfig {
destinations?: string[]
export interface SwiftPMBuilder {
type?: "swiftpm"
// flags passed directly to `swift build`
settings?: ConfigFlags
// --triple flag
// warning: cross-compilation triples break macros as of Swift 5.9
triple?: string
}

export interface XcodeBuilder {
type: "xcode"
// flags passed directly to `xcodebuild`
settings?: ConfigFlags
// -destination parameters
destinations?: string[]
}

export type Builder = SwiftPMBuilder | XcodeBuilder;

export interface Config {
buildPath?: string
packagePath?: string
product?: string

triple?: string
napi?: number | "experimental"

static?: boolean

spmFlags?: ConfigFlags
cFlags?: ConfigFlags
swiftFlags?: ConfigFlags
cxxFlags?: ConfigFlags
linkerFlags?: ConfigFlags

// if truthy, build using xcodebuild instead of swift-build
xcode?: boolean | XcodeConfig
// flags passed to `swift package dump-package`
dumpFlags?: ConfigFlags

builder?: Builder | Builder["type"]
}

export async function clean(config: Config = {}) {
Expand Down Expand Up @@ -74,7 +88,7 @@ function getFlags<C>(config: C, name: keyof C & string): string[] {
}

export async function build(mode: BuildMode, config: Config = {}): Promise<string> {
let spmFlags = getFlags(config, "spmFlags");
let dumpFlags = getFlags(config, "dumpFlags");
let cFlags = getFlags(config, "cFlags");
let swiftFlags = getFlags(config, "swiftFlags");
let cxxFlags = getFlags(config, "cxxFlags");
Expand Down Expand Up @@ -102,12 +116,6 @@ export async function build(mode: BuildMode, config: Config = {}): Promise<strin

let napi = config.napi;

if (typeof config.triple === "string") {
spmFlags.push("--triple", config.triple);
} else if (typeof config.triple !== "undefined") {
throw new Error("Invalid value for triple option.");
}

if (typeof napi === "number") {
cFlags.push(`-DNAPI_VERSION=${napi}`);
swiftFlags.push("-DNAPI_VERSIONED");
Expand Down Expand Up @@ -136,7 +144,7 @@ export async function build(mode: BuildMode, config: Config = {}): Promise<strin
"package",
"dump-package",
"--package-path", packagePath,
...spmFlags.filter(f => f !== "-v"),
...dumpFlags,
...nonSPMFlags,
],
{ stdio: ["inherit", "pipe", "inherit"] }
Expand Down Expand Up @@ -217,8 +225,8 @@ export async function build(mode: BuildMode, config: Config = {}): Promise<strin

const realBinaryPath = path.join(buildDir, mode, `${product}.node`);
const binaryPath = path.join(buildDir, `${product}.node`);
if (config.xcode) {
const xcode = typeof config.xcode === "object" ? config.xcode : {};
if (config.builder === "xcode" || (typeof config.builder === "object" && config.builder.type === "xcode")) {
const xcode = typeof config.builder === "object" ? config.builder : ({ type: "xcode" } as XcodeBuilder);
const settings = getFlags(xcode, "settings");
const destinations = xcode.destinations || ["generic/platform=macOS"];
const derivedDataPath = path.join(buildDir, "DerivedData");
Expand Down Expand Up @@ -258,6 +266,11 @@ export async function build(mode: BuildMode, config: Config = {}): Promise<strin
{ recursive: true, force: true }
);
} else {
const swiftPM = typeof config.builder === "object" ? config.builder : {};
const swiftPMFlags = getFlags(swiftPM, "settings");
if (typeof swiftPM.triple === "string") {
swiftPMFlags.push("--triple", swiftPM.triple);
}
const result = spawnSync(
"swift",
[
Expand All @@ -267,7 +280,7 @@ export async function build(mode: BuildMode, config: Config = {}): Promise<strin
"--build-path", buildDir,
"--package-path", packagePath,
...ldflags,
...spmFlags,
...swiftPMFlags,
...nonSPMFlags,
],
{
Expand Down