< prev index next >

src/jdk.jextract/share/classes/com/sun/tools/jextract/Context.java

Print this page




   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 package com.sun.tools.jextract;
  24 
  25 import jdk.internal.clang.*;
  26 import jdk.internal.foreign.LibrariesHelper;
  27 
  28 import java.io.ByteArrayOutputStream;
  29 import java.io.File;
  30 import java.io.IOException;
  31 import java.io.OutputStream;
  32 import java.io.PrintWriter;
  33 import java.io.UncheckedIOException;
  34 import java.lang.invoke.MethodHandles;

  35 import java.foreign.Library;

  36 import java.nio.file.Files;
  37 import java.nio.file.Path;

  38 import java.util.ArrayList;
  39 import java.util.Arrays;
  40 import java.util.Collections;
  41 import java.util.HashMap;
  42 import java.util.List;
  43 import java.util.Map;

  44 import java.util.Properties;
  45 import java.util.Set;
  46 import java.util.TreeSet;
  47 import java.util.function.Function;
  48 import java.util.function.Predicate;
  49 import java.util.jar.JarOutputStream;
  50 import java.util.logging.Logger;
  51 import java.util.regex.Pattern;
  52 import java.util.stream.Collectors;
  53 import java.util.zip.ZipEntry;
  54 import com.sun.tools.jextract.parser.Parser;
  55 import com.sun.tools.jextract.tree.FunctionTree;
  56 import com.sun.tools.jextract.tree.HeaderTree;
  57 import com.sun.tools.jextract.tree.Tree;
  58 
  59 import static java.nio.file.StandardOpenOption.CREATE;
  60 import static java.nio.file.StandardOpenOption.TRUNCATE_EXISTING;
  61 import static java.nio.file.StandardOpenOption.WRITE;
  62 
  63 /**


 124     public void addSource(Path path) {
 125         sources.add(path);
 126     }
 127 
 128     void addLibraryName(String name) {
 129         libraryNames.add(name);
 130     }
 131 
 132     void addLibraryPath(String path) {
 133         libraryPaths.add(path);
 134     }
 135 
 136     void addLinkCheckPath(String path) {
 137         linkCheckPaths.add(path);
 138     }
 139 
 140     void addExcludeSymbols(String pattern) {
 141         excludeSymbols.add(Pattern.compile(pattern));
 142     }
 143 































 144     private void initSymChecker() {
 145         if (!libraryNames.isEmpty() && !linkCheckPaths.isEmpty()) {
 146             Library[] libs = LibrariesHelper.loadLibraries(MethodHandles.lookup(),
 147                 linkCheckPaths.toArray(new String[0]),
 148                 libraryNames.toArray(new String[0]));
 149 
 150             // check if the given symbol is found in any of the libraries or not.
 151             // If not found, warn the user for the missing symbol.
 152             symChecker = name -> {
 153                 if (Main.DEBUG) {
 154                     err.println("Searching symbol: " + name);
 155                 }
 156                 return (Arrays.stream(libs).filter(lib -> {
 157                         try {
 158                             lib.lookup(name);
 159                             if (Main.DEBUG) {
 160                                 err.println("Found symbol: " + name);
 161                             }
 162                             return true;
 163                         } catch (NoSuchMethodException nsme) {
 164                             return false;
 165                         }
 166                     }).findFirst().isPresent());




   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 package com.sun.tools.jextract;
  24 
  25 import jdk.internal.clang.*;

  26 
  27 import java.io.ByteArrayOutputStream;
  28 import java.io.File;
  29 import java.io.IOException;
  30 import java.io.OutputStream;
  31 import java.io.PrintWriter;
  32 import java.io.UncheckedIOException;
  33 import java.lang.invoke.MethodHandles;
  34 import java.lang.invoke.MethodHandles.Lookup;
  35 import java.foreign.Library;
  36 import java.foreign.Libraries;
  37 import java.nio.file.Files;
  38 import java.nio.file.Path;
  39 import java.nio.file.Paths;
  40 import java.util.ArrayList;
  41 import java.util.Arrays;
  42 import java.util.Collections;
  43 import java.util.HashMap;
  44 import java.util.List;
  45 import java.util.Map;
  46 import java.util.Optional;
  47 import java.util.Properties;
  48 import java.util.Set;
  49 import java.util.TreeSet;
  50 import java.util.function.Function;
  51 import java.util.function.Predicate;
  52 import java.util.jar.JarOutputStream;
  53 import java.util.logging.Logger;
  54 import java.util.regex.Pattern;
  55 import java.util.stream.Collectors;
  56 import java.util.zip.ZipEntry;
  57 import com.sun.tools.jextract.parser.Parser;
  58 import com.sun.tools.jextract.tree.FunctionTree;
  59 import com.sun.tools.jextract.tree.HeaderTree;
  60 import com.sun.tools.jextract.tree.Tree;
  61 
  62 import static java.nio.file.StandardOpenOption.CREATE;
  63 import static java.nio.file.StandardOpenOption.TRUNCATE_EXISTING;
  64 import static java.nio.file.StandardOpenOption.WRITE;
  65 
  66 /**


 127     public void addSource(Path path) {
 128         sources.add(path);
 129     }
 130 
 131     void addLibraryName(String name) {
 132         libraryNames.add(name);
 133     }
 134 
 135     void addLibraryPath(String path) {
 136         libraryPaths.add(path);
 137     }
 138 
 139     void addLinkCheckPath(String path) {
 140         linkCheckPaths.add(path);
 141     }
 142 
 143     void addExcludeSymbols(String pattern) {
 144         excludeSymbols.add(Pattern.compile(pattern));
 145     }
 146 
 147     // return the absolute path of the library of given name by searching
 148     // in the given array of paths.
 149     private static Optional<Path> findLibraryPath(Path[] paths, String libName) {
 150          return Arrays.stream(paths).
 151               map(p -> p.resolve(System.mapLibraryName(libName))).
 152               filter(Files::isRegularFile).map(Path::toAbsolutePath).findFirst();
 153     }
 154 
 155     /*
 156      * Load the specified shared libraries from the specified paths.
 157      *
 158      * @param lookup Lookup object of the caller.
 159      * @param pathStrs array of paths to load the shared libraries from.
 160      * @param names array of shared library names.
 161      */
 162     // used by jextract tool to load libraries for symbol checks.
 163     public static Library[] loadLibraries(Lookup lookup, String[] pathStrs, String[] names) {
 164         if (pathStrs == null || pathStrs.length == 0) {
 165             return Arrays.stream(names).map(
 166                 name -> Libraries.loadLibrary(lookup, name)).toArray(Library[]::new);
 167         } else {
 168             Path[] paths = Arrays.stream(pathStrs).map(Paths::get).toArray(Path[]::new);
 169             return Arrays.stream(names).map(libName -> {
 170                 Optional<Path> absPath = findLibraryPath(paths, libName);
 171                 return absPath.isPresent() ?
 172                     Libraries.load(lookup, absPath.get().toString()) :
 173                     Libraries.loadLibrary(lookup, libName);
 174             }).toArray(Library[]::new);
 175         }
 176     }
 177 
 178     private void initSymChecker() {
 179         if (!libraryNames.isEmpty() && !linkCheckPaths.isEmpty()) {
 180             Library[] libs = loadLibraries(MethodHandles.lookup(),
 181                 linkCheckPaths.toArray(new String[0]),
 182                 libraryNames.toArray(new String[0]));
 183 
 184             // check if the given symbol is found in any of the libraries or not.
 185             // If not found, warn the user for the missing symbol.
 186             symChecker = name -> {
 187                 if (Main.DEBUG) {
 188                     err.println("Searching symbol: " + name);
 189                 }
 190                 return (Arrays.stream(libs).filter(lib -> {
 191                         try {
 192                             lib.lookup(name);
 193                             if (Main.DEBUG) {
 194                                 err.println("Found symbol: " + name);
 195                             }
 196                             return true;
 197                         } catch (NoSuchMethodException nsme) {
 198                             return false;
 199                         }
 200                     }).findFirst().isPresent());


< prev index next >