-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflake.nix
More file actions
184 lines (168 loc) · 6.87 KB
/
flake.nix
File metadata and controls
184 lines (168 loc) · 6.87 KB
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
184
{
description = "Lua 5.1 for the MC6809";
inputs = {
gcc6809.url = "github:blark/gcc6809-nix";
nixpkgs.follows = "gcc6809/nixpkgs";
nixpkgs-unstable.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
};
outputs = { self, nixpkgs, gcc6809, nixpkgs-unstable, ... }:
let
systems = [ "x86_64-linux" "aarch64-darwin" ];
forAllSystems = nixpkgs.lib.genAttrs systems;
# Shared derivations per system
perSystem = system:
let
pkgs = import nixpkgs { inherit system; };
pkgsUnstable = import nixpkgs-unstable { inherit system; };
toolchain = gcc6809.packages.${system}.default;
luaOriginal = pkgs.fetchzip {
url = "https://www.lua.org/ftp/lua-5.1.5.tar.gz";
sha256 = "sha256-QnrgndxTSEgkFPXiWQhkD4aVddWpLaVZIe/ya21WAeQ=";
};
luaSrc = pkgs.applyPatches {
src = luaOriginal;
patches = [ ./patches/6809-phase1.patch ];
};
luac-int32 = pkgs.stdenv.mkDerivation {
pname = "luac-int32";
version = "5.1.5";
src = luaOriginal;
patches = [ ./patches/luac-int32.patch ];
buildPhase = "make -C src luac CC=cc MYCFLAGS='-O2'";
installPhase = "mkdir -p $out/bin && cp src/luac $out/bin/luac-int32";
meta = {
description = "Lua compiler patched for 32-bit integers";
license = pkgs.lib.licenses.mit;
platforms = pkgs.lib.platforms.unix;
};
};
lua6809-vm = pkgs.stdenv.mkDerivation {
pname = "lua6809-vm";
version = "5.1.5";
src = luaSrc;
nativeBuildInputs = [ toolchain pkgs.gnumake ];
postUnpack = "cp $sourceRoot/Makefile.6809 $sourceRoot/src/Makefile";
buildFlags = [ "-C" "src" ];
preBuild = ''
export CC=m6809-unknown-none-gcc
export CFLAGS="-DLUA_USE_6809 -DLUA_CORE -I. -I${toolchain}/m6809-unknown-none/include -Os"
export LDFLAGS="-L${toolchain}/m6809-unknown-none/lib -L${toolchain}/lib/gcc/m6809-unknown-none/4.3.6"
'';
installPhase = "mkdir -p $out && cp src/lua.s19 $out/";
meta = {
description = "Lua 5.1 VM for MC6809 processor";
license = pkgs.lib.licenses.mit;
platforms = [ "x86_64-linux" "aarch64-darwin" ];
};
};
luac6809 = pkgs.writeShellScriptBin "luac6809" ''
set -e
OUT="" INPUT=""
[ "$1" = "-o" ] && { OUT="$2"; shift 2; }
INPUT="$1"
[ -z "$OUT" ] && OUT="''${INPUT%.lua}.luac"
TMPFILE=$(mktemp)
trap "rm -f $TMPFILE" EXIT
${luac-int32}/bin/luac-int32 -o "$TMPFILE" "$INPUT"
${pkgs.python3}/bin/python3 ${./tools/luac_convert.py} "$TMPFILE" "$OUT"
'';
regen-patch = pkgs.writeShellScriptBin "regen-patch" ''
set -e
cd "$(git rev-parse --show-toplevel 2>/dev/null || pwd)"
[ ! -d lua-work/src ] && { echo "Error: lua-work/src not found"; exit 1; }
echo "Creating patch from lua-work/..."
diff -ruN "${luaOriginal}/src" lua-work/src \
| sed "s|${luaOriginal}/src|a/src|g; s|lua-work/src|b/src|g" \
> patches/6809-phase1.patch
echo "" >> patches/6809-phase1.patch
diff -ruN /dev/null lua-work/Makefile.6809 \
| sed 's|/dev/null|a/Makefile.6809|; s|lua-work/Makefile.6809|b/Makefile.6809|' \
>> patches/6809-phase1.patch
echo "Done. Run 'direnv reload' to rebuild."
'';
mc6809 = pkgsUnstable.python3Packages.buildPythonPackage rec {
pname = "MC6809";
version = "0.6.0";
src = pkgsUnstable.fetchPypi {
inherit pname version;
sha256 = "sha256-Q5DNA+RmMmSR2I33WLIsVWyZJpknWBK820dL41Bgd74=";
};
pyproject = true;
build-system = [ pkgsUnstable.python3Packages.poetry-core ];
dependencies = [ pkgsUnstable.python3Packages.click ];
postPatch = ''
substituteInPlace pyproject.toml \
--replace-fail 'poetry.masonry.api' 'poetry.core.masonry.api' \
--replace-fail 'poetry>=0.12' 'poetry-core>=1.0.0'
'';
doCheck = false;
dontCheckRuntimeDeps = true;
meta = {
description = "MC6809 CPU emulator written in Python";
license = pkgsUnstable.lib.licenses.gpl3;
platforms = pkgsUnstable.lib.platforms.unix;
};
};
pythonEnv = pkgsUnstable.python3.withPackages (_: [ mc6809 ]);
in {
inherit pkgs pkgsUnstable luaOriginal luaSrc luac-int32 lua6809-vm
luac6809 regen-patch mc6809 pythonEnv toolchain;
};
# Cache perSystem results to avoid redundant evaluation
cached = forAllSystems perSystem;
in {
packages = forAllSystems (system:
let s = cached.${system}; in {
default = s.lua6809-vm;
lua-original = s.luaOriginal;
vm = s.lua6809-vm;
luac = s.luac6809;
});
apps = forAllSystems (system:
let
s = cached.${system};
testScript = s.pkgs.writeShellScriptBin "lua6809-test" ''
export LUA6809_S19="${s.lua6809-vm}/lua.s19"
export PYTHONPATH="${s.pythonEnv}/${s.pythonEnv.sitePackages}:$PYTHONPATH"
export PATH="${s.luac6809}/bin:$PATH"
cd ${./.}
${s.pythonEnv}/bin/python3 ./run_tests.py "$@"
'';
in {
test = {
type = "app";
program = "${testScript}/bin/lua6809-test";
meta.description = "Run Lua 6809 test suite";
};
});
devShells = forAllSystems (system:
let s = cached.${system}; in {
default = s.pkgs.mkShell {
packages = [
s.toolchain
s.pkgs.gnumake
s.luac-int32
s.luac6809
s.regen-patch
s.lua6809-vm
s.pkgsUnstable.uv
s.pkgsUnstable.python3
s.pkgs.srecord
];
LUA_ORIGINAL_DIR = "${s.luaOriginal}";
LUA_SRC_DIR = "${s.luaSrc}";
LUA6809_S19 = "${s.lua6809-vm}/lua.s19";
shellHook = ''
echo "Lua 6809 Development Environment"
echo ""
echo "Commands:"
echo " luac6809 script.lua Compile Lua to 6809 bytecode"
echo " regen-patch Regenerate patch from lua-work/"
echo " nix run .#test Run test suite"
echo ""
echo "Workflow: Edit lua-work/ -> regen-patch -> direnv reload"
'';
};
});
};
}