Skip to content

Commit

Permalink
Build: add support for building on OpenBSD
Browse files Browse the repository at this point in the history
This requires gcc8 from ports to build the Toolchain.
  • Loading branch information
jcs authored and awesomekling committed Jan 2, 2020
1 parent d611319 commit 5e430e4
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 26 deletions.
7 changes: 7 additions & 0 deletions Documentation/BuildInstructions.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ Notes:
- osxfuse, e2fsprogs, m4, autoconf, automake, libtool and `BuildFuseExt2.sh` are needed if you want to build the root filesystem disk image natively on macOS. This allows mounting an EXT2 fs and also installs commands like `mke2fs` that are not available on stock macOS.
- If you install some commercial EXT2 macOS fs handler instead of osxfuse and fuse-ext2, you will need to `brew install e2fsprogs` to obtain `mke2fs` anyway.

### OpenBSD prerequisites
```
pkg_add bash gmp gcc git flock gmake sudo
```

When building with `make`, `gmake` must be used. The `makeall.sh` script will do this automatically when building on OpenBSD.

### Build
Go into the `Toolchain/` directory and run the **BuildIt.sh** script.

Expand Down
17 changes: 14 additions & 3 deletions Kernel/build-image-qemu.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ die() {
if [ "$(id -u)" != 0 ]; then
die "this script needs to run as root"
fi
if [ "$(uname)" = "Darwin" ]; then
if [ "$(uname -s)" = "Darwin" ]; then
export PATH="/usr/local/opt/e2fsprogs/bin:$PATH"
export PATH="/usr/local/opt/e2fsprogs/sbin:$PATH"
fi
Expand All @@ -20,13 +20,21 @@ chown "$build_user":"$build_group" _disk_image || die "couldn't adjust permissio
echo "done"

printf "creating new filesystem... "
mke2fs -q -I 128 _disk_image || die "couldn't create filesystem"
if [ "$(uname -s)" = "OpenBSD" ]; then
VND=`vnconfig _disk_image`
(echo "e 0"; echo 83; echo n; echo 0; echo "*"; echo "quit") | fdisk -e $VND
mkfs.ext2 -I 128 -F /dev/${VND}i || die "couldn't create filesystem"
else
mke2fs -q -I 128 _disk_image || die "couldn't create filesystem"
fi
echo "done"

printf "mounting filesystem... "
mkdir -p mnt
if [ "$(uname)" = "Darwin" ]; then
if [ "$(uname -s)" = "Darwin" ]; then
fuse-ext2 _disk_image mnt -o rw+,allow_other,uid=501,gid=20 || die "couldn't mount filesystem"
elif [ "$(uname -s)" = "OpenBSD" ]; then
mount -t ext2fs /dev/${VND}i mnt/ || die "couldn't mount filesystem"
else
mount _disk_image mnt/ || die "couldn't mount filesystem"
fi
Expand All @@ -37,6 +45,9 @@ cleanup() {
printf "unmounting filesystem... "
umount mnt || ( sleep 1 && sync && umount mnt )
rm -rf mnt
if [ "$(uname -s)" = "OpenBSD" ]; then
vnconfig -u $VND
fi
echo "done"
fi
}
Expand Down
8 changes: 5 additions & 3 deletions Kernel/build-root-filesystem.sh
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,12 @@ echo "done"

printf "installing userland... "

if [ "$(uname)" != "Darwin" ]; then
find ../Userland/ -type f -executable -exec cp {} mnt/bin/ \;
if [ "$(uname -s)" = "Darwin" ]; then
find ../Userland/ -type f -perm +111 -exec cp {} mnt/bin/ \;
elif [ "$(uname -s)" = "OpenBSD" ]; then
find ../Userland/ -type f -perm -555 -exec cp {} mnt/bin/ \;
else
find ../Userland/ -type f -perm +111 -exec cp {} mnt/bin/ \;
find ../Userland/ -type f -executable -exec cp {} mnt/bin/ \;
fi
chmod 4755 mnt/bin/su
chmod 4755 mnt/bin/ping
Expand Down
14 changes: 10 additions & 4 deletions Kernel/makeall.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,14 @@ export build_group

sudo id

make -C ../ clean && \
make -C ../ && \
make -C ../ test && \
make -C ../ install &&
MAKE=make

if [ "$(uname -s)" = "OpenBSD" ]; then
MAKE=gmake
fi

$MAKE -C ../ clean && \
$MAKE -C ../ && \
$MAKE -C ../ test && \
$MAKE -C ../ install &&
sudo -E PATH="$PATH" ./build-image-qemu.sh
2 changes: 2 additions & 0 deletions Libraries/LibC/limits.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

#include <bits/stdint.h>

#ifndef PAGE_SIZE
#define PAGE_SIZE 4096
#endif

#define PATH_MAX 4096
#if !defined MAXPATHLEN && defined PATH_MAX
Expand Down
5 changes: 5 additions & 0 deletions Makefile.common
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ INCLUDE_FLAGS += \
VERBOSE = 0

ifneq ($(USE_HOST_CXX),)
UNAME_S := $(shell uname -s)
ifeq ($(UNAME_S),OpenBSD)
HOST_CXX ?= clang++
endif

ifeq '' '$(findstring clang++,$(CXX))'
C_WARNING_FLAGS += -Wno-unknown-warning-option
CXX_WARNING_FLAGS += -Wno-unknown-warning-option
Expand Down
54 changes: 38 additions & 16 deletions Toolchain/BuildIt.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/bash
#!/usr/bin/env bash
set -e

# This file will need to be run in bash, for now.
Expand All @@ -12,6 +12,20 @@ TARGET="$ARCH-pc-serenity"
PREFIX="$DIR/Local"
SYSROOT="$DIR/../Root"

MAKE=make
MD5SUM=md5sum
NPROC=nproc

if [ `uname -s` = "OpenBSD" ]; then
MAKE=gmake
MD5SUM="md5 -q"
NPROC="sysctl -n hw.ncpuonline"
export CC=egcc
export CXX=eg++
export with_gmp=/usr/local
export LDFLAGS=-Wl,-z,notext
fi

echo PREFIX is "$PREFIX"
echo SYSROOT is "$SYSROOT"

Expand All @@ -30,7 +44,7 @@ GCC_PKG="${GCC_NAME}.tar.gz"
GCC_BASE_URL="http:https://ftp.gnu.org/gnu/gcc"

pushd "$DIR/Tarballs"
md5="$(md5sum $BINUTILS_PKG | cut -f1 -d' ')"
md5="$($MD5SUM $BINUTILS_PKG | cut -f1 -d' ')"
echo "bu md5='$md5'"
if [ ! -e $BINUTILS_PKG ] || [ "$md5" != ${BINUTILS_MD5SUM} ] ; then
rm -f $BINUTILS_PKG
Expand All @@ -39,7 +53,7 @@ pushd "$DIR/Tarballs"
echo "Skipped downloading binutils"
fi

md5="$(md5sum ${GCC_PKG} | cut -f1 -d' ')"
md5="$($MD5SUM ${GCC_PKG} | cut -f1 -d' ')"
echo "gc md5='$md5'"
if [ ! -e $GCC_PKG ] || [ "$md5" != ${GCC_MD5SUM} ] ; then
rm -f $GCC_PKG
Expand All @@ -50,7 +64,7 @@ pushd "$DIR/Tarballs"

if [ ! -d ${BINUTILS_NAME} ]; then
echo "Extracting binutils..."
tar -xf ${BINUTILS_PKG}
tar -xzf ${BINUTILS_PKG}

pushd ${BINUTILS_NAME}
git init >/dev/null
Expand All @@ -64,7 +78,7 @@ pushd "$DIR/Tarballs"

if [ ! -d $GCC_NAME ]; then
echo "Extracting gcc..."
tar -xf $GCC_PKG
tar -xzf $GCC_PKG

pushd $GCC_NAME
git init >/dev/null
Expand All @@ -90,7 +104,7 @@ mkdir -p "$DIR/Build/binutils"
mkdir -p "$DIR/Build/gcc"

if [ -z "$MAKEJOBS" ]; then
MAKEJOBS=$(nproc)
MAKEJOBS=$($NPROC)
fi

pushd "$DIR/Build/"
Expand All @@ -106,16 +120,20 @@ pushd "$DIR/Build/"
# under macOS generated makefiles are not resolving the "intl"
# dependency properly to allow linking its own copy of
# libintl when building with --enable-shared.
make -j "$MAKEJOBS" || true
"$MAKE" -j "$MAKEJOBS" || true
pushd intl
make all-yes
"$MAKE" all-yes
popd
fi
make -j "$MAKEJOBS" || exit 1
make install || exit 1
"$MAKE" -j "$MAKEJOBS" || exit 1
"$MAKE" install || exit 1
popd

pushd gcc
if [ `uname -s` = "OpenBSD" ]; then
perl -pi -e 's/-no-pie/-nopie/g' "$DIR"/Tarballs/gcc-9.2.0/gcc/configure
fi

"$DIR"/Tarballs/gcc-9.2.0/configure --prefix="$PREFIX" \
--target="$TARGET" \
--with-sysroot="$SYSROOT" \
Expand All @@ -125,18 +143,22 @@ pushd "$DIR/Build/"
--enable-languages=c,c++ || exit 1

echo "XXX build gcc and libgcc"
make -j "$MAKEJOBS" all-gcc all-target-libgcc || exit 1
"$MAKE" -j "$MAKEJOBS" all-gcc all-target-libgcc || exit 1
echo "XXX install gcc and libgcc"
make install-gcc install-target-libgcc || exit 1
"$MAKE" install-gcc install-target-libgcc || exit 1

echo "XXX serenity libc and libm"
( cd "$DIR/../Libraries/LibC/" && make clean && make && make install )
( cd "$DIR/../Libraries/LibM/" && make clean && make && make install )
( cd "$DIR/../Libraries/LibC/" && "$MAKE" clean && "$MAKE" && "$MAKE" install )
( cd "$DIR/../Libraries/LibM/" && "$MAKE" clean && "$MAKE" && "$MAKE" install )

echo "XXX build libstdc++"
make all-target-libstdc++-v3 || exit 1
"$MAKE" all-target-libstdc++-v3 || exit 1
echo "XXX install libstdc++"
make install-target-libstdc++-v3 || exit 1
"$MAKE" install-target-libstdc++-v3 || exit 1

if [ `uname -s` = "OpenBSD" ]; then
cd "$DIR"/Local/libexec/gcc/i686-pc-serenity/9.2.0 && ln -sf liblto_plugin.so.0.0 liblto_plugin.so
fi
popd
popd

0 comments on commit 5e430e4

Please sign in to comment.