Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions .github/workflows/get_all_packages.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import json
import subprocess


def run(purpose, *args):
process = subprocess.Popen(
[*args],
Expand All @@ -16,6 +17,7 @@ def run(purpose, *args):
exit(-1)
return process.stdout


flake_meta = json.load(run("get flake metadata", "nix", "flake", "show", "--json"))
packages = flake_meta["packages"]
for platform, packages in packages.items():
Expand All @@ -24,10 +26,15 @@ def run(purpose, *args):
continue
tgt = f".#packages.{platform}.{package}"
outputs = []
drv = json.load(run(f"get derivation info for {package}", "nix", "derivation", "show", tgt))
drv = json.load(
run(f"get derivation info for {package}", "nix", "derivation", "show", tgt)
)
keys = list(drv.keys())
if len(keys) != 1:
print(f"'nix derivation show' unexpectedly returned {len(keys)} paths, expected exactly 1: {tgt}", file=sys.stderr)
print(
f"'nix derivation show' unexpectedly returned {len(keys)} paths, expected exactly 1: {tgt}",
file=sys.stderr,
)
exit(-1)
output_list = drv[keys[0]]["outputs"]
for output in output_list:
Expand Down
26 changes: 17 additions & 9 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@
doCheck = pkgs.system != "x86_64-darwin";
});

## ghdl
ghdl-llvm = pkgs.ghdl-llvm.override {gnat = pkgs'.gnat14;};
ghdl-bin = callPackage ./nix/ghdl-bin.nix {};

# Main
magic = callPackage ./nix/magic.nix {};
magic-vlsi = pkgs'.magic; # alias, there's a python package called magic
Expand All @@ -117,7 +121,12 @@
yosys-eqy = callPackage ./nix/yosys-eqy.nix {};
yosys-lighter = callPackage ./nix/yosys-lighter.nix {};
yosys-slang = callPackage ./nix/yosys-slang.nix {};
yosys-ghdl = callPackage ./nix/yosys-ghdl.nix {};
yosys-ghdl = callPackage ./nix/yosys-ghdl.nix {
ghdl =
if (lib.lists.any (el: el == pkgs'.system) pkgs'.ghdl-bin.meta.platforms)
then pkgs'.ghdl-bin
else pkgs'.ghdl-llvm;
};
})
(
self.composePythonOverlay (
Expand Down Expand Up @@ -150,14 +159,13 @@
pkgs = self.legacyPackages."${system}";
in
{
yosysFull = pkgs.yosys.withPlugins (with pkgs;
[
yosys-sby
yosys-eqy
yosys-lighter
yosys-slang
]
++ lib.optionals (lib.lists.any (el: el == system) yosys-ghdl.meta.platforms) [yosys-ghdl]);
yosysFull = pkgs.yosys.withPlugins (with pkgs; [
yosys-sby
yosys-eqy
yosys-lighter
yosys-slang
yosys-ghdl
]);
inherit (pkgs) magic magic-vlsi netgen klayout klayout-gdsfactory tclFull tk-x11 iverilog verilator xschem ngspice bitwuzla yosys yosys-sby yosys-eqy yosys-lighter yosys-slang;
inherit (pkgs.python3.pkgs) gdsfactory gdstk tclint;
}
Expand Down
88 changes: 88 additions & 0 deletions nix/ghdl-bin.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
{
lib,
system,
stdenv,
fetchurl,
autoPatchelfHook,
patchelfUnstable,
insert-dylib,
darwin,
zlib,
python3,
data ? (builtins.fromTOML (builtins.readFile ./ghdl-bin.toml)),
}: let
version = data.version;
system-data = data."${system}";
in
stdenv.mkDerivation (finalAttrs: {
pname = "ghdl-bin";
inherit version;

src = fetchurl {
url = "https://github.com/ghdl/ghdl/releases/download/v${finalAttrs.version}/ghdl-${system-data.backend}-${finalAttrs.version}-${system-data.platform_double}.tar.gz";
inherit (system-data) sha256;
};

buildInputs = [
zlib
];

nativeBuildInputs =
lib.optionals (!stdenv.hostPlatform.isDarwin) [
autoPatchelfHook
patchelfUnstable
]
++ lib.optionals (stdenv.hostPlatform.isDarwin) [
(python3.withPackages (ps: with ps; [lief click]))
darwin.autoSignDarwinBinariesHook
];

buildPhase = "true";

installPhase = ''
runHook preInstall
mkdir -p $out
cp -r bin $out/bin
cp -r include $out/include
cp -r lib $out/lib
runHook postInstall
'';

fixupPhase =
lib.optionalString (!stdenv.isDarwin) "exit -1" # TODO
+ lib.optionalString (stdenv.isDarwin) ''
runHook preFixup
install_name_tool \
-change /usr/lib/libc++.1.dylib ${stdenv.cc.libcxx}/lib/libc++.1.dylib \
-change /usr/lib/libz.1.dylib ${zlib}/lib/libz.1.dylib \
$out/bin/ghdl1-llvm
clang \
-x c\
-dynamiclib \
-D GHDL_PREFIX=\"$out/lib/ghdl\" \
-o $out/lib/set_ghdl_pfx.dylib \
-install_name $out/lib/set_ghdl_pfx.dylib \
- <<'EOF'
#include <stdlib.h>

__attribute__((constructor))
static void set_ghdl_pfx(void) {
if (!getenv("GHDL_PREFIX")) {
setenv("GHDL_PREFIX", GHDL_PREFIX, 1);
}
}
EOF
python3 ${./supporting/lief_inject_dylib.py} \
--inject $out/lib/set_ghdl_pfx.dylib \
--inplace $out/lib/libghdl-*.dylib
runHook postFixup
'';

meta = {
description = "VHDL 2008/93/87 simulator";
homepage = "https://github.com/ghdl/ghdl";
license = lib.licenses.gpl2Plus;
platforms = lib.lists.remove "version" (builtins.attrNames data);
broken = !stdenv.isDarwin;
};
})
11 changes: 11 additions & 0 deletions nix/ghdl-bin.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
version = "5.0.1"

[x86_64-darwin]
backend = "llvm"
platform_double = "macos13-x86_64"
sha256 = "sha256-F29YhXysqAOSFfnq08krXOBVigQ0FSlXyishnVuWZEM="

[aarch64-darwin]
backend = "llvm"
platform_double = "macos14-aarch64"
sha256 = "sha256-R+nLj50zBumolpcbKgrO8Td25bmNcXRaLmHVcy1eyWQ="
18 changes: 12 additions & 6 deletions nix/supporting/download_github_snapshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
# Adapted from OpenLane Build Scripts
#
# https://github.com/The-OpenROAD-Project/OpenLane/blob/3c41826a8aaaf724e423593ddc7bea392e65277e/docker/utils.py
#
#
# and Volare
#
#
#
#
# Copyright 2021 Efabless Corporation
#
Expand Down Expand Up @@ -41,7 +41,8 @@
file=sys.stderr,
)
exit(-1)



class GitHubSession(httpx.Client):
def __init__(
self,
Expand Down Expand Up @@ -88,10 +89,12 @@ def __init__(
@classmethod
def get_user_agent(Self) -> str:
return f"nix-eda"



github_client = GitHubSession()
download_cache = {}


def download_tarball(repo_url: str, commit: str) -> str:
repo_path = urllib.parse.urlsplit(repo_url).path.strip("/")
tarball_url = f"https://api.github.com/repos/{repo_path}/tarball/{commit}"
Expand Down Expand Up @@ -237,13 +240,16 @@ def main(repo_url, commit, out_dir, filter):
"""
Downloads a snapshot of a GitHub repo, i.e. with all GitHub-based submodules
recursively downloaded.

Currently does not work for repositories with private or externally hosted
submodules.
"""
global github_client
fetch_tarball_and_submodules(
repo_url=repo_url, commit=commit, base_path=out_dir, filter=filter,
repo_url=repo_url,
commit=commit,
base_path=out_dir,
filter=filter,
)


Expand Down
42 changes: 42 additions & 0 deletions nix/supporting/lief_inject_dylib.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/usr/bin/env python3
"""
Uses the lief library to inject a dylib
"""
import os
import lief
import click


@click.command()
@click.option("-o", "--dylib-out", type=click.Path(writable=True), required=False)
@click.option("-i", "--inplace", is_flag=True)
@click.option("--inject", type=click.Path(readable=True), required=True)
@click.argument("dylib_in")
@click.pass_context
def main(ctx, dylib_out, inplace, inject, dylib_in):
universal = lief.MachO.parse(dylib_in)
if universal is None:
ctx.exit(-1)

inject = os.path.abspath(inject)

if inplace:
dylib_out = dylib_in

arch_found = False
for arch in universal:
if arch_found:
ctx.fail("lief_inject_dylib.py doesn't support multi-arch binaries")
ctx.exit(-1)
arch_found = True
arch.add_library(os.path.abspath(inject))

if arch.has_code_signature:
arch.remove_signature()

print(f"Writing to '{dylib_out}'…")
universal.write(dylib_out)


if __name__ == "__main__":
main()
79 changes: 40 additions & 39 deletions nix/yosys-ghdl.nix
Original file line number Diff line number Diff line change
Expand Up @@ -38,53 +38,54 @@
yosys,
fetchFromGitHub,
python3,
ghdl-mcode,
ghdl-llvm,
ghdl,
pkg-config,
rev ? "c9b05e481423c55ffcbb856fd5296701f670808c",
rev-date ? "2022-01-11",
sha256 ? "sha256-tT2+DXUtbJIBzBUBcyG2sz+3G+dTkciLVIczcRPr0Jw=",
}: let
ghdl =
if yosys.stdenv.isLinux
then ghdl-mcode
else ghdl-llvm;
in
yosys.stdenv.mkDerivation {
pname = "yosys-ghdl";
version = rev-date;
}:
yosys.stdenv.mkDerivation {
pname = "yosys-ghdl";
version = rev-date;

dylibs = ["ghdl"];
dylibs = ["ghdl"];

src = fetchFromGitHub {
owner = "ghdl";
repo = "ghdl-yosys-plugin";
inherit rev;
inherit sha256;
};
src = fetchFromGitHub {
owner = "ghdl";
repo = "ghdl-yosys-plugin";
inherit rev;
inherit sha256;
};

buildInputs = [
yosys
python3
ghdl
];
buildInputs = [
yosys
python3
ghdl
];

nativeBuildInputs = [
pkg-config
];
nativeBuildInputs = [
pkg-config
];

doCheck = false;
installPhase = ''
runHook preInstall
mkdir -p $out/share/yosys/plugins
cp ghdl.so $out/share/yosys/plugins/ghdl.so
runHook postInstall
'';

installPhase = ''
mkdir -p $out/share/yosys/plugins
cp ghdl.so $out/share/yosys/plugins/ghdl.so
'';
doCheck = true;

meta = {
description = "VHDL synthesis (based on GHDL and Yosys)";
homepage = "http://ghdl.github.io/ghdl/using/Synthesis.html";
license = lib.licenses.gpl3Plus;
# inherit (ghdl.meta) platforms; # lists non-functioning platforms
platforms = ["x86_64-linux" "x86_64-darwin"];
};
}
checkPhase = ''
runHook preCheck
yosys -p "plugin -i $PWD/ghdl.so; ghdl testsuite/examples/dff/dff.vhdl -e dff; hierarchy"
runHook postcheck
'';

meta = {
description = "VHDL synthesis (based on GHDL and Yosys)";
homepage = "http://ghdl.github.io/ghdl/using/Synthesis.html";
license = lib.licenses.gpl3Plus;
inherit (ghdl.meta) platforms;
};
}