Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Build options

Some code should be optional at build time: linking a library such as libcrypto or zlib when it is there, tracing that only a debug build wants, a backend chosen when the program is built. Build options set that on the command line with -D, as in C compilers, and the code tests them with BuildOption.

pep-build main.pep -D USE_LIBCRYPTO -D LOG_LEVEL=2 -D BACKEND=zlib -o app
  • -D NAME defines an option. It counts as NAME=1, as in C.
  • -D NAME=VALUE defines it with a value, which may be any text.
  • Repeat -D for more options. When two name the same option, the later one wins.

pep-build --test defines TEST, which keeps the [Test] functions and can mark code only tests use (see Unit tests). A package's [features] that a build turns on define options too, named <package>_<feature> (http_tls; see Features), and so do the pep:build-option=NAME lines a package's build script prints, named <package>_<NAME> (see Build scripts).

A name is a letter or _, then letters, digits and _. Any other name is an error:

error: invalid build option name '2FAST' in '-D 2FAST'; a name is a letter or '_' followed by letters, digits and '_'
# Build options: compile with
# `pep-build main.pep -D FAST_MATH -D LOG_LEVEL=2 -D QUIET_LEVEL=1` to pick
# the other versions.

# `[BuildOption("NAME")]` keeps a declaration when `-D NAME` is given, and
# `!BuildOption(NAME)` inside a `Target` keeps its fallback otherwise.
[BuildOption("FAST_MATH")]
scale(x: int): int {
    return x << 1;
}

[Target(!BuildOption(FAST_MATH))]
scale(x: int): int {
    return x * 2;
}

# A `static` block reads an option's value while compiling, with a default
# for a build that does not set it.
log_level(): int {
    var level = 1;
    static (level) {
        if (reflect.build_option("LOG_LEVEL", "1") == "2") {
            level = 2;
        }
    }
    return level;
}

# Two options compare with each other too, as numbers when both values are.
# The comparison is false, with a warning, unless the build defines both.
verbose(): bool {
    [Target(BuildOption(LOG_LEVEL) > BuildOption(QUIET_LEVEL))] {
        return true;
    }
    return false;
}

# Either side of a comparison computes with `+ - * / %` on options and
# numbers.
even_level(): bool {
    [Target(BuildOption(LOG_LEVEL) % 2 == 0)] {
        return true;
    }
    return false;
}

main(): int {
    if (scale(21) != 42) {
        return 1;
    }
    var level = log_level();
    if (level < 1 || level > 2) {
        return 2;
    }
    if (verbose() and level != 2) {
        return 3;
    }
    if (even_level() != (level == 2)) {
        return 4;
    }
    return 0;
}

Selecting code

[BuildOption("NAME")] keeps a declaration or a block only when the build defines NAME. It is shorthand for [Target(BuildOption(NAME))], and it goes everywhere [Target(...)] goes: functions, externs, structs, methods, module-scope variables, imports, re-exports and { } blocks in a function (see Platform selection).

Inside Target, BuildOption(NAME) combines with OS(...), Arch(...), !, &&, || and parentheses. Used alone, it is true when NAME is defined, whatever its value, and false when it is not, with no warning:

[BuildOption("USE_LIBCRYPTO")]
[Link("crypto")]
extern int SHA256_Init(__ptr<void> context);

[Target(!BuildOption(USE_LIBCRYPTO))]
_sha256_compress(...) { ... }   # the portable fallback

[Target(BuildOption(TRACE) && OS(Linux))]
import "linux_trace";

Several attributes on one item combine with "and", as [Target] does.

Options the runtime reads

OptionEffect
USE_LIBCRYPTOdigest's SHA-256 and SHA-1 use libcrypto (CNG on Windows); see Cryptographic hashes
USE_ZLIBcompress uses zlib; see Compression
TESTdefined by pep-build --test; see Unit tests

Comparing values

BuildOption(NAME) may be compared with a literal using ==, !=, <, <=, > and >=, on either side. The literal's type says how the value is read:

LiteralThe value is read asExample
integera signed 64-bit integer, decimal or 0x hexBuildOption(LOG_LEVEL) >= 2
floata doubleBuildOption(SCALE) > 1.5
true / falsetrue/false, 1/0, on/off or yes/no, in any case (== and != only)BuildOption(TRACE) == true
stringtext, compared byte for byteBuildOption(BACKEND) == "zlib"
[Target(BuildOption(LOG_LEVEL) >= 2 && OS(Linux))]
_trace(message: String) { ... }

[Target(BuildOption(BACKEND) == "zlib")]
import "zlib_backend";

A value the literal's type cannot read is an error at the comparison:

main.pep:1:28: error: build option 'LEVEL' is 'high', which is not an integer
    1 | [Target(BuildOption(LEVEL) >= 2)]
      |                            ^~~~

Comparing a build option with anything but a literal or another build option is an error too.

Two build options compare with each other, BuildOption(A) <= BuildOption(B), by their values: as integers when both read as integers, else as doubles when both read as numbers, else as text, byte for byte. Text compares only with == and !=; ordering a value that is not a number is an error at the operator:

main.pep:1:27: error: build option 'MODE' is 'fast', which is not a number; '<' compares two build options only as numbers
    1 | [Target(BuildOption(MODE) < BuildOption(LEVEL))]
      |                           ^~~~~~~~~~~~~~~~~~~~

Arithmetic on options

Either side of a comparison may compute with +, -, *, / and % on build options and number literals, with the precedence and parentheses of Pepper expressions:

[Target(BuildOption(LEVEL) % 2 == 0)]
_even_level() { ... }

[Target((BuildOption(MAJOR) * 100 + BuildOption(MINOR)) >= 203)]
import "new_api";
  • Each value is read as an integer, decimal or 0x hex, that fits in 64 bits, else as a double. Arithmetic is on signed 64-bit integers when every value in it is one, and on doubles otherwise, so BuildOption(TEN) / 4 is 2 and BuildOption(TEN) / 4.0 is 2.5. / and % on integers round toward zero, as in Pepper.

  • A value that is not a number is an error at the operator:

    main.pep:1:28: error: build option 'LEVEL' is 'high', which is not a number; '%' takes only numbers
        1 | [Target(BuildOption(LEVEL) % 2 == 0)]
          |                            ^~~
    
  • Integer division or remainder by zero is an error at the operator (division by zero in a 'Target' condition), and so is an integer result that does not fit in 64 bits (`integer overflow: '9223372036854775807

    • 2' does not fit in a 'long'). On doubles both follow IEEE 754: 1.5 / 0` is infinity, and a NaN compares unequal to everything.
  • Arithmetic must end in a comparison: [Target(BuildOption(A) + 1)] is an error.

Options the build does not define

A comparison with an option the build does not define is false, whatever the operator, and the compiler warns at it, since a missing option there is usually a mistake. With two options, it is false when either is not defined, BuildOption(A) == BuildOption(B) included when neither is; with arithmetic, when any option in it is not, before anything is computed (so BuildOption(A) / 0 > 1 is false, not an error, without -D A). The compiler warns at each one missing:

main.pep:3:13: warning: build option 'LOG_LEVEL' is not defined; the comparison is false
    3 |     [Target(BuildOption(LOG_LEVEL) >= 2)] {
      |             ^~~~~~~~~~~~~~~~~~~~~~
note: define it with '-D LOG_LEVEL=<value>'

Reading values in static blocks

A static block reads build options while compiling:

  • reflect.has_build_option("NAME") is whether the build defines NAME.
  • reflect.build_option("NAME") is its value, as a String. It is an error when the build does not define NAME.
  • reflect.build_option("NAME", "default") is its value, or default when the build does not define it.

The build cache and the editor

The options and their values are part of the build cache's key, so building with other options never reuses objects built with different ones.

The language server analyzes with the options in the VS Code setting pepper.buildOptions, a list written as -D takes them (["USE_ZLIB", "LOG_LEVEL=2"]). It has none by default, so code that needs an option shows dimmed, and comparisons with options warn until the setting names them.

Not yet

  • A pep.toml table declaring the options a package takes, with their types and defaults.