Advertisement
Guest User

Untitled

a guest
Sep 10th, 2019
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.70 KB | None | 0 0
  1.  
  2. using Clang
  3.  
  4. ## we assume a working directory of "/tmp/st", where we execute the following script
  5. ## furthermore, we assume only 1 header file in that directory:
  6. ##
  7. ## "https://github.com/luigirizzo/netmap/blob/master/sys/net/netmap_user.h"
  8. ##
  9. ## Note that if you use the file "netmap_user.h" as is,
  10. ## several definitions are *NOT* generated as wrappers (e.g., "nm_desc" struct is missing, several functions)
  11. ## But if you delete all the "#ifdefs", the output is correct
  12. ##
  13. ## I have netmap installed on my machine, don't know if you need that to run this code
  14. ## (e.g., netmap_user.h includes netmap.h (which is part of the installation))
  15. const LIBCLANG_INCLUDE = "/tmp/st" ## copy only the file "netmap_user.h" into this dir
  16. const LIBCLANG_HEADERS = [joinpath(LIBCLANG_INCLUDE, header) for header in readdir(LIBCLANG_INCLUDE) if endswith(header, ".h")]
  17.  
  18. ctx = DefaultContext()
  19.  
  20. parse_headers!(ctx,
  21. LIBCLANG_HEADERS,
  22. args=["-I", joinpath(LIBCLANG_INCLUDE, "..")],
  23. includes=vcat(LIBCLANG_INCLUDE, CLANG_INCLUDE),
  24. )
  25.  
  26. ctx.libname = "libnetmap_user"
  27. ctx.options["is_function_strictly_typed"] = false
  28. ctx.options["is_struct_mutable"] = false
  29.  
  30. api_file = joinpath(@__DIR__, "libnetmap_user_api.jl")
  31. api_stream = open(api_file, "w")
  32.  
  33. ## NOTE: we have only on ".h" header file in "/tmp/st", so net line works(!)
  34. trans_unit = first(ctx.trans_units)
  35. root_cursor = getcursor(trans_unit)
  36. push!(ctx.cursor_stack, root_cursor)
  37. header = spelling(root_cursor)
  38. @info "wrapping header: $header ..."
  39.  
  40. ctx.children = children(root_cursor)
  41. for (i, child) in enumerate(ctx.children)
  42. child_name = name(child)
  43. child_header = filename(child)
  44. ctx.children_index = i
  45.  
  46. startswith(child_name, "__") && continue # skip compiler definitions
  47. child_name in keys(ctx.common_buffer) && continue # already wrapped
  48. child_header != header && continue # skip if cursor filename is not in the headers to be wrapped
  49.  
  50. wrap!(ctx, child)
  51. end
  52. @info "writing $(api_file)"
  53. println(api_stream, "# Julia wrapper for header: $(basename(header))")
  54. println(api_stream, "# Automatically generated using Clang.jl\n")
  55. print_buffer(api_stream, ctx.api_buffer)
  56. empty!(ctx.api_buffer) # clean up api_buffer for the next header
  57.  
  58. close(api_stream)
  59.  
  60. common_file = joinpath(@__DIR__, "netmap_user_common.jl")
  61. open(common_file, "w") do f
  62. println(f, "# Automatically generated using Clang.jl\n")
  63. print_buffer(f, dump_to_buffer(ctx.common_buffer))
  64. end
  65.  
  66. ## NOTE:
  67. ## The resulting files should be about this big:
  68. ## 2,4K Sep 10 08:55 libnetmap_user_api.jl
  69. ## 1,9K Sep 10 08:55 netmap_user_common.jl
  70. ## If you got smaller files, there might be a bug!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement