biglybt: 3.5.0.0 -> 3.6.0.0
[NixPkgs.git] / pkgs / applications / emulators / darling / default.nix
blob174b2f14c9672789af4fd1baf9ba192c39ea001c
1 { clangStdenv
2 , lib
3 , runCommandWith
4 , writeShellScript
5 , fetchFromGitHub
6 , fetchpatch
7 , nixosTests
9 , freetype
10 , libjpeg
11 , libpng
12 , libtiff
13 , giflib
14 , libX11
15 , libXext
16 , libXrandr
17 , libXcursor
18 , libxkbfile
19 , cairo
20 , libglvnd
21 , fontconfig
22 , dbus
23 , libGLU
24 , fuse
25 , ffmpeg
26 , pulseaudio
28 , makeWrapper
29 , python2
30 , python3
31 , cmake
32 , ninja
33 , pkg-config
34 , bison
35 , flex
37 , libbsd
38 , openssl
40 , xdg-user-dirs
42 , addOpenGLRunpath
44 # Whether to pre-compile Python 2 bytecode for performance.
45 , compilePy2Bytecode ? false
47 let
48   stdenv = clangStdenv;
50   # The build system invokes clang to compile Darwin executables.
51   # In this case, our cc-wrapper must not be used.
52   ccWrapperBypass = runCommandWith {
53     inherit stdenv;
54     name = "cc-wrapper-bypass";
55     runLocal = false;
56     derivationArgs = {
57       template = writeShellScript "template" ''
58         for (( i=1; i<=$#; i++)); do
59           j=$((i+1))
60           if [[ "''${!i}" == "-target" && "''${!j}" == *"darwin"* ]]; then
61             # their flags must take precedence
62             exec @unwrapped@ "$@" $NIX_CFLAGS_COMPILE
63           fi
64         done
65         exec @wrapped@ "$@"
66       '';
67     };
68   } ''
69     unwrapped_bin=${stdenv.cc.cc}/bin
70     wrapped_bin=${stdenv.cc}/bin
72     mkdir -p $out/bin
74     unwrapped=$unwrapped_bin/$CC wrapped=$wrapped_bin/$CC \
75       substituteAll $template $out/bin/$CC
76     unwrapped=$unwrapped_bin/$CXX wrapped=$wrapped_bin/$CXX \
77       substituteAll $template $out/bin/$CXX
79     chmod +x $out/bin/$CC $out/bin/$CXX
80   '';
82   wrappedLibs = [
83     # To find all of them: rg -w wrap_elf
85     # src/native/CMakeLists.txt
86     freetype
87     libjpeg
88     libpng
89     libtiff
90     giflib
91     libX11
92     libXext
93     libXrandr
94     libXcursor
95     libxkbfile
96     cairo
97     libglvnd
98     fontconfig
99     dbus
100     libGLU
102     # src/external/darling-dmg/CMakeLists.txt
103     fuse
105     # src/CoreAudio/CMakeLists.txt
106     ffmpeg
107     pulseaudio
108   ];
109 in stdenv.mkDerivation {
110   pname = "darling";
111   version = "unstable-2024-02-03";
113   src = fetchFromGitHub {
114     owner = "darlinghq";
115     repo = "darling";
116     rev = "25afbc76428c39c3909e9efcf5caef1140425211";
117     fetchSubmodules = true;
118     hash = "sha256-T0g38loUFv3jHvUu3R3QH9hwP8JVe2al4g4VhXnBDMc=";
119   };
121   outputs = [ "out" "sdk" ];
123   postPatch = ''
124     # We have to be careful - Patching everything indiscriminately
125     # would affect Darwin scripts as well
126     chmod +x src/external/bootstrap_cmds/migcom.tproj/mig.sh
127     patchShebangs \
128       src/external/bootstrap_cmds/migcom.tproj/mig.sh \
129       src/external/darlingserver/scripts \
130       src/external/openssl_certificates/scripts
132     substituteInPlace src/startup/CMakeLists.txt --replace SETUID ""
133     substituteInPlace src/external/basic_cmds/CMakeLists.txt --replace SETGID ""
134   '';
136   nativeBuildInputs = [
137     bison
138     ccWrapperBypass
139     cmake
140     flex
141     makeWrapper
142     ninja
143     pkg-config
144     python3
145   ]
146   ++ lib.optional compilePy2Bytecode python2;
147   buildInputs = wrappedLibs ++ [
148     libbsd
149     openssl
150     stdenv.cc.libc.linuxHeaders
151   ];
153   # Breaks valid paths like
154   # Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include
155   dontFixCmake = true;
157   # src/external/objc4 forces OBJC_IS_DEBUG_BUILD=1, which conflicts with NDEBUG
158   # TODO: Fix in a better way
159   cmakeBuildType = " ";
161   cmakeFlags = [
162     "-DTARGET_i386=OFF"
163     "-DCOMPILE_PY2_BYTECODE=${if compilePy2Bytecode then "ON" else "OFF"}"
164     "-DDARLINGSERVER_XDG_USER_DIR_CMD=${xdg-user-dirs}/bin/xdg-user-dir"
165   ];
167   env.NIX_CFLAGS_COMPILE = "-Wno-macro-redefined -Wno-unused-command-line-argument";
169   # Linux .so's are dlopen'd by wrapgen during the build
170   env.LD_LIBRARY_PATH = lib.makeLibraryPath wrappedLibs;
172   # Breaks shebangs of Darwin scripts
173   dontPatchShebangs = true;
175   postInstall = ''
176     # Install the SDK as a separate output
177     mkdir -p $sdk
179     sdkDir=$(readlink -f ../Developer)
181     while read -r path; do
182       dst="$sdk/Developer/''${path#$sdkDir}"
184       if [[ -L "$path" ]]; then
185         target=$(readlink -m "$path")
186         if [[ -e "$target" && "$target" == "$NIX_BUILD_TOP"* && "$target" != "$sdkDir"* ]]; then
187           # dereference
188           cp -r -L "$path" "$dst"
189         elif [[ -e "$target" ]]; then
190           # preserve symlink
191           cp -d "$path" "$dst"
192         else
193           # ignore symlink
194           >&2 echo "Ignoring symlink $path -> $target"
195         fi
196       elif [[ -f $path ]]; then
197         cp "$path" "$dst"
198       elif [[ -d $path ]]; then
199         mkdir -p "$dst"
200       fi
201     done < <(find $sdkDir)
203     mkdir -p $sdk/bin
204     cp src/external/cctools-port/cctools/ld64/src/*-ld $sdk/bin
205     cp src/external/cctools-port/cctools/ar/*-{ar,ranlib} $sdk/bin
206   '';
208   postFixup = ''
209     echo "Checking for references to $NIX_STORE in Darling root..."
211     set +e
212     grep -r --exclude=mldr "$NIX_STORE" $out/libexec/darling
213     ret=$?
214     set -e
216     if [[ $ret == 0 ]]; then
217       echo "Found references to $NIX_STORE in Darling root (see above)"
218       exit 1
219     fi
221     patchelf --add-rpath "${lib.makeLibraryPath wrappedLibs}:${addOpenGLRunpath.driverLink}/lib" \
222       $out/libexec/darling/usr/libexec/darling/mldr
223   '';
225   passthru.tests.nixos = nixosTests.darling;
227   meta = with lib; {
228     description = "Open-source Darwin/macOS emulation layer for Linux";
229     homepage = "https://www.darlinghq.org";
230     license = licenses.gpl3Plus;
231     maintainers = with maintainers; [ zhaofengli ];
232     platforms = [ "x86_64-linux" ];
233     mainProgram = "darling";
234   };