Skip to content

Commit

Permalink
Merge pull request #42 from Gnimuc/regenerate
Browse files Browse the repository at this point in the history
Test Clang.jl's new generator
  • Loading branch information
Gnimuc authored Apr 2, 2021
2 parents a61a512 + 6451bfd commit b990620
Show file tree
Hide file tree
Showing 10 changed files with 6,873 additions and 6,795 deletions.
2 changes: 2 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ authors = ["Yupei Qi <qiyupei@gmail.com>"]
version = "1.79.0"

[deps]
CEnum = "fa961155-64e5-5f13-b03f-caf6b980ea82"
CImGui_jll = "7dd61d3b-0da5-5c94-bbf9-a0296c6e3925"
CSyntax = "ea656a56-6ca6-5dda-bba5-7b6963a5f74c"
GLFW = "f7f18e0c-5ee9-5ccd-a5bf-e8befd85ed98"
Libdl = "8f399da3-3557-5675-b5ff-fb832c97cbdb"
ModernGL = "66fc600b-dfda-50eb-8b99-91cfa97b1301"

[compat]
CEnum = "0.2,0.3,0.4"
CImGui_jll = "~1.79.0"
CSyntax = "0.4.0"
GLFW = "3.2.0,3.3.0,3.4.0"
Expand Down
File renamed without changes.
45 changes: 0 additions & 45 deletions gen/gen_helper.jl

This file was deleted.

72 changes: 22 additions & 50 deletions gen/generator.jl
Original file line number Diff line number Diff line change
@@ -1,56 +1,28 @@
using Clang
using Clang.Generators
using CImGui.LibCImGui.CImGui_jll

const CIMGUI_H = joinpath(dirname(CImGui_jll.libcimgui_path), "..", "include", "cimgui.h") |> normpath

# create a work context
ctx = DefaultContext()

# parse headers
parse_headers!(ctx, [CIMGUI_H], args=[map(x->"-I"*x, find_std_headers())..., "-DCIMGUI_DEFINE_ENUMS_AND_STRUCTS"], includes=[LLVM_INCLUDE])

# settings
ctx.libname = "libcimgui"
ctx.options["is_function_strictly_typed"] = false
ctx.options["is_struct_mutable"] = false

# write output
api_file = joinpath(@__DIR__, "libcimgui_api.jl")
api_stream = open(api_file, "w")

for trans_unit in ctx.trans_units
root_cursor = getcursor(trans_unit)
push!(ctx.cursor_stack, root_cursor)
header = spelling(root_cursor)
@info "wrapping header: $header ..."
# loop over all of the child cursors and wrap them, if appropriate.
ctx.children = children(root_cursor)
for (i, child) in enumerate(ctx.children)
child_name = name(child)
child_header = filename(child)
ctx.children_index = i
# choose which cursor to wrap
startswith(child_name, "__") && continue # skip compiler definitions
child_name in keys(ctx.common_buffer) && continue # already wrapped
child_header != header && continue # skip if cursor filename is not in the headers to be wrapped

wrap!(ctx, child)
end
@info "writing $(api_file)"
println(api_stream, "# Julia wrapper for header: $(basename(header))")
println(api_stream, "# Automatically generated using Clang.jl\n")
print_buffer(api_stream, ctx.api_buffer)
empty!(ctx.api_buffer) # clean up api_buffer for the next header
end
close(api_stream)
include_dir = joinpath(CImGui_jll.artifact_dir, "include")

const CIMGUI_H = joinpath(include_dir, "cimgui.h") |> normpath
const HELPER_H = joinpath(include_dir, "helper.h") |> normpath

options = load_options(joinpath(@__DIR__, "libcimgui.toml"))

# write "common" definitions: types, typealiases, etc.
common_file = joinpath(@__DIR__, "libcimgui_common.jl")
open(common_file, "w") do f
println(f, "# Automatically generated using Clang.jl\n")
print_buffer(f, dump_to_buffer(ctx.common_buffer))
args = ["-I$include_dir", "-DCIMGUI_DEFINE_ENUMS_AND_STRUCTS"]

ctx = create_context([CIMGUI_H, HELPER_H], args, options)

build!(ctx, BUILDSTAGE_NO_PRINTING)

for node in get_nodes(ctx.dag)
file = get_filename(node.cursor)
file == HELPER_H || continue
Generators.is_function(node) || continue
if !Generators.is_variadic_function(node)
expr = node.exprs[1]
expr.args[2].args[1].args[2].args[2] = :libcimgui_helper
end
end

# uncomment the following code to generate dependency and template files
# copydeps(dirname(api_file))
# print_template(joinpath(dirname(api_file), "LibTemplate.jl"))
build!(ctx, BUILDSTAGE_PRINTING_ONLY)
9 changes: 9 additions & 0 deletions gen/libcimgui.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[general]
library_name = "libcimgui"
output_file_path = "./src/LibCImGui.jl"
module_name = "LibCImGui"
jll_pkg_name = "CImGui_jll"
export_symbol_prefixes = ["ig", "Im"]
prologue_file_path = ""
epilogue_file_path = "./gen/epilogue.jl"

Loading

0 comments on commit b990620

Please sign in to comment.