diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 4491ae09..8629cbb9 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -6,6 +6,9 @@ on: branches: [ main ] schedule: - cron: "43 7 * * 0" +concurrency: + group: ${{ github.ref }} + cancel-in-progress: true jobs: test-default-features: @@ -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 \ No newline at end of file diff --git a/Makefile b/Makefile index 8e8add9e..79df80df 100644 --- a/Makefile +++ b/Makefile @@ -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. # diff --git a/README.md b/README.md index 53df1317..9673d470 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/rust_icu_sys/build.rs b/rust_icu_sys/build.rs index e81e68fe..2922387b 100644 --- a/rust_icu_sys/build.rs +++ b/rust_icu_sys/build.rs @@ -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"); @@ -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(); +} diff --git a/rust_icu_sys/src/lib.rs b/rust_icu_sys/src/lib.rs index d4c64fc2..9e56f01d 100644 --- a/rust_icu_sys/src/lib.rs +++ b/rust_icu_sys/src/lib.rs @@ -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"))] @@ -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 for UCharCategory { fn from(value: i8) -> Self { match value { @@ -132,7 +114,7 @@ impl From for UCharCategory { 28 => UCharCategory::U_INITIAL_PUNCTUATION, 29 => UCharCategory::U_FINAL_PUNCTUATION, 30 => UCharCategory::U_CHAR_CATEGORY_COUNT, - _ => { + _ => { panic!("could not convert: {}", value); } }