From b69e1094c00ca3187001987af05b0bfa0a7deb32 Mon Sep 17 00:00:00 2001 From: Michael Schlottke-Lakemper Date: Sat, 6 Mar 2021 13:25:34 +0100 Subject: [PATCH] Add deps/ folder with build script --- deps/.gitignore | 1 + deps/Project.toml | 7 +++ deps/build.jl | 87 +++++++++++++++++++++++++++++ deps/include_override/algorithm | 0 deps/include_override/cstddef | 2 + deps/include_override/helper.h | 6 ++ deps/include_override/map | 0 deps/include_override/memory | 0 deps/include_override/string | 0 deps/include_override/unordered_map | 0 deps/include_override/vector | 0 11 files changed, 103 insertions(+) create mode 100644 deps/.gitignore create mode 100644 deps/Project.toml create mode 100644 deps/build.jl create mode 100644 deps/include_override/algorithm create mode 100644 deps/include_override/cstddef create mode 100644 deps/include_override/helper.h create mode 100644 deps/include_override/map create mode 100644 deps/include_override/memory create mode 100644 deps/include_override/string create mode 100644 deps/include_override/unordered_map create mode 100644 deps/include_override/vector diff --git a/deps/.gitignore b/deps/.gitignore new file mode 100644 index 0000000..faac91a --- /dev/null +++ b/deps/.gitignore @@ -0,0 +1 @@ +libsealc.jl diff --git a/deps/Project.toml b/deps/Project.toml new file mode 100644 index 0000000..9829471 --- /dev/null +++ b/deps/Project.toml @@ -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" diff --git a/deps/build.jl b/deps/build.jl new file mode 100644 index 0000000..4ad2783 --- /dev/null +++ b/deps/build.jl @@ -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 diff --git a/deps/include_override/algorithm b/deps/include_override/algorithm new file mode 100644 index 0000000..e69de29 diff --git a/deps/include_override/cstddef b/deps/include_override/cstddef new file mode 100644 index 0000000..27113b1 --- /dev/null +++ b/deps/include_override/cstddef @@ -0,0 +1,2 @@ +// Just include stddef.h since we do not need anything C++-specific and `cstddef` is not a C header +#include diff --git a/deps/include_override/helper.h b/deps/include_override/helper.h new file mode 100644 index 0000000..52b8018 --- /dev/null +++ b/deps/include_override/helper.h @@ -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 diff --git a/deps/include_override/map b/deps/include_override/map new file mode 100644 index 0000000..e69de29 diff --git a/deps/include_override/memory b/deps/include_override/memory new file mode 100644 index 0000000..e69de29 diff --git a/deps/include_override/string b/deps/include_override/string new file mode 100644 index 0000000..e69de29 diff --git a/deps/include_override/unordered_map b/deps/include_override/unordered_map new file mode 100644 index 0000000..e69de29 diff --git a/deps/include_override/vector b/deps/include_override/vector new file mode 100644 index 0000000..e69de29