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

Cannot link sqlcipher statically #558

Open
Fensteer opened this issue Aug 6, 2019 · 10 comments
Open

Cannot link sqlcipher statically #558

Fensteer opened this issue Aug 6, 2019 · 10 comments

Comments

@Fensteer
Copy link

Fensteer commented Aug 6, 2019

I'm trying to link sqlcipher statically but I can't managed to do it...

Step to reproduce

Cargo.toml

[dependencies]
libsqlite3-sys = { version = "*", features = ["sqlcipher"] }

main.rs

use libsqlite3_sys;

fn main() {
    unsafe {
        libsqlite3_sys::sqlite3_initialize();
    }
}
$ export SQLCIPHER_STATIC=1
$ export SQLCIPHER_INCLUDE_DIR=/usr/include/sqlcipher
$ export SQLCIPHER_LIB_DIR=/usr/lib/x86_64-linux-gnu/
$ cargo build
$ ldd target/debug/test_sqlcipher
...
        libsqlcipher.so.0 => /usr/lib/x86_64-linux-gnu/libsqlcipher.so.0
...
$ file /usr/lib/x86_64-linux-gnu/libsqlcipher.a
/usr/lib/x86_64-linux-gnu/libsqlcipher.a: current ar archive

I also tried with PKG_CONFIG_ALL_STATIC=1 without success.

Any idea ?

@Fensteer Fensteer changed the title Cannot link salcipher statically Cannot link sqlcipher statically Aug 6, 2019
@thomcc
Copy link
Member

thomcc commented Aug 6, 2019

We manage to do this in https://github.com/mozilla/application-services, but the build code is mostly spread out. I'll see if I can repro this later on a linux VM.

Just to check, your SQLCIPHER_LIB_DIR does have a libsqlcipher.a in it right?

@JoyceBabu
Copy link

I was able to do this on Mac.

# Cargo.toml
[dependencies]
rusqlite = "0.20"
time = "0.1.42"
openssl-sys = "0.9.48"

[features]
default = ["sqlcipher"]
sqlcipher = ["rusqlite/sqlcipher"]
$ brew install sqlcipher
$ export SQLCIPHER_STATIC=1
$ export SQLCIPHER_LIB_DIR=/usr/local/opt/sqlcipher/lib
$ export SQLCIPHER_INCLUDE_DIR=/usr/local/opt/sqlcipher/include
$ cargo build

$ otool -L target/debug/sqlite-test
target/debug/sqlite-test:
	/usr/lib/libz.1.dylib (compatibility version 1.0.0, current version 1.2.11)
	/usr/local/opt/openssl/lib/libcrypto.1.0.0.dylib (compatibility version 1.0.0, current version 1.0.0)
	/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1252.250.1)
	/usr/lib/libresolv.9.dylib (compatibility version 1.0.0, current version 1.0.0)

@Fensteer
Copy link
Author

Fensteer commented Aug 7, 2019

@thomcc Yes the folder contains libsqlcipher.a
I managed to compile statically by adding println!("cargo:rustc-link-lib=static=sqlcipher") in libsqlite3-sys build.rs.
Before this patch the file output contains :

cargo:rerun-if-env-changed=SQLCIPHER_INCLUDE_DIR
cargo:rerun-if-env-changed=SQLCIPHER_LIB_DIR
cargo:rerun-if-env-changed=SQLCIPHER_STATIC
cargo:rustc-link-lib=sqlcipher
cargo:rustc-link-lib=dl
cargo:rustc-link-lib=crypto
cargo:rustc-link-lib=pthread
cargo:rustc-link-lib=dl

@gwenn
Copy link
Collaborator

gwenn commented Aug 7, 2019

Could you please confirm that pkg_config probe succeeds ?
If so pkg_config should have emit a static=sqlcipher:
https://github.com/rust-lang/pkg-config-rs/blob/69d261f9bdcdfff3b237290324cd042a98f9f95b/src/lib.rs#L23

//! * `FOO_STATIC` - pass `--static` for the library `foo`

No ?

@Fensteer
Copy link
Author

Fensteer commented Aug 7, 2019

The method pkg_config::Config::new().statik(true).probe("sqlcipher") succeed and print :

cargo:rustc-link-lib=sqlcipher
cargo:rustc-link-lib=dl
cargo:rustc-link-lib=crypto
cargo:rustc-link-lib=pthread
cargo:rustc-link-lib=dl

Do you think is a pkg-config issue ?

@gwenn
Copy link
Collaborator

gwenn commented Aug 7, 2019

Sorry to bother you but could you please check:
https://github.com/rust-lang/pkg-config-rs/blob/69d261f9bdcdfff3b237290324cd042a98f9f95b/src/lib.rs#L564

/// System libraries should only be linked dynamically

And below:
https://github.com/rust-lang/pkg-config-rs/blob/69d261f9bdcdfff3b237290324cd042a98f9f95b/src/lib.rs#L574

        !system_roots.iter().any(|sys| dir.starts_with(sys)) &&

/usr/local/opt/sqlcipher/lib starts with /usr...

@gwenn
Copy link
Collaborator

gwenn commented Aug 7, 2019

See rust-lang/pkg-config-rs#36

@thomcc
Copy link
Member

thomcc commented Aug 7, 2019

As I've mentioned before (https://github.com/jgallagher/rusqlite/issues/510) I kind of feel like pkgconfig shouldn't be invoked if the user specifies the lib_dir/include_dir environment variables explicitly.

This is not a real fix, though, since ideally SQLCIPHER_STATIC would still be respected even if the include/lib environment variables weren't manually provided.

@avnerbarr
Copy link

is this going to be merged?

@gwenn
Copy link
Collaborator

gwenn commented Oct 4, 2021

May be fixed by version 0.26.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants