← Back to AILP Home

cfg — Conditional Compilation

Nitpick provides conditional compilation to allow you to include or exclude code based on target platforms, build configurations, or custom features.

#[cfg(...)] Attribute Syntax

The #[cfg(...)] attribute is placed before a declaration (function, module, struct, etc.) to specify that the item should only be compiled if the given condition evaluates to true.

#[cfg(target_os = "linux")]
pub func:syscall = int32() {
    pass 0i32;
};

#[cfg(feature = "experimental")]
mod experimental_features {
    // ...
}

cfg!(...) Expression Macro

You can evaluate cfg conditions inline as a boolean expression using the cfg! macro. This is evaluated at compile time and is useful for logic that should be conditionally enabled without entirely removing the code structure.

func:main = int32() {
    if (cfg!(debug)) {
        println("Running in debug mode");
    }
    exit 0;
};

Supported Predicates

Predicate Description Example
debug True when compiling without optimizations (default) cfg(debug)
release True when compiling with optimizations (e.g., -O3) cfg(release)
target_os The operating system cfg(target_os = "linux")
target_arch The CPU architecture cfg(target_arch = "x86_64")
feature Custom features enabled via CLI cfg(feature = "networking")

Combinators

You can build complex conditions using logical combinators:

#[cfg(all(target_os = "linux", not(target_arch = "arm")))]
func:fast_linux_x86_impl = int32() { pass 0i32; };

#[cfg(any(target_os = "macos", target_os = "ios"))]
func:apple_specific = int32() { pass 0i32; };

Command Line Flags

You can enable specific features during compilation by passing the --feature=<name> flag:

nitpick build main.npk --feature=networking --feature=ssl

Platform Detection Table

Common combinations of target_os and target_arch:

Examples

Platform-Specific Code

#[cfg(target_os = "linux")]
func:get_os_name = string() { pass "Linux"; };

#[cfg(target_os = "windows")]
func:get_os_name = string() { pass "Windows"; };

Feature-Gated Modules

#[cfg(feature = "beta")]
mod new_api {
    pub func:cool_feature = int32() { pass 42i32; };
}

Debug-Only Logging

func:log_state = int32(int32:state) {
    if (cfg!(debug)) {
        println(`State: &{state}`);
    }
    pass 0i32;
};