5.0 KiB
Cross-compilation and Targets
DCR supports cross-compilation to different platforms using the --target flag. This allows building your project for architectures and operating systems different from the host.
Basic Usage
# Build for a specific target
dcr build --target x86_64-unknown-linux-gnu --release
# Short names for common platforms
dcr build --target linux --release # x86_64-unknown-linux-gnu
dcr build --target macos --release # x86_64-apple-darwin
dcr build --target windows --release # x86_64-pc-windows-msvc
# Run for a specific target
dcr run --target linux --release
Supported Targets
Short Names
linux→x86_64-unknown-linux-gnumacos→x86_64-apple-darwinwindows→x86_64-pc-windows-msvcfreebsd→x86_64-unknown-freebsdopenbsd→x86_64-unknown-openbsdnetbsd→x86_64-unknown-netbsd
Full Target Triples
DCR supports any valid target triple in the format <arch>-<vendor>-<os>-<abi>. Common examples:
x86_64-unknown-linux-gnuaarch64-linux-gnux86_64-apple-darwinaarch64-apple-darwinx86_64-pc-windows-msvci686-pc-windows-gnux86_64-unknown-freebsdx86_64-unknown-openbsdx86_64-unknown-netbsd
Configuration
Multiple Targets
Configure multiple targets to build simultaneously:
[build]
targets = ["linux", "macos", "windows"]
# Or per profile
[build.release]
targets = ["linux", "macos"]
If no --target is specified, DCR builds for all targets in build.targets (or native if empty).
Target-Specific Settings
Override settings for specific targets:
[build.linux]
compiler = "gcc"
cflags = ["-Wall", "-O2"]
[build.macos]
compiler = "clang"
cflags = ["-Wall", "-O2", "-arch", "x86_64"]
[build.windows]
compiler = "x86_64-w64-mingw32-gcc"
ldflags = ["-static"]
[build.freebsd]
compiler = "clang"
cflags = ["-Wall", "-O2"]
Target-Specific Toolchain
Configure toolchain per target:
[toolchain.linux]
cc = "gcc"
ld = "ld"
[toolchain.macos]
cc = "clang"
ld = "ld"
[toolchain.windows]
cc = "x86_64-w64-mingw32-gcc"
ld = "x86_64-w64-mingw32-ld"
Target-Specific Run Commands
Use custom run commands per target:
[run.linux]
cmd = "./target/linux/release/myapp"
[run.macos]
cmd = "open ./target/macos/release/myapp.app"
[run.windows]
cmd = "wine ./target/windows/release/myapp.exe"
[run.freebsd]
cmd = "./target/freebsd/release/myapp"
Workspace and Dependencies Inheritance
Workspace and dependencies sections also support target/profile inheritance:
[workspace.linux]
members = [
{ name = "lib1", path = "libs/lib1" },
]
[dependencies.linux]
sdl2 = { system = true, include = ["/usr/include/SDL2"] }
[dependencies.windows]
sdl2 = { system = true, include = ["C:/SDL2/include"] }
Order: workspace.target.profile → workspace.profile.target → workspace.target → workspace.profile → workspace.
Add inherit = false to disable inheritance in any section.
Settings are applied in order:
[build.<target>][build.<profile>][build]
Toolchain Configuration
For cross-compilation, configure toolchains:
[toolchain]
cc = "aarch64-linux-gnu-gcc"
cxx = "aarch64-linux-gnu-g++"
ar = "aarch64-linux-gnu-ar"
ld = "aarch64-linux-gnu-ld"
Output Structure
Build outputs are organized by target:
target/
├── native/
│ ├── debug/
│ └── release/
├── x86_64-unknown-linux-gnu/
│ ├── debug/
│ └── release/
├── aarch64-linux-gnu/
│ ├── debug/
│ └── release/
└── x86_64-unknown-freebsd/
├── debug/
└── release/
Requirements
- Cross-compilation requires appropriate toolchains installed (e.g.,
aarch64-linux-gnu-gccon Arch Linux). - For Windows targets on Linux, install MinGW:
pacman -S mingw-w64-gcc. - For macOS targets on Linux, use
osxcrossor similar.
Examples
ARM Linux Build
# Install toolchain (Arch Linux)
pacman -S aarch64-linux-gnu-gcc
# Configure
echo '[build.aarch64-linux-gnu]
compiler = "aarch64-linux-gnu-gcc"' >> dcr.toml
# Build
dcr build --target aarch64-linux-gnu --release
Note: If you set target = "aarch64-linux-gnu" (or any value) directly under [build], DCR will automatically add --target=... to cflags when using clang. You usually don't need to repeat it manually.
Windows Build on Linux
# Install toolchain (Arch Linux)
pacman -S mingw-w64-gcc
# Configure
echo '[build.windows]
compiler = "x86_64-w64-mingw32-gcc"' >> dcr.toml
# Build
dcr build --target windows --release
Troubleshooting
- Toolchain not found: Ensure the compiler is installed and in PATH, or configure
[toolchain]indcr.toml. For Arch Linux:pacman -S aarch64-linux-gnu-gccfor ARM,pacman -S mingw-w64-gccfor Windows cross-compilation. - Linking errors: Check
ldflagsand library paths for cross-compilation. - Header issues: Include paths may differ; use target-specific
cflags.
For more help, see Troubleshooting map.