Skip to content

Commit 958aeb5

Browse files
committed
[GR-20372] [GR-20822] Do not store paths to the build system in the prebuilt image heap
PullRequest: graalpython/794
2 parents ea2795d + 38b2729 commit 958aeb5

File tree

6 files changed

+185
-87
lines changed

6 files changed

+185
-87
lines changed

ci.jsonnet

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{ overlay: "0804360960e04fffe3fc4ed87c6b5f8b65641c78" }
1+
{ "overlay": "7ca344fe93c220d6200eb3b26ddaf65a7651c386" }

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@
9797
import com.oracle.truffle.api.profiles.ConditionProfile;
9898
import com.oracle.truffle.llvm.api.Toolchain;
9999

100+
import org.graalvm.nativeimage.ImageInfo;
101+
100102
@CoreFunctions(defineModule = "sys")
101103
public class SysModuleBuiltins extends PythonBuiltins {
102104
public static final String LLVM_LANGUAGE = "llvm";
@@ -190,10 +192,12 @@ public void postInitialize(PythonCore core) {
190192
String stdlibHome = context.getStdlibHome();
191193
String capiHome = context.getCAPIHome();
192194

193-
sys.setAttribute("executable", PythonOptions.getOption(context, PythonOptions.Executable));
195+
if (!ImageInfo.inImageBuildtimeCode()) {
196+
sys.setAttribute("executable", PythonOptions.getOption(context, PythonOptions.Executable));
197+
sys.setAttribute("graal_python_home", context.getLanguage().getHome());
198+
}
194199
sys.setAttribute("graal_python_jython_emulation_enabled", PythonOptions.getOption(context, PythonOptions.EmulateJython));
195200
sys.setAttribute("graal_python_host_import_enabled", context.getEnv().isHostLookupAllowed());
196-
sys.setAttribute("graal_python_home", context.getLanguage().getHome());
197201
sys.setAttribute("graal_python_core_home", coreHome);
198202
sys.setAttribute("graal_python_stdlib_home", stdlibHome);
199203
sys.setAttribute("graal_python_capi_home", capiHome);

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

Lines changed: 42 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017, 2019, Oracle and/or its affiliates.
2+
* Copyright (c) 2017, 2020, Oracle and/or its affiliates.
33
* Copyright (c) 2013, Regents of the University of California
44
*
55
* All rights reserved.
@@ -51,15 +51,12 @@
5151
import java.util.function.Supplier;
5252
import java.util.logging.Level;
5353

54-
import com.oracle.graal.python.builtins.objects.cext.capi.CApiContext;
55-
import org.graalvm.nativeimage.ImageInfo;
56-
import org.graalvm.options.OptionValues;
57-
5854
import com.oracle.graal.python.PythonLanguage;
5955
import com.oracle.graal.python.builtins.objects.PNone;
6056
import com.oracle.graal.python.builtins.objects.PythonAbstractObject;
6157
import com.oracle.graal.python.builtins.objects.cext.PThreadState;
6258
import com.oracle.graal.python.builtins.objects.cext.PythonNativeClass;
59+
import com.oracle.graal.python.builtins.objects.cext.capi.CApiContext;
6360
import com.oracle.graal.python.builtins.objects.common.HashingCollectionNodes.GetDictStorageNode;
6461
import com.oracle.graal.python.builtins.objects.common.HashingStorage;
6562
import com.oracle.graal.python.builtins.objects.common.HashingStorageNodes.GetItemInteropNode;
@@ -95,6 +92,9 @@
9592
import com.oracle.truffle.api.nodes.Node;
9693
import com.oracle.truffle.api.utilities.CyclicAssumption;
9794

95+
import org.graalvm.nativeimage.ImageInfo;
96+
import org.graalvm.options.OptionValues;
97+
9898
public final class PythonContext {
9999

100100
private static final class PythonThreadState {
@@ -464,28 +464,34 @@ public void initializeHomeAndPrefixPaths(Env newEnv, String languageHome) {
464464
stdLibHome = newEnv.getOptions().get(PythonOptions.StdLibHome);
465465
capiHome = newEnv.getOptions().get(PythonOptions.CAPI);
466466

467-
PythonCore.writeInfo((MessageFormat.format("Initial locations:" +
467+
PythonCore.writeInfo(() -> MessageFormat.format("Initial locations:" +
468468
"\n\tLanguage home: {0}" +
469469
"\n\tSysPrefix: {1}" +
470470
"\n\tBaseSysPrefix: {2}" +
471471
"\n\tCoreHome: {3}" +
472472
"\n\tStdLibHome: {4}" +
473-
"\n\tCAPI: {5}", languageHome, sysPrefix, basePrefix, coreHome, stdLibHome, capiHome)));
473+
"\n\tCAPI: {5}", languageHome, sysPrefix, basePrefix, coreHome, stdLibHome, capiHome));
474474

475-
TruffleFile home = null;
476-
if (languageHome != null) {
477-
home = newEnv.getInternalTruffleFile(languageHome);
475+
String envHome = null;
476+
try {
477+
envHome = System.getenv("GRAAL_PYTHONHOME");
478+
} catch (SecurityException e) {
478479
}
479480

480-
try {
481-
String envHome = System.getenv("GRAAL_PYTHONHOME");
482-
if (envHome != null) {
483-
TruffleFile envHomeFile = newEnv.getInternalTruffleFile(envHome);
484-
if (envHomeFile.isDirectory()) {
485-
home = envHomeFile;
486-
}
481+
final TruffleFile home;
482+
if (languageHome != null && envHome == null) {
483+
home = newEnv.getInternalTruffleFile(languageHome);
484+
} else if (envHome != null) {
485+
boolean envHomeIsDirectory = false;
486+
TruffleFile envHomeFile = null;
487+
try {
488+
envHomeFile = newEnv.getInternalTruffleFile(envHome);
489+
envHomeIsDirectory = envHomeFile.isDirectory();
490+
} catch (SecurityException e) {
487491
}
488-
} catch (SecurityException e) {
492+
home = envHomeIsDirectory ? envHomeFile : null;
493+
} else {
494+
home = null;
489495
}
490496

491497
if (home != null) {
@@ -528,15 +534,26 @@ public void initializeHomeAndPrefixPaths(Env newEnv, String languageHome) {
528534
if (capiHome.isEmpty()) {
529535
capiHome = coreHome;
530536
}
537+
}
531538

532-
PythonCore.writeInfo((MessageFormat.format("Updated locations:" +
533-
"\n\tLanguage home: {0}" +
534-
"\n\tSysPrefix: {1}" +
535-
"\n\tSysBasePrefix: {2}" +
536-
"\n\tCoreHome: {3}" +
537-
"\n\tStdLibHome: {4}" +
538-
"\n\tExecutable: {6}", home.getPath(), sysPrefix, basePrefix, coreHome, stdLibHome, newEnv.getOptions().get(PythonOptions.Executable))));
539+
if (ImageInfo.inImageBuildtimeCode()) {
540+
// use relative paths at buildtime to avoid freezing buildsystem paths
541+
TruffleFile base = newEnv.getInternalTruffleFile("").getAbsoluteFile();
542+
sysPrefix = base.relativize(newEnv.getInternalTruffleFile(sysPrefix)).getPath();
543+
basePrefix = base.relativize(newEnv.getInternalTruffleFile(basePrefix)).getPath();
544+
coreHome = base.relativize(newEnv.getInternalTruffleFile(coreHome)).getPath();
545+
stdLibHome = base.relativize(newEnv.getInternalTruffleFile(stdLibHome)).getPath();
546+
capiHome = base.relativize(newEnv.getInternalTruffleFile(capiHome)).getPath();
539547
}
548+
549+
PythonCore.writeInfo(() -> MessageFormat.format("Updated locations:" +
550+
"\n\tLanguage home: {0}" +
551+
"\n\tSysPrefix: {1}" +
552+
"\n\tBaseSysPrefix: {2}" +
553+
"\n\tCoreHome: {3}" +
554+
"\n\tStdLibHome: {4}" +
555+
"\n\tExecutable: {5}" +
556+
"\n\tCAPI: {6}", home != null ? home.getPath() : "", sysPrefix, basePrefix, coreHome, stdLibHome, newEnv.getOptions().get(PythonOptions.Executable), capiHome));
540557
}
541558

542559
@TruffleBoundary

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017, 2019, Oracle and/or its affiliates.
2+
* Copyright (c) 2017, 2020, Oracle and/or its affiliates.
33
* Copyright (c) 2013, Regents of the University of California
44
*
55
* All rights reserved.
@@ -25,6 +25,8 @@
2525
*/
2626
package com.oracle.graal.python.runtime;
2727

28+
import java.util.function.Supplier;
29+
2830
import com.oracle.graal.python.PythonLanguage;
2931
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
3032
import com.oracle.graal.python.builtins.objects.floats.PFloat;
@@ -90,4 +92,7 @@ static void writeInfo(String message) {
9092
PythonLanguage.getLogger().fine(message);
9193
}
9294

95+
static void writeInfo(Supplier<String> messageSupplier) {
96+
PythonLanguage.getLogger().fine(messageSupplier);
97+
}
9398
}

mx.graalpython/mx_graalpython.py

Lines changed: 128 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -738,77 +738,149 @@ def delete_self_if_testdownstream(args):
738738
shutil.rmtree(SUITE.dir, ignore_errors=True)
739739

740740

741-
def update_import(name, rev="origin/master", callback=None):
742-
primary = mx.primary_suite()
743-
dep_dir = mx.suite(name).vc_dir
741+
def update_import(name, suite_py, rev="origin/master"):
742+
mx_name = "mx." + name
743+
parent = os.path.join(SUITE.dir, "..")
744+
for dirpath,dirnames,filenames in os.walk(parent):
745+
if os.path.sep in os.path.relpath(dirpath, parent):
746+
dirnames.clear() # we're looking for siblings or sibling-subdirs
747+
elif name in dirnames:
748+
dep_dir = os.path.join(os.path.join(dirpath))
749+
break
750+
if not dep_dir:
751+
mx.warn("could not find suite %s to update" % name)
752+
return
744753
vc = mx.VC.get_vc(dep_dir)
745-
vc.pull(dep_dir, update=False)
754+
if rev != "HEAD":
755+
vc.pull(dep_dir, update=False)
746756
vc.update(dep_dir, rev=rev)
747757
tip = str(vc.tip(dep_dir)).strip()
748758
contents = None
749-
suitefile = os.path.join(primary.dir, "mx." + primary.name, "suite.py")
750-
with open(suitefile, 'r') as f:
759+
with open(suite_py, 'r') as f:
751760
contents = f.read()
752761
dep_re = re.compile(r"['\"]name['\"]:\s+['\"]%s['\"],\s+['\"]version['\"]:\s+['\"]([a-z0-9]+)['\"]" % name, re.MULTILINE)
753762
dep_match = dep_re.search(contents)
754763
if dep_match:
755764
start = dep_match.start(1)
756765
end = dep_match.end(1)
757766
assert end - start == len(tip)
758-
mx.update_file(suitefile, "".join([contents[:start], tip, contents[end:]]), showDiff=True)
759-
if callback:
760-
callback()
761-
else:
762-
mx.abort("%s not found in %s" % (name, suitefile))
767+
mx.update_file(suite_py, "".join([contents[:start], tip, contents[end:]]), showDiff=True)
763768

764769

765770
def update_import_cmd(args):
766-
"""Update our mx or overlay imports"""
767-
try:
768-
args.remove("--no-pull")
769-
except ValueError:
770-
rev = "origin/master"
771-
else:
772-
rev = "HEAD"
773-
if not args:
774-
args = ["truffle"]
775-
if "overlay" in args:
776-
mx.log("Updating overlays")
777-
dirs = os.listdir(os.path.join(SUITE.dir, ".."))
778-
for d in dirs:
779-
if d.startswith("graalpython"):
780-
d = os.path.join(SUITE.dir, "..", d)
781-
jsonnetfile = os.path.join(d, "ci.jsonnet")
782-
if not os.path.exists(jsonnetfile):
783-
continue
784-
overlaydir = os.path.join(d, "..", "ci-overlays")
785-
if not os.path.exists(overlaydir):
786-
mx.abort("Overlays must be next to repo")
787-
vc = mx.VC.get_vc(overlaydir)
788-
tip = str(vc.tip(overlaydir)).strip()
789-
with open(jsonnetfile, "w") as f:
790-
f.write('{ overlay: "%s" }\n' % tip)
791-
args.remove("overlay")
792-
if "sulong" in args:
793-
args.append("regex")
794-
if "regex" in args:
795-
args.append("sulong")
796-
if "truffle" in args:
797-
args.remove("truffle")
798-
args += ["sulong", "regex"]
799-
if "sulong" in args:
800-
join = os.path.join
801-
callback = lambda: shutil.copy(
802-
join(mx.dependency("SULONG_LEGACY").output, "include", "truffle.h"),
803-
join(SUITE.dir, "graalpython", "com.oracle.graal.python.cext", "include", "truffle.h")
804-
) and shutil.copy(
805-
join(mx.dependency("SULONG_HOME").output, "include", "polyglot.h"),
806-
join(SUITE.dir, "graalpython", "com.oracle.graal.python.cext", "include", "polyglot.h")
807-
)
771+
"""Update our imports"""
772+
join = os.path.join
773+
vc = SUITE.vc
774+
775+
current_branch = vc.active_branch(SUITE.dir)
776+
if current_branch == "master":
777+
mx.abort("updating imports should be done on a branch")
778+
if vc.isDirty(SUITE.dir):
779+
mx.abort("updating imports should be done on a clean branch")
780+
781+
suite_py_files = []
782+
local_names = []
783+
repos = []
784+
785+
# find all relevant other repos that may need updating
786+
for sibling in os.listdir(os.path.join(SUITE.dir, "..")):
787+
if sibling.startswith("graalpython"):
788+
dd = os.path.join(SUITE.dir, "..", sibling)
789+
jsonnetfile = os.path.join(dd, "ci.jsonnet")
790+
if os.path.exists(jsonnetfile):
791+
local_names.append(sibling)
792+
repos.append(dd)
793+
for dirpath,dirnames,filenames in os.walk(dd):
794+
mx_dirs = list(filter(lambda x: x.startswith("mx."), dirnames))
795+
if mx_dirs:
796+
dirnames[:] = mx_dirs # don't go deeper once we found some mx dirs
797+
dirnames[:] = list(filter(lambda x: not (x.startswith(".") or x.startswith("__")), dirnames))
798+
if "suite.py" in filenames:
799+
suite_py_files.append(join(dirpath, "suite.py"))
800+
801+
# make sure all other repos are clean and on the same branch
802+
for d in repos:
803+
if vc.isDirty(d):
804+
mx.abort("repo %s is not clean" % d)
805+
d_branch = vc.active_branch(d)
806+
if d_branch == current_branch:
807+
pass
808+
elif d_branch == "master":
809+
vc.set_branch(d, current_branch, with_remote=False)
810+
vc.git_command(d, ["checkout", current_branch], abortOnError=True)
811+
else:
812+
mx.abort("repo %s is not on master or on %s" % (d, current_branch))
813+
814+
# make sure we can update the overlays
815+
overlaydir = join(SUITE.dir, "..", "ci-overlays")
816+
if not os.path.exists(overlaydir):
817+
mx.abort("Overlays repo must be next to graalpython repo")
818+
vc = mx.VC.get_vc(overlaydir)
819+
if vc.isDirty(overlaydir):
820+
mx.abort("overlays repo must be clean")
821+
overlaybranch = vc.active_branch(overlaydir)
822+
if overlaybranch == "master":
823+
vc.pull(overlaydir)
824+
vc.set_branch(overlaydir, current_branch, with_remote=False)
825+
vc.git_command(overlaydir, ["checkout", current_branch], abortOnError=True)
826+
elif overlaybranch == current_branch:
827+
pass
808828
else:
809-
callback = None
810-
for name in set(args):
811-
update_import(name, rev=rev, callback=callback)
829+
mx.abort("overlays repo must be on master or branch %s" % current_branch)
830+
831+
# find all imports we might update
832+
imports_to_update = set()
833+
for suite_py in suite_py_files:
834+
dict = {}
835+
with open(suite_py) as f:
836+
exec(f.read(), dict, dict)
837+
for suite in dict["suite"].get("imports", {}).get("suites", []):
838+
import_name = suite["name"]
839+
if suite.get("version") and import_name not in local_names:
840+
imports_to_update.add(import_name)
841+
842+
# now update all imports
843+
for name in imports_to_update:
844+
for idx, suite_py in enumerate(suite_py_files):
845+
update_import(name, suite_py, rev=("HEAD" if idx else "origin/master"))
846+
847+
# copy files we inline from our imports
848+
shutil.copy(
849+
join(mx.dependency("SULONG_LEGACY").output, "include", "truffle.h"),
850+
join(SUITE.dir, "graalpython", "com.oracle.graal.python.cext", "include", "truffle.h"))
851+
shutil.copy(
852+
join(mx.dependency("SULONG_HOME").output, "include", "polyglot.h"),
853+
join(SUITE.dir, "graalpython", "com.oracle.graal.python.cext", "include", "polyglot.h"))
854+
shutil.copy(
855+
join(mx.suite("truffle").dir, "..", "common.json"),
856+
join(overlaydir, "python", "graal-common.json"))
857+
858+
repos_updated = []
859+
860+
# commit ci if dirty
861+
overlaytip = str(vc.tip(overlaydir)).strip()
862+
if vc.isDirty(overlaydir):
863+
vc.commit(overlaydir, "Update Python imports")
864+
overlaytip = str(vc.tip(overlaydir)).strip()
865+
repos_updated.append(overlaydir)
866+
867+
# update ci import in all our repos, commit the full update, and push verbosely
868+
prev_verbosity = mx._opts.very_verbose
869+
for repo in repos:
870+
jsonnetfile = os.path.join(repo, "ci.jsonnet")
871+
with open(jsonnetfile, "w") as f:
872+
f.write('{ "overlay": "%s" }\n' % overlaytip)
873+
if vc.isDirty(repo):
874+
vc.commit(repo, "Update imports")
875+
try:
876+
mx._opts.very_verbose = True
877+
vc.git_command(repo, ["push", "-u", "origin", "HEAD:%s" % current_branch], abortOnError=True)
878+
finally:
879+
mx._opts.very_verbose = prev_verbosity
880+
repos_updated.append(repo)
881+
882+
if repos_updated:
883+
mx.log("These repos were updated: " + ", ".join(repos_updated))
812884

813885

814886
def python_style_checks(args):

mx.graalpython/suite.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,15 @@
4444
},
4545
{
4646
"name": "sulong",
47-
"version": "8fb566e1b9dbfb9007c379b6908ed26ec29cb243",
47+
"version": "563408ea34f8593f488e1b8cd6d7101effb91f2d",
4848
"subdir": True,
4949
"urls": [
5050
{"url": "https://github.com/oracle/graal", "kind": "git"},
5151
]
5252
},
5353
{
5454
"name": "regex",
55-
"version": "8fb566e1b9dbfb9007c379b6908ed26ec29cb243",
55+
"version": "563408ea34f8593f488e1b8cd6d7101effb91f2d",
5656
"subdir": True,
5757
"urls": [
5858
{"url": "https://github.com/oracle/graal", "kind": "git"},

0 commit comments

Comments
 (0)