Skip to content
Closed
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
19 changes: 18 additions & 1 deletion BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,6 +1,23 @@
load("@bazel-orfs//:yosys_pkg.bzl", "yosys_prefix_tar")
load("@rules_shell//shell:sh_binary.bzl", "sh_binary")

sh_binary(
name = "install_for_bazel",
srcs = ["bazel/install.sh"],
# bazel/ is its own package (bazel/BUILD), so a cross-package label
# is required here; bazel/install.sh silently broke every root-package
# target once bazel/BUILD appeared.
srcs = ["//bazel:install.sh"],
)

# Relocatable PREFIX tarball of the hermetic bazel-built yosys
# (bin/yosys, bin/yosys-abc, share/yosys/..., share/yosys/plugins/slang.so).
# This is the single source of truth for the yosys that
# etc/DependencyInstaller.sh and bazel/install.sh install; the version is
# the @yosys pin in bazel-orfs MODULE.bazel (bump via
# `bazelisk run @bazel-orfs//:bump`).
yosys_prefix_tar(
name = "yosys_tar",
# Same label as orfs.default(yosys_plugins = ...) in MODULE.bazel, so
# SYNTH_HDL_FRONTEND=slang works identically in the classic flow.
plugins = ["@yosys-slang//src/yosys_plugin:slang.so"],
)
35 changes: 35 additions & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ bazel_dep(name = "rules_shell", version = "0.6.1")
# our MODULE.bazel doesn't need to be patched at non-root consumption
# time.

bazel_dep(name = "rules_pycross", version = "0.8.1", dev_dependency = True)
bazel_dep(name = "toolchains_llvm", version = "1.5.0", dev_dependency = True)
bazel_dep(name = "openroad", dev_dependency = True)
local_path_override(
Expand All @@ -41,9 +42,18 @@ BAZEL_ORFS_COMMIT = "6ebadeb4be5c9ada103081c9a5e668c014126616"
BAZEL_ORFS_REMOTE = "https://github.com/The-OpenROAD-Project/bazel-orfs.git"

# To bump version, run: bazelisk run @bazel-orfs//:bump
#
# `patches =` keeps small bazel-orfs changes vendored in this repo while
# they make their way upstream, instead of round-tripping every change
# through a bazel-orfs PR + pin bump. When a patch lands upstream, drop
# the entry here and bump BAZEL_ORFS_COMMIT.
git_override(
module_name = "bazel-orfs",
commit = BAZEL_ORFS_COMMIT,
patch_strip = 1,
patches = [
"//patches/bazel-orfs:0001-add-yosys-prefix-tar-packaging.patch",
],
remote = BAZEL_ORFS_REMOTE,
)

Expand All @@ -66,6 +76,17 @@ git_override(
remote = "https://github.com/povik/yosys-slang.git",
)

# The sed resolved via @yosys -> abc -> readline -> ncurses ships a
# gnulib whose memchr replacement does not compile against glibc 2.43
# (its function-like memchr macro mangles the definition). Pin the
# newest sed and carry small gnulib fixes until they land upstream.
single_version_override(
module_name = "sed",
patch_strip = 1,
patches = ["//patches/bcr:sed-4.10-glibc-2.43.patch"],
version = "4.10",
)

# --- Extensions ---

llvm = use_extension(
Expand All @@ -89,6 +110,20 @@ python.toolchain(
python_version = "3.13",
)

# sv-lang (via @openroad) pulls in rules_pycross, whose toolchain
# computation fails on Python 3.8 (registered transitively via
# or-tools -> pybind11_abseil, dropped from rules_python 2.0.0's
# MINOR_MAPPING). pycross.configure_* tags are root-honored only, so the
# workaround from tools/OpenROAD/MODULE.bazel must be repeated here.
# Without it any target that needs python toolchain resolution from this
# root (e.g. rules_pkg's pkg_tar) fails.
pycross = use_extension(
"@rules_pycross//pycross/extensions:pycross.bzl",
"pycross",
dev_dependency = True,
)
pycross.configure_environments(python_versions = ["3.13"])

pip = use_extension("@rules_python//python/extensions:pip.bzl", "pip")
pip.parse(
hub_name = "orfs-pip",
Expand Down
2 changes: 1 addition & 1 deletion bazel/BUILD
Original file line number Diff line number Diff line change
@@ -1 +1 @@
exports_files(glob(["*.patch"]))
exports_files(glob(["*.patch"]) + ["install.sh"])
102 changes: 19 additions & 83 deletions bazel/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,15 @@
set -e

# ORFS developer install script
# Builds and installs OpenROAD, Yosys, and yosys-slang to tools/install/
# where flow/Makefile expects them.
# Builds and installs OpenROAD, Yosys (with the yosys-slang plugin) to
# tools/install/ where flow/Makefile expects them.
#
# Uses stamp files for fast no-op re-runs (seconds when nothing changed).
# Yosys comes from the bazel-built //:yosys_tar tarball (hermetic, cached
# by bazel), so re-runs are fast and no host toolchain beyond bazelisk is
# needed.

WORKSPACE="${BUILD_WORKSPACE_DIRECTORY:-.}"
INSTALL_DIR="${WORKSPACE}/tools/install"
NUM_THREADS=$(nproc)

# --- Check system dependencies for yosys/slang builds ---
check_deps() {
local missing_cmds=()

for cmd in bison flex gawk g++ pkg-config tclsh git cmake; do
if ! command -v "$cmd" &>/dev/null; then
missing_cmds+=("$cmd")
fi
done

if [[ ${#missing_cmds[@]} -eq 0 ]]; then
return
fi

echo "ERROR: Missing commands: ${missing_cmds[*]}"
echo ""

# Platform-specific install hint
if [[ "$(uname -s)" == "Darwin" ]]; then
echo " brew install bison flex gawk cmake pkg-config tcl-tk"
elif command -v apt-get &>/dev/null; then
echo " sudo apt-get install bison flex gawk g++ pkg-config tcl cmake git"
elif command -v dnf &>/dev/null; then
echo " sudo dnf install bison flex gawk gcc-c++ pkgconf tcl cmake git"
elif command -v yum &>/dev/null; then
echo " sudo yum install bison flex gawk gcc-c++ pkgconf tcl cmake git"
elif command -v zypper &>/dev/null; then
echo " sudo zypper install bison flex gawk gcc-c++ pkg-config tcl cmake git"
fi
exit 1
}

check_deps

BUILD_OPENROAD=1

Expand All @@ -54,7 +21,6 @@ Usage: bazelisk run //:install_for_bazel [-- OPTIONS]
Options:
--help, -h Show this help
--skip-openroad Skip OpenROAD build
--threads N Compilation threads (default: nproc)
EOF
exit 0
}
Expand All @@ -67,10 +33,6 @@ while [[ $# -gt 0 ]]; do
--skip-openroad)
BUILD_OPENROAD=0
;;
--threads)
NUM_THREADS="$2"
shift
;;
*)
echo "Unknown option: $1"
usage
Expand All @@ -80,7 +42,7 @@ while [[ $# -gt 0 ]]; do
done

# --- Check submodules are initialized ---
for sub in tools/OpenROAD tools/yosys tools/yosys-slang; do
for sub in tools/OpenROAD; do
if [[ ! -d "${WORKSPACE}/${sub}" ]] || [[ -z "$(ls -A "${WORKSPACE}/${sub}" 2>/dev/null)" ]]; then
echo "ERROR: ${sub} not initialized."
echo "Run: git submodule update --init --recursive"
Expand All @@ -94,47 +56,21 @@ if [[ $BUILD_OPENROAD -eq 1 ]]; then
(cd "${WORKSPACE}/tools/OpenROAD" && bazelisk run --//:platform=gui //packaging:install)
fi

# --- Yosys ---
# Uses stamp file for fast no-op: if the yosys submodule commit hasn't
# changed, skip the build entirely.
# --- Yosys (bazel-packaged tarball, includes yosys-abc and slang.so) ---
# //:yosys_tar is the bazel-built relocatable PREFIX tarball; bazel
# caches the build so re-runs are fast no-ops.
YOSYS_INSTALL="${INSTALL_DIR}/yosys"
YOSYS_STAMP="${YOSYS_INSTALL}/.yosys_commit"
YOSYS_COMMIT="$(git -C "${WORKSPACE}/tools/yosys" rev-parse HEAD)"

if [[ -f "${YOSYS_STAMP}" ]] && [[ "$(cat "${YOSYS_STAMP}")" == "${YOSYS_COMMIT}" ]]; then
echo "=== Yosys already up to date (${YOSYS_COMMIT:0:12}) ==="
else
echo "=== Building Yosys ==="
(
cd "${WORKSPACE}/tools/yosys"
make -j "${NUM_THREADS}" PREFIX="${YOSYS_INSTALL}" ABC_ARCHFLAGS=-Wno-register
make install PREFIX="${YOSYS_INSTALL}"
)
echo "${YOSYS_COMMIT}" > "${YOSYS_STAMP}"
echo "Yosys installed to ${YOSYS_INSTALL}/bin/yosys"
fi

# --- yosys-slang ---
SLANG_STAMP="${YOSYS_INSTALL}/.slang_commit"
SLANG_COMMIT="$(git -C "${WORKSPACE}/tools/yosys-slang" rev-parse HEAD)"

if [[ -f "${SLANG_STAMP}" ]] && [[ "$(cat "${SLANG_STAMP}")" == "${SLANG_COMMIT}" ]]; then
echo "=== yosys-slang already up to date (${SLANG_COMMIT:0:12}) ==="
else
echo "=== Building yosys-slang ==="
(
cd "${WORKSPACE}/tools/yosys-slang"
cmake -S . -B build \
-DYOSYS_CONFIG="${YOSYS_INSTALL}/bin/yosys-config" \
-DCMAKE_BUILD_TYPE=Release \
-DYOSYS_SLANG_REVISION=unknown \
-DSLANG_REVISION=unknown
cmake --build build -j "${NUM_THREADS}"
cmake --install build --prefix "${YOSYS_INSTALL}"
)
echo "${SLANG_COMMIT}" > "${SLANG_STAMP}"
echo "yosys-slang installed to ${YOSYS_INSTALL}/share/yosys/plugins/"
fi
echo "=== Installing Yosys (bazel //:yosys_tar) ==="
(
cd "${WORKSPACE}"
bazelisk build //:yosys_tar
TARBALL="$(bazelisk info execution_root)/$(bazelisk cquery --output=files //:yosys_tar 2>/dev/null)"
rm -rf "${YOSYS_INSTALL}"
mkdir -p "${YOSYS_INSTALL}"
tar -xzf "${TARBALL}" -C "${YOSYS_INSTALL}"
Comment on lines +67 to +71

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

If bazelisk cquery fails or returns an empty/invalid path, TARBALL will resolve to an invalid path (e.g., just the execution root directory). The script will then proceed to delete the existing YOSYS_INSTALL directory and fail with a cryptic error from tar. Adding defensive checks to verify that bazelisk is available, the query succeeds, and the tarball exists before deleting the old installation prevents this failure mode.

Suggested change
bazelisk build //:yosys_tar
TARBALL="$(bazelisk info execution_root)/$(bazelisk cquery --output=files //:yosys_tar 2>/dev/null)"
rm -rf "${YOSYS_INSTALL}"
mkdir -p "${YOSYS_INSTALL}"
tar -xzf "${TARBALL}" -C "${YOSYS_INSTALL}"
if ! command -v bazelisk &> /dev/null; then
echo "ERROR: bazelisk not found in PATH" >&2
exit 1
fi
bazelisk build //:yosys_tar
TARBALL_RELATIVE="$(bazelisk cquery --output=files //:yosys_tar 2>/dev/null)"
if [[ -z "${TARBALL_RELATIVE}" ]]; then
echo "ERROR: Failed to resolve yosys_tar path via bazelisk cquery" >&2
exit 1
fi
TARBALL="$(bazelisk info execution_root)/${TARBALL_RELATIVE}"
if [[ ! -f "${TARBALL}" ]]; then
echo "ERROR: Yosys tarball not found at ${TARBALL}" >&2
exit 1
fi
rm -rf "${YOSYS_INSTALL}"
mkdir -p "${YOSYS_INSTALL}"
tar -xzf "${TARBALL}" -C "${YOSYS_INSTALL}"

)
echo "Yosys installed to ${YOSYS_INSTALL}/bin/yosys"

echo ""
echo "=== Done ==="
Expand Down
44 changes: 33 additions & 11 deletions etc/DependencyInstaller.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,40 @@ _versionCompare() {
}

_installORDependencies() {
if [[ ${YOSYS_VER} == "" ]]; then
YOSYS_VER=v$(grep 'yosys_ver =' tools/yosys/docs/source/conf.py | awk -F'"' '{print $2}')
# Bootstrap bazelisk; the bazel-packaged yosys below needs it and it
# is idempotent (-bazel alone is a valid OpenROAD installer call).
./tools/OpenROAD/etc/DependencyInstaller.sh -bazel ${PREFIX:+-prefix=${PREFIX}}

# Install the bazel-packaged yosys. Single source of truth: the
# @yosys pin in bazel-orfs MODULE.bazel (bump via
# `bazelisk run @bazel-orfs//:bump`). Bazel caches the build, so
# re-runs are fast: no git clone, no make, and the same hermetic
# yosys on every OS.
_installYosysBazel

# Delegate the rest. Pass the version of the yosys installed above
# so the OpenROAD installer's _install_yosys version check succeeds
# and its git-clone-and-make build is a no-op; -eqy still installs
# eqy/sby against the bazel-built yosys.
local yosys_version
yosys_version=$("${PREFIX:-/usr/local}/bin/yosys" --version | awk '{print $2}')
./tools/OpenROAD/etc/DependencyInstaller.sh ${OR_INSTALLER_ARGS} -yosys-ver="v${yosys_version}"
Comment on lines +45 to +47

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

If the newly installed Yosys binary is missing, not executable, or fails to run, yosys_version will be empty, causing the script to pass -yosys-ver="v" to the OpenROAD installer. Adding defensive checks to verify that the binary exists and successfully returns a version ensures the script fails early with a clear error message.

Suggested change
local yosys_version
yosys_version=$("${PREFIX:-/usr/local}/bin/yosys" --version | awk '{print $2}')
./tools/OpenROAD/etc/DependencyInstaller.sh ${OR_INSTALLER_ARGS} -yosys-ver="v${yosys_version}"
local yosys_bin="${PREFIX:-/usr/local}/bin/yosys"
if [[ ! -x "${yosys_bin}" ]]; then
echo "ERROR: Yosys binary not found or not executable at ${yosys_bin}" >&2
exit 1
fi
local yosys_version
yosys_version=$("${yosys_bin}" --version | awk '{print $2}')
if [[ -z "${yosys_version}" ]]; then
echo "ERROR: Failed to detect Yosys version from ${yosys_bin}" >&2
exit 1
fi
./tools/OpenROAD/etc/DependencyInstaller.sh ${OR_INSTALLER_ARGS} -yosys-ver="v${yosys_version}"

}

_installYosysBazel() {
local yosys_prefix=${PREFIX:-"/usr/local"}
local bazelisk="bazelisk"
if ! command -v bazelisk &> /dev/null; then
bazelisk="${yosys_prefix}/bin/bazelisk"
fi
./tools/OpenROAD/etc/DependencyInstaller.sh ${OR_INSTALLER_ARGS} -yosys-ver="${YOSYS_VER}"
# --symlink_prefix=/ avoids dropping bazel-* convenience symlinks
# (root-owned when run under sudo) in the source tree; resolve the
# tarball via the execution root instead of the symlinks.
"${bazelisk}" build --symlink_prefix=/ //:yosys_tar
local tarball
tarball="$("${bazelisk}" info execution_root)/$("${bazelisk}" cquery --symlink_prefix=/ --output=files //:yosys_tar 2> /dev/null)"
mkdir -p "${yosys_prefix}"
tar -xzf "${tarball}" -C "${yosys_prefix}"
Comment on lines +53 to +63

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

If bazelisk cquery fails or returns an empty path, tarball will resolve to the execution root directory itself, causing tar to fail. Adding defensive checks to verify that bazelisk is available, the query succeeds, and the tarball exists before running tar ensures robust error handling.

    if ! command -v "${bazelisk}" &> /dev/null; then
        echo "ERROR: bazelisk not found in PATH or at ${yosys_prefix}/bin/bazelisk" >&2
        exit 1
    fi
    # --symlink_prefix=/ avoids dropping bazel-* convenience symlinks
    # (root-owned when run under sudo) in the source tree; resolve the
    # tarball via the execution root instead of the symlinks.
    "${bazelisk}" build --symlink_prefix=/ //:yosys_tar
    local tarball_relative
    tarball_relative="$("${bazelisk}" cquery --symlink_prefix=/ --output=files //:yosys_tar 2> /dev/null)"
    if [[ -z "${tarball_relative}" ]]; then
        echo "ERROR: Failed to resolve yosys_tar path via bazelisk cquery" >&2
        exit 1
    fi
    local tarball
    tarball="$("${bazelisk}" info execution_root)/${tarball_relative}"
    if [[ ! -f "${tarball}" ]]; then
        echo "ERROR: Yosys tarball not found at ${tarball}" >&2
        exit 1
    fi
    mkdir -p "${yosys_prefix}"
    tar -xzf "${tarball}" -C "${yosys_prefix}"

}

_installPipCommon() {
Expand Down Expand Up @@ -350,10 +380,6 @@ Usage: $0 [-all|-base|-common] [-<ARGS>]
# sudo or with root access.
$0 -ci
# Installs CI tools
$0 -yosys-ver=VERSION
# Installs specified version of Yosys.
# By default, the Yosys version is
# obtained from tools/yosys/docs/source/conf.py
$0 -constant-build-dir
# Use constant build directory, instead of
# random one.
Expand All @@ -363,7 +389,6 @@ EOF

# default args
OR_INSTALLER_ARGS="-eqy"
YOSYS_VER=""
# default prefix
PREFIX=""
# default option
Expand Down Expand Up @@ -407,9 +432,6 @@ while [ "$#" -gt 0 ]; do
-save-deps-prefixes=*)
OR_INSTALLER_ARGS="${OR_INSTALLER_ARGS} $1"
;;
-yosys-ver=*)
YOSYS_VER=${1#*=}
;;
-prefix=*)
OR_INSTALLER_ARGS="${OR_INSTALLER_ARGS} $1"
PREFIX=${1#*=}
Expand Down
Loading