Skip to content

Commit 4b4add5

Browse files
committed
support linking of libraries built during the numpy setup into the final library
1 parent 8cad13e commit 4b4add5

File tree

1 file changed

+87
-0
lines changed
  • graalpython/com.oracle.graal.python.shell/src/com/oracle/graal/python/shell

1 file changed

+87
-0
lines changed

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

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,20 @@
4040
*/
4141
package com.oracle.graal.python.shell;
4242

43+
import java.io.BufferedReader;
44+
import java.io.File;
45+
import java.io.IOException;
46+
import java.lang.ProcessBuilder.Redirect;
47+
import java.nio.file.Files;
48+
import java.nio.file.Path;
49+
import java.nio.file.Paths;
4350
import java.util.ArrayList;
4451
import java.util.Arrays;
52+
import java.util.Collection;
4553
import java.util.List;
4654

55+
import jline.internal.InputStreamReader;
56+
4757
public class GraalPythonLD extends GraalPythonCompiler {
4858
private static List<String> linkPrefix = Arrays.asList(new String[]{
4959
"llvm-link",
@@ -68,6 +78,7 @@ private void parseOptions(String[] args) {
6878
ldArgs = new ArrayList<>(linkPrefix);
6979
fileInputs = new ArrayList<>();
7080
List<String> droppedArgs = new ArrayList<>();
81+
List<String> libraryDirs = new ArrayList<>();
7182
for (int i = 0; i < args.length; i++) {
7283
String arg = args[i];
7384
switch (arg) {
@@ -84,6 +95,22 @@ private void parseOptions(String[] args) {
8495
default:
8596
if (arg.endsWith(".o") || arg.endsWith(".bc")) {
8697
fileInputs.add(arg);
98+
} else if (arg.startsWith("-L")) {
99+
libraryDirs.add(arg.substring(2));
100+
} else if (arg.startsWith("-l")) {
101+
List<String> bcFiles = searchLib(libraryDirs, arg.substring(2));
102+
for (String bcFile : bcFiles) {
103+
try {
104+
if (Files.probeContentType(Paths.get(bcFile)).contains("llvm-ir-bitcode")) {
105+
logV("library input:", bcFile);
106+
fileInputs.add(bcFile);
107+
} else {
108+
droppedArgs.add(bcFile + "(dropped as library input)");
109+
}
110+
} catch (IOException e) {
111+
throw new RuntimeException(e);
112+
}
113+
}
87114
} else {
88115
droppedArgs.add(arg);
89116
}
@@ -95,6 +122,66 @@ private void parseOptions(String[] args) {
95122
logV("Dropped args: ", droppedArgs);
96123
}
97124

125+
private List<String> searchLib(List<String> libraryDirs, String lib) {
126+
List<String> bcFiles = new ArrayList<>();
127+
String[] suffixes = new String[]{".bc", ".o", ".a", ".so"};
128+
String[] prefixes = new String[]{"lib", ""};
129+
for (String libdir : libraryDirs) {
130+
for (String prefix : prefixes) {
131+
for (String suffix : suffixes) {
132+
Path path = Paths.get(libdir, prefix + lib + suffix);
133+
String pathString = path.toAbsolutePath().toString();
134+
logV("Checking for library:", pathString);
135+
if (Files.exists(path)) {
136+
if (suffix.equals(".a")) {
137+
// extract members
138+
try {
139+
bcFiles.addAll(arMembers(pathString));
140+
} catch (IOException | InterruptedException e) {
141+
throw new RuntimeException(e);
142+
}
143+
} else {
144+
bcFiles.add(pathString);
145+
break;
146+
}
147+
}
148+
}
149+
}
150+
}
151+
logV("Found bitcode files for library:", bcFiles);
152+
return bcFiles;
153+
}
154+
155+
private Collection<? extends String> arMembers(String path) throws IOException, InterruptedException {
156+
List<String> members = new ArrayList<>();
157+
File temp = File.createTempFile(path, Long.toString(System.nanoTime()));
158+
temp.delete();
159+
temp.mkdir();
160+
temp.deleteOnExit();
161+
162+
ProcessBuilder extractAr = new ProcessBuilder();
163+
extractAr.redirectInput(Redirect.INHERIT);
164+
extractAr.redirectError(Redirect.INHERIT);
165+
extractAr.redirectOutput(Redirect.PIPE);
166+
extractAr.directory(temp);
167+
logV("ar", path);
168+
// "ar t" lists the members one per line
169+
extractAr.command("ar", "t", path);
170+
Process start = extractAr.start();
171+
try (BufferedReader buffer = new BufferedReader(new InputStreamReader(start.getInputStream()))) {
172+
String line = null;
173+
while ((line = buffer.readLine()) != null) {
174+
members.add(temp.getAbsolutePath() + File.separator + line);
175+
}
176+
}
177+
start.waitFor();
178+
// actually extract them now
179+
extractAr.redirectOutput(Redirect.INHERIT);
180+
extractAr.command("ar", "xv", path);
181+
extractAr.start().waitFor();
182+
return members;
183+
}
184+
98185
private void launchLD() {
99186
ldArgs.add(outputFilename);
100187
ldArgs.addAll(fileInputs);

0 commit comments

Comments
 (0)