Skip to content

Commit 15fa584

Browse files
committed
by default generate a native executable when asked to do so, so that numpy build tools can introspect
1 parent 91c534e commit 15fa584

File tree

3 files changed

+56
-28
lines changed

3 files changed

+56
-28
lines changed

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

Lines changed: 51 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,22 @@ public class GraalPythonCC extends GraalPythonCompiler {
5555
private String outputFilename;
5656
private boolean linkExecutable;
5757
private boolean link;
58+
private boolean linkLLI;
5859
private Boolean compile;
5960
private List<String> clangArgs;
61+
private List<String> execLinkArgs;
6062
private List<String> fileInputs;
6163

6264
GraalPythonCC() {
6365
}
6466

67+
private static List<String> execLinkPrefix = Arrays.asList(new String[]{
68+
"clang",
69+
"-fembed-bitcode",
70+
"-fPIC",
71+
"-ggdb",
72+
"-O1",
73+
});
6574
private static List<String> clangPrefix = Arrays.asList(new String[]{
6675
"clang",
6776
"-emit-llvm",
@@ -100,10 +109,12 @@ private void run(String[] args) {
100109
private void parseOptions(String[] args) {
101110
outputFilename = A_OUT;
102111
linkExecutable = true;
112+
linkLLI = false;
103113
link = true;
104114
verbose = false;
105115
compile = null;
106116
clangArgs = new ArrayList<>(clangPrefix);
117+
execLinkArgs = new ArrayList<>(execLinkPrefix);
107118
fileInputs = new ArrayList<>();
108119
for (int i = 0; i < args.length; i++) {
109120
String arg = args[i];
@@ -123,6 +134,9 @@ private void parseOptions(String[] args) {
123134
link = false;
124135
linkExecutable = false;
125136
break;
137+
case "--link-lli-scripts":
138+
linkLLI = true;
139+
continue; // skip adding this to clang args
126140
case "-v":
127141
if (!verbose) {
128142
verbose = true;
@@ -144,6 +158,8 @@ private void parseOptions(String[] args) {
144158
throw new RuntimeException("cannot mix source and compiled sources");
145159
}
146160
fileInputs.add(arg);
161+
} else {
162+
execLinkArgs.add(arg);
147163
}
148164
}
149165
clangArgs.add(arg);
@@ -173,6 +189,13 @@ private void launchCC() {
173189
String bcFile = bcFileFromFilename(f);
174190
assert Files.exists(Paths.get(bcFile));
175191
optFile(bcFile);
192+
try {
193+
String objFile = objectFileFromFilename(f);
194+
logV("Optimized:", bcFile, "->", objFile);
195+
Files.move(Paths.get(bcFile), Paths.get(objFile));
196+
} catch (IOException e) {
197+
throw new RuntimeException(e);
198+
}
176199
}
177200
} else {
178201
optFile(outputFilename);
@@ -192,15 +215,21 @@ private void launchCC() {
192215
}
193216
}
194217

195-
private static void linkExecutable(String executableScript, String linkedBcFile) throws IOException {
196-
List<String> cmdline = GraalPythonMain.getCmdline(Arrays.asList(), Arrays.asList());
197-
cmdline.add("-LLI");
198-
cmdline.add(linkedBcFile);
199-
Path executablePath = Paths.get(executableScript);
200-
Files.write(executablePath, String.join(" ", cmdline).getBytes());
201-
HashSet<PosixFilePermission> perms = new HashSet<>(Arrays.asList(new PosixFilePermission[]{PosixFilePermission.OWNER_EXECUTE, PosixFilePermission.GROUP_EXECUTE}));
202-
perms.addAll(Files.getPosixFilePermissions(executablePath));
203-
Files.setPosixFilePermissions(executablePath, perms);
218+
private void linkExecutable(String executableScript, String linkedBcFile) throws IOException {
219+
if (linkLLI) {
220+
List<String> cmdline = GraalPythonMain.getCmdline(Arrays.asList(), Arrays.asList());
221+
cmdline.add("-LLI");
222+
cmdline.add(linkedBcFile);
223+
Path executablePath = Paths.get(executableScript);
224+
Files.write(executablePath, String.join(" ", cmdline).getBytes());
225+
HashSet<PosixFilePermission> perms = new HashSet<>(Arrays.asList(new PosixFilePermission[]{PosixFilePermission.OWNER_EXECUTE, PosixFilePermission.GROUP_EXECUTE}));
226+
perms.addAll(Files.getPosixFilePermissions(executablePath));
227+
} else {
228+
execLinkArgs.add(linkedBcFile);
229+
execLinkArgs.add("-o");
230+
execLinkArgs.add(executableScript);
231+
exec(execLinkArgs);
232+
}
204233
}
205234

206235
private void linkShared(List<String> bitcodeFiles) {
@@ -221,4 +250,17 @@ private void optFile(String bcFile) {
221250
opt.add(bcFile);
222251
exec(opt);
223252
}
253+
254+
private static String bcFileFromFilename(String f) {
255+
int dotIdx = f.lastIndexOf('.');
256+
if (dotIdx > 1) {
257+
return f.substring(0, dotIdx + 1) + "bc";
258+
} else {
259+
return f + ".bc";
260+
}
261+
}
262+
263+
private static String objectFileFromFilename(String f) {
264+
return bcFileFromFilename(f).replaceAll("\\.bc$", ".o");
265+
}
224266
}

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

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,20 +41,12 @@
4141
package com.oracle.graal.python.shell;
4242

4343
import java.io.IOException;
44+
import java.util.Arrays;
4445
import java.util.List;
4546

4647
public class GraalPythonCompiler {
4748
protected static final String A_OUT = "a.out";
4849

49-
protected static String bcFileFromFilename(String f) {
50-
int dotIdx = f.lastIndexOf('.');
51-
if (dotIdx > 1) {
52-
return f.substring(0, dotIdx + 1) + "bc";
53-
} else {
54-
return f + ".bc";
55-
}
56-
}
57-
5850
protected boolean verbose;
5951

6052
public GraalPythonCompiler() {
@@ -91,4 +83,7 @@ protected void logV(List<String> args) {
9183
logV(null, args);
9284
}
9385

86+
protected void logV(String... args) {
87+
logV(null, Arrays.asList(args));
88+
}
9489
}

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

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,6 @@
4040
*/
4141
package com.oracle.graal.python.shell;
4242

43-
import java.nio.file.Files;
44-
import java.nio.file.Paths;
4543
import java.util.ArrayList;
4644
import java.util.Arrays;
4745
import java.util.List;
@@ -84,14 +82,7 @@ private void parseOptions(String[] args) {
8482
verbose = true;
8583
break;
8684
default:
87-
if (arg.endsWith(".o")) {
88-
String bc = bcFileFromFilename(arg);
89-
if (Files.exists(Paths.get(bc))) {
90-
fileInputs.add(bc);
91-
} else {
92-
fileInputs.add(arg);
93-
}
94-
} else if (arg.endsWith(".bc")) {
85+
if (arg.endsWith(".o") || arg.endsWith(".bc")) {
9586
fileInputs.add(arg);
9687
} else {
9788
droppedArgs.add(arg);

0 commit comments

Comments
 (0)