Overview
BuckSample supports multiple build configurations (Debug and Release) through Buck’s configuration file system. This allows you to optimize builds for different purposes: fast iteration during development (Debug) or performance and size for production (Release).Configuration Architecture
Buck uses a hierarchical configuration system where you can define custom values and reference them throughout your build files.Base Configuration
The.buckconfig file defines default (Debug) configuration:
.buckconfig
.buckconfig
The
$(config custom.optimization) syntax references the optimization value from the [custom] section, allowing you to change optimization levels without modifying compiler flag definitions.Release Configuration
TheBuildConfigurations/Release.buckconfig file overrides values for Release builds:
BuildConfigurations/Release.buckconfig
--config-file, it overrides the values from .buckconfig.
Debug Configuration
The Debug configuration is optimized for fast compilation and debugging support.Optimization Level
- Fastest compilation times
- Variables retain their values (easier debugging)
- Code matches source line-by-line
- Larger binary size
- Slower runtime performance
Swift Compiler Flags
C/C++ Flags
The Debug configuration also affects C/C++ compilation:.buckconfig
- -g: Debug symbols
- -D DEBUG: DEBUG preprocessor macro
- -D BUCK: BUCK preprocessor macro
Release Configuration
The Release configuration is optimized for size and performance.Optimization Level
- Smaller app binary
- Reduced memory footprint
- Balanced performance
- Longer compilation times
Alternative optimization levels:
- -O: Optimize for speed (fastest runtime)
- -Osize: Optimize for size (smallest binary)
- -Onone: No optimization (Debug default)
Swift Compiler Flags
- Define
RELEASEpreprocessor macro - Remove
-enable-testing(no@testableimports) - Remove
-g(no debug symbols by default, though you can add them)
Whole Module Optimization
Both configurations use whole module optimization:- Better optimization across file boundaries
- Smaller final binary
- Longer compilation but better runtime performance
Building with Configurations
Debug Build (Default)
Build with Debug configuration:Makefile
.buckconfig settings (Debug).
Release Build
Build with Release configuration:Makefile
--config-file flag applies the Release configuration on top of .buckconfig.
Running with Configurations
Makefile
Creating Custom Configurations
You can create additional configurations for specific purposes.Define Custom Values
Add configuration overrides:This creates a Staging configuration with:
BuildConfigurations/Staging.buckconfig
- Speed optimization (
-O) - STAGING preprocessor macro
- Debug symbols for debugging production issues
Conditional Compilation
Use preprocessor macros to conditionally compile code:Swift
Objective-C
Configuration Best Practices
Use Debug for Development
Always use Debug configuration during development:
- Faster compilation
- Better debugging experience
- Access to testing utilities
Test Release Builds
Periodically test Release builds to catch optimization-related bugs:Some bugs only appear in optimized builds (race conditions, logic errors revealed by optimization).
Use Macros Sparingly
Avoid overusing preprocessor macros. Prefer runtime configuration when possible:
Advanced Configuration
Per-Target Configuration
You can apply different configurations to different targets:BUCK
Environment-Specific Settings
Combine with environment variables:BuildConfigurations/Production.buckconfig
Combining Configuration Files
You can specify multiple configuration files:Comparing Configurations
| Aspect | Debug | Release |
|---|---|---|
| Optimization | -Onone | -Osize |
| Compilation Speed | Fast | Slower |
| Binary Size | Larger | Smaller |
| Runtime Performance | Slower | Faster |
| Debug Symbols | Yes (-g) | Optional |
| Testing Support | Yes (-enable-testing) | No |
| Preprocessor Macro | DEBUG | RELEASE |
| Use Case | Development, debugging | Production, App Store |
Troubleshooting
Configuration Not Applied
If your configuration seems ignored:-
Verify the file path is correct:
- Check for syntax errors in the config file
-
Ensure you’re using
--config-fileflag:
Macro Not Defined
If preprocessor macros aren’t working:-
Verify the macro is defined in
config_swift_compiler_flags: -
Check that flags are referenced correctly:
-
Clean and rebuild:
Unexpected Optimization
If code behaves differently between configurations:- This is often due to optimization revealing bugs (uninitialized variables, race conditions)
- Test with different optimization levels to isolate:
- Use sanitizers to catch issues:
Related Topics
- Debugging - Debug builds with proper symbol support
- Code Coverage - Coverage configuration
- CI/CD - Testing both Debug and Release in CI