2022-03-30 09:31:56 +00:00
|
|
|
{ lib, stdenv, fetchFromGitHub, runCommand, unzip, cosmopolitan,bintools-unwrapped }:
|
2021-04-17 00:35:05 +00:00
|
|
|
|
2022-03-30 09:31:56 +00:00
|
|
|
stdenv.mkDerivation rec {
|
2021-04-17 00:35:05 +00:00
|
|
|
pname = "cosmopolitan";
|
2022-03-30 09:31:56 +00:00
|
|
|
version = "unstable-2022-03-22";
|
2021-04-17 00:35:05 +00:00
|
|
|
|
|
|
|
src = fetchFromGitHub {
|
|
|
|
owner = "jart";
|
|
|
|
repo = "cosmopolitan";
|
2022-03-30 09:31:56 +00:00
|
|
|
rev = "5022f9e9207ff2b79ddd6de6d792d3280e12fb3a";
|
|
|
|
sha256 = "sha256-UjL4wR5HhuXiQXg6Orcx2fKiVGRPMJk15P779BP1fRA=";
|
2021-04-17 00:35:05 +00:00
|
|
|
};
|
|
|
|
|
2022-03-30 09:31:56 +00:00
|
|
|
patches = [
|
|
|
|
./ioctl.patch # required /dev/tty
|
|
|
|
];
|
|
|
|
|
2021-04-17 00:35:05 +00:00
|
|
|
postPatch = ''
|
|
|
|
patchShebangs build/
|
|
|
|
'';
|
|
|
|
|
|
|
|
dontConfigure = true;
|
|
|
|
dontFixup = true;
|
|
|
|
enableParallelBuilding = true;
|
2022-03-30 09:31:56 +00:00
|
|
|
nativeBuildInputs = [ bintools-unwrapped unzip ];
|
2021-04-17 00:35:05 +00:00
|
|
|
|
|
|
|
installPhase = ''
|
|
|
|
runHook preInstall
|
2022-03-30 09:31:56 +00:00
|
|
|
mkdir -p $out/{bin,include,lib}
|
|
|
|
install o/cosmopolitan.h $out/include
|
2021-04-17 00:35:05 +00:00
|
|
|
install o/cosmopolitan.a o/libc/crt/crt.o o/ape/ape.{o,lds} $out/lib
|
2022-03-30 09:31:56 +00:00
|
|
|
|
2021-04-17 00:35:05 +00:00
|
|
|
cat > $out/bin/cosmoc <<EOF
|
2022-03-30 09:31:56 +00:00
|
|
|
#!${stdenv.shell}
|
|
|
|
exec ${stdenv.cc}/bin/${stdenv.cc.targetPrefix}gcc \
|
2021-04-17 00:35:05 +00:00
|
|
|
-O -static -nostdlib -nostdinc -fno-pie -no-pie -mno-red-zone \
|
|
|
|
"\$@" \
|
2022-03-30 09:31:56 +00:00
|
|
|
-Wl,--gc-sections -Wl,-z,max-page-size=0x1000 \
|
2021-04-17 00:35:05 +00:00
|
|
|
-fuse-ld=bfd -Wl,-T,$out/lib/ape.lds \
|
2022-03-30 09:31:56 +00:00
|
|
|
-include $out/include/cosmopolitan.h \
|
|
|
|
-I $out/include \
|
|
|
|
$out/lib/{crt.o,ape.o,cosmopolitan.a}
|
2021-04-17 00:35:05 +00:00
|
|
|
EOF
|
|
|
|
chmod +x $out/bin/cosmoc
|
2022-03-30 09:31:56 +00:00
|
|
|
|
|
|
|
pushd o
|
|
|
|
find -iname "*.com" -type f -exec install -D {} $out/{} \;
|
|
|
|
popd
|
|
|
|
find -iname "*.h" -type f -exec install -m644 -D {} $out/include/{} \;
|
|
|
|
find -iname "*.inc" -type f -exec install -m644 -D {} $out/include/{} \;
|
2021-04-17 00:35:05 +00:00
|
|
|
runHook postInstall
|
|
|
|
'';
|
|
|
|
|
2022-03-30 09:31:56 +00:00
|
|
|
passthru.tests = lib.optionalAttrs (stdenv.buildPlatform == stdenv.hostPlatform) {
|
2021-04-17 00:35:05 +00:00
|
|
|
hello = runCommand "hello-world" { } ''
|
2022-03-30 09:31:56 +00:00
|
|
|
printf '#include "libc/stdio/stdio.h"\nmain() { printf("hello world\\n"); }\n' >hello.c
|
|
|
|
${stdenv.cc}/bin/gcc -g -O -static -nostdlib -nostdinc -fno-pie -no-pie -mno-red-zone -o hello.com.dbg hello.c \
|
2021-04-17 00:35:05 +00:00
|
|
|
-fuse-ld=bfd -Wl,-T,${cosmopolitan}/lib/ape.lds \
|
2022-03-30 09:31:56 +00:00
|
|
|
-include ${cosmopolitan}/include/cosmopolitan.h \
|
|
|
|
-I ${cosmopolitan}/include \
|
|
|
|
${cosmopolitan}/lib/{crt.o,ape.o,cosmopolitan.a}
|
|
|
|
${stdenv.cc.bintools.bintools_bin}/bin/objcopy -S -O binary hello.com.dbg hello.com
|
2021-04-17 00:35:05 +00:00
|
|
|
./hello.com
|
|
|
|
printf "test successful" > $out
|
|
|
|
'';
|
|
|
|
cosmoc = runCommand "cosmoc-hello" { } ''
|
2022-03-30 09:31:56 +00:00
|
|
|
printf '#include "libc/stdio/stdio.h"\nmain() { printf("hello world\\n"); }\n' >hello.c
|
2021-04-17 00:35:05 +00:00
|
|
|
${cosmopolitan}/bin/cosmoc hello.c
|
|
|
|
./a.out
|
|
|
|
printf "test successful" > $out
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
meta = with lib; {
|
|
|
|
homepage = "https://justine.lol/cosmopolitan/";
|
|
|
|
description = "Your build-once run-anywhere c library";
|
|
|
|
platforms = platforms.x86_64;
|
|
|
|
badPlatforms = platforms.darwin;
|
|
|
|
license = licenses.isc;
|
|
|
|
maintainers = with maintainers; [ lourkeur tomberek ];
|
|
|
|
};
|
|
|
|
}
|