openbsd.sys (OpenBSD kernel): init (#353935)

This commit is contained in:
John Ericson 2024-11-14 22:06:48 -05:00 committed by GitHub
commit 63de88ed5f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
16 changed files with 223 additions and 2 deletions

View File

@ -0,0 +1,32 @@
{
mkDerivation,
lib,
flex,
byacc,
compatHook,
}:
mkDerivation {
path = "usr.sbin/config";
extraNativeBuildInputs = [
flex
byacc
compatHook
];
postPatch = ''
rm $BSDSRCDIR/usr.sbin/config/ukc.c
rm $BSDSRCDIR/usr.sbin/config/ukcutil.c
rm $BSDSRCDIR/usr.sbin/config/cmd.c
rm $BSDSRCDIR/usr.sbin/config/exec_elf.c
'';
buildPhase = ''
for f in *.l; do flex $f; done
for f in *.y; do yacc -H ''${f%.y}.h $f; done
for f in *.c; do $CC -I$TMP/include -DMAKE_BOOTSTRAP -c $f; done
$CC *.o -o config
'';
meta.platforms = lib.platforms.linux;
}

View File

@ -0,0 +1,25 @@
{
mkDerivation,
lib,
flex,
byacc,
compatHook,
}:
mkDerivation {
path = "usr.bin/ctags";
extraNativeBuildInputs = [
flex
byacc
compatHook
];
buildPhase = ''
for f in *.l; do flex $f; done
for f in *.y; do yacc -H ''${f%.y}.h $f; done
for f in *.c; do $CC -I$TMP/include -DMAKE_BOOTSTRAP -c $f; done
$CC *.o -o ctags
'';
meta.platforms = lib.platforms.linux;
}

View File

@ -0,0 +1,30 @@
#pragma once
#include_next <err.h>
#include <errno.h>
#include <stdarg.h>
static inline void __attribute__((__format__(printf, 3, 4)))
errc(int eval, int code, const char *fmt, ...) {
// verr uses the error code from errno
// No need to keep the old value since this is noreturn anyway
errno = code;
va_list args;
va_start(args, fmt);
verr(eval, fmt, args);
va_end(args);
}
static inline void __attribute__((__format__(printf, 2, 3)))
warnc(int code, const char *fmt, ...) {
// verr uses the error code from errno
int old_errno = errno;
errno = code;
va_list args;
va_start(args, fmt);
vwarn(fmt, args);
va_end(args);
errno = old_errno;
}

View File

@ -0,0 +1,6 @@
#pragma once
#include_next <fcntl.h>
// Linux doesn't let you lock during open, make these do nothing
#define O_EXLOCK 0
#define O_SHLOCK 0

View File

@ -0,0 +1,4 @@
#include_next <sys/cdefs.h>
#define __packed __attribute__((__packed__))
#define __aligned(x) __attribute__((__aligned__(x)))

View File

@ -0,0 +1 @@
#include <dirent.h>

View File

@ -0,0 +1,2 @@
// Seems to be the only header for htonl
#include <netinet/in.h>

View File

@ -0,0 +1,10 @@
#include_next <sys/types.h>
// for makedev, major, minor
#include <sys/sysmacros.h>
// For htonl, htons, etc.
#include <arpa/inet.h>
// for uint32_t etc.
#include <stdint.h>

View File

@ -0,0 +1,8 @@
#pragma once
#include_next <unistd.h>
// Reimplementing pledge and unvail with seccomp would be a pain,
// so do nothing but claim they succeeded
static int pledge(const char *, const char *) { return 0; }
static int unveil(const char *, const char *) { return 0; }

View File

@ -0,0 +1 @@
#include <sys/types.h>

View File

@ -0,0 +1,16 @@
{ runCommand, lib }:
runCommand "openbsd-compat"
{
include = ./include;
meta = with lib; {
description = "A header-only library for running OpenBSD software on Linux";
platforms = lib.platforms.linux;
maintainers = with lib.maintainers; [ artemist ];
};
}
''
mkdir -p $out
cp -R $include $out/include
''

View File

@ -0,0 +1,13 @@
{
stdenv,
makeSetupHook,
compat,
}:
makeSetupHook {
name = "openbsd-compat-hook";
substitutions = {
inherit compat;
inherit (stdenv.cc) suffixSalt;
};
} ./setup-hook.sh

View File

@ -0,0 +1,5 @@
useOpenBSDCompat () {
export NIX_CFLAGS_COMPILE_@suffixSalt@+="-I@compat@/include"
}
postHooks+=(useOpenBSDCompat)

View File

@ -34,6 +34,8 @@ mkDerivation {
sed -i -E \
-e 's|/usr/lib|\$\{LIBDIR\}|' \
share/mk/bsd.prog.mk
substituteInPlace share/mk/bsd.obj.mk --replace-fail /bin/pwd pwd
'';
installPhase = ''

View File

@ -53,7 +53,7 @@ lib.makeOverridable (
install
tsort
lorder
];
] ++ (attrs.extraNativeBuildInputs or [ ]);
HOST_SH = stdenv'.shell;
@ -93,6 +93,6 @@ lib.makeOverridable (
dontBuild = true;
}
// lib.optionalAttrs stdenv'.hostPlatform.isStatic { NOLIBSHARED = true; }
// attrs
// (builtins.removeAttrs attrs [ "extraNativeBuildInputs" ])
)
)

View File

@ -0,0 +1,66 @@
{
mkDerivation,
boot-config,
pkgsBuildTarget,
baseConfig ? "GENERIC",
}:
mkDerivation {
path = "sys/arch/amd64";
pname = "sys";
extraPaths = [ "sys" ];
noLibc = true;
extraNativeBuildInputs = [
boot-config
];
postPatch =
# The in-kernel debugger (DDB) requires compiler flags not supported by clang, disable it
''
sed -E -i -e '/DDB/d' $BSDSRCDIR/sys/conf/GENERIC
sed -E -i -e '/pseudo-device\tdt/d' $BSDSRCDIR/sys/arch/amd64/conf/GENERIC
''
+
# Clang flags compatibility
''
find $BSDSRCDIR -name 'Makefile*' -exec sed -E -i -e 's/-fno-ret-protector/-fno-stack-protector/g' -e 's/-nopie/-no-pie/g' {} +
sed -E -i -e 's_^\tinstall.*$_\tinstall bsd ''${out}/bsd_' -e s/update-link// $BSDSRCDIR/sys/arch/*/conf/Makefile.*
''
+
# Remove randomness in build
''
sed -E -i -e 's/^PAGE_SIZE=.*$/PAGE_SIZE=4096/g' -e '/^random_uniform/a echo 0; return 0;' $BSDSRCDIR/sys/conf/makegap.sh
sed -E -i -e 's/^v=.*$/v=0 u=nixpkgs h=nixpkgs t=`date -d @1`/g' $BSDSRCDIR/sys/conf/newvers.sh
'';
postConfigure = ''
export BSDOBJDIR=$TMP/obj
mkdir $BSDOBJDIR
make obj
cd conf
config ${baseConfig}
cd -
'';
preBuild =
# A lot of files insist on calling unprefixed GNU `ld` and `objdump`.
# It's easier to add them to PATH than patch and substitute.
''
mkdir $TMP/bin
export PATH=$TMP/bin:$PATH
ln -s ${pkgsBuildTarget.binutils}/bin/${pkgsBuildTarget.binutils.targetPrefix}objdump $TMP/bin/objdump
ln -s ${pkgsBuildTarget.binutils}/bin/${pkgsBuildTarget.binutils.targetPrefix}ld $TMP/bin/ld
''
+
# The Makefile claims it needs includes, but it really doesn't.
# Tell it includes aren't real and can't hurt it.
''
cd compile/${baseConfig}/obj
echo 'includes:' >>Makefile
'';
# stand is in a separate package
env.SKIPDIR = "stand";
env.NIX_CFLAGS_COMPILE = "-Wno-unused-command-line-argument -Wno-visibility";
}