openbsd.{compat,compatHook}: init

OpenBSD does not provide a compatibility library for running
build tools on other OSes, but we still need one.
`openbsd.compat` inspired by the `freebsd.compat` package and provides
a header-only compatibility layer that can be used across multiple
openbsd build packages. The source is included in the nixpkgs tree
because it functions similarly to per-package patches.

`openbsd.compatHook` provides a build hook that can be added into
`extraNativeBuildInputs` to include `compat` in the library search
path as a system library.
This commit is contained in:
Artemis Tosini 2024-11-03 22:39:16 +00:00
parent 51a07b20cd
commit d48f526db3
No known key found for this signature in database
GPG Key ID: EE5227935FE3FF18
11 changed files with 96 additions and 0 deletions

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)