Skip to content

Commit ae64202

Browse files
authored
Allow embedded ICU data with StaticICULinking (#113761)
1 parent 365dde3 commit ae64202

File tree

4 files changed

+49
-2
lines changed

4 files changed

+49
-2
lines changed

src/coreclr/nativeaot/BuildIntegration/Microsoft.NETCore.Native.Unix.targets

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ The .NET Foundation licenses this file to you under the MIT license.
356356
<Output TaskParameter="ExitCode" PropertyName="_DsymUtilOutput" />
357357
</Exec>
358358

359-
<Exec Command="CC=&quot;$(CppLinker)&quot; &quot;$(IlcHostPackagePath)/native/src/libs/build-local.sh&quot; &quot;$(IlcHostPackagePath)/&quot; &quot;$(IntermediateOutputPath)&quot; System.Globalization.Native"
359+
<Exec Command="CC=&quot;$(CppLinker)&quot; &quot;$(IlcHostPackagePath)/native/src/libs/build-local.sh&quot; &quot;$(IlcHostPackagePath)/&quot; &quot;$(MSBuildProjectDirectory)/$(IntermediateOutputPath)&quot; System.Globalization.Native &quot;$(EmbedIcuDataPath)&quot;"
360360
Condition="'$(StaticICULinking)' == 'true' and '$(InvariantGlobalization)' != 'true'" />
361361

362362
<Exec Command="CC=&quot;$(CppLinker)&quot; &quot;$(IlcHostPackagePath)/native/src/libs/build-local.sh&quot; &quot;$(IlcHostPackagePath)/&quot; &quot;$(IntermediateOutputPath)&quot; System.Security.Cryptography.Native"

src/coreclr/nativeaot/docs/compiling.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,29 @@ You can use this feature by adding the `StaticICULinking` property to your proje
7474
```xml
7575
<PropertyGroup>
7676
<StaticICULinking>true</StaticICULinking>
77+
78+
<!-- Optional: Embeds ICU data into the binary, making it fully standalone when system ICU
79+
libraries are not installed on the target machine.
80+
Update path to match your ICU version and variant: l(arge), b(ig endian), s(mall) or full. -->
81+
<EmbedIcuDataPath>/usr/share/icu/74.2/icudt74l.dat</EmbedIcuDataPath>
7782
</PropertyGroup>
7883
```
7984

85+
> [!NOTE]
86+
> Some distros, such as Alpine and Gentoo, currently package ICU data as a `icudt*.dat` archive,
87+
> while others, like Ubuntu, do not.
88+
> To use `EmbedIcuDataPath` on a distro that does not provide the `.dat` file,
89+
> you may need to build ICU with `--with-data-packaging=archive` to generate it.
90+
> See https://unicode-org.github.io/icu/userguide/icu_data#building-and-linking-against-icu-data.
91+
> ```sh
92+
> # e.g. to obtain icudt*.dat on Ubuntu
93+
> $ curl -sSL https://github.com/unicode-org/icu/releases/download/release-74-2/icu4c-74_2-src.tgz | tar xzf -
94+
> $ cd icu/source
95+
> $ ./configure --with-data-packaging=archive --enable-static --disable-shared --disable-samples
96+
> $ make -j
97+
> $ find . -path *out/* -name icudt*.dat -exec echo $(pwd)/{} \;
98+
> ```
99+
80100
This feature is only supported on Linux. This feature is not supported when crosscompiling.
81101
82102
License (Unicode): https://github.com/unicode-org/icu/blob/main/icu4c/LICENSE

src/native/libs/System.Globalization.Native/pal_icushim_static.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,15 @@ int32_t GlobalizationNative_LoadICU(void)
216216
}
217217
#endif
218218

219+
#if defined(EMBEDDED_ICU_DATA_HEADER)
220+
#include EMBEDDED_ICU_DATA_HEADER
221+
222+
if (!load_icu_data(icu_data))
223+
{
224+
return 0;
225+
}
226+
#endif
227+
219228
UErrorCode status = 0;
220229
UVersionInfo version;
221230
// Request the CLDR version to perform basic ICU initialization and find out

src/native/libs/build-local.sh

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,24 @@
1313
SHIM_SOURCE_DIR="$1"/native/src
1414
INTERMEDIATE_OUTPUT_PATH="$2"
1515
TARGET_LIBRARY="$3"
16+
EMBED_ICU_DATA_PATH="$4"
17+
18+
if [ -n "$EMBED_ICU_DATA_PATH" ]; then
19+
header="${INTERMEDIATE_OUTPUT_PATH}embedded_icu_data.h"
20+
21+
cat <<EOF > "$header"
22+
#ifndef EMBEDDED_ICU_DATA
23+
#define EMBEDDED_ICU_DATA
24+
25+
static const unsigned char icu_data[] __attribute__((aligned(16))) = {
26+
$(od -An -vtx1 "$EMBED_ICU_DATA_PATH" | tr -d ' \n' | sed 's/\(..\)/0x\1, /g')
27+
};
28+
29+
#endif // EMBEDDED_ICU_DATA
30+
EOF
31+
32+
CMAKE_EXTRA_ARGS=-DCMAKE_C_FLAGS="-DEMBEDDED_ICU_DATA_HEADER='\"$header\"'"
33+
fi
1634

1735
if [ -d "$SHIM_SOURCE_DIR" ]; then
1836
LOCAL_SHIM_DIR="$INTERMEDIATE_OUTPUT_PATH"/libs/$TARGET_LIBRARY/build
@@ -22,7 +40,7 @@ if [ -d "$SHIM_SOURCE_DIR" ]; then
2240
exit 1
2341
fi
2442

25-
if ! cmake -S "$SHIM_SOURCE_DIR/libs/$TARGET_LIBRARY/" -DLOCAL_BUILD:STRING=1 -DCLR_CMAKE_TARGET_UNIX:STRING=1; then
43+
if ! cmake -S "$SHIM_SOURCE_DIR/libs/$TARGET_LIBRARY/" -DLOCAL_BUILD:STRING=1 -DCLR_CMAKE_TARGET_UNIX:STRING=1 $CMAKE_EXTRA_ARGS; then
2644
echo "local_build.sh::ERROR: cmake failed"
2745
exit 1
2846
fi

0 commit comments

Comments
 (0)