lol
1{
2 lib,
3 stdenv,
4 fetchFromGitHub,
5 fetchurl,
6 autoconf,
7 automake,
8 fontconfig,
9 libX11,
10 perl,
11 flex,
12 bison,
13 pkg-config,
14 tcl,
15 tk,
16 xorg,
17 yices, # bsc uses a patched version of yices
18 zlib,
19 ghc,
20 gmp-static,
21 iverilog,
22 asciidoctor,
23 texliveFull,
24 which,
25 makeBinaryWrapper,
26 cctools,
27 targetPackages,
28 # install -m 644 lib/libstp.dylib /private/tmp/nix-build-bluespec-2024.07.drv-5/source/inst/lib/SAT
29 # install: cannot stat 'lib/libstp.dylib': No such file or directory
30 # https://github.com/B-Lang-org/bsc/pull/600 might fix it
31 stubStp ? !stdenv.hostPlatform.isDarwin,
32 withDocs ? true,
33 # With 23 core parallel 10 mins on r9 5900x
34 # Broken on darwin currently
35 withSuiteCheck ? false,
36 gnugrep,
37 time,
38 dejagnu,
39 systemc,
40 glibcLocales,
41 buildPackages,
42}:
43
44let
45 ghcWithPackages = ghc.withPackages (
46 g:
47 (with g; [
48 old-time
49 regex-compat
50 syb
51 split
52 ])
53 );
54
55in
56stdenv.mkDerivation rec {
57 pname = "bluespec";
58 version = "2024.07";
59
60 src = fetchFromGitHub {
61 owner = "B-Lang-org";
62 repo = "bsc";
63 tag = version;
64 sha256 = "sha256-gA/vfAkkM2cuArN99JZVYEWTIJqg82HlC+BHNVS5Ot0=";
65 };
66
67 yices-src = fetchurl {
68 url = "https://github.com/B-Lang-org/bsc/releases/download/${version}/yices-src-for-bsc-${version}.tar.gz";
69 sha256 = "sha256-pyEdCJvmgwOYPMZEtw7aro76tSn/Y/2GcKTyARmIh4E=";
70 };
71
72 enableParallelBuilding = true;
73
74 outputs = [
75 "out"
76 ]
77 ++ lib.optionals withDocs [
78 "doc"
79 ];
80
81 # https://github.com/B-Lang-org/bsc/pull/278 is still applicable, but will probably not be applied as such
82 # there is work ongoing: https://github.com/B-Lang-org/bsc/issues/595 https://github.com/B-Lang-org/bsc/pull/600
83 patches = [ ./libstp_stub_makefile.patch ];
84
85 postUnpack = ''
86 tar -C $sourceRoot/ -xf ${yices-src}
87 chmod -R +rwX $sourceRoot/src/vendor/yices/v2.6/yices2
88 '';
89
90 postPatch = ''
91 patchShebangs \
92 src/vendor/stp/src/AST/genkinds.pl \
93 src/Verilog/copy_module.pl \
94 src/comp/update-build-version.sh \
95 src/comp/update-build-system.sh \
96 src/comp/wrapper.sh
97
98 substituteInPlace src/comp/Makefile \
99 --replace-fail 'install-bsc install-bluetcl' 'install-bsc install-bluetcl $(UTILEXES) $(SHOWRULESEXES) install-utils install-showrules'
100
101 # For darwin
102 # ld: library not found for -ltcl8.5
103 substituteInPlace ./platform.sh \
104 --replace-fail 'TCLSH=/usr/bin/tclsh' 'TCLSH=`which tclsh`'
105 ''
106 + lib.optionalString withSuiteCheck ''
107 substituteInPlace testsuite/bsc.options/verilog-e/verilog-e.exp \
108 --replace-fail "/bin/echo" "${lib.getExe' buildPackages.coreutils "echo"}"
109
110 substituteInPlace testsuite/test_list.sh testsuite/findfailures.csh \
111 --replace-fail "bin/csh" "${lib.getExe buildPackages.tcsh}"
112
113 patchShebangs \
114 testsuite/test_list.sh \
115 testsuite/findfailures.csh \
116 scripts/tool-find.sh \
117 testsuite/bsc.bluetcl/packages/expandPorts/compareOutput.pl \
118 testsuite/bsc.bsv_examples/AES/funcit.pl \
119 testsuite/bsc.bsv_examples/AES/makeVecs.pl \
120 testsuite/bsc.bsv_examples/AES/makeVecs192.pl \
121 testsuite/bsc.bsv_examples/AES/makeVecs256.pl \
122 testsuite/bsc.if/split/canonicalize.pl \
123 testsuite/bsc.interra/operators/Arith/generate/gen.pl \
124 testsuite/bsc.interra/operators/Arith/generate/sort.pl \
125 testsuite/bsc.interra/operators/BitSel/generate/gen.pl \
126 testsuite/bsc.interra/operators/BitSel/generate/sort.pl \
127 testsuite/bsc.interra/operators/Logic/generate/gen.pl \
128 testsuite/bsc.interra/operators/Logic/generate/sort.pl \
129 testsuite/bsc.preprocessor/ifdef/iftestcase-perl.pl \
130 testsuite/bsc.verilog/filter/basicinout.pl \
131 testsuite/scripts/collapse.pl \
132 testsuite/scripts/double-directory.pl \
133 testsuite/scripts/process-summary-file.pl \
134 testsuite/scripts/sort-by-time.pl \
135 testsuite/scripts/times-by-directory.pl
136 '';
137
138 preBuild = ''
139 # allow running bsc to bootstrap
140 export LD_LIBRARY_PATH=$PWD/inst/lib/SAT
141
142 # use more cores for GHC building, 44 causes heap overflows in ghc, and
143 # there is not much speedup after 8..
144 if [[ $NIX_BUILD_CORES -gt 8 ]] ; then export GHCJOBS=8; else export GHCJOBS=$NIX_BUILD_CORES; fi
145 '';
146
147 buildInputs = yices.buildInputs ++ [
148 fontconfig
149 libX11 # tcltk
150 tcl
151 tk
152 which
153 xorg.libXft
154 zlib
155 ];
156
157 nativeBuildInputs = [
158 automake
159 autoconf
160 bison
161 flex
162 ghcWithPackages
163 perl
164 pkg-config
165 tcl
166 makeBinaryWrapper
167 ]
168 ++ lib.optionals withDocs [
169 texliveFull
170 asciidoctor
171 ]
172 ++ lib.optionals stdenv.hostPlatform.isDarwin [
173 # https://github.com/B-Lang-org/bsc/blob/main/src/comp/bsc.hs#L1838
174 # /nix/store/7y0vlsf6l8lr3vjsbrirqrsbx4mwqiwf-cctools-binutils-darwin-1010.6/bin/strip: error: unknown argument '-u'
175 # make[1]: *** [Makefile:97: smoke_test_bluesim] Error 1
176 cctools
177 ];
178
179 env.NIX_CFLAGS_COMPILE = toString (
180 lib.optionals (stdenv.cc.isClang) [
181 # wide_data.cxx:1750:15: error: variable length arrays in C++ are a Clang extension [-Werror,-Wvla-cxx-extension]
182 "-Wno-error"
183 ]
184 );
185
186 makeFlags = [
187 "NO_DEPS_CHECKS=1" # skip the subrepo check (this deriviation uses yices-src instead of the subrepo)
188 "NOGIT=1" # https://github.com/B-Lang-org/bsc/issues/12
189 "LDCONFIG=ldconfig" # https://github.com/SRI-CSL/yices2/blob/fda0a325ea7923f152ea9f9a5d20eddfd1d96224/Makefile.build#L66
190 (if withDocs then "release" else "install-src")
191 ]
192 ++ lib.optionals stubStp [
193 "STP_STUB=1" # uses yices as a SMT solver and stub out STP
194 ];
195
196 installPhase = ''
197 mkdir -p $out
198 mv inst/bin $out
199 mv inst/lib $out
200
201 ''
202 + lib.optionalString withDocs ''
203 # fragile, I know..
204 mkdir -p $doc/share/doc/bsc
205 mv inst/README $doc/share/doc/bsc
206 mv inst/ReleaseNotes.* $doc/share/doc/bsc
207 mv inst/doc/*.pdf $doc/share/doc/bsc
208 '';
209
210 postFixup = ''
211 # https://github.com/B-Lang-org/bsc/blob/65e3a87a17f6b9cf38cbb7b6ad7a4473f025c098/src/comp/bsc.hs#L1839
212 # `/bin/bsc` is a bash script which the script name to call the binary in the `/bin/core` directory
213 # thus wrapping `/bin/bsc` messes up the scriptname detection in it.
214 wrapProgram $out/bin/core/bsc \
215 --prefix PATH : ${
216 lib.makeBinPath (if stdenv.hostPlatform.isDarwin then [ cctools ] else [ targetPackages.stdenv.cc ])
217 }
218 '';
219
220 doCheck = true;
221 doInstallCheck = true;
222
223 # TODO To fix check-suite:
224 # On darwin
225 # ```
226 # FAIL: `Cpreprocess_line.bsv.bsc-out' differs from `Cpreprocess_line.bsv.bsc-out.expected'
227
228 # FAIL: `sysGCD.bsc-vcomp-out.filtered' differs from `empty.expected'
229
230 # FAIL: module `' in `ImpArgConnect3.bsv' should compile to Verilog
231 # Caught error in sed: sed: can't read mkArgImpConnect3.v: No such file or directory
232 # FAIL: `mkArgImpConnect3.v.filtered' differs from `mkArgImpConnect3.v.expected.filtered'
233 # ```
234
235 checkTarget = if withSuiteCheck then "checkparallel" else "check-smoke"; # this is the shortest check but "check-suite" tests much more
236
237 # bash: warning: setlocale: LC_ALL: cannot change locale (en_US.UTF-8)
238 LOCALE_ARCHIVE = lib.optionalString (
239 withSuiteCheck && stdenv.hostPlatform.isLinux
240 ) "${glibcLocales}/lib/locale/locale-archive";
241
242 nativeCheckInputs = [
243 gmp-static
244 iverilog
245 ]
246 ++ lib.optionals withSuiteCheck [
247 time
248 dejagnu # for `/bin/runtest` in `check-suite`
249 gnugrep # `testsuite/bsc.interra/operators/Arith/arith.exp` and more
250 ];
251
252 checkInputs = lib.optionals withSuiteCheck [
253 systemc
254 ];
255
256 checkPhase = lib.optionalString withSuiteCheck ''
257 (
258 cd testsuite
259 set +e # disable exit on error
260 make -j $NIX_BUILD_CORES $checkTarget
261 test_exit_code=$?
262 set -e
263 failures=$(./findfailures.csh)
264 echo "$failures"
265 for failure in $failures; do
266 logpath="''${failure/%sum/log}"
267 echo "\nFAILURE LOG: $logpath"
268 cat "$logpath"
269 echo "END LOG: $logpath"
270 done
271
272 if [[ "$test_exit_code" != "0" || -n "$failures" ]]; then
273 echo "Some tests failed or the makefile failed to run"
274 exit 1
275 fi
276 )
277 '';
278
279 installCheckPhase = ''
280 output="$($out/bin/bsc 2>&1 || true)"
281 echo "bsc output:"
282 echo "$output"
283 echo "$output" | grep -q "to get help"
284 '';
285
286 meta = {
287 description = "Toolchain for the Bluespec Hardware Definition Language";
288 homepage = "https://github.com/B-Lang-org/bsc";
289 license = lib.licenses.bsd3;
290 platforms = [
291 "aarch64-linux"
292 "x86_64-linux"
293 ]
294 ++ lib.platforms.darwin;
295 mainProgram = "bsc";
296 maintainers = with lib.maintainers; [
297 jcumming
298 thoughtpolice
299 ];
300 };
301}