Still WIP...
Make sure you don't have LIBCLANGEX_INSTALL_PREFIX
in your environment variable.
pkg> add https://github.com/Gnimuc/ClangCompiler.jl.git
- Build libclangex locally and set
ENV["LIBCLANGEX_INSTALL_PREFIX"]
to theinstall
directory. - Install and test the package by running:
pkg> dev https://github.com/Gnimuc/ClangCompiler.jl.git
pkg> test ClangCompiler
using ClangCompiler
# source file
src = joinpath(dirname(pathof(ClangCompiler)), "..", "examples", "sample.cpp")
# compilation flags
args = get_compiler_args()
cc = ClangCompiler.IncrementalIRGenerator(src, args)
decl_lookup = DeclFinder(cc.instance)
@assert decl_lookup(cc, "vector", "std::vector")
decl = get_decl(decl_lookup)
dump(decl)
# clean up
dispose(decl_lookup)
dispose(cc)
The following example is only tested on macOS.
using ClangCompiler
using ClangCompiler.LLVM
# source file
src = joinpath(dirname(pathof(ClangCompiler)), "..", "examples", "sample.cpp")
# compilation flags
args = get_compiler_args()
# create JIT
jit = LLJIT(;tm=JITTargetMachine())
# generate LLVM IR
irgen = IRGenerator(src, args)
# compile and link
cc = CXCompiler(irgen, jit)
link_process_symbols(cc)
compile(cc)
# lookup and call the main function
addr = lookup(jit, "main")
@eval main() = ccall($(pointer(addr)), Cint, ())
main()
# clean up
dispose(cc)