Skip to content

Commit

Permalink
Make usage of openSSL optional
Browse files Browse the repository at this point in the history
  • Loading branch information
stormshield-gt committed Aug 22, 2024
1 parent c5d8aa0 commit 466c4fb
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 10 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@ for Rust libraries in [RFC #1105](https://github.com/rust-lang/rfcs/blob/master/

## Unreleased

### Changed

* The `bundled` feature of `pq-sys` no longer use openssl by default.
To activate `openssl` when compiling `libpq` from source, add `pq-src` with the `with-openssl` feature to your crate dependencies:
```toml
[dependencies]
pq-src = { version = "0.3.0", features = ["with-openssl"] }
```

## pq-sys [0.6.1] 2024-06-11

### Changed
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ If pkg-config is being used, it's configuration options will apply.

* `buildtime_bindgen`: Run `bindgen` at build-time to generate bindings using installed headers. Not compatible with the `bundled` feature.
* `bundled`: Build the bundled version of `libpq` from source.
Optionally you can also activate the usage of `openssl` within `libpq` by adding the `pq-src` crate with the `with-openssl` feature to
your crate dependencies:
```toml
[dependencies]
pq-src = { version = "0.3.0", features = ["with-openssl"] }
```
To use a bundled version of `openssl`, add the `openssl-sys` crate with the `vendored` feature to your crate dependencies:
```toml
[dependencies]
Expand Down
3 changes: 2 additions & 1 deletion pq-src/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@ categories = ["database", "external-ffi-bindings"]
readme = "README.md"

[dependencies]
openssl-sys = "0.9.93"
openssl-sys = { version = "0.9.93", optional = true}

[build-dependencies]
cc = "1.0.83"

[features]
default = []
with-asan = []
with-openssl = ["dep:openssl-sys"]
7 changes: 4 additions & 3 deletions pq-src/additional_include/pg_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@
#define BLCKSZ 8192

/* Saved arguments from configure */
#if defined USE_OPENSSL
#define CONFIGURE_ARGS " '--with-openssl' '--without-readline'"
#else
#define CONFIGURE_ARGS " '--without-readline'"
#endif

/* Define to the default TCP port number on which the server listens and to
which clients will try to connect. This can be overridden at run-time, but
Expand Down Expand Up @@ -212,9 +216,6 @@
/* Define to 1 if you have the ANSI C header files. */
#define STDC_HEADERS 1

/* Define to 1 to build with OpenSSL support. (--with-ssl=openssl) */
#define USE_OPENSSL 1

/* Define WORDS_BIGENDIAN to 1 if your processor stores words with the most
significant byte first (like Motorola and SPARC, unlike Intel). */
#if defined AC_APPLE_UNIVERSAL_BUILD
Expand Down
35 changes: 29 additions & 6 deletions pq-src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,16 @@ const LIBCOMMON_BASE: &[&str] = &[
"restricted_token.c",
"sprompt.c",
"logging.c",
];

const LIBCOMMON_OPENSSL: &[&str] = &[
"cryptohash_openssl.c",
"hmac_openssl.c",
"protocol_openssl.c",
];

const LIBCOMMON_NOT_OPENSSL: &[&str] = &["cryptohash.c", "hmac.c", "md5.c", "sha1.c", "sha2.c"];

const LIBCOMMON_NOT_WINDOWS: &[&str] = &[];

const LIBCOMMON_WINDOWS: &[&str] = &["wchar.c"];
Expand All @@ -102,10 +107,10 @@ const LIBPQ_BASE: &[&str] = &[
"legacy-pqsignal.c",
"libpq-events.c",
"pqexpbuffer.c",
"fe-secure-common.c",
"fe-secure-openssl.c",
];

const LIBPQ_OPENSSL: &[&str] = &["fe-secure-common.c", "fe-secure-openssl.c"];

const LIBPQ_NOT_WINDOWS: &[&str] = &[];

const LIBPQ_WINDOWS: &[&str] = &["fe-secure.c", "pthread-win32.c", "win32.c"];
Expand All @@ -120,6 +125,7 @@ fn unimplemented() -> ! {

fn main() {
let target_os = env::var("CARGO_CFG_TARGET_OS").unwrap();
let use_openssl = env::var("CARGO_FEATURE_WITH_OPENSSL").is_ok();

println!("cargo:rerun-if-changed=additional_include");
let crate_dir = env!("CARGO_MANIFEST_DIR");
Expand Down Expand Up @@ -168,10 +174,9 @@ fn main() {
format!("{path}src/include"),
format!("{crate_dir}/additional_include"),
temp_include.clone(),
env::var("DEP_OPENSSL_INCLUDE").unwrap().clone(),
][..];

let includes = if target_os == "windows" {
let mut includes = if target_os == "windows" {
let includes_windows = &[
format!("{path}/src/include/port/win32/"),
format!("{path}/src/include/port/win32_msvc/"),
Expand All @@ -181,6 +186,10 @@ fn main() {
base_includes.to_vec()
};

if use_openssl {
includes.extend_from_slice(&[env::var("DEP_OPENSSL_INCLUDE").unwrap().clone()]);
}

basic_build
.define("FRONTEND", None)
.warnings(false)
Expand Down Expand Up @@ -212,9 +221,23 @@ fn main() {
_ => unimplemented(),
};

let (libcommon, libpq) = if use_openssl {
// Define to 1 to build with OpenSSL support. (--with-ssl=openssl)
basic_build.define("USE_OPENSSL", "1");
(
[LIBCOMMON_BASE, LIBCOMMON_OPENSSL].concat(),
[LIBPQ_BASE, LIBPQ_OPENSSL].concat(),
)
} else {
(
[LIBCOMMON_BASE, LIBCOMMON_NOT_OPENSSL].concat(),
LIBPQ_BASE.to_vec(),
)
};

let libports = LIBPORTS_BASE.iter().chain(libports_os);
let libcommon = LIBCOMMON_BASE.iter().chain(libcommon_os);
let libpq = LIBPQ_BASE.iter().chain(libpq_os);
let libcommon = libcommon.iter().chain(libcommon_os);
let libpq = libpq.iter().chain(libpq_os);

basic_build
.files(
Expand Down
1 change: 1 addition & 0 deletions pq-src/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
#[cfg(feature = "with-openssl")]
extern crate openssl_sys;

0 comments on commit 466c4fb

Please sign in to comment.