Skip to content

Commit fe4d87f

Browse files
committed
fix: libvirt build files
1 parent d1b3423 commit fe4d87f

File tree

7 files changed

+1368
-162
lines changed

7 files changed

+1368
-162
lines changed

.github/workflows/build-libvirt.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,16 @@ jobs:
5656
name: Setup pnpm cache
5757
with:
5858
path: ${{ steps.pnpm-cache.outputs.STORE_PATH }}
59-
key: ${{ runner.os }}-pnpm-store-${{ hashFiles('api/pnpm-lock.yaml') }}
59+
key: ${{ runner.os }}-pnpm-store-${{ hashFiles('libvirt/package.json') }}
6060
restore-keys: |
6161
${{ runner.os }}-pnpm-store-
6262
63-
- name: Run pnpm install
64-
run: pnpm install
65-
6663
- name: pnpm install
6764
run: pnpm install
6865

66+
- name: Build
67+
run: pnpm run build
68+
6969
- name: pnpm lint/bindings
7070
run: pnpm run lint/bindings
7171
continue-on-error: false

libvirt/binding.gyp

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,32 @@
1616
"<!@(pkg-config --cflags-only-I libvirt | sed 's/-I//g')",
1717
"."
1818
],
19+
"dependencies": [
20+
"<!(node -p \"require('node-addon-api').gyp\")" ],
21+
"cflags!": [ "-fno-exceptions" ],
22+
"cflags_cc!": [ "-fno-exceptions" ],
23+
"xcode_settings": {
24+
"GCC_ENABLE_CPP_EXCEPTIONS": "YES",
25+
"CLANG_CXX_LIBRARY": "libc++",
26+
"MACOSX_DEPLOYMENT_TARGET": "<!(sw_vers -productVersion | cut -d. -f1,2)"
27+
},
28+
"msvs_settings": {
29+
"VCCLCompilerTool": { "ExceptionHandling": 1 }
30+
},
1931
"conditions": [
2032
["OS==\"mac\"", {
2133
"include_dirs": ["<!(echo $LIBVIRT_INCLUDE_DIR)"],
2234
"libraries": [
23-
"<!(echo $LIBVIRT_LIB_DIR)/libvirt.dylib"
35+
"<!(echo ${LIBVIRT_LIB_DIR:=/opt/homebrew/lib})/libvirt.dylib"
2436
],
25-
"xcode_settings": {
26-
"GCC_ENABLE_CPP_EXCEPTIONS": "YES",
27-
"CLANG_CXX_LIBRARY": "libc++",
28-
"MACOSX_DEPLOYMENT_TARGET": "10.15"
29-
}
37+
"defines": [ "NAPI_CPP_EXCEPTIONS" ]
3038
}],
3139
["OS!=\"mac\"", {
3240
"libraries": [
3341
"<!@(pkg-config --libs libvirt)"
3442
]
3543
}]
36-
],
37-
"cflags!": [ "-fno-exceptions" ],
38-
"cflags_cc!": [ "-fno-exceptions" ],
39-
"defines": [ "NAPI_CPP_EXCEPTIONS" ]
44+
]
4045
}
4146
]
4247
}

libvirt/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"scripts": {
1313
"prepare": "pnpm run build",
1414
"build": "node scripts/conditional-build.js",
15-
"build/native": "node-gyp rebuild",
15+
"build/native": "node scripts/build-native.js",
1616
"build/stub": "tsup lib/stub.ts --dts",
1717
"build/ts": "tsup lib/index.ts --dts --format cjs --clean",
1818
"test": "mocha -r ts-node/register lib/**/*.spec.ts",

libvirt/scripts/build-native.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
const { spawnSync } = require('child_process');
2+
const { platform } = require('os');
3+
4+
console.log('Running build-native.js script...');
5+
6+
function runCommand(command, args, options = {}) {
7+
const result = spawnSync(command, args, {
8+
stdio: 'inherit',
9+
shell: true,
10+
...options,
11+
});
12+
13+
if (result.status !== 0) {
14+
console.error(`Command failed: ${command} ${args.join(' ')}`);
15+
process.exit(1);
16+
}
17+
18+
return result;
19+
}
20+
21+
// Set up platform-specific environment variables
22+
const env = { ...process.env };
23+
if (platform() === 'darwin') {
24+
env.LIBVIRT_INCLUDE_DIR = '/opt/homebrew/include';
25+
env.LIBVIRT_LIB_DIR = '/opt/homebrew/lib';
26+
}
27+
28+
// Run node-gyp rebuild with the appropriate environment
29+
runCommand('pnpm', ['exec', 'node-gyp', 'rebuild'], { env });
Lines changed: 54 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,75 @@
1-
const { spawnSync } = require('child_process');
2-
const { platform } = require('os');
3-
const fs = require('fs');
1+
const { spawnSync } = require("child_process");
2+
const { platform } = require("os");
3+
const fs = require("fs");
44

55
function runCommand(command, args, options = {}) {
6-
const result = spawnSync(command, args, {
7-
stdio: 'inherit',
8-
shell: true,
9-
...options
10-
});
6+
const result = spawnSync(command, args, {
7+
stdio: "inherit",
8+
shell: true,
9+
...options,
10+
});
1111

12-
if (result.status !== 0) {
13-
console.error(`Command failed: ${command} ${args.join(' ')}`);
14-
process.exit(1);
15-
}
12+
if (result.status !== 0) {
13+
console.error(`Command failed: ${command} ${args.join(" ")}`);
14+
process.exit(1);
15+
}
1616

17-
return result;
17+
return result;
1818
}
1919

2020
function installLibvirtMac() {
21-
console.log('Installing libvirt via Homebrew...');
22-
return runCommand('brew', ['install', 'libvirt']);
21+
console.log("Installing libvirt via Homebrew...");
22+
return runCommand("brew", ["install", "libvirt"]);
2323
}
2424

2525
function checkLibvirt() {
26-
if (platform() === 'darwin') {
27-
// Check if libvirt is installed on macOS
28-
const result = spawnSync('brew', ['list', 'libvirt'], { stdio: 'pipe' });
29-
if (result.status !== 0) {
30-
return installLibvirtMac();
31-
}
26+
if (platform() === "darwin") {
27+
// Check if libvirt is installed on macOS
28+
const result = spawnSync("brew", ["list", "libvirt"], { stdio: "pipe" });
29+
if (result.status !== 0) {
30+
return installLibvirtMac();
31+
}
3232

33-
return true;
34-
}
33+
return true;
34+
}
3535

36-
if (platform() === 'linux') {
37-
return true;
38-
}
36+
if (platform() === "linux") {
37+
return true;
38+
}
3939

40-
// Add other platform checks as needed
41-
return false;
40+
// Add other platform checks as needed
41+
return false;
4242
}
4343

4444
async function build() {
45-
try {
46-
if (checkLibvirt()) {
47-
console.log('Building native bindings...');
48-
// On macOS, we need to specify the libvirt include and lib paths from Homebrew
49-
if (platform() === 'darwin') {
50-
process.env.LIBVIRT_INCLUDE_DIR = '/opt/homebrew/include';
51-
process.env.LIBVIRT_LIB_DIR = '/opt/homebrew/lib';
52-
}
45+
try {
46+
if (checkLibvirt()) {
47+
console.log("Building native bindings...");
48+
runCommand("pnpm", ["run", "build/native"]);
5349

54-
runCommand('npm', ['run', 'build/native'], {
55-
env: { ...process.env }
56-
});
50+
console.log("Building TypeScript...");
51+
runCommand("pnpm", ["run", "build/ts"]);
52+
} else {
53+
console.log(
54+
"Failed to install/find libvirt, building stub implementation..."
55+
);
56+
runCommand("pnpm", ["run", "build/stub"]);
5757

58-
console.log('Building TypeScript...');
59-
runCommand('npm', ['run', 'build/ts']);
60-
} else {
61-
console.log('Failed to install/find libvirt, building stub implementation...');
62-
runCommand('npm', ['run', 'build/stub']);
63-
64-
if (fs.existsSync('dist/stub.d.ts')) {
65-
fs.copyFileSync('dist/stub.d.ts', 'dist/index.d.ts');
66-
fs.copyFileSync('dist/stub.js', 'dist/index.js');
67-
} else {
68-
console.error('Stub build failed to generate files');
69-
process.exit(1);
70-
}
71-
}
72-
} catch (error) {
73-
console.error('Build failed:', error);
74-
process.exit(1);
75-
}
58+
if (fs.existsSync("dist/stub.d.ts")) {
59+
fs.copyFileSync("dist/stub.d.ts", "dist/index.d.ts");
60+
fs.copyFileSync("dist/stub.js", "dist/index.js");
61+
} else {
62+
console.error("Stub build failed to generate files");
63+
process.exit(1);
64+
}
65+
}
66+
} catch (error) {
67+
console.error("Build failed:", error);
68+
process.exit(1);
69+
}
7670
}
7771

78-
build().catch(error => {
79-
console.error('Unhandled error:', error);
80-
process.exit(1);
72+
build().catch((error) => {
73+
console.error("Unhandled error:", error);
74+
process.exit(1);
8175
});

0 commit comments

Comments
 (0)