Skip to content

Commit

Permalink
Move linking config to build.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
gferon committed Oct 24, 2023
1 parent b56c59f commit 1266c67
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 21 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ Feature | Default? | Description
`renaming` | Yes | If set, ICU bindings are generated with version numbers appended. This is called "renaming" in ICU, and is normally needed only when linking against specific ICU version is required, for example to work around having to link different ICU versions. See [the ICU documentation](https://unicode-org.github.io/icu/userguide/icu/design.html) for a discussion of renaming. **This feature MUST be used when `bindgen` is NOT used.**
`icu_config` | Yes | If set, the binary icu-config will be used to configure the library. Turn this feature off if you do not want `build.rs` to try to autodetect the build environment. You will want to skip this feature if your build environment configures ICU in a different way. **This feature is only meaningful when `bindgen` feature is used; otherwise it has no effect.**
`icu_version_in_env` | No | If set, ICU bindings are made for the ICU version specified in the environment variable `RUST_ICU_MAJOR_VERSION_NUMBER`, which is made available to cargo at build time. See section below for details on how to use this feature. **This feature is only meaningful when `bindgen` feature is NOT used; otherwise it has no effect.**
`static` | No | If set, link ICU libraries statically (and the standard C++ dynamically). You can use `RUST_ICU_LINK_SEARCH_DIR` to add an extra path to the search path if you have a build of ICU in a non-standard directory.

# Prerequisites

Expand Down
27 changes: 26 additions & 1 deletion rust_icu_sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,24 @@ macro_rules! versioned_function {
}
}

fn rustc_link_libs() {
if cfg!(feature = "static") {
println!("cargo:rustc-link-lib=static=icuuc");
println!("cargo:rustc-link-lib=static=icui18n");
println!("cargo:rustc-link-lib=static:+whole-archive,-bundle=icudata");
// On systems such as macOS, libc++ is the default library
if cfg!(target_vendor = "apple") {
println!("cargo:rustc-link-lib=dylib=c++");
} else {
println!("cargo:rustc-link-lib=dylib=stdc++");
}
} else {
println!("cargo:rustc-link-lib=dylib=icuuc");
println!("cargo:rustc-link-lib=dylib=icui18n");
println!("cargo:rustc-link-lib=dylib=icudata");
}
}

#[cfg(feature = "use-bindgen")]
fn main() -> Result<(), anyhow::Error> {
std::env::set_var("RUST_BACKTRACE", "full");
Expand All @@ -486,10 +504,17 @@ fn main() -> Result<(), anyhow::Error> {
return Ok(());
}
inner::icu_config_autodetect()?;
rustc_link_libs();
println!("done:true");
Ok(())
}

/// No-op if use-bindgen is disabled.
#[cfg(not(feature = "use-bindgen"))]
fn main() {}
fn main() {
// can be used to provide an extra path to find libicuuc, libicui18n and libicudata
if let Ok(lib_dir) = std::env::var("RUST_ICU_LINK_SEARCH_DIR") {
println!("cargo:rustc-link-search=native={}", lib_dir);
}
rustc_link_libs();
}
22 changes: 2 additions & 20 deletions rust_icu_sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
non_upper_case_globals,
unused_imports,
rustdoc::bare_urls,
deref_nullptr,
deref_nullptr
)]

#[cfg(all(feature = "icu_version_in_env", feature = "icu_config"))]
Expand Down Expand Up @@ -80,24 +80,6 @@ impl std::fmt::Display for UErrorCode {

extern crate libc;

// A "fake" extern used to express link preferences. The libraries mentioned
// below will be converted to "-l" flags to the linker.
#[cfg_attr(not(feature = "static"), link(name = "icui18n", kind = "dylib"))]
#[cfg_attr(not(feature = "static"), link(name = "icuuc", kind = "dylib"))]
#[cfg_attr(feature = "static", link(name = "icui18n", kind = "static"))]
#[cfg_attr(feature = "static", link(name = "icuuc", kind = "static"))]
#[cfg_attr(feature = "static", link(name = "icudata", kind = "static"))]
// On systems such as macOS, libc++ is the default library
#[cfg_attr(
all(target_vendor = "apple", feature = "static"),
link(name = "c++", kind = "dylib")
)]
#[cfg_attr(
not(all(target_vendor = "apple", feature = "static")),
link(name = "stdc++", kind = "dylib")
)]
extern "C" {}

impl From<i8> for UCharCategory {
fn from(value: i8) -> Self {
match value {
Expand Down Expand Up @@ -132,7 +114,7 @@ impl From<i8> for UCharCategory {
28 => UCharCategory::U_INITIAL_PUNCTUATION,
29 => UCharCategory::U_FINAL_PUNCTUATION,
30 => UCharCategory::U_CHAR_CATEGORY_COUNT,
_ => {
_ => {
panic!("could not convert: {}", value);
}
}
Expand Down

0 comments on commit 1266c67

Please sign in to comment.