Skip to content
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

feat: add musl target support in npm pakcage #1067

Merged
merged 4 commits into from
Dec 7, 2023
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
27 changes: 26 additions & 1 deletion packages/@biomejs/biome/bin/biome
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
#!/usr/bin/env node
const { platform, arch, env, version, release } = process;
const { execSync } = require("child_process");

function isMusl() {
let stderr;
try {
stderr = execSync("ldd --version", {
stdio: ['pipe', 'pipe', 'pipe']
});
} catch (err) {
stderr = err.stderr;
}
if (stderr.indexOf("musl") > -1) {
return true;
}
return false;
}

const PLATFORMS = {
win32: {
Expand All @@ -14,12 +30,21 @@ const PLATFORMS = {
x64: "@biomejs/cli-linux-x64/biome",
arm64: "@biomejs/cli-linux-arm64/biome",
},
"linux-musl": {
x64: "@biomejs/cli-linux-x64-musl/biome",
arm64: "@biomejs/cli-linux-arm64-musl/biome",
},
};
if (env.ROME_BINARY) {
console.warn(`[WARN] The environment variable "ROME_BINARY" is deprecated. Use "BIOME_BINARY" instead.`)
}

const binPath = env.BIOME_BINARY || env.ROME_BINARY || PLATFORMS?.[platform]?.[arch];
const binPath = env.BIOME_BINARY || env.ROME_BINARY ||
(platform === "linux" && isMusl()
? PLATFORMS?.["linux-musl"]?.[arch]
: PLATFORMS?.[platform]?.[arch]
);

if (binPath) {
const packageManager = detectPackageManager();
const result = require("child_process").spawnSync(
Expand Down
27 changes: 19 additions & 8 deletions packages/@biomejs/biome/scripts/generate-packages.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as fs from "node:fs";
import { resolve } from "node:path";
import { fileURLToPath } from "node:url";
import { format } from "node:util";

const CLI_ROOT = resolve(fileURLToPath(import.meta.url), "../..");
const PACKAGES_ROOT = resolve(CLI_ROOT, "..");
Expand All @@ -11,10 +12,16 @@ const rootManifest = JSON.parse(
fs.readFileSync(MANIFEST_PATH).toString("utf-8"),
);

function generateNativePackage(platform, arch) {
const packageName = `@biomejs/cli-${platform}-${arch}`;
const packageRoot = resolve(PACKAGES_ROOT, `cli-${platform}-${arch}`);
function getName(platform, arch, prefix = 'cli') {
return format(`${prefix}-${platform}`, arch);
}

function generateNativePackage(platform, arch) {
const os = platform.split('-')[0];
const buildName = getName(platform, arch)
const packageName = `@biomejs/${buildName}`
const packageRoot = resolve(PACKAGES_ROOT, buildName);

// Remove the directory just in case it already exists (it's autogenerated
// so there shouldn't be anything important there anyway)
fs.rmSync(packageRoot, { recursive: true, force: true });
Expand All @@ -33,17 +40,21 @@ function generateNativePackage(platform, arch) {
repository,
engines,
homepage,
os: [platform],
os: [os],
cpu: [arch],
libc: os === "linux"
? packageName.endsWith('musl')
? ['musl'] : ['glibc']
: undefined
});

const manifestPath = resolve(packageRoot, "package.json");
console.log(`Create manifest ${manifestPath}`);
fs.writeFileSync(manifestPath, manifest);

// Copy the CLI binary
const ext = platform === "win32" ? ".exe" : "";
const binarySource = resolve(REPO_ROOT, `biome-${platform}-${arch}${ext}`);
const ext = os === "win32" ? ".exe" : "";
const binarySource = resolve(REPO_ROOT, `${getName(platform, arch, 'biome')}${ext}`);
const binaryTarget = resolve(packageRoot, `biome${ext}`);

console.log(`Copy binary ${binaryTarget}`);
Expand Down Expand Up @@ -75,7 +86,7 @@ function writeManifest(packagePath) {

const nativePackages = PLATFORMS.flatMap((platform) =>
ARCHITECTURES.map((arch) => [
`@biomejs/cli-${platform}-${arch}`,
`@biomejs/${getName(platform, arch)}`,
rootManifest.version,
]),
);
Expand All @@ -88,7 +99,7 @@ function writeManifest(packagePath) {
fs.writeFileSync(manifestPath, content);
}

const PLATFORMS = ["win32", "darwin", "linux"];
const PLATFORMS = ["win32-%s", "darwin-%s", "linux-%s", "linux-%s-musl"];
const ARCHITECTURES = ["x64", "arm64"];
const WASM_TARGETS = ["bundler", "nodejs", "web"];

Expand Down
26 changes: 25 additions & 1 deletion packages/@biomejs/biome/scripts/postinstall.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,20 @@
const { platform, arch } = process;
const { execSync } = require("child_process");

function isMusl() {
let stderr;
try {
stderr = execSync("ldd --version", {
stdio: ['pipe', 'pipe', 'pipe']
});
} catch (err) {
stderr = err.stderr;
}
if (stderr.indexOf("musl") > -1) {
return true;
}
return false;
}

const PLATFORMS = {
win32: {
Expand All @@ -13,9 +29,17 @@ const PLATFORMS = {
x64: "@biomejs/cli-linux-x64/biome",
arm64: "@biomejs/cli-linux-arm64/biome",
},
"linux-musl": {
x64: "@biomejs/cli-linux-x64-musl/biome",
arm64: "@biomejs/cli-linux-arm64-musl/biome",
},
};

const binName = PLATFORMS?.[platform]?.[arch];
const binName =
platform === "linux" && isMusl()
? PLATFORMS?.["linux-musl"]?.[arch]
: PLATFORMS?.[platform]?.[arch];

if (binName) {
let binPath;
try {
Expand Down