neon-cli
Version:
Build and load native Rust/Neon modules.
115 lines (114 loc) • 4.89 kB
JavaScript
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const rust = __importStar(require("./rust"));
const path_1 = __importDefault(require("path"));
const rimraf_1 = require("./async/rimraf");
const LIB_PREFIX = {
darwin: "lib",
freebsd: "lib",
linux: "lib",
sunos: "lib",
win32: "",
};
const LIB_SUFFIX = {
darwin: ".dylib",
freebsd: ".so",
linux: ".so",
sunos: ".so",
win32: ".dll",
};
/** The Rust build artifacts for a single build target of a Neon crate. */
class Target {
constructor(crate, options = {}) {
let { release = true, arch = process.env.npm_config_arch || process.arch } = options;
this.crate = crate;
this.release = release;
this.arch = arch;
if (process.platform === "win32") {
this.triple =
arch === "ia32" ? "i686-pc-windows-msvc" : "x86_64-pc-windows-msvc";
}
else {
this.triple = "";
}
if (process.env.CARGO_BUILD_TARGET) {
this.triple = process.env.CARGO_BUILD_TARGET;
}
this.subdirectory = path_1.default.join(this.triple, release ? "release" : "debug");
this.root = path_1.default.resolve(crate.project.targetDirectory, this.subdirectory);
let prefix = LIB_PREFIX[process.platform];
let suffix = LIB_SUFFIX[process.platform];
this.dylib = path_1.default.resolve(this.root, prefix + crate.name + suffix);
}
clean() {
return __awaiter(this, void 0, void 0, function* () {
// Remove the directory associated with this target.
const absolutePathSubdir = path_1.default.resolve(this.crate.root, "target", this.subdirectory);
yield (0, rimraf_1.rimraf)(absolutePathSubdir);
// If this target was the active target, remove the addon.
if (this.crate.artifacts.haveActivated(this.subdirectory)) {
yield this.crate.removeAddon();
}
// Update the build state.
this.crate.artifacts.delete(this.subdirectory);
this.crate.saveArtifacts();
});
}
build(toolchain, settings, additionalArgs) {
return __awaiter(this, void 0, void 0, function* () {
let releaseFlags = this.release ? ["--release"] : [];
let targetFlags = this.triple ? ["--target=" + this.triple] : [];
let args = ["build"].concat(releaseFlags, targetFlags, additionalArgs);
try {
let result = yield rust.spawn("cargo", args, toolchain, {
cwd: this.crate.root,
stdio: "inherit",
});
if (result !== 0) {
throw new Error("cargo build failed");
}
this.crate.artifacts.activate(this.subdirectory, settings);
return result;
}
finally {
this.crate.saveArtifacts();
}
});
}
inState(settings) {
let savedSettings = this.crate.artifacts.lookup(this.subdirectory);
return savedSettings && savedSettings.match(settings);
}
}
exports.default = Target;