Skip to content

Commit

Permalink
zig build
Browse files Browse the repository at this point in the history
  • Loading branch information
kassane committed Aug 21, 2023
1 parent 34e8649 commit 82cbd38
Show file tree
Hide file tree
Showing 15 changed files with 378 additions and 13 deletions.
27 changes: 14 additions & 13 deletions README.md

Large diffs are not rendered by default.

Binary file added assets/img/zig-console.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 21 additions & 0 deletions templates.json
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,27 @@
"mainFileBreakpointLine": 12,
"successDebugMessage": "Hello Torizon!"
}
},
{
"img": "zig-console.png",
"folder": "zigConsole",
"description": "Zig Console Application",
"runtime": "no-libc",
"language": "Zig",
"debugSSHPort": "2320",
"multiContainer": true,
"support": "Community",
"tests" : {
"debuggers": {
"local": ["Local AMD64"],
"arm": ["Torizon ARMv7"],
"arm64": ["Torizon ARMv8"]
},
"debuggerConsole": "cppdbg",
"mainFile": "src/main.zig",
"mainFileBreakpointLine": 2,
"successDebugMessage": "info(toradex): Hello Torizon!"
}
}
]
}
8 changes: 8 additions & 0 deletions zigConsole/.conf/deps.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"packages": [
"openssh-client",
"sshpass",
"zig",
"zls"
]
}
27 changes: 27 additions & 0 deletions zigConsole/.conf/id_rsa
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
-----BEGIN RSA PRIVATE KEY-----
MIIEpQIBAAKCAQEAqpUaOa6i/TaSKwpdsHzLfGK95dDcFYpmYH5hUj0cBhVEVdl8
JAG0MEDcZsjQHiBlfUiHmyU2iOWBoKUytpMnzYF6ggdZBv5hBMv3RcTlnNijxzxg
KvbjTm3LqAY8uu4SF9I8oDXVOn7xwZYgiC7s3XRGOP6TowSVEHsLB+cehWs4Y5io
3BngOlcOYZtVrh+i3A7xSHWP7n2IetJEo5C425oaOCZXUJT2egau6ODmvBItZjpG
mNdwVwrEI2H3o+LLI9tieN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JS6at
Y2xc8hkpY9k5sF6L3F0K9IhrMnYj6GzfAiJgs1gk29rCzQAWjLUi/v/zIQLHvuMy
Xwo3iUUCgYEAo/27Qkb5nOdL8sJwFggDrdB1pPrxXN20KmYCJF1P9wPDhnj1fJ7+
0Z/56XYzPA2rQx0vf5idvoGQ3KZS7QkOClLtcyevH3b38fnmAqv+dPLRHmrSd+3G
BspdMlr3rwZK8RKwXjDzLYpwSvjmf7PTGTPO3C7WTzg+m0U9RbSJKi4=
-----END RSA PRIVATE KEY-----
1 change: 1 addition & 0 deletions zigConsole/.conf/id_rsa.pub
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCqlRo5rqL9NpIrCl2wfMt8Yr3l0NwVimZgfmFSPRwGFURV2XwkAbQwQNxmyNAeIGV9SIebJTaI5YGgpTK2kyfNgXqCB1kG/mEEy/dFxOWc2KPHPGAq9uNObcuoBjy67hIX0jygNdU6fvHBliCILuzddEY4/pOjBJUQewsH5x6FazhjmKjcGeA6Vw5hm1WuH6LcDvFIdY/ufYh60kSjkLjbmho4JldQlPZ6Bq7o4Oa8Ei1mOkaY13BXCsQjYfej4ssj22J41/IVxQU80NFduS7VXSZdT1WNntYWKFlehvXVc0ISRjsGDdKnNOe1JGf2FkPuLfENT0xInP3SuefnogPX
6 changes: 6 additions & 0 deletions zigConsole/.conf/update.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[
{
"source": ".vscode/launch.json",
"target": ".vscode/launch.json"
}
]
1 change: 1 addition & 0 deletions zigConsole/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
credentials.zip
1 change: 1 addition & 0 deletions zigConsole/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
zig-*/
106 changes: 106 additions & 0 deletions zigConsole/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
ARG CROSS_SDK_BASE_TAG=3.0.9-bookworm
ARG BASE_VERSION=3.0.8-bookworm
##
# Board architecture
# arm or arm64
##
ARG IMAGE_ARCH=

##
# Application Name
##
ARG APP_EXECUTABLE=__change__


# BUILD ------------------------------------------------------------------------
FROM torizon/debian-cross-toolchain-${IMAGE_ARCH}:${CROSS_SDK_BASE_TAG} As Build

ARG IMAGE_ARCH
ARG COMPILER_ARCH
ENV IMAGE_ARCH ${IMAGE_ARCH}
ARG HOST_ARCH
ARG TARGET

# __deps__
RUN apt-get -q -y update && \
apt-get -q -y install \
wget \
# DO NOT REMOVE THIS LABEL: this is used for VS Code automation
# __torizon_packages_dev_start__
# __torizon_packages_dev_end__
# DO NOT REMOVE THIS LABEL: this is used for VS Code automation
&& \
apt-get clean && apt-get autoremove && \
rm -rf /var/lib/apt/lists/*
# __deps__

RUN if [ "$IMAGE_ARCH" = "arm64" ] ; then \
HOST_ARCH=aarch64 ; \
elif [ "$IMAGE_ARCH" = "amd64" ] ; then \
HOST_ARCH=x86_64 ; \
fi

# Install Zig 0.11.0 (uses LLVM-16.0.6)
RUN wget -q https://github.com/marler8997/zigup/releases/download/v2023_07_27/zigup.ubuntu-latest-${HOST_ARCH}.zip && \
unzip zigup.ubuntu-latest-${HOST_ARCH}.zip -d /usr/bin && \
chmod +x /usr/bin/zigup && \
zigup 0.11.0

ARG UNAME=ziguana
ARG UID=1000
ARG GID=1000
RUN groupadd -g $GID -o $UNAME
RUN useradd -m -u $UID -g $GID -o -s /bin/bash $UNAME

USER $UNAME

# default linux-libc: host - glibc (most), cross-compilation - musl (static)
# zig pure-code (no ffi), no need libc linking

# for glibc choose version:
# e.g.: -Dtarget=$TARGET-gnu.2.20 (my host has 2.36 [default], but my target use old version)
# for cross-compilation to glibc, use minimum version (2.19) supported.

# C++: zig uses llvm-libcxx/abi + llvm-unwind on almost all (LLVM)targets. [builtin static-linking]

RUN if [ "$IMAGE_ARCH" = "arm64" ] ; then \
TARGET=aarch64-linux-musl; \
elif [ "$IMAGE_ARCH" = "arm" ] ; then \
TARGET=arm-linux-musl; \
elif [ "$IMAGE_ARCH" = "amd64" ] ; then \
TARGET=x86_64-linux-gnu; \
fi

WORKDIR /app
COPY . /app

# zig optimizers:
# (Debug | ReleaseSafe) w/ dbg-info + runtime stacktrace (compiler safety),
# (ReleaseSmall | ReleaseFast) no runtime stacktrace (undefined & fast)
RUN zig build -Doptimize=ReleaseSmall -Dtarget=$TARGET && \
mkdir -p target/deploy && \
cp zig-deploy/$TARGET/release/bin/$APP_EXECUTABLE target/deploy

# BUILD ------------------------------------------------------------------------

# DEPLOY ------------------------------------------------------------------------
FROM --platform=linux/${IMAGE_ARCH} torizon/debian:${BASE_VERSION} AS Deploy

ARG IMAGE_ARCH
ARG APP_EXECUTABLE
ENV APP_EXECUTABLE ${APP_EXECUTABLE}

RUN apt-get -y update && apt-get install -y --no-install-recommends \
# DO NOT REMOVE THIS LABEL: this is used for VS Code automation
# __torizon_packages_prod_start__
# __torizon_packages_prod_end__
# DO NOT REMOVE THIS LABEL: this is used for VS Code automation
&& apt-get clean && apt-get autoremove && rm -rf /var/lib/apt/lists/*

# copy the build
COPY --from=Build /app/target/deploy/${APP_EXECUTABLE} /usr/bin/${APP_EXECUTABLE}

# ADD YOUR ARGUMENTS HERE
CMD ${APP_EXECUTABLE}

# DEPLOY ------------------------------------------------------------------------
94 changes: 94 additions & 0 deletions zigConsole/Dockerfile.debug
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
# ARGUMENTS --------------------------------------------------------------------
##
# Board architecture
##
ARG IMAGE_ARCH=
# For armv7 use:
#ARG IMAGE_ARCH=arm

##
# Base container version
##
ARG BASE_VERSION=3.0.8-bookworm

##
# Application Name
##
ARG APP_EXECUTABLE=app

##
# Debug port
##
ARG SSH_DEBUG_PORT=

##
# Run as
##
ARG SSHUSERNAME=

# BUILD ------------------------------------------------------------------------
##
# Deploy Step
##
FROM --platform=linux/${IMAGE_ARCH} \
torizon/debian:${BASE_VERSION} AS Debug

ARG IMAGE_ARCH
ARG SSH_DEBUG_PORT
ARG APP_EXECUTABLE
ARG SSHUSERNAME
ENV APP_EXECUTABLE ${APP_EXECUTABLE}

# SSH for remote debug
EXPOSE ${SSH_DEBUG_PORT}

# Make sure we don't get notifications we can't answer during building.
ENV DEBIAN_FRONTEND="noninteractive"

# your regular RUN statements here
# Install required packages
RUN apt-get -q -y update && \
apt-get -q -y install \
openssl \
openssh-server \
rsync \
file \
curl \
gdb && \
apt-get clean && apt-get autoremove && \
rm -rf /var/lib/apt/lists/*

# automate for torizonPackages.json
RUN apt-get -q -y update && \
apt-get -q -y install \
# DO NOT REMOVE THIS LABEL: this is used for VS Code automation
# __torizon_packages_dev_start__
# __torizon_packages_dev_end__
# DO NOT REMOVE THIS LABEL: this is used for VS Code automation
&& \
apt-get clean && apt-get autoremove && \
rm -rf /var/lib/apt/lists/*

# ⚠️ DEBUG PURPOSES ONLY!!
# copies RSA key to enable SSH login for user
COPY .conf/id_rsa.pub /id_rsa.pub

# create folders needed for the different components
# configures SSH access to the container and sets environment by default
RUN mkdir /var/run/sshd && \
sed 's@session\s*required\s*pam_loginuid.so@session optional pam_loginuid.so@g' \
-i /etc/pam.d/sshd && \
if test $SSHUSERNAME != root ; \
then mkdir -p /home/$SSHUSERNAME/.ssh ; \
else mkdir -p /root/.ssh ; fi && \
if test $SSHUSERNAME != root ; \
then cp /id_rsa.pub /home/$SSHUSERNAME/.ssh/authorized_keys ; \
else cp /id_rsa.pub /root/.ssh/authorized_keys ; fi && \
echo "PermitUserEnvironment yes" >> /etc/ssh/sshd_config && \
echo "Port ${SSH_DEBUG_PORT}" >> /etc/ssh/sshd_config && \
su -c "env" $SSHUSERNAME > /etc/environment

RUN rm -r /etc/ssh/ssh*key && \
dpkg-reconfigure openssh-server

CMD [ "/usr/sbin/sshd", "-D" ]
34 changes: 34 additions & 0 deletions zigConsole/Dockerfile.sdk
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# ARGUMENTS --------------------------------------------------------------------
ARG CROSS_SDK_BASE_TAG=3.0.9-bookworm

##
# Board architecture
# arm or arm64
##
ARG IMAGE_ARCH=


# BUILD ------------------------------------------------------------------------
FROM torizon/debian-cross-toolchain-${IMAGE_ARCH}:${CROSS_SDK_BASE_TAG}

# __deps__
RUN apt-get -q -y update && \
apt-get -q -y install \
# ADD YOUR PACKAGES HERE
&& \
apt-get clean && apt-get autoremove && \
rm -rf /var/lib/apt/lists/*
# __deps__

# automate for torizonPackages.json
RUN apt-get -q -y update && \
apt-get -q -y install \
# DO NOT REMOVE THIS LABEL: this is used for VS Code automation
# __torizon_packages_dev_start__
# __torizon_packages_dev_end__
# DO NOT REMOVE THIS LABEL: this is used for VS Code automation
&& \
apt-get clean && apt-get autoremove && \
rm -rf /var/lib/apt/lists/*

WORKDIR /app
38 changes: 38 additions & 0 deletions zigConsole/build.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const std = @import("std");
const Path = std.Build.LazyPath;

pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});

const mode = switch (optimize) {
.ReleaseFast, .ReleaseSafe, .ReleaseSmall => "release",
else => "debug",
};
const triple_target = target.linuxTriple(b.allocator) catch &.{};

const binary = b.addExecutable(.{
.name = "__change__",
.root_source_file = Path.relative("src/main.zig"),
// or (implicit LazyPath struct, w/ path field)
// .root_source_file = .{ .path = "src/main.zig" },
.target = target,
.optimize = optimize,
});

// Need use libc to C code build? uncomment below
// build C and/or C++
// binary.addCSourceFile(.{ .file = Path.relative("src/main.c"), .flags = &.{ "-Wall", "-Wextra" } });
// binary.addCSourceFiles(&.{ "foo.cc", "bar.cc" }, &.{ "-Wall", "-std=c++17" });
// warning to mixing C and C++ code. Use same flags, but c++flags(exclusive) not working (e.g.: "-std=c++11")

// linking
// binary.linklibC() // get libc and c-stdlib
// for C++
// binary.linklibCpp(); // builtin llvm-libcxx/abi + libunwind (+ stl) + libc (avoid duplicate - no need linklibC())

// copy binary from zig-cache to zig-out/bin [default]
b.installArtifact(binary);
// overwritten: copy binary from zig-cache to zig-deploy/{triple-target}/{mode}/bin
b.resolveInstallPrefix(b.fmt("zig-deploy/{s}/{s}", .{ triple_target, mode }), .{});
}
15 changes: 15 additions & 0 deletions zigConsole/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
version: "3.9"
services:
__container__-debug:
build:
context: .
dockerfile: Dockerfile.debug
image: ${LOCAL_REGISTRY}:5002/__container__-debug:${TAG}
ports:
- 2230:2230

__container__:
build:
context: .
dockerfile: Dockerfile
image: ${DOCKER_LOGIN}/__container__:${TAG}
12 changes: 12 additions & 0 deletions zigConsole/src/main.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const std = @import("std");
const log = std.log.scoped(.toradex);

// override the std implementation
pub const std_options = struct {
pub const log_level = .info;
};
pub fn main() void {
log.info("Hello {s}!\n", .{"Torizon"});
// output:
// info(toradex): Hello Torizon!
}

0 comments on commit 82cbd38

Please sign in to comment.