< prev index next >
src/jdk.jextract/share/classes/com/sun/tools/jextract/Context.java
Print this page
*** 42,82 ****
import static java.nio.file.StandardOpenOption.*;
/**
* The setup for the tool execution
*/
! public class Context {
// The folder path mapping to package name
private final Map<Path, String> pkgMap;
// The header file parsed
private final Map<Path, HeaderFile> headerMap;
// The args for parsing C
! final List<String> clangArgs;
// The set of source header files
! final Set<Path> sources;
// The list of library names
! final List<String> libraryNames;
// The list of library paths
! final List<String> libraryPaths;
// The list of library paths for link checks
! final List<String> linkCheckPaths;
final PrintWriter out;
final PrintWriter err;
// check if a symbol is found in any of the libraries or not.
! public static interface SymbolChecker {
public boolean lookup(String name);
}
! SymbolChecker symChecker;
! final static String defaultPkg = "jextract.dump";
! private static Context instance;
! public final Logger logger = Logger.getLogger(getClass().getPackage().getName());
! private Context(PrintWriter out, PrintWriter err) {
this.pkgMap = new HashMap<>();
this.headerMap = new HashMap<>();
this.clangArgs = new ArrayList<>();
this.sources = new TreeSet<>();
this.libraryNames = new ArrayList<>();
--- 42,84 ----
import static java.nio.file.StandardOpenOption.*;
/**
* The setup for the tool execution
*/
! public final class Context {
! // package name to TypeDictionary
! private final Map<String, TypeDictionary> tdMap;
// The folder path mapping to package name
private final Map<Path, String> pkgMap;
// The header file parsed
private final Map<Path, HeaderFile> headerMap;
// The args for parsing C
! private final List<String> clangArgs;
// The set of source header files
! private final Set<Path> sources;
// The list of library names
! private final List<String> libraryNames;
// The list of library paths
! private final List<String> libraryPaths;
// The list of library paths for link checks
! private final List<String> linkCheckPaths;
final PrintWriter out;
final PrintWriter err;
// check if a symbol is found in any of the libraries or not.
! static interface SymbolChecker {
public boolean lookup(String name);
}
! private SymbolChecker symChecker;
! private final static String defaultPkg = "jextract.dump";
! final Logger logger = Logger.getLogger(getClass().getPackage().getName());
! public Context(PrintWriter out, PrintWriter err) {
! this.tdMap = new HashMap<>();
this.pkgMap = new HashMap<>();
this.headerMap = new HashMap<>();
this.clangArgs = new ArrayList<>();
this.sources = new TreeSet<>();
this.libraryNames = new ArrayList<>();
*** 84,110 ****
this.linkCheckPaths = new ArrayList<>();
this.out = out;
this.err = err;
}
! // used by jtreg tests
! public static Context newInstance() {
! return newInstance(new PrintWriter(System.out, true), new PrintWriter(System.err, true));
}
! static Context newInstance(PrintWriter out, PrintWriter err) {
! return instance = new Context(out, err);
}
! static Context getInstance() {
! return instance;
}
public void addSource(Path path) {
sources.add(path);
}
/**
* Setup a package name for a given folder.
*
* @param folder The path to the folder, use null to set catch-all.
* @param pkg The package name
--- 86,123 ----
this.linkCheckPaths = new ArrayList<>();
this.out = out;
this.err = err;
}
! public Context() {
! this(new PrintWriter(System.out, true), new PrintWriter(System.err, true));
}
! TypeDictionary typeDictionaryFor(String pkg) {
! return tdMap.computeIfAbsent(pkg, p->new TypeDictionary(this, p));
}
! void addClangArg(String arg) {
! clangArgs.add(arg);
}
public void addSource(Path path) {
sources.add(path);
}
+ void addLibraryName(String name) {
+ libraryNames.add(name);
+ }
+
+ void addLibraryPath(String path) {
+ libraryPaths.add(path);
+ }
+
+ void addLinkCheckPath(String path) {
+ linkCheckPaths.add(path);
+ }
+
/**
* Setup a package name for a given folder.
*
* @param folder The path to the folder, use null to set catch-all.
* @param pkg The package name
*** 127,139 ****
logger.warning(() -> "Package " + existing + " had been selected for " + finalFolder + ", request to use " + pkg + " is ignored.");
return false;
}
}
! public static class Entity {
! public final String pkg;
! public final String entity;
Entity(String pkg, String entity) {
this.pkg = pkg;
this.entity = entity;
}
--- 140,152 ----
logger.warning(() -> "Package " + existing + " had been selected for " + finalFolder + ", request to use " + pkg + " is ignored.");
return false;
}
}
! static class Entity {
! final String pkg;
! final String entity;
Entity(String pkg, String entity) {
this.pkg = pkg;
this.entity = entity;
}
*** 147,157 ****
*
* @param origin The source path
* @return The Entity
* @see Context::usePackageForFolder(Path, String)
*/
! public Entity whatis(Path origin) {
// normalize to absolute path
origin = origin.toAbsolutePath();
String filename = null;
if (!Files.isDirectory(origin)) {
// ensure it's a folder name
--- 160,170 ----
*
* @param origin The source path
* @return The Entity
* @see Context::usePackageForFolder(Path, String)
*/
! Entity whatis(Path origin) {
// normalize to absolute path
origin = origin.toAbsolutePath();
String filename = null;
if (!Files.isDirectory(origin)) {
// ensure it's a folder name
*** 205,215 ****
logger.warning(() -> "Not a regular file: " + header.toString());
throw new IllegalArgumentException(header.toString());
}
final Context.Entity e = whatis(header);
! return new HeaderFile(header, e.pkg, e.entity, main, symChecker);
}
void processCursor(Cursor c, HeaderFile main, Function<HeaderFile, CodeFactory> fn) {
SourceLocation loc = c.getSourceLocation();
if (loc == null) {
--- 218,228 ----
logger.warning(() -> "Not a regular file: " + header.toString());
throw new IllegalArgumentException(header.toString());
}
final Context.Entity e = whatis(header);
! return new HeaderFile(this, header, e.pkg, e.entity, main, symChecker);
}
void processCursor(Cursor c, HeaderFile main, Function<HeaderFile, CodeFactory> fn) {
SourceLocation loc = c.getSourceLocation();
if (loc == null) {
*** 254,263 ****
--- 267,280 ----
}
header.processCursor(c, main, isBuiltIn);
}
+ public void parse() {
+ parse(header -> new AsmCodeFactory(this, header));
+ }
+
public void parse(Function<HeaderFile, CodeFactory> fn) {
if (!libraryNames.isEmpty() && !linkCheckPaths.isEmpty()) {
Library[] libs = NativeLibraryImpl.loadLibraries(
linkCheckPaths.toArray(new String[0]),
libraryNames.toArray(new String[0]));
*** 316,326 ****
private Map<String, List<CodeFactory>> getPkgCfMap() {
final Map<String, List<CodeFactory>> mapPkgCf = new HashMap<>();
// Build the pkg to CodeFactory map
headerMap.values().forEach(header -> {
! CodeFactory cf = header.cf;
String pkg = header.pkgName;
logger.config(() -> "File " + header + " is in package: " + pkg);
if (cf == null) {
logger.config(() -> "File " + header + " code generation is not activated!");
return;
--- 333,343 ----
private Map<String, List<CodeFactory>> getPkgCfMap() {
final Map<String, List<CodeFactory>> mapPkgCf = new HashMap<>();
// Build the pkg to CodeFactory map
headerMap.values().forEach(header -> {
! CodeFactory cf = header.getCodeFactory();
String pkg = header.pkgName;
logger.config(() -> "File " + header + " is in package: " + pkg);
if (cf == null) {
logger.config(() -> "File " + header + " code generation is not activated!");
return;
*** 374,384 ****
mapPkgCf.getOrDefault(pkg_name, Collections.emptyList())
.forEach(cf -> writeJar(cf, jos));
}
}
! public void collectJarFile(final Path jar, String... pkgs) throws IOException {
logger.info(() -> "Collecting jar file " + jar);
try (OutputStream os = Files.newOutputStream(jar, CREATE, TRUNCATE_EXISTING, WRITE);
JarOutputStream jo = new JarOutputStream(os)) {
collectJarFile(jo, pkgs);
} catch (UncheckedIOException uioe) {
--- 391,401 ----
mapPkgCf.getOrDefault(pkg_name, Collections.emptyList())
.forEach(cf -> writeJar(cf, jos));
}
}
! void collectJarFile(final Path jar, String... pkgs) throws IOException {
logger.info(() -> "Collecting jar file " + jar);
try (OutputStream os = Files.newOutputStream(jar, CREATE, TRUNCATE_EXISTING, WRITE);
JarOutputStream jo = new JarOutputStream(os)) {
collectJarFile(jo, pkgs);
} catch (UncheckedIOException uioe) {
*** 411,421 ****
* Perform a global lookup
*
* @param c The cursor define or declare the type.
* @return
*/
! public JType getJType(final Cursor c) {
if (c.isInvalid()) {
throw new IllegalArgumentException();
}
SourceLocation loc = c.getSourceLocation();
if (null == loc) {
--- 428,438 ----
* Perform a global lookup
*
* @param c The cursor define or declare the type.
* @return
*/
! JType getJType(final Cursor c) {
if (c.isInvalid()) {
throw new IllegalArgumentException();
}
SourceLocation loc = c.getSourceLocation();
if (null == loc) {
< prev index next >