< prev index next >

test/jdk/com/sun/tools/jextract/jclang-ffi/src/jdk/internal/clang/LibClang.java

Print this page




   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 package jdk.internal.clang;
  24 
  25 import clang.CXString.CXString;
  26 
  27 import java.foreign.Libraries;
  28 import java.foreign.Library;
  29 import java.foreign.memory.Pointer;
  30 import java.lang.invoke.MethodHandles;
  31 import java.nio.file.Paths;
  32 
  33 public class LibClang {
  34     private static final boolean DEBUG = Boolean.getBoolean("libclang.debug");
  35 
  36     static final clang.Index lib;
  37     private static final clang.CXString lcxstr;
  38 
  39     static {
  40         if (DEBUG) {
  41             System.err.println("Loading LibClang FFI");
  42         }
  43         String libName = System.getProperty("os.name").startsWith("Windows")
  44                 ? "libclang"
  45                 : "clang";
  46         Library libclang = Libraries.loadLibrary(MethodHandles.lookup(), libName);
  47         lib = Libraries.bind(clang.Index.class, libclang);
  48         lcxstr = Libraries.bind(clang.CXString.class, libclang);
  49     }
  50 
  51     public static Index createIndex(boolean local) {
  52         Index index = new Index(lib.clang_createIndex(local ? 1 : 0, 0));
  53         lib.clang_toggleCrashRecovery(0);
  54         if (DEBUG) {
  55             System.err.println("LibClang crash recovery disabled");
  56         }
  57         return index;
  58     }
  59 
  60     public static String CXStrToString(CXString cxstr) {
  61         Pointer<Byte> buf = lcxstr.clang_getCString(cxstr);
  62         String str = Pointer.toString(buf);
  63         lcxstr.clang_disposeString(cxstr);
  64         return str;
  65     }
  66 
  67     public static String version() {
  68         return CXStrToString(lib.clang_getClangVersion());


   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 package jdk.internal.clang;
  24 
  25 import clang.CXString_h.CXString;
  26 
  27 import java.foreign.Libraries;
  28 import java.foreign.Library;
  29 import java.foreign.memory.Pointer;
  30 import java.lang.invoke.MethodHandles;
  31 import java.nio.file.Paths;
  32 
  33 public class LibClang {
  34     private static final boolean DEBUG = Boolean.getBoolean("libclang.debug");
  35 
  36     static final clang.Index_h lib;
  37     private static final clang.CXString_h lcxstr;
  38 
  39     static {
  40         if (DEBUG) {
  41             System.err.println("Loading LibClang FFI");
  42         }
  43         String libName = System.getProperty("os.name").startsWith("Windows")
  44                 ? "libclang"
  45                 : "clang";
  46         Library libclang = Libraries.loadLibrary(MethodHandles.lookup(), libName);
  47         lib = Libraries.bind(clang.Index_h.class, libclang);
  48         lcxstr = Libraries.bind(clang.CXString_h.class, libclang);
  49     }
  50 
  51     public static Index createIndex(boolean local) {
  52         Index index = new Index(lib.clang_createIndex(local ? 1 : 0, 0));
  53         lib.clang_toggleCrashRecovery(0);
  54         if (DEBUG) {
  55             System.err.println("LibClang crash recovery disabled");
  56         }
  57         return index;
  58     }
  59 
  60     public static String CXStrToString(CXString cxstr) {
  61         Pointer<Byte> buf = lcxstr.clang_getCString(cxstr);
  62         String str = Pointer.toString(buf);
  63         lcxstr.clang_disposeString(cxstr);
  64         return str;
  65     }
  66 
  67     public static String version() {
  68         return CXStrToString(lib.clang_getClangVersion());
< prev index next >