CHAPTER 2THE CGULL COMMAND5

CHAPTER 2The cgull Command

The cgull command makes projects, builds them, and runs them. It is installed with the project, so inside one it is npx cgull; the project's package.json also has npm run scripts for the common cases. cgull --help prints a summary of what follows, and cgull --version the version.

2.1 cgull new

cgull new dir

Makes a new project in dir, which must not exist or must be empty. The project's name, used as the Worker's name, is the directory's name in lower case. It contains:

src/main.cThe program: a page with a form, a route with a parameter, and a JSON endpoint.
public/Static files, served as they are (Section 7.3).
wrangler.jsoncThe Worker's configuration.
package.jsonThe dependencies, cgull and wrangler, and the scripts build, dev and deploy.
.gitignoreLeaves out node_modules/, build/ and .wrangler/.

Run npm install in it before anything else.

2.2 cgull build

cgull build [--debug]
cgull build --native

Compiles every .c file under src/, in any subdirectory, together with the cgull runtime, into build/app.wasm. Next to it, it writes build/worker.js, the Worker's entry point, and build/shim.js, which runs the program (Chapter 8). Don't edit these; they are rewritten on every build. wrangler runs cgull build by itself before dev and deploy, so you seldom need to.

The compiler is called with these options, then any from package.json (Section 2.7):

--target=wasm32-wasip1 -std=c17 -Wall -Wextra -Wno-unused-parameter
-DCGULL_VERSION="0.1.0" -Iruntime -Isrc -O2 -Wl,--strip-all

-Isrc means headers in src/ can be included with #include "name.h" from anywhere in the tree. Unused parameters are not warned about, since most handlers don't use one of theirs.

With --debug it compiles with -O0 -g and keeps the symbols, so a crash in the Worker's logs names the function it happened in.

With --native it builds the program for the computer you're on instead, as build/app, using the C compiler in $CC (or cc). The result is a CGI program; see Section 8.4 for what it is good for.

2.3 cgull run

cgull run [options] path

Builds the program, sends it one request, and prints the response body. No server is started and nothing is deployed, so it is the fastest way to see what a handler does. The path may include a query string; quote it from the shell.

-X, --method methodThe method. GET, or POST if there is a body.
-H, --header 'Name: value'Adds a request header. May be repeated.
-d, --data bodyThe request body. @file reads it from a file.
-e, --env NAME=valueSets a Worker variable for cg_env. May be repeated.
-i, --includePrints the status line and headers before the body.
--nativeBuilds and runs the native program instead of the WebAssembly.
--debugBuilds as cgull build --debug does.

As with curl, a body sent with -d is labelled as a form, application/x-www-form-urlencoded, so cg_form can read it. For any other kind of body, give its type:

$ npx cgull run -d name=Ada /greet
$ npx cgull run -d '{"name":"Ada"}' -H 'content-type: application/json' /api/greet
$ npx cgull run -X PUT -d @photo.jpg -H 'content-type: image/jpeg' /photos/1

The request's host is localhost:8787. Variables from wrangler.jsonc are not read; pass the ones you need with -e.

2.4 cgull dev and deploy

cgull dev [wrangler options]
cgull deploy [wrangler options]

These run wrangler dev and wrangler deploy from the project's node_modules, passing on any options, such as --port 9000. npm run dev and npm run deploy do the same.

dev serves the Worker at http://localhost:8787 on workerd, the runtime Cloudflare's servers use, with the variables and static files from wrangler.jsonc. When a file in src/ changes, it rebuilds and reloads. deploy builds and uploads the Worker; the first time, wrangler asks you to log in. For everything else wrangler can do, such as secrets, custom domains and logs, see Cloudflare's documentation for it.

2.5 cgull setup

cgull setup

Prints where wasi-sdk is, downloading it first if it isn't anywhere. Builds do this by themselves, so you need it only to download the SDK ahead of time, say in a CI job's cache step. cgull looks for wasi-sdk in these places, in order:

  1. the directory in $WASI_SDK_PATH;
  2. ~/.cache/wasi-sdk/wasi-sdk-33.0-arch-os (or under $XDG_CACHE_HOME), which is where it downloads to;
  3. /opt/wasi-sdk.

It is pinned to wasi-sdk 33 and checks the download's SHA-256 checksum. There are builds for Linux and macOS, on x86-64 and ARM.

2.6 The Project

cgull finds the project by looking for a directory with both a package.json and a src/, starting from the current directory and going up. The wrangler.jsonc that cgull new writes looks like this:

{
    "name": "hello",
    "main": "build/worker.js",
    "compatibility_date": "2026-11-29",
    "build": { "command": "npx cgull build", "watch_dir": "src" },
    "assets": { "directory": "public" },
    "vars": { "SITE_NAME": "hello" }
}

main and build are what connect wrangler to cgull, and should be left as they are. assets and vars are yours to change or remove. Anything else wrangler understands can be added; bindings such as KV namespaces are allowed but can't yet be used from C.

2.7 Compiler Flags

Options for the compiler and linker go in package.json, under "cgull":

"cgull": {
    "cflags": ["-std=c23", "-DMAX_ITEMS=50"],
    "ldflags": ["-lm"]
}

cflags come after cgull's own, so they win where the two disagree. This book uses -std=c23, for #embed. ldflags come at the end of the command line. The same lists are used for native builds.

The runtime defines CGULL_VERSION, a string such as "0.1.0", for programs that want to show it.

Exercise 2-1. Run your program with cgull run --native, then with plain cgull run. Is the output the same? Should it be?

Set in Tinos and Courier Prime. Served by a C program on Cloudflare Workers, cgull 0.1.0. Source on GitHub, BSD licensed.