java-port / clank

Clank is a Java-port of popular Clang frontend
38 stars 8 forks source link

Update on how to build/use + example #2

Open elijah-z142 opened 5 years ago

elijah-z142 commented 5 years ago

In the readme, it is stated "TODO: Need to prepare build and run instructions" as of Sep. 2017. It is currently Nov. 2018, so I figure that I'd ask for how to use it and whatever information is found can assist with the Readme.

I'm looking to access the AST for a given C source file, similar to what one would take from clang -ast-dump, but with access to the objects from Java. I was going to experiment with building a small C to Java tool as I have been doing a lot of manual C to Java conversions recently for my PHP to Java tool (www.runtimeconverter.com).

What I'd need is a small example of how to get from Clank the AST root element when given a source file, and to build or get from somewhere the minimum .jar files needed to link such an example project.

I've figured out that this repository is a NetBeans project and have built 27 out of 41 modules. The error that I'm stuck on is "Cannot compile against a module: /Applications/NetBeans/NetBeans 8.2.app/Contents/Resources/NetBeans/platform/lib/org-openide-util-lookup.jar because of dependency: org.openide.util.lookup > 8.34 found 8.33.1".

I've included a .txt file with more info about the build attempt. Maybe I already have enough .Jar files (?), but I'd still need that basic example of how to get the AST.

clank_build.txt

elijah-z142 commented 5 years ago

I was able to build the suite by bumping the version of "org-openide-util-lookup.jar" to 8.34 in the MANIFEST.MF file and repackaging. Now I just need an example or some pointers on how to use the APIs.

Also, I was going to attempt to use the steps found in (http://clang.llvm.org/docs/LibTooling.html), but it appears that the class "Tool" is not a part of the java sources.

shanemikel commented 1 year ago

Poking around in this plugin to see how it works. I have successfully built it with Apache Netbeans 16.

I had to open the clank.suite project in Netbeans itself, which automatically generates some properties files in nbproject/private pointing ant to my Netbeans installation. I also had to edit nproject/platform.properties to set nbplatform.active=default.

The clank library uses sun.misc.Unsafe, and I'm not sure how to set compiler options in Netbeans' Ant tasks to open that module to the plugin. In meantime, I built with a Java 1.8 JDK as a workaround.

shanemikel commented 1 year ago

I tried installing the clang.suite NetBeans plugin (build produces a bunch of .nbm files), but I'm not sure if the version here includes any GUI accessible functionality or how to use it.

The script mentioned in the README, modules/org.clang.tools.services/test/unit/clank.sh, does run but it fails with current Clang (system or homebrew version on Mac) with:

Error parsing clang output.

Have to look at what it's trying to parse, but not surprised that CLI (or AST?) output parsing is broken after many years. README mentions a variable, LLVM39, so I guess they were last testing against version 3.9 around 2016.

I was not successful in building LLVM 3.9 on my Intel Mac running Monterey (almost current OS version). I can build all of the LLVM binaries, but the Clang module itself produces just a few errors like so:

llvm/tools/clang/lib/CodeGen/CGOpenMPRuntime.cpp:6191:56: error: a lambda parameter cannot shadow an explicitly captured entity

I think LLVM uses the system libstdc++? Anyway, I will try on Linux.