|
| 1 | +{ pkgs ? import <nixpkgs> {}, |
| 2 | + llvm-hs-src ? pkgs.fetchFromGitHub { |
| 3 | + owner = "llvm-hs"; |
| 4 | + repo = "llvm-hs"; |
| 5 | + rev = "llvm-12"; |
| 6 | + sha256 = "IG4Mh89bY+PtBJtzlXKYsPljfHP7OSQk03pV6fSmdRY="; |
| 7 | + }, |
| 8 | + cudaPackage ? pkgs.cudaPackages.cudatoolkit_11, |
| 9 | + cuda ? false, |
| 10 | + optimized ? true, |
| 11 | + live ? true, |
| 12 | +}: |
| 13 | +let |
| 14 | + llvm-hs-pure = pkgs.haskellPackages.callCabal2nix "llvm-hs-pure" "${llvm-hs-src}/llvm-hs-pure" { |
| 15 | + }; |
| 16 | + llvm-hs = (pkgs.haskellPackages.callCabal2nix "llvm-hs" "${llvm-hs-src}/llvm-hs" { |
| 17 | + inherit llvm-hs-pure; |
| 18 | + }).overrideAttrs (oldAttrs: rec { |
| 19 | + buildInputs = oldAttrs.buildInputs ++ [ |
| 20 | + pkgs.llvm_12 |
| 21 | + ]; |
| 22 | + }); |
| 23 | + buildFlags = pkgs.lib.optionals optimized [ |
| 24 | + "-foptimized" |
| 25 | + ] ++ pkgs.lib.optionals live [ |
| 26 | + "-flive" |
| 27 | + ] ++ pkgs.lib.optionals cuda [ |
| 28 | + "-fcuda" |
| 29 | + "--extra-include-dirs=${cudaPackage}/include" |
| 30 | + "--extra-lib-dirs=${cudaPackage}/lib64/stubs" |
| 31 | + ]; |
| 32 | + cxxFlags = [ |
| 33 | + "-fPIC" |
| 34 | + "-std=c++11" |
| 35 | + "-fno-exceptions" |
| 36 | + "-fno-rtti" |
| 37 | + ] ++ pkgs.lib.optional cuda "-DDEX_CUDA" |
| 38 | + ++ pkgs.lib.optional live "-DDEX_LIVE"; |
| 39 | + buildRuntimeCommand = '' |
| 40 | + ${pkgs.clang_9}/bin/clang++ \ |
| 41 | + ${builtins.concatStringsSep " " cxxFlags} \ |
| 42 | + -c \ |
| 43 | + -emit-llvm \ |
| 44 | + -I${pkgs.libpng}/include \ |
| 45 | + src/lib/dexrt.cpp \ |
| 46 | + -o src/lib/dexrt.bc |
| 47 | + ''; |
| 48 | +in |
| 49 | + # `callCabal2nix` converts `dex.cabal` into a Nix file and builds it. |
| 50 | + # Before we do the Haskell build though, we need to first compile the Dex runtime |
| 51 | + # so it's properly linked in when compiling Dex. Normally the makefile does this, |
| 52 | + # so we instead sneak compiling the runtime in the configuration phase for the Haskell build. |
| 53 | + (pkgs.haskellPackages.callCabal2nix "dex" ./. { |
| 54 | + inherit llvm-hs; |
| 55 | + inherit llvm-hs-pure; |
| 56 | + }).overrideAttrs (attrs: { |
| 57 | + configurePhase = '' |
| 58 | + # Compile the Dex runtime |
| 59 | + echo 'Compiling the Dex runtime...' |
| 60 | + set -x |
| 61 | + ${buildRuntimeCommand} |
| 62 | + set +x |
| 63 | + echo 'Done compiling the Dex runtime.' |
| 64 | +
|
| 65 | + # Run the Haskell configuration phase |
| 66 | + ${attrs.configurePhase} |
| 67 | + ''; |
| 68 | + configureFlags = builtins.concatStringsSep " " buildFlags; |
| 69 | + buildInputs = attrs.buildInputs ++ (pkgs.lib.optional cuda |
| 70 | + cudaPackage |
| 71 | + ); |
| 72 | + }) |
0 commit comments