Files
mojibake/CONTRIBUTING.md
Francesco BigiariniandClaude Opus 5.5 5dea3b999f Go: add a Windows CI job (#203)
cgo does not support MSVC, so the job installs MinGW-w64 GCC (UCRT64)
with MSYS2, puts it on PATH, and runs the Go tests with the optional
features on and off, plus the example. Document how to test on Windows.

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

20 KiB

Contributing to Mojibake

This document provides guidelines and information for contributors.

Building from source

Some .h/.c/.ts files are automatically generated by the build scripts from Unicode standard files. If you make changes to these files, they will likely be rewritten when make generate is run. If you see "This file/enum/function is automatically generated. Do not edit", don't do it.

  1. src/unicode-data.h file
  2. src/wasm/mojibake.d.ts file
  3. src/wasm/locales.ts file
  4. src/rust/mojibake-sys/src/functions.rs file
  5. tests/example.c and tests/cpp/example.cpp files
  6. API.md file
  7. src/mojibake.h functions list
  8. src/unicode.h and src/wasm/unicode.ts enumerations
  9. src/locales.h enumeration and src/locales.c tables
  10. src/properties.c tables
  11. mjb_bidi_brackets and mjb_bidi_mirroring arrays in src/bidi.c
  12. src/CMakeLists.txt source list

Public function declarations, their documentation, and the C and C++ examples come from utils/generate/functions.ts. Edit that file and run make generate instead of editing the outputs.

Requirements

Mojibake requires a C11 compiler. Building the C sources as C++ requires C++17, and the header-only C++ wrapper requires C++20 with concepts and <ranges>. The minimum supported compiler versions for the library are:

Toolchain Minimum version Language modes
GCC 9 C11 and C++17
LLVM Clang 10 C11 and C++17
Apple Clang 16 C11 and C++17
MSVC 19.30 (Visual Studio 2022) C11 and C++17

CI builds and smoke-tests both language modes with the exact GCC 9 and LLVM Clang 10 baselines. Those compilers lack C++20 support, so the test suite skips the C++ wrapper tests there. A C++ job runs them only when its compiler passes the C++20 check in tests/CMakeLists.txt (GCC 10 and later do; Clang 14 with libstdc++ does not). The macOS and Windows jobs continuously test the supported Apple Clang and MSVC families using the hosted platform toolchains. MinGW-w64 and Emscripten are also tested as rolling compatibility targets.

On POSIX systems: Linux, macOS, FreeBSD, OpenBSD, NetBSD, Haiku:

  • CMake 3.14 or newer
  • Node.js and npm
  • make (usually already included in the release)
  • curl and unzip (usually already included in the release)

On Windows 10/11:

POSIX

At the very first run:

cd utils/generate
npm ci

Then from root folder:

make
make test

Windows

At the very first run, from a Visual Studio developer command prompt:

cd .\utils\generate
npm ci

Then from root folder:

nmake /F Makefile.nmake
nmake /F Makefile.nmake test

WebAssembly

To build the WASM module you need Emscripten and generate assets with:

make wasm

or:

nmake /F Makefile.nmake wasm

This builds the module in build-wasm/, copies mojibake.js and mojibake.wasm into src/wasm/, and regenerates the demo site in src/site/. Use make build-api to also build the TypeScript package.

You can run a local server with:

make watch-site
# Open http://localhost:6251

Run the JavaScript API server with:

make watch-api

Code Style

  • We follow a consistent code style using clang-format
  • Use 4 spaces for indentation (no tabs)
  • Maximum line length is 100 characters
  • See .clang-format for complete style configuration
  • make lint checks formatting (it uses Apple's xcrun clang-format, so it is macOS-only; on other platforms run clang-format --dry-run --Werror directly)
  • make tidy runs clang-tidy with .clang-tidy; set CLANG_TIDY to select a specific binary
  • Record user-visible changes under [Unreleased] in CHANGELOG.md

Developer Certificate of Origin

By contributing to Mojibake, you certify that your contribution complies with the Developer Certificate of Origin 1.1.

Sign off every commit with your real name and email address:

git commit --signoff

This adds a trailer to the commit message:

Signed-off-by: Your Name <your.email@example.com>

The sign-off must match the commit author's name and email address. It certifies the contribution; it is not the same as cryptographically signing a commit with git commit -S.

Pull Request Process

  1. Fork the repository and create a new branch for your feature/fix
  2. Write clear commit messages explaining your changes
  3. Sign off every commit as described above
  4. Add/update tests to cover your changes
  5. Ensure all tests pass before submitting
  6. Submit a pull request with a description of your changes

Testing

You can run tests in multiple ways. On POSIX platforms:

  1. make test run tests compiled as C (pass a name filter with make test ARGS="-f normalization")
  2. make test-cpp run tests as C++
  3. make test-release / make test-cpp-release run optimized C or C++ builds
  4. make test-asan run tests with AddressSanitizer
  5. make test-ubsan run tests with UndefinedBehaviorSanitizer
  6. make test-no-names, make test-no-collation, make test-no-idna, make test-no-security run tests with one optional feature disabled
  7. make test-minimal / make test-cpp-minimal run C or C++ tests with every optional feature disabled
  8. make ctest run tests using ctest, including the CLI output checks
  9. make ctest-cpp run C and C++ tests using ctest
  10. make test-wasm rebuild the WASM module with Emscripten, build the TypeScript package, and test the WASM API in Node and a local Chromium-based browser (set MJB_BROWSER if it is not on the default search path)
  11. make test-rust run rustfmt, clippy, and the Rust crates' tests

Groups of the targets above:

  • make test-native runs test and test-cpp
  • make test-optimized runs test-release and test-cpp-release
  • make test-features runs every feature-disabled configuration
  • make test-sanitizers runs test-asan and test-ubsan
  • make test-all runs all of the local native configurations

Windows 10/11 (Makefile.nmake provides the same targets):

  1. nmake /F Makefile.nmake test run tests compiled as C
  2. nmake /F Makefile.nmake test-cpp run tests compiled as C++
  3. nmake /F Makefile.nmake test-asan run tests with AddressSanitizer
  4. nmake /F Makefile.nmake test-ubsan run tests with UndefinedBehaviorSanitizer
  5. nmake /F Makefile.nmake ctest run tests using ctest
  6. nmake /F Makefile.nmake ctest-cpp run C and C++ tests using ctest

Docker:

  1. make test-docker run tests on Alpine Linux, using Docker
  2. FUZZ_TIME=60 make fuzz fuzz the public API with libFuzzer, using Docker

Running the test binary by hand:

The tests read the Unicode data files under unicode-data/ with paths relative to the repository root, so at startup mojibake-test changes into that directory. It is resolved in this order: the MJB_TEST_SOURCE_DIR environment variable if set and non-empty, then the repository root recorded at build time by tests/CMakeLists.txt, then the current directory.

MJB_TEST_SOURCE_DIR=/path/to/mojibake /path/to/build-test/tests/mojibake-test -f normalization

Important information about testing when adding new code:

  • We "assert" something that is true, not something that is false
  • Add unit tests for any new functionality
  • Run the full test suite before submitting changes
  • Current test coverage is tracked in TESTS.md; regenerate it with make coverage
  • Aim to maintain or improve test coverage
  • Never remove a test, always add one

We use Attractor to run tests. Define a test case with ATT_TEST(name) { ... }. A new test file must be added to TEST_SOURCES in tests/CMakeLists.txt.

ATT_ASSERT(what_you_are_testing, expected_value, "Description");

Use ATT_ASSERT_STATUS for mjb_status values.

For example, imagine we have found a bug in the mjb_codepoint_count function that does not correctly count the codepoints in the "Hello, test" string and we fixed it. In tests/encoding.c, add a new assertion:

size_t count = 0;
mjb_text text = mjb_text_utf8("Hello, test", 11);
ATT_ASSERT_STATUS(mjb_codepoint_count(&text, MJB_MALFORMED_STOP, &count, NULL), MJB_STATUS_OK,
    "UTF-8 Hello, test status");
ATT_ASSERT(count, (size_t)11, "UTF-8 Hello, test");

Then run make test and make test-cpp to be sure all tests are working.

Rust crates

The Rust crates live in src/rust/; their user-facing documentation is src/rust/README.md.

Building and testing

From the repository root:

make test-rust

This runs rustfmt, clippy, and the tests with all features on and with all features off.

The mojibake-sys build script looks for the C sources in this order:

  1. MOJIBAKE_SRC_DIR, when it is set
  2. the repository's src/ directory, when the crate is built inside a Mojibake checkout
  3. mojibake-sys/csrc/, the git-ignored copy that make vendor-rust makes for packaging

A checkout wins over csrc/, so a leftover copy is never compiled in-tree. The source list comes from the generated SOURCES block of CMakeLists.txt.

The declarations in mojibake-sys/src/functions.rs are generated from utils/generate/functions.ts by make generate; do not edit them. The types, constants, and layout tests in mojibake-sys/src/lib.rs and mojibake-sys/shim/layout.c are written by hand.

Packaging and publishing

make package-rust copies src/*.c, src/*.h, src/CMakeLists.txt, and LICENSE into mojibake-sys/csrc/, then runs cargo package --workspace and cargo publish --workspace --dry-run. Both crates are packaged and each is built from its .crate file, with mojibake-rs resolving the unpublished mojibake-sys from Cargo's local staging registry. CI runs it on every push. It needs Cargo 1.90 or newer for workspace packaging; users of the crates still need only Rust 1.77.

Cargo treats the git-ignored csrc/ as uncommitted, so the target always passes --allow-dirty and instead refuses to run when src/ or LICENSE has uncommitted or untracked changes. Set ALLOW_DIRTY=1 to package a work in progress. Extra Cargo flags go in ARGS.

To release the crates:

  1. Set VERSION and run make update-version. It sets the workspace version in Cargo.toml, the mojibake-sys dependency of mojibake-rs, and both entries in Cargo.lock.

  2. Commit the release and check that make test-rust and make package-rust pass.

  3. Publish from the same clean checkout. Cargo uploads mojibake-sys first, then mojibake-rs:

    make vendor-rust
    (cd src/rust && cargo publish --workspace --locked --allow-dirty)
    

Not done yet

  • expose the remaining APIs in the safe crate: the _into variants and streaming decoders over mjb_decode_next, if a use case appears; String, Cow, and str iterators cover them today
  • route mjb_set_allocator through std::alloc. mjb_realloc and mjb_free already pass the exact block size that Layout needs, so owned results could become Strings without a copy

Go package

The Go package lives in src/go/; its user-facing documentation is src/go/README.md. It needs Go 1.23 or newer (the go directive of go.mod) and a C compiler.

Building and testing

From the repository root:

make test-go

This checks gofmt, runs go vet, and runs the tests and examples with all optional features on and with all of them off, with the C sources compiled with warnings as errors (GO_CFLAGS, -O2 -g -Wall -Werror by default). Extra go test flags go in ARGS, for example make test-go ARGS="-run Conformance -v". Inside src/go, plain go test works too. CI also runs the tests with -race and GOEXPERIMENT=cgocheck2, which checks every pointer passed to C.

On Windows, cgo needs GCC: MSVC is not supported. The go-windows CI job installs mingw-w64-ucrt-x86_64-gcc with MSYS2, puts its ucrt64\bin on PATH, and runs the tests with features on and off. To do the same locally, run go test ./src/go from a shell that has MinGW-w64 GCC on PATH, for example the MSYS2 UCRT64 shell with Go installed. make test-go needs a POSIX shell and make, so it is not the Windows entry point.

The module is the repository root (go.mod, module github.com/zaerl/mojibake), so the module zip that go get downloads holds the C sources in src/ next to the package. Release tags such as v0.9.0 are therefore also the Go module versions, and nothing needs vendoring or updating at release time. examples/go has its own go.mod, with a replace that points at the checkout, so it stays out of the published module; build it with make -C examples example-go.

cgo compiles every .c file in the package directory, so src/go/mojibake.c includes the library sources, mojibake.c first, as one translation unit, like the amalgamation. TestUnitySources fails when its list differs from the SOURCES block of src/CMakeLists.txt; add new sources to both. The optional features are the mojibake_no_* build tags of feature_no_*.go, which add -DMJB_FEATURE_*=0 to the cgo flags.

shim.h holds the static inline C helpers that the Go files call. They take a Go string or byte slice as a pointer and a length and build the mjb_text on the C stack, because the cgo pointer rules forbid passing Go memory that holds a Go pointer. When a transform borrows its input (mjb_result.owned is false), the helper clears the output pointer, and the Go side returns its own input instead.

The Go constants of Category, BidiClass, DecompositionType, and the other enums are bound to the C enum values, and compile-time checks at the end of char.go fail the build when a C enum outgrows the name tables next to them.

Not done yet

  • the _into variants, mjb_set_allocator, and streaming decoders over mjb_decode_next, if a use case appears

Elixir package

The Elixir package lives in src/elixir/; its user-facing documentation is src/elixir/README.md. It needs Elixir 1.15 or newer, Erlang/OTP, a C compiler, and make, or MSVC and nmake on Windows.

Building and testing

From the repository root:

make test-elixir

This checks formatting, runs the tests with all optional features on and with all of them off, and builds the docs, with warnings as errors for both the Elixir code and the C sources (ELIXIR_CFLAGS, -O2 -Werror by default). Extra mix test flags go in ARGS, for example make test-elixir ARGS="--only conformance". Inside src/elixir, plain mix test works too.

On Windows, run nmake /F Makefile.nmake test-elixir from an x64 Native Tools Command Prompt; ELIXIR_CFLAGS defaults to /O2 /WX there. vendor-elixir and package-elixir have nmake versions too.

The NIF is c_src/mojibake_nif.c, built through elixir_make by src/elixir/Makefile, or by src/elixir/Makefile.win with MSVC and nmake on Windows. mix.exs hands Makefile.win its source paths and directories in Windows form, since nmake cannot compute them; run mix there from PowerShell or a Developer Command Prompt, not Git Bash, whose link.exe hides the MSVC linker. Inside the repository the package always compiles the NIF. mix.exs finds the C sources in this order:

  1. MOJIBAKE_SRC_DIR, when it is set
  2. the repository's src/ directory, when the package is built inside a Mojibake checkout
  3. c_src/mojibake/, the git-ignored copy that make vendor-elixir makes for packaging

The source list comes from the generated SOURCES block of src/CMakeLists.txt. The optional C features follow config :mojibake, features: [...], or the MOJIBAKE_FEATURES environment variable (none or a comma-separated list), which wins.

Every NIF that takes text reschedules itself on a dirty CPU scheduler when the input is larger than 16 KiB. The C library allocates through enif_alloc, installed with mjb_set_allocator when the NIF loads.

lib/mojibake/unicode_data.ex holds the atom tables for categories, decomposition types, scripts, blocks, and properties, indexed by the C enums. It is generated by utils/generate/file-generators/elixir-unicode-data.ts during make generate, so do not edit it or format it. The bidi classes and the emoji sequence types and qualifications are hand-written C enums; their atom lists live in lib/mojibake/char.ex and lib/mojibake/emoji.ex, and _Static_asserts in the NIF fail the build when the enums change.

The conformance tests in test/conformance_test.exs, test/ucd_test.exs, and test/text_conformance_test.exs (bidi, collation, and IDNA) read unicode-data/; set MOJIBAKE_TEST_DATA to read the files from elsewhere. They are skipped when the files are missing.

A resolved bidi paragraph is an mjb_bidi_paragraph in a NIF resource, freed with mjb_bidi_paragraph_free by the resource destructor; a code upgrade takes the resource type over.

examples/elixir/example.exs uses the package through Mix.install with a path dependency. Run it with make -C examples example-elixir.

Packaging and publishing

make package-elixir copies src/*.c, src/*.h, src/CMakeLists.txt, and LICENSE into the package, runs mix hex.build --unpack into build-elixir-package/, and compiles and loads the unpacked package from its vendored sources (MOJIBAKE_BUILD=1 skips the precompiled NIF). Like package-rust, it refuses to run when src/ or LICENSE has uncommitted or untracked changes, unless ALLOW_DIRTY=1.

The package downloads a precompiled NIF with elixir_make and cc_precompiler, configured in mix.exs, and compiles from source when there is none for the platform. The NIFs are built for NIF version 2.16 (OTP 24 and 25), which every later OTP loads, for x86_64-linux-gnu, aarch64-linux-gnu, x86_64-apple-darwin, aarch64-apple-darwin, and x86_64-windows-msvc. .github/workflows/elixir-precompile.yml builds them on Ubuntu 22.04, macOS, and Windows, and checks with src/elixir/scripts/check_precompiled.exs that the unpacked package installs the NIF of the runner without compiling it. It runs on pull requests that touch the package or the C sources, and for a published release it uploads the NIFs to that release. To check archives yourself:

(cd src/elixir && MIX_ENV=prod ELIXIR_MAKE_CACHE_DIR=$PWD/precompiled mix elixir_make.precompile)
elixir src/elixir/scripts/check_precompiled.exs src/elixir/precompiled

checksum.exs lists the checksums of the NIFs of a release. It is git-ignored and written at release time; the package includes it when it exists, and mix hex.publish refuses to run without it.

To release the package:

  1. Set VERSION and run make update-version. It sets the package version in mix.exs, the README, and the version doctest.

  2. Commit the release and check that make test-elixir and make package-elixir pass.

  3. Publish the GitHub release v<VERSION>. The Elixir precompiled NIFs workflow uploads the five NIFs to it; wait for it to finish, or run it on the tag with workflow_dispatch.

  4. From the same clean checkout, download the NIFs to write checksum.exs, then publish. make vendor-elixir puts the C sources and LICENSE in the package, which mix hex.publish uploads along with checksum.exs and the docs:

    (cd src/elixir && mix elixir_make.checksum --all)
    make vendor-elixir
    (cd src/elixir && mix hex.publish)
    

    mix elixir_make.checksum --all fails if a NIF is missing from the release. mix hex.publish lists the package files and asks for confirmation; check that checksum.exs, c_src/mojibake/, and LICENSE are in the list.

Reporting Issues

  • Use the GitHub issue tracker
  • Provide a clear description of the issue
  • Include steps to reproduce if applicable
  • Note your environment/version information

License

By contributing to Mojibake, you agree that your contributions will be licensed under the MIT License. See LICENSE for details.

Questions?

Feel free to open an issue for any questions about contributing.

Thank you for helping.