Skip to content

Commit 1e57033

Browse files
committed
[GR-43614] Fix setting of LD_LIBRARY_PATH to occur during and not after context initialization
1 parent 30e6944 commit 1e57033

File tree

6 files changed

+62
-8
lines changed

6 files changed

+62
-8
lines changed

graalpython/com.oracle.graal.python.frozen/freeze_modules.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ def add_graalpython_core():
119119
"java",
120120
"pip_hook",
121121
"unicodedata",
122+
"sulong_support",
122123
]:
123124
modname = f"graalpy.{os.path.basename(name)}"
124125
modpath = os.path.join(lib_graalpython, f"{name}.py")

graalpython/com.oracle.graal.python.shell/src/com/oracle/graal/python/shell/GraalPythonMain.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -649,6 +649,7 @@ protected void launch(Builder contextBuilder) {
649649
if (!noSite) {
650650
contextBuilder.option("python.ForceImportSite", "true");
651651
}
652+
contextBuilder.option("python.SetupLLVMLibraryPaths", "true");
652653
contextBuilder.option("python.IgnoreEnvironmentFlag", Boolean.toString(ignoreEnv));
653654
contextBuilder.option("python.UnbufferedIO", Boolean.toString(unbufferedIO));
654655

@@ -687,14 +688,6 @@ protected void launch(Builder contextBuilder) {
687688
evalInternal(context, "__graalpython__.startup_wall_clock_ts = " + startupWallClockTime + "; __graalpython__.startup_nano = " + startupNanoTime);
688689
}
689690

690-
// Set LD_LIBRARY_PATH so that binaries built with sulong toolchain can find libc++.so
691-
evalInternal(context, """
692-
import os
693-
path = os.environ.get('LD_LIBRARY_PATH', '')
694-
sulong_path = os.pathsep.join(__graalpython__.get_toolchain_paths('LD_LIBRARY_PATH'))
695-
os.environ['LD_LIBRARY_PATH'] = path + os.pathsep + sulong_path if path else sulong_path
696-
""");
697-
698691
if (!quietFlag && (verboseFlag || (commandString == null && inputFile == null && stdinIsInteractive))) {
699692
print("Python " + evalInternal(context, "import sys; sys.version + ' on ' + sys.platform").asString());
700693
if (!noSite) {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/module/FrozenModules.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ private static final class Map {
214214
private static final PythonFrozenModule GRAALPY_JAVA = new PythonFrozenModule("GRAALPY_JAVA", "graalpy.java", false);
215215
private static final PythonFrozenModule GRAALPY_PIP_HOOK = new PythonFrozenModule("GRAALPY_PIP_HOOK", "graalpy.pip_hook", false);
216216
private static final PythonFrozenModule GRAALPY_UNICODEDATA = new PythonFrozenModule("GRAALPY_UNICODEDATA", "graalpy.unicodedata", false);
217+
private static final PythonFrozenModule GRAALPY_SULONG_SUPPORT = new PythonFrozenModule("GRAALPY_SULONG_SUPPORT", "graalpy.sulong_support", false);
217218
}
218219

219220
public static final PythonFrozenModule lookup(String name) {
@@ -572,6 +573,8 @@ public static final PythonFrozenModule lookup(String name) {
572573
return Map.GRAALPY_PIP_HOOK;
573574
case "graalpy.unicodedata":
574575
return Map.GRAALPY_UNICODEDATA;
576+
case "graalpy.sulong_support":
577+
return Map.GRAALPY_SULONG_SUPPORT;
575578
default:
576579
return null;
577580
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/PythonContext.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@
9494

9595
import com.oracle.graal.python.PythonLanguage;
9696
import com.oracle.graal.python.builtins.Python3Core;
97+
import com.oracle.graal.python.builtins.modules.ImpModuleBuiltins;
9798
import com.oracle.graal.python.builtins.modules.MathGuards;
9899
import com.oracle.graal.python.builtins.modules.ctypes.CtypesModuleBuiltins.CtypesThreadState;
99100
import com.oracle.graal.python.builtins.objects.PNone;
@@ -1430,6 +1431,9 @@ private void importSiteIfForced() {
14301431
// When InputFilePath is set, this is handled by __graalpython__.run_path
14311432
addSysPath0();
14321433
}
1434+
if (getOption(PythonOptions.SetupLLVMLibraryPaths)) {
1435+
ImpModuleBuiltins.importFrozenModuleObject(this, toTruffleStringUncached("graalpy.sulong_support"), false);
1436+
}
14331437
}
14341438

14351439
public void addSysPath0() {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/PythonOptions.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,9 @@ private PythonOptions() {
303303
@Option(category = OptionCategory.EXPERT, usageSyntax = "true|false", help = "Force to automatically import site.py module.") //
304304
public static final OptionKey<Boolean> ForceImportSite = new OptionKey<>(false);
305305

306+
@Option(category = OptionCategory.EXPERT, usageSyntax = "true|false", help = "Set-up library search paths to include GraalPy's LLVM toolchain library directories.") //
307+
public static final OptionKey<Boolean> SetupLLVMLibraryPaths = new OptionKey<>(false);
308+
306309
@Option(category = OptionCategory.EXPERT, usageSyntax = "true|false", help = "This option is set by the Python launcher to tell the language it can print exceptions directly") //
307310
public static final OptionKey<Boolean> AlwaysRunExcepthook = new OptionKey<>(false);
308311

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
2+
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3+
#
4+
# The Universal Permissive License (UPL), Version 1.0
5+
#
6+
# Subject to the condition set forth below, permission is hereby granted to any
7+
# person obtaining a copy of this software, associated documentation and/or
8+
# data (collectively the "Software"), free of charge and under any and all
9+
# copyright rights in the Software, and any and all patent rights owned or
10+
# freely licensable by each licensor hereunder covering either (i) the
11+
# unmodified Software as contributed to or provided by such licensor, or (ii)
12+
# the Larger Works (as defined below), to deal in both
13+
#
14+
# (a) the Software, and
15+
#
16+
# (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
17+
# one is included with the Software each a "Larger Work" to which the Software
18+
# is contributed by such licensors),
19+
#
20+
# without restriction, including without limitation the rights to copy, create
21+
# derivative works of, display, perform, and distribute the Software and make,
22+
# use, sell, offer for sale, import, export, have made, and have sold the
23+
# Software and the Larger Work(s), and to sublicense the foregoing rights on
24+
# either these or other terms.
25+
#
26+
# This license is subject to the following condition:
27+
#
28+
# The above copyright notice and either this complete permission notice or at a
29+
# minimum a reference to the UPL must be included in all copies or substantial
30+
# portions of the Software.
31+
#
32+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
33+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
34+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
35+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
36+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
37+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
38+
# SOFTWARE.
39+
40+
# Set library search path so that binaries built with sulong toolchain can find libc++.so
41+
import os, sys
42+
if sys.platform == "linux":
43+
var = "LD_LIBRARY_PATH"
44+
elif sys.platform == "darwin":
45+
var = "DYLD_LIBRARY_PATH"
46+
elif sys.platform == "win32":
47+
var = "PATH"
48+
path = os.environ.get(var, '')
49+
sulong_path = os.pathsep.join(__graalpython__.get_toolchain_paths('LD_LIBRARY_PATH'))
50+
os.environ[var] = f"{path}{os.pathsep}{sulong_path}" if path else sulong_path

0 commit comments

Comments
 (0)