/
utils.jl
109 lines (91 loc) · 3.57 KB
/
utils.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
"""
Reserved Julia identifiers will be prepended with "_"
"""
const RESERVED_WORDS = ["begin", "while", "if", "for", "try", "return", "break", "continue",
"function", "macro", "quote", "let", "local", "global", "const", "do",
"struct", "module", "baremodule", "using", "import", "export", "end",
"else", "elseif", "catch", "finally", "true", "false"]
"""
name_safe(name::AbstractString)
Return a valid Julia variable name, prefixed with "_" if the `name` is conflict with Julia's
reserved words.
"""
name_safe(name::AbstractString) = (name in RESERVED_WORDS) ? "_"*name : name
"""
symbol_safe(name::AbstractString)
Same as [`name_safe`](@ref), but return a Symbol.
"""
symbol_safe(name::AbstractString) = Symbol(name_safe(name))
## dumpobj
printind(ind::Int, st...) = println(join([repeat(" ", 2*ind), st...]))
dumpobj(cursor::CLCursor) = dumpobj(0, cursor)
dumpobj(t::CLType) = join(typeof(t), " ", spelling(t))
dumpobj(t::CLInt) = t
dumpobj(t::CLPointer) = pointee_type(t)
dumpobj(ind::Int, t::CLType) = printind(ind, dumpobj(t))
function dumpobj(ind::Int, cursor::Union{CLFieldDecl, CLParmDecl})
printind(ind+1, typeof(cursor), " ", dumpobj(type(cursor)), " ", name(cursor))
end
function dumpobj(ind::Int, node::Union{CLCursor,CLStructDecl,CLCompoundStmt,CLFunctionDecl,CLBinaryOperator})
printind(ind, " ", typeof(node), " ", name(node))
for c in children(node)
dumpobj(ind + 1, c)
end
end
"""
copydeps(dst)
Copy dependencies to `dst`.
"""
function copydeps(dst)
cp(joinpath(@__DIR__, "ctypes.jl"), joinpath(dst, "ctypes.jl"), force=true)
end
function print_template(path, libname="LibXXX")
open(path, "w") do io
print(io, """
module $libname
import Libdl
# Load in `deps.jl`, complaining if it does not exist
const depsjl_path = joinpath(@__DIR__, "..", "deps", "deps.jl")
if !isfile(depsjl_path)
error("$libname was not build properly. Please run Pkg.build(\\"$libname\\").")
end
include(depsjl_path)
# Module initialization function
function __init__()
check_deps()
end
using CEnum
include("ctypes.jl")
export Ctm, Ctime_t, Cclock_t
include(joinpath(@__DIR__, "..", "gen", "$(lowercasefirst(libname))_common.jl"))
include(joinpath(@__DIR__, "..", "gen", "$(lowercasefirst(libname))_api.jl"))
# export everything
#foreach(names(@__MODULE__, all=true)) do s
# if startswith(string(s), "SOME_PREFIX")
# @eval export \$s
# end
#end
end # module
""")
end
end
function find_std_headers()
headers = String[]
@static if Sys.isapple()
xcode_path = strip(read(`xcode-select --print-path`, String))
sdk_path = strip(read(`xcrun --show-sdk-path`, String))
occursin("Xcode", xcode_path) && (xcode_path *= "/Toolchains/XcodeDefault.xctoolchain/")
didfind = false
lib = joinpath(xcode_path, "usr", "lib", "c++", "v1")
inc = joinpath(xcode_path, "usr", "include", "c++", "v1")
isdir(lib) && (push!(headers, lib); didfind = true;)
isdir(inc) && (push!(headers, inc); didfind = true;)
if isdir("/usr/include")
push!(headers, "/usr/include")
else isdir(joinpath(sdk_path, "usr", "include"))
push!(headers, joinpath(sdk_path, "usr", "include"))
end
didfind || error("Could not find C++ standard library. Is XCode or CommandLineTools installed?")
end
return headers
end