Files
Francesco BigiariniandClaude Opus 5.5 6132c343fc Add a Go wrapper in src/go (#202)
* Add a Go wrapper in src/go

A cgo package, github.com/zaerl/mojibake/src/go, that compiles the C
library as one translation unit with the Go code. The module lives at
the repository root so that go get fetches the C sources with it.

It covers normalization, locale-aware casing and caseless matching,
BCP 47 tags, segmentation iterators, terminal width, encodings, bidi,
character data and properties, filters, identifiers, emoji, collation,
IDNA, confusables, and Hangul. Build tags leave out the optional
features. The tests include the Unicode conformance files.

Add make test-go, an example in examples/go, a Go CI job, and docs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Francesco Bigiarini <francesco.bigiarini@gmail.com>

* Go: pass the character-name placeholder through

Without the name data, the C library names every character with a
placeholder such as "Codepoint U+00E9". The Go package dropped it and
returned "". Return it instead, as the C++ wrapper and the Rust crate
do, and document it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Francesco Bigiarini <francesco.bigiarini@gmail.com>

---------

Signed-off-by: Francesco Bigiarini <francesco.bigiarini@gmail.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-29 22:36:19 +01:00

189 lines
7.2 KiB
Makefile

# The Mojibake library
#
# This file is distributed under the MIT License. See LICENSE for details.
CPPFLAGS ?= -DNDEBUG
CFLAGS ?= -O3
CXXFLAGS ?= -O3
CARGO ?= cargo
ELIXIR ?= elixir
GO ?= go
ZIG ?= zig
FEATURE_CHARACTER_NAMES ?= 1
FEATURE_COLLATION ?= 1
FEATURE_IDNA ?= 1
FEATURE_SECURITY ?= 1
EXAMPLE_BUILD_DIR = build
# Amalgamation files
AMALGAMATION_DIR = ../build-amalgamation
AMALGAMATION_HEADER = $(AMALGAMATION_DIR)/mojibake.h
AMALGAMATION_SOURCE = $(AMALGAMATION_DIR)/mojibake.c
# C files
EXAMPLE_SOURCE = example.c
# C++ files
CPP_EXAMPLE_SOURCE = cpp/example.cpp
CPP_WRAPPER = ../src/cpp/mojibake.hpp
CPP_EXAMPLE = $(EXAMPLE_BUILD_DIR)/example-cpp
CPP_EXAMPLE_DEPS = $(CPP_EXAMPLE_SOURCE) $(CPP_WRAPPER) $(AMALGAMATION_HEADER) \
$(AMALGAMATION_SOURCE)
# Python files
PYTHON_EXAMPLE_SOURCE = python/example.py
ifeq ($(OS),Windows_NT)
PYTHON_LIBRARY = $(EXAMPLE_BUILD_DIR)/mojibake.dll
PYTHON_SHARED_FLAGS = -shared
else ifeq ($(shell uname -s),Darwin)
PYTHON_LIBRARY = $(EXAMPLE_BUILD_DIR)/libmojibake.dylib
PYTHON_SHARED_FLAGS = -dynamiclib
else
PYTHON_LIBRARY = $(EXAMPLE_BUILD_DIR)/libmojibake.so
PYTHON_SHARED_FLAGS = -shared -fPIC
endif
# Rust files. The example uses the safe crate in ../src/rust, which builds the C sources itself.
RUST_MANIFEST = rust/Cargo.toml
RUST_TARGET_DIR = $(EXAMPLE_BUILD_DIR)/rust
RUST_EXAMPLE = $(EXAMPLE_BUILD_DIR)/example-rust
RUST_FEATURES = $(strip \
$(if $(filter 1,$(FEATURE_CHARACTER_NAMES)),mojibake-rs/names) \
$(if $(filter 1,$(FEATURE_COLLATION)),mojibake-rs/collation) \
$(if $(filter 1,$(FEATURE_IDNA)),mojibake-rs/idna) \
$(if $(filter 1,$(FEATURE_SECURITY)),mojibake-rs/security))
# Zig files
ZIG_EXAMPLE_SOURCE = zig/example.zig
ZIG_BINDINGS = $(EXAMPLE_BUILD_DIR)/mojibake.zig
ZIG_EXAMPLE = $(EXAMPLE_BUILD_DIR)/example-zig
ZIG_EXAMPLE_DEPS = $(ZIG_EXAMPLE_SOURCE) $(ZIG_BINDINGS) $(AMALGAMATION_SOURCE)
GENERATE_DIR = ../utils/generate
EXAMPLE_DEPS = $(EXAMPLE_SOURCE) $(AMALGAMATION_HEADER) $(AMALGAMATION_SOURCE)
# Features
EXAMPLE_CPPFLAGS = -DMJB_FEATURE_CHARACTER_NAMES=$(FEATURE_CHARACTER_NAMES) \
-DMJB_FEATURE_COLLATION=$(FEATURE_COLLATION) \
-DMJB_FEATURE_IDNA=$(FEATURE_IDNA) \
-DMJB_FEATURE_SECURITY=$(FEATURE_SECURITY)
NO_CHARACTER_NAMES_CPPFLAGS = -DMJB_FEATURE_CHARACTER_NAMES=0 \
-DMJB_FEATURE_COLLATION=$(FEATURE_COLLATION) \
-DMJB_FEATURE_IDNA=$(FEATURE_IDNA) \
-DMJB_FEATURE_SECURITY=$(FEATURE_SECURITY)
NO_COLLATION_CPPFLAGS = -DMJB_FEATURE_CHARACTER_NAMES=$(FEATURE_CHARACTER_NAMES) \
-DMJB_FEATURE_COLLATION=0 \
-DMJB_FEATURE_IDNA=$(FEATURE_IDNA) \
-DMJB_FEATURE_SECURITY=$(FEATURE_SECURITY)
NO_IDNA_CPPFLAGS = -DMJB_FEATURE_CHARACTER_NAMES=$(FEATURE_CHARACTER_NAMES) \
-DMJB_FEATURE_COLLATION=$(FEATURE_COLLATION) \
-DMJB_FEATURE_IDNA=0 \
-DMJB_FEATURE_SECURITY=$(FEATURE_SECURITY)
NO_SECURITY_CPPFLAGS = -DMJB_FEATURE_CHARACTER_NAMES=$(FEATURE_CHARACTER_NAMES) \
-DMJB_FEATURE_COLLATION=$(FEATURE_COLLATION) \
-DMJB_FEATURE_IDNA=$(FEATURE_IDNA) \
-DMJB_FEATURE_SECURITY=0
# Basic example program without optional features
NO_FEATURES_CPPFLAGS = -DMJB_FEATURE_CHARACTER_NAMES=0 -DMJB_FEATURE_COLLATION=0 \
-DMJB_FEATURE_IDNA=0 -DMJB_FEATURE_SECURITY=0
all: example example-no-character-names example-no-collation example-no-idna example-no-security \
example-no-features
# Compile example program
example: $(EXAMPLE_DEPS) | $(EXAMPLE_BUILD_DIR)
$(CC) $(CPPFLAGS) $(EXAMPLE_CPPFLAGS) $(CFLAGS) -o $(EXAMPLE_BUILD_DIR)/$@ \
$(EXAMPLE_SOURCE) $(AMALGAMATION_SOURCE)
# Compile the C++ example program
example-cpp: $(CPP_EXAMPLE_DEPS) | $(EXAMPLE_BUILD_DIR)
$(CXX) $(CPPFLAGS) $(EXAMPLE_CPPFLAGS) $(CXXFLAGS) -std=c++20 -x c++ \
-I $(AMALGAMATION_DIR) -o $(CPP_EXAMPLE) $(CPP_EXAMPLE_SOURCE) \
$(AMALGAMATION_SOURCE)
# Build the shared library used by the Python example
example-python: $(PYTHON_EXAMPLE_SOURCE) $(PYTHON_LIBRARY)
$(PYTHON_LIBRARY): $(AMALGAMATION_HEADER) $(AMALGAMATION_SOURCE) | $(EXAMPLE_BUILD_DIR)
$(CC) $(CPPFLAGS) $(EXAMPLE_CPPFLAGS) $(CFLAGS) -DMJB_SHARED=1 \
-DMJB_BUILDING_LIBRARY=1 $(PYTHON_SHARED_FLAGS) -o $@ $(AMALGAMATION_SOURCE)
# Compile the Rust example program with Cargo and the mojibake crate
example-rust: | $(EXAMPLE_BUILD_DIR)
$(CARGO) build --release --manifest-path $(RUST_MANIFEST) \
--target-dir $(RUST_TARGET_DIR) --no-default-features \
--features "$(RUST_FEATURES)"
cp $(RUST_TARGET_DIR)/release/example-rust$(if $(filter Windows_NT,$(OS)),.exe) \
$(RUST_EXAMPLE)$(if $(filter Windows_NT,$(OS)),.exe)
# Run the Elixir example script. Mix.install compiles the package in ../src/elixir, NIF included,
# into its own cache on the first run.
example-elixir:
$(ELIXIR) elixir/example.exs
# Compile the Go example program. cgo compiles the C sources in ../src with the package. The
# FEATURE_* variables map to the package's mojibake_no_* build tags.
comma = ,
GO_TAGS = $(subst $(eval) ,$(comma),$(strip \
$(if $(filter 0,$(FEATURE_CHARACTER_NAMES)),mojibake_no_names) \
$(if $(filter 0,$(FEATURE_COLLATION)),mojibake_no_collation) \
$(if $(filter 0,$(FEATURE_IDNA)),mojibake_no_idna) \
$(if $(filter 0,$(FEATURE_SECURITY)),mojibake_no_security)))
example-go: | $(EXAMPLE_BUILD_DIR)
cd go && $(GO) build -tags "$(GO_TAGS)" \
-o ../$(EXAMPLE_BUILD_DIR)/example-go$(if $(filter Windows_NT,$(OS)),.exe) .
# Compile the Zig example program
example-zig: $(ZIG_EXAMPLE_DEPS) | $(EXAMPLE_BUILD_DIR)
$(ZIG) build-exe --dep mojibake -I $(AMALGAMATION_DIR) $(AMALGAMATION_SOURCE) \
-Mroot=$(ZIG_EXAMPLE_SOURCE) -Mmojibake=$(ZIG_BINDINGS) \
-lc -femit-bin=$(ZIG_EXAMPLE)
# Generate Zig bindings from the amalgamation header
$(ZIG_BINDINGS): $(AMALGAMATION_HEADER) | $(EXAMPLE_BUILD_DIR)
$(ZIG) translate-c $(AMALGAMATION_HEADER) -I $(AMALGAMATION_DIR) > $@.tmp
mv $@.tmp $@
# Compile example program without Unicode character name tables
example-no-character-names: $(EXAMPLE_DEPS) | $(EXAMPLE_BUILD_DIR)
$(CC) $(CPPFLAGS) $(NO_CHARACTER_NAMES_CPPFLAGS) $(CFLAGS) \
-o $(EXAMPLE_BUILD_DIR)/$@ $(EXAMPLE_SOURCE) $(AMALGAMATION_SOURCE)
# Compile example program without Unicode collation support
example-no-collation: $(EXAMPLE_DEPS) | $(EXAMPLE_BUILD_DIR)
$(CC) $(CPPFLAGS) $(NO_COLLATION_CPPFLAGS) $(CFLAGS) -o $(EXAMPLE_BUILD_DIR)/$@ \
$(EXAMPLE_SOURCE) $(AMALGAMATION_SOURCE)
# Compile example program without IDNA support
example-no-idna: $(EXAMPLE_DEPS) | $(EXAMPLE_BUILD_DIR)
$(CC) $(CPPFLAGS) $(NO_IDNA_CPPFLAGS) $(CFLAGS) -o $(EXAMPLE_BUILD_DIR)/$@ \
$(EXAMPLE_SOURCE) $(AMALGAMATION_SOURCE)
# Compile example program without Unicode security support
example-no-security: $(EXAMPLE_DEPS) | $(EXAMPLE_BUILD_DIR)
$(CC) $(CPPFLAGS) $(NO_SECURITY_CPPFLAGS) $(CFLAGS) -o $(EXAMPLE_BUILD_DIR)/$@ \
$(EXAMPLE_SOURCE) $(AMALGAMATION_SOURCE)
# Compile example program without optional features
example-no-features: $(EXAMPLE_DEPS) | $(EXAMPLE_BUILD_DIR)
$(CC) $(CPPFLAGS) $(NO_FEATURES_CPPFLAGS) $(CFLAGS) -o $(EXAMPLE_BUILD_DIR)/$@ \
$(EXAMPLE_SOURCE) $(AMALGAMATION_SOURCE)
$(EXAMPLE_BUILD_DIR):
mkdir -p $@
$(AMALGAMATION_HEADER) $(AMALGAMATION_SOURCE):
mkdir -p $(AMALGAMATION_DIR)
cd $(GENERATE_DIR) && npm run generate -- amalgamation
clean:
rm -rf $(EXAMPLE_BUILD_DIR)
.PHONY: all example example-cpp example-elixir example-go example-python example-rust example-zig \
example-no-character-names example-no-collation example-no-idna \
example-no-security example-no-features clean