initial import

master
Sébastien Marie 2021-11-08 18:54:45 +00:00
commit a419f01538
4 changed files with 156 additions and 0 deletions

63
README.md Normal file
View File

@ -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`, …

79
build-ziglibc.sh Executable file
View File

@ -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 <<EOF
usage: ${0} url output-dir
examples:
- ${0} https://cdn.openbsd.org/pub/OpenBSD/6.9/amd64/ \${PWD}/6.9/amd64
- ${0} https://cdn.openbsd.org/pub/OpenBSD/snapshots/i386/ \${PWD}/snapshots/i386
EOF
exit 1
fi
url="${1}"
target_directory="${2}"
if [ -r "${target_directory}/libc.conf" ] ; then
echo "warn: ${target_directory}/libc.conf already exists" >&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" <<EOF
Signature: 8a477f597d28d172789f06886806bc55
# This file is a cache directory tag created by ${0}.
# For information about cache directory tags see https://bford.info/cachedir/
EOF
${DOWNLOAD_BIN} "${url}/base${target_version}.tgz" \
| tar zxf - -C "${target_directory}" ./usr/include ./usr/lib
${DOWNLOAD_BIN} "${url}/comp${target_version}.tgz" \
| tar zxf - -C "${target_directory}" ./usr/include ./usr/lib
echo "- Creating linux-like library links" >&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" <<EOF
# The directory that contains \`stdlib.h\`.
# On POSIX-like systems, include directories be found with: \`cc -E -Wp,-v -xc /dev/null\`
include_dir=${target_directory}/usr/include
# The system-specific include directory. May be the same as \`include_dir\`.
# On Windows it's the directory that includes \`vcruntime.h\`.
# On POSIX it's the directory that includes \`sys/errno.h\`.
sys_include_dir=${target_directory}/usr/include
# The directory that contains \`crt1.o\` or \`crt2.o\`.
# On POSIX, can be found with \`cc -print-file-name=crt1.o\`.
# Not needed when targeting MacOS.
crt_dir=${target_directory}/usr/lib
# The directory that contains \`vcruntime.lib\`.
# Only needed when targeting MSVC on Windows.
msvc_lib_dir=
# The directory that contains \`kernel32.lib\`.
# Only needed when targeting MSVC on Windows.
kernel32_lib_dir=
# The directory that contains \`crtbeginS.o\` and \`crtendS.o\`
# Only needed when targeting Haiku.
gcc_dir=
EOF
echo "ZIG_LIBC=${target_directory}/libc.conf"

9
tests/hello.c Normal file
View File

@ -0,0 +1,9 @@
#include <stdio.h>
#include <stdlib.h>
int
main(int argc, char *argv[])
{
printf("Hello world\n");
return EXIT_SUCCESS;
}

5
tests/hello.zig Normal file
View File

@ -0,0 +1,5 @@
const std = @import("std");
pub fn main() void {
std.debug.print("Hello world\n", .{});
}