33
33
import java .lang .management .ManagementFactory ;
34
34
import java .nio .file .Files ;
35
35
import java .nio .file .NoSuchFileException ;
36
+ import java .nio .file .Path ;
36
37
import java .nio .file .Paths ;
38
+ import java .nio .file .StandardCopyOption ;
37
39
import java .util .ArrayList ;
38
40
import java .util .Arrays ;
39
41
import java .util .List ;
@@ -75,6 +77,7 @@ public static void main(String[] args) {
75
77
private boolean stdinIsInteractive = System .console () != null ;
76
78
private boolean runCC = false ;
77
79
private boolean runLD = false ;
80
+ private boolean runLLI = false ;
78
81
private VersionAction versionAction = VersionAction .None ;
79
82
80
83
@ Override
@@ -147,6 +150,9 @@ protected List<String> preprocessArguments(List<String> arguments, Map<String, S
147
150
runLD = true ;
148
151
programArgs .addAll (arguments .subList (i + 1 , arguments .size ()));
149
152
return unrecognized ;
153
+ case "-LLI" :
154
+ runLLI = true ;
155
+ break ;
150
156
case "-debug-perf" :
151
157
subprocessArgs .add ("Dgraal.TraceTruffleCompilation=true" );
152
158
subprocessArgs .add ("Dgraal.TraceTrufflePerformanceWarnings=true" );
@@ -212,6 +218,16 @@ protected void launch(Builder contextBuilder) {
212
218
} else if (runCC ) {
213
219
launchCC ();
214
220
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 ;
215
231
}
216
232
217
233
if (!ignoreEnv ) {
@@ -385,7 +401,7 @@ private void exec(String[] cmdarray) {
385
401
System .err .print (cmd );
386
402
System .err .print (" " );
387
403
}
388
- System .err .println ("" );
404
+ System .err .println ();
389
405
}
390
406
processBuilder .command (cmdarray );
391
407
int status = processBuilder .start ().waitFor ();
@@ -402,9 +418,22 @@ private void launchCC() {
402
418
403
419
// if we are started without -c, we need to run the linker later
404
420
boolean runLinker = false ;
421
+ boolean linkExecutable = false ;
405
422
if (!programArgs .contains ("-c" )) {
406
423
runLinker = true ;
407
424
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 ();
408
437
}
409
438
410
439
// run the clang compiler to generate bc files
@@ -415,20 +444,29 @@ private void launchCC() {
415
444
}
416
445
String [] combine = combine (clangPrefix , args );
417
446
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
+ }
418
456
exec (combine );
419
457
420
- String output = getOutputFilename ();
421
458
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
426
462
for (String f : programArgs ) {
427
463
if (Files .exists (Paths .get (f ))) {
428
464
String bcFile = bcFileFromFilename (f );
429
465
if (Files .exists (Paths .get (bcFile ))) {
430
466
outputFiles .add (bcFile );
431
467
exec (combine (optPrefix , new String []{bcFile , bcFile }));
468
+ } else {
469
+ outputFiles .add (f );
432
470
}
433
471
}
434
472
}
@@ -440,12 +478,34 @@ private void launchCC() {
440
478
}
441
479
}
442
480
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
+ }
444
493
}
445
494
}
446
495
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
+
447
502
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
+ }
449
509
}
450
510
451
511
private void launchLD () {
@@ -475,15 +535,25 @@ private void launchLD() {
475
535
System .err .print ("Dropped linker arguments because we're using llvm-link:" );
476
536
System .err .println (droppedArgs .toString ());
477
537
}
478
- link (objectFiles );
538
+ if (verboseFlag ) {
539
+ System .err .print ("[python] [LD]" );
540
+ System .err .println ();
541
+ }
542
+ linkShared (objectFiles );
479
543
}
480
544
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 () {
482
552
String output = getOutputFilename ();
483
553
if (output == null ) {
484
554
output = "a.out" ;
485
555
}
486
- exec ( combine ( combine ( linkPrefix , new String []{ output }), objectFiles . toArray ( new String [ 0 ]))) ;
556
+ return output ;
487
557
}
488
558
489
559
private String getOutputFilename () {
@@ -722,6 +792,16 @@ private void setupREPL(Context context, ConsoleHandler consoleHandler) {
722
792
* re-execute the process with the additional options.
723
793
*/
724
794
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 ) {
725
805
List <String > cmd = new ArrayList <>();
726
806
if (isAOT ()) {
727
807
cmd .add (ProcessProperties .getExecutableName ());
@@ -754,12 +834,7 @@ private static void subExec(List<String> args, List<String> subProcessDefs) {
754
834
}
755
835
756
836
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 ;
763
838
}
764
839
765
840
private static final class ExitException extends RuntimeException {
0 commit comments