-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathflake.nix
183 lines (150 loc) · 5.63 KB
/
flake.nix
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
{
description = "Haxe Ascii Spriting Engine";
outputs = { self, nixpkgs }: let
inherit (nixpkgs) lib;
version = "0.1.0";
systems = lib.attrNames (removeAttrs nixpkgs.legacyPackages [
# Excluding architectures where OCaml is either broken or unsupported.
"aarch64-linux" "armv6l-linux" "armv7l-linux"
]);
forAllSystems = lib.genAttrs systems;
forEachTest = fun: let
preloaders = map toString (lib.range 1 3);
mkPreloadMain = p: "hase.test.preloader.PreloaderTest${p}";
preloadMains = map mkPreloadMain preloaders;
mains = lib.singleton "hase.test.Main" ++ preloadMains;
preloadTargets = map (p: "preload_test_${p}") preloaders;
targets = lib.singleton "main_tests" ++ preloadTargets;
in lib.concatStringsSep "\n" (lib.zipListsWith fun mains targets);
compileWith = targetLang: main: target: let
file = if targetLang == "cpp" then target
else if targetLang == "neko" then "${target}.n"
else "${target}.${targetLang}";
args = [ "haxe" "-main" main "-${targetLang}" file "-dce" "full" ];
in lib.concatMapStringsSep " " lib.escapeShellArg args;
mkPackage = pkgs: runTests: isShell: pkgs.stdenv.mkDerivation {
pname = "hase";
inherit version;
src = if isShell then null else self;
nativeBuildInputs = [ pkgs.phantomjs ];
buildInputs = [ pkgs.haxe pkgs.hxcpp pkgs.neko ];
buildPhase = lib.optionalString runTests ''
${forEachTest (compileWith "neko")}
${forEachTest (compileWith "js")}
${forEachTest (compileWith "cpp")}
'';
doCheck = runTests;
checkPhase = ''
header "running C++ tests"
${forEachTest (main: target: let
mainCls = lib.head (builtins.match ".*\\.(.+)" main);
in "./${target}/${mainCls}")}
stopNest
header "running Neko tests"
${forEachTest (main: target: "neko ${target}.n")}
stopNest
header "running JS tests"
${forEachTest (main: target: "phantomjs ${target}.js")}
stopNest
'';
installPhase = pkgs.haxePackages.installLibHaxe {
libname = "hase";
inherit version;
files = "hase";
};
};
mkPackageWithSystem = system: mkPackage nixpkgs.legacyPackages.${system};
mkExample = system: target: let
pkgs = import nixpkgs {
inherit system;
overlays = [ self.overlay ];
};
in pkgs.buildHaseApplication ({
mainClass = "Example";
pname = "hase-example";
inherit version target;
src = ./example;
postInstall = ''
if [ "$target" = js ]; then
install -m 0644 -vD example.html "$installPath/example.html"
fi
'';
} // lib.optionalAttrs (target == "cpp") {
executableName = "hase-example";
});
in {
packages = forAllSystems (system: {
hase = mkPackageWithSystem system false false;
example-js = mkExample system "js";
example-neko = mkExample system "neko";
example-cpp = mkExample system "cpp";
});
overlay = final: prev: {
haxePackages = prev.haxePackages // {
hase = mkPackage prev false false;
};
buildHaseApplication =
{ mainClass
, executableName ? lib.toLower mainClass
, pname ? executableName
, target ? "js"
, hxArgs ? []
, ...
}@attrs:
let
programSuffix = if target == "neko" then "n" else target;
drvAttrs = rec {
buildInputs = [ final.haxe final.haxePackages.hase ]
++ lib.optional (target == "cpp") final.hxcpp
++ (attrs.buildInputs or []);
inherit pname mainClass target hxArgs;
targetPath = "${executableName}.${programSuffix}";
installBinary = targetPath;
installPath = "${placeholder "out"}/share/${pname}";
installMode = "0644";
buildPhase = ''
runHook preBuild
haxe -main "$mainClass" -lib hase "-$target" "$targetPath" \
-dce full $hxArgs
runHook postBuild
'';
installPhase = ''
runHook preInstall
install -vD -m "$installMode" "$installBinary" \
"$installPath/$targetPath"
runHook postInstall
'';
} // lib.optionalAttrs (target == "cpp") {
targetPath = executableName;
installBinary = "${executableName}/${mainClass}";
installPath = "${placeholder "out"}/bin";
installMode = "0755";
};
finalAttrs = drvAttrs // removeAttrs attrs [ "buildInputs" ];
in final.stdenv.mkDerivation finalAttrs;
};
defaultPackage = forAllSystems (system: self.packages.${system}.hase);
checks = forAllSystems (system: {
unit = mkPackageWithSystem system true false;
});
devShell = forAllSystems (system: mkPackageWithSystem system false true);
hydraJobs = let
hydraSystems = [ "x86_64-linux" ];
forAllHydraSystems = lib.genAttrs hydraSystems;
in {
build = forAllHydraSystems (system: self.packages.${system}.hase);
tests = forAllHydraSystems (system: self.checks.${system});
examples = forAllHydraSystems (system: {
js = self.packages.${system}.example-js.overrideAttrs (drv: {
postInstall = (drv.postInstall or "") + ''
mkdir -p "$out/nix-support"
echo "file example $out/share/hase-example example.html" \
> "$out/nix-support/hydra-build-products"
'';
});
neko = self.packages.${system}.example-neko;
cpp = self.packages.${system}.example-cpp;
});
};
};
}