Overview
BRICK provides a powerful templating system that allows you to create flexible, platform-independent build scripts. The templating system uses a simple syntax with strict typing and variable expansion.
Templates help you:
- Write portable build scripts that work across different environments
- Simplify existing scripts with built-in variables
- Handle different build configurations dynamically
- Access information about dependencies and build context
Templating Syntax
Templates in BRICK use curly brace syntax and can be inserted anywhere in your build scripts. The templating system is strictly typed, ensuring type safety and catching errors at build time.
Basic Template Syntax
Templates are enclosed in double curly braces: {{ }}
# Basic string template
echo {{ "Hello, World!" }}
# Numeric comparisons
{{ 5 > 10 }} # Returns: false
{{ 15 >= 10 }} # Returns: true
# String output
{{ "example text" }} # Returns: example text
Type System
BRICK's templating system enforces strict typing to prevent runtime errors. Mixing incompatible types will result in a syntax error at build time.
# Valid operations
{{ 5 + 3 }} # Numbers: returns 8
{{ "hello" + " world" }} # Strings: returns "hello world"
{{ true && false }} # Booleans: returns false
# Type errors (will fail at build time)
{{ "text" < 5 }} # ❌ Cannot compare string to number
{{ 42 + "string" }} # ❌ Cannot add number to string
{{ true + 1 }} # ❌ Cannot add boolean to number
Built-in Variables
BRICK provides built-in variables that give you access to platform and environment information. The sys
namespace contains system-related variables, but other variable namespaces may be available as well.
# Architecture detection
{{ sys.arch }} # Returns system architecture
{{ sys.arch == "x86_64" }} # Check if 64-bit system
# Operating system information
{{ sys.os }} # Returns operating system name
{{ sys.kernel }} # Returns kernel name
{{ sys.node }} # Returns system hostname
{{ sys.kernel_version }} # Returns kernel version
# Platform detection
{{ sys.platform.windows }} # Boolean: true on Windows
{{ sys.platform.linux }} # Boolean: true on Linux
{{ sys.platform.mac }} # Boolean: true on macOS
Conditional Expressions
BRICK supports conditional expressions using the ternary operator syntax:condition ? value_if_true : value_if_false
# Platform-specific commands
{{ sys.platform.windows ? "Windows" : "Something else" }}
# Architecture-specific flags
gcc {{ sys.arch == "x86_64" ? "-m64" : "-m32" }} -o program main.c
# Complex conditionals
{{ sys.platform.windows ? "cmd.exe" : (sys.platform.macos ? "zsh" : "bash") }}
Comparison Operators
BRICK supports standard comparison operators for numeric and string values:
Numeric Comparisons
>
- Greater than>=
- Greater than or equal<
- Less than<=
- Less than or equal==
- Equal to!=
- Not equal to
String Comparisons
==
- String equality!=
- String inequality+
- String concatenation
Boolean Operations
&&
- Logical AND||
- Logical OR!
- Logical NOT
Practical Examples
Here are some real-world examples of using templates in BRICK scripts:
# Platform-specific build tools
{{ if sys.platform.windows ? "msbuild" : "make" }} -j4
# Architecture-specific library paths
export LD_LIBRARY_PATH=/usr/lib/{{ sys.arch }}
# Conditional dependency installation
{{ if sys.platform.linux ? "apt-get install build-essential" : "echo 'Dependencies managed by system'" }}
# Dynamic output file naming
cp program ${BUILD_INSTALL_DIR}/program-{{ sys.arch }}{{ if sys.platform.windows ? ".exe" : "" }}
Template Best Practices
- Keep templates simple and readable
- Use meaningful variable names and comments
- Test templates across different platforms when possible
- Prefer explicit comparisons over implicit boolean conversion
- Use parentheses to clarify complex conditional expressions
Built-in Variables
BRICK provides several built-in variables that give you access to system information and build context. These variables are organized into namespaces for better organization and clarity.
sys
The sys
namespace provides comprehensive access to system and platform information. All variables in this namespace return string values except for platform detection flags which return boolean values.
sys.arch
StringReturns the system architecture (e.g., "x86_64", "arm64", "i386").
# Architecture-specific compilation flags
gcc {{ sys.arch == "x86_64" ? "-m64" : "-m32" }} -o program main.c
sys.os
StringReturns the operating system name (e.g., "Windows_NT", "Linux", "Darwin").
# OS-specific environment setup
{{ sys.os == "Linux" ? "export LD_LIBRARY_PATH=/usr/local/lib" : "echo 'Non-Linux system'" }}
sys.kernel
StringReturns the kernel name (e.g., "Linux", "Windows_NT", "Darwin").
# Kernel-specific optimizations
{{ sys.kernel == "Linux" ? "-pthread" : "" }}
sys.node
StringReturns the system hostname/node name.
# Include hostname in build artifacts
echo "Built on: {{ sys.node }}" > build-info.txt
sys.kernel_version
StringReturns the kernel version string.
# Log kernel version for debugging
echo "Kernel version: {{ sys.kernel_version }}" >> build.log
sys.platform.*
BooleanPlatform detection flags that return true
or false
:
sys.platform.windows
- True on Windows systemssys.platform.linux
- True on Linux systemssys.platform.mac
- True on macOS systems
# Platform-specific build commands
{{ if sys.platform.windows ? "msbuild project.sln" : "make all" }}
# Platform-specific file extensions
cp program ${INSTALL_DIR}/program{{ if sys.platform.windows ? ".exe" : "" }}
# Multiple platform checks
{{ if sys.platform.linux ? "linux-specific-command" : (sys.platform.mac ? "macos-command" : "windows-command") }}