Skip to content

Commit cef09a9

Browse files
committed
cleanup wrapper generator
* add comments * don't cd() to the directory where the headers are generated * don't require JULIAHOME env variable
1 parent 04eb9d3 commit cef09a9

File tree

1 file changed

+46
-47
lines changed

1 file changed

+46
-47
lines changed

src/wrap_sundials.jl

Lines changed: 46 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,48 @@
1+
using Clang.wrap_c
2+
13
# This file is not an active part of the package. This is the code
24
# that uses the Clang.jl package to wrap Sundials using the headers.
5+
# To run it:
6+
# include(joinpath(Pkg.dir("Sundials"), "src", "wrap_sundials.jl"))
7+
8+
# Where the julian wrappers would be generated
9+
# If the generated .jl files are ok,
10+
# they have to be compied to the package "src" folder overwriting the old ones
11+
const outpath = normpath(joinpath(dirname(@__FILE__), "..", "new"))
12+
mkpath(outpath)
313

414
# Find all headers
5-
incpath = normpath(joinpath(dirname(@__FILE__), "..", "deps", "usr", "include"))
15+
const incpath = normpath(joinpath(dirname(@__FILE__), "..", "deps", "usr", "include"))
616
if !isdir(incpath)
717
error("Run Pkg.build(\"Sundials\") before trying to wrap C headers.")
818
end
919

10-
wdir = joinpath(dirname(@__FILE__), "..", "new")
11-
mkpath(wdir)
12-
cd(wdir)
13-
14-
sundials_names = ["nvector", "sundials", "cvode", "cvodes", "ida", "idas", "kinsol"]
15-
if isdir(joinpath(incpath, "arkode"))
16-
push!(sundials_names, "arkode")
17-
end
18-
headers = ASCIIString[]
19-
for name in sundials_names
20-
path = joinpath(incpath, name)
21-
append!(headers, map(x->joinpath(path, x),
22-
sort!(convert(Vector{ASCIIString}, readdir(path)))))
23-
end
24-
# @show headers
25-
26-
27-
## Do wrapping using Clang.jl
28-
ENV["JULIAHOME"] = "/Users/jgoldfar/Public/julia/usr/"
29-
30-
using Clang.wrap_c
31-
32-
if (!haskey(ENV, "JULIAHOME"))
33-
error("Please set JULIAHOME variable to the root of your julia install")
20+
info("Scanning Sundials headers in $incpath...")
21+
const sundials_folders = filter!(isdir, map!(folder -> joinpath(incpath, folder),
22+
["nvector", "sundials", "cvode", "cvodes",
23+
"ida", "idas", "kinsol", "arkode"]))
24+
info("Folders: $sundials_folders")
25+
const sundials_headers = similar(sundials_folders, 0)
26+
for folder in sundials_folders
27+
append!(sundials_headers,
28+
map(x->joinpath(folder, x),
29+
sort!(convert(typeof(sundials_headers), readdir(folder)))))
3430
end
31+
# @show sundials_headers
3532

36-
clang_includes = map(x->joinpath(ENV["JULIAHOME"], x), [
37-
"deps/llvm-3.2/build/Release/lib/clang/3.2/include",
38-
"deps/llvm-3.2/include",
39-
"deps/llvm-3.2/include",
40-
"deps/llvm-3.2/build/include/",
41-
"deps/llvm-3.2/include/"
42-
])
33+
const clang_path = "/usr/lib/clang/3.8.0" # change to your clang location
34+
const clang_includes = [
35+
joinpath(clang_path, "include"),
36+
]
4337

4438
# check_use_header(path) = true
4539
# Callback to test if a header should actually be wrapped (for exclusion)
46-
function wrap_header(top_hdr::ASCIIString, cursor_header::ASCIIString)
40+
function wrap_header(top_hdr::AbstractString, cursor_header::AbstractString)
4741
!ismatch(r"(_parallel|_impl)\.h$", cursor_header) && # don't wrap parallel and implementation definitions
4842
(top_hdr == cursor_header) # don't wrap if header is included from the other header (e.g. nvector in cvode or cvode_direct from cvode_band)
4943
end
5044

51-
function wrap_cursor(name::ASCIIString, cursor)
45+
function wrap_cursor(name::AbstractString, cursor)
5246
if typeof(cursor) == Clang.cindex.FunctionDecl
5347
# only wrap API functions
5448
return ismatch(r"^(CV|KIN|IDA|N_V)", name)
@@ -63,7 +57,7 @@ function julia_file(header::AbstractString)
6357
if src_name == "sundials"
6458
src_name = "libsundials" # avoid having both Sundials.jl and sundials.jl
6559
end
66-
return string(src_name, ".jl")
60+
return joinpath(outpath, string(src_name, ".jl"))
6761
end
6862
function library_file(header::AbstractString)
6963
header_name = basename(header)
@@ -74,18 +68,21 @@ function library_file(header::AbstractString)
7468
end
7569
end
7670

77-
clang_extraargs = [
78-
"-D", "__STDC_LIMIT_MACROS", "-D", "__STDC_CONSTANT_MACROS",
79-
"-v"]
80-
context = wrap_c.init(
81-
common_file="types_and_consts.jl",
82-
clang_args = clang_extraargs, clang_diagnostics = true,
83-
clang_includes = [clang_includes; incpath],
84-
header_outputfile = julia_file,
85-
header_library = library_file,
86-
header_wrapped=wrap_header,
87-
cursor_wrapped=wrap_cursor)
88-
context.headers = headers
71+
const context = wrap_c.init(
72+
common_file="types_and_consts.jl",
73+
clang_args = [
74+
"-D", "__STDC_LIMIT_MACROS",
75+
"-D", "__STDC_CONSTANT_MACROS",
76+
"-v"
77+
],
78+
clang_diagnostics = true,
79+
clang_includes = [clang_includes; incpath],
80+
header_outputfile = julia_file,
81+
header_library = library_file,
82+
header_wrapped=wrap_header,
83+
cursor_wrapped=wrap_cursor
84+
)
85+
context.headers = sundials_headers
8986

9087
# 1st arg name to wrapped arg type map
9188
const arg1_name2type = Dict(
@@ -97,6 +94,7 @@ const arg1_name2type = Dict(
9794
:idaadj_mem => :(IDAMemPtr), # Sundials typo?
9895
)
9996

97+
# substitute Ptr{Void} with the typed pointer
10098
const ctor_return_type = Dict(
10199
"CVodeCreate" => :(CVODEMemPtr),
102100
"IDACreate" => :(IDAMemPtr),
@@ -246,4 +244,5 @@ context.rewriter = function(exprs)
246244
mod_exprs
247245
end
248246

247+
info("Generating .jl wrappers for Sundials in $outpath...")
249248
run(context)

0 commit comments

Comments
 (0)