Skip to content

Commit

Permalink
Compile from source on Windows (#65)
Browse files Browse the repository at this point in the history
* Let the cc crate use its default behaviour for -fPIC

* Make sure we pass "/bigobj" when compiling with the MSVC compiler

* Fix type error because DuckDB constants were inferred to be i32 on Windows

* Use the "duckdb_state" typedef instead of assuming it'll always be c_uint

* Make sure we define "DUCKDB_BUILD_LIBRARY" to set dllexport and friends

* Windows doesn't allow concurrent writes to a file

* Re-enable Windows builds

* Update arrow requirement from 17 to 18 (#64)

* Update arrow requirement from 17 to 18

Updates the requirements on [arrow](https://github.com/apache/arrow-rs) to permit the latest version.
- [Release notes](https://github.com/apache/arrow-rs/releases)
- [Changelog](https://github.com/apache/arrow-rs/blob/master/CHANGELOG.md)
- [Commits](apache/arrow-rs@17.0.0...18.0.0)

---
updated-dependencies:
- dependency-name: arrow
  dependency-type: direct:production
...

Signed-off-by: dependabot[bot] <[email protected]>

* fix build error

Change-Id: I252a9021ef57cbcb82e5122f30754415085718c8

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: wangfenjin <[email protected]>

* use action to download and extract

Change-Id: Ia2057bdf3dad686aa07b64f38cce20faa6d9a898

* fix filepath

Change-Id: I91c5abcad6460051000dea430373cf64d7bc44bb

* fix windows

Change-Id: If0d2dad8c889f6df387550f96712681f63c16c9c

* fix windows

Change-Id: Ia43544c3683bc6418eddf4e971413d621d629a89

* static set windows

Change-Id: I685ff5f761ff0e342bc9afeb053df48ec28f1ad2

* windows

Change-Id: I91c9f7325de3de5767baf462d1ac681931c3ab95

* fix

Change-Id: Ic8cc0833a7fad865b1c7b671518da043ffb02d57

* fix

Change-Id: Ib9a4dc6af13fbe39eff78dd849614b3285690dfe

* fix cargo test

Change-Id: I3b3f5726507e4a9c94188994fe533ee335c4192e

* use path

Change-Id: I1bf94c209ad65637be945c5d0a81a640da798f57

* set path

Change-Id: I86957007844bc9e20ed2829d04e731176ac5e3ae

* Add windows build from source

Change-Id: I3d45e79e61b49690a6a58756ccc8b7fb3abcc153

* fix test

Change-Id: Ibffbe97225c5d807bb2eaf4b7f80e807b9634405

Co-authored-by: Michael-F-Bryan <[email protected]>
Co-authored-by: Michael Bryan <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
  • Loading branch information
4 people authored Jul 24, 2022
1 parent 5c08014 commit 213b1a5
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 29 deletions.
64 changes: 55 additions & 9 deletions .github/workflows/rust.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ jobs:
fail-fast: true
matrix:
include:
#- { target: x86_64-pc-windows-msvc, os: windows-latest }
- { target: x86_64-unknown-linux-gnu, os: ubuntu-latest }
- { target: x86_64-pc-windows-msvc, os: windows-latest, duckdb: libduckdb-windows-amd64.zip }
- { target: x86_64-unknown-linux-gnu, os: ubuntu-latest, duckdb: libduckdb-linux-amd64.zip }
#- { target: x86_64-apple-darwin, os: macos-latest }
#- {
#target: x86_64-pc-windows-gnu,
Expand All @@ -33,34 +33,80 @@ jobs:
rust-version: stable${{ matrix.host }}
targets: ${{ matrix.target }}
components: 'rustfmt, clippy'
# download libduckdb
- uses: robinraju/[email protected]
name: Download duckdb
with:
repository: "duckdb/duckdb"
tag: "v0.4.0"
fileName: ${{ matrix.duckdb }}
out-file-path: .

# For Linux
- name: Linux extract duckdb
if: matrix.os == 'ubuntu-latest'
uses: ihiroky/extract-action@v1
with:
file_path: ${{ github.workspace }}/${{ matrix.duckdb }}
extract_dir: libduckdb
- run: cargo fmt --all -- --check
- name: Download DuckDB
run: |
wget https://github.com/duckdb/duckdb/releases/download/v0.4.0/libduckdb-linux-amd64.zip -O libduckdb.zip
unzip libduckdb.zip -d libduckdb
# - run: cargo clippy --all-targets --workspace --features bundled --features modern-full -- -D warnings -A clippy::redundant-closure
if: matrix.os == 'ubuntu-latest'
- run: cargo clippy --all-targets --workspace --features buildtime_bindgen --features modern-full -- -D warnings -A clippy::redundant-closure
if: matrix.os == 'ubuntu-latest'
name: run cargo clippy
env:
DUCKDB_LIB_DIR: ${{ github.workspace }}/libduckdb
DUCKDB_INCLUDE_DIR: ${{ github.workspace }}/libduckdb
LD_LIBRARY_PATH: ${{ github.workspace }}/libduckdb
- name: Run cargo-tarpaulin
if: matrix.os == 'ubuntu-latest'
uses: actions-rs/[email protected]
with:
# Intentionally omit time feature until we're on time 0.3, at which
# point it should be added to `bundled-full`.
args: '--features "buildtime_bindgen" --features "modern-full" --avoid-cfg-tarpaulin' # TODO restore to normal (https://github.com/xd009642/tarpaulin/issues/756#issuecomment-838769320)
# args: '--features "bundled" --features "modern-full" --avoid-cfg-tarpaulin' # TODO restore to normal (https://github.com/xd009642/tarpaulin/issues/756#issuecomment-838769320)
env:
DUCKDB_LIB_DIR: ${{ github.workspace }}/libduckdb
DUCKDB_INCLUDE_DIR: ${{ github.workspace }}/libduckdb
LD_LIBRARY_PATH: ${{ github.workspace }}/libduckdb
- name: Upload to codecov.io
if: matrix.os == 'ubuntu-latest'
uses: codecov/codecov-action@v1

sanitizer:
# For windows
- name: Windows extract duckdb
if: matrix.os == 'windows-latest'
uses: DuckSoft/[email protected]
with:
pathSource: D:\a\duckdb-rs\duckdb-rs\${{ matrix.duckdb }}
pathTarget: ${{ github.workspace }}/libduckdb

- name: Add path to PATH environment variable
if: matrix.os == 'windows-latest'
uses: myci-actions/export-env-var-powershell@1
with:
name: PATH
value: $env:PATH;${{ github.workspace }}/libduckdb
- name: Run cargo-test
if: matrix.os == 'windows-latest'
run: cargo test --features "buildtime_bindgen" --features "modern-full"
env:
DUCKDB_LIB_DIR: ${{ github.workspace }}/libduckdb
DUCKDB_INCLUDE_DIR: ${{ github.workspace }}/libduckdb

Windows:
name: Windows build from source
needs: test
runs-on: windows-latest
steps:
- uses: actions/checkout@v2
- uses: hecrj/setup-rust-action@v1
with:
rust-version: stable
targets: x86_64-pc-windows-msvc
- run: cargo test --features "bundled" --features "modern-full"

Sanitizer:
name: Address Sanitizer
needs: test
runs-on: ubuntu-latest
Expand Down
11 changes: 8 additions & 3 deletions libduckdb-sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ fn main() {
mod build_bundled {
use std::path::Path;

use crate::win_target;

pub fn main(out_dir: &str, out_path: &Path) {
let lib_name = super::lib_name();

Expand All @@ -63,13 +65,16 @@ mod build_bundled {
let mut cfg = cc::Build::new();
cfg.file(format!("{}/duckdb.cpp", lib_name))
.cpp(true)
// .static_flag(true)
.shared_flag(true)
.pic(true)
.flag_if_supported("-std=c++11")
.flag_if_supported("-stdlib=libc++")
.flag_if_supported("-stdlib=libstdc++")
.flag_if_supported("/bigobj")
.warnings(false);

if win_target() {
cfg.define("DUCKDB_BUILD_LIBRARY", None);
}

cfg.compile(lib_name);

println!("cargo:lib_dir={}", out_dir);
Expand Down
8 changes: 4 additions & 4 deletions libduckdb-sys/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::duckdb_state;
use std::error;
use std::fmt;
use std::os::raw::c_uint;

/// Error Codes
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
Expand Down Expand Up @@ -59,11 +59,11 @@ pub enum ErrorCode {
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Error {
pub code: ErrorCode,
pub extended_code: c_uint,
pub extended_code: duckdb_state,
}

impl Error {
pub fn new(result_code: c_uint) -> Error {
pub fn new(result_code: duckdb_state) -> Error {
Error {
code: ErrorCode::Unknown,
extended_code: result_code,
Expand All @@ -88,6 +88,6 @@ impl error::Error for Error {
}
}

pub fn code_to_str(_: c_uint) -> &'static str {
pub fn code_to_str(_: duckdb_state) -> &'static str {
"Unknown error code"
}
6 changes: 2 additions & 4 deletions libduckdb-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@ mod bindings {
#[allow(clippy::all)]
pub use bindings::*;

use std::os::raw::c_uint;

pub const DuckDBError: c_uint = duckdb_state_DuckDBError;
pub const DuckDBSuccess: c_uint = duckdb_state_DuckDBSuccess;
pub const DuckDBError: duckdb_state = duckdb_state_DuckDBError;
pub const DuckDBSuccess: duckdb_state = duckdb_state_DuckDBSuccess;

pub use self::error::*;
mod error;
Expand Down
2 changes: 1 addition & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ impl Config {
};
if state != ffi::DuckDBSuccess {
return Err(Error::DuckDBFailure(
ffi::Error::new(state as u32),
ffi::Error::new(state),
Some(format!("set {}:{} error", key, value)),
));
}
Expand Down
11 changes: 5 additions & 6 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use crate::types::Type;
use std::error;
use std::ffi::CStr;
use std::fmt;
use std::os::raw::c_uint;
use std::path::PathBuf;
use std::str;

Expand Down Expand Up @@ -216,13 +215,13 @@ impl error::Error for Error {
// These are public but not re-exported by lib.rs, so only visible within crate.

#[inline]
fn error_from_duckdb_code(code: c_uint, message: Option<String>) -> Result<()> {
Err(Error::DuckDBFailure(ffi::Error::new(code as u32), message))
fn error_from_duckdb_code(code: ffi::duckdb_state, message: Option<String>) -> Result<()> {
Err(Error::DuckDBFailure(ffi::Error::new(code), message))
}

#[cold]
#[inline]
pub fn result_from_duckdb_appender(code: c_uint, mut appender: ffi::duckdb_appender) -> Result<()> {
pub fn result_from_duckdb_appender(code: ffi::duckdb_state, mut appender: ffi::duckdb_appender) -> Result<()> {
if code == ffi::DuckDBSuccess {
return Ok(());
}
Expand All @@ -241,7 +240,7 @@ pub fn result_from_duckdb_appender(code: c_uint, mut appender: ffi::duckdb_appen

#[cold]
#[inline]
pub fn result_from_duckdb_prepare(code: c_uint, mut prepare: ffi::duckdb_prepared_statement) -> Result<()> {
pub fn result_from_duckdb_prepare(code: ffi::duckdb_state, mut prepare: ffi::duckdb_prepared_statement) -> Result<()> {
if code == ffi::DuckDBSuccess {
return Ok(());
}
Expand All @@ -260,7 +259,7 @@ pub fn result_from_duckdb_prepare(code: c_uint, mut prepare: ffi::duckdb_prepare

#[cold]
#[inline]
pub fn result_from_duckdb_arrow(code: c_uint, mut out: ffi::duckdb_arrow) -> Result<()> {
pub fn result_from_duckdb_arrow(code: ffi::duckdb_state, mut out: ffi::duckdb_arrow) -> Result<()> {
if code == ffi::DuckDBSuccess {
return Ok(());
}
Expand Down
2 changes: 1 addition & 1 deletion src/inner_connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ impl InnerConnection {
if r != ffi::DuckDBSuccess {
let msg = Some(CStr::from_ptr(c_err).to_string_lossy().to_string());
ffi::duckdb_free(c_err as *mut c_void);
return Err(Error::DuckDBFailure(ffi::Error::new(r as u32), msg));
return Err(Error::DuckDBFailure(ffi::Error::new(r), msg));
}
InnerConnection::new(db, true)
}
Expand Down
3 changes: 2 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
//! for person in person_iter {
//! println!("Found person {:?}", person.unwrap());
//! }
//!
//!
//! // query table by arrow
//! let rbs: Vec<RecordBatch> = stmt.query_arrow([])?.collect();
//! print_batches(&rbs);
Expand Down Expand Up @@ -558,6 +558,7 @@ mod test {
}

#[test]
#[cfg_attr(windows, ignore = "Windows doesn't allow concurrent writes to a file")]
fn test_concurrent_transactions_busy_commit() -> Result<()> {
let tmp = tempfile::tempdir().unwrap();
let path = tmp.path().join("transactions.db3");
Expand Down
1 change: 1 addition & 0 deletions src/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,7 @@ mod test {
}

#[test]
#[cfg_attr(windows, ignore = "Windows doesn't allow concurrent writes to a file")]
fn test_insert_duplicate() -> Result<()> {
let db = Connection::open_in_memory()?;
db.execute_batch("CREATE TABLE foo(x INTEGER UNIQUE)")?;
Expand Down

0 comments on commit 213b1a5

Please sign in to comment.