ICU Setup for SwiftPM

27 阅读1分钟

Clibicuuc ICU Setup (Parallel Tracks)

Goal: keep Homebrew and non-Homebrew flows equivalent for ICU build mode, pkg-config discovery, and SwiftPM system library usage.

1) Build ICU (same mode in both tracks)

A) Homebrew (icu4c@78)

brew sh
export HOMEBREW_NO_INSTALL_FROM_API=1
brew edit icu4c@78
# Add to formula args:
#   --disable-renaming
brew reinstall --build-from-source icu4c@78

B) Custom source build (no Homebrew)

# From ICU source root
./configure --disable-renaming \
    --disable-shared \
    --enable-static \
    --with-data-packaging=static
make -j"$(sysctl -n hw.ncpu)"
make install

2) Configure pkg-config lookup (parallel)

A) Homebrew (icu4c@78)

export PKG_CONFIG_PATH="$(brew --prefix icu4c@78)/lib/pkgconfig:${PKG_CONFIG_PATH}"
pkg-config --modversion icu-uc
pkg-config --cflags --libs icu-uc

B) Custom prefix

export ICU_PREFIX="/usr/local"
export PKG_CONFIG_PATH="${ICU_PREFIX}/lib/pkgconfig:${PKG_CONFIG_PATH}"
pkg-config --modversion icu-uc
pkg-config --cflags --libs icu-uc

3) .systemLibrary config (parallel)

A) Homebrew provider declared

.systemLibrary(
  name: "Clibicuuc",
  pkgConfig: "icu-uc",
  providers: [
    .brew(["icu4c@78"])
  ]
)

B) Custom build provider omitted

.systemLibrary(
  name: "Clibicuuc",
  pkgConfig: "icu-uc"
)

For both tracks, compile with matching renaming mode:

.define(
  "U_DISABLE_RENAMING", to: "1",
  .when(platforms: [.macOS, .iOS, .tvOS, .watchOS, .visionOS])
)

4) Populate Clibicuuc-Bridging-Header.h

Sources/Clibicuuc/Clibicuuc-Bridging-Header.h should include the ICU headers your package exposes via Clibicuuc:

#include <unicode/utypes.h>
#include <unicode/ustring.h>
#include <unicode/ubidi.h>
#include <unicode/unorm2.h>
  • Add additional <unicode/...> headers as needed by your call sites.

5) Shared module.modulemap

Sources/Clibicuuc/module.modulemap:

module Clibicuuc [system][extern_c] {
  header "Clibicuuc-Bridging-Header.h"
  link "icuuc"
  link "icudata"
  export *
}

6) Build + verify

swift package clean
swift build -v

Optional checks:

pkg-config --libs icu-uc
pkg-config --cflags icu-uc

7) Fast triage

# Re-export PKG_CONFIG_PATH for the track you use, then:
pkg-config --modversion icu-uc
swift package clean && swift build -v
  • If link errors show suffixed symbols (for example *_78), ICU build mode and U_DISABLE_RENAMING are not aligned.

References