Publishing packages
A crossbind package is an npm package with a crossbind.config.js that apps list in their dependencies. You build it once per platform and publish the libraries; apps link them without compiling your code. Every @crossbind/port-* package is made this way.
Package types
- Prebuiltexport.type: 'cmake'
- The default. Compiled libraries per platform under
dist/prebuilt/, so the app compiles nothing of yours. The ports are prebuilt, and so is what the rest of this page builds. - Sourceexport.type: 'source'
- Your headers and sources, compiled in the app's own build.
- CMakeexport.type: 'cmake'
- Your own
CMakeLists.txt, withpaths.output: '.': the app adds it withadd_subdirectoryand builds it. Name the library target after theCROSSBIND_LIB_NAMEthe app's build passes in, since only that target'sPUBLICinclude directories reach the app. - Cargoexport.type: 'cargo'
- A Rust crate built per platform; see Rust crates.
Each C++ type has a template: npm create crossbind@beta -- my-lib Library Prebuilt, with Source or CMake in place of Prebuilt.
The package config
export.libName lists the archives the package ships, one per library (['ssl', 'crypto'] for OpenSSL); it defaults to general.name. paths.output is the folder you publish. types: true writes one declaration file for the package's headers and points package.json's types at it, so imports of mylib/<header>.h get types. The other export fields are in Configuration.
A package's targetSpecs reach every app that depends on it: the data and env it declares (Data files), and flags such as binary.addonFlags for a system library a Node.js addon must link, or cmake.compileOptions for a define your headers expect.
Build once per platform
Without -e, crossbind build makes only the libraries, which is what a package publishes: static archives, and shared libraries on Android. Run it for each platform you support; iOS and macOS build on a Mac, the rest in the toolchain images:
Each target gets a folder named <platform>-<arch>-<runtime>-<buildType>:
dist/prebuilt/CMakeLists.txt is how an app's build finds your libraries, and an import path after your package name resolves in the target's include/. An iOS build also writes <libName>.xcframework at the package root, and a .podspec beside it when the package has none.
Publish dist/, the xcframework and the podspec, and keep .crossbind/ out, as the ports' .npmignore does. A prepublishOnly script that runs the build keeps npm publish from shipping stale libraries.
One package, or a family
One package can carry every platform, as lib-prebuilt-matrix does, but then every app downloads all of them. The ports split each library into a family instead: a meta package, @crossbind/port-zlib, with the shared recipe and config, and one package per platform (-wasm, -android, -ios, -darwin, -linux, -linuxmusl, -win32, -wasi), each depending on the meta package and built with its own crossbind build -p <platform>.
general.alias makes the variants one library to an app: every variant names the meta package there, so @crossbind/port-zlib/zlib.h resolves to the variant in the app's dependencies that has libraries for the target. The config of zlib's wasm variant comes down to:
zlib writes the shared part once, in the meta package's mergeConfig.mjs, and each variant's config calls it; each variant's crossbind.build.js is one line, export { default } from '@crossbind/port-zlib/build.mjs';, re-exporting the recipe.
Wrapping an upstream project
To package a library you do not maintain, put a crossbind.build.js next to the config. crossbind build then downloads the tarball of the nativeVersion in package.json, checks its SHA-256, applies your patches and builds it with the upstream's own build system for each target. zlib's recipe, shortened:
getURL(version),sha256- Name the upstream tarball and pin it; a different download refuses to build.
buildType'cmake'(the default) or'configure'for autotools;configureProgramandmakePhasesadjust the two steps.getBuildParams(target, depPaths)-Dflags or configure arguments, per target.envCFLAGS,LDFLAGSand the like for the upstream build, as a list or a function of the target.patches- Upstream fixes the pinned release lacks, as the diffs upstream published; one that no longer applies stops the build.
replaceList,sourceReplaceList(target, depPaths)- Patch the extracted source once, or each target's copy.
copyToSource,copyToDist- Add a file to the source before the build, or ship one in each target's folder.
beforeRun(cmakeDir)- Commands to run first, such as
autoreconf -fi. getExtraLibs(target)- Link libraries the dependencies do not bring.
bundled- Third-party code the upstream compiles in, per platform, so its license is listed and shipped.
The build already puts your native dependencies' include and library folders on CFLAGS and LDFLAGS; depPaths.<libName>.header and .lib name them per target for flags like GDAL's -DZLIB_INCLUDE_DIR. When a recipe builds a static library on Android, say so in the config with targetSpecs: [{ platform: 'android', specs: { libType: 'static' } }], as zlib does, so apps link the archive instead of looking for a .so. Every argument: crossbind-build.md.
Native dependencies
A package that links another declares it twice, each time with the variant that matches its own (a -wasm package depends on -wasm packages). The config's dependencies is what crossbind links and puts on the include path. The dependencies of package.json install it with your package and, in a workspace, build it before yours. Neither does the other's job.
Rust crates
A Rust crate ships as one package for every platform. Set export.type: 'cargo' and point export.crate at the folder holding Cargo.toml. The crate stays plain Rust: crossbind generates the bindings from the public items of its src/lib.rs.
crossbind build -p <platform> builds the crate for each target and puts the static library in dist/prebuilt/<target>/lib/, like any prebuilt; WASI is skipped. Add @crossbind/core-embind-rust as a dev dependency. bindings.vectors declares standalone Vec<T> classes of i32, f64 or bool.
Apps import the package by name, as in import { initNative, RustyCounter } from '@crossbind/embind-rust-demo', with types mapped as on the Rust page. An app that finds no prebuilt for its target, or one built against another embind-rs, builds it first, with the toolchain any Rust build needs (Rust).
Licenses
An app that ships your package takes on its license and those of everything linked in, so make that checkable, as the ports do:
- Pin the upstream source with
sha256in the recipe. A mismatch refuses the build; without a pin the download is not checked. - Ship the upstream license text as
LICENSEin every package that carries binaries, and set the npmlicensefield to the wrapped library's license as an SPDX expression. - In a family, record the upstream license once, in the meta package's
package.jsonundercrossbind.upstream.license:declared,selectedfor your pick of anORexpression,filesandcopyright.crossbind licensesreads it there; a package outside a family it lists from itslicensefield andLICENSEfile. - Declare third-party code the upstream compiles in under
bundledin the recipe. The build copies its license texts todist/licenses/<name>/, andcrossbind licenses --platform <platform>lists it.
Run crossbind licenses --check in each platform package before you publish. Apps then write notices and an SBOM for everything they link with --notices and --sbom (CLI).