.\" Made by website/plain/build.mjs from website/words/sg-build.md; change that file. .TH SG-BUILD 1 2026-11-28 "SG" "SG commands" .SH NAME sg build \- compiles through C: a program, the program of its tests, a library C calls, or an object without an operating system .SH SYNOPSIS .TP \fBsg build file.sg, sg build folder\fR a debug build of the program, named after the file without .sg, or after the folder .TP \fBsg build path \-o out\fR the same, written to out .TP \fBsg build path \-\-release\fR a release build: optimised, overflow wraps, no checks of a debug build .TP \fBsg build path \-\-test\fR the program that runs the tests, each in a process of its own; ./tests --only N runs test N alone .TP \fBsg build path \-\-library\fR a static library C links, libname.a (name.lib on Windows), and its header name.h .TP \fBsg build path \-\-shared\fR a shared library, libname.so (.dylib on macOS, name.dll on Windows), and its header .TP \fBsg build path \-\-freestanding\fR an object, name.o, for a machine without an operating system .TP \fBsg build path \-\-freestanding \-\-target triple\fR the same for another machine, compiled by Clang .TP \fBsg build path \-L folder, \-Lfolder\fR looks for the libraries of extern blocks in that folder first; given once for each folder .TP \fBsg build path \-\- flags\fR gives the C compiler these flags too, after sg's own .TP \fBCC=compiler sg build path\fR builds with that C compiler .TP \fBsg build \-\-help\fR prints this page .SH DESCRIPTION \fBsg build\fR checks the program as \fBsg\fR does, writes it as C, and has a C compiler make it. The C compiler is the one \fBCC\fR names, else \fBcc\fR on Linux and macOS and Clang on Windows. What the build makes on the way, the C and the objects, is in a folder of the temporary folder made new for it, which only its user opens, and removed at the end. \fBsg emit\-c\fR prints that C instead. .PP A debug build, the default, is compiled without optimisation (\fB\-O0\fR) and checks what the interpreter checks: integer overflow, a cast that does not fit, a shift past the bits of its type, raw memory read at an address not aligned for its type, and, when \fBmain\fR returns, the \fBshared\fR values still alive. It carries what the system's debugger reads: inside the program on Linux, in \fBname.dSYM\fR beside it on macOS, in \fBname.pdb\fR on Windows. A release build (\fB\-\-release\fR) is optimised (\fB\-O2\fR), lets overflow wrap and a cast keep the bits that fit, and counts nothing. What keeps memory safe is checked in both: an index out of range, division by zero, a conflict of borrows, \fBassert\fR and \fBpanic\fR\&. .PP The output goes to the current folder, whatever folder the source is in, or to the path \fB\-o\fR names, a file there being written over, unless it is a source: the program's own file, a file of SG or of C, or a header \fBsg build\fR did not write is never replaced. A program is named after its file without \fB\&.sg\fR, or after its folder, \fBsg build .\fR too. On Windows a program gets \fB\&.exe\fR\&. \fB\-\-test\fR builds the \fBtest\fR blocks instead of \fBmain\fR, as \fBsg test \-\-compiled\fR does; with \fB\-\-release\fR too, the tests wrap and count nothing. .PP A library is the \fBexport\fR functions of the program, which must have no \fBmain\fR\&. It is named after its folder or file: \fBsg build temps \-\-library\fR writes \fBlibtemps.a\fR and \fBtemps.h\fR, and says how a program of C links it. A shared library shows only its \fBexport\fR functions and is never unloaded. Named with \fB\-o\fR, the line says to link it by that name: \fB\-o out/libscore.so\fR gives \fB\-Lout \-lscore\fR, and a name \fB\-l\fR cannot find, one that does not start with \fBlib\fR or end with \fB\&.so\fR (\fB\&.dylib\fR on macOS), is linked by its path. \fB\-\-freestanding\fR writes an object with no \fBmain\fR and nothing of C's library, which the code that starts the machine links; with \fB\-\-target\fR the machine is another one, and the build uses Clang: the one \fBCC\fR names, else the first \fBclang\fR of the path. .PP A program and its tests write nothing when they are built. A library and an object write a line, \fBbuilt ...\fR, on the standard output. .PP \fB\-L folder\fR names a folder where the libraries of \fBextern\fR blocks are looked for first, and it is written into the program (its \fBrpath\fR on Linux and macOS), so the program finds them when it runs. On Windows the DLLs linked from those folders are copied beside the program. .PP The flags after \fB\-\-\fR go to the C compiler on every kind of build, after sg's own, so the last one written wins: \fB\-O3\fR over \fB\-O2\fR, \fB\-march=native\fR, \fB\-flto\fR, \fB\-g\fR, \fB\-fsanitize=address\fR, \fB\-l\fR, \fB\-I\fR, \fB\-Wl,\fR\&. Most go on quietly, unknown ones too. Some go on with a warning, written on every build: what changes fractional results (\fB\-ffast\-math\fR, \fB\-Ofast\fR, \fB\-ffp\-contract=fast\fR or \fBon\fR, \fB\-funsafe\-math\-optimizations\fR, \fB\-ffinite\-math\-only\fR, \fB\-fno\-signed\-zeros\fR, \fB\-freciprocal\-math\fR, \fB\-fassociative\-math\fR, \fB\-mfpmath=387\fR), what takes a guard away (\fB\-fno\-stack\-clash\-protection\fR, \fBexecstack\fR or \fBnorelro\fR for the linker), and a setting of SG's runtime (\fB\-DSG_...\fR, \fB\-USG_...\fR). Some are refused before anything is compiled: what sg decides itself (\fB\-o\fR, \fB\-c\fR, \fB\-S\fR, \fB\-E\fR, \fB\-x\fR, \fB\-std=\fR, \fB\-shared\fR, \fB\-static\fR, \fB\-\-target\fR) and what changes the size or the layout of C's types (\fB\-m16\fR, \fB\-m32\fR, \fB\-mx32\fR, \fB\-fshort\-enums\fR, \fB\-fpack\-struct\fR, \fB\-fshort\-wchar\fR, \fB\-funsigned\-char\fR, \fB\-fsigned\-char\fR). A flag of C turns off no check of SG: only \fB\-\-release\fR leaves out the checks of a debug build. .PP The C compiler writes its own messages as it goes, and when it builds nothing, sg says whose mistake it found: a mistake in a C file of the program's own (\fBextern "./native.c"\fR) is that file's, and its first errors are said again under sg's line; a compiler that ends without naming an error, or ends with 0 and writes nothing, may not be a C compiler at all; C that sg wrote and the compiler refused is a mistake in sg, to be reported. .PP The exit code is 0 when it is built; 1 when the checker refuses the program, or the C compiler cannot be run or does not build it; 2 when the command line is not one of the forms (a second path, a flag given twice, \fB\-o\fR or \fB\-\-target\fR with nothing after it, \fB\-\-target\fR without \fB\-\-freestanding\fR), a path or a folder of \fB\-L\fR or \fB\-o\fR is not there, the output is a folder or a source, or a flag of C is refused. .SH EXAMPLES .SS "A debug build and a release build" The file level/level.sg: .PP .RS 4 .nf void main() { byte level = 250 for _ in 0..10 { level += 1 } print(level) } .fi .RE .PP .RS 4 .nf $ cd level $ sg build level.sg $ ./level panic: overflow: 255 + 1 does not fit in byte --> level.sg:4:9 4 | level += 1 | ^^^^^^^^^^ hint: `tryAdd` gives `null` instead, and `wrappingAdd` wraps around $ sg build level.sg --release -o level-fast $ ./level-fast 4 .fi .RE .PP The debug build stops where the interpreter stops, with the same message; the release build wraps, 250 and ten more being 4 in a \fBbyte\fR\&. Neither build writes anything when it works. .SS "A folder, named with -o" The file greet/words/caps.sg: .PP .RS 4 .nf public string capitalized(read string word) { if word.length() == 0 { return "" } return $"{word[0..1].toUpper()}{word[1..]}".toString() } .fi .RE .PP The file greet/main.sg: .PP .RS 4 .nf use words void main(List args) { if args.length() == 0 { print("Hello, nobody.") return } for name in read args { print($"Hello, {capitalized(name)}.") } } .fi .RE .PP .RS 4 .nf $ sg build greet -o greeter $ ./greeter ada grace Hello, Ada. Hello, Grace. $ ./greeter Hello, nobody. .fi .RE .PP A folder is a program, with the folders under it as its modules. Built from the folder above, it would be named \fBgreet\fR, which is the folder itself, so it is named with \fB\-o\fR\&. .SS "The program of tests" The file tax/tax.sg: .PP .RS 4 .nf long withTax(long cents, int percent) { return cents + cents * percent / 100 } test "twenty percent" { assert(withTax(1000, 20) == 1200) } test "a third" { assert(withTax(999, 33) == 1329) } .fi .RE .PP .RS 4 .nf $ cd tax $ sg build tax.sg --test -o tests $ ./tests ok twenty percent FAIL a third panic: assert failed: withTax(999, 33) == 1329 left: 1328 right: 1329 --> tax.sg:10:5 10 | assert(withTax(999, 33) == 1329) | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 1 of 2 test(s) failed $ ./tests --only 0; echo $? 0 $ ./tests --only 1; echo $? panic: assert failed: withTax(999, 33) == 1329 left: 1328 right: 1329 --> tax.sg:10:5 10 | assert(withTax(999, 33) == 1329) | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 1 .fi .RE .PP The program starts itself again for each test, with \fB\-\-only\fR and its number, counted from 0 in the order of the report. Run so by hand, one test runs in the program's own process, writes no line of its own and ends with 0 when it passes: that is the way to look at one test in a debugger, \fBgdb \-\-args ./tests \-\-only 1\fR\&. It takes no other word: a number that is not one of its tests, or another word, is told on the standard error, and it ends with 2 having run nothing. .PP .RS 4 .nf $ cd tax $ ./tests --only 2; echo $? error: `--only` takes the number of a test, from 0 to 1 2 $ ./tests --verbose; echo $? usage: ./tests [--only N] runs every test, or test N alone (0 is the first) 2 .fi .RE .SS "Flags of C" .RS 4 .nf $ cd level $ sg build level.sg -o level-o3 -- -O3 $ ./level-o3 panic: overflow: 255 + 1 does not fit in byte --> level.sg:4:9 4 | level += 1 | ^^^^^^^^^^ hint: `tryAdd` gives `null` instead, and `wrappingAdd` wraps around $ sg build level.sg --release -o level-small -- -Os -DSG_MAIN_STACK=67108864 warning: -DSG_MAIN_STACK=67108864: it changes a setting of SG's runtime, which is not a promise and may change from one version to the next $ ./level-small 4 .fi .RE .PP With \fBlevel.sg\fR of the first example. \fB\-O3\fR optimises the debug build harder and keeps its checks. The second build gives \fBmain\fR a stack of 64 MB instead of 1 GB: it is built, and told on every build, since the names of SG's runtime may change. .SS "A library of C, with -L" The file scale/grams.c: .PP .RS 4 .nf #include int64_t ounces_to_centigrams(int64_t ounces) { return ounces * 2835; } .fi .RE .PP The file scale/main.sg: .PP .RS 4 .nf extern "grams" { long ounces_to_centigrams(long ounces) } void main(List args) { for word in read args { var ounces = long.parse(word) ?? panic($"not a number: {word}") print($"{ounces} oz = {unsafe ounces_to_centigrams(ounces) / 100} g") } } .fi .RE .PP .RS 4 .nf $ cd scale $ mkdir -p vendor && cc -c -fPIC -o grams.o grams.c && ar rcs vendor/libgrams.a grams.o $ sg build main.sg -L vendor -o scale $ ./scale 1 16 1 oz = 28 g 16 oz = 453 g $ sg -L vendor main.sg 8 8 oz = 226 g .fi .RE .PP \fBextern "grams"\fR is linked as \fB\-lgrams\fR, found in \fBvendor\fR\&. The interpreter takes \fB\-L\fR before the file, since the words after the file are the program's. .SS "A library for C" The file tally/tally/tally.sg: .PP .RS 4 .nf // The sum of `count` integers at `items`; 0 for none. export long tally_sum(raw int items, nuint count) { long total = 0 for value in unsafe Memory.view(items, (nint)count) { total += value } return total } export int tally_max(int a, int b) { return a.max(b) } .fi .RE .PP The file tally/main.c: .PP .RS 4 .nf #include #include "tally.h" int main(void) { int32_t scores[] = { 7, 12, 5 }; printf("%lld %d\en", (long long)tally_sum(scores, 3), tally_max(-4, 9)); return 0; } .fi .RE .PP .RS 4 .nf $ cd tally $ sg build tally --library built libtally.a and tally.h; a program links it with `libtally.a -lm -pthread` $ cc -std=c11 -o scores main.c libtally.a -lm -pthread $ ./scores 24 9 $ grep tally_ tally.h TALLY_API int64_t tally_sum(int32_t *items, uint64_t count); TALLY_API int32_t tally_max(int32_t a, int32_t b); .fi .RE .PP \fBtally.h\fR declares the \fBexport\fR functions with the types of C. \fB\-\-shared\fR builds \fBlibtally.so\fR (\fBlibtally.dylib\fR on macOS) from the same folder, which a program links with \fB\-L. \-ltally\fR; \fB\-\-shared \-o out/libscore.so\fR writes \fBout/tally.h\fR beside it, and the line says \fB\-Lout \-lscore\fR\&. A library is a debug build too unless \fB\-\-release\fR is given, and a panic in it stops the program of C with the message. .SS "Without an operating system" The file board/device.sg: .PP .RS 4 .nf // A checksum the code that starts the board calls. export uint checksum(raw byte data, nuint length) { uint sum = 0 for b in unsafe Memory.view(data, (nint)length) { sum = sum.wrappingMultiply(31).wrappingAdd(b) } return sum } void onPanic(read string message) { } .fi .RE .PP The file board/start.c: .PP .RS 4 .nf #include #include uint32_t checksum(const uint8_t *data, uint64_t length); int main(void) { printf("%u\en", checksum((const uint8_t *)"board", 5)); return 0; } .fi .RE .PP .RS 4 .nf $ cd board $ sg build device.sg --freestanding built device.o for a machine without an operating system; link it with the code that starts the machine; it takes nothing of C's library, and of the C compiler's own only what that compiler calls on its own (GCC's atomics on 64-bit Arm, `-lgcc`) $ cc -o start start.c device.o $ ./start 93908710 .fi .RE .PP \fBdevice.o\fR has no \fBmain\fR and takes nothing of C's library, so the C program here stands in for a machine. A program built so has an \fBonPanic\fR function, which a panic goes to. For a real device the machine is named: \fBsg build kernel \-\-freestanding \-\-target riscv64\-unknown\-elf \-\- \-march=rv64gc \-mabi=lp64d\fR, compiled by a Clang that knows that machine (Apple's knows only its own). .SH "MISTAKES IT TELLS" .SS "An output in a folder that is not there" The file hello/hello.sg: .PP .RS 4 .nf void main() { print("hello") } .fi .RE .PP .RS 4 .nf $ sg build hello/hello.sg -o bin/hello; echo $? error: cannot write bin/hello: there is no folder bin 2 .fi .RE .PP It is told before anything is compiled. An output that is a folder is told too: \fBsg build greet\fR from the folder above \fBgreet\fR gives \fBerror: `greet` is a folder; name the program with \-o\fR\&. .SS "An output that would replace a source" .RS 4 .nf $ sg build hello/hello.sg -o hello/hello.sg; echo $? error: cannot write hello/hello.sg: it is the program itself, which the build would replace; name the output with -o 2 .fi .RE .PP A C compiler refuses an output that is one of its inputs, and so does \fBsg build\fR, before anything is compiled. A library's header is kept the same way when a header of one's own lies where it would go. .SS "A flag of C that sg decides" .RS 4 .nf $ sg build hello/hello.sg -- -o hello/out; echo $? error: -o in the C flags: the output is sg's to name: write -o before `--` 2 $ sg build hello/hello.sg -- -m32; echo $? error: -m32 in the C flags: it changes the size or the layout of C's types, which the C that sg writes, and the libraries it calls, are written for 2 .fi .RE .SS "A C compiler that cannot be run" .RS 4 .nf $ CC=nosuchcc sg build hello/hello.sg -o hi; echo $? error: cannot run the C compiler `nosuchcc`: No such file or directory (os error 2) 1 .fi .RE .PP .RS 4 .nf $ CC=false sg build hello/hello.sg -o hi; echo $? error: the C compiler `false` ended with 1 and named no error; is `false` a C compiler? `CC` names it 1 .fi .RE .PP \fBCC\fR names one program. One that ends without naming an error, as \fBfalse\fR does, or that ends with 0 and writes no program, is told as perhaps no C compiler; a C compiler that cannot build what sg writes is told by its own messages, then by sg's. .SS "A mistake in a C file of the program" The file native/twice.c: .PP .RS 4 .nf #include int64_t twice(int64_t x) { return x * 2 } .fi .RE .PP The file native/main.sg: .PP .RS 4 .nf extern "./twice.c" { long twice(long x) } void main() { print(unsafe twice(21)) } .fi .RE .PP .RS 4 .nf $ cd native $ sg build main.sg 2>&1 | grep -A1 '^error:' | cut -d: -f1-4 error: the C compiler did not accept twice.c, a file of C of the program; the mistake is in that file twice.c:4:17: error .fi .RE .PP What the C compiler wrote comes first, as it wrote it; under it, sg names the file the mistake is in and says its first errors again, here the missing \fB;\fR (cut to the place, since each compiler words its error its own way). C that sg wrote and the C compiler refused is a mistake in sg, and the line says so. .SS "A library with a main" .RS 4 .nf $ sg build hello/hello.sg --library; echo $? error: a library has no `main`: C calls its `export` functions --> hello/hello.sg:1:6 1 | void main() { | ^^^^ hint: a program that starts at `main` is built without `--library` and `--shared` 1 .fi .RE .PP A library that has no \fBexport\fR function is refused too, with the hint of one. .SH "SEE ALSO" .BR sg (1), .BR sg-test (1), .BR sg-emit-c (1), .BR sg-extern (7), .BR sg-export (7), .BR sg-test (7) .PP https://sg\-lang.org/reference/sg-build.html