Skip to content

Commit

Permalink
Fallback to local host build when no default docker image found
Browse files Browse the repository at this point in the history
  • Loading branch information
messense committed Dec 14, 2022
1 parent fc1bacf commit a5461e1
Show file tree
Hide file tree
Showing 2 changed files with 230 additions and 187 deletions.
189 changes: 103 additions & 86 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11595,15 +11595,19 @@ async function installMaturin(tag) {
return exe;
}
}
async function dockerBuild(tag, manylinux, args) {
var _a, _b;
const target = getRustTarget(args);
const rustToolchain = (await getRustToolchain(args)) || 'stable';
async function getDockerContainer(target, manylinux) {
var _a;
let container = core.getInput('container');
if (container.length === 0) {
container =
((_a = DEFAULT_CONTAINERS[target]) === null || _a === void 0 ? void 0 : _a[manylinux]) || DEFAULT_CONTAINER[manylinux];
}
return container;
}
async function dockerBuild(container, maturinRelease, args) {
var _a;
const target = getRustTarget(args);
const rustToolchain = (await getRustToolchain(args)) || 'stable';
const targetOrHostTriple = target ? target : DEFAULT_TARGET[process.arch];
const dockerArgs = [];
let image;
Expand All @@ -11613,12 +11617,12 @@ async function dockerBuild(tag, manylinux, args) {
image = container;
}
else {
image = `${container}:${tag}`;
image = `${container}:${maturinRelease}`;
dockerArgs.push('--entrypoint', '/bin/bash');
}
}
else if (!container.includes(':') &&
((_b = DEFAULT_CONTAINERS[targetOrHostTriple]) === null || _b === void 0 ? void 0 : _b[container])) {
((_a = DEFAULT_CONTAINERS[targetOrHostTriple]) === null || _a === void 0 ? void 0 : _a[container])) {
image = DEFAULT_CONTAINERS[targetOrHostTriple][container];
}
else {
Expand All @@ -11641,9 +11645,9 @@ async function dockerBuild(tag, manylinux, args) {
core.info(`Using existing ${image} Docker image`);
}
const arch = process.arch === 'arm64' ? 'aarch64' : 'x86_64';
const url = tag === 'latest'
const url = maturinRelease === 'latest'
? `https://github.com/PyO3/maturin/releases/latest/download/maturin-${arch}-unknown-linux-musl.tar.gz`
: `https://github.com/PyO3/maturin/releases/download/${tag}/maturin-${arch}-unknown-linux-musl.tar.gz`;
: `https://github.com/PyO3/maturin/releases/download/${maturinRelease}/maturin-${arch}-unknown-linux-musl.tar.gz`;
const rustupComponents = core.getInput('rustup-components');
const commands = [
'#!/bin/bash',
Expand Down Expand Up @@ -11785,6 +11789,87 @@ async function addToolCachePythonVersionsToPath() {
}
}
}
async function hostBuild(maturinRelease, args) {
const command = core.getInput('command');
const target = getRustTarget(args);
const rustToolchain = await getRustToolchain(args);
const rustupComponents = core.getInput('rustup-components');
const workdir = core.getInput('working-directory') || process.cwd();
core.startGroup('Install Rust target');
if (rustToolchain.length > 0) {
await exec.exec('rustup', ['override', 'set', rustToolchain]);
await exec.exec('rustup', ['component', 'add', 'llvm-tools-preview'], {
ignoreReturnCode: true
});
}
if (rustupComponents.length > 0) {
const rustupArgs = ['component', 'add'].concat(rustupComponents.split(' '));
await exec.exec('rustup', rustupArgs);
}
await installRustTarget(target, rustToolchain);
core.endGroup();
if (IS_MACOS && !process.env.pythonLocation) {
addToolCachePythonVersionsToPath();
}
core.startGroup('Install maturin');
core.info(`Installing 'maturin' from tag '${maturinRelease}'`);
const maturinPath = await installMaturin(maturinRelease);
await exec.exec(maturinPath, ['--version'], { ignoreReturnCode: true });
if (IS_LINUX) {
await exec.exec('python3', ['-m', 'pip', 'install', 'patchelf']);
}
core.endGroup();
if (args.includes('--zig')) {
core.startGroup('Install Zig');
await exec.exec('python3', ['-m', 'pip', 'install', 'ziglang']);
core.endGroup();
}
const isUniversal2 = args.includes('--universal2');
const isArm64 = IS_MACOS && target.startsWith('aarch64');
const env = {};
for (const [k, v] of Object.entries(process.env)) {
if (v !== undefined) {
env[k] = v;
}
}
if (isUniversal2 || isArm64) {
const buildEnvName = isUniversal2 ? 'universal2' : 'arm64';
core.startGroup(`Prepare macOS ${buildEnvName} build environment`);
if (isUniversal2) {
await installRustTarget('x86_64-apple-darwin', rustToolchain);
}
await installRustTarget('aarch64-apple-darwin', rustToolchain);
if (!env.DEVELOPER_DIR) {
env.DEVELOPER_DIR = '/Applications/Xcode.app/Contents/Developer';
}
if (!env.SDKROOT) {
env.SDKROOT =
'/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk';
}
core.endGroup();
}
let fullCommand = `${maturinPath} ${args.join(' ')}`;
if (command === 'upload') {
const uploadArgs = [];
for (const arg of args.slice(1)) {
if (arg.startsWith('-')) {
uploadArgs.push(arg);
}
else {
const cwd = process.cwd();
process.chdir(workdir);
const globber = await glob.create(arg);
for await (const file of globber.globGenerator()) {
uploadArgs.push(file);
}
process.chdir(cwd);
}
}
fullCommand = `${maturinPath} ${command} ${uploadArgs.join(' ')}`;
}
const exitCode = await exec.exec(fullCommand, undefined, { env, cwd: workdir });
return exitCode;
}
async function innerMain() {
const inputArgs = core.getInput('args');
const args = (0, string_argv_1.default)(inputArgs);
Expand Down Expand Up @@ -11821,88 +11906,20 @@ async function innerMain() {
args.push('--target', target);
}
}
const tag = await findVersion(args);
const maturinRelease = await findVersion(args);
let exitCode;
if (useDocker) {
exitCode = await dockerBuild(tag, manylinux, args);
}
else {
const rustToolchain = await getRustToolchain(args);
const rustupComponents = core.getInput('rustup-components');
const workdir = core.getInput('working-directory') || process.cwd();
core.startGroup('Install Rust target');
if (rustToolchain.length > 0) {
await exec.exec('rustup', ['override', 'set', rustToolchain]);
await exec.exec('rustup', ['component', 'add', 'llvm-tools-preview'], {
ignoreReturnCode: true
});
const container = await getDockerContainer(target, manylinux);
if (container) {
exitCode = await dockerBuild(container, maturinRelease, args);
}
if (rustupComponents.length > 0) {
const rustupArgs = ['component', 'add'].concat(rustupComponents.split(' '));
await exec.exec('rustup', rustupArgs);
}
await installRustTarget(target, rustToolchain);
core.endGroup();
if (IS_MACOS && !process.env.pythonLocation) {
addToolCachePythonVersionsToPath();
}
core.startGroup('Install maturin');
core.info(`Installing 'maturin' from tag '${tag}'`);
const maturinPath = await installMaturin(tag);
await exec.exec(maturinPath, ['--version'], { ignoreReturnCode: true });
if (IS_LINUX) {
await exec.exec('python3', ['-m', 'pip', 'install', 'patchelf']);
}
core.endGroup();
if (args.includes('--zig')) {
core.startGroup('Install Zig');
await exec.exec('python3', ['-m', 'pip', 'install', 'ziglang']);
core.endGroup();
}
const isUniversal2 = args.includes('--universal2');
const isArm64 = IS_MACOS && target.startsWith('aarch64');
const env = {};
for (const [k, v] of Object.entries(process.env)) {
if (v !== undefined) {
env[k] = v;
}
}
if (isUniversal2 || isArm64) {
const buildEnvName = isUniversal2 ? 'universal2' : 'arm64';
core.startGroup(`Prepare macOS ${buildEnvName} build environment`);
if (isUniversal2) {
await installRustTarget('x86_64-apple-darwin', rustToolchain);
}
await installRustTarget('aarch64-apple-darwin', rustToolchain);
if (!env.DEVELOPER_DIR) {
env.DEVELOPER_DIR = '/Applications/Xcode.app/Contents/Developer';
}
if (!env.SDKROOT) {
env.SDKROOT =
'/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk';
}
core.endGroup();
}
let fullCommand = `${maturinPath} ${args.join(' ')}`;
if (command === 'upload') {
const uploadArgs = [];
for (const arg of args.slice(1)) {
if (arg.startsWith('-')) {
uploadArgs.push(arg);
}
else {
const cwd = process.cwd();
process.chdir(workdir);
const globber = await glob.create(arg);
for await (const file of globber.globGenerator()) {
uploadArgs.push(file);
}
process.chdir(cwd);
}
}
fullCommand = `${maturinPath} ${command} ${uploadArgs.join(' ')}`;
else {
core.info('No Docker container found, fallback to build on host');
exitCode = await hostBuild(maturinRelease, args);
}
exitCode = await exec.exec(fullCommand, undefined, { env, cwd: workdir });
}
else {
exitCode = await hostBuild(maturinRelease, args);
}
if (exitCode !== 0) {
throw new Error(`maturin: returned ${exitCode}`);
Expand Down
Loading

0 comments on commit a5461e1

Please sign in to comment.