about summary refs log tree commit diff
path: root/pkgs/applications/emulators
diff options
context:
space:
mode:
authorZhaofeng Li <hello@zhaofeng.li>2023-05-01 17:39:19 -0600
committerZhaofeng Li <hello@zhaofeng.li>2023-05-06 22:39:39 -0600
commit925e670b550a6d8038a414a631c9d63847fe5222 (patch)
tree9e7984be4961b2d6fbb55f5d7072b99bf1588e38 /pkgs/applications/emulators
parent120dd6a8032fa2dcf9197b0d49614b07b20581fe (diff)
darling: init at unstable-2023-05-02
Diffstat (limited to 'pkgs/applications/emulators')
-rw-r--r--pkgs/applications/emulators/darling/default.nix197
1 files changed, 197 insertions, 0 deletions
diff --git a/pkgs/applications/emulators/darling/default.nix b/pkgs/applications/emulators/darling/default.nix
new file mode 100644
index 0000000000000..053b82cfedeef
--- /dev/null
+++ b/pkgs/applications/emulators/darling/default.nix
@@ -0,0 +1,197 @@
+{ clangStdenv
+, lib
+, runCommandWith
+, writeShellScript
+, fetchFromGitHub
+, fetchpatch
+
+, freetype
+, libjpeg
+, libpng
+, libtiff
+, giflib
+, libX11
+, libXext
+, libXrandr
+, libXcursor
+, libxkbfile
+, cairo
+, libglvnd
+, fontconfig
+, dbus
+, libGLU
+, fuse
+, ffmpeg
+, pulseaudio
+
+, makeWrapper
+, python2
+, python3
+, cmake
+, ninja
+, pkg-config
+, bison
+, flex
+
+, libbsd
+, openssl
+
+, xdg-user-dirs
+
+, addOpenGLRunpath
+
+# Whether to pre-compile Python 2 bytecode for performance.
+, compilePy2Bytecode ? false
+}:
+let
+  stdenv = clangStdenv;
+
+  # The build system invokes clang to compile Darwin executables.
+  # In this case, our cc-wrapper must not be used.
+  ccWrapperBypass = runCommandWith {
+    inherit stdenv;
+    name = "cc-wrapper-bypass";
+    runLocal = false;
+    derivationArgs = {
+      template = writeShellScript "template" ''
+        for (( i=1; i<=$#; i++)); do
+          j=$((i+1))
+          if [[ "''${!i}" == "-target" && "''${!j}" == *"darwin"* ]]; then
+            # their flags must take precedence
+            exec @unwrapped@ "$@" $NIX_CFLAGS_COMPILE
+          fi
+        done
+        exec @wrapped@ "$@"
+      '';
+    };
+  } ''
+    unwrapped_bin=${stdenv.cc.cc}/bin
+    wrapped_bin=${stdenv.cc}/bin
+
+    mkdir -p $out/bin
+
+    unwrapped=$unwrapped_bin/$CC wrapped=$wrapped_bin/$CC \
+      substituteAll $template $out/bin/$CC
+    unwrapped=$unwrapped_bin/$CXX wrapped=$wrapped_bin/$CXX \
+      substituteAll $template $out/bin/$CXX
+
+    chmod +x $out/bin/$CC $out/bin/$CXX
+  '';
+
+  wrappedLibs = [
+    # To find all of them: rg -w wrap_elf
+
+    # src/native/CMakeLists.txt
+    freetype
+    libjpeg
+    libpng
+    libtiff
+    giflib
+    libX11
+    libXext
+    libXrandr
+    libXcursor
+    libxkbfile
+    cairo
+    libglvnd
+    fontconfig
+    dbus
+    libGLU
+
+    # src/external/darling-dmg/CMakeLists.txt
+    fuse
+
+    # src/CoreAudio/CMakeLists.txt
+    ffmpeg
+    pulseaudio
+  ];
+in stdenv.mkDerivation {
+  pname = "darling";
+  version = "unstable-2023-05-02";
+
+  src = fetchFromGitHub {
+    owner = "darlinghq";
+    repo = "darling";
+    rev = "557e7e9dece394a3f623825679474457e5b64fd0";
+    fetchSubmodules = true;
+    hash = "sha256-SOoLaV7wg33qRHPQXkdMvrY++CvoG85kwd6IU6DkYa0=";
+  };
+
+
+  postPatch = ''
+    # We have to be careful - Patching everything indiscriminately
+    # would affect Darwin scripts as well
+    chmod +x src/external/bootstrap_cmds/migcom.tproj/mig.sh
+    patchShebangs \
+      src/external/bootstrap_cmds/migcom.tproj/mig.sh \
+      src/external/darlingserver/scripts \
+      src/external/openssl_certificates/scripts
+
+    substituteInPlace src/startup/CMakeLists.txt --replace SETUID ""
+    substituteInPlace src/external/basic_cmds/CMakeLists.txt --replace SETGID ""
+  '';
+
+  nativeBuildInputs = [
+    bison
+    ccWrapperBypass
+    cmake
+    flex
+    makeWrapper
+    ninja
+    pkg-config
+    python3
+  ]
+  ++ lib.optional compilePy2Bytecode python2;
+  buildInputs = wrappedLibs ++ [
+    libbsd
+    openssl
+    stdenv.cc.libc.linuxHeaders
+  ];
+
+  # Breaks valid paths like
+  # Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include
+  dontFixCmake = true;
+
+  # src/external/objc4 forces OBJC_IS_DEBUG_BUILD=1, which conflicts with NDEBUG
+  # TODO: Fix in a better way
+  cmakeBuildType = " ";
+
+  cmakeFlags = [
+    "-DTARGET_i386=OFF"
+    "-DCOMPILE_PY2_BYTECODE=${if compilePy2Bytecode then "ON" else "OFF"}"
+    "-DDARLINGSERVER_XDG_USER_DIR_CMD=${xdg-user-dirs}/bin/xdg-user-dir"
+  ];
+
+  env.NIX_CFLAGS_COMPILE = "-Wno-macro-redefined -Wno-unused-command-line-argument";
+
+  # Linux .so's are dlopen'd by wrapgen during the build
+  env.LD_LIBRARY_PATH = lib.makeLibraryPath wrappedLibs;
+
+  # Breaks shebangs of Darwin scripts
+  dontPatchShebangs = true;
+
+  postFixup = ''
+    echo "Checking for references to $NIX_STORE in Darling root..."
+
+    set +e
+    grep -r --exclude=mldr "$NIX_STORE" $out/libexec/darling
+    ret=$?
+    set -e
+
+    if [[ $ret == 0 ]]; then
+      echo "Found references to $NIX_STORE in Darling root (see above)"
+      exit 1
+    fi
+
+    patchelf --add-rpath "${lib.makeLibraryPath wrappedLibs}:${addOpenGLRunpath.driverLink}/lib" \
+      $out/libexec/darling/usr/libexec/darling/mldr
+  '';
+
+  meta = with lib; {
+    description = "Open-source Darwin/macOS emulation layer for Linux";
+    homepage = "https://www.darlinghq.org";
+    license = licenses.gpl3Plus;
+    maintainers = with maintainers; [ zhaofengli ];
+    platforms = [ "x86_64-linux" ];
+  };
+}