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.FlexibleArrayWarningVisitor;
  28 import com.sun.tools.jextract.tree.Tree;
  29 
  30 import java.io.IOException;
  31 import java.io.UncheckedIOException;
  32 import java.nio.file.Files;
  33 import java.nio.file.Path;
  34 import java.util.Collection;
  35 import java.util.HashSet;
  36 import java.util.LinkedHashMap;
  37 import java.util.List;
  38 import java.util.Map;
  39 import java.util.Set;
  40 import java.util.function.Function;
  41 import java.util.stream.Collectors;
  42 import java.util.stream.Stream;
  43 
  44 public class JextractTool {
  45     private final HeaderResolver headerResolver;
  46     private final Parser parser;
  47     private final Function<HeaderFile, AsmCodeFactory> codeFactory;
  48     private final Collection<String> clangArgs;
  49     private final Collection<Path> sources;
  50     private final Context ctx;
  51 
  52     public JextractTool(Context ctx) {
  53         this.headerResolver = new HeaderResolver(ctx);
  54         this.parser = new Parser(ctx, Options.INCLUDE_MACROS);
  55         this.codeFactory = ctx.options.genStaticForwarder ?
  56                 hf -> new AsmCodeFactoryExt(ctx, hf) :
  57                 hf -> new AsmCodeFactory(ctx, hf);
  58         this.clangArgs = ctx.options.clangArgs;
  59         this.sources = ctx.sources;
  60         this.ctx = ctx;
  61         assert sources.size() > 0;
  62     }
  63 
  64     /** This is the main jextract entry point */
  65     public Writer processHeaders() {
  66         Path source = sources.size() > 1? generateTmpSource() : sources.iterator().next();
  67         Map<HeaderFile, List<Tree>> headerMap = Stream.of(parser.parse(source, clangArgs))
  68                 .map(new SymbolFilter(ctx))
  69                 .map(new LibraryLookupFilter(ctx))
  70                 .map(new DependencyFilter(ctx))
  71                 .map(new TypedefHandler(ctx))
  72                 .map(new EmptyNameHandler())
  73                 .map(new DuplicateDeclarationHandler())
  74                 .flatMap(h -> h.declarations().stream())
  75                 .peek(new FlexibleArrayWarningVisitor(ctx))
  76                 .collect(Collectors.groupingBy(this::headerFromDecl));
  77 
  78         //generate classes
  79         Map<String, byte[]> results = new LinkedHashMap<>();
  80         headerMap.forEach((hf, decls) -> generateHeader(hf, decls, results));
  81         return new Writer(ctx, results);
  82     }
  83 
  84     private void generateHeader(HeaderFile hf, List<Tree> decls, Map<String, byte[]> results) {
  85         TypeEnter enter = new TypeEnter(hf.dictionary());
  86         decls.forEach(t -> t.accept(enter, null));
  87         if (ctx.options.srcDumpDir != null) {
  88             JavaSourceFactory jsb = ctx.options.genStaticForwarder ?
  89                 new JavaSourceFactoryExt(ctx, hf) : new JavaSourceFactory(ctx, hf);
  90             jsb.generate(decls);
  91         }
  92         AsmCodeFactory cf = codeFactory.apply(hf);
  93         results.putAll(cf.generateNativeHeader(decls));
  94     }
  95 
  96     private HeaderFile headerFromDecl(Tree tree) {
  97         Path path = tree.cursor().getSourceLocation().getFileLocation().path();
  98         return headerResolver.headerFor(path);
  99     }
 100 
 101     private Path generateTmpSource() {
 102         assert sources.size() > 1;
 103         try {
 104             Path tmpFile = Files.createTempFile("jextract", ".h");
 105             tmpFile.toFile().deleteOnExit();
 106             Files.write(tmpFile, sources.stream().
 107                 map(src -> "#include \"" + src + "\"").
 108                 collect(Collectors.toList()));
 109             return tmpFile;
 110         } catch (IOException ioExp) {
 111             throw new UncheckedIOException(ioExp);
 112         }
 113     }
 114 }