btrbk: add mainProgram (#356350)
[NixPkgs.git] / pkgs / development / compilers / gerbil / build.nix
blob5ed7339c31beb60281c86fa2a9c68f0e73310fd3
1 { gccStdenv, lib, coreutils,
2   openssl, zlib, sqlite,
3   version, git-version, src,
4   gambit-support,
5   gambit-git-version,
6   gambit-stampYmd,
7   gambit-stampHms,
8   gambit-params }:
10 # We use Gambit, that works 10x better with GCC than Clang. See ../gambit/build.nix
11 let stdenv = gccStdenv; in
13 stdenv.mkDerivation rec {
14   pname = "gerbil";
15   inherit version;
16   inherit src;
18   buildInputs_libraries = [ openssl zlib sqlite ];
20   # TODO: either fix all of Gerbil's dependencies to provide static libraries,
21   # or give up and delete all tentative support for static libraries.
22   #buildInputs_staticLibraries = map makeStaticLibraries buildInputs_libraries;
24   buildInputs = buildInputs_libraries;
26   postPatch = ''
27     patchShebangs . ;
28     grep -Fl '#!/usr/bin/env' `find . -type f -executable` | while read f ; do
29       substituteInPlace "$f" --replace '#!/usr/bin/env' '#!${coreutils}/bin/env' ;
30     done ;
31     cat > MANIFEST <<EOF
32     gerbil_stamp_version=v${git-version}
33     gambit_stamp_version=v${gambit-git-version}
34     gambit_stamp_ymd=${gambit-stampYmd}
35     gambit_stamp_hms=${gambit-stampHms}
36     EOF
37     for f in src/bootstrap/gerbil/compiler/driver__0.scm \
38              src/build/build-libgerbil.ss \
39              src/gerbil/compiler/driver.ss ; do
40       substituteInPlace "$f" --replace '"gcc"' '"${gccStdenv.cc}/bin/${gccStdenv.cc.targetPrefix}gcc"' ;
41     done
42   '';
44 ## TODO: make static compilation work.
45 ## For that, get all the packages below to somehow expose static libraries,
46 ## so we can offer users the option to statically link them into Gambit and/or Gerbil.
47 ## Then add the following to the postPatch script above:
48 #     cat > etc/gerbil_static_libraries.sh <<EOF
49 # OPENSSL_LIBCRYPTO=${makeStaticLibraries openssl}/lib/libcrypto.a # MISSING!
50 # OPENSSL_LIBSSL=${makeStaticLibraries openssl}/lib/libssl.a # MISSING!
51 # ZLIB=${makeStaticLibraries zlib}/lib/libz.a
52 # SQLITE=${makeStaticLibraries sqlite}/lib/sqlite.a # MISSING!
53 # EOF
55   configureFlags = [
56     "--prefix=$out/gerbil"
57     "--enable-zlib"
58     "--enable-sqlite"
59     "--enable-shared"
60     "--enable-march=" # Avoid non-portable invalid instructions. Use =native if local build only.
61   ];
63   configurePhase = ''
64     export CC=${gccStdenv.cc}/bin/${gccStdenv.cc.targetPrefix}gcc \
65            CXX=${gccStdenv.cc}/bin/${gccStdenv.cc.targetPrefix}g++ \
66            CPP=${gccStdenv.cc}/bin/${gccStdenv.cc.targetPrefix}cpp \
67            CXXCPP=${gccStdenv.cc}/bin/${gccStdenv.cc.targetPrefix}cpp \
68            LD=${gccStdenv.cc}/bin/${gccStdenv.cc.targetPrefix}ld \
69            XMKMF=${coreutils}/bin/false
70     unset CFLAGS LDFLAGS LIBS CPPFLAGS CXXFLAGS
71     ./configure ${builtins.concatStringsSep " " configureFlags}
72   '';
74   extraLdOptions = [
75       "-L${zlib}/lib"
76       "-L${openssl.out}/lib"
77       "-L${sqlite.out}/lib"
78     ];
80   buildPhase = ''
81     runHook preBuild
83     # gxprof testing uses $HOME/.cache/gerbil/gxc
84     export HOME=$PWD
85     export GERBIL_BUILD_CORES=$NIX_BUILD_CORES
86     export GERBIL_GXC=$PWD/bin/gxc
87     export GERBIL_BASE=$PWD
88     export GERBIL_PREFIX=$PWD
89     export GERBIL_PATH=$PWD/lib
90     export PATH=$PWD/bin:$PATH
91     ${gambit-support.export-gambopt gambit-params}
93     # Build, replacing make by build.sh
94     ( cd src && sh build.sh )
96     f=build/lib/libgerbil.so.ldd ; [ -f $f ] && :
97     substituteInPlace "$f" --replace '(' \
98       '(${lib.strings.concatStrings (map (x: "\"${x}\" " ) extraLdOptions)}'
100     runHook postBuild
101   '';
103   installPhase = ''
104     runHook preInstall
105     mkdir -p $out/gerbil $out/bin
106     ./install.sh
107     (cd $out/bin ; ln -s ../gerbil/bin/* .)
108     runHook postInstall
109   '' + lib.optionalString stdenv.hostPlatform.isDarwin ''
110     libgerbil="$(realpath "$out/gerbil/lib/libgerbil.so")"
111     install_name_tool -id "$libgerbil" "$libgerbil"
112   '';
114   dontStrip = true;
116   meta = {
117     description = "Gerbil Scheme";
118     homepage    = "https://github.com/vyzo/gerbil";
119     license     = lib.licenses.lgpl21Only; # dual, also asl20, like Gambit
120     # NB regarding platforms: regularly tested on Linux and on macOS.
121     # Please report success and/or failure to fare.
122     platforms   = lib.platforms.unix;
123     maintainers = with lib.maintainers; [ fare ];
124   };
126   outputsToInstall = [ "out" ];