1 /* 2 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 package com.sun.tools.jextract; 25 26 import com.sun.tools.jextract.parser.Parser; 27 import com.sun.tools.jextract.tree.Tree; 28 29 import java.nio.file.Path; 30 import java.util.Collection; 31 import java.util.LinkedHashMap; 32 import java.util.List; 33 import java.util.Map; 34 import java.util.function.Function; 35 import java.util.stream.Collectors; 36 37 public class JextractTool { 38 private final HeaderResolver headerResolver; 39 private final SymbolFilter symbolFilter; 40 private final Parser parser; 41 private final Function<HeaderFile, AsmCodeFactory> codeFactory; 42 private final Collection<String> clangArgs; 43 private final Collection<Path> sources; 44 45 public JextractTool(Context ctx) { 46 this.headerResolver = new HeaderResolver(ctx); 47 this.parser = new Parser(ctx, Main.INCLUDE_MACROS); 48 this.symbolFilter = new SymbolFilter(ctx); 49 this.codeFactory = ctx.genStaticForwarder ? 50 hf -> new AsmCodeFactoryExt(ctx, hf) : 51 hf -> new AsmCodeFactory(ctx, hf); 52 this.clangArgs = ctx.clangArgs; 53 this.sources = ctx.sources; 54 } 55 56 /** This is the main jextract entry point */ 57 public Writer processHeaders() { 58 Map<HeaderFile, List<Tree>> headerMap = parser.parse(sources, clangArgs).stream() 59 .map(symbolFilter) 60 .map(new TypedefHandler()) 61 .map(new EmptyNameHandler()) 62 .map(new DuplicateDeclarationHandler()) 63 .flatMap(h -> h.declarations().stream()) 64 .distinct() 65 .collect(Collectors.groupingBy(this::headerFromDecl)); 66 67 //generate classes 68 Map<String, byte[]> results = new LinkedHashMap<>(); 69 headerMap.forEach((hf, decls) -> generateHeader(hf, decls, results)); 70 return new Writer(results); 71 } 72 73 private void generateHeader(HeaderFile hf, List<Tree> decls, Map<String, byte[]> results) { 74 TypeEnter enter = new TypeEnter(hf.dictionary()); 75 decls.forEach(t -> t.accept(enter, null)); 76 AsmCodeFactory cf = codeFactory.apply(hf); 77 results.putAll(cf.generateNativeHeader(decls)); 78 } 79 80 private HeaderFile headerFromDecl(Tree tree) { 81 Path path = tree.cursor().getSourceLocation().getFileLocation().path(); 82 return headerResolver.headerFor(path); 83 } 84 }