Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate bindings for each target #7

Merged
merged 5 commits into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions gen/Manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ uuid = "b77e0a4c-d291-57a0-90e8-8db25a27a240"

[[deps.JLLPrefixes]]
deps = ["Artifacts", "Git", "HistoricalStdlibVersions", "Pkg", "Preferences", "SHA", "Scratch"]
git-tree-sha1 = "9343fd4bcb8a4f4f661325e353d70aa00be884c2"
git-tree-sha1 = "c43b83a688b86b33f5b6d8f48d70a41526d845f8"
uuid = "afc68a34-7891-4c5a-9da1-1c62935e7b0d"
version = "0.3.4"
version = "0.3.5"

[[deps.JLLWrappers]]
deps = ["Artifacts", "Preferences"]
Expand Down Expand Up @@ -137,9 +137,9 @@ version = "1.2.0"

[[deps.OpenSSL_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl"]
git-tree-sha1 = "60e3045590bd104a16fefb12836c00c0ef8c7f8c"
git-tree-sha1 = "3da7367955dcc5c54c1ba4d402ccdc09a1a3e046"
uuid = "458c3c95-2e84-50aa-8efc-19380b2a3a95"
version = "3.0.13+0"
version = "3.0.13+1"

[[deps.PCRE2_jll]]
deps = ["Artifacts", "Libdl"]
Expand Down
93 changes: 61 additions & 32 deletions gen/generator.jl
Original file line number Diff line number Diff line change
@@ -1,44 +1,73 @@
using Clang.Generators
using Clang.JLLEnvs
using JLLPrefixes
import aws_c_common_jll

cd(@__DIR__)

options = load_options(joinpath(@__DIR__, "generator.toml"))
options["general"]["output_file_path"] = joinpath(@__DIR__, "..", "src", "lib.jl")

metas = collect_artifact_metas(["aws_c_common_jll"])
metas_keys = collect(keys(metas))
function remove_itt_symbols!(dag::ExprDAG)
for i in eachindex(dag.nodes)
node = dag.nodes[i]
for expr in get_exprs(node)
node_name = if expr.head == :function
if expr.args[1].args[1] isa Expr # function is Module.name instead of just name
expr.args[1].args[1].args[2]
else
expr.args[1].args[1]
end
elseif expr.head == :struct
if expr.args[2] isa Expr # struct has type parameter
expr.args[2].args[1]
else
expr.args[2]
end
elseif expr.head == :const
expr.args[1].args[1]
end
# remove the node by renaming it to IGNORED, which we include in the generator's ignorelist
if contains(lowercase(string(node_name)), "itt")
dag.nodes[i] = ExprNode(:IGNORED, node.type, node.cursor, node.exprs, node.premature_exprs, node.adj)
end
end
end
return nothing
end

jll_include_dirs = Dict(
"aws_c_common_jll" => ["aws/common"],
)
for target in JLLEnvs.JLL_ENV_TRIPLES
if target == "i686-w64-mingw32"
# aws_c_common_jll does not support i686 windows https://github.com/JuliaPackaging/Yggdrasil/blob/bbab3a916ae5543902b025a4a873cf9ee4a7de68/A/aws_c_common/build_tarballs.jl#L48-L49
continue
end
options = load_options(joinpath(@__DIR__, "generator.toml"))
options["general"]["output_file_path"] = joinpath(@__DIR__, "..", "lib", "$target.jl")

# add compiler flags, e.g. "-DXXXXXXXXX"
args = get_default_args() # Note you must call this function firstly and then append your own flags
header_dirs = String[]
header_dirs = []
args = get_default_args(target)
inc = JLLEnvs.get_pkg_include_dir(aws_c_common_jll, target)
push!(args, "-I$inc")
push!(header_dirs, inc)

for (jll_name, include_dirs) in collect(jll_include_dirs)
meta_key = metas_keys[findfirst(it -> it.name == jll_name, metas_keys)]
meta = metas[meta_key]
if length(meta["paths"]) != 1
error("not sure what to do with these paths", meta)
end
path = meta["paths"][1]
push!(args, "-I$(joinpath(path, "include"))")
for dir in include_dirs
push!(header_dirs, joinpath(path, "include", dir))
headers = String[]
for header_dir in header_dirs
for (root, dirs, files) in walkdir(header_dir)
for file in files
if endswith(file, ".h")
push!(headers, joinpath(root, file))
end
end
end
end
end
unique!(headers)

headers = String[]
for header_dir in header_dirs, file in readdir(header_dir, join=true)
if endswith(file, ".h")
push!(headers, file)
end
end
ctx = create_context(headers, args, options)

# create context
ctx = create_context(headers, args, options)
# build without printing so we can do custom rewriting
build!(ctx, BUILDSTAGE_NO_PRINTING)

# run generator
build!(ctx)
# the ITT symbols are just for aws-c-common's profiling stuff, we don't need to generate them and they cause
# problems with the generated code
remove_itt_symbols!(ctx.dag)

# print
build!(ctx, BUILDSTAGE_PRINTING_ONLY)
end
2 changes: 2 additions & 0 deletions gen/generator.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ output_ignorelist = [
"AWS_COMMON_MATH_API",
"AWS_THREAD_ONCE_STATIC_INIT",
"AWS_BUS_ADDRESS_ALL",
"IGNORED",
"STDCALL",
]

auto_mutability = false
Expand Down
Loading
Loading