@@ -0,0 +1,94 @@
1+{ stdenv, fetchurl, bison, pkgconfig, glib, gettext, perl, libgdiplus, libX11, callPackage, ncurses, zlib, withLLVM ? false, cacert, Foundation, libobjc, python, version, sha256, autoconf, libtool, automake, cmake, which }:
2+
3+let
4+ llvm = callPackage ./llvm.nix { };
5+in
6+stdenv.mkDerivation rec {
7+ name = "mono-${version}";
8+
9+ src = fetchurl {
10+ inherit sha256;
11+ url = "http://download.mono-project.com/sources/mono/${name}.tar.bz2";
12+ };
13+
14+ buildInputs =
15+ [ bison pkgconfig glib gettext perl libgdiplus libX11 ncurses zlib python autoconf libtool automake cmake which
16+ ]
17+ ++ (stdenv.lib.optionals stdenv.isDarwin [ Foundation libobjc ]);
18+
19+ propagatedBuildInputs = [glib];
20+
21+ NIX_LDFLAGS = if stdenv.isDarwin then "" else "-lgcc_s" ;
22+
23+ # To overcome the bug https://bugzilla.novell.com/show_bug.cgi?id=644723
24+ dontDisableStatic = true;
25+
26+ # In fact I think this line does not help at all to what I
27+ # wanted to achieve: have mono to find libgdiplus automatically
28+ configureFlags = [
29+ "--x-includes=${libX11.dev}/include"
30+ "--x-libraries=${libX11.out}/lib"
31+ "--with-libgdiplus=${libgdiplus}/lib/libgdiplus.so"
32+ ]
33+ ++ stdenv.lib.optionals withLLVM [
34+ "--enable-llvm"
35+ "--enable-llvmloaded"
36+ "--with-llvm=${llvm}"
37+ ];
38+
39+ configurePhase = ''
40+ substituteInPlace ./autogen.sh --replace "/usr/bin/env sh" "${stdenv.shell}"
41+ ./autogen.sh --prefix $out
42+ '';
43+
44+ # Attempt to fix this error when running "mcs --version":
45+ # The file /nix/store/xxx-mono-2.4.2.1/lib/mscorlib.dll is an invalid CIL image
46+ dontStrip = true;
47+
48+ # Parallel building doesn't work, as shows http://hydra.nixos.org/build/2983601
49+ enableParallelBuilding = false;
50+
51+ # We want pkg-config to take priority over the dlls in the Mono framework and the GAC
52+ # because we control pkg-config
53+ patches = [ ./pkgconfig-before-gac.patch ];
54+
55+ # Patch all the necessary scripts. Also, if we're using LLVM, we fix the default
56+ # LLVM path to point into the Mono LLVM build, since it's private anyway.
57+ preBuild = ''
58+ makeFlagsArray=(INSTALL=`type -tp install`)
59+ patchShebangs ./
60+ substituteInPlace mcs/class/corlib/System/Environment.cs --replace /usr/share "$out/share"
61+ '' + stdenv.lib.optionalString withLLVM ''
62+ substituteInPlace mono/mini/aot-compiler.c --replace "llvm_path = g_strdup (\"\")" "llvm_path = g_strdup (\"${llvm}/bin/\")"
63+ '';
64+
65+ # Fix mono DLLMap so it can find libX11 and gdiplus to run winforms apps
66+ # Other items in the DLLMap may need to be pointed to their store locations, I don't think this is exhaustive
67+ # http://www.mono-project.com/Config_DllMap
68+ postBuild = ''
69+ find . -name 'config' -type f | xargs \
70+ sed -i -e "s@libX11.so.6@${libX11.out}/lib/libX11.so.6@g" \
71+ -e "s@/.*libgdiplus.so@${libgdiplus}/lib/libgdiplus.so@g" \
72+ '';
73+
74+ # Without this, any Mono application attempting to open an SSL connection will throw with
75+ # The authentication or decryption has failed.
76+ # ---> Mono.Security.Protocol.Tls.TlsException: Invalid certificate received from server.
77+ postInstall = ''
78+ echo "Updating Mono key store"
79+ $out/bin/cert-sync ${cacert}/etc/ssl/certs/ca-bundle.crt
80+ ''
81+ # According to [1], gmcs is just mcs
82+ # [1] https://github.com/mono/mono/blob/master/scripts/gmcs.in
83+ + ''
84+ ln -s $out/bin/mcs $out/bin/gmcs
85+ '';
86+
87+ meta = {
88+ homepage = http://mono-project.com/;
89+ description = "Cross platform, open source .NET development framework";
90+ platforms = with stdenv.lib.platforms; darwin ++ linux;
91+ maintainers = with stdenv.lib.maintainers; [ viric thoughtpolice obadz vrthra ];
92+ license = stdenv.lib.licenses.free; # Combination of LGPL/X11/GPL ?
93+ };
94+}