aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMathieu Féry <[email protected]>2023-06-15 19:18:34 +0200
committerMathieu Féry <[email protected]>2023-06-16 10:51:24 +0200
commit738c15d654f67ec8aeccec2b46f5a0804c793491 (patch)
tree259b5149ce3975acdd1e514d0afa291ec4362c69
parent88309192b450e09ba16e9d72adac178c4ce47dc3 (diff)
feat(feature): Enable pragma once management
-rw-r--r--.gitmodules1
m---------jcpp0
-rw-r--r--src/java/com/jogamp/gluegen/GlueGen.java12
-rw-r--r--src/java/com/jogamp/gluegen/pcpp/PCPP.java35
-rw-r--r--src/java/com/jogamp/gluegen/structgen/CStructAnnotationProcessor.java11
-rw-r--r--src/junit/com/jogamp/gluegen/test/junit/generation/PCPPTest.java31
-rw-r--r--src/junit/com/jogamp/gluegen/test/junit/generation/pcpptest-included.h2
-rw-r--r--src/junit/com/jogamp/gluegen/test/junit/generation/pcpptest.h3
8 files changed, 80 insertions, 15 deletions
diff --git a/.gitmodules b/.gitmodules
index c3f990a..3422d58 100644
--- a/.gitmodules
+++ b/.gitmodules
@@ -1,3 +1,4 @@
[submodule "jcpp"]
path = jcpp
url = ../jcpp
+ branch = feature/enable_pragma_once
diff --git a/jcpp b/jcpp
-Subproject eddcad41a1dc2658747235b307bfd4ffd2c27bd
+Subproject 00f97cc623469377c59985898b9b765ae66c0ae
diff --git a/src/java/com/jogamp/gluegen/GlueGen.java b/src/java/com/jogamp/gluegen/GlueGen.java
index 6dee6f0..50711e9 100644
--- a/src/java/com/jogamp/gluegen/GlueGen.java
+++ b/src/java/com/jogamp/gluegen/GlueGen.java
@@ -104,7 +104,7 @@ public class GlueGen implements GlueEmitterControls {
public static final String __GLUEGEN__ = "__GLUEGEN__";
@SuppressWarnings("unchecked")
- public void run(final Reader reader, final String filename, final Class<?> emitterClass, final List<String> includePaths, final List<String> cfgFiles, final String outputRootDir, final boolean copyCPPOutput2Stderr) {
+ public void run(final Reader reader, final String filename, final Class<?> emitterClass, final List<String> includePaths, final List<String> cfgFiles, final String outputRootDir, final boolean copyCPPOutput2Stderr, final boolean enablePragmaOnce) {
try {
if(debug) {
@@ -131,8 +131,8 @@ public class GlueGen implements GlueEmitterControls {
final File out = File.createTempFile("CPPTemp", ".cpp");
final FileOutputStream outStream = new FileOutputStream(out);
- // preprocessor = new PCPP(includePaths, debug, copyCPPOutput2Stderr);
- preprocessor = new JCPP(includePaths, debug, copyCPPOutput2Stderr);
+ // preprocessor = new PCPP(includePaths, debug, copyCPPOutput2Stderr, enablePragmaOnce);
+ preprocessor = new JCPP(includePaths, debug, copyCPPOutput2Stderr, enablePragmaOnce);
final String cppName = preprocessor.getClass().getSimpleName();
if(debug) {
System.err.println("CPP <"+cppName+"> output at (persistent): " + out.getAbsolutePath());
@@ -375,6 +375,7 @@ public class GlueGen implements GlueEmitterControls {
String outputRootDir = null;
final List<String> cfgFiles = new ArrayList<String>();
boolean copyCPPOutput2Stderr = false;
+ boolean enablePragmaOnce = false;
final List<String> includePaths = new ArrayList<String>();
for (int i = 0; i < args.length; i++) {
@@ -396,6 +397,8 @@ public class GlueGen implements GlueEmitterControls {
debug=true;
} else if (arg.equals("--dumpCPP")) {
copyCPPOutput2Stderr=true;
+ } else if (arg.equals("--enablePragmaOnce")) {
+ enablePragmaOnce=true;
} else {
usage();
}
@@ -420,7 +423,7 @@ public class GlueGen implements GlueEmitterControls {
try {
final Class<?> emitterClass = emitterFQN == null ? null : Class.forName(emitterFQN);
- new GlueGen().run(reader, filename, emitterClass, includePaths, cfgFiles, outputRootDir, copyCPPOutput2Stderr);
+ new GlueGen().run(reader, filename, emitterClass, includePaths, cfgFiles, outputRootDir, copyCPPOutput2Stderr, enablePragmaOnce);
} catch (final ClassNotFoundException ex) {
throw new RuntimeException("specified emitter class was not in the classpath", ex);
}
@@ -445,6 +448,7 @@ public class GlueGen implements GlueEmitterControls {
out.println("-Cjava-emitter.cfg.");
out.println(" --debug enables debug mode");
out.println(" --dumpCPP directs CPP to dump all output to stderr as well");
+ out.println(" --enablePragmaOnce allow handle of #pragma once directive during parsing");
exit(1);
}
}
diff --git a/src/java/com/jogamp/gluegen/pcpp/PCPP.java b/src/java/com/jogamp/gluegen/pcpp/PCPP.java
index c766634..c4af374 100644
--- a/src/java/com/jogamp/gluegen/pcpp/PCPP.java
+++ b/src/java/com/jogamp/gluegen/pcpp/PCPP.java
@@ -87,18 +87,21 @@ public class PCPP implements GenericCPP {
/** List containing the #include paths as Strings */
private final List<String> includePaths;
+ private final List<String> alreadyIncludedFiles = new ArrayList<String>();
private ParseState state;
private final boolean enableDebugPrint;
private final boolean enableCopyOutput2Stderr;
+ private final boolean enablePragmaOnce;
- public PCPP(final List<String> includePaths, final boolean debug, final boolean copyOutput2Stderr) {
+ public PCPP(final List<String> includePaths, final boolean debug, final boolean copyOutput2Stderr, final boolean pragmaOnce) {
LOG = Logging.getLogger(PCPP.class.getPackage().getName(), PCPP.class.getSimpleName());
this.includePaths = includePaths;
setOut(System.out);
enableDebugPrint = debug;
enableCopyOutput2Stderr = copyOutput2Stderr;
+ enablePragmaOnce = pragmaOnce;
}
@Override
@@ -450,6 +453,9 @@ public class PCPP implements GenericCPP {
} else if (w.equals("include")) {
handleInclude();
shouldPrint = false;
+ } else if (w.equals("pragma")){
+ handlePragma();
+ shouldPrint = false;
} else {
int line = -1;
try {
@@ -1019,14 +1025,29 @@ public class PCPP implements GenericCPP {
if (fullname == null) {
throw new RuntimeException("Can't find #include file \"" + filename + "\" at file " + filename() + ", line " + lineNumber());
}
- // Process this file in-line
- final Reader reader = new BufferedReader(new FileReader(fullname));
- run(reader, fullname);
+ if ((!enablePragmaOnce || !alreadyIncludedFiles.contains(fullname))) {
+ // Process this file in-line
+ final Reader reader = new BufferedReader(new FileReader(fullname));
+ run(reader, fullname);
+ } else {
+ //System.err.println("INACTIVE BLOCK, SKIPPING " + filename);
+ }
} else {
//System.err.println("INACTIVE BLOCK, SKIPPING " + filename);
}
}
+ /////////////////////////////////////
+ // Handling of #pragma directives //
+ /////////////////////////////////////
+
+ private void handlePragma() throws IOException {
+ final String msg = nextWordOrString();
+ if (enablePragmaOnce && msg.equals("once")) {
+ alreadyIncludedFiles.add(filename());
+ }
+ }
+
////////////
// Output //
////////////
@@ -1128,6 +1149,7 @@ public class PCPP implements GenericCPP {
System.err.println("Output goes to standard output. Standard input can be used as input");
System.err.println("by passing '-' as the argument.");
System.err.println(" --debug enables debug mode");
+ System.err.println(" --enablePragmaOnce enables pragma once management");
System.exit(1);
}
@@ -1135,6 +1157,7 @@ public class PCPP implements GenericCPP {
Reader reader = null;
String filename = null;
boolean debug = false;
+ boolean enablePragmaOnce = false;
if (args.length == 0) {
usage();
@@ -1151,6 +1174,8 @@ public class PCPP implements GenericCPP {
}
} else if (arg.equals("--debug")) {
debug = true;
+ } else if (arg.equals("--enablePragmaOnce")) {
+ enablePragmaOnce = true;
} else {
usage();
}
@@ -1169,7 +1194,7 @@ public class PCPP implements GenericCPP {
}
}
- new PCPP(includePaths, debug, debug).run(reader, filename);
+ new PCPP(includePaths, debug, debug, enablePragmaOnce).run(reader, filename);
}
}
diff --git a/src/java/com/jogamp/gluegen/structgen/CStructAnnotationProcessor.java b/src/java/com/jogamp/gluegen/structgen/CStructAnnotationProcessor.java
index 13bf418..52c20e0 100644
--- a/src/java/com/jogamp/gluegen/structgen/CStructAnnotationProcessor.java
+++ b/src/java/com/jogamp/gluegen/structgen/CStructAnnotationProcessor.java
@@ -98,12 +98,15 @@ public class CStructAnnotationProcessor extends AbstractProcessor {
}
private static final String STRUCTGENOUTPUT_OPTION = "structgen.output";
+ private static final String STRUCTGENPRAGMA_ONCE = "structgen.enable.pragma.once";
private static final String STRUCTGENOUTPUT = PropertyAccess.getProperty("jogamp.gluegen."+STRUCTGENOUTPUT_OPTION, true, "gensrc");
+ private static final String STRUCTGENPRAGMAONCE = PropertyAccess.getProperty("jogamp.gluegen."+STRUCTGENPRAGMA_ONCE, true, "false");
private Filer filer;
private Messager messager;
private Elements eltUtils;
private String outputPath;
+ private boolean enablePragmaOnce;
private final static Set<String> generatedStructs = new HashSet<String>();
@@ -118,6 +121,9 @@ public class CStructAnnotationProcessor extends AbstractProcessor {
outputPath = processingEnv.getOptions().get(STRUCTGENOUTPUT_OPTION);
outputPath = outputPath == null ? STRUCTGENOUTPUT : outputPath;
+
+ final String enablePragmaOnceOpt = processingEnv.getOptions().get(STRUCTGENPRAGMAONCE);
+ enablePragmaOnce = Boolean.parseBoolean(enablePragmaOnceOpt == null ? STRUCTGENPRAGMAONCE : enablePragmaOnceOpt);
}
private File locateSource(final String packageName, final String relativeName) {
@@ -211,7 +217,7 @@ public class CStructAnnotationProcessor extends AbstractProcessor {
headerParent = root0.substring(0, root0.length()-headerFile.getName().length()-1);
rootOut = headerParent.substring(0, headerParent.length()-packageName.length()) + "..";
}
- System.err.println("CStruct: "+headerFile+", abs: "+headerFile.isAbsolute()+", headerParent "+headerParent+", rootOut "+rootOut);
+ System.err.println("CStruct: "+headerFile+", abs: "+headerFile.isAbsolute()+", headerParent "+headerParent+", rootOut "+rootOut+", enablePragmaOnce"+enablePragmaOnce);
generateStructBinding(element, struct, isPackageOrType, rootOut, packageName, headerFile, headerParent);
} catch (final IOException ex) {
@@ -276,7 +282,8 @@ public class CStructAnnotationProcessor extends AbstractProcessor {
GlueGen.setDebug(true);
}
new GlueGen().run(reader, filename, AnnotationProcessorJavaStructEmitter.class,
- includePaths, cfgFiles, outputPath1, false /* copyCPPOutput2Stderr */);
+ includePaths, cfgFiles, outputPath1, false /* copyCPPOutput2Stderr */,
+ enablePragmaOnce /* enablePragmaOnce */);
configFile.delete();
generatedStructs.add(finalType);
diff --git a/src/junit/com/jogamp/gluegen/test/junit/generation/PCPPTest.java b/src/junit/com/jogamp/gluegen/test/junit/generation/PCPPTest.java
index 608a17f..5e06a37 100644
--- a/src/junit/com/jogamp/gluegen/test/junit/generation/PCPPTest.java
+++ b/src/junit/com/jogamp/gluegen/test/junit/generation/PCPPTest.java
@@ -62,13 +62,23 @@ public class PCPPTest extends SingletonJunitCase {
}
@Test
- public void pcppMacroDefinitionTest() throws FileNotFoundException, IOException {
- final PCPP pp = new PCPP(Collections.<String>emptyList(), false, false);
+ public void pcppMacroDefinitionTestWithoutPragmaOnce() throws FileNotFoundException, IOException {
+ pcppMacroDefinitionTest(false);
+ }
+
+ @Test
+ public void pcppMacroDefinitionTestWithPragmaOnce() throws FileNotFoundException, IOException {
+ pcppMacroDefinitionTest(true);
+ }
+
+ public void pcppMacroDefinitionTest(final boolean pragmaOnce) throws FileNotFoundException, IOException {
+ final String folderpath = BuildEnvironment.gluegenRoot + "/src/junit/com/jogamp/gluegen/test/junit/generation";
+ final PCPP pp = new PCPP(Collections.<String>singletonList(folderpath), false, false, pragmaOnce);
final ByteArrayOutputStream output = new ByteArrayOutputStream();
pp.setOut(output);
final String filename = "pcpptest.h";
- final String filepath = BuildEnvironment.gluegenRoot + "/src/junit/com/jogamp/gluegen/test/junit/generation/" + filename ;
+ final String filepath = folderpath + "/" + filename ;
pp.run(new BufferedReader(new FileReader(filepath)), filename);
final String expected = "# 1 \"pcpptest.h\""+
@@ -95,7 +105,20 @@ public class PCPPTest extends SingletonJunitCase {
"#128\"pcpptest.h\""+
"#130\"pcpptest.h\""+
"#134\"pcpptest.h\""+
- "#136\"pcpptest.h\"";
+ "#1\""+folderpath+"/pcpptest-included.h\""+
+ "# define EXAMPLE 42"+
+ "#134\"pcpptest.h\""+
+ (!pragmaOnce ?
+ (
+ "#1\""+folderpath+"/pcpptest-included.h\""+
+ "# define EXAMPLE 42"+
+ "#135\"pcpptest.h\""
+ ):
+ ""
+ )+
+ "#137\"pcpptest.h\""+
+ "#139\"pcpptest.h\""
+ ;
output.flush();
diff --git a/src/junit/com/jogamp/gluegen/test/junit/generation/pcpptest-included.h b/src/junit/com/jogamp/gluegen/test/junit/generation/pcpptest-included.h
new file mode 100644
index 0000000..8dbe022
--- /dev/null
+++ b/src/junit/com/jogamp/gluegen/test/junit/generation/pcpptest-included.h
@@ -0,0 +1,2 @@
+#pragma once
+#define EXAMPLE 42 \ No newline at end of file
diff --git a/src/junit/com/jogamp/gluegen/test/junit/generation/pcpptest.h b/src/junit/com/jogamp/gluegen/test/junit/generation/pcpptest.h
index e9ba181..31b80c3 100644
--- a/src/junit/com/jogamp/gluegen/test/junit/generation/pcpptest.h
+++ b/src/junit/com/jogamp/gluegen/test/junit/generation/pcpptest.h
@@ -131,5 +131,8 @@ int TEST_G_VAL;
}
#endif
+#include <pcpptest-included.h>
+#include <pcpptest-included.h>
+
#endif /* __test_h_ */