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

Upgrade generator script to use the latest Clang.jl #102

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
5 changes: 5 additions & 0 deletions .ci/change-uuid.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
using UUIDs

project_filename = joinpath(dirname(@__DIR__), "Project.toml")

write("Project.toml", replace(read("Project.toml", String), r"uuid = .*?\n" =>"uuid = \"$(uuid4())\"\n"))
7 changes: 3 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,10 @@ jobs:
${{ runner.os }}-test-${{ env.cache-name }}-
${{ runner.os }}-test-
${{ runner.os }}-
- run: julia --color=yes test/libcurl_uuid.jl
- run: julia --color=yes .ci/change-uuid.jl
- uses: julia-actions/julia-buildpkg@v1
- name: Run tests
run: |
julia --project --color=yes -e 'using UUIDs; write("Project.toml", replace(read("Project.toml", String), r"uuid = .*?\n" =>"uuid = \"$(uuid4())\"\n"));'
julia --project --color=yes --check-bounds=yes -e 'import Pkg; Pkg.build(); Pkg.test(; coverage=true)'
- uses: julia-actions/julia-runtest@v1
- uses: julia-actions/julia-processcoverage@v1
- uses: codecov/codecov-action@v1
with:
Expand Down
145 changes: 145 additions & 0 deletions gen/Manifest.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
# This file is machine-generated - editing it directly is not advised

[[ArgTools]]
uuid = "0dad84c5-d112-42e6-8d28-ef12dabb789f"

[[Artifacts]]
uuid = "56f22d72-fd6d-98f1-02f0-08ddc0907c33"

[[Base64]]
uuid = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"

[[CEnum]]
git-tree-sha1 = "215a9aa4a1f23fbd05b92769fdd62559488d70e9"
uuid = "fa961155-64e5-5f13-b03f-caf6b980ea82"
version = "0.4.1"

[[Clang]]
deps = ["CEnum", "Clang_jll", "Downloads", "Pkg", "TOML"]
git-tree-sha1 = "93b424759f11b7dbd300eb424fa1bfe590e15cf6"
repo-rev = "master"
repo-url = "https://github.com/JuliaInterop/Clang.jl.git"
uuid = "40e3b903-d033-50b4-a0cc-940c62c95e31"
version = "0.14.0"

[[Clang_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg", "libLLVM_jll"]
git-tree-sha1 = "a5923c06de3178dd755f4b9411ea8922a7ae6fb8"
uuid = "0ee61d77-7f21-5576-8119-9fcc46b10100"
version = "11.0.1+3"

[[Dates]]
deps = ["Printf"]
uuid = "ade2ca70-3891-5945-98fb-dc099432e06a"

[[Downloads]]
deps = ["ArgTools", "LibCURL", "NetworkOptions"]
uuid = "f43a241f-c20a-4ad4-852c-f6b1247861c6"

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

[[JLLWrappers]]
deps = ["Preferences"]
git-tree-sha1 = "642a199af8b68253517b80bd3bfd17eb4e84df6e"
uuid = "692b3bcd-3c85-4b1f-b108-f13ce0eb3210"
version = "1.3.0"

[[LibCURL]]
deps = ["LibCURL_jll", "MozillaCACerts_jll"]
uuid = "b27032c2-a3e7-50c8-80cd-2d36dbcbfd21"

[[LibCURL_jll]]
deps = ["Artifacts", "LibSSH2_jll", "Libdl", "MbedTLS_jll", "Zlib_jll", "nghttp2_jll"]
uuid = "deac9b47-8bc7-5906-a0fe-35ac56dc84c0"

[[LibGit2]]
deps = ["Base64", "NetworkOptions", "Printf", "SHA"]
uuid = "76f85450-5226-5b5a-8eaa-529ad045b433"

[[LibSSH2_jll]]
deps = ["Artifacts", "Libdl", "MbedTLS_jll"]
uuid = "29816b5a-b9ab-546f-933c-edad1886dfa8"

[[Libdl]]
uuid = "8f399da3-3557-5675-b5ff-fb832c97cbdb"

[[Logging]]
uuid = "56ddb016-857b-54e1-b83d-db4d58db5568"

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

[[MbedTLS_jll]]
deps = ["Artifacts", "Libdl"]
uuid = "c8ffd9c3-330d-5841-b78e-0817d7145fa1"

[[MozillaCACerts_jll]]
uuid = "14a3606d-f60d-562e-9121-12d972cd8159"

[[NetworkOptions]]
uuid = "ca575930-c2e3-43a9-ace4-1e988b2c1908"

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

[[Preferences]]
deps = ["TOML"]
git-tree-sha1 = "00cfd92944ca9c760982747e9a1d0d5d86ab1e5a"
uuid = "21216c6a-2e73-6563-6e65-726566657250"
version = "1.2.2"

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

[[REPL]]
deps = ["InteractiveUtils", "Markdown", "Sockets", "Unicode"]
uuid = "3fa0cd96-eef1-5676-8a61-b3b8758bbffb"

[[Random]]
deps = ["Serialization"]
uuid = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"

[[SHA]]
uuid = "ea8e919c-243c-51af-8825-aaa63cd721ce"

[[Serialization]]
uuid = "9e88b42a-f829-5b0c-bbe9-9e923198166b"

[[Sockets]]
uuid = "6462fe0b-24de-5631-8697-dd941f90decc"

[[TOML]]
deps = ["Dates"]
uuid = "fa267f1f-6049-4f14-aa54-33bafae1ed76"

[[Tar]]
deps = ["ArgTools", "SHA"]
uuid = "a4e569a6-e804-4fa4-b0f3-eef7a1d5b13e"

[[UUIDs]]
deps = ["Random", "SHA"]
uuid = "cf7118a7-6976-5b1a-9a39-7adc72f591a4"

[[Unicode]]
uuid = "4ec0a83e-493e-50e2-b9ac-8f72acf5a8f5"

[[Zlib_jll]]
deps = ["Libdl"]
uuid = "83775a58-1f1d-513f-b197-d71354ab007a"

[[libLLVM_jll]]
deps = ["Artifacts", "Libdl"]
uuid = "8f36deef-c2a5-5394-99ed-8e07531fb29a"

[[nghttp2_jll]]
deps = ["Artifacts", "Libdl"]
uuid = "8e850ede-7688-5339-a07c-302acd2aaf8d"

[[p7zip_jll]]
deps = ["Artifacts", "Libdl"]
uuid = "3f19e933-33d8-53b3-aaab-bd5110c3b7a0"
9 changes: 9 additions & 0 deletions gen/Project.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[deps]
Clang = "40e3b903-d033-50b4-a0cc-940c62c95e31"
Gnimuc marked this conversation as resolved.
Show resolved Hide resolved
LibCURL_jll = "deac9b47-8bc7-5906-a0fe-35ac56dc84c0"
MozillaCACerts_jll = "14a3606d-f60d-562e-9121-12d972cd8159"

[compat]
Clang = "0.14"
LibCURL_jll = "7.66"
MozillaCACerts_jll = ">= 2020"
77 changes: 23 additions & 54 deletions gen/generate.jl
Original file line number Diff line number Diff line change
@@ -1,66 +1,35 @@
using Clang.cindex
using Clang.wrap_c
using Printf
using Pkg
using Pkg.Artifacts
using Clang.Generators
using Clang.Generators.JLLEnvs
using LibCURL_jll

# Set these to correspond to your local filesystem's curl and clang include paths
const CURL_PATH = "/usr/local/opt/curl/include/curl"
const CLANG_INCLUDES = [
"/usr/local/opt/llvm/include",
]
cd(@__DIR__)

const SRC_DIR = abspath(@__DIR__, "..", "src")
artifact_toml = joinpath(dirname(Pkg.pathof(LibCURL_jll)), "..", "StdlibArtifacts.toml")
omus marked this conversation as resolved.
Show resolved Hide resolved
Gnimuc marked this conversation as resolved.
Show resolved Hide resolved
artifact_dir = Pkg.Artifacts.ensure_artifact_installed("LibCURL", artifact_toml)

headers = map(x -> joinpath(CURL_PATH, x), ["curl.h", "easy.h", "multi.h"])
context = wrap_c.init(;
headers = headers,
clang_args = String[],
common_file = joinpath(SRC_DIR, "lC_common_h.jl"),
clang_includes = CLANG_INCLUDES,
clang_diagnostics = true,
header_wrapped = (top_header, cursor_header) -> in(cursor_header, headers),
header_library = header -> "libcurl",
header_outputfile = header -> joinpath(SRC_DIR, "lC_") * replace(basename(header), "." => "_") * ".jl",
)
context.options.wrap_structs = true
include_dir = joinpath(artifact_dir, "include") |> normpath
curl_h = joinpath(include_dir, "curl", "curl.h")
@assert isfile(curl_h)

begin
context.headers = [joinpath(CURL_PATH, "curl.h")]
run(context)
end

function write_constants(filename::AbstractString, startswith_identifier::AbstractString, exports_file)
open(filename, "r") do file
lines = split(read(file, String), "\n")
# mprintf_h = joinpath(include_dir, "curl", "mprintf.h")
# stdcheaders_h = joinpath(include_dir, "curl", "stdcheaders.h")

for line in lines
if startswith(line, startswith_identifier)
@printf exports_file "export %s\n" split(line, r" |\(")[2]
end
end
end
end
options = load_options(joinpath(@__DIR__, "generator.toml"))

# Generate export statements
open(joinpath(SRC_DIR, "lC_exports_h.jl"), "w+") do exports_file
println(exports_file, "# Generating exports")
for target in JLLEnvs.JLL_ENV_TRIPLES
@info "processing $target"

write_constants(joinpath(SRC_DIR, "lC_curl_h.jl"), "function", exports_file)
write_constants(joinpath(SRC_DIR, "lC_common_h.jl"), "const", exports_file)
options["general"]["output_file_path"] = joinpath(@__DIR__, "..", "lib", "$target.jl")

# Generate define constants
open(joinpath(SRC_DIR, "lC_defines_h.jl"), "w+") do defines_file
println(defines_file, "# Generating #define constants")
args = get_default_args(target)
push!(args, "-I$include_dir")

hashdefs = split(read(`gcc -E -dD -P $(joinpath(CURL_PATH, "curl.h"))`, String), "\n")
# header_files = detect_headers(include_dir, args)
header_files = [curl_h]

for line in hashdefs
m = match(r"^\s*#define\s+CURL(\w+)\s+(.+)", line)
ctx = create_context(header_files, args, options)

if m !== nothing
c2 = replace(m.captures[2], "(unsigned long)" => "")
@printf defines_file "const CURL%-30s = %s\n" m.captures[1] c2
@printf exports_file "export CURL%s\n" m.captures[1]
end
end
end
build!(ctx)
end
14 changes: 14 additions & 0 deletions gen/generator.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[general]
library_name = "libcurl"
use_julia_native_enum_type = true
use_deterministic_symbol = true
auto_mutability = true

printer_blacklist = [
"CURL_SUFFIX_CURL_OFF_T",
"CURL_SUFFIX_CURL_OFF_TU",
"CURL_ZERO_TERMINATED",
]

[codegen.macro]
macro_mode = "basic"
Loading