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

Move linking flags to build.rs to help with non-cargo build systems #287

Merged
merged 5 commits into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ on:
branches: [ main ]
schedule:
- cron: "43 7 * * 0"
concurrency:
group: ${{ github.ref }}
cancel-in-progress: true

jobs:
test-default-features:
Expand Down Expand Up @@ -46,3 +49,14 @@ jobs:
- uses: actions/checkout@v2
- name: 'Test static-bindgen'
run: 'make static-bindgen'
test-static-linking:
runs-on: ${{ matrix.runs-on }}
strategy:
matrix:
runs-on: [ubuntu-latest, macos-latest]
steps:
- uses: actions/checkout@v2
- if: matrix.runs-on == 'macos-latest'
run: make macos-test
- if: matrix.runs-on == 'ubuntu-latest'
run: make test
8 changes: 8 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,14 @@ docker-test:
${DOCKER_REPO}/${DOCKER_TEST_ENV}:${USED_BUILDENV_VERSION}
.PHONY: docker-test

# Test with the homebrew version of icu4c on macOS with static linking (the default way of linking for distribution on Apple platforms)
macos-test:
brew install icu4c
RUST_ICU_LINK_SEARCH_DIR="$(shell brew --prefix)/opt/icu4c/lib" \
RUST_ICU_MAJOR_VERSION_NUMBER=${RUST_ICU_MAJOR_VERSION_NUMBER} \
cargo test --no-default-features --features=icu_version_64_plus,icu_version_67_plus,icu_version_68_plus,icu_version_in_env,renaming,static
.PHONY: macos-test

# Refreshes the static bindgen output (contents of ./rust_icu_sys/bindgen) based
# on the currently present ICU versions in the test environment.
#
Expand Down
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
Loading