-
Notifications
You must be signed in to change notification settings - Fork 68
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature suggestion: @cinclude macro. #309
Comments
Better to rebuild the package upon the latest Clang.jl#master and use cross-platform support for loading those system headers. There might be compatibility issues between the stdlibs used in Julia and those on the users local machine. |
Thanks for the tip @Gnimuc. In my current project I'm only using released packages, so I'll hold off using |
@notinaboat I've been thinking of making a Julia package like rust's libc to provide similar functionalities in your package, which doesn't need libclang as a runtime dependency. But I don't have enough bandwidth to work on it. |
Hi @Gnimuc, Also of interest is this C REPL I found today: |
Yeah. Clang is a full-featured compiler library with which you can do what a compiler can do, for example, implementing a C++ interpreter. https://github.com/JuliaInterop/Cxx.jl is another example. |
Yes, Cxx.jl is awesome! I played with it a little many years ago, but I have never been able to use it in a project. |
A proof-of-concept of this is already demonstrated with https://github.com/analytech-solutions/System.jl using https://github.com/analytech-solutions/CBinding.jl to expose LibC and Linux API's, so it is possible to do in Julia. |
The problem with trying to interpret the The problem with building an inline C++ program to evaluate I've now implemented a basic macro-value resolver using the C-REPL See: https://github.com/notinaboat/UnixIO.jl/blob/julia18/packages/UnixIOHeaders/src/UnixIOHeaders.jl#L65 First the relevant headers are % cling -isystem/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk//usr/include
[cling]$ #include <signal.h>
[cling]$ SIGSEGV
(int) 11
[cling]$ SA_RESTART
(int) 2
[cling]$ SIG_DFL
(void (*)(int)) Function @0x0
[cling]$ BADSIG
(void (*)(int)) Function @0xffffffffffffffff
[cling]$ UINT8_MAX
(int) 255
[cling]$ UINT16_MAX
(int) 65535
[cling]$ UINT32_MAX
(unsigned int) 4294967295
[cling]$ UINT64_MAX
(unsigned long long) 18446744073709551615 In Julia we end up with: const SIGSEGV = Cint(11)
const SA_RESTART = Cint(2)
const SIG_DFL = bitcast(Ptr{Cvoid}, UInt(0x00))
const BADSIG = bitcast(Ptr{Cvoid}, UInt(0xffffffffffffffff))
const UINT8_MAX = Cint(255)
const UINT16_MAX = Cint(65535)
const UINT32_MAX = Cuint(4294967295)
const UINT64_MAX = Culonglong(18446744073709551615) The existing const SIGSEGV = 11
const SA_RESTART = 0x0002
const SIG_DFL = ((Cvoid(*))(Cint))(0)
const SIG_ERR = (Cvoid(*))(Cint) - 1
const BADSIG = SIG_ERR
const UINT8_MAX = 255
const UINT16_MAX = 65535
const UINT32_MAX = Cuint(4294967295)
const UINT64_MAX = Culonglong(18446744073709551615)
In the case where the symbol does not resolve to a value, the cling REPL prints and error message. [cling]$ sa_handler
input_line_11:2:2: error: cannot use dot operator on a type
sa_handler
^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk//usr/include/sys/signal.h:295:38: note: expanded from macro 'sa_handler'
#define sa_handler __sigaction_u.__sa_handler
^
[cling]$ PTHREAD_ONCE_INIT
input_line_19:2:2: error: expected ';' after expression
PTHREAD_ONCE_INIT
^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk//usr/include/pthread.h:220:50: note: expanded from macro 'PTHREAD_ONCE_INIT'
#define PTHREAD_ONCE_INIT {_PTHREAD_ONCE_SIG_init, {0}}
^
input_line_19:2:2: error: expected ';' after expression
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk//usr/include/pthread.h:220:54: note: expanded from macro 'PTHREAD_ONCE_INIT'
#define PTHREAD_ONCE_INIT {_PTHREAD_ONCE_SIG_init, {0}}
^
[cling]$ |
As for the interpreter , some work has been done in Gnimuc/ClangCompiler.jl#16, but not finished. I'm still waiting for the integration of Cling and LLVM/Clang. There was a proposal for exposing a generic C++ interop interface from LLVM/Clang. |
Looks good, I will look forward to this! |
In Clang.jl, integer literals(e.g. |
Clang.jl also inherits Julia's behaviour of converting hexadecimal constants to the smallest possible unsigned type (In C they are |
I'm wondering how bad this would be when doing interop-ing. I guess it's OK for most of the cases as long as the type is large enough to cover the value. But indeed this should be fixed in the future. |
Cling is now available as a jll: https://github.com/JuliaBinaryWrappers/Cling_jll.jl |
In some cases, especially for standard C library APIs, it is convenient to just
#include
a header without having to generate a wrapper first.I've made a little prototype
@cinclude
macro here: https://github.com/notinaboat/CInclude.jle.g.
The text was updated successfully, but these errors were encountered: