summaryrefslogtreecommitdiffstats
path: root/src/java/com/jogamp/gluegen/GlueGen.java
blob: 872214d98e2ec5216d19afc1413a35b8a0017475 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
/*
 * Copyright (c) 2003 Sun Microsystems, Inc. All Rights Reserved.
 * Copyright (c) 2010 JogAmp Community. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 * - Redistribution of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 *
 * - Redistribution in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the distribution.
 *
 * Neither the name of Sun Microsystems, Inc. or the names of
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * This software is provided "AS IS," without a warranty of any kind. ALL
 * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
 * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
 * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN
 * MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE FOR
 * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
 * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR
 * ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR
 * DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE
 * DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY,
 * ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF
 * SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
 *
 * You acknowledge that this software is not designed or intended for use
 * in the design, construction, operation or maintenance of any nuclear
 * facility.
 *
 * Sun gratefully acknowledges that this software was originally authored
 * and developed by Kenneth Bradley Russell and Christopher John Kline.
 */
package com.jogamp.gluegen;

import com.jogamp.common.GlueGenVersion;

import java.io.*;
import java.util.*;
import java.util.logging.Level;

import antlr.*;

import com.jogamp.gluegen.cgram.*;
import com.jogamp.gluegen.cgram.types.*;
import com.jogamp.gluegen.jcpp.JCPP;

import static java.lang.System.*;

/**
 * Glue code generator for C functions and data structures.<br>
 */
public class GlueGen implements GlueEmitterControls {

    static{
        Logging.init();
    }

    private final List<String> forcedStructNames = new ArrayList<String>();
    private GenericCPP preprocessor;

    // State for SymbolFilters
    private List<ConstantDefinition> allConstants;
    private List<FunctionSymbol> allFunctions;

    private static boolean debug = false;

    private static Level logLevel = null;

    public static void setDebug(final boolean v) { debug=v; }
    public static void setLogLevel(final Level l) { logLevel=l; }
    public static boolean debug() { return debug; }

    @Override
    public void forceStructEmission(final String typedefName) {
        forcedStructNames.add(typedefName);
    }

    @Override
    public String findHeaderFile(final String headerFileName) {
        return preprocessor.findFile(headerFileName);
    }

    @Override
    public void runSymbolFilter(final SymbolFilter filter) {
        filter.filterSymbols(allConstants, allFunctions);
        final List<ConstantDefinition> newConstants = filter.getConstants();
        final List<FunctionSymbol> newFunctions = filter.getFunctions();
        if (newConstants != null) {
            allConstants = newConstants;
        }
        if (newFunctions != null) {
            allFunctions = newFunctions;
        }
    }

    /** GlueGen's build in macro name {@value}, when compiling w/ GlueGen. */
    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, final boolean enablePragmaOnce, final boolean preserveGeneratedCPP)
    {
        try {
            if(debug) {
                Logging.getLogger().setLevel(Level.ALL);
                System.err.println("GlueGen.run: filename: "+filename+", emitter: "+emitterClass.getName()+", outputRootDir "+outputRootDir+
                                   ", copyCPPOutput2Stderr "+copyCPPOutput2Stderr+", enablePragmaOnce "+enablePragmaOnce+", preserveGeneratedCPP "+preserveGeneratedCPP);
                System.err.println("GlueGen.run: includePaths "+includePaths);
                System.err.println("GlueGen.run: cfgFiles "+cfgFiles);
            } else if( null != logLevel ) {
                Logging.getLogger().setLevel(logLevel);
            }
            final GlueEmitter emit;
            if (emitterClass == null) {
                emit = new JavaEmitter();
            } else {
                try {
                    emit = (GlueEmitter) emitterClass.newInstance();
                } catch (final Exception e) {
                    throw new RuntimeException("Exception occurred while instantiating emitter class.", e);
                }
            }

            for (final String config : cfgFiles) {
                emit.readConfigurationFile(config);
            }
            final JavaConfiguration cfg = emit.getConfig();

            final File out = File.createTempFile("CPPTemp", ".cpp");
            final FileOutputStream outStream = new FileOutputStream(out);

            // preprocessor = new PCPP(includePaths, debug, copyCPPOutput2Stderr, enablePragmaOnce);
            preprocessor = new JCPP(includePaths, debug, copyCPPOutput2Stderr, enablePragmaOnce);
            final String cppName = preprocessor.getClass().getSimpleName();
            if(debug || preserveGeneratedCPP) {
                System.err.println("CPP <"+cppName+"> output at (persistent): " + out.getAbsolutePath());
            } else {
                out.deleteOnExit();
            }

            preprocessor.addDefine(__GLUEGEN__, "2");
            preprocessor.setOut(outStream);

            preprocessor.run(reader, filename);
            outStream.flush();
            outStream.close();
            if(debug) {
                System.err.println("CPP <"+cppName+"> done");
            }

            final FileInputStream inStream = new FileInputStream(out);
            final DataInputStream dis = new DataInputStream(inStream);

            final GnuCLexer lexer = new GnuCLexer(dis);
            lexer.setTokenObjectClass(CToken.class.getName());
            lexer.initialize();
            // Parse the input expression.
            final GnuCParser parser = new GnuCParser(lexer);

            // set AST node type to TNode or get nasty cast class errors
            parser.setASTNodeClass(TNode.class.getName());
            TNode.setTokenVocabulary(GNUCTokenTypes.class.getName());

            // invoke parser
            try {
                parser.translationUnit();
            } catch (final RecognitionException e) {
                throw new RuntimeException(String.format(
                        "Fatal error during translation (Localisation : %s:%s:%s)",
                        e.getFilename(), e.getLine(), e.getColumn()
                ), e);
            } catch (final TokenStreamRecognitionException e) {
                throw new RuntimeException(String.format(
                        "Fatal error during translation (Localisation : %s:%s:%s)",
                        e.recog.getFilename(), e.recog.getLine(), e.recog.getColumn()
                ), e);
            } catch (final TokenStreamException e) {
                throw new RuntimeException("Fatal IO error", e);
            }

            final HeaderParser headerParser = new HeaderParser();
            headerParser.setDebug(debug);
            headerParser.setJavaConfiguration(cfg);
            final TypeDictionary td = new TypeDictionary();
            headerParser.setTypedefDictionary(td);
            final TypeDictionary sd = new TypeDictionary();
            headerParser.setStructDictionary(sd);
            // set AST node type to TNode or get nasty cast class errors
            headerParser.setASTNodeClass(TNode.class.getName());
            // walk that tree
            headerParser.translationUnit(parser.getAST());
            dis.close();
            inStream.close();

            /**
            // For debugging: Dump type dictionary and struct dictionary to System.err
            if(debug) {
                td.dumpDictionary(err, "All Types");
                sd.dumpDictionary(err, "All Structs");
            } */

            // At this point we have all of the pieces we need in order to
            // generate glue code: the #defines to constants, the set of
            // typedefs, and the set of functions.

            if (null != outputRootDir && outputRootDir.trim().length() > 0) {
                if (emit instanceof JavaEmitter) {
                    // FIXME: hack to interfere with the *Configuration setting via commandlines
                    final JavaEmitter jemit = (JavaEmitter) emit;
                    if (null != jemit.getConfig()) {
                        jemit.getConfig().setOutputRootDir(outputRootDir);
                    }
                }
            }

            // Repackage the enum and #define statements from the parser into a common format
            // so that SymbolFilters can operate upon both identically
            allConstants = new ArrayList<ConstantDefinition>();
            for (final EnumType enumeration : headerParser.getEnums()) {
                String enumName = enumeration.getName();
                if (enumName.equals("<anonymous>")) {
                    enumName = null;
                }
                // iterate over all values in the enumeration
                for (int i = 0; i < enumeration.getNumEnumerates(); ++i) {
                    final EnumType.Enumerator enumerate = enumeration.getEnum(i);
                    final ConstantDefinition def =
                            new ConstantDefinition(enumerate.getName(), enumerate.getExpr(),
                                                   enumerate.getNumber(),
                                                   enumName, enumeration.getASTLocusTag());
                    allConstants.add(def);
                }
            }
            for (final Object elem : lexer.getDefines()) {
                final Define def = (Define) elem;
                allConstants.add(new ConstantDefinition(def.getName(), def.getValue(), null, def.getASTLocusTag()));
            }
            allConstants.addAll(preprocessor.getConstantDefinitions());

            allFunctions = headerParser.getParsedFunctions();

            // begin emission of glue code,
            // incl. firing up 'runSymbolFilter(SymbolFilter)' calls, which:
            //    - filters all ConstantDefinition
            //    - filters all FunctionSymbol
            emit.beginEmission(this);

            if( debug() ) {
                int i=0;
                System.err.println("Filtered Constants: "+allConstants.size());
                for (final ConstantDefinition def : allConstants) {
                    if( debug() ) {
                        System.err.println("Filtered ["+i+"]: "+def.getAliasedString());
                        i++;
                    }
                }
                i=0;
                System.err.println("Filtered Functions: "+allFunctions.size());
                for (final FunctionSymbol cFunc : allFunctions) {
                    System.err.println("Filtered ["+i+"]: "+cFunc.getAliasedString());
                    i++;
                }
            }

            if ( !cfg.structsOnly() ) {
                emit.beginDefines();
                final Set<String> emittedDefines = new HashSet<String>(100);
                // emit java equivalent of enum { ... } statements
                final StringBuilder comment = new StringBuilder();
                for (final ConstantDefinition def : allConstants) {
                    if (!emittedDefines.contains(def.getName())) {
                        emittedDefines.add(def.getName());
                        final Set<String> aliases = cfg.getAliasedDocNames(def);
                        if (aliases != null && aliases.size() > 0 ) {
                            int i=0;
                            comment.append("Alias for: <code>");
                            for (final String alias : aliases) {
                                if(0 < i) {
                                    comment.append("</code>, <code>");
                                }
                                comment.append(alias);
                                i++;
                            }
                            comment.append("</code>");
                        }
                        if (def.getEnumName() != null) {
                            if (comment.length() > 0)
                                comment.append("<br>\n");

                            comment.append("Defined as part of enum type \"");
                            comment.append(def.getEnumName());
                            comment.append("\"");
                        }
                        if (comment.length() > 0) {
                            emit.emitDefine(def, comment.toString());
                            comment.setLength(0);
                        }
                        else {
                            emit.emitDefine(def, null);
                        }
                    }
                }
                emit.endDefines();
            }

            // Iterate through the functions finding structs that are referenced in
            // the function signatures; these will be remembered for later emission
            final ReferencedStructs referencedStructs = new ReferencedStructs();
            for (final FunctionSymbol sym : allFunctions) {
                // FIXME: this doesn't take into account the possibility that some of
                // the functions we send to emitMethodBindings() might not actually be
                // emitted (e.g., if an Ignore directive in the JavaEmitter causes it to be skipped).
                sym.getType().visit(referencedStructs);
            }

            // Normally only referenced types will be emitted. The user can force a
            // type to be emitted via a .cfg file directive. Those directives are
            // processed here.
            for (final String name : forcedStructNames) {
                final Type type = td.get(name);
                if (type == null) {
                    err.println("WARNING: during forced struct emission: struct \"" + name + "\" not found");
                } else if (!type.isCompound()) {
                    err.println("WARNING: during forced struct emission: type \"" + name + "\" was not a struct");
                } else {
                    type.visit(referencedStructs);
                }
            }

            // Lay out structs
            emit.beginStructLayout();
            for (final Iterator<CompoundType> iter = referencedStructs.layouts(); iter.hasNext();) {
                final CompoundType c = iter.next();
                if( !c.isLayouted() ) {
                    emit.layoutStruct(c);
                }
            }
            emit.endStructLayout();

            // Emit structs
            emit.beginStructs(td, sd, headerParser.getCanonMap());
            for (final Iterator<Type> iter = referencedStructs.results(); iter.hasNext();) {
                final Type t = iter.next();
                if (t.isCompound()) {
                    assert t.isTypedef() && t.getName() == null : "ReferencedStructs incorrectly recorded compound type " + t;
                    emit.emitStruct(t.asCompound(), null);
                } else if (t.isPointer()) {
                    final PointerType p = t.asPointer();
                    final CompoundType c = p.getTargetType().asCompound();
                    assert p.isTypedef() && c.getName() == null : "ReferencedStructs incorrectly recorded pointer type " + p;
                    emit.emitStruct(c, p);
                }
            }
            emit.endStructs();

            if ( !cfg.structsOnly() ) {
                // emit java and C code to interface with the native functions
                emit.beginFunctions(td, sd, headerParser.getCanonMap());
                emit.emitFunctions(allFunctions);
                emit.endFunctions();
            }

            // end emission of glue code
            emit.endEmission();

        } catch (final Exception e) {
            throw new RuntimeException("Exception occurred while generating glue code.", e);
        }
    }

    public static void main(final String... args) {

        if (args.length == 0) {
            System.err.println(GlueGenVersion.getInstance());
            usage();
        }

        Reader reader = null;
        String filename = null;
        String emitterFQN = null;
        String outputRootDir = null;
        final List<String> cfgFiles = new ArrayList<String>();
        boolean copyCPPOutput2Stderr = false;
        boolean enablePragmaOnce = true;
        boolean preserveGeneratedCPP = false;

        final List<String> includePaths = new ArrayList<String>();
        for (int i = 0; i < args.length; i++) {
            if (i < args.length - 1) {
                final String arg = args[i];
                if (arg.startsWith("-I")) {
                    final String[] paths = arg.substring(2).split(getProperty("path.separator"));
                    includePaths.addAll(Arrays.asList(paths));
                } else if (arg.startsWith("-O")) {
                    outputRootDir = arg.substring(2);
                } else if (arg.startsWith("-E")) {
                    emitterFQN = arg.substring(2);
                } else if (arg.startsWith("-C")) {
                    cfgFiles.add(arg.substring(2));
                } else if (arg.equals("--logLevel")) {
                    i++;
                    logLevel = Level.parse(args[i]);
                } else if (arg.equals("--debug")) {
                    debug=true;
                } else if (arg.equals("--dumpCPP")) {
                    copyCPPOutput2Stderr=true;
                } else if (arg.equals("--disablePragmaOnce")) {
                    enablePragmaOnce=false;
                } else if (arg.equals("--preserveGeneratedCPP")) {
                    preserveGeneratedCPP=true;
                } else {
                    usage();
                }
            } else {
                final String arg = args[i];
                if (arg.equals("-")) {
                    reader = new InputStreamReader(in);
                    filename = "standard input";
                } else {
                    if (arg.startsWith("-")) {
                        usage();
                    }
                    filename = arg;
                    try {
                        reader = new BufferedReader(new FileReader(filename));
                    } catch (final FileNotFoundException ex) {
                        throw new RuntimeException("input file not found", ex);
                    }
                }
            }
        }

        try {
            final Class<?> emitterClass = emitterFQN == null ? null : Class.forName(emitterFQN);
            new GlueGen().run(reader, filename, emitterClass, includePaths, cfgFiles, outputRootDir, copyCPPOutput2Stderr, enablePragmaOnce, preserveGeneratedCPP);
        } catch (final ClassNotFoundException ex) {
            throw new RuntimeException("specified emitter class was not in the classpath", ex);
        }

    }

    //----------------------------------------------------------------------
    // Internals only below this point
    //
    private static void usage() {
        out.println("Usage: java GlueGen [-I...] [-Eemitter_class_name] [-Ccfg_file_name...] <filename | ->");
        out.println();
        out.println("Runs C header parser on input file or standard input, first");
        out.println("passing input through minimal pseudo-C-preprocessor. Use -I");
        out.println("command-line arguments to specify the search path for #includes.");
        out.println("Emitter class name can be specified with -E option: i.e.,");
        out.println("-Ecom.jogamp.gluegen.JavaEmitter (the default). Use");
        out.println("-Ecom.jogamp.gluegen.DebugEmitter to print recognized entities");
        out.println("(#define directives to constant numbers, typedefs, and function");
        out.println("declarations) to standard output. Emitter-specific configuration");
        out.println("file or files can be specified with -C option; e.g,");
        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("  --disablePragmaOnce disable handling of #pragma once directive during parsing (enabled by default)");
        out.println("  --preserveGeneratedCPP preserve generated CPP file during generation (File it's already preserved by debug mode)");
        exit(1);
    }
}