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>
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.
src/unicode-data.hfilesrc/wasm/mojibake.d.tsfilesrc/wasm/locales.tsfilesrc/rust/mojibake-sys/src/functions.rsfiletests/example.candtests/cpp/example.cppfilesAPI.mdfilesrc/mojibake.hfunctions listsrc/unicode.handsrc/wasm/unicode.tsenumerationssrc/locales.henumeration andsrc/locales.ctablessrc/properties.ctablesmjb_bidi_bracketsandmjb_bidi_mirroringarrays insrc/bidi.csrc/CMakeLists.txtsource 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:
CMake3.14 or newerNode.jsandnpmmake(usually already included in the release)curlandunzip(usually already included in the release)
On Windows 10/11:
- The MSVC compiler. Build Tools for Visual Studio is enough
CMake3.14 or newerNode.jsandnpm
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 lintchecks formatting (it uses Apple'sxcrun clang-format, so it is macOS-only; on other platforms runclang-format --dry-run --Werrordirectly)make tidyruns clang-tidy with.clang-tidy; setCLANG_TIDYto select a specific binary- Record user-visible changes under
[Unreleased]inCHANGELOG.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
- Fork the repository and create a new branch for your feature/fix
- Write clear commit messages explaining your changes
- Sign off every commit as described above
- Add/update tests to cover your changes
- Ensure all tests pass before submitting
- Submit a pull request with a description of your changes
Testing
You can run tests in multiple ways. On POSIX platforms:
make testrun tests compiled as C (pass a name filter withmake test ARGS="-f normalization")make test-cpprun tests as C++make test-release/make test-cpp-releaserun optimized C or C++ buildsmake test-asanrun tests with AddressSanitizermake test-ubsanrun tests with UndefinedBehaviorSanitizermake test-no-names,make test-no-collation,make test-no-idna,make test-no-securityrun tests with one optional feature disabledmake test-minimal/make test-cpp-minimalrun C or C++ tests with every optional feature disabledmake ctestrun tests using ctest, including the CLI output checksmake ctest-cpprun C and C++ tests usingctestmake test-wasmrebuild the WASM module with Emscripten, build the TypeScript package, and test the WASM API in Node and a local Chromium-based browser (setMJB_BROWSERif it is not on the default search path)make test-rustrun rustfmt, clippy, and the Rust crates' tests
Groups of the targets above:
make test-nativerunstestandtest-cppmake test-optimizedrunstest-releaseandtest-cpp-releasemake test-featuresruns every feature-disabled configurationmake test-sanitizersrunstest-asanandtest-ubsanmake test-allruns all of the local native configurations
Windows 10/11 (Makefile.nmake provides the same targets):
nmake /F Makefile.nmake testrun tests compiled as Cnmake /F Makefile.nmake test-cpprun tests compiled as C++nmake /F Makefile.nmake test-asanrun tests withAddressSanitizernmake /F Makefile.nmake test-ubsanrun tests withUndefinedBehaviorSanitizernmake /F Makefile.nmake ctestrun tests usingctestnmake /F Makefile.nmake ctest-cpprun C and C++ tests usingctest
Docker:
make test-dockerrun tests on Alpine Linux, using DockerFUZZ_TIME=60 make fuzzfuzz 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 withmake 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:
MOJIBAKE_SRC_DIR, when it is set- the repository's
src/directory, when the crate is built inside a Mojibake checkout mojibake-sys/csrc/, the git-ignored copy thatmake vendor-rustmakes 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:
-
Set
VERSIONand runmake update-version. It sets the workspace version inCargo.toml, themojibake-sysdependency ofmojibake-rs, and both entries inCargo.lock. -
Commit the release and check that
make test-rustandmake package-rustpass. -
Publish from the same clean checkout. Cargo uploads
mojibake-sysfirst, thenmojibake-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
_intovariants and streaming decoders overmjb_decode_next, if a use case appears;String,Cow, andstriterators cover them today - route
mjb_set_allocatorthroughstd::alloc.mjb_reallocandmjb_freealready pass the exact block size thatLayoutneeds, so owned results could becomeStrings 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
_intovariants,mjb_set_allocator, and streaming decoders overmjb_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:
MOJIBAKE_SRC_DIR, when it is set- the repository's
src/directory, when the package is built inside a Mojibake checkout c_src/mojibake/, the git-ignored copy thatmake vendor-elixirmakes 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:
-
Set
VERSIONand runmake update-version. It sets the package version inmix.exs, the README, and the version doctest. -
Commit the release and check that
make test-elixirandmake package-elixirpass. -
Publish the GitHub release
v<VERSION>. TheElixir precompiled NIFsworkflow uploads the five NIFs to it; wait for it to finish, or run it on the tag withworkflow_dispatch. -
From the same clean checkout, download the NIFs to write
checksum.exs, then publish.make vendor-elixirputs the C sources andLICENSEin the package, whichmix hex.publishuploads along withchecksum.exsand the docs:(cd src/elixir && mix elixir_make.checksum --all) make vendor-elixir (cd src/elixir && mix hex.publish)mix elixir_make.checksum --allfails if a NIF is missing from the release.mix hex.publishlists the package files and asks for confirmation; check thatchecksum.exs,c_src/mojibake/, andLICENSEare 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.