Skip to content

Commit 2bd7d1f

Browse files
Gnimucmaleadt
authored andcommitted
Update wrap.jl to use Clang.jl's new generator
1 parent e063c96 commit 2bd7d1f

File tree

9 files changed

+3640
-3455
lines changed

9 files changed

+3640
-3455
lines changed

lib/libLLVM_common.jl

Lines changed: 0 additions & 741 deletions
This file was deleted.

lib/libLLVM_extra.jl

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# Julia wrapper for source: julia/src/llvm-api.cpp
22

3-
43
# initialization functions
54

65
function LLVMInitializeAllTargetInfos()
@@ -43,7 +42,6 @@ function LLVMInitializeNativeDisassembler()
4342
ccall(:LLVMExtraInitializeNativeDisassembler,LLVMBool,())
4443
end
4544

46-
4745
# infrastructure for writing LLVM passes in Julia
4846

4947
mutable struct LLVMOpaquePass

lib/libLLVM_h.jl

Lines changed: 3480 additions & 2603 deletions
Large diffs are not rendered by default.

res/Manifest.toml

Lines changed: 54 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
# This file is machine-generated - editing it directly is not advised
22

3+
[[ArgTools]]
4+
uuid = "0dad84c5-d112-42e6-8d28-ef12dabb789f"
5+
36
[[Artifacts]]
4-
deps = ["Pkg"]
5-
git-tree-sha1 = "c30985d8821e0cd73870b17b0ed0ce6dc44cb744"
67
uuid = "56f22d72-fd6d-98f1-02f0-08ddc0907c33"
7-
version = "1.3.0"
88

99
[[Base64]]
1010
uuid = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"
@@ -15,92 +15,86 @@ uuid = "fa961155-64e5-5f13-b03f-caf6b980ea82"
1515
version = "0.4.1"
1616

1717
[[Clang]]
18-
deps = ["CEnum", "Clang_jll", "DataStructures", "Libdl"]
19-
git-tree-sha1 = "5237d0c6c52179b958df0aeef171658a791456ab"
18+
deps = ["CEnum", "Clang_jll", "TOML"]
19+
path = "/Users/gnimuc/.julia/dev/Clang"
2020
uuid = "40e3b903-d033-50b4-a0cc-940c62c95e31"
21-
version = "0.12.1"
21+
version = "0.13.0"
2222

2323
[[Clang_jll]]
24-
deps = ["Libdl", "Pkg", "libLLVM_jll"]
25-
git-tree-sha1 = "ff31103423b99f59859d4b0505af4093a1e99079"
24+
deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg", "libLLVM_jll"]
25+
git-tree-sha1 = "df205bee9db582fecbf5393a86f93426b79c0e93"
2626
uuid = "0ee61d77-7f21-5576-8119-9fcc46b10100"
27-
version = "9.0.1+4"
28-
29-
[[Compat]]
30-
deps = ["Base64", "Dates", "DelimitedFiles", "Distributed", "InteractiveUtils", "LibGit2", "Libdl", "LinearAlgebra", "Markdown", "Mmap", "Pkg", "Printf", "REPL", "Random", "SHA", "Serialization", "SharedArrays", "Sockets", "SparseArrays", "Statistics", "Test", "UUIDs", "Unicode"]
31-
git-tree-sha1 = "a706ff10f1cd8dab94f59fd09c0e657db8e77ff0"
32-
uuid = "34da2185-b29b-5c13-b0c7-acf172513d20"
33-
version = "3.23.0"
34-
35-
[[DataStructures]]
36-
deps = ["Compat", "InteractiveUtils", "OrderedCollections"]
37-
git-tree-sha1 = "fb0aa371da91c1ff9dc7fbed6122d3e411420b9c"
38-
uuid = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"
39-
version = "0.18.8"
27+
version = "11.0.1+2"
4028

4129
[[Dates]]
4230
deps = ["Printf"]
4331
uuid = "ade2ca70-3891-5945-98fb-dc099432e06a"
4432

45-
[[DelimitedFiles]]
46-
deps = ["Mmap"]
47-
uuid = "8bb1440f-4735-579b-a4ab-409b98df4dab"
48-
49-
[[Distributed]]
50-
deps = ["Random", "Serialization", "Sockets"]
51-
uuid = "8ba89e20-285c-5b6f-9357-94700520ee1b"
33+
[[Downloads]]
34+
deps = ["ArgTools", "LibCURL", "NetworkOptions"]
35+
uuid = "f43a241f-c20a-4ad4-852c-f6b1247861c6"
5236

5337
[[InteractiveUtils]]
5438
deps = ["Markdown"]
5539
uuid = "b77e0a4c-d291-57a0-90e8-8db25a27a240"
5640

5741
[[JLLWrappers]]
58-
git-tree-sha1 = "c70593677bbf2c3ccab4f7500d0f4dacfff7b75c"
42+
git-tree-sha1 = "a431f5f2ca3f4feef3bd7a5e94b8b8d4f2f647a0"
5943
uuid = "692b3bcd-3c85-4b1f-b108-f13ce0eb3210"
60-
version = "1.1.3"
44+
version = "1.2.0"
6145

6246
[[LLVM_full_jll]]
6347
deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"]
64-
git-tree-sha1 = "465041a64bcea5a504733323f9bf1b4c6220883d"
48+
git-tree-sha1 = "3a744e1960881da01d8f2ebc6117619ee56ca503"
6549
uuid = "a3ccf953-465e-511d-b87f-60a6490c289d"
66-
version = "11.0.0+6"
50+
version = "11.0.0+10"
51+
52+
[[LibCURL]]
53+
deps = ["LibCURL_jll", "MozillaCACerts_jll"]
54+
uuid = "b27032c2-a3e7-50c8-80cd-2d36dbcbfd21"
55+
56+
[[LibCURL_jll]]
57+
deps = ["Artifacts", "LibSSH2_jll", "Libdl", "MbedTLS_jll", "Zlib_jll", "nghttp2_jll"]
58+
uuid = "deac9b47-8bc7-5906-a0fe-35ac56dc84c0"
6759

6860
[[LibGit2]]
69-
deps = ["Printf"]
61+
deps = ["Base64", "NetworkOptions", "Printf", "SHA"]
7062
uuid = "76f85450-5226-5b5a-8eaa-529ad045b433"
7163

64+
[[LibSSH2_jll]]
65+
deps = ["Artifacts", "Libdl", "MbedTLS_jll"]
66+
uuid = "29816b5a-b9ab-546f-933c-edad1886dfa8"
67+
7268
[[Libdl]]
7369
uuid = "8f399da3-3557-5675-b5ff-fb832c97cbdb"
7470

75-
[[LinearAlgebra]]
76-
deps = ["Libdl"]
77-
uuid = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
78-
7971
[[Logging]]
8072
uuid = "56ddb016-857b-54e1-b83d-db4d58db5568"
8173

8274
[[Markdown]]
8375
deps = ["Base64"]
8476
uuid = "d6f4376e-aef5-505a-96c1-9c027394607a"
8577

86-
[[Mmap]]
87-
uuid = "a63ad114-7e13-5084-954f-fe012c677804"
78+
[[MbedTLS_jll]]
79+
deps = ["Artifacts", "Libdl"]
80+
uuid = "c8ffd9c3-330d-5841-b78e-0817d7145fa1"
8881

89-
[[OrderedCollections]]
90-
git-tree-sha1 = "cf59cfed2e2c12e8a2ff0a4f1e9b2cd8650da6db"
91-
uuid = "bac558e1-5e72-5ebc-8fee-abe8a469f55d"
92-
version = "1.3.2"
82+
[[MozillaCACerts_jll]]
83+
uuid = "14a3606d-f60d-562e-9121-12d972cd8159"
84+
85+
[[NetworkOptions]]
86+
uuid = "ca575930-c2e3-43a9-ace4-1e988b2c1908"
9387

9488
[[Pkg]]
95-
deps = ["Dates", "LibGit2", "Libdl", "Logging", "Markdown", "Printf", "REPL", "Random", "SHA", "UUIDs"]
89+
deps = ["Artifacts", "Dates", "Downloads", "LibGit2", "Libdl", "Logging", "Markdown", "Printf", "REPL", "Random", "SHA", "Serialization", "TOML", "Tar", "UUIDs"]
9690
uuid = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
9791

9892
[[Printf]]
9993
deps = ["Unicode"]
10094
uuid = "de0858da-6303-5e67-8744-51eddeeeb8d7"
10195

10296
[[REPL]]
103-
deps = ["InteractiveUtils", "Markdown", "Sockets"]
97+
deps = ["InteractiveUtils", "Markdown", "Sockets", "Unicode"]
10498
uuid = "3fa0cd96-eef1-5676-8a61-b3b8758bbffb"
10599

106100
[[Random]]
@@ -113,24 +107,16 @@ uuid = "ea8e919c-243c-51af-8825-aaa63cd721ce"
113107
[[Serialization]]
114108
uuid = "9e88b42a-f829-5b0c-bbe9-9e923198166b"
115109

116-
[[SharedArrays]]
117-
deps = ["Distributed", "Mmap", "Random", "Serialization"]
118-
uuid = "1a1011a3-84de-559e-8e89-a11a2f7dc383"
119-
120110
[[Sockets]]
121111
uuid = "6462fe0b-24de-5631-8697-dd941f90decc"
122112

123-
[[SparseArrays]]
124-
deps = ["LinearAlgebra", "Random"]
125-
uuid = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
113+
[[TOML]]
114+
deps = ["Dates"]
115+
uuid = "fa267f1f-6049-4f14-aa54-33bafae1ed76"
126116

127-
[[Statistics]]
128-
deps = ["LinearAlgebra", "SparseArrays"]
129-
uuid = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
130-
131-
[[Test]]
132-
deps = ["Distributed", "InteractiveUtils", "Logging", "Random"]
133-
uuid = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
117+
[[Tar]]
118+
deps = ["ArgTools", "SHA"]
119+
uuid = "a4e569a6-e804-4fa4-b0f3-eef7a1d5b13e"
134120

135121
[[UUIDs]]
136122
deps = ["Random", "SHA"]
@@ -139,8 +125,14 @@ uuid = "cf7118a7-6976-5b1a-9a39-7adc72f591a4"
139125
[[Unicode]]
140126
uuid = "4ec0a83e-493e-50e2-b9ac-8f72acf5a8f5"
141127

128+
[[Zlib_jll]]
129+
deps = ["Libdl"]
130+
uuid = "83775a58-1f1d-513f-b197-d71354ab007a"
131+
142132
[[libLLVM_jll]]
143-
deps = ["Libdl", "Pkg"]
144-
git-tree-sha1 = "8bbcb2ac071438cd061ac210f6ba6754f45b8672"
133+
deps = ["Artifacts", "Libdl"]
145134
uuid = "8f36deef-c2a5-5394-99ed-8e07531fb29a"
146-
version = "9.0.1+5"
135+
136+
[[nghttp2_jll]]
137+
deps = ["Artifacts", "Libdl"]
138+
uuid = "8e850ede-7688-5339-a07c-302acd2aaf8d"

res/missing_defs.jl

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
@static if VERSION >= v"1.6.0"
2+
@static if Sys.islinux() && Sys.ARCH === :aarch64 && !occursin("musl", Base.BUILD_TRIPLET)
3+
const __off_t = Clong
4+
const off_t = __off_t
5+
elseif Sys.islinux() && Sys.ARCH === :aarch64 && occursin("musl", Base.BUILD_TRIPLET)
6+
const off_t = Clong
7+
elseif Sys.islinux() && startswith(string(Sys.ARCH), "arm") && !occursin("musl", Base.BUILD_TRIPLET)
8+
const __off_t = Clong
9+
const off_t = __off_t
10+
elseif Sys.islinux() && startswith(string(Sys.ARCH), "arm") && occursin("musl", Base.BUILD_TRIPLET)
11+
const off_t = Clonglong
12+
elseif Sys.islinux() && Sys.ARCH === :i686 && !occursin("musl", Base.BUILD_TRIPLET)
13+
const __off_t = Clong
14+
const off_t = __off_t
15+
elseif Sys.islinux() && Sys.ARCH === :i686 && occursin("musl", Base.BUILD_TRIPLET)
16+
const off_t = Clonglong
17+
elseif Sys.iswindows() && Sys.ARCH === :i686
18+
const off32_t = Clong
19+
const off_t = off32_t
20+
elseif Sys.islinux() && Sys.ARCH === :powerpc64le
21+
const __off_t = Clong
22+
const off_t = __off_t
23+
elseif Sys.isapple() && Sys.ARCH === :x86_64
24+
const __darwin_off_t = Int64
25+
const off_t = __darwin_off_t
26+
elseif Sys.islinux() && Sys.ARCH === :x86_64 && !occursin("musl", Base.BUILD_TRIPLET)
27+
const __off_t = Clong
28+
const off_t = __off_t
29+
elseif Sys.islinux() && Sys.ARCH === :x86_64 && occursin("musl", Base.BUILD_TRIPLET)
30+
const off_t = Clong
31+
elseif Sys.isbsd() && !Sys.isapple()
32+
const __off_t = Int64
33+
const off_t = __off_t
34+
elseif Sys.iswindows() && Sys.ARCH === :x86_64
35+
const off32_t = Clong
36+
const off_t = off32_t
37+
end
38+
else
39+
const off_t = Csize_t
40+
end

res/wrap.jl

Lines changed: 30 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -2,63 +2,49 @@
22

33
using LLVM_full_jll
44

5-
using Clang
5+
using Clang.Generators
6+
7+
cd(@__DIR__)
8+
9+
@add_def off_t
10+
11+
# replace `ccall` with `@runtime_ccall`
12+
function rewriter!(ctx)
13+
for node in get_nodes(ctx.dag)
14+
Generators.is_function(node) || continue
15+
if !Generators.is_variadic_function(node)
16+
expr = node.exprs[1]
17+
call_expr = expr.args[2].args[1]
18+
call_expr.head = :macrocall
19+
call_expr.args[1] = Symbol("@runtime_ccall")
20+
insert!(call_expr.args, 2, nothing)
21+
end
22+
end
23+
end
24+
25+
function main()
26+
options = load_options(joinpath(@__DIR__, "wrap.toml"))
627

7-
function wrap()
828
includedir = LLVM_full_jll.llvm_config() do config
929
readchomp(`$config --includedir`)
1030
end
1131
cppflags = LLVM_full_jll.llvm_config() do config
1232
split(readchomp(`$config --cppflags`))
1333
end
1434

15-
# Set-up arguments to clang
16-
clang_includes = map(x->x[3:end], filter( x->startswith(x,"-I"), cppflags))
17-
clang_extraargs = filter(x->!startswith(x,"-I"), cppflags)
35+
args = get_default_args("x86_64-linux-gnu")
36+
push!(args, "-I$includedir")
37+
append!(args, cppflags)
1838

19-
# FIXME: Clang.jl doesn't properly detect system headers
20-
push!(clang_extraargs, "-I/usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/include")
39+
header_files = detect_headers(joinpath(includedir, "llvm-c"), args)
2140

22-
# Recursively discover LLVM C API headers (files ending in .h)
23-
header_dirs = String[joinpath(includedir, "llvm-c")]
24-
header_files = String[]
25-
while !isempty(header_dirs)
26-
parent = pop!(header_dirs)
27-
children = readdir(parent)
28-
for child in children
29-
path = joinpath(parent, child)
30-
if isdir(path)
31-
push!(header_dirs, path)
32-
elseif isfile(path) && endswith(path, ".h")
33-
push!(header_files, path)
34-
end
35-
end
36-
end
41+
ctx = create_context(header_files, args, options)
3742

38-
context = init(;
39-
headers = header_files,
40-
output_file = "libLLVM_h.jl",
41-
common_file = "libLLVM_common.jl",
42-
clang_includes = convert(Vector{String}, clang_includes),
43-
clang_args = convert(Vector{String}, clang_extraargs),
44-
header_library = x->"libllvm",
45-
header_wrapped = (top,cursor)->occursin("include/llvm", cursor)
46-
)
43+
build!(ctx, BUILDSTAGE_NO_PRINTING)
4744

48-
run(context)
49-
end
45+
rewriter!(ctx)
5046

51-
function main()
52-
cd(joinpath(dirname(@__DIR__), "lib")) do
53-
wrap()
54-
end
47+
build!(ctx, BUILDSTAGE_PRINTING_ONLY)
5548
end
5649

5750
isinteractive() || main()
58-
59-
# Manual clean-up:
60-
# - remove build-host details (LLVM_DEFAULT_TARGET_TRIPLE etc) in libLLVM_common.jl
61-
# - remove "# Skipping ..." comments by Clang.jl
62-
# - replace `const (LLVMOpaque.*) = Cvoid` with `struct $1 end`
63-
# - use `gawk -i inplace '/^[[:blank:]]*$/ { print; next; }; {cur = seen[$0]; if(!seen[$0]++ || (/^end$/ && !prev) || /^.*Clang.*$/) print $0; prev=cur}' libLLVM_h.jl` to remove duplicates
64-
# - use `cat -s` to remove duplicate empty lines

res/wrap.toml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
[general]
2+
library_name = "libllvm[]"
3+
output_file_path = "../lib/libLLVM_h.jl"
4+
prologue_file_path = "./missing_defs.jl"
5+
epilogue_file_path = ""
6+
use_julia_native_enum_type = false
7+
use_deterministic_symbol = true
8+
printer_blacklist = [
9+
"LLVM_C_STRICT_PROTOTYPES_END",
10+
"LLVM_NATIVE_ARCH",
11+
"LLVMInitializeAllTargetInfos",
12+
"LLVMInitializeAllTargets",
13+
"LLVMInitializeAllTargetMCs",
14+
"LLVMInitializeAllAsmPrinters",
15+
"LLVMInitializeAllAsmParsers",
16+
"LLVMInitializeAllDisassemblers",
17+
"LLVMInitializeNativeTarget",
18+
"LLVMInitializeNativeAsmPrinter",
19+
"LLVMInitializeNativeAsmParser",
20+
"LLVMInitializeNativeDisassembler",
21+
"LLVM_C_EXTERN_C_BEGIN",
22+
"LLVM_C_EXTERN_C_END",
23+
"HUGE_VALF",
24+
]
25+
26+
[codegen]
27+
use_julia_bool = true
28+
always_NUL_terminated_string = true
29+
is_function_strictly_typed = false
30+
opaque_func_arg_as_PtrCvoid = false
31+
opaque_as_mutable_struct = true
32+
33+
[codegen.macro]
34+
macro_mode = "basic"
35+
add_comment_for_skipped_macro = false

src/LLVM.jl

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@ module API
1515
using CEnum
1616
using ..LLVM
1717
using ..LLVM: libllvm, @runtime_ccall
18-
const off_t = Csize_t
1918
libdir = joinpath(@__DIR__, "..", "lib")
20-
include(joinpath(libdir, "libLLVM_common.jl"))
2119
include(joinpath(libdir, "libLLVM_h.jl"))
2220
include(joinpath(libdir, "libLLVM_extra.jl"))
2321
end

src/core/function.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ struct FunctionAttrSet
3939
idx::API.LLVMAttributeIndex
4040
end
4141

42-
function_attributes(f::Function) = FunctionAttrSet(f, API.LLVMAttributeFunctionIndex)
42+
function_attributes(f::Function) = FunctionAttrSet(f, reinterpret(API.LLVMAttributeIndex, API.LLVMAttributeFunctionIndex))
4343
parameter_attributes(f::Function, idx::Integer) = FunctionAttrSet(f, API.LLVMAttributeIndex(idx))
4444
return_attributes(f::Function) = FunctionAttrSet(f, API.LLVMAttributeReturnIndex)
4545

0 commit comments

Comments
 (0)