diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 00:47:55 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 00:47:55 +0000 |
commit | 26a029d407be480d791972afb5975cf62c9360a6 (patch) | |
tree | f435a8308119effd964b339f76abb83a57c29483 /intl/icu_capi/c/examples/fixeddecimal_tiny | |
parent | Initial commit. (diff) | |
download | firefox-26a029d407be480d791972afb5975cf62c9360a6.tar.xz firefox-26a029d407be480d791972afb5975cf62c9360a6.zip |
Adding upstream version 124.0.1.upstream/124.0.1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'intl/icu_capi/c/examples/fixeddecimal_tiny')
4 files changed, 153 insertions, 0 deletions
diff --git a/intl/icu_capi/c/examples/fixeddecimal_tiny/.gitignore b/intl/icu_capi/c/examples/fixeddecimal_tiny/.gitignore new file mode 100644 index 0000000000..586ba666b3 --- /dev/null +++ b/intl/icu_capi/c/examples/fixeddecimal_tiny/.gitignore @@ -0,0 +1,8 @@ +a.out +optim* +*.i64 +*.dot +*.elf +*.o +a.out.dSYM +baked_data diff --git a/intl/icu_capi/c/examples/fixeddecimal_tiny/Makefile b/intl/icu_capi/c/examples/fixeddecimal_tiny/Makefile new file mode 100644 index 0000000000..e9f610f429 --- /dev/null +++ b/intl/icu_capi/c/examples/fixeddecimal_tiny/Makefile @@ -0,0 +1,83 @@ +# This file is part of ICU4X. For terms of use, please see the file +# called LICENSE at the top level of the ICU4X source tree +# (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ). + +# PLEASE KEEP THIS FILE IN SYNC WITH ../segmenter_tiny/Makefile + +.DEFAULT_GOAL := test +.PHONY: build test +FORCE: + +ALL_HEADERS := $(wildcard ../../include/*.h) +ICU4X_NIGHTLY_TOOLCHAIN ?= "nightly-2022-12-26" + +$(ALL_HEADERS): + +GCC := gcc +CLANG := clang-15 +LLD := lld-15 + +baked_data/macros.rs: + cargo run -p icu_datagen -- --locales en bn --keys all --fallback preresolved --format mod --out baked_data --overwrite + +crate/target/debug/libcrate.a: FORCE + cd crate && cargo build --features std + +crate/target/x86_64-unknown-linux-gnu/debug/libcrate.a: FORCE baked_data/macros.rs + rustup toolchain install ${ICU4X_NIGHTLY_TOOLCHAIN} + rustup component add rust-src --toolchain ${ICU4X_NIGHTLY_TOOLCHAIN} + cd crate && \ + RUSTFLAGS="-Clinker-plugin-lto -Clinker=$(CLANG) -Ccodegen-units=1 -Clink-arg=-flto -Cpanic=abort" ICU4X_DATA_DIR=$(shell pwd)/baked_data cargo +${ICU4X_NIGHTLY_TOOLCHAIN} build -Z build-std=std,panic_abort -Z build-std-features=panic_immediate_abort --target x86_64-unknown-linux-gnu + +crate/target/x86_64-unknown-linux-gnu/release/libcrate.a: FORCE baked_data/macros.rs + rustup toolchain install ${ICU4X_NIGHTLY_TOOLCHAIN} + rustup component add rust-src --toolchain ${ICU4X_NIGHTLY_TOOLCHAIN} + cd crate && \ + RUSTFLAGS="-Clto -Cembed-bitcode -Clinker-plugin-lto -Clinker=$(CLANG) -Ccodegen-units=1 -Clink-arg=-flto -Cpanic=abort -Copt-level=s" ICU4X_DATA_DIR=$(shell pwd)/baked_data cargo +${ICU4X_NIGHTLY_TOOLCHAIN} build -Z build-std=std,panic_abort -Z build-std-features=panic_immediate_abort --target x86_64-unknown-linux-gnu --release + +# Naive target: no optimizations, full std +optim0.elf: crate/target/debug/libcrate.a $(ALL_HEADERS) test.c + $(GCC) test.c crate/target/debug/libcrate.a -ldl -lpthread -lm -g -o optim0.elf + +# optim.elf: gcc with maximum link-time code stripping (gc-sections and strip-all) +optim1.elf: crate/target/debug/libcrate.a $(ALL_HEADERS) test.c + $(GCC) -fdata-sections -ffunction-sections test.c crate/target/debug/libcrate.a -ldl -lpthread -lm -g -o optim1.elf -Wl,--gc-sections -Wl,--strip-all + +# optim2.elf: clang single-step with gc-sections +optim2.elf: crate/target/x86_64-unknown-linux-gnu/debug/libcrate.a $(ALL_HEADERS) test.c + $(CLANG) -flto -fdata-sections -ffunction-sections test.c crate/target/x86_64-unknown-linux-gnu/debug/libcrate.a -g -o optim2.elf -Wl,--gc-sections + +optim3.o: $(ALL_HEADERS) test.c + $(CLANG) -c -flto=thin -fdata-sections -ffunction-sections --target=x86_64-unknown-linux-gnu test.c -g -o optim3.o + +# optim3.elf: clang two-step with lld, debug mode +optim3.elf: optim3.o crate/target/x86_64-unknown-linux-gnu/debug/libcrate.a + $(CLANG) -flto=thin -fuse-ld=$(LLD) -L . -o optim3.elf optim3.o crate/target/x86_64-unknown-linux-gnu/debug/libcrate.a -Wl,--gc-sections + +optim4.o: $(ALL_HEADERS) test.c + $(CLANG) -c -flto=thin -fdata-sections -ffunction-sections --target=x86_64-unknown-linux-gnu test.c -g -o optim4.o + +# optim4.elf: clang two-step with lld, release mode with debug symbols +optim4.elf: optim4.o crate/target/x86_64-unknown-linux-gnu/release/libcrate.a + $(CLANG) -flto=thin -fuse-ld=$(LLD) -L . -o optim4.elf optim4.o crate/target/x86_64-unknown-linux-gnu/release/libcrate.a -Wl,--gc-sections + +optim5.o: $(ALL_HEADERS) test.c + $(CLANG) -c -flto=thin -fdata-sections -ffunction-sections --target=x86_64-unknown-linux-gnu test.c -o optim5.o + +# optim5.elf: clang two-step with lld, release mode stripped of debug symbols +optim5.elf: optim5.o crate/target/x86_64-unknown-linux-gnu/release/libcrate.a + $(CLANG) -flto=thin -fuse-ld=$(LLD) -L . -o optim5.elf optim5.o crate/target/x86_64-unknown-linux-gnu/release/libcrate.a -Wl,--gc-sections -Wl,--strip-all + +build: optim0.elf optim1.elf optim2.elf optim3.elf optim4.elf optim5.elf + ls -l optim*.elf + +test: build + ./optim0.elf bn + ./optim1.elf bn + ./optim2.elf bn + ./optim2.elf bn + ./optim4.elf bn + ./optim5.elf bn + +clean: + git clean -xf * diff --git a/intl/icu_capi/c/examples/fixeddecimal_tiny/README.md b/intl/icu_capi/c/examples/fixeddecimal_tiny/README.md new file mode 100644 index 0000000000..0aba7c3419 --- /dev/null +++ b/intl/icu_capi/c/examples/fixeddecimal_tiny/README.md @@ -0,0 +1,8 @@ +# Tiny FixedDecimal FFI Demo + +This example contains tooling to build a size-optimized binary using FixedDecimal and FixedDecimalFormatter in C over FFI. + +Prerequisites: `clang` and `lld` 14. `apt-get install clang lld` *might* work, but if you run into errors, refer to the following thread for tips: + +https://github.com/rust-lang/rust/issues/60059 + diff --git a/intl/icu_capi/c/examples/fixeddecimal_tiny/test.c b/intl/icu_capi/c/examples/fixeddecimal_tiny/test.c new file mode 100644 index 0000000000..880cbad144 --- /dev/null +++ b/intl/icu_capi/c/examples/fixeddecimal_tiny/test.c @@ -0,0 +1,54 @@ +// This file is part of ICU4X. For terms of use, please see the file +// called LICENSE at the top level of the ICU4X source tree +// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ). + +#include "../../include/ICU4XFixedDecimalFormatter.h" +#include <string.h> +#include <stdio.h> + +int main(int argc, char *argv[]) { + if (argc != 2) { + printf("Usage: %s <language>\n", argv[0]); + return 1; + } + + ICU4XLocale* locale = ICU4XLocale_create_und(); + if (!ICU4XLocale_set_language(locale, argv[1], strlen(argv[1])).is_ok) { + printf("Invalid language tag \"%s\"\n", argv[1]); + return 1; + } + + ICU4XDataProvider* provider = ICU4XDataProvider_create_compiled(); + ICU4XFixedDecimal* decimal = ICU4XFixedDecimal_create_from_u64(1000007); + + diplomat_result_box_ICU4XFixedDecimalFormatter_ICU4XError fdf_result = + ICU4XFixedDecimalFormatter_create_with_grouping_strategy(provider, locale, ICU4XFixedDecimalGroupingStrategy_Auto); + if (!fdf_result.is_ok) { + printf("Failed to create FixedDecimalFormatter\n"); + return 1; + } + ICU4XFixedDecimalFormatter* fdf = fdf_result.ok; + char output[40]; + + DiplomatWriteable write = diplomat_simple_writeable(output, 40); + + bool success = ICU4XFixedDecimalFormatter_format(fdf, decimal, &write).is_ok; + if (!success) { + printf("Failed to write result of FixedDecimalFormatter::format to string.\n"); + return 1; + } + printf("Output is %s\n", output); + + const char* expected = u8"১০,০০,০০৭"; + if (strcmp(output, expected) != 0) { + printf("Output does not match expected output!\n"); + return 1; + } + + ICU4XFixedDecimal_destroy(decimal); + ICU4XFixedDecimalFormatter_destroy(fdf); + ICU4XLocale_destroy(locale); + ICU4XDataProvider_destroy(provider); + + return 0; +} |