Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions website/content/sources.md
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,43 @@ Build args can be specified as well as `args` which is a map of key value pairs.

Build sources are considered to be "directory" sources.

#### Cross-compiling in a build source

When the target platform differs from the platform of the build machine, the [build steps](spec.md#build-section) run under emulation, which is slow for compilers.
Comment thread
atiasadir marked this conversation as resolved.
Resolved
A build source can do the compiling instead.
Dalec builds the Dockerfile for the target platform, so the [platform build args](https://docs.docker.com/reference/dockerfile/#automatic-platform-args-in-the-global-scope) are set in it.
Comment thread
atiasadir marked this conversation as resolved.
Resolved
A stage that starts with `FROM --platform=$BUILDPLATFORM` runs natively and can cross-compile for `TARGETOS` and `TARGETARCH`:

```dockerfile
# build.Dockerfile, in the build context next to the Go code
FROM --platform=$BUILDPLATFORM golang:1.27 AS build
ARG TARGETOS TARGETARCH
WORKDIR /src
COPY . .
RUN CGO_ENABLED=0 GOOS=$TARGETOS GOARCH=$TARGETARCH go build -o /out/hello .

FROM scratch
COPY --from=build /out/ /out/
```

```yaml
sources:
bin:
path: /out
build:
dockerfile_path: build.Dockerfile
source:
context: {}

artifacts:
binaries:
bin/hello: {}
```

With this spec, `docker build --platform linux/arm64` on an amd64 machine compiles `hello` for arm64 without emulation.
The spec's build steps still run on the target platform, so keep them light.
This works for toolchains that can cross-compile on their own, such as Go with `CGO_ENABLED=0`.

## Generators

Generators are used to generate a source from another source.
Expand Down
6 changes: 6 additions & 0 deletions website/content/spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,17 @@ args:
TARGETARCH:
TARGETPLATFORM:
TARGETVARIANT:
BUILDOS:
BUILDARCH:
BUILDPLATFORM:
BUILDVARIANT:
DALEC_TARGET:
```

These arguments are set based on the default docker platform for the machine, *unless* the platform is overridden explicitly in the docker build with `--platform`. For example, upon invoking `docker build` on a Linux amd64 machine, we would have `TARGETOS=linux`, `TARGETARCH=amd64`, `TARGETPLATFORM=linux/amd64`.

The `BUILD*` arguments are the platform of the machine running the build, and `--platform` does not change them. For example, `docker build --platform linux/amd64` on a Linux arm64 machine gives `BUILDARCH=arm64` and `TARGETARCH=amd64`. See [cross-compiling in a build source](sources.md#cross-compiling-in-a-build-source) for an example.

`DALEC_TARGET` is set to the target name, such as `azlinux3` or `windowscross`.

{{< callout type="info" >}}
Expand Down