Skip to content

Commit

Permalink
Merge pull request #1 from 0xcaff/martin/sam-bundled-build
Browse files Browse the repository at this point in the history
setup bundled build
  • Loading branch information
samansmink authored Aug 27, 2024
2 parents eeca07d + 424312a commit f07a11d
Show file tree
Hide file tree
Showing 15 changed files with 17,246 additions and 106 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ Cargo.lock

*.db

crates/libduckdb-sys/duckdb-sources/
crates/libduckdb-sys/duckdb-sources/*
crates/libduckdb-sys/duckdb/
crates/libduckdb-sys/._duckdb
1 change: 0 additions & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
[submodule "crates/libduckdb-sys/duckdb-sources"]
path = crates/libduckdb-sys/duckdb-sources
url = https://github.com/duckdb/duckdb
update = none
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ pkg-config = "0.3.24"
polars = "0.35.4"
polars-core = "0.35.4"
pretty_assertions = "1.4.0"
prettyplease = "0.2.20"
proc-macro2 = "1.0.56"
quote = "1.0.21"
r2d2 = "0.8.9"
Expand Down
2 changes: 1 addition & 1 deletion add_rustfmt_hook.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ command -v rustfmt >/dev/null 2>&1 || { echo >&2 "Rustfmt is required but it's n
# write a whole script to pre-commit hook
# NOTE: it will overwrite pre-commit file!
cat > .git/hooks/pre-commit <<'EOF'
#!/bin/bash -e
#!/bin/bash
declare -a rust_files=()
files=$(git diff-index --name-only --cached HEAD)
echo 'Formatting source files'
Expand Down
15 changes: 9 additions & 6 deletions crates/duckdb-loadable-macros/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
#![allow(clippy::redundant_clone)]
use proc_macro2::{Ident, Span, Punct, Literal};
use proc_macro2::{Ident, Literal, Punct, Span};

use syn::{parse_macro_input, spanned::Spanned, Item};

use proc_macro::TokenStream;
use quote::quote_spanned;

use darling::{Error, FromMeta};
use darling::ast::NestedMeta;
use darling::{ast::NestedMeta, Error, FromMeta};
use syn::ItemFn;

/// For parsing the arguments to the duckdb_entrypoint_c_api macro
Expand All @@ -23,18 +22,22 @@ struct CEntryPointMacroArgs {
pub fn duckdb_entrypoint_c_api(attr: TokenStream, item: TokenStream) -> TokenStream {
let attr_args = match NestedMeta::parse_meta_list(attr.into()) {
Ok(v) => v,
Err(e) => { return TokenStream::from(Error::from(e).write_errors()); }
Err(e) => {
return TokenStream::from(Error::from(e).write_errors());
}
};

let args = match CEntryPointMacroArgs::from_list(&attr_args) {
Ok(v) => v,
Err(e) => { return TokenStream::from(e.write_errors()); }
Err(e) => {
return TokenStream::from(e.write_errors());
}
};

/// TODO FETCH THE DEFAULT AUTOMATICALLY SOMEHOW
let minimum_duckdb_version = match args.min_duckdb_version {
Some(i) => i,
None => "dev".to_string()
None => "dev".to_string(),
};

let ast = parse_macro_input!(item as syn::Item);
Expand Down
7 changes: 4 additions & 3 deletions crates/duckdb/examples/hello-ext-capi/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,9 @@ impl VTab for HelloVTab {
}
}

#[duckdb_entrypoint_c_api(ext_name="rusty_quack", min_duckdb_version="v0.0.2")]
pub unsafe fn ExtensionEntrypoint(con : Connection) -> Result<(), Box<dyn Error>>{
con.register_table_function::<HelloVTab>("hello").expect("Failed to register hello table function");
#[duckdb_entrypoint_c_api(ext_name = "rusty_quack", min_duckdb_version = "v0.0.2")]
pub unsafe fn ExtensionEntrypoint(con: Connection) -> Result<(), Box<dyn Error>> {
con.register_table_function::<HelloVTab>("hello")
.expect("Failed to register hello table function");
Ok(())
}
8 changes: 4 additions & 4 deletions 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 = ["buildtime_bindgen"]
loadable_extension = ["prettyplease", "quote", "syn", "buildtime_bindgen"]

[dependencies]

Expand All @@ -36,9 +36,9 @@ vcpkg = { workspace = true, optional = true }
serde = { workspace = true, features = ["derive"] }
serde_json = { workspace = true }
tar = { workspace = true }
prettyplease = "0.2.20"
syn = "2.0"
quote = "1.0"
syn = { workspace = true, optional = true }
quote = { workspace = true, optional = true }
prettyplease = { workspace = true, optional = true }

[dev-dependencies]
arrow = { workspace = true, features = ["ffi"] }
30 changes: 0 additions & 30 deletions crates/libduckdb-sys/bindgen.sh

This file was deleted.

52 changes: 30 additions & 22 deletions crates/libduckdb-sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,17 @@ mod build_bundled {
let header = HeaderLocation::FromPath(format!("{out_dir}/{lib_name}/src/include/"));
bindings::write_to_out_dir(header, out_path);
}

#[cfg(not(feature = "buildtime_bindgen"))]
{
use std::fs;
fs::copy("src/bindgen_bundled_version.rs", out_path).expect("Could not copy bindings to output directory");
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");
}

let manifest_file = std::fs::File::open(format!("{out_dir}/{lib_name}/manifest.json")).expect("manifest file");
Expand Down Expand Up @@ -178,21 +185,27 @@ impl From<HeaderLocation> for String {
let prefix = env_prefix();
let mut header = env::var(format!("{prefix}_INCLUDE_DIR"))
.unwrap_or_else(|_| env::var(format!("{}_LIB_DIR", env_prefix())).unwrap());
header.push_str("/duckdb.h");
header.push_str(if cfg!(feature = "loadable_extension") {
"/duckdb_extension.h"
} else {
"/duckdb.h"
});
header
}
HeaderLocation::Wrapper => if cfg!(feature = "loadable_extension") {
"wrapper_ext.h".into()
} else {
"wrapper.h".into()
},
HeaderLocation::Wrapper => {
if cfg!(feature = "loadable_extension") {
"wrapper_ext.h".into()
} else {
"wrapper.h".into()
}
}
HeaderLocation::FromPath(path) => format!(
"{}/{}",
path,
if cfg!(feature = "loadable_extension") {
"duckdb.h"
"duckdb_extension.h"
} else {
"duckdb_extension.h"
"duckdb.h"
}
),
}
Expand Down Expand Up @@ -314,15 +327,12 @@ mod build_linked {
}
}


#[cfg(feature = "buildtime_bindgen")]
mod bindings {
use super::HeaderLocation;

use std::{fs::OpenOptions, io::Write, path::Path};

use syn;

#[cfg(feature = "loadable_extension")]
fn extract_method(ty: &syn::Type) -> Option<&syn::TypeBareFn> {
match ty {
Expand Down Expand Up @@ -363,11 +373,11 @@ mod bindings {
})
.expect("could not find duckdb_ext_api_v0");

// let duckdb_ext_api_v0_ident = duckdb_ext_api_v0.ident;
// let duckdb_ext_api_v0_ident = duckdb_ext_api_v0.ident;

let p_api = quote::format_ident!("p_api");
let mut stores = Vec::new();
// let mut malloc = Vec::new();
// let mut malloc = Vec::new();
// (2) `#define sqlite3_xyz sqlite3_api->abc` => `pub unsafe fn
// sqlite3_xyz(args) -> ty {...}` for each `abc` field:

Expand All @@ -382,11 +392,8 @@ mod bindings {

let method = extract_method(&field.ty).unwrap_or_else(|| panic!("unexpected type for {function_name}"));

let arg_names: syn::punctuated::Punctuated<&syn::Ident, syn::token::Comma> = method
.inputs
.iter()
.map(|i| &i.name.as_ref().unwrap().0)
.collect();
let arg_names: syn::punctuated::Punctuated<&syn::Ident, syn::token::Comma> =
method.inputs.iter().map(|i| &i.name.as_ref().unwrap().0).collect();

let args = &method.inputs;

Expand Down Expand Up @@ -446,7 +453,8 @@ mod bindings {
builder = builder.ignore_functions();
}

builder.trust_clang_mangling(false)
builder
.trust_clang_mangling(false)
.header(header.clone())
.parse_callbacks(Box::new(bindgen::CargoCallbacks::new()))
.generate()
Expand All @@ -456,7 +464,8 @@ mod bindings {

let mut output = String::from_utf8(output).expect("bindgen output was not UTF-8?!");

if cfg!(feature = "loadable_extension") {
#[cfg(feature = "loadable_extension")]
{
generate_functions(&mut output);
}

Expand All @@ -469,6 +478,5 @@ mod bindings {

file.write_all(output.as_bytes())
.unwrap_or_else(|_| panic!("Could not write to {out_path:?}"));

}
}
1 change: 1 addition & 0 deletions crates/libduckdb-sys/duckdb-sources
Submodule duckdb-sources added at 77a322
Binary file modified crates/libduckdb-sys/duckdb.tar.gz
Binary file not shown.
4,477 changes: 4,476 additions & 1 deletion crates/libduckdb-sys/src/bindgen_bundled_version.rs

Large diffs are not rendered by default.

Loading

0 comments on commit f07a11d

Please sign in to comment.