1
+ using Clang. wrap_c
2
+
1
3
# This file is not an active part of the package. This is the code
2
4
# 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)
3
13
4
14
# Find all headers
5
- incpath = normpath (joinpath (dirname (@__FILE__ ), " .." , " deps" , " usr" , " include" ))
15
+ const incpath = normpath (joinpath (dirname (@__FILE__ ), " .." , " deps" , " usr" , " include" ))
6
16
if ! isdir (incpath)
7
17
error (" Run Pkg.build(\" Sundials\" ) before trying to wrap C headers." )
8
18
end
9
19
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)))))
34
30
end
31
+ # @show sundials_headers
35
32
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
+ ]
43
37
44
38
# check_use_header(path) = true
45
39
# 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 )
47
41
! ismatch (r" (_parallel|_impl)\. h$" , cursor_header) && # don't wrap parallel and implementation definitions
48
42
(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)
49
43
end
50
44
51
- function wrap_cursor (name:: ASCIIString , cursor)
45
+ function wrap_cursor (name:: AbstractString , cursor)
52
46
if typeof (cursor) == Clang. cindex. FunctionDecl
53
47
# only wrap API functions
54
48
return ismatch (r" ^(CV|KIN|IDA|N_V)" , name)
@@ -63,7 +57,7 @@ function julia_file(header::AbstractString)
63
57
if src_name == " sundials"
64
58
src_name = " libsundials" # avoid having both Sundials.jl and sundials.jl
65
59
end
66
- return string (src_name, " .jl" )
60
+ return joinpath (outpath, string (src_name, " .jl" ) )
67
61
end
68
62
function library_file (header:: AbstractString )
69
63
header_name = basename (header)
@@ -74,18 +68,21 @@ function library_file(header::AbstractString)
74
68
end
75
69
end
76
70
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
89
86
90
87
# 1st arg name to wrapped arg type map
91
88
const arg1_name2type = Dict (
@@ -97,6 +94,7 @@ const arg1_name2type = Dict(
97
94
:idaadj_mem => :(IDAMemPtr), # Sundials typo?
98
95
)
99
96
97
+ # substitute Ptr{Void} with the typed pointer
100
98
const ctor_return_type = Dict (
101
99
" CVodeCreate" => :(CVODEMemPtr),
102
100
" IDACreate" => :(IDAMemPtr),
@@ -246,4 +244,5 @@ context.rewriter = function(exprs)
246
244
mod_exprs
247
245
end
248
246
247
+ info (" Generating .jl wrappers for Sundials in $outpath ..." )
249
248
run (context)
0 commit comments