Skip to content

Commit

Permalink
Support cross platform builds
Browse files Browse the repository at this point in the history
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
  • Loading branch information
mmartinv committed Dec 2, 2022
1 parent 04f7bab commit 21b985d
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 7 deletions.
32 changes: 26 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,50 @@
#

BUILDER ?= docker
PLATFORM ?= linux/amd64
VERSION ?= latest
IMAGE ?= weechat

ALPINE_BASE_IMAGE = alpine:3.15
DEBIAN_BASE_IMAGE = debian:bullseye-slim

TEST_IMAGE = $(IMAGE)
ifeq ($(strip $(BUILDER)),podman)
TEST_IMAGE=localhost/$(IMAGE)
endif

TEST_VERSION = $(VERSION)
ifeq ($(strip $(TEST_VERSION)),$(filter $(VERSION),latest stable))
TEST_VERSION=$(shell curl "https://weechat.org/dev/info/stable/")
endif

.PHONY: all debian debian-slim alpine alpine-slim

all: debian

all-images: debian debian-slim alpine alpine-slim

debian:
./build.py -b "$(BUILDER)" -d "debian" "$(VERSION)"
./build.py -b "$(BUILDER)" -p "$(PLATFORM)" -d "debian" "$(VERSION)"

debian-slim:
./build.py -b "$(BUILDER)" -d "debian" --slim "$(VERSION)"
./build.py -b "$(BUILDER)" -p "$(PLATFORM)" -d "debian" --slim "$(VERSION)"

alpine:
./build.py -b "$(BUILDER)" -d "alpine" "$(VERSION)"
./build.py -b "$(BUILDER)" -p "$(PLATFORM)" -d "alpine" "$(VERSION)"

alpine-slim:
./build.py -b "$(BUILDER)" -d "alpine" --slim "$(VERSION)"
./build.py -b "$(BUILDER)" -p "$(PLATFORM)" -d "alpine" --slim "$(VERSION)"

test-container:
"$(BUILDER)" run "$(IMAGE)" weechat --version
"$(BUILDER)" run "$(IMAGE)" weechat-headless --version
$(BUILDER) run --rm --platform "$(PLATFORM)" "$(TEST_IMAGE):$(TEST_VERSION)" weechat --version
$(BUILDER) run --rm --platform "$(PLATFORM)" "$(TEST_IMAGE):$(TEST_VERSION)" weechat-headless --version

clean-images:
$(BUILDER) images --format="{{.Repository}}:{{.Tag}}" $(IMAGE) | xargs --no-run-if-empty $(BUILDER) rmi -f
$(BUILDER) images --format="{{.Repository}}:{{.Tag}}" $(ALPINE_BASE_IMAGE) | xargs --no-run-if-empty $(BUILDER) rmi -f
$(BUILDER) images --format="{{.Repository}}:{{.Tag}}" $(DEBIAN_BASE_IMAGE) | xargs --no-run-if-empty $(BUILDER) rmi -f
$(BUILDER) image prune -f

lint: flake8 pylint mypy bandit

Expand Down
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,23 @@ The slim version includes all plugins except these ones:
- scripting languages: perl, python, ruby, lua, tcl, guile, php
- spell

## Supported platforms

It's possible to build for different linux platforms other than amd64,
the only prerequisite is to have `qemu-user-static` package installed

- `linux/i386`
- `linux/amd64`
- `linux/arm`
- `linux/arm/v5`
- `linux/arm/v6`
- `linux/arm/v7`
- `linux/arm64`
- `linux/arm64/v8`
- `linux/ppc64le`
- `linux/s390x`


## Install from Docker Hub

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

Build all images with latest stable version of WeeChat for 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
```

Build an Alpine-based image with Podman, slim version, WeeChat 3.6:

```
Expand Down
38 changes: 37 additions & 1 deletion build.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,46 @@
import urllib.request
import subprocess # nosec

BUILDERS: Sequence[str] = (
"docker",
"podman",
)

DISTROS: Sequence[str] = (
"debian",
"alpine",
)

supported_platforms = {
"linux": {
"i386": [],
"amd64": [],
"arm": ["v5", "v6", "v7" ],
"arm64": ["v8"],
"ppc64le": [],
"s390x": [],
}
}

def generate_valid_platforms() -> list[str]:
"""Return the list of supported platforms."""
valid_platforms = []
for os in supported_platforms:
for arch in supported_platforms[os]:
valid_platforms.append(f"{os}/{arch}")
for variant in supported_platforms[os][arch]:
valid_platforms.append(f"{os}/{arch}/{variant}")
return valid_platforms


def get_parser() -> argparse.ArgumentParser:
"""Return the command line parser."""
parser = argparse.ArgumentParser(description="Build of WeeChat container")
parser.add_argument(
"-b",
"--builder",
default="docker",
choices=BUILDERS,
default=BUILDERS[0],
help=(
"program used to build the container image, "
"like docker (default) or podman"
Expand All @@ -49,6 +76,13 @@ def get_parser() -> argparse.ArgumentParser:
default=DISTROS[0],
help="base Linux distribution for the container",
)
parser.add_argument(
"-p",
"--platform",
default="linux/amd64",
choices=generate_valid_platforms(),
help="platform for the container",
)
parser.add_argument(
"-n",
"--dry-run",
Expand Down Expand Up @@ -130,6 +164,8 @@ def main() -> None:
command = [
f"{args.builder}",
"build",
"--platform",
f"{args.platform}",
"-f",
f"{args.distro}/Containerfile",
"--build-arg",
Expand Down

0 comments on commit 21b985d

Please sign in to comment.