Skip to content

Commit 69df6e7

Browse files
authored
Rpath (hmenyus#18)
* rpath * rpath
1 parent 078f1e9 commit 69df6e7

4 files changed

Lines changed: 53 additions & 7 deletions

File tree

binding.gyp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,12 @@
3030
],
3131
"link_settings": {
3232
"libraries": [
33-
"<!(python3-config --ldflags --embed)"
33+
"<!(python3-config --ldflags --embed)",
34+
"<!(node ./scripts/rpaths.js)"
3435
]
3536
},
36-
'cflags': ['-fexceptions'],
37-
'cflags_cc': ['-fexceptions']
37+
'cflags': ["<!(python3-config --cflags)", "-fexceptions"],
38+
'cflags_cc': ["<!(python3-config --cflags)", "-fexceptions"]
3839
}],
3940
['OS=="mac"', {
4041
"include_dirs" : [

index.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,27 @@ class Interpreter
88
constructor()
99
{
1010
this.py = new nodecallspython.PyInterpreter();
11-
if (process.platform === "linux")
11+
if (process.platform === "linux" || process.platform === "darwin")
1212
{
1313
const stdout = execSync("python3-config --configdir");
1414
if (stdout)
1515
{
1616
const dir = stdout.toString().trim();
1717
if (fs.existsSync(dir))
1818
{
19+
const reg = process.platform === "linux" ? /libpython.*\.so/ : /libpython.*\.dylib/;
1920
fs.readdirSync(dir).forEach(file => {
20-
if (file.match(/libpython.*\.so/))
21-
this.fixlink(path.join(dir, file));
21+
if (file.match(reg))
22+
{
23+
try
24+
{
25+
this.fixlink(path.join(dir, file));
26+
}
27+
catch(e)
28+
{
29+
console.error(e);
30+
}
31+
}
2232
});
2333
}
2434
}

scripts/rpaths.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
const { execSync } = require("child_process");
2+
3+
const stdout = execSync("python3-config --ldflags --embed");
4+
if (stdout)
5+
{
6+
const splits = stdout.toString().trim().split(" ");
7+
8+
const result = [];
9+
splits.forEach(s => {
10+
if (s.startsWith("-L"))
11+
result.push(s.substring(2));
12+
});
13+
14+
result.forEach((r, i) => {
15+
if (i == 0)
16+
{
17+
if (result.length > 1)
18+
console.log("-Wl,-rpath," + r +" \\");
19+
else
20+
console.log("-Wl,-rpath," + r);
21+
}
22+
else if (i + 1 == result.length)
23+
console.log("\t-Wl,-rpath," + r);
24+
else
25+
console.log("\t-Wl,-rpath," + r + " \\");
26+
});
27+
}

src/addon.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -562,7 +562,15 @@ namespace nodecallspython
562562
if (valuetype == napi_string)
563563
{
564564
auto filename = convertString(env, args[0]);
565-
dlopen(filename.c_str(), RTLD_LAZY | RTLD_GLOBAL);
565+
auto result = dlopen(filename.c_str(), RTLD_LAZY | RTLD_GLOBAL);
566+
if (!result)
567+
{
568+
auto error = dlerror();
569+
if (error)
570+
napi_throw_error(env, "args", error);
571+
else
572+
napi_throw_error(env, "args", "Unknown error of dlopen");
573+
}
566574

567575
return nullptr;
568576
}

0 commit comments

Comments
 (0)