1{
2 stdenv,
3 lib,
4 bison,
5 boost,
6 fetchFromGitHub,
7 flex,
8 gtkwave,
9 libffi,
10 makeWrapper,
11 pkg-config,
12 python3,
13 readline,
14 symlinkJoin,
15 tcl,
16 iverilog,
17 zlib,
18 yosys,
19 yosys-bluespec,
20 yosys-ghdl,
21 yosys-symbiflow,
22 enablePython ? true, # enable python binding
23}:
24
25# NOTE: as of late 2020, yosys has switched to an automation robot that
26# automatically tags their repository Makefile with a new build number every
27# day when changes are committed. please MAKE SURE that the version number in
28# the 'version' field exactly matches the YOSYS_VER field in the Yosys
29# makefile!
30#
31# if a change in yosys isn't yet available under a build number like this (i.e.
32# it was very recently merged, within an hour), wait a few hours for the
33# automation robot to tag the new version, like so:
34#
35# https://github.com/YosysHQ/yosys/commit/71ca9a825309635511b64b3ec40e5e5e9b6ad49b
36#
37# note that while most nix packages for "unstable versions" use a date-based
38# version scheme, synchronizing the nix package version here with the unstable
39# yosys version number helps users report better bugs upstream, and is
40# ultimately less confusing than using dates.
41
42let
43
44 # Provides a wrapper for creating a yosys with the specified plugins preloaded
45 #
46 # Example:
47 #
48 # my_yosys = yosys.withPlugins (with yosys.allPlugins; [
49 # fasm
50 # bluespec
51 # ]);
52 withPlugins =
53 plugins:
54 let
55 paths = lib.closePropagation plugins;
56 module_flags =
57 with builtins;
58 concatStringsSep " " (map (n: "--add-flags -m --add-flags ${n.plugin}") plugins);
59 in
60 lib.appendToName "with-plugins" (symlinkJoin {
61 inherit (yosys) name;
62 paths = paths ++ [ yosys ];
63 nativeBuildInputs = [ makeWrapper ];
64 postBuild = ''
65 wrapProgram $out/bin/yosys \
66 --set NIX_YOSYS_PLUGIN_DIRS $out/share/yosys/plugins \
67 ${module_flags}
68 '';
69 });
70
71 allPlugins = {
72 bluespec = yosys-bluespec;
73 ghdl = yosys-ghdl;
74 } // (yosys-symbiflow);
75
76 boost_python = boost.override {
77 enablePython = true;
78 python = python3;
79 };
80
81in
82stdenv.mkDerivation (finalAttrs: {
83 pname = "yosys";
84 version = "0.51";
85
86 src = fetchFromGitHub {
87 owner = "YosysHQ";
88 repo = "yosys";
89 tag = "v${finalAttrs.version}";
90 hash = "sha256-Y2Gf3CXd1em+4dlIo2+dwfZbqahM3kqG0rZUTjkIZak=";
91 fetchSubmodules = true;
92 leaveDotGit = true;
93 postFetch = ''
94 # set up git hashes as if we used the tarball
95
96 pushd $out
97 git rev-parse HEAD > .gitcommit
98 cd $out/abc
99 git rev-parse HEAD > .gitcommit
100 popd
101
102 # remove .git now that we are through with it
103 find "$out" -name .git -print0 | xargs -0 rm -rf
104 '';
105 };
106
107 enableParallelBuilding = true;
108 nativeBuildInputs = [
109 pkg-config
110 bison
111 flex
112 ];
113 propagatedBuildInputs = [
114 tcl
115 readline
116 libffi
117 zlib
118 (python3.withPackages (
119 pp: with pp; [
120 click
121 ]
122 ))
123 ] ++ lib.optional enablePython boost_python;
124
125 makeFlags = [ "PREFIX=${placeholder "out"}" ];
126
127 patches = [
128 ./plugin-search-dirs.patch
129 ./fix-clang-build.patch # see https://github.com/YosysHQ/yosys/issues/2011
130 ];
131
132 postPatch = ''
133 substituteInPlace ./Makefile \
134 --replace-fail 'echo UNKNOWN' 'echo ${builtins.substring 0 10 finalAttrs.src.rev}'
135
136 patchShebangs tests ./misc/yosys-config.in
137 '';
138
139 preBuild =
140 ''
141 chmod -R u+w .
142 make config-${if stdenv.cc.isClang or false then "clang" else "gcc"}
143
144 if ! grep -q "YOSYS_VER := $version" Makefile; then
145 echo "ERROR: yosys version in Makefile isn't equivalent to version of the nix package (allegedly ${finalAttrs.version}), failing."
146 exit 1
147 fi
148 ''
149 + lib.optionalString enablePython ''
150 echo "ENABLE_PYOSYS := 1" >> Makefile.conf
151 echo "PYTHON_DESTDIR := $out/${python3.sitePackages}" >> Makefile.conf
152 echo "BOOST_PYTHON_LIB := -lboost_python${lib.versions.major python3.version}${lib.versions.minor python3.version}" >> Makefile.conf
153 '';
154
155 preCheck = ''
156 # autotest.sh automatically compiles a utility during startup if it's out of date.
157 # having N check jobs race to do that creates spurious codesigning failures on macOS.
158 # run it once without asking it to do anything so that compilation is done before the jobs start.
159 tests/tools/autotest.sh
160 '';
161
162 checkTarget = "test";
163 doCheck = true;
164 nativeCheckInputs = [
165 gtkwave
166 iverilog
167 ];
168
169 setupHook = ./setup-hook.sh;
170
171 passthru = {
172 inherit withPlugins allPlugins;
173 };
174
175 meta = with lib; {
176 description = "Open RTL synthesis framework and tools";
177 homepage = "https://yosyshq.net/yosys/";
178 license = licenses.isc;
179 platforms = platforms.all;
180 maintainers = with maintainers; [
181 shell
182 thoughtpolice
183 Luflosi
184 ];
185 };
186})