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

Use CBindingGen to conveniently generate bindings to the SEAL C functions #20

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions deps/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
libsealc.jl
7 changes: 7 additions & 0 deletions deps/Project.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[deps]
CBindingGen = "308a6e0c-0495-45e1-b1ab-67fb455a0d77"
Libdl = "8f399da3-3557-5675-b5ff-fb832c97cbdb"
SEAL_jll = "cdf55027-1209-57fc-8789-28745714751d"

[compat]
CBindingGen = "0.4"
87 changes: 87 additions & 0 deletions deps/build.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
using CBindingGen
using Libdl
import SEAL_jll

# Generate C bindings
@info "Generate C bindings..."

# Determine library path
seal_path = SEAL_jll.get_libsealc_path() |> dirname |> dirname
@info "SEAL path" seal_path

# For the actual include directory we need to first find the version-specific directory
seal_include = joinpath(seal_path, "include")
function get_version_include(include_dir)
version_include = ""
for dir in readdir(seal_include)
if startswith(dir, "SEAL-")
version_include = dir
break
end
end
return version_include
end
version_include = get_version_include(seal_include)
if isempty(version_include)
error("could not find proper include directory path in $seal_include")
end
seal_include = joinpath(seal_include, version_include)
seal_include_c = joinpath(seal_include, "seal", "c")
include_override = joinpath(@__DIR__, "include_override")
@info "SEAL include directory" seal_include
@info "SEAL C include directory" seal_include_c
@info "include override directory" include_override

# Find header files to consider
# headers = String["helper.h"]
headers = String[]
skip_headers = ["targetver.h"]
for filename in readdir(seal_include_c)
if !isfile(joinpath(seal_include_c, filename))
continue
end
if !endswith(filename, ".h")
continue
end
if filename in skip_headers
continue
end
push!(headers, joinpath("seal", "c", filename))
end
@info "Header files" headers

# Build list of arguments for Clang
clang_args = String[]
include_directories = [seal_include]
# include_directories = [seal_include, include_override]
for dir in include_directories
append!(clang_args, ("-I", dir))
end
append!(clang_args, ("-x", "c++", "-std=c++17"))
# append!(clang_args, ("-Dstatic_assert(a...)=", "-Wno-macro-redefined"))
@info "Clang args" clang_args

# Convert symbols in header
@info "Convert symbols in headers to Julia expressions..."
cvts = convert_headers(headers, args=clang_args) do cursor
header = CodeLocation(cursor).file
name = string(cursor)

# only wrap the SEAL headers
dirname, filename = splitdir(header)
qualified_filename = joinpath("seal", "c", filename)
if !(qualified_filename in headers)
return false
end

@info "converting " qualified_filename

return true
end

# Write generated C bindings to file
@info "Write generated C bindings to file..."
const bindings_filename = joinpath(@__DIR__, "libsealc.jl")
open(bindings_filename, "w+") do io
generate(io, SEAL_jll.get_libsealc_path() => cvts)
end
Empty file added deps/include_override/algorithm
Empty file.
2 changes: 2 additions & 0 deletions deps/include_override/cstddef
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Just include stddef.h since we do not need anything C++-specific and `cstddef` is not a C header
#include <stddef.h>
6 changes: 6 additions & 0 deletions deps/include_override/helper.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Include stdbool.h to introduce `bool`
#include "stdbool.h"

// Include defines.h and then redefine the SEAL_C_FUNC macro to remove the `extern "C"`
#include "seal/c/defines.h"
#define SEAL_C_FUNC HRESULT
Empty file added deps/include_override/map
Empty file.
Empty file added deps/include_override/memory
Empty file.
Empty file added deps/include_override/string
Empty file.
Empty file.
Empty file added deps/include_override/vector
Empty file.