aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/com/jogamp/gluegen/jcpp/JCPP.java
blob: 3f1e057105b4a621b6856d26b7fec05abb5e48b6 (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
/**
 * Copyright 2015 JogAmp Community. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied.  See the License for the specific language governing
 * permissions and limitations under the License.
 *
 * The views and conclusions contained in the software and documentation are those of the
 * authors and should not be interpreted as representing official policies, either expressed
 * or implied, of JogAmp Community.
 */
package com.jogamp.gluegen.jcpp;

import java.io.File;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;

import com.jogamp.gluegen.ASTLocusTag;
import com.jogamp.gluegen.ConstantDefinition;
import com.jogamp.gluegen.GenericCPP;
import com.jogamp.gluegen.GlueGen;
import com.jogamp.gluegen.GlueGenException;
import com.jogamp.gluegen.Logging;
import com.jogamp.gluegen.Logging.LoggerIf;

public class JCPP implements GenericCPP {
    private final LoggerIf LOG;

    public final Preprocessor cpp;
    private OutputStream out;
    private final List<String> includePaths;
    private final boolean enableCopyOutput2Stderr;

    public JCPP(final List<String> includePaths, final boolean debug, final boolean copyOutput2Stderr,
                final boolean enablePragmaOnce) {
        LOG = Logging.getLogger(JCPP.class);
        setOut(System.out);
        this.includePaths = includePaths;
        this.enableCopyOutput2Stderr = copyOutput2Stderr;

        cpp = new Preprocessor();
        cpp.addFeature(Feature.DIGRAPHS);
        cpp.addFeature(Feature.TRIGRAPHS);
        cpp.addFeature(Feature.LINEMARKERS);
        cpp.addFeature(Feature.CSYNTAX);
        cpp.addFeature(Feature.KEEPCOMMENTS);
        if (enablePragmaOnce) {
            cpp.addFeature(Feature.PRAGMA_ONCE);
        }
        cpp.addWarning(Warning.IMPORT);
        cpp.setListener(new DefaultPreprocessorListener() {
            @Override
            public void handleError(final Source source, final int line, final int column,
                                    final String msg) throws LexerException {
                super.handleError(source, line, column, msg);
                throw new GlueGenException(msg, new ASTLocusTag(source.getPath(), line, column, null));
            }
        });
        if (debug) {
            cpp.addFeature(Feature.DEBUG);
        }
        cpp.setSystemIncludePath(includePaths);
        cpp.setQuoteIncludePath(includePaths);

        if (cpp.getFeature(Feature.DEBUG)) {
            LOG.info("#" + "include \"...\" search starts here:");
            for (final String dir : cpp.getQuoteIncludePath())
                LOG.info("  " + dir);
            LOG.info("#" + "include <...> search starts here:");
            for (final String dir : cpp.getSystemIncludePath())
                LOG.info("  " + dir);
            LOG.info("End of search list.");
        }
    }

    @Override
    public void addDefine(final String name, final String value) throws LexerException {
        cpp.addMacro(name, value);
    }

    @Override
    public List<ConstantDefinition> getConstantDefinitions() throws GlueGenException {
        final List<ConstantDefinition> constants = new ArrayList<ConstantDefinition>();
        final List<Macro> macros;
        try {
            macros = cpp.getMacros(true);
        } catch (final Throwable t) {
            throw new GlueGenException(t);
        }
        final int count = macros.size();
        for(int i=0; i<count; i++) {
            final Macro macro = macros.get(i);
            final String name = macro.getName();
            if( !GlueGen.__GLUEGEN__.equals(name) ) {
                if( !macro.isFunctionLike() ) {
                    final String value = macro.getText();
                    if ( ConstantDefinition.isConstantExpression(value) ) {
                        final Source source = macro.getSource();
                        final ASTLocusTag locus = new ASTLocusTag(
                                                    null != source ? source.getPath() : "<programmatic>",
                                                    null != source ? source.getLine() : -1,
                                                    null != source ? source.getColumn() : -1,
                                                    macro.toString());
                        final ConstantDefinition c = new ConstantDefinition(macro.getName(), value, null, locus);
                        constants.add(c);
                    }
                }
            }
        }
        return constants;
    }

    @Override
    public String findFile(final String filename) {
        final String sep = File.separator;
        for (final String inclPath : includePaths) {
            final String fullPath = inclPath + sep + filename;
            final File file = new File(fullPath);
            if (file.exists()) {
                return fullPath;
            }
        }
        return null;
    }

    @Override
    public OutputStream out() {
        return out;
    }
    @Override
    public void setOut(final OutputStream out) {
        this.out = out;
    }

    @Override
    public void run(final Reader reader, final String filename) throws GlueGenException {
        final PrintWriter writer = new PrintWriter(out);
        cpp.addInput(new LexerSource(reader, true) {
                        @Override
                        public String getPath() { return filename; }
                        @Override
                        public String getName() { return filename; }
                        @Override
                        public String toString() { return "file " + filename; }
                   } );
        try {
            for (;;) {
                final Token tok = cpp.token();
                if (tok == null)
                    break;
                if (tok.getType() == Token.EOF)
                    break;
                final String s = tok.getText();
                writer.print(s);
                if (enableCopyOutput2Stderr) {
                    System.err.print(s);
                    System.err.flush();
                }
            }
            writer.flush();
        } catch (final Exception e) {
            final StringBuilder buf = new StringBuilder("Preprocessor failed:\n");
            Source s = cpp.getSource();
            while (s != null) {
                buf.append(" -> ").append(s).append("\n");
                s = s.getParent();
            }
            buf.append(" : {0}\n");
            LOG.log(Level.SEVERE, buf.toString(), e);
            if( e instanceof GlueGenException ) {
                throw (GlueGenException)e;
            } else {
                throw new GlueGenException("Preprocessor failed",
                                           new ASTLocusTag(null != s ? s.getPath() : "n/a",
                                                           null != s ? s.getLine() : -1,
                                                           null != s ? s.getColumn() : -1, null), e);
            }
        }
    }

}