Skip to content

Commit

Permalink
fix build, regen bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
samansmink committed Sep 16, 2024
1 parent 54e2618 commit 7621ce5
Show file tree
Hide file tree
Showing 10 changed files with 39 additions and 47 deletions.
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,9 @@ all:

test:
cargo test --features bundled --features modern-full -- --nocapture

# Build the rust-based CAPI extension demo, change the target arch below to
EXAMPLE_TARGET_ARCH = osx_arm64
examples-capi-demo:
cargo build --example hello-ext-capi --features="vtab-loadable,loadable_extension" --release
python3 scripts/append_extension_metadata.py -l target/release/examples/libhello_ext_capi.dylib -n rusty_quack -dv v0.0.1 -ev v0.0.1 -p $EXAMPLE_TARGET_ARCH
30 changes: 0 additions & 30 deletions README_C_EXT_API_POC.md

This file was deleted.

12 changes: 10 additions & 2 deletions crates/duckdb-loadable-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ use syn::ItemFn;
#[derive(Debug, FromMeta)]
struct CEntryPointMacroArgs {
#[darling(default)]
/// The name to be given to this extension. This name is used in the entrypoint function called by duckdb
ext_name: String,
/// The minimum C API version this extension requires. It is recommended to set this to the lowest possible version
/// at which your extension still compiles
min_duckdb_version: Option<String>,
}

Expand All @@ -34,7 +37,7 @@ pub fn duckdb_entrypoint_c_api(attr: TokenStream, item: TokenStream) -> TokenStr
}
};

/// TODO FETCH THE DEFAULT AUTOMATICALLY SOMEHOW
// Set the minimum duckdb version (dev by default)
let minimum_duckdb_version = match args.min_duckdb_version {
Some(i) => i,
None => "dev".to_string(),
Expand All @@ -55,7 +58,12 @@ pub fn duckdb_entrypoint_c_api(attr: TokenStream, item: TokenStream) -> TokenStr
/// Will be called by duckdb
#[no_mangle]
pub unsafe extern "C" fn #c_entrypoint(info: ffi::duckdb_extension_info, access: ffi::duckdb_extension_access) {
ffi::duckdb_rs_extension_api_init(info, access).expect("Failed to initialize DuckDB C Extension API");
let res = ffi::duckdb_rs_extension_api_init(info, access, #minimum_duckdb_version);

if let Err(x) = res {
// Error will be handled by DuckDB
return;
}

let db : ffi::duckdb_database = *access.get_database.unwrap()(info);
let connection = Connection::open_from_raw(db.cast()).expect("can't open db connection");
Expand Down
2 changes: 1 addition & 1 deletion crates/duckdb/examples/hello-ext-capi/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ impl VTab for HelloVTab {
}
}

#[duckdb_entrypoint_c_api(ext_name = "rusty_quack", min_duckdb_version = "v0.0.2")]
#[duckdb_entrypoint_c_api(ext_name = "rusty_quack", min_duckdb_version = "v0.0.1")]
pub unsafe fn ExtensionEntrypoint(con: Connection) -> Result<(), Box<dyn Error>> {
con.register_table_function::<HelloVTab>("hello")
.expect("Failed to register hello table function");
Expand Down
2 changes: 1 addition & 1 deletion crates/libduckdb-sys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ buildtime_bindgen = ["bindgen", "pkg-config", "vcpkg"]
json = ["bundled"]
parquet = ["bundled"]
extensions-full = ["json", "parquet"]
loadable_extension = ["prettyplease", "quote", "syn", "buildtime_bindgen"]
loadable_extension = ["prettyplease", "quote", "syn"]

[dependencies]

Expand Down
27 changes: 16 additions & 11 deletions crates/libduckdb-sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,14 +228,19 @@ mod build_linked {
#[allow(unused_variables)]
let header = find_duckdb();

if !cfg!(feature = "buildtime_bindgen") {
std::fs::copy("src/bindgen_bundled_version.rs", out_path)
.expect("Could not copy bindings to output directory");
} else {
#[cfg(feature = "buildtime_bindgen")]
{
bindings::write_to_out_dir(header, out_path);
}
#[cfg(not(feature = "buildtime_bindgen"))]
{
std::fs::copy(
#[cfg(not(feature = "loadable_extension"))]
"src/bindgen_bundled_version.rs",
#[cfg(feature = "loadable_extension")]
"src/bindgen_bundled_version_loadable.rs",
out_path).expect("Could not copy bindings to output directory");
}

#[cfg(feature = "buildtime_bindgen")]
{
bindings::write_to_out_dir(header, out_path);
}
}

Expand Down Expand Up @@ -428,9 +433,9 @@ mod bindings {
// (3) generate rust code similar to DUCKDB_EXTENSION_API_INIT macro
let tokens = quote::quote! {
/// Like DUCKDB_EXTENSION_API_INIT macro
pub unsafe fn duckdb_rs_extension_api_init(info: duckdb_extension_info, access: duckdb_extension_access) -> ::std::result::Result<(), &'static str> {
// TODO: fix this shit
let #p_api = access.get_api.unwrap()(info, c"v0.0.2".as_ptr()) as *const duckdb_ext_api_v0;
pub unsafe fn duckdb_rs_extension_api_init(info: duckdb_extension_info, access: duckdb_extension_access, version: &str) -> ::std::result::Result<(), &'static str> {
let version_c_string = std::ffi::CString::new(version).unwrap();
let #p_api = access.get_api.unwrap()(info, version_c_string.as_ptr()) as *const duckdb_ext_api_v0;
if #p_api.is_null() {
return Err("DuckDB passed a nullpointer while trying to initialize the extension");
}
Expand Down
2 changes: 1 addition & 1 deletion crates/libduckdb-sys/duckdb-sources
Submodule duckdb-sources updated 384 files
Binary file modified crates/libduckdb-sys/duckdb.tar.gz
Binary file not shown.
4 changes: 3 additions & 1 deletion crates/libduckdb-sys/src/bindgen_bundled_version_loadable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14025,8 +14025,10 @@ pub unsafe fn duckdb_stream_fetch_chunk(result: duckdb_result) -> duckdb_data_ch
pub unsafe fn duckdb_rs_extension_api_init(
info: duckdb_extension_info,
access: duckdb_extension_access,
version: &str,
) -> ::std::result::Result<(), &'static str> {
let p_api = access.get_api.unwrap()(info, c"v0.0.2".as_ptr())
let version_c_string = std::ffi::CString::new(version).unwrap();
let p_api = access.get_api.unwrap()(info, version_c_string.as_ptr())
as *const duckdb_ext_api_v0;
if p_api.is_null() {
return Err(
Expand Down
1 change: 1 addition & 0 deletions crates/libduckdb-sys/wrapper_ext.h
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
// FIXME: remove this once C EXTENSION API is stable (expected for DuckDB v1.2.0 release)
#define DUCKDB_EXTENSION_API_VERSION_DEV 1
#include "duckdb/duckdb_extension.h"

0 comments on commit 7621ce5

Please sign in to comment.