summary refs log tree commit diff
diff options
context:
space:
mode:
authoraszlig <aszlig@redmoonstudios.org>2014-04-21 10:00:35 +0200
committeraszlig <aszlig@redmoonstudios.org>2014-04-21 10:00:35 +0200
commit625d7b9043be673a8df33cc842ec5032d7705c06 (patch)
tree38777f48f2bb7b403b3e9f148000d0ffa8026cde
parentcdd1c9caa5d2a6eb184ec159bcfde3ac9745c56d (diff)
parent2d3376e84e4a24ca10480a9ade972db1e5c54e9f (diff)
downloadnixlib-625d7b9043be673a8df33cc842ec5032d7705c06.tar
nixlib-625d7b9043be673a8df33cc842ec5032d7705c06.tar.gz
nixlib-625d7b9043be673a8df33cc842ec5032d7705c06.tar.bz2
nixlib-625d7b9043be673a8df33cc842ec5032d7705c06.tar.lz
nixlib-625d7b9043be673a8df33cc842ec5032d7705c06.tar.xz
nixlib-625d7b9043be673a8df33cc842ec5032d7705c06.tar.zst
nixlib-625d7b9043be673a8df33cc842ec5032d7705c06.zip
Merge pull request #1928 from 'cross-win-osx'.
This includes a lot of fixes for cross-building to Windows and Mac OS X
and could possibly fix things even for non-cross-builds, like for
example OpenSSL on Windows.

The main reason for merging this in 14.04 already is that we already
have runInWindowsVM in master and it doesn't work until we actually
cross-build Cygwin's setup binary as the upstream version is a fast
moving target which gets _overwritten_ on every new release.

Conflicts:
	pkgs/top-level/all-packages.nix
-rw-r--r--pkgs/build-support/gcc-cross-wrapper/builder.sh4
-rw-r--r--pkgs/build-support/gcc-cross-wrapper/default.nix1
-rw-r--r--pkgs/build-support/gcc-cross-wrapper/setup-hook.sh3
-rw-r--r--pkgs/data/misc/tzdata/default.nix6
-rw-r--r--pkgs/development/compilers/gcc/4.6/default.nix6
-rw-r--r--pkgs/development/compilers/gcc/4.8/default.nix18
-rw-r--r--pkgs/development/interpreters/lua-5/5.2.nix33
-rw-r--r--pkgs/development/libraries/SDL/default.nix46
-rw-r--r--pkgs/development/libraries/audiofile/default.nix2
-rw-r--r--pkgs/development/libraries/glew/default.nix29
-rw-r--r--pkgs/development/libraries/libgcrypt/1.6.nix6
-rw-r--r--pkgs/development/libraries/libgpg-error/default.nix6
-rw-r--r--pkgs/development/libraries/libjpeg/default.nix6
-rw-r--r--pkgs/development/libraries/libpng/12.nix9
-rw-r--r--pkgs/development/libraries/openssl/cert-file.patch14
-rw-r--r--pkgs/development/libraries/openssl/default.nix8
-rw-r--r--pkgs/development/libraries/pcre/default.nix16
-rw-r--r--pkgs/development/libraries/qt-4.x/4.8/default.nix3
-rw-r--r--pkgs/development/libraries/zlib/default.nix2
-rw-r--r--pkgs/development/mobile/xpwn/default.nix28
-rw-r--r--pkgs/development/tools/build-managers/cmake/default.nix8
-rw-r--r--pkgs/development/tools/misc/binutils-cross/builder.sh26
-rw-r--r--pkgs/development/tools/misc/binutils-cross/default.nix13
-rw-r--r--pkgs/os-specific/darwin/cctools-port/default.nix64
-rw-r--r--pkgs/os-specific/darwin/cctools-port/ld-ignore-rpath-link.patch16
-rw-r--r--pkgs/os-specific/darwin/cctools-port/ld-rpath-nonfinal.patch31
-rw-r--r--pkgs/os-specific/darwin/maloader/default.nix36
-rw-r--r--pkgs/os-specific/darwin/opencflite/default.nix21
-rw-r--r--pkgs/os-specific/darwin/xcode/default.nix52
-rw-r--r--pkgs/os-specific/darwin/xcode/gcc-fix-enum-attributes.patch13
-rw-r--r--pkgs/os-specific/windows/mingw-w64/default.nix28
-rw-r--r--pkgs/tools/compression/bzip2/default.nix1
-rw-r--r--pkgs/tools/graphics/gifsicle/default.nix8
-rw-r--r--pkgs/tools/graphics/optipng/default.nix22
-rw-r--r--pkgs/top-level/all-packages.nix51
-rw-r--r--pkgs/top-level/release-cross.nix11
36 files changed, 523 insertions, 124 deletions
diff --git a/pkgs/build-support/gcc-cross-wrapper/builder.sh b/pkgs/build-support/gcc-cross-wrapper/builder.sh
index 2dfa315d168f..1d7d1046d367 100644
--- a/pkgs/build-support/gcc-cross-wrapper/builder.sh
+++ b/pkgs/build-support/gcc-cross-wrapper/builder.sh
@@ -26,6 +26,10 @@ if test -z "$nativeLibc"; then
       fi
     fi
 
+    if [ -n "$osxMinVersion" ]; then
+        cflagsCompile="$cflagsCompile -mmacosx-version-min=$osxMinVersion"
+    fi
+
     echo "$cflagsCompile -B$libc/lib/ -idirafter $libc/include -idirafter $gcc/lib/gcc/*/*/include-fixed" > $out/nix-support/libc-cflags
 
     echo "-L$libc/lib -rpath $libc/lib -rpath-link $libc/lib" > $out/nix-support/libc-ldflags
diff --git a/pkgs/build-support/gcc-cross-wrapper/default.nix b/pkgs/build-support/gcc-cross-wrapper/default.nix
index b2e30be10ef7..d3494b83a87b 100644
--- a/pkgs/build-support/gcc-cross-wrapper/default.nix
+++ b/pkgs/build-support/gcc-cross-wrapper/default.nix
@@ -46,6 +46,7 @@ stdenv.mkDerivation {
   addFlags = ./add-flags;
   inherit nativeTools nativeLibc nativePrefix gcc libc binutils;
   crossConfig = if cross != null then cross.config else null;
+  osxMinVersion = cross.osxMinVersion or null;
   gccLibs = if gcc != null then gccLibs else null;
   name = chosenName;
   langC = if nativeTools then true else gcc.langC;
diff --git a/pkgs/build-support/gcc-cross-wrapper/setup-hook.sh b/pkgs/build-support/gcc-cross-wrapper/setup-hook.sh
index a7be09283ee0..433d36ced434 100644
--- a/pkgs/build-support/gcc-cross-wrapper/setup-hook.sh
+++ b/pkgs/build-support/gcc-cross-wrapper/setup-hook.sh
@@ -75,6 +75,9 @@ fi
 # native compilations.
 doCheck=""
 
+# Don't strip foreign binaries with native "strip" tool.
+dontStrip=1
+
 # Add the output as an rpath.
 if test "$NIX_NO_SELF_RPATH" != "1"; then
     export NIX_CROSS_LDFLAGS="-rpath $out/lib -rpath-link $out/lib $NIX_CROSS_LDFLAGS"
diff --git a/pkgs/data/misc/tzdata/default.nix b/pkgs/data/misc/tzdata/default.nix
index da043da09678..57b1dddbda55 100644
--- a/pkgs/data/misc/tzdata/default.nix
+++ b/pkgs/data/misc/tzdata/default.nix
@@ -17,13 +17,17 @@ stdenv.mkDerivation rec {
     ];
 
   sourceRoot = ".";
+  outputs = [ "out" "lib" ];
 
-  makeFlags = "TOPDIR=$(out) TZDIR=$(out)/share/zoneinfo ETCDIR=$(TMPDIR)/etc LIBDIR=$(TMPDIR)/lib MANDIR=$(TMPDIR)/man AWK=awk";
+  makeFlags = "TOPDIR=$(out) TZDIR=$(out)/share/zoneinfo ETCDIR=$(TMPDIR)/etc LIBDIR=$(lib)/lib MANDIR=$(TMPDIR)/man AWK=awk";
 
   postInstall =
     ''
       mv $out/share/zoneinfo-posix $out/share/zoneinfo/posix
       mv $out/share/zoneinfo-leaps $out/share/zoneinfo/right
+
+      ensureDir "$lib/include"
+      cp tzfile.h "$lib/include/tzfile.h"
     '';
 
   meta = {
diff --git a/pkgs/development/compilers/gcc/4.6/default.nix b/pkgs/development/compilers/gcc/4.6/default.nix
index 3ed0f7ad0e6d..af1113226980 100644
--- a/pkgs/development/compilers/gcc/4.6/default.nix
+++ b/pkgs/development/compilers/gcc/4.6/default.nix
@@ -159,10 +159,8 @@ let version = "4.6.3";
             # In any case, mingw32 g++ linking is broken by default with shared libs,
             # unless adding "-lsupc++" to any linking command. I don't know why.
             " --disable-shared" +
-            (if cross.config == "x86_64-w64-mingw32" then
-              # To keep ABI compatibility with upstream mingw-w64
-              " --enable-fully-dynamic-string"
-              else "")
+            # To keep ABI compatibility with upstream mingw-w64
+            " --enable-fully-dynamic-string"
             else (if cross.libc == "uclibc" then
               # In uclibc cases, libgomp needs an additional '-ldl'
               # and as I don't know how to pass it, I disable libgomp.
diff --git a/pkgs/development/compilers/gcc/4.8/default.nix b/pkgs/development/compilers/gcc/4.8/default.nix
index 890122fe4770..4f9e734d41c0 100644
--- a/pkgs/development/compilers/gcc/4.8/default.nix
+++ b/pkgs/development/compilers/gcc/4.8/default.nix
@@ -117,7 +117,8 @@ let version = "4.8.2";
         withMode;
 
     /* Cross-gcc settings */
-    crossMingw = (cross != null && cross.libc == "msvcrt");
+    crossMingw = cross != null && cross.libc == "msvcrt";
+    crossDarwin = cross != null && cross.libc == "libSystem";
     crossConfigureFlags = let
         gccArch = stdenv.cross.gcc.arch or null;
         gccCpu = stdenv.cross.gcc.cpu or null;
@@ -161,7 +162,13 @@ let version = "4.8.2";
           " --disable-shared" +
           " --disable-decimal-float" # libdecnumber requires libc
           else
-          " --with-headers=${libcCross}/include" +
+          (if crossDarwin then " --with-sysroot=${libcCross}/share/sysroot"
+           else                " --with-headers=${libcCross}/include") +
+          # Ensure that -print-prog-name is able to find the correct programs.
+          (stdenv.lib.optionalString (crossMingw || crossDarwin) (
+            " --with-as=${binutilsCross}/bin/${cross.config}-as" +
+            " --with-ld=${binutilsCross}/bin/${cross.config}-ld"
+          )) +
           " --enable-__cxa_atexit" +
           " --enable-long-long" +
           (if crossMingw then
@@ -175,10 +182,8 @@ let version = "4.8.2";
             # In any case, mingw32 g++ linking is broken by default with shared libs,
             # unless adding "-lsupc++" to any linking command. I don't know why.
             " --disable-shared" +
-            (if cross.config == "x86_64-w64-mingw32" then
-              # To keep ABI compatibility with upstream mingw-w64
-              " --enable-fully-dynamic-string"
-              else "")
+            # To keep ABI compatibility with upstream mingw-w64
+            " --enable-fully-dynamic-string"
             else (if cross.libc == "uclibc" then
               # In uclibc cases, libgomp needs an additional '-ldl'
               # and as I don't know how to pass it, I disable libgomp.
@@ -346,6 +351,7 @@ stdenv.mkDerivation ({
         ++ optional langAda      "ada"
         ++ optional langVhdl     "vhdl"
         ++ optional langGo       "go"
+        ++ optionals crossDarwin [ "objc" "obj-c++" ]
         )
       )
     }
diff --git a/pkgs/development/interpreters/lua-5/5.2.nix b/pkgs/development/interpreters/lua-5/5.2.nix
index 3e7323e461d1..f346b1b09868 100644
--- a/pkgs/development/interpreters/lua-5/5.2.nix
+++ b/pkgs/development/interpreters/lua-5/5.2.nix
@@ -17,7 +17,7 @@ stdenv.mkDerivation rec {
     sha256 = "004zyh9p3lpvbwhyhlmrw6wwcia5abx84q4h2brkn4zdypipvmiz";
   };
 
-  buildInputs = [ readline ];
+  nativeBuildInputs = [ readline ];
 
   patches = if stdenv.isDarwin then [ ./5.2.darwin.patch ] else [ dsoPatch ];
 
@@ -54,6 +54,37 @@ stdenv.mkDerivation rec {
     EOF
   '';
 
+  crossAttrs = let
+    isMingw = stdenv.cross.libc == "msvcrt";
+    isDarwin = stdenv.cross.libc == "libSystem";
+  in {
+    configurePhase = ''
+      makeFlagsArray=(
+        INSTALL_TOP=$out
+        INSTALL_MAN=$out/share/man/man1
+        CC=${stdenv.cross.config}-gcc
+        STRIP=:
+        RANLIB=${stdenv.cross.config}-ranlib
+        V=${majorVersion}
+        R=${version}
+        ${if isMingw then "mingw" else stdenv.lib.optionalString isDarwin ''
+        AR="${stdenv.cross.config}-ar rcu"
+        macosx
+        ''}
+      )
+    '' + stdenv.lib.optionalString isMingw ''
+      installFlagsArray=(
+        TO_BIN="lua.exe luac.exe"
+        TO_LIB="liblua.a lua52.dll"
+        INSTALL_DATA="cp -d"
+      )
+    '';
+  } // stdenv.lib.optionalAttrs isDarwin {
+    postPatch = ''
+      sed -i -e 's/-Wl,-soname[^ ]* *//' src/Makefile
+    '';
+  };
+
   meta = {
     homepage = "http://www.lua.org";
     description = "Powerful, fast, lightweight, embeddable scripting language";
diff --git a/pkgs/development/libraries/SDL/default.nix b/pkgs/development/libraries/SDL/default.nix
index e58397fa9a5b..ee3640fe64b4 100644
--- a/pkgs/development/libraries/SDL/default.nix
+++ b/pkgs/development/libraries/SDL/default.nix
@@ -7,21 +7,13 @@
 
 # OSS is no longer supported, for it's much crappier than ALSA and
 # PulseAudio.
-assert alsaSupport || pulseaudioSupport;
+assert !(stdenv ? cross) -> alsaSupport || pulseaudioSupport;
 
 assert openglSupport -> (mesa != null && x11Support);
 assert x11Support -> (x11 != null && libXrandr != null);
 assert alsaSupport -> alsaLib != null;
 assert pulseaudioSupport -> pulseaudio != null;
 
-let
-  configureFlagsFun = attrs: ''
-        --disable-oss --disable-video-x11-xme
-        --disable-x11-shared --disable-alsa-shared --enable-rpath --disable-pulseaudio-shared
-        --disable-osmesa-shared
-        ${if alsaSupport then "--with-alsa-prefix=${attrs.alsaLib}/lib" else ""}
-      '';
-in
 stdenv.mkDerivation rec {
   version = "1.2.15";
   name    = "SDL-${version}";
@@ -32,20 +24,46 @@ stdenv.mkDerivation rec {
   };
 
   # Since `libpulse*.la' contain `-lgdbm', PulseAudio must be propagated.
-  propagatedBuildInputs = stdenv.lib.optionals x11Support [ x11 libXrandr ] ++
+  propagatedNativeBuildInputs =
+    stdenv.lib.optionals x11Support [ x11 libXrandr ] ++
     stdenv.lib.optional pulseaudioSupport pulseaudio;
 
-  buildInputs = [ pkgconfig audiofile ] ++
+  buildInputs = let
+    notMingw = !(stdenv ? cross) || stdenv.cross.libc != "msvcrt";
+  in stdenv.lib.optional notMingw audiofile;
+
+  nativeBuildInputs = [ pkgconfig ] ++
     stdenv.lib.optional openglSupport [ mesa ] ++
     stdenv.lib.optional alsaSupport alsaLib;
 
   # XXX: By default, SDL wants to dlopen() PulseAudio, in which case
   # we must arrange to add it to its RPATH; however, `patchelf' seems
   # to fail at doing this, hence `--disable-pulseaudio-shared'.
-  configureFlags = configureFlagsFun { inherit alsaLib; };
+  configureFlags = [
+    "--disable-oss"
+    "--disable-video-x11-xme"
+    "--disable-x11-shared"
+    "--disable-alsa-shared"
+    "--enable-rpath"
+    "--disable-pulseaudio-shared"
+    "--disable-osmesa-shared"
+  ] ++ stdenv.lib.optionals (stdenv ? cross) ([
+    "--without-x"
+  ] ++ stdenv.lib.optional alsaSupport "--with-alsa-prefix=${alsaLib}/lib");
 
-  crossAttrs = {
-      configureFlags = configureFlagsFun { alsaLib = alsaLib.crossDrv; };
+  crossAttrs =stdenv.lib.optionalAttrs (stdenv.cross.libc == "libSystem") {
+    patches = let
+      f = rev: sha256: fetchurl {
+        url = "http://hg.libsdl.org/SDL/raw-rev/${rev}";
+        inherit sha256;
+      };
+    in [
+      (f "e9466ead70e5" "0ygir3k83d0vxp7s3k48jn3j8n2bnv9wm6613wpx3ybnjrxabrip")
+      (f "bbfb41c13a87" "17v29ybjifvka19m8qf14rjc43nfdwk9v9inaizznarhb17amlnv")
+    ];
+    postPatch = ''
+      sed -i -e 's/ *-fpascal-strings//' configure
+    '';
   };
 
   passthru = {inherit openglSupport;};
diff --git a/pkgs/development/libraries/audiofile/default.nix b/pkgs/development/libraries/audiofile/default.nix
index a5fd80dc0db9..c8fb8c53dc0c 100644
--- a/pkgs/development/libraries/audiofile/default.nix
+++ b/pkgs/development/libraries/audiofile/default.nix
@@ -3,7 +3,7 @@
 stdenv.mkDerivation rec {
   name = "audiofile-0.3.6";
 
-  buildInputs = stdenv.lib.optional (!stdenv.isDarwin) alsaLib;
+  nativeBuildInputs = stdenv.lib.optional stdenv.isLinux alsaLib;
 
   src = fetchurl {
     url = "http://audiofile.68k.org/${name}.tar.gz";
diff --git a/pkgs/development/libraries/glew/default.nix b/pkgs/development/libraries/glew/default.nix
index ef81f210d39f..9ec88799d0dd 100644
--- a/pkgs/development/libraries/glew/default.nix
+++ b/pkgs/development/libraries/glew/default.nix
@@ -1,5 +1,7 @@
 { stdenv, fetchurl, mesa_glu, x11, libXmu, libXi }:
 
+with stdenv.lib;
+
 stdenv.mkDerivation rec {
   name = "glew-1.10.0";
 
@@ -8,23 +10,38 @@ stdenv.mkDerivation rec {
     sha256 = "01zki46dr5khzlyywr3cg615bcal32dazfazkf360s1znqh17i4r";
   };
 
-
-  buildInputs = [ x11 libXmu libXi ];
-  propagatedBuildInputs = [ mesa_glu ]; # GL/glew.h includes GL/glu.h
+  nativeBuildInputs = [ x11 libXmu libXi ];
+  propagatedNativeBuildInputs = [ mesa_glu ]; # GL/glew.h includes GL/glu.h
 
   patchPhase = ''
     sed -i 's|lib64|lib|' config/Makefile.linux
+    ${optionalString (stdenv ? cross) ''
+    sed -i -e 's/\(INSTALL.*\)-s/\1/' Makefile
+    ''}
+  '';
+
+  buildFlags = [ "all" ];
+  installFlags = [ "install.all" ];
+
+  preInstall = ''
+    export GLEW_DEST="$out"
   '';
 
-buildPhase = "make all";
-  installPhase = ''
-    GLEW_DEST=$out make install.all
+  postInstall = ''
     mkdir -pv $out/share/doc/glew
     mkdir -p $out/lib/pkgconfig
     cp glew*.pc $out/lib/pkgconfig
     cp -r README.txt LICENSE.txt doc $out/share/doc/glew
   '';
 
+  crossAttrs.makeFlags = [
+    "CC=${stdenv.cross.config}-gcc"
+    "LD=${stdenv.cross.config}-gcc"
+    "AR=${stdenv.cross.config}-ar"
+    "STRIP="
+  ] ++ optional (stdenv.cross.libc == "msvcrt") "SYSTEM=mingw"
+    ++ optional (stdenv.cross.libc == "libSystem") "SYSTEM=darwin";
+
   meta = {
     description = "An OpenGL extension loading library for C(++)";
     homepage = http://glew.sourceforge.net/;
diff --git a/pkgs/development/libraries/libgcrypt/1.6.nix b/pkgs/development/libraries/libgcrypt/1.6.nix
index ea981f7ed127..9dc8b8a23179 100644
--- a/pkgs/development/libraries/libgcrypt/1.6.nix
+++ b/pkgs/development/libraries/libgcrypt/1.6.nix
@@ -24,6 +24,12 @@ stdenv.mkDerivation rec {
     make check
   '';
 
+  crossAttrs = let
+    isCross64 = stdenv.cross.config == "x86_64-w64-mingw32";
+  in stdenv.lib.optionalAttrs isCross64 {
+    configureFlags = [ "--disable-asm" "--disable-padlock-support" ];
+  };
+
   meta = {
     description = "GNU Libgcrypt, a general-pupose cryptographic library";
 
diff --git a/pkgs/development/libraries/libgpg-error/default.nix b/pkgs/development/libraries/libgpg-error/default.nix
index da142a957ad7..8ce9a10701ec 100644
--- a/pkgs/development/libraries/libgpg-error/default.nix
+++ b/pkgs/development/libraries/libgpg-error/default.nix
@@ -1,4 +1,4 @@
-{ stdenv, fetchurl, bash }:
+{ stdenv, fetchurl, bash, gettext }:
 
 stdenv.mkDerivation (rec {
   name = "libgpg-error-1.12";
@@ -8,6 +8,10 @@ stdenv.mkDerivation (rec {
     sha256 = "0pz58vr12qihq2f0bypjxsb6cf6ajq5258fmfm8s6lvwm3b9xz6a";
   };
 
+  # If architecture-dependant MO files aren't available, they're generated
+  # during build, so we need gettext for cross-builds.
+  crossAttrs.buildInputs = [ gettext ];
+
   doCheck = true;
 
   meta = {
diff --git a/pkgs/development/libraries/libjpeg/default.nix b/pkgs/development/libraries/libjpeg/default.nix
index 63d698dda18f..d7a1bcb46385 100644
--- a/pkgs/development/libraries/libjpeg/default.nix
+++ b/pkgs/development/libraries/libjpeg/default.nix
@@ -1,4 +1,6 @@
-{ stdenv, fetchurl }: 
+{ stdenv, fetchurl, static ? false }: 
+
+with stdenv.lib;
 
 stdenv.mkDerivation {
   name = "libjpeg-8d";
@@ -7,6 +9,8 @@ stdenv.mkDerivation {
     url = http://www.ijg.org/files/jpegsrc.v8d.tar.gz;
     sha256 = "1cz0dy05mgxqdgjf52p54yxpyy95rgl30cnazdrfmw7hfca9n0h0";
   };
+
+  configureFlags = optional static "--enable-static --disable-shared";
   
   meta = {
     homepage = http://www.ijg.org/;
diff --git a/pkgs/development/libraries/libpng/12.nix b/pkgs/development/libraries/libpng/12.nix
index 094483da660a..ff04c7181df5 100644
--- a/pkgs/development/libraries/libpng/12.nix
+++ b/pkgs/development/libraries/libpng/12.nix
@@ -1,6 +1,6 @@
 { stdenv, fetchurl, zlib, xz }:
 
-assert zlib != null;
+assert !(stdenv ? cross) -> zlib != null;
 
 stdenv.mkDerivation rec {
   name = "libpng-1.2.51";
@@ -16,6 +16,13 @@ stdenv.mkDerivation rec {
 
   passthru = { inherit zlib; };
 
+  crossAttrs = stdenv.lib.optionalAttrs (stdenv.cross.libc == "libSystem") {
+    propagatedBuildInputs = [];
+    passthru = {};
+  };
+
+  configureFlags = "--enable-static";
+
   meta = {
     description = "The official reference implementation for the PNG file format";
     homepage = http://www.libpng.org/pub/png/libpng.html;
diff --git a/pkgs/development/libraries/openssl/cert-file.patch b/pkgs/development/libraries/openssl/cert-file.patch
index 6b7a60e90267..26b51c0295e0 100644
--- a/pkgs/development/libraries/openssl/cert-file.patch
+++ b/pkgs/development/libraries/openssl/cert-file.patch
@@ -12,7 +12,7 @@ diff -ru -x '*~' openssl-1.0.0e-orig/crypto/x509/x509_def.c openssl-1.0.0e/crypt
  #include "cryptlib.h"
  #include <openssl/crypto.h>
  #include <openssl/x509.h>
-@@ -71,7 +75,19 @@
+@@ -71,7 +75,25 @@
  	{ return(X509_CERT_DIR); }
  
  const char *X509_get_default_cert_file(void)
@@ -23,9 +23,15 @@ diff -ru -x '*~' openssl-1.0.0e-orig/crypto/x509/x509_def.c openssl-1.0.0e/crypt
 +	if (!init) {
 +	    init = 1;
 +	    char * s = getenv("OPENSSL_X509_CERT_FILE");
-+	    if (s && getuid() == geteuid()) {
-+		strncpy(buf, s, sizeof(buf));
-+		buf[sizeof(buf) - 1] = 0;
++	    if (s) {
++#ifndef OPENSSL_SYS_WINDOWS
++	        if (getuid() == geteuid()) {
++#endif
++		        strncpy(buf, s, sizeof(buf));
++		        buf[sizeof(buf) - 1] = 0;
++#ifndef OPENSSL_SYS_WINDOWS
++	        }
++#endif
 +	    }
 +	}
 +	return buf;
diff --git a/pkgs/development/libraries/openssl/default.nix b/pkgs/development/libraries/openssl/default.nix
index 0d57018dcd6f..7217c876bfd9 100644
--- a/pkgs/development/libraries/openssl/default.nix
+++ b/pkgs/development/libraries/openssl/default.nix
@@ -8,7 +8,9 @@ let
     (throw "openssl needs its platform name cross building" null)
     stdenv.cross;
 
-  patchesCross = isCross:
+  patchesCross = isCross: let
+    isDarwin = stdenv.isDarwin || (isCross && stdenv.cross.libc == "libSystem");
+  in
     [ # Allow the location of the X509 certificate file (the CA
       # bundle) to be set through the environment variable
       # ‘OPENSSL_X509_CERT_FILE’.  This is necessary because the
@@ -29,7 +31,7 @@ let
           ./kfreebsd-gnu.patch
         ]
 
-    ++ stdenv.lib.optional stdenv.isDarwin ./darwin-arch.patch;
+    ++ stdenv.lib.optional isDarwin ./darwin-arch.patch;
 
 in
 
@@ -91,6 +93,8 @@ stdenv.mkDerivation {
       rm $out/bin/c_rehash $out/ssl/misc/CA.pl $out/ssl/misc/tsget
     '';
     configureScript = "./Configure";
+  } // stdenv.lib.optionalAttrs (opensslCrossSystem == "darwin64-x86_64-cc") {
+    CC = "gcc";
   };
 
   meta = {
diff --git a/pkgs/development/libraries/pcre/default.nix b/pkgs/development/libraries/pcre/default.nix
index 51279701d4a5..7138c44da643 100644
--- a/pkgs/development/libraries/pcre/default.nix
+++ b/pkgs/development/libraries/pcre/default.nix
@@ -1,4 +1,8 @@
-{ stdenv, fetchurl, unicodeSupport ? true, cplusplusSupport ? true }:
+{ stdenv, fetchurl, unicodeSupport ? true, cplusplusSupport ? true
+, windows ? null
+}:
+
+with stdenv.lib;
 
 stdenv.mkDerivation rec {
   name = "pcre-8.34";
@@ -16,12 +20,16 @@ stdenv.mkDerivation rec {
     --enable-jit
     ${if unicodeSupport then "--enable-unicode-properties" else ""}
     ${if !cplusplusSupport then "--disable-cpp" else ""}
-  '' + stdenv.lib.optionalString stdenv.isDarwin "CXXFLAGS=-O0";
+  '' + optionalString stdenv.isDarwin "CXXFLAGS=-O0";
 
   doCheck = with stdenv; !(isCygwin || isFreeBSD);
     # XXX: test failure on Cygwin
     # we are running out of stack on both freeBSDs on Hydra
 
+  crossAttrs = optionalAttrs (stdenv.cross.libc == "msvcrt") {
+    buildInputs = [ windows.mingw_w64_pthreads.crossDrv ];
+  };
+
   meta = {
     homepage = "http://www.pcre.org/";
     description = "A library for Perl Compatible Regular Expressions";
@@ -35,7 +43,7 @@ stdenv.mkDerivation rec {
       PCRE library is free, even for building proprietary software.
     '';
 
-    platforms = stdenv.lib.platforms.all;
-    maintainers = [ stdenv.lib.maintainers.simons ];
+    platforms = platforms.all;
+    maintainers = [ maintainers.simons ];
   };
 }
diff --git a/pkgs/development/libraries/qt-4.x/4.8/default.nix b/pkgs/development/libraries/qt-4.x/4.8/default.nix
index 16bd1ee65d2e..1bd027f52986 100644
--- a/pkgs/development/libraries/qt-4.x/4.8/default.nix
+++ b/pkgs/development/libraries/qt-4.x/4.8/default.nix
@@ -148,8 +148,7 @@ stdenv.mkDerivation rec {
   '';
 
   crossAttrs = let
-    isMingw = stdenv.cross.config == "i686-pc-mingw32" ||
-      stdenv.cross.config == "x86_64-w64-mingw32";
+    isMingw = stdenv.cross.libc == "msvcrt";
   in {
     # I've not tried any case other than i686-pc-mingw32.
     # -nomake tools:   it fails linking some asian language symbols
diff --git a/pkgs/development/libraries/zlib/default.nix b/pkgs/development/libraries/zlib/default.nix
index f1eea80ab7a3..088eb1b94930 100644
--- a/pkgs/development/libraries/zlib/default.nix
+++ b/pkgs/development/libraries/zlib/default.nix
@@ -36,6 +36,8 @@ stdenv.mkDerivation rec {
       "-f" "win32/Makefile.gcc"
       "PREFIX=${stdenv.cross.config}-"
     ] ++ (if static then [] else [ "SHARED_MODE=1" ]);
+  } // stdenv.lib.optionalAttrs (stdenv.cross.libc == "libSystem") {
+    makeFlags = [ "RANLIB=${stdenv.cross.config}-ranlib" ];
   };
 
   # zlib doesn't like the automatic --disable-shared from the Cygwin stdenv.
diff --git a/pkgs/development/mobile/xpwn/default.nix b/pkgs/development/mobile/xpwn/default.nix
new file mode 100644
index 000000000000..b248107141f5
--- /dev/null
+++ b/pkgs/development/mobile/xpwn/default.nix
@@ -0,0 +1,28 @@
+{ stdenv, fetchgit, cmake, zlib, libpng, bzip2, libusb, openssl }:
+
+stdenv.mkDerivation {
+  name = "xpwn-0.5.8git";
+
+  src = fetchgit {
+    url = "git://github.com/dborca/xpwn.git";
+    rev = "4534da88d4e8a32cdc9da9b5326e2cc482c95ef0";
+    sha256 =
+      "1h1ak40fg5bym0hifpii9q2hqdp2m387cwfzb4bl6qq36xpkd6wv";
+  };
+
+  preConfigure = ''
+    sed -r -i \
+      -e 's/(install.*TARGET.*DESTINATION )\.\)/\1bin)/' \
+      -e 's!(install.*(FILE|DIR).*DESTINATION )([^)]*)!\1share/xpwn/\3!' \
+      */CMakeLists.txt
+    sed -i -e '/install/d' CMakeLists.txt
+  '';
+
+  buildInputs = [ cmake zlib libpng bzip2 libusb openssl ];
+
+  meta = {
+    homepage = "http://planetbeing.lighthouseapp.com/projects/15246-xpwn";
+    description = "Custom NOR firmware loader/IPSW generator for the iPhone";
+    license = stdenv.lib.licenses.gpl3Plus;
+  };
+}
diff --git a/pkgs/development/tools/build-managers/cmake/default.nix b/pkgs/development/tools/build-managers/cmake/default.nix
index 9ce69374cd88..42277b22036b 100644
--- a/pkgs/development/tools/build-managers/cmake/default.nix
+++ b/pkgs/development/tools/build-managers/cmake/default.nix
@@ -26,7 +26,13 @@ stdenv.mkDerivation rec {
   patches =
     # Don't search in non-Nix locations such as /usr, but do search in
     # Nixpkgs' Glibc. 
-    optional (stdenv ? glibc) ./search-path.patch;
+    optional (stdenv ? glibc) ./search-path.patch ++
+    optional (stdenv ? cross) (fetchurl {
+      name = "fix-darwin-cross-compile.patch";
+      url = "http://public.kitware.com/Bug/file_download.php?"
+          + "file_id=4981&type=bug";
+      sha256 = "16acmdr27adma7gs9rs0dxdiqppm15vl3vv3agy7y8s94wyh4ybv";
+    });
 
   buildInputs = [ curl expat zlib bzip2 libarchive ]
     ++ optional useNcurses ncurses
diff --git a/pkgs/development/tools/misc/binutils-cross/builder.sh b/pkgs/development/tools/misc/binutils-cross/builder.sh
deleted file mode 100644
index 29d3b3779b6a..000000000000
--- a/pkgs/development/tools/misc/binutils-cross/builder.sh
+++ /dev/null
@@ -1,26 +0,0 @@
-source $stdenv/setup
-
-if test $cross = "arm-linux" ; then
-  configureFlags="--target=arm-linux"
-elif test $cross = "mips-linux" ; then
-  configureFlags="--target=mips-linux"
-elif test $cross = "mipsel-linux" ; then
-  configureFlags="--target=mipsel-linux"
-elif test $cross = "sparc-linux" ; then
-  configureFlags="--target=sparc-linux"
-elif test $cross = "powerpc-linux" ; then
-  configureFlags="--target=powerpc-linux"
-elif test $cross = "ppc-linux" ; then
-  configureFlags="--target=powerpc-linux"
-fi
-
-patchConfigure() {
-    # Clear the default library search path.
-    if test "$noSysDirs" = "1"; then
-        echo 'NATIVE_LIB_DIRS=' >> ld/configure.tgt
-    fi
-}
-
-preConfigure=patchConfigure
-
-genericBuild
diff --git a/pkgs/development/tools/misc/binutils-cross/default.nix b/pkgs/development/tools/misc/binutils-cross/default.nix
deleted file mode 100644
index 7d77baced373..000000000000
--- a/pkgs/development/tools/misc/binutils-cross/default.nix
+++ /dev/null
@@ -1,13 +0,0 @@
-{stdenv, fetchurl, noSysDirs, cross}:
-
-stdenv.mkDerivation {
-  name = "binutils-2.16.1";
-  builder = ./builder.sh;
-  src = fetchurl {
-    url = http://ftp.nluug.nl/gnu/binutils/binutils-2.16.1.tar.bz2;
-    md5 = "6a9d529efb285071dad10e1f3d2b2967";
-  };
-  inherit noSysDirs;
-  #configureFlags = if cross=="arm-linux" then "--target=arm-linux" else if cross=="mips-linux" then "--target=mips-linux" else if cross=="sparc-linux" then "--target=sparc-linux"; 
-  inherit cross;
-}
diff --git a/pkgs/os-specific/darwin/cctools-port/default.nix b/pkgs/os-specific/darwin/cctools-port/default.nix
new file mode 100644
index 000000000000..cdc259c1e2a0
--- /dev/null
+++ b/pkgs/os-specific/darwin/cctools-port/default.nix
@@ -0,0 +1,64 @@
+{ stdenv, cross, fetchurl, autoconf, automake, libtool
+, libcxx, llvm, clang, openssl, libuuid
+, maloader, makeWrapper, xctoolchain
+}:
+
+stdenv.mkDerivation rec {
+  name = "cctools-port-${version}";
+  version = "845";
+
+  src = fetchurl {
+    url = "https://github.com/tpoechtrager/cctools-port/archive/"
+        + "cctools-${version}-ld64-136-1.tar.gz";
+    sha256 = "06pg6h1g8avgx4j6cfykdpggf490li796gzhhyqn27jsagli307i";
+  };
+
+  buildInputs = [
+    autoconf automake libtool libcxx llvm clang openssl libuuid makeWrapper
+  ];
+
+  patches = [ ./ld-rpath-nonfinal.patch ./ld-ignore-rpath-link.patch ];
+
+  enableParallelBuilding = true;
+
+  postPatch = ''
+    patchShebangs tools
+    sed -i -e 's/which/type -P/' tools/*.sh
+    sed -i -e 's|clang++|& -I${libcxx}/include/c++/v1|' cctools/autogen.sh
+
+    # Workaround for https://www.sourceware.org/bugzilla/show_bug.cgi?id=11157
+    cat > cctools/include/unistd.h <<EOF
+    #ifdef __block
+    #  undef __block
+    #  include_next "unistd.h"
+    #  define __block __attribute__((__blocks__(byref)))
+    #else
+    #  include_next "unistd.h"
+    #endif
+    EOF
+  '';
+
+  preConfigure = ''
+    cd cctools
+    sh autogen.sh
+  '';
+
+  configureFlags = [
+    "CXXFLAGS=-I${libcxx}/include/c++/v1"
+    "--target=${cross.config}"
+  ];
+
+  postInstall = ''
+    for tool in dyldinfo dwarfdump dsymutil; do
+      makeWrapper "${maloader}/bin/ld-mac" "$out/bin/${cross.config}-$tool" \
+        --add-flags "${xctoolchain}/bin/$tool"
+      ln -s "$out/bin/${cross.config}-$tool" "$out/bin/$tool"
+    done
+  '';
+
+  meta = {
+    homepage = "http://www.opensource.apple.com/source/cctools/";
+    description = "Mac OS X Compiler Tools (cross-platform port)";
+    license = stdenv.lib.licenses.apsl20;
+  };
+}
diff --git a/pkgs/os-specific/darwin/cctools-port/ld-ignore-rpath-link.patch b/pkgs/os-specific/darwin/cctools-port/ld-ignore-rpath-link.patch
new file mode 100644
index 000000000000..fc87f69ac32d
--- /dev/null
+++ b/pkgs/os-specific/darwin/cctools-port/ld-ignore-rpath-link.patch
@@ -0,0 +1,16 @@
+diff --git a/cctools/ld64/src/ld/Options.cpp b/cctools/ld64/src/ld/Options.cpp
+index 2565518..9250016 100644
+--- a/cctools/ld64/src/ld/Options.cpp
++++ b/cctools/ld64/src/ld/Options.cpp
+@@ -2522,6 +2522,11 @@ void Options::parse(int argc, const char* argv[])
+ 					throw "missing argument to -rpath";
+ 				fRPaths.push_back(path);
+ 			}
++			else if ( strcmp(arg, "-rpath-link") == 0 ) {
++				const char* path = argv[++i];
++				if ( path == NULL )
++					throw "missing argument to -rpath-link";
++			}
+ 			else if ( strcmp(arg, "-read_only_stubs") == 0 ) {
+ 				fReadOnlyx86Stubs = true;
+ 			}
diff --git a/pkgs/os-specific/darwin/cctools-port/ld-rpath-nonfinal.patch b/pkgs/os-specific/darwin/cctools-port/ld-rpath-nonfinal.patch
new file mode 100644
index 000000000000..265d0d304771
--- /dev/null
+++ b/pkgs/os-specific/darwin/cctools-port/ld-rpath-nonfinal.patch
@@ -0,0 +1,31 @@
+diff --git a/cctools/ld64/src/ld/Options.cpp b/cctools/ld64/src/ld/Options.cpp
+index 9250016..91d54ec 100644
+--- a/cctools/ld64/src/ld/Options.cpp
++++ b/cctools/ld64/src/ld/Options.cpp
+@@ -4175,23 +4175,9 @@ void Options::checkIllegalOptionCombinations()
+ 		throw "-r and -dead_strip cannot be used together";
+ 
+ 	// can't use -rpath unless targeting 10.5 or later
+-	if ( fRPaths.size() > 0 ) {
+-		if ( !minOS(ld::mac10_5, ld::iOS_2_0) )
+-			throw "-rpath can only be used when targeting Mac OS X 10.5 or later";
+-		switch ( fOutputKind ) {
+-			case Options::kDynamicExecutable:
+-			case Options::kDynamicLibrary:
+-			case Options::kDynamicBundle:
+-				break;
+-			case Options::kStaticExecutable:
+-			case Options::kObjectFile:
+-			case Options::kDyld:
+-			case Options::kPreload:
+-			case Options::kKextBundle:
+-				throw "-rpath can only be used when creating a dynamic final linked image";
+-		}
+-	}
+-	
++	if ( fRPaths.size() > 0 && !minOS(ld::mac10_5, ld::iOS_2_0) )
++		throw "-rpath can only be used when targeting Mac OS X 10.5 or later";
++
+ 	if ( fPositionIndependentExecutable ) {
+ 		switch ( fOutputKind ) {
+ 			case Options::kDynamicExecutable:
diff --git a/pkgs/os-specific/darwin/maloader/default.nix b/pkgs/os-specific/darwin/maloader/default.nix
new file mode 100644
index 000000000000..f5bfe890735c
--- /dev/null
+++ b/pkgs/os-specific/darwin/maloader/default.nix
@@ -0,0 +1,36 @@
+{ stdenv, fetchgit, opencflite, clang, libcxx }:
+
+stdenv.mkDerivation {
+  name = "maloader-0git";
+
+  src = fetchgit {
+    url = "git://github.com/shinh/maloader.git";
+    rev = "5f220393e0b7b9ad0cf1aba0e89df2b42a1f0442";
+    sha256 = "07j9b7n0grrbxxyn2h8pnk6pa8b370wq5z5zwbds8dlhi7q37rhn";
+  };
+
+  postPatch = ''
+    sed -i \
+      -e '/if.*loadLibMac.*mypath/s|mypath|"'"$out/lib/"'"|' \
+      -e 's|libCoreFoundation\.so|${opencflite}/lib/&|' \
+      ld-mac.cc
+  '';
+
+  NIX_CFLAGS_COMPILE = "-I${libcxx}/include/c++/v1";
+  buildInputs = [ clang libcxx ];
+  buildFlags = [ "USE_LIBCXX=1" "release" ];
+
+  installPhase = ''
+    install -vD libmac.so "$out/lib/libmac.so"
+
+    for bin in extract macho2elf ld-mac; do
+      install -vD "$bin" "$out/bin/$bin"
+    done
+  '';
+
+  meta = {
+    description = "Mach-O loader for Linux";
+    homepage = "https://github.com/shinh/maloader";
+    license = stdenv.lib.licenses.bsd2;
+  };
+}
diff --git a/pkgs/os-specific/darwin/opencflite/default.nix b/pkgs/os-specific/darwin/opencflite/default.nix
new file mode 100644
index 000000000000..a2408bfc5c8a
--- /dev/null
+++ b/pkgs/os-specific/darwin/opencflite/default.nix
@@ -0,0 +1,21 @@
+{ stdenv, fetchurl, icu, libuuid, tzdata }:
+
+stdenv.mkDerivation rec {
+  name = "opencflite-${version}";
+  version = "476.19.0";
+
+  src = fetchurl {
+    url = "mirror://sourceforge/opencflite/${name}.tar.gz";
+    sha256 = "0jgmzs0ycl930hmzcvx0ykryik56704yw62w394q1q3xw5kkjn9v";
+  };
+
+  configureFlags = [ "--with-uuid=${libuuid}" ];
+  buildInputs = [ icu tzdata.lib ];
+  enableParallelBuilding = true;
+
+  meta = {
+    description = "Cross platform port of the OS X CoreFoundation";
+    homepage = "http://sourceforge.net/projects/opencflite/";
+    license = stdenv.lib.licenses.apsl20;
+  };
+}
diff --git a/pkgs/os-specific/darwin/xcode/default.nix b/pkgs/os-specific/darwin/xcode/default.nix
new file mode 100644
index 000000000000..73585d59c0b3
--- /dev/null
+++ b/pkgs/os-specific/darwin/xcode/default.nix
@@ -0,0 +1,52 @@
+{ stdenv, requireFile, xpwn }:
+
+with stdenv.lib;
+
+let
+  osxVersion = "10.9";
+in stdenv.mkDerivation rec {
+  name = "xcode-${version}";
+  version = "5.1";
+
+  src = requireFile {
+    name = "xcode_${version}.dmg";
+    url = meta.homepage;
+    sha256 = "70bb550cc14eca80b9825f4ae9bfbf7f076bb75777311be428bc30a7eb7a6f7e";
+  };
+
+  phases = [ "unpackPhase" "patchPhase" "installPhase" "fixupPhase" ];
+  outputs = [ "out" "toolchain" ];
+
+
+  unpackCmd = let
+    basePath = "Xcode.app/Contents/Developer/Platforms/MacOSX.platform";
+    sdkPath = "${basePath}/Developer/SDKs";
+  in ''
+    ${xpwn}/bin/dmg extract "$curSrc" main.hfs > /dev/null
+    ${xpwn}/bin/hfsplus main.hfs extractall "${sdkPath}" > /dev/null
+  '';
+
+  setSourceRoot = "sourceRoot=MacOSX${osxVersion}.sdk";
+
+  patches = optional (osxVersion == "10.9") ./gcc-fix-enum-attributes.patch;
+
+  installPhase = ''
+    ensureDir "$out/share/sysroot"
+    cp -a * "$out/share/sysroot/"
+    ln -s "$out/share/sysroot/usr/lib" "$out/lib"
+    ln -s "$out/share/sysroot/usr/include" "$out/include"
+
+    ensureDir "$toolchain"
+    pushd "$toolchain"
+    ${xpwn}/bin/hfsplus "$(dirs +1)/../main.hfs" extractall \
+      Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr \
+      > /dev/null
+    popd
+  '';
+
+  meta = {
+    homepage = "https://developer.apple.com/downloads/";
+    description = "Apple's XCode SDK";
+    license = stdenv.lib.licenses.unfree;
+  };
+}
diff --git a/pkgs/os-specific/darwin/xcode/gcc-fix-enum-attributes.patch b/pkgs/os-specific/darwin/xcode/gcc-fix-enum-attributes.patch
new file mode 100644
index 000000000000..1e1895c28945
--- /dev/null
+++ b/pkgs/os-specific/darwin/xcode/gcc-fix-enum-attributes.patch
@@ -0,0 +1,13 @@
+diff --git a/System/Library/Frameworks/Foundation.framework/Headers/NSUserNotification.h b/System/Library/Frameworks/Foundation.framework/Headers/NSUserNotification.h
+index fa0c290..7da7e0c 100644
+--- a/System/Library/Frameworks/Foundation.framework/Headers/NSUserNotification.h
++++ b/System/Library/Frameworks/Foundation.framework/Headers/NSUserNotification.h
+@@ -13,7 +13,7 @@ typedef NS_ENUM(NSInteger, NSUserNotificationActivationType) {
+     NSUserNotificationActivationTypeNone = 0,
+     NSUserNotificationActivationTypeContentsClicked = 1,
+     NSUserNotificationActivationTypeActionButtonClicked = 2,
+-    NSUserNotificationActivationTypeReplied NS_AVAILABLE(10_9, NA) = 3
++    NSUserNotificationActivationTypeReplied = 3
+ } NS_ENUM_AVAILABLE(10_8, NA);
+ 
+ NS_CLASS_AVAILABLE(10_8, NA)
diff --git a/pkgs/os-specific/windows/mingw-w64/default.nix b/pkgs/os-specific/windows/mingw-w64/default.nix
index c358565c4551..bf15b208e1a7 100644
--- a/pkgs/os-specific/windows/mingw-w64/default.nix
+++ b/pkgs/os-specific/windows/mingw-w64/default.nix
@@ -1,30 +1,30 @@
-{stdenv, fetchurl, binutilsCross ? null, gccCross ? null, onlyHeaders ? false}:
+{ stdenv, fetchurl, binutilsCross ? null, gccCross ? null
+, onlyHeaders ? false
+, onlyPthreads ? false
+}:
 
 let
-  name = "mingw-w64-2.0.3";
+  name = "mingw-w64-3.1.0";
 in
 stdenv.mkDerivation (rec {
   inherit name;
 
   src = fetchurl {
-    url = "mirror://sourceforge/mingw-w64/mingw-w64-v2.0.3.tar.gz";
-    sha256 = "043jk6z90f9pxs9kfn6ckh2vlnbgcv6yfbp5ybahrj3z58dcijp5";
+    url = "mirror://sourceforge/mingw-w64/mingw-w64-v3.1.0.tar.bz2";
+    sha256 = "1lhpw381gc59w8b1r9zzdwa9cdi2wx6qx7s6rvajapmbw7ksgrzc";
   };
-
-  # I don't know what's that $host directory about, I put the
-  # files inside include as usual.
-  postInstall = ''
-    rmdir $out/include
-    mv $out/x86_64-w64-mingw32/* $out
-    rm -R $out/x86_64-w64-mingw32
-  '';
 } //
 (if onlyHeaders then {
   name = name + "-headers";
-  preConfingure = ''
+  preConfigure = ''
     cd mingw-w64-headers
   '';
-  configureFlags = "--without-crt --host=x86_64-w64-mingw32";
+  configureFlags = "--without-crt";
+} else if onlyPthreads then {
+  name = name + "-pthreads";
+  preConfigure = ''
+    cd mingw-w64-libraries/winpthreads
+  '';
 } else {
   buildInputs = [ gccCross binutilsCross ];
 
diff --git a/pkgs/tools/compression/bzip2/default.nix b/pkgs/tools/compression/bzip2/default.nix
index a431cd30a21f..55fca6ca3cb9 100644
--- a/pkgs/tools/compression/bzip2/default.nix
+++ b/pkgs/tools/compression/bzip2/default.nix
@@ -14,6 +14,7 @@ stdenv.mkDerivation {
 
   crossAttrs = {
     patchPhase = ''
+      sed -i -e '/<sys\\stat\.h>/s|\\|/|' bzip2.c
       sed -i -e 's/CC=gcc/CC=${stdenv.cross.config}-gcc/' \
         -e 's/AR=ar/AR=${stdenv.cross.config}-ar/' \
         -e 's/RANLIB=ranlib/RANLIB=${stdenv.cross.config}-ranlib/' \
diff --git a/pkgs/tools/graphics/gifsicle/default.nix b/pkgs/tools/graphics/gifsicle/default.nix
index f3f17692df87..debc340ee4ce 100644
--- a/pkgs/tools/graphics/gifsicle/default.nix
+++ b/pkgs/tools/graphics/gifsicle/default.nix
@@ -1,4 +1,6 @@
-{ stdenv, fetchurl, xproto, libXt, libX11 }:
+{ stdenv, fetchurl, xproto, libXt, libX11, gifview ? false, static ? false }:
+
+with stdenv.lib;
 
 stdenv.mkDerivation {
   name = "gifsicle-1.78";
@@ -8,7 +10,9 @@ stdenv.mkDerivation {
     sha256 = "0dzp5sg82klji4lbj1m4cyg9fb3l837gkipdx657clib97klyv53";
   };
 
-  buildInputs = [ xproto libXt libX11 ];
+  buildInputs = optional gifview [ xproto libXt libX11 ];
+
+  LDFLAGS = optional static "-static";
 
   meta = { 
     description = "Command-line tool for creating, editing, and getting information about GIF images and animations";
diff --git a/pkgs/tools/graphics/optipng/default.nix b/pkgs/tools/graphics/optipng/default.nix
index 1b2fc6dbb90c..1984ab7b1de8 100644
--- a/pkgs/tools/graphics/optipng/default.nix
+++ b/pkgs/tools/graphics/optipng/default.nix
@@ -1,7 +1,9 @@
-{ stdenv, fetchurl }:
+{ stdenv, fetchurl, libpng, static ? false }:
 
 # This package comes with its own copy of zlib, libpng and pngxtern
 
+with stdenv.lib;
+
 stdenv.mkDerivation rec {
   name = "optipng-0.7.4";
 
@@ -10,6 +12,24 @@ stdenv.mkDerivation rec {
     sha256 = "1zrphbz17rhhfl1l95q5s979rrhifbwczl2xj1fdrnq5jid5s2sj";
   };
 
+  buildInputs = [ libpng ];
+
+  LDFLAGS = optional static "-static";
+  configureFlags = "--with-system-zlib --with-system-libpng";
+
+  crossAttrs = {
+    CC="${stdenv.cross.config}-gcc";
+    LD="${stdenv.cross.config}-gcc";
+    AR="${stdenv.cross.config}-ar";
+    RANLIB="${stdenv.cross.config}-ranlib";
+    configurePhase = ''
+      ./configure -prefix="$out" --with-system-zlib --with-system-libpng
+    '';
+    postInstall = optional (stdenv.cross.libc == "msvcrt") ''
+      mv "$out"/bin/optipng "$out"/bin/optipng.exe
+    '';
+  };
+
   meta = {
     homepage = http://optipng.sourceforge.net/;
     description = "A PNG optimizer";
diff --git a/pkgs/top-level/all-packages.nix b/pkgs/top-level/all-packages.nix
index 9f9b15920b67..2817906d2819 100644
--- a/pkgs/top-level/all-packages.nix
+++ b/pkgs/top-level/all-packages.nix
@@ -1631,7 +1631,9 @@ let
 
   openvpn_learnaddress = callPackage ../tools/networking/openvpn/openvpn_learnaddress.nix { };
 
-  optipng = callPackage ../tools/graphics/optipng { };
+  optipng = callPackage ../tools/graphics/optipng {
+    libpng = libpng12;
+  };
 
   oslrd = callPackage ../tools/networking/oslrd { };
 
@@ -2515,10 +2517,10 @@ let
   gcc_realCross = gcc48_realCross;
 
   gccCrossStageStatic = let
-      isMingw = (stdenv.cross.libc == "msvcrt");
-      isMingw64 = isMingw && stdenv.cross.config == "x86_64-w64-mingw32";
-      libcCross1 = if isMingw64 then windows.mingw_w64_headers else
-                   if isMingw then windows.mingw_headers1 else null;
+      libcCross1 =
+        if stdenv.cross.libc == "msvcrt" then windows.mingw_w64_headers
+        else if stdenv.cross.libc == "libSystem" then darwin.xcode
+        else null;
     in
       wrapGCCCross {
       gcc = forceNativeDrv (lib.addMetaAttrs { hydraPlatforms = []; } (
@@ -3594,11 +3596,13 @@ let
     gold = false;
   });
 
-  binutilsCross = lowPrio (forceNativeDrv (import ../development/tools/misc/binutils {
-    inherit stdenv fetchurl zlib;
-    noSysDirs = true;
-    cross = assert crossSystem != null; crossSystem;
-  }));
+  binutilsCross =
+    if crossSystem != null && crossSystem.libc == "libSystem" then darwin.cctools
+    else lowPrio (forceNativeDrv (import ../development/tools/misc/binutils {
+      inherit stdenv fetchurl zlib;
+      noSysDirs = true;
+      cross = assert crossSystem != null; crossSystem;
+    }));
 
   bison2 = callPackage ../development/tools/parsing/bison/2.x.nix { };
   bison3 = callPackage ../development/tools/parsing/bison/3.x.nix { };
@@ -3985,6 +3989,8 @@ let
 
   xmlindent = callPackage ../development/web/xmlindent {};
 
+  xpwn = callPackage ../development/mobile/xpwn {};
+
   xxdiff = callPackage ../development/tools/misc/xxdiff {
     bison = bison2;
   };
@@ -4409,9 +4415,8 @@ let
   # We can choose:
   libcCrossChooser = name : if name == "glibc" then glibcCross
     else if name == "uclibc" then uclibcCross
-    else if name == "msvcrt" && stdenv.cross.config == "x86_64-w64-mingw32" then
-      windows.mingw_w64
-    else if name == "msvcrt" then windows.mingw_headers3
+    else if name == "msvcrt" then windows.mingw_w64
+    else if name == "libSystem" then darwin.xcode
     else throw "Unknown libc";
 
   libcCross = assert crossSystem != null; libcCrossChooser crossSystem.libc;
@@ -6761,6 +6766,22 @@ let
 
   cramfsswap = callPackage ../os-specific/linux/cramfsswap { };
 
+  darwin = rec {
+    cctools = forceNativeDrv (callPackage ../os-specific/darwin/cctools-port {
+      cross = assert crossSystem != null; crossSystem;
+      inherit maloader;
+      xctoolchain = xcode.toolchain;
+    });
+
+    maloader = callPackage ../os-specific/darwin/maloader {
+      inherit opencflite;
+    };
+
+    opencflite = callPackage ../os-specific/darwin/opencflite {};
+
+    xcode = callPackage ../os-specific/darwin/xcode {};
+  };
+
   devicemapper = lvm2;
 
   dmidecode = callPackage ../os-specific/linux/dmidecode { };
@@ -7404,6 +7425,10 @@ let
       onlyHeaders = true;
     };
 
+    mingw_w64_pthreads = callPackage ../os-specific/windows/mingw-w64 {
+      onlyPthreads = true;
+    };
+
     pthreads = callPackage ../os-specific/windows/pthread-w32 {
       mingw_headers = mingw_headers3;
     };
diff --git a/pkgs/top-level/release-cross.nix b/pkgs/top-level/release-cross.nix
index 6871567ca8d1..cef3d2dffbdb 100644
--- a/pkgs/top-level/release-cross.nix
+++ b/pkgs/top-level/release-cross.nix
@@ -140,11 +140,11 @@ in {
   crossUltraSparcLinux = mapTestOnCross crossSystem basic;
 }) // (
 
-/* Test some cross builds on mingw32 */
+/* Test some cross builds on 32 bit mingw-w64 */
 let
   crossSystem = {
-      config = "i686-pc-mingw32";
-      arch = "x86";
+      config = "i686-w64-mingw32";
+      arch = "x86"; # Irrelevant
       libc = "msvcrt"; # This distinguishes the mingw (non posix) toolchain
       platform = {};
   };
@@ -161,11 +161,10 @@ in {
   };
 }) // (
 
-/* Test some cross builds on mingw-w64 */
+/* Test some cross builds on 64 bit mingw-w64 */
 let
   crossSystem = {
-      # That's the triplet they use in the mingw-w64 docs,
-      # and it's relevant for nixpkgs conditions.
+      # That's the triplet they use in the mingw-w64 docs.
       config = "x86_64-w64-mingw32";
       arch = "x86_64"; # Irrelevant
       libc = "msvcrt"; # This distinguishes the mingw (non posix) toolchain