35 import java.util.jar.JarOutputStream;
36 import java.util.logging.Logger;
37 import java.util.zip.ZipEntry;
38
39 import static java.nio.file.StandardOpenOption.*;
40
41 /**
42 * The setup for the tool execution
43 */
44 public class Context {
45 // The folder path mapping to package name
46 private final Map<Path, String> pkgMap;
47 // The header file parsed
48 private final Map<Path, HeaderFile> headerMap;
49 // The args for parsing C
50 final List<String> clangArgs;
51 // The set of source header files
52 final Set<Path> sources;
53 // The list of libraries
54 final List<String> libraries;
55
56 //
57 final static String defaultPkg = "jextract.dump";
58 private static Context instance = new Context();
59 public final Logger logger = Logger.getLogger(getClass().getPackage().getName());
60
61 private Context() {
62 pkgMap = new HashMap<>();
63 headerMap = new HashMap<>();
64 clangArgs = new ArrayList<>();
65 sources = new TreeSet<>();
66 libraries = new ArrayList<>();
67 }
68
69 // used only for jtreg testing
70 public static Context newInstance() {
71 return instance = new Context();
72 }
73
74 public static Context getInstance() {
75 return instance;
76 }
77
78 public void addSource(Path path) {
79 sources.add(path);
80 }
81
82 /**
83 * Setup a package name for a given folder.
84 *
85 * @param folder The path to the folder, use null to set catch-all.
86 * @param pkg The package name
223 (!loc.isInSystemHeader()) && (header.pkgName.equals(main.pkgName))) {
224 logger.config("Code gen for header " + p + " enabled in package " + header.pkgName);
225 header.useCodeFactory(fn.apply(header));
226 }
227 headerMap.put(p, header);
228 }
229 }
230 }
231
232 header.processCursor(c, main, isBuiltIn);
233 }
234
235 public void parse(Function<HeaderFile, CodeFactory> fn) {
236 sources.forEach(path -> {
237 if (headerMap.containsKey(path)) {
238 logger.info(() -> path.toString() + " seen earlier via #include");
239 return;
240 }
241
242 HeaderFile hf = headerMap.computeIfAbsent(path, p -> getHeaderFile(p, null));
243 hf.useLibraries(libraries);
244 hf.useCodeFactory(fn.apply(hf));
245 logger.info(() -> "Parsing header file " + path);
246
247 Index index = LibClang.createIndex();
248 Cursor tuCursor = index.parse(path.toString(),
249 d -> {
250 System.err.println(d);
251 if (d.severity() > Diagnostic.CXDiagnostic_Warning) {
252 throw new RuntimeException(d.toString());
253 }
254 },
255 Main.INCLUDE_MACROS,
256 clangArgs.toArray(new String[0]));
257
258 tuCursor.children()
259 .peek(c -> logger.finest(
260 () -> "Cursor: " + c.spelling() + "@" + c.USR() + "?" + c.isDeclaration()))
261 .filter(c -> c.isDeclaration() || c.isPreprocessing())
262 .forEach(c -> processCursor(c, hf, fn));
263 });
|
35 import java.util.jar.JarOutputStream;
36 import java.util.logging.Logger;
37 import java.util.zip.ZipEntry;
38
39 import static java.nio.file.StandardOpenOption.*;
40
41 /**
42 * The setup for the tool execution
43 */
44 public class Context {
45 // The folder path mapping to package name
46 private final Map<Path, String> pkgMap;
47 // The header file parsed
48 private final Map<Path, HeaderFile> headerMap;
49 // The args for parsing C
50 final List<String> clangArgs;
51 // The set of source header files
52 final Set<Path> sources;
53 // The list of libraries
54 final List<String> libraries;
55 // The list of library paths
56 final List<String> libraryPaths;
57
58 //
59 final static String defaultPkg = "jextract.dump";
60 private static Context instance = new Context();
61 public final Logger logger = Logger.getLogger(getClass().getPackage().getName());
62
63 private Context() {
64 pkgMap = new HashMap<>();
65 headerMap = new HashMap<>();
66 clangArgs = new ArrayList<>();
67 sources = new TreeSet<>();
68 libraries = new ArrayList<>();
69 libraryPaths = new ArrayList<>();
70 }
71
72 // used only for jtreg testing
73 public static Context newInstance() {
74 return instance = new Context();
75 }
76
77 public static Context getInstance() {
78 return instance;
79 }
80
81 public void addSource(Path path) {
82 sources.add(path);
83 }
84
85 /**
86 * Setup a package name for a given folder.
87 *
88 * @param folder The path to the folder, use null to set catch-all.
89 * @param pkg The package name
226 (!loc.isInSystemHeader()) && (header.pkgName.equals(main.pkgName))) {
227 logger.config("Code gen for header " + p + " enabled in package " + header.pkgName);
228 header.useCodeFactory(fn.apply(header));
229 }
230 headerMap.put(p, header);
231 }
232 }
233 }
234
235 header.processCursor(c, main, isBuiltIn);
236 }
237
238 public void parse(Function<HeaderFile, CodeFactory> fn) {
239 sources.forEach(path -> {
240 if (headerMap.containsKey(path)) {
241 logger.info(() -> path.toString() + " seen earlier via #include");
242 return;
243 }
244
245 HeaderFile hf = headerMap.computeIfAbsent(path, p -> getHeaderFile(p, null));
246 hf.useLibraries(libraries, libraryPaths);
247 hf.useCodeFactory(fn.apply(hf));
248 logger.info(() -> "Parsing header file " + path);
249
250 Index index = LibClang.createIndex();
251 Cursor tuCursor = index.parse(path.toString(),
252 d -> {
253 System.err.println(d);
254 if (d.severity() > Diagnostic.CXDiagnostic_Warning) {
255 throw new RuntimeException(d.toString());
256 }
257 },
258 Main.INCLUDE_MACROS,
259 clangArgs.toArray(new String[0]));
260
261 tuCursor.children()
262 .peek(c -> logger.finest(
263 () -> "Cursor: " + c.spelling() + "@" + c.USR() + "?" + c.isDeclaration()))
264 .filter(c -> c.isDeclaration() || c.isPreprocessing())
265 .forEach(c -> processCursor(c, hf, fn));
266 });
|