Skip to content

Commit 5301c08

Browse files
committed
first iteration for linking and running "executables"
1 parent 7fe623d commit 5301c08

File tree

1 file changed

+92
-17
lines changed

1 file changed

+92
-17
lines changed

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

Lines changed: 92 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@
3333
import java.lang.management.ManagementFactory;
3434
import java.nio.file.Files;
3535
import java.nio.file.NoSuchFileException;
36+
import java.nio.file.Path;
3637
import java.nio.file.Paths;
38+
import java.nio.file.StandardCopyOption;
3739
import java.util.ArrayList;
3840
import java.util.Arrays;
3941
import java.util.List;
@@ -75,6 +77,7 @@ public static void main(String[] args) {
7577
private boolean stdinIsInteractive = System.console() != null;
7678
private boolean runCC = false;
7779
private boolean runLD = false;
80+
private boolean runLLI = false;
7881
private VersionAction versionAction = VersionAction.None;
7982

8083
@Override
@@ -147,6 +150,9 @@ protected List<String> preprocessArguments(List<String> arguments, Map<String, S
147150
runLD = true;
148151
programArgs.addAll(arguments.subList(i + 1, arguments.size()));
149152
return unrecognized;
153+
case "-LLI":
154+
runLLI = true;
155+
break;
150156
case "-debug-perf":
151157
subprocessArgs.add("Dgraal.TraceTruffleCompilation=true");
152158
subprocessArgs.add("Dgraal.TraceTrufflePerformanceWarnings=true");
@@ -212,6 +218,16 @@ protected void launch(Builder contextBuilder) {
212218
} else if (runCC) {
213219
launchCC();
214220
return;
221+
} else if (runLLI) {
222+
assert inputFile != null : "lli needs an input file";
223+
try (Context context = contextBuilder.build()) {
224+
try {
225+
context.eval(Source.newBuilder("llvm", new File(inputFile)).build());
226+
} catch (IOException e) {
227+
e.printStackTrace();
228+
}
229+
}
230+
return;
215231
}
216232

217233
if (!ignoreEnv) {
@@ -385,7 +401,7 @@ private void exec(String[] cmdarray) {
385401
System.err.print(cmd);
386402
System.err.print(" ");
387403
}
388-
System.err.println("");
404+
System.err.println();
389405
}
390406
processBuilder.command(cmdarray);
391407
int status = processBuilder.start().waitFor();
@@ -402,9 +418,22 @@ private void launchCC() {
402418

403419
// if we are started without -c, we need to run the linker later
404420
boolean runLinker = false;
421+
boolean linkExecutable = false;
405422
if (!programArgs.contains("-c")) {
406423
runLinker = true;
407424
programArgs.add(0, "-c");
425+
if (!programArgs.contains("-shared")) {
426+
assert !programArgs.contains("-static") : "static linking is not supported";
427+
linkExecutable = true;
428+
}
429+
}
430+
431+
if (verboseFlag) {
432+
System.err.print("[python] [CC] ");
433+
if (runLinker) {
434+
System.err.print("Running linker.");
435+
}
436+
System.err.println();
408437
}
409438

410439
// run the clang compiler to generate bc files
@@ -415,20 +444,29 @@ private void launchCC() {
415444
}
416445
String[] combine = combine(clangPrefix, args);
417446

447+
String output = getOutputFilename();
448+
449+
if (output != null && Files.exists(Paths.get(output))) {
450+
try {
451+
Files.delete(Paths.get(output));
452+
} catch (IOException e) {
453+
throw new RuntimeException(e);
454+
}
455+
}
418456
exec(combine);
419457

420-
String output = getOutputFilename();
421458
ArrayList<String> outputFiles = new ArrayList<>();
422-
if (output == null) {
423-
// if no explicit output filename was given, we search the commandline for files for
424-
// which now
425-
// have a bc file and optimize those
459+
if (output == null || !Files.exists(Paths.get(output))) {
460+
// if no explicit output filename was given or produced, we search the commandline for
461+
// files for which now have a bc file and optimize those
426462
for (String f : programArgs) {
427463
if (Files.exists(Paths.get(f))) {
428464
String bcFile = bcFileFromFilename(f);
429465
if (Files.exists(Paths.get(bcFile))) {
430466
outputFiles.add(bcFile);
431467
exec(combine(optPrefix, new String[]{bcFile, bcFile}));
468+
} else {
469+
outputFiles.add(f);
432470
}
433471
}
434472
}
@@ -440,12 +478,34 @@ private void launchCC() {
440478
}
441479
}
442480
if (runLinker) {
443-
link(outputFiles);
481+
if (linkExecutable) {
482+
assert !outputFiles.contains(getOutputFilename());
483+
String linkOutput = linkShared(outputFiles);
484+
try {
485+
Path linkedBCfile = Files.move(Paths.get(linkOutput), Paths.get(bcFileFromFilename(linkOutput)), StandardCopyOption.REPLACE_EXISTING);
486+
linkExecutable(linkOutput, linkedBCfile.toString());
487+
} catch (IOException e) {
488+
throw new RuntimeException(e);
489+
}
490+
} else {
491+
linkShared(outputFiles);
492+
}
444493
}
445494
}
446495

496+
private static void linkExecutable(String output, String bc) throws IOException {
497+
List<String> cmdline = getCmdline(Arrays.asList(), Arrays.asList());
498+
cmdline.add(bc);
499+
Files.write(Paths.get(output), ("#!" + String.join(" ", cmdline)).getBytes());
500+
}
501+
447502
private static String bcFileFromFilename(String f) {
448-
return f.substring(0, f.lastIndexOf('.') + 1) + "bc";
503+
int dotIdx = f.lastIndexOf('.');
504+
if (dotIdx > 1) {
505+
return f.substring(0, dotIdx + 1) + "bc";
506+
} else {
507+
return f + ".bc";
508+
}
449509
}
450510

451511
private void launchLD() {
@@ -475,15 +535,25 @@ private void launchLD() {
475535
System.err.print("Dropped linker arguments because we're using llvm-link:");
476536
System.err.println(droppedArgs.toString());
477537
}
478-
link(objectFiles);
538+
if (verboseFlag) {
539+
System.err.print("[python] [LD]");
540+
System.err.println();
541+
}
542+
linkShared(objectFiles);
479543
}
480544

481-
private void link(ArrayList<String> objectFiles) {
545+
private String linkShared(ArrayList<String> objectFiles) {
546+
String output = getOutputForLinking();
547+
exec(combine(combine(linkPrefix, new String[]{output}), objectFiles.toArray(new String[0])));
548+
return output;
549+
}
550+
551+
private String getOutputForLinking() {
482552
String output = getOutputFilename();
483553
if (output == null) {
484554
output = "a.out";
485555
}
486-
exec(combine(combine(linkPrefix, new String[]{output}), objectFiles.toArray(new String[0])));
556+
return output;
487557
}
488558

489559
private String getOutputFilename() {
@@ -722,6 +792,16 @@ private void setupREPL(Context context, ConsoleHandler consoleHandler) {
722792
* re-execute the process with the additional options.
723793
*/
724794
private static void subExec(List<String> args, List<String> subProcessDefs) {
795+
List<String> cmd = getCmdline(args, subProcessDefs);
796+
try {
797+
System.exit(new ProcessBuilder(cmd.toArray(new String[0])).inheritIO().start().waitFor());
798+
} catch (IOException | InterruptedException e) {
799+
System.err.println(e.getMessage());
800+
System.exit(-1);
801+
}
802+
}
803+
804+
private static List<String> getCmdline(List<String> args, List<String> subProcessDefs) {
725805
List<String> cmd = new ArrayList<>();
726806
if (isAOT()) {
727807
cmd.add(ProcessProperties.getExecutableName());
@@ -754,12 +834,7 @@ private static void subExec(List<String> args, List<String> subProcessDefs) {
754834
}
755835

756836
cmd.addAll(args);
757-
try {
758-
System.exit(new ProcessBuilder(cmd.toArray(new String[0])).inheritIO().start().waitFor());
759-
} catch (IOException | InterruptedException e) {
760-
System.err.println(e.getMessage());
761-
System.exit(-1);
762-
}
837+
return cmd;
763838
}
764839

765840
private static final class ExitException extends RuntimeException {

0 commit comments

Comments
 (0)