Skip to content

Commit 8e37d48

Browse files
committed
[GR-44415] Use native toolchain by default
1 parent 00e1882 commit 8e37d48

File tree

6 files changed

+65
-7
lines changed

6 files changed

+65
-7
lines changed

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

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,15 @@ protected void launch(Builder contextBuilder) {
617617
}
618618
}
619619

620+
if (isLLVMToolchainLauncher()) {
621+
if (!hasContextOptionSetViaCommandLine("UseSystemToolchain")) {
622+
contextBuilder.option("python.UseSystemToolchain", "false");
623+
}
624+
if (!hasContextOptionSetViaCommandLine("NativeModules")) {
625+
contextBuilder.option("python.NativeModules", "");
626+
}
627+
}
628+
620629
if (relaunchArgs != null) {
621630
Iterator<String> it = origArgs.iterator();
622631
while (it.hasNext()) {
@@ -886,6 +895,9 @@ protected String getLanguageId() {
886895

887896
@Override
888897
protected void printHelp(OptionCategory maxCategory) {
898+
if (isLLVMToolchainLauncher()) {
899+
System.out.println("GraalPy launcher to use LLVM toolchain and Sulong execution of native extensions.\n");
900+
}
889901
System.out.println("usage: python [option] ... (-c cmd | file) [arg] ...\n" +
890902
"Options and arguments (and corresponding environment variables):\n" +
891903
"-B : this disables writing .py[co] files on import\n" +
@@ -958,6 +970,11 @@ protected void printHelp(OptionCategory maxCategory) {
958970
" This is required when starting subprocesses of python.\n" : ""));
959971
}
960972

973+
private boolean isLLVMToolchainLauncher() {
974+
String exeName = getResolvedExecutableName();
975+
return exeName != null && exeName.endsWith("-lt");
976+
}
977+
961978
@Override
962979
protected String[] getDefaultLanguages() {
963980
return new String[]{getLanguageId(), "llvm", "regex"};
@@ -1186,6 +1203,39 @@ private void subExec(List<String> args, List<String> subProcessDefs) {
11861203
}
11871204
}
11881205

1206+
protected String getResolvedExecutableName() {
1207+
// Note that with thin launchers, both graalpy and graalpy-managed are actual executables
1208+
// that load and start GraalPy from a shared library
1209+
if (ImageInfo.inImageRuntimeCode()) {
1210+
// This is the fastest, most straightforward way to get the name of the actual
1211+
// executable, i.e., with symlinks resolved all the way down to either graalpy or
1212+
// graalpy-managed.
1213+
String executableName = ProcessProperties.getExecutableName();
1214+
if (executableName != null) {
1215+
return executableName;
1216+
}
1217+
}
1218+
// The program name can be a symlink, or a command resolved via $PATH
1219+
// We use getLauncherExecName, which should resolve $PATH commands for us
1220+
String launcherExecName = getLauncherExecName();
1221+
if (launcherExecName == null) {
1222+
return null;
1223+
}
1224+
Path programPath = Paths.get(launcherExecName);
1225+
Path realPath;
1226+
try {
1227+
// toRealPath resolves symlinks along the way
1228+
realPath = programPath.toRealPath();
1229+
} catch (IOException ex) {
1230+
throw abort(String.format("Cannot determine the executable name from path: '%s'", programPath), 72);
1231+
}
1232+
Path f = realPath.getFileName();
1233+
if (f == null) {
1234+
throw abort(String.format("Cannot determine the executable name from path: '%s'", realPath), 73);
1235+
}
1236+
return f.toString();
1237+
}
1238+
11891239
private List<String> getCmdline(List<String> args, List<String> subProcessDefs) {
11901240
List<String> cmd = new ArrayList<>();
11911241
if (isAOT()) {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/GraalPythonModuleBuiltins.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ public void postInitialize(Python3Core core) {
229229
PList executableList = PythonObjectFactory.getUncached().createList(arr);
230230
mod.setAttribute(tsLiteral("executable_list"), executableList);
231231
mod.setAttribute(tsLiteral("ForeignType"), core.lookupType(PythonBuiltinClassType.ForeignObject));
232+
mod.setAttribute(tsLiteral("use_system_toolchain"), context.getOption(PythonOptions.UseSystemToolchain));
232233

233234
if (!context.getOption(PythonOptions.EnableDebuggingBuiltins)) {
234235
mod.setAttribute(tsLiteral("dump_truffle_ast"), PNone.NO_VALUE);

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
@@ -391,6 +391,9 @@ private PythonOptions() {
391391
@Option(category = OptionCategory.INTERNAL, help = "The list of the original command line arguments passed to the Python executable.") //
392392
public static final OptionKey<TruffleString> OrigArgv = new OptionKey<>(T_EMPTY_STRING, TS_OPTION_TYPE);
393393

394+
@Option(category = OptionCategory.EXPERT, help = "If true, use the system's toolchain for native extension compilation. Otherwise, use the LLVM Toolchain included with GraalVM.") //
395+
public static final OptionKey<Boolean> UseSystemToolchain = new OptionKey<>(true);
396+
394397
public static final OptionDescriptors DESCRIPTORS = new PythonOptionsOptionDescriptors();
395398

396399
@CompilationFinal(dimensions = 1) private static final OptionKey<?>[] ENGINE_OPTION_KEYS;

graalpython/lib-graalpython/_sysconfig.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ def _get_posix_vars():
5656
so_ext = ".so"
5757
assert _imp.extension_suffixes()[0] == "." + so_abi + so_ext, "mismatch between extension suffix to _imp.extension_suffixes"
5858

59-
use_native_toolchain = os.environ.get('NATIVE_TOOLCHAIN', '').lower() in {'1', 'true'}
60-
if use_native_toolchain:
59+
use_system_toolchain = __graalpython__.use_system_toolchain
60+
if use_system_toolchain:
6161
def get_toolchain(name):
6262
return dict(CC='cc',CXX='c++',AR='ar',RANLIB='ranlib',LD='ld',NM='nm')[name]
6363
else:
@@ -78,7 +78,7 @@ def get_toolchain(name):
7878
g['CC'] = get_toolchain('CC')
7979
g['CXX'] = toolchain_cxx if have_cxx else get_toolchain('CC') + ' --driver-mode=g++'
8080
opt_flags = ["-DNDEBUG"]
81-
if not use_native_toolchain:
81+
if not use_system_toolchain:
8282
opt_flags += ["-stdlib=libc++"]
8383
g['OPT'] = ' '.join(opt_flags)
8484
g['INCLUDEPY'] = python_inc
@@ -87,7 +87,7 @@ def get_toolchain(name):
8787
gnu_source = "-D_GNU_SOURCE=1"
8888
g['USE_GNU_SOURCE'] = gnu_source
8989
cflags_default = list(opt_flags)
90-
if not use_native_toolchain:
90+
if not use_system_toolchain:
9191
cflags_default += ["-Wno-unused-command-line-argument", "-DGRAALVM_PYTHON_LLVM"]
9292
if win32_native:
9393
cflags_default += ["-DMS_WINDOWS", "-DPy_ENABLE_SHARED", "-DHAVE_DECLSPEC_DLL"]

graalpython/lib-python/3/venv/__init__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ def create(self, env_dir):
7474
if self.with_pip:
7575
self._setup_pip(context)
7676
# Truffle change
77-
self._install_compilers(context)
77+
if not __graalpython__.use_system_toolchain:
78+
self._install_compilers(context)
7879
# End of Truffle change
7980
if not self.upgrade:
8081
self.setup_scripts(context)
@@ -183,7 +184,7 @@ def create_if_needed(d):
183184

184185

185186
def _install_compilers(self, context):
186-
"""Puts the Graal LLVM compiler tools on the path"""
187+
"""Puts the Graal LLVM compiler tools on the path."""
187188
bin_dir = os.path.join(context.env_dir, context.bin_name)
188189
for (tool_path, names) in __graalpython__.get_toolchain_tools_for_venv().items():
189190
for name in names:

mx.graalpython/mx_graalpython.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1937,7 +1937,7 @@ def verify_ci(dest_suite, common_ci_dir="ci_common", args=None, ext=('.jsonnet',
19371937
],
19381938
library_configs=[
19391939
mx_sdk.LanguageLibraryConfig(
1940-
launchers=['bin/<exe:graalpy>', 'bin/<exe:python>', 'bin/<exe:python3>'],
1940+
launchers=['bin/<exe:graalpy>', 'bin/<exe:graalpy-lt>', 'bin/<exe:python>', 'bin/<exe:python3>'],
19411941
jar_distributions=['graalpython:GRAALPYTHON-LAUNCHER'],
19421942
main_class=GRAALPYTHON_MAIN_CLASS,
19431943
build_args=[
@@ -2381,6 +2381,9 @@ def run(self, args, env=None, cwd=None, **kwargs):
23812381
env.update(mx.dependency("com.oracle.graal.python.cext").getBuildEnv())
23822382
env.update(self.subject.getBuildEnv())
23832383

2384+
# we need to use the sulong toolchain for this
2385+
args.insert(0, "--python.UseSystemToolchain=false")
2386+
23842387
# distutils will honor env variables CC, CFLAGS, LDFLAGS but we won't allow to change them,
23852388
# besides keeping custom sysroot, since our toolchain forwards to the system headers
23862389
for var in ["CC", "CFLAGS", "LDFLAGS"]:

0 commit comments

Comments
 (0)