Skip to content

Commit

Permalink
Merge pull request #9160 from Agoric/kriskowal-this-time-with-feeling…
Browse files Browse the repository at this point in the history
…-8536

feat(xsnap): Build sensitivity to presence of build toolchain
  • Loading branch information
mergify[bot] committed Apr 4, 2024
2 parents 2b7439e + efbaf26 commit c060e65
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 9 deletions.
11 changes: 9 additions & 2 deletions packages/xsnap/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"scripts": {
"repl": "node src/xsrepl.js",
"build:bin": "if test -d ./test; then node src/build.js; else yarn build:from-env; fi",
"build:env": "test -d ./test && node src/build.js --show-env > build.env",
"build:env": "node src/build.js --show-env > build.env",
"build:from-env": "{ cat build.env; echo node src/build.js; } | xargs env",
"build": "yarn build:bin && yarn build:env",
"postinstall": "yarn build:from-env",
Expand Down Expand Up @@ -50,7 +50,14 @@
"LICENSE*",
"api.js",
"build.env",
"src"
"moddable/modules/data",
"moddable/xs/includes",
"moddable/xs/makefiles",
"moddable/xs/platforms/*.h",
"moddable/xs/sources",
"src",
"xsnap-native/xsnap/makefiles",
"xsnap-native/xsnap/sources"
],
"publishConfig": {
"access": "public"
Expand Down
21 changes: 21 additions & 0 deletions packages/xsnap/scripts/test-package.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/bin/bash
# Verifies that files in package.json covers everything xsnap needs to compile
# from sources out of an npm package.
set -xueo pipefail

TEMP=$(mktemp -d)
# function cleanup() {
# rm -rf "$TEMP"
# }
# trap cleanup EXIT

yarn pack -f "$TEMP/package.tar"
(
cd "$TEMP"
tar xvf package.tar
cd package
time yarn
time yarn
time yarn
time yarn
)
108 changes: 102 additions & 6 deletions packages/xsnap/src/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ const makeSubmodule = (path, repoUrl, { git }) => {
};

/**
* @param {string[]} args
* @param {boolean} showEnv
* @param {{
* env: Record<string, string | undefined>,
* stdout: typeof process.stdout,
Expand All @@ -149,12 +149,9 @@ const makeSubmodule = (path, repoUrl, { git }) => {
* rmdirSync: typeof import('fs').rmdirSync,
* readFile: typeof import('fs').promises.readFile,
* },
* os: {
* type: typeof import('os').type,
* }
* }} io
*/
async function main(args, { env, stdout, spawn, fs, os }) {
const updateSubmodules = async (showEnv, { env, stdout, spawn, fs }) => {
const git = makeCLI('git', { spawn });

// When changing/adding entries here, make sure to search the whole project
Expand All @@ -176,7 +173,7 @@ async function main(args, { env, stdout, spawn, fs, os }) {
];

await null;
if (args.includes('--show-env')) {
if (showEnv) {
for (const submodule of submodules) {
const { path, envPrefix, commitHash } = submodule;
if (!commitHash) {
Expand Down Expand Up @@ -214,7 +211,22 @@ async function main(args, { env, stdout, spawn, fs, os }) {
await submodule.init();
}
}
};

/**
* @param {{
* spawn: typeof import('child_process').spawn,
* fs: {
* existsSync: typeof import('fs').existsSync,
* rmdirSync: typeof import('fs').rmdirSync,
* readFile: typeof import('fs').promises.readFile,
* },
* os: {
* type: typeof import('os').type,
* }
* }} io
*/
const makeXsnap = async ({ spawn, fs, os }) => {
const pjson = await fs.readFile(asset('../package.json'), 'utf-8');
const pkg = JSON.parse(pjson);

Expand All @@ -239,6 +251,90 @@ async function main(args, { env, stdout, spawn, fs, os }) {
},
);
}
};

/**
* @param {string[]} args
* @param {{
* env: Record<string, string | undefined>,
* stdout: typeof process.stdout,
* spawn: typeof import('child_process').spawn,
* fs: {
* existsSync: typeof import('fs').existsSync,
* rmdirSync: typeof import('fs').rmdirSync,
* readFile: typeof import('fs').promises.readFile,
* },
* os: {
* type: typeof import('os').type,
* }
* }} io
*/
async function main(args, { env, stdout, spawn, fs, os }) {
// I solemnly swear I will do no synchronous work followed by a variable
// number turns of the event loop.
await null;

const osType = os.type();
const platform = {
Linux: 'lin',
Darwin: 'mac',
// Windows_NT: 'win', // One can dream.
}[osType];
if (platform === undefined) {
throw Error(`xsnap does not support platform ${osType}`);
}

// If this is a working copy of xsnap in a checkout of agoric-sdk, we need to
// either clone or update submodules.
// Otherwise, we are running from an extracted npm tarball and we should not
// attempt to update Git submodules and should make the binary from the
// published source.
//
// These steps will avoid rebuilding native xsnap in the common case for end
// users.
//
// || | X || git
// || X | X || make
// || ---- | ---- || ----
// | bin | src | .git || pack | work ||
// | --- | --- | ---- || ---- | ---- ||
// | | | || | X ||
// | | | X || | ||
// | | X | || X | ||
// | | X | X || | X ||
// | X | | || | ||
// | X | | X || | ||
// | X | X | || X | ||
// | X | X | X || | X ||
//
// We build both release and debug, so checking for one should suffice.
// XXX This will need to account for the .exe extension if we recover support
// for Windows.
const hasBin = fs.existsSync(
asset(`../xsnap-native/xsnap/build/bin/${platform}/release/xsnap-worker`),
);
let hasSource = fs.existsSync(asset('../moddable/xs/includes/xs.h'));
const hasGit = fs.existsSync(asset('../moddable/.git'));
const isWorkingCopy = hasGit || (!hasSource && !hasBin);
const showEnv = args.includes('--show-env');

if (isWorkingCopy || showEnv) {
if (showEnv && !isWorkingCopy) {
throw new Error('XSnap requires a working copy and git to --show-env');
}
await updateSubmodules(showEnv, { env, stdout, spawn, fs });
hasSource = true;
}

if (!showEnv) {
if (hasSource) {
await makeXsnap({ spawn, fs, os });
} else if (!hasBin) {
throw new Error(
'XSnap has neither sources nor a pre-built binary. Docker? .dockerignore? npm files?',
);
}
}
}

const run = () =>
Expand Down
2 changes: 1 addition & 1 deletion packages/xsnap/src/xsnap.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export async function xsnap(options) {
const platform = {
Linux: 'lin',
Darwin: 'mac',
Windows_NT: 'win',
// Windows_NT: 'win', // One can dream.
}[os];

if (platform === undefined) {
Expand Down

0 comments on commit c060e65

Please sign in to comment.