From a419f01538607677b2bb599dda0c35a437316b3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Marie?= Date: Mon, 8 Nov 2021 18:54:45 +0000 Subject: [PATCH] initial import --- README.md | 63 ++++++++++++++++++++++++++++++++++++++ build-ziglibc.sh | 79 ++++++++++++++++++++++++++++++++++++++++++++++++ tests/hello.c | 9 ++++++ tests/hello.zig | 5 +++ 4 files changed, 156 insertions(+) create mode 100644 README.md create mode 100755 build-ziglibc.sh create mode 100644 tests/hello.c create mode 100644 tests/hello.zig diff --git a/README.md b/README.md new file mode 100644 index 0000000..2b03227 --- /dev/null +++ b/README.md @@ -0,0 +1,63 @@ +OpenBSD ZIG_LIBC support +======================== + +Simple shell script to build a `ZIG_LIBC` environment for [Zig](https://ziglang.org/) targeting [OpenBSD](https://www.openbsd.org/). + +Usage +----- + +``` +$ ./build-ziglibc.sh +usage: ./build-ziglibc.sh url output-dir +``` + +- *url* : URL pointing to OpenBSD sets. + + Typical url to use: https://cdn.openbsd.org/pub/OpenBSD/*version*/*platform*/ + + Please note that OpenBSD officially support only the two last + released versions. Snapshots url is also valid for targeting + -current, but be aware that it is a moving target. + + Refer to [supported platforms](https://www.openbsd.org/plat.html) + list from OpenBSD site web for the name used. + +- *output-dir* : where to put the environment. + + Prefer absolute path: it will be used in generated `libc.conf` file. + + The directory will be created. + + +Examples of url +--------------- + +- OpenBSD 6.9 [amd64](https://www.openbsd.org/amd64.html) (x86_64 on Linux world) + + https://cdn.openbsd.org/pub/OpenBSD/6.9/amd64/ + +- OpenBSD -current [i386](https://www.openbsd.org/i386.html) + + https://cdn.openbsd.org/pub/OpenBSD/snapshots/i386/ + + +Use the environment +------------------- + +``` +$ ZIG_LIBC=path/to/amd64/libc.conf zig build-exe -target x86_64-openbsd -o test/helloz test/hello.zig +$ ZIG_LIBC=path/to/amd64/libc.conf zig cc -target x86_64-openbsd -o test/helloc test/hello.c +``` + +Make shell wrapper +------------------ + +For easy use of crosscompilation, you could create a shell wrapper +named `x86_64-openbsd-cc`: + +``` +#!/bin/sh +exec env ZIG_LIBC=path/to/amd64/libc.conf zig cc -target x86_64-openbsd "$@" +``` + +And do the same for `x86_64-openbsd-c++`, `x86_64-openbsd-ar`, … diff --git a/build-ziglibc.sh b/build-ziglibc.sh new file mode 100755 index 0000000..57283bd --- /dev/null +++ b/build-ziglibc.sh @@ -0,0 +1,79 @@ +#!/bin/sh +set -eu + +# tool used to download. url as argument, output to stdout. +DOWNLOAD_BIN=${DOWNLOAD_BIN:-ftp -Vo-} +#DOWNLOAD_BIN=${DOWNLOAD_BIN:-curl -s} + +if [ $# -ne 2 ]; then + cat >&2 <&2 + exit 0 +fi + +echo "- Getting OpenBSD version" >&2 +target_version=`${DOWNLOAD_BIN} "${url}/SHA256" \ + | sed -ne 's,.*(base\([0-9]\)\([0-9]\)\.tgz).*,\1\2,p'` + +echo "- Downloading and extracting libc environment" >&2 +mkdir -p "${target_directory}" +cat >"${target_directory}/CACHEDIR.TAG" <&2 +for lib in "${target_directory}"/usr/lib/lib*.so.*.* ; do + ln -fs -- "${lib}" "${lib%.*.*}" +done + +echo "- Creating ZIG_LIBC file" >&2 +cat >"${target_directory}/libc.conf" < +#include + +int +main(int argc, char *argv[]) +{ + printf("Hello world\n"); + return EXIT_SUCCESS; +} diff --git a/tests/hello.zig b/tests/hello.zig new file mode 100644 index 0000000..7a8e7be --- /dev/null +++ b/tests/hello.zig @@ -0,0 +1,5 @@ +const std = @import("std"); + +pub fn main() void { + std.debug.print("Hello world\n", .{}); +}