Skip to content

Conversation

@jonas
Copy link
Member

@jonas jonas commented Apr 18, 2017

This is still early stage, but it would be great to have some feedback regarding what to focus on at this point and ease the way to have it integrated. For now it is focused on internal usage and a good use case will be to help complete #104. However, for this to happen there are some things to figure out such as how to sort the read C types to make them stable across systems, whether to use white- or black-listing to "weed" out non-POSIX/standard types etc.

Some initial thoughts can be found in docs/contrib/bindgen.rst
And a dummy test parsing unit-tests/src/main/resources/bindgen/test.h is in the BindgenSuite.

References: #154
CC: @frgomes


import scalanative.native._

#define EXTERN_FUNCTION0(symbol, id, tpe) @name(#symbol) def id(): tpe = extern;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A new type of Scala macro ;)

struct bindgen_context *ctx = data;

CXSourceLocation location = clang_getCursorLocation(cursor);
if (!clang_Location_isFromMainFile(location))
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FIXME: Move to Scala code

|@extern
|object Test {
| object color {
| val RED = 1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe @inline final val ?

extern;

@name("scalanative_clang_getCursorKind")
def getCursorKind(`*cursor`: CXCursor): CXCursorKind = extern;
Copy link
Contributor

@MasseGuillaume MasseGuillaume Apr 18, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hum this *cursor looks suspicious.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this is a relic of using C preprocessor to generate the Scala code.


Binding generation for now is experimental. The goal is to first use it
internally to generate bindings for C's stdlib and later make it
available via scala.meta macros and sbt tasks.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would vote for code generation here. The less powerful technique that works well with IDEs

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IDE support is a blocker here, if scala.meta macros don't work well enough we might have to use code generation instead.

@MasseGuillaume
Copy link
Contributor

Really cool! Nice work. I can help on the sbt front ;-).

@jonas jonas force-pushed the bindgen branch 3 times, most recently from 9d1d4da to f2f6e79 Compare April 21, 2017 04:11
@jonas
Copy link
Member Author

jonas commented Apr 21, 2017

The main problem right now is that xcode includes the libclang dynlib but not the libclang header files.

@jonas
Copy link
Member Author

jonas commented Apr 22, 2017

Achievement unlocked 🎉 CI passes

.travis.yml Outdated
- brew install jq
- brew install re2
- brew install [email protected]
env: PATH="$PATH:/usr/local/opt/[email protected]/bin"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why 3.7 ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To keep the macOS build on the same version, however, we could also change it so CI builds for oldest and newest supported LLVM version.

@densh
Copy link
Member

densh commented Apr 22, 2017

Amazing progress @jonas! I'm going to have a deeper look at the code itself as soon as 0.2 is shipped.

@jonas jonas force-pushed the bindgen branch 2 times, most recently from 8545f12 to 2d6af88 Compare April 24, 2017 12:43
@densh densh changed the title Proof-of-concept to generate bindings using libclang [WIP] generate bindings using libclang May 3, 2017
jonas added 5 commits June 1, 2017 21:25
Make sure we pick the version of llvm-config which matches the
discovered clang binary. Currently Scala Native does not use LLVM
include and libraries however this will be important for bindgen and
other tooling.
@jacob-carlborg
Copy link

jacob-carlborg commented Jun 5, 2017

However, for this to happen there are some things to figure out such as how to sort the read C types to make them stable across systems

This is an interesting problem. I work on a binding tool for D [1] myself using libclang as well. When header files start to use #ifdef to check for different platforms it starts to get complicated. For example:

#ifdef _WIN32
#include <windows.h>
DWORD foo();
#else
int foo();
#endif

The above code would generate different bindings on different platforms. Trying to generate bindings for the Windows branch on a Posix platform wouldn't work because windows.h wouldn't be available.

I would also say it depends on how the bindings are used/generated. If the bindings are to be generated with an external tool and placed in a source repository you will have the problem mentioned above. The advantage of this approach is that it's possible (or at least simpler) to create wrappers on top of the bindings, to make the API nicer for the host language.

But, if the bindings are generated on the fly by the compiler or a build tool the above problem doesn't matter since you can only interface with the API that is provided on the current platform anyway, making platform dependent bindings not an issue. This is kind of how Swift interfaces with C and Objective-C.

[1] https://github.com/jacob-carlborg/dstep

jonas pushed a commit to jonas/scala-bindgen that referenced this pull request Jul 5, 2017
Imports the libclang wrapper from scala-native/scala-native#642 as well
as the build setup to compile it. Test bindgen against the existing
test files in a separate project.
@densh
Copy link
Member

densh commented Aug 1, 2017

It seems like current state of this PR is a bit premature to be included upstream. Especially considering that we're planning to do a major overhaul of the interop layer in 0.4 series.

Additionally, the binding generation tool can happily leave outside of the main repo in its own subproject and including it into the main repo might aggravate our build time and CI issues we've been having recently.

I'm closing this PR for now, we can continue discussion on going forward with this on gitter.

@densh densh closed this Aug 1, 2017
@jonas jonas deleted the bindgen branch August 1, 2017 10:53
@jonas
Copy link
Member Author

jonas commented Aug 1, 2017

👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants