11# Cross-Building
22
3- Cross-building is the process of building .NET for a target platform on a
4- different host platform. For example, building for ` linux-x64 ` on a host that
5- uses the ` azurelinux-3.0-net10.0-cross-amd64 ` container image with a
6- dedicated sysroot.
3+ Cross-building is the process of building .NET on a host machine for a target
4+ OS and/or CPU architecture that differs from the host. For example, compiling
5+ ` linux-arm64 ` binaries on an ` x64 ` host, or building for a specific Linux
6+ distribution using a container image that provides a dedicated sysroot.
7+
8+ A sysroot is a self-contained root filesystem for the target platform. It
9+ contains the headers, libraries, and other files needed to compile and link
10+ code for that target. Cross-build container images provided by the .NET team
11+ (see [ dotnet-buildtools-prereqs-docker] ( https://github.com/dotnet/dotnet-buildtools-prereqs-docker ) )
12+ typically place the sysroot at a well-known path and expose it via the
13+ ` ROOTFS_DIR ` environment variable.
714
815## When is Cross-Building Required?
916
10- Cross-building is required when:
17+ Cross-building is required when any of the following are true :
1118
12- - The build environment uses a cross-build container image (e.g.
13- ` mcr.microsoft.com/dotnet-buildtools/prereqs:azurelinux-3.0-net10.0-cross-amd64 ` )
14- that provides a sysroot via the ` ROOTFS_DIR ` environment variable, even when
15- building for the same architecture as the host (e.g. ` linux-x64 ` ).
19+ - The ** target CPU architecture** differs from the host (e.g. building
20+ ` linux-arm64 ` on an ` x64 ` machine).
21+ - The ** target OS or Linux distribution** requires a dedicated sysroot that is
22+ provided by a cross-build container image via the ` ROOTFS_DIR ` environment
23+ variable, even when the host and target share the same CPU architecture
24+ (e.g. building ` linux-x64 ` inside an
25+ ` azurelinux-3.0-net10.0-cross-amd64 ` container).
1626
1727## Enabling Cross-Building
1828
19- When building in a cross-build container with ` ROOTFS_DIR ` set, you must pass
20- ` /p:CrossBuild=true ` to the build command. Without this flag, the runtime build
21- will not use the sysroot when searching for native dependencies such as OpenSSL,
22- causing the build to fail.
29+ When ` ROOTFS_DIR ` is set, you must pass ` /p:CrossBuild=true ` to the build
30+ command. Without this flag, the runtime build will not use the sysroot when
31+ searching for native dependencies such as OpenSSL, causing the build to fail.
2332
24- ## Example
33+ ## Example: Building linux-x64 in a Cross-Build Container
2534
2635``` bash
2736docker run \
@@ -38,6 +47,22 @@ docker run \
3847 /p:CrossBuild=true
3948```
4049
41- > ** Note:** ` /p:CrossBuild=true ` is only required when using a cross-build
42- > container with ` ROOTFS_DIR ` set. It is not needed when building natively on
43- > the target platform.
50+ ## Example: Building linux-arm64 on an x64 Host
51+
52+ ``` bash
53+ docker run \
54+ --rm \
55+ --volume /path/to/dotnet:/dotnet \
56+ --workdir /dotnet \
57+ --env ROOTFS_DIR=/crossrootfs/arm64 \
58+ mcr.microsoft.com/dotnet-buildtools/prereqs:azurelinux-3.0-net10.0-cross-arm64 \
59+ ./build.sh \
60+ --clean-while-building \
61+ --source-only \
62+ --arch arm64 \
63+ --os linux \
64+ /p:CrossBuild=true
65+ ```
66+
67+ > ** Note:** ` /p:CrossBuild=true ` is only required when ` ROOTFS_DIR ` is set.
68+ > It is not needed when building natively on the target platform.
0 commit comments