summary refs log tree commit diff
path: root/pkgs/development/compilers/crystal
diff options
context:
space:
mode:
authorPeter Hoeg <peter@hoeg.com>2020-03-18 10:47:24 +0800
committerMichael Fellinger <michael.fellinger@iohk.io>2020-04-17 18:13:51 +0200
commit85897a44234a07941df7e402b800ba9e3cad0642 (patch)
treedff8e69c02fa5e7e73eaa814621b182031315ec2 /pkgs/development/compilers/crystal
parentfd1047a798d3e413472030a84afdba644dab4923 (diff)
downloadnixpkgs-85897a44234a07941df7e402b800ba9e3cad0642.tar
nixpkgs-85897a44234a07941df7e402b800ba9e3cad0642.tar.gz
nixpkgs-85897a44234a07941df7e402b800ba9e3cad0642.tar.bz2
nixpkgs-85897a44234a07941df7e402b800ba9e3cad0642.tar.lz
nixpkgs-85897a44234a07941df7e402b800ba9e3cad0642.tar.xz
nixpkgs-85897a44234a07941df7e402b800ba9e3cad0642.tar.zst
nixpkgs-85897a44234a07941df7e402b800ba9e3cad0642.zip
crystal: build using Makefile or shards if available
Diffstat (limited to 'pkgs/development/compilers/crystal')
-rw-r--r--pkgs/development/compilers/crystal/build-package.nix150
1 files changed, 105 insertions, 45 deletions
diff --git a/pkgs/development/compilers/crystal/build-package.nix b/pkgs/development/compilers/crystal/build-package.nix
index 8ffa89a11b4..9cf5701d12f 100644
--- a/pkgs/development/compilers/crystal/build-package.nix
+++ b/pkgs/development/compilers/crystal/build-package.nix
@@ -1,6 +1,9 @@
-{ stdenv, lib, crystal, linkFarm, fetchFromGitHub }:
-{ # Generate shards.nix with `nix-shell -p crystal2nix --run crystal2nix` in the projects root
-  shardsFile ? null
+{ stdenv, lib, crystal, shards, git, pkgconfig, which, linkFarm, fetchFromGitHub }:
+{
+  # Some projects do not include a lock file, so you can pass one
+  lockFile ? null
+  # Generate shards.nix with `nix-shell -p crystal2nix --run crystal2nix` in the projects root
+, shardsFile ? null
   # Specify binaries to build in the form { foo.src = "src/foo.cr"; }
   # The default `crystal build` options can be overridden with { foo.options = [ "--no-debug" ]; }
 , crystalBinaries ? {}
@@ -9,45 +12,102 @@
 let
   mkDerivationArgs = builtins.removeAttrs args [ "shardsFile" "crystalBinaries" ];
 
-  crystalLib = linkFarm "crystal-lib" (lib.mapAttrsToList (name: value: {
-    inherit name;
-    path = fetchFromGitHub value;
-  }) (import shardsFile));
-
-  defaultOptions = [ "--release" "--progress" "--no-debug" "--verbose" ];
-
-in stdenv.mkDerivation (mkDerivationArgs // {
-
-  configurePhase = args.configurePhase or ''
-    runHook preConfigure
-    ${lib.optionalString (shardsFile != null) "ln -s ${crystalLib} lib"}
-    runHook postConfigure
-  '';
-
-  buildInputs = args.buildInputs or [] ++ [ crystal ];
-
-  buildPhase = args.buildPhase or ''
-    runHook preBuild
-    ${lib.concatStringsSep "\n" (lib.mapAttrsToList (bin: attrs: ''
-      crystal ${lib.escapeShellArgs ([
-        "build"
-        "-o" bin
-        (attrs.src or (throw "No source file for crystal binary ${bin} provided"))
-      ] ++ attrs.options or defaultOptions)}
-    '') crystalBinaries)}
-    runHook postBuild
-  '';
-
-  installPhase = args.installPhase or ''
-    runHook preInstall
-    mkdir -p "$out/bin"
-    ${lib.concatMapStringsSep "\n" (bin: ''
-      mv ${lib.escapeShellArgs [ bin "${placeholder "out"}/bin/${bin}" ]}
-    '') (lib.attrNames crystalBinaries)}
-    runHook postInstall
-  '';
-
-  meta = args.meta or {} // {
-    platforms = args.meta.platforms or crystal.meta.platforms;
-  };
-})
+  crystalLib = linkFarm "crystal-lib" (
+    lib.mapAttrsToList (
+      name: value: {
+        inherit name;
+        path = fetchFromGitHub value;
+      }
+    ) (import shardsFile)
+  );
+
+  # we previously had --no-debug here but that is not recommended by upstream
+  defaultOptions = [ "--release" "--progress" "--verbose" ];
+
+  buildDirectly = shardsFile == null || crystalBinaries != {};
+in
+stdenv.mkDerivation (
+  mkDerivationArgs // {
+
+    configurePhase = args.configurePhase or ''
+      runHook preConfigure
+
+      ${lib.optionalString (lockFile != null) "ln -s ${lockFile} ./shard.lock"}
+      ${lib.optionalString (shardsFile != null) "ln -s ${crystalLib} lib"}
+
+      runHook postConfigure
+    '';
+
+    CRFLAGS = lib.concatStringsSep " " defaultOptions;
+
+    buildInputs = args.buildInputs or [] ++ [ crystal shards ];
+
+    nativeBuildInputs = args.nativeBuildInputs or [] ++ [ git pkgconfig which ];
+
+    buildPhase = args.buildPhase or (
+      ''
+        runHook preBuild
+
+        if [ -e Makefile ]; then
+          echo " ** building with make"
+
+          make ''${buildTargets:-build} $buildFlags
+        else
+      '' + (
+        if buildDirectly then ''
+          echo " ** building with crystal"
+
+          ${lib.concatStringsSep "\n" (
+          lib.mapAttrsToList (
+            bin: attrs: ''
+              crystal ${lib.escapeShellArgs (
+              [
+                "build"
+                "-o"
+                bin
+                (attrs.src or (throw "No source file for crystal binary ${bin} provided"))
+              ] ++ attrs.options or defaultOptions
+            )}
+            ''
+          ) crystalBinaries
+        )}
+        '' else ''
+          echo " ** building with shards"
+          shards build --local --production ${lib.concatStringsSep " " defaultOptions}
+        ''
+      ) + ''
+        fi
+
+        runHook postBuild
+      ''
+    );
+
+    installPhase = args.installPhase or (
+      ''
+        runHook preInstall
+
+        if [ -e Makefile ]; then
+          make ''${installTargets:-install} $installFlags
+        else
+      '' + (
+        if buildDirectly then
+          lib.concatMapStringsSep "\n" (
+            bin: ''
+              install -Dm555 ${lib.escapeShellArgs [ bin "${placeholder "out"}/bin/${bin}" ]}
+            ''
+          ) (lib.attrNames crystalBinaries)
+        else ''
+          shards install
+        ''
+      ) + ''
+        fi
+
+        runHook postInstall
+      ''
+    );
+
+    meta = args.meta or {} // {
+      platforms = args.meta.platforms or crystal.meta.platforms;
+    };
+  }
+)