Skip to content

Commit 387fc30

Browse files
committed
Support cross platform builds
Add support for cross building the following platforms: - `linux/i386` - `linux/amd64` - `linux/arm` - `linux/arm/v5` - `linux/arm/v6` - `linux/arm/v7` - `linux/arm64` - `linux/arm64/v8` - `linux/ppc64le` - `linux/s390x` To build for a different platform other than the local native platform the `qemu-user-static` package must be installed. Example build for an ARM platform: ``` make PLATFORM=linux/arm all-images ``` Once the images are built it's necessary to clean the images before building for another platform or the build will fail: ``` make clean-images make PLATFORM=linux/ppc64le all-images ```
1 parent 04f7bab commit 387fc30

File tree

3 files changed

+96
-8
lines changed

3 files changed

+96
-8
lines changed

Makefile

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,30 +16,50 @@
1616
#
1717

1818
BUILDER ?= docker
19+
PLATFORM ?= linux/amd64
1920
VERSION ?= latest
2021
IMAGE ?= weechat
2122

23+
ALPINE_BASE_IMAGE = alpine:3.15
24+
DEBIAN_BASE_IMAGE = debian:bullseye-slim
25+
26+
TEST_IMAGE = $(IMAGE)
27+
ifeq ($(strip $(BUILDER)),podman)
28+
TEST_IMAGE=localhost/$(IMAGE)
29+
endif
30+
31+
TEST_VERSION = $(VERSION)
32+
ifeq ($(strip $(TEST_VERSION)),$(filter $(VERSION),latest stable))
33+
TEST_VERSION=$(shell curl "https://weechat.org/dev/info/stable/")
34+
endif
35+
2236
.PHONY: all debian debian-slim alpine alpine-slim
2337

2438
all: debian
2539

2640
all-images: debian debian-slim alpine alpine-slim
2741

2842
debian:
29-
./build.py -b "$(BUILDER)" -d "debian" "$(VERSION)"
43+
./build.py -b "$(BUILDER)" -p "$(PLATFORM)" -d "debian" "$(VERSION)"
3044

3145
debian-slim:
32-
./build.py -b "$(BUILDER)" -d "debian" --slim "$(VERSION)"
46+
./build.py -b "$(BUILDER)" -p "$(PLATFORM)" -d "debian" --slim "$(VERSION)"
3347

3448
alpine:
35-
./build.py -b "$(BUILDER)" -d "alpine" "$(VERSION)"
49+
./build.py -b "$(BUILDER)" -p "$(PLATFORM)" -d "alpine" "$(VERSION)"
3650

3751
alpine-slim:
38-
./build.py -b "$(BUILDER)" -d "alpine" --slim "$(VERSION)"
52+
./build.py -b "$(BUILDER)" -p "$(PLATFORM)" -d "alpine" --slim "$(VERSION)"
3953

4054
test-container:
41-
"$(BUILDER)" run "$(IMAGE)" weechat --version
42-
"$(BUILDER)" run "$(IMAGE)" weechat-headless --version
55+
$(BUILDER) run --rm --platform "$(PLATFORM)" "$(TEST_IMAGE):$(TEST_VERSION)" weechat --version
56+
$(BUILDER) run --rm --platform "$(PLATFORM)" "$(TEST_IMAGE):$(TEST_VERSION)" weechat-headless --version
57+
58+
clean-images:
59+
$(BUILDER) images --format="{{.Repository}}:{{.Tag}}" $(IMAGE) | xargs --no-run-if-empty $(BUILDER) rmi -f
60+
$(BUILDER) images --format="{{.Repository}}:{{.Tag}}" $(ALPINE_BASE_IMAGE) | xargs --no-run-if-empty $(BUILDER) rmi -f
61+
$(BUILDER) images --format="{{.Repository}}:{{.Tag}}" $(DEBIAN_BASE_IMAGE) | xargs --no-run-if-empty $(BUILDER) rmi -f
62+
$(BUILDER) image prune -f
4363

4464
lint: flake8 pylint mypy bandit
4565

README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,23 @@ The slim version includes all plugins except these ones:
3131
- scripting languages: perl, python, ruby, lua, tcl, guile, php
3232
- spell
3333

34+
## Supported platforms
35+
36+
It's possible to build for different linux platforms other than amd64,
37+
the only prerequisite is to have `qemu-user-static` package installed
38+
39+
- `linux/i386`
40+
- `linux/amd64`
41+
- `linux/arm`
42+
- `linux/arm/v5`
43+
- `linux/arm/v6`
44+
- `linux/arm/v7`
45+
- `linux/arm64`
46+
- `linux/arm64/v8`
47+
- `linux/ppc64le`
48+
- `linux/s390x`
49+
50+
3451
## Install from Docker Hub
3552

3653
You can install directly the latest version from the Docker Hub:
@@ -72,6 +89,20 @@ Build all images with latest stable version of WeeChat:
7289
$ make all-images
7390
```
7491

92+
Build all images with latest stable version of WeeChat for ARM platform:
93+
94+
```
95+
$ make PLATFORM=linux/arm all-images
96+
```
97+
98+
Once the images are built it's necessary to clean the images before building
99+
for another platform or the build will fail:
100+
101+
```
102+
$ make clean-images
103+
$ make PLATFORM=linux/ppc64le all-images
104+
```
105+
75106
Build an Alpine-based image with Podman, slim version, WeeChat 3.6:
76107

77108
```

build.py

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,53 @@
1818

1919
"""Build WeeChat container image."""
2020

21-
from typing import List, Sequence, Tuple
21+
from typing import List, Sequence, Tuple, Dict
2222

2323
import argparse
2424
import urllib.request
2525
import subprocess # nosec
2626

27+
BUILDERS: Sequence[str] = (
28+
"docker",
29+
"podman",
30+
)
31+
2732
DISTROS: Sequence[str] = (
2833
"debian",
2934
"alpine",
3035
)
3136

37+
SUPPORTED_PLATFORMS: Dict[str, Dict[str, List[str]]] = {
38+
"linux": {
39+
"i386": [],
40+
"amd64": [],
41+
"arm": ["v5", "v6", "v7"],
42+
"arm64": ["v8"],
43+
"ppc64le": [],
44+
"s390x": [],
45+
},
46+
}
47+
48+
49+
def generate_valid_platforms() -> list[str]:
50+
"""Return the list of supported platforms."""
51+
valid_platforms = []
52+
for os_name, archs in SUPPORTED_PLATFORMS.items():
53+
for arch, variants in archs.items():
54+
valid_platforms.append(f"{os_name}/{arch}")
55+
for variant in variants:
56+
valid_platforms.append(f"{os_name}/{arch}/{variant}")
57+
return valid_platforms
58+
3259

3360
def get_parser() -> argparse.ArgumentParser:
3461
"""Return the command line parser."""
3562
parser = argparse.ArgumentParser(description="Build of WeeChat container")
3663
parser.add_argument(
3764
"-b",
3865
"--builder",
39-
default="docker",
66+
choices=BUILDERS,
67+
default=BUILDERS[0],
4068
help=(
4169
"program used to build the container image, "
4270
"like docker (default) or podman"
@@ -49,6 +77,13 @@ def get_parser() -> argparse.ArgumentParser:
4977
default=DISTROS[0],
5078
help="base Linux distribution for the container",
5179
)
80+
parser.add_argument(
81+
"-p",
82+
"--platform",
83+
default="linux/amd64",
84+
choices=generate_valid_platforms(),
85+
help="platform for the container",
86+
)
5287
parser.add_argument(
5388
"-n",
5489
"--dry-run",
@@ -130,6 +165,8 @@ def main() -> None:
130165
command = [
131166
f"{args.builder}",
132167
"build",
168+
"--platform",
169+
f"{args.platform}",
133170
"-f",
134171
f"{args.distro}/Containerfile",
135172
"--build-arg",

0 commit comments

Comments
 (0)