Skip to content

Commit

Permalink
Add PETSc driver script example
Browse files Browse the repository at this point in the history
  • Loading branch information
ihnorton committed Aug 24, 2013
1 parent d5cbd05 commit 1489ab3
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions examples/wrap_PETSc.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#
# Example script to wrap PETSc
#

using Clang.cindex
using Clang.wrap_c

PETSC_INCLUDE = abspath("/usr/include/petsc")
MPI_INCLUDE = "/usr/include/openmpi"
JULIA_ROOT=abspath(JULIA_HOME, "../../")

LLVM_VER = "3.3"
LLVM_BUILD_TYPE = "Release+Asserts"

LLVM_PATH = joinpath(JULIA_ROOT, "deps/llvm-$LLVM_VER")

This comment has been minimized.

Copy link
@stevengj

stevengj Jan 23, 2015

Why does this LLVM_PATH stuff need to be included in the script? Isn't that set up by Pkg.build("Clang")?

clanginc_path = joinpath(LLVM_PATH, "build/$LLVM_BUILD_TYPE/lib/clang/3.3/include")

petsc_hdrs = [joinpath(PETSC_INCLUDE, "petsc.h")]

clang_includes = map(x::ASCIIString->joinpath(LLVM_PATH, x), [
"build/$LLVM_BUILD_TYPE/lib/clang/3.3/include",
"build/include/",
"include"
])
push!(clang_includes, PETSC_INCLUDE)
push!(clang_includes, MPI_INCLUDE)
clang_extraargs = ["-v"]
# clang_extraargs = ["-D", "__STDC_LIMIT_MACROS", "-D", "__STDC_CONSTANT_MACROS"]

# Callback to test if a header should actually be wrapped (for exclusion)
function should_wrap(hdr::ASCIIString, name::ASCIIString)
return beginswith(dirname(hdr), PETSC_INCLUDE)
end

function lib_file(hdr::ASCIIString)
return "petsc"
end

function output_file(hdr::ASCIIString)
return "PETSc.jl"
end

const wc = wrap_c.init("libPETSc_h.jl", "libPETSc_common.jl", clang_includes,
clang_extraargs,
should_wrap,
lib_file,
output_file)

function wrap_libPETSc(wc::WrapContext, wrap_hdrs)
wrap_c.wrap_c_headers(wc, wrap_hdrs)
end

6 comments on commit 1489ab3

@ihnorton
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@stevengj

output here: https://gist.github.com/ihnorton/6331142

I need to tweak the exclusion criteria a little bit because right now it is pulling in hdf5 and other externals that we don't actually want to wrap. Loading takes about 6 seconds - which is faster than I expected.

fwiw, I agree with you about wrapping high-level APIs. I don't think clang.jl is worth the trouble for less than at least several dozen functions. The real goal of writing it was as a foundation for a C++ wrapper generator, which is much less practical to do manually.

@stevengj
Copy link

Choose a reason for hiding this comment

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

I can't seem to get this file to work anymore. I updated the paths to those on my system, commented out the reference to the obsolete wc.cache_wrapped field, and it ran without printing an error, but no output was produced.

Also, I don't understand why this is so complicated. Why shouldn't it suffice to do

using Clang
using Clang.cindex

function should_wrap(top_hdr, cursor_hdr)
    println("hdr_wrapped: $top_hdr, $cursor_hdr")
    return startswith(basename(cursor_hdr), "petsc")
end

context = Clang.wrap_c.init(common_file="petsc_h.jl",
                            output_dir="petsc",
                            header_library=x->"petsc",
                            clang_includes=["/usr/local/include"],
                            header_wrapped = should_wrap,
                            clang_diagnostics=true)
context.headers = ["/usr/local/include/petsc.h"]

? This produces an output file, at least, but the output file is empty.

@ihnorton
Copy link
Collaborator Author

@ihnorton ihnorton commented on 1489ab3 Jan 23, 2015 via email

Choose a reason for hiding this comment

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

@stevengj
Copy link

Choose a reason for hiding this comment

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

If you get a chance to update this soon, that would be great. I was hoping to work on the PETSc Julia wrappers, but I wanted to Clang-ify petsc.h first.

@ihnorton
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Fixed a couple small but blocking bugs. Updated now. The output is somewhat expansive, mostly due to trying much harder now with macros (of which petsc uses many) than in Aug 2013. It may take some more tweaking here and in wrap_c to get down to repeatable re-generation without hand-editing.

@ihnorton
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I updated the gist with the latest generator output (this is against petsc-dev 3.4.2 on ubuntu 14.04)

Please sign in to comment.