| |
| |
| |
| import glob |
| import os |
| import shutil |
| import sys |
| from contextlib import suppress |
| from pathlib import Path |
| from typing import Dict, List, Optional, Tuple |
|
|
| import nox |
|
|
| |
| nox.options.sessions = ["dev_test"] |
|
|
| test_deps = ["pytest>=6"] |
| coverage_deps = ["coverage[toml]>=5.0", "pytest-cov"] |
| |
| |
| |
| |
| |
| |
| coverage_report_deps = ["coverage[toml]>=5.0", "jinja2<3.1", "gcovr==5.0"] |
|
|
| dev_deps = [ |
| "mypy", |
| "pre-commit", |
| "nox", |
| "ruff", |
| "clang-format", |
| ] |
|
|
| |
| cibuildwheel_version = "2.15.0" |
|
|
| |
| |
| |
|
|
|
|
| def simulator_support_matrix() -> List[Tuple[str, str, str]]: |
| """ |
| Get a list of supported simulator/toplevel-language/GPI-interface tuples. |
| """ |
|
|
| |
| standard = [ |
| (sim, toplevel_lang, gpi_interface) |
| for sim in ("activehdl", "riviera", "xcelium") |
| for toplevel_lang in ("verilog", "vhdl") |
| for gpi_interface in ("vpi", "vhpi") |
| if (toplevel_lang, gpi_interface) in (("verilog", "vpi"), ("vhdl", "vhpi")) |
| ] |
|
|
| |
| special = [ |
| ("cvc", "verilog", "vpi"), |
| ("ghdl", "vhdl", "vpi"), |
| ("icarus", "verilog", "vpi"), |
| ("nvc", "vhdl", "vhpi"), |
| ("questa", "verilog", "vpi"), |
| ("questa", "vhdl", "fli"), |
| ("questa", "vhdl", "vhpi"), |
| ("verilator", "verilog", "vpi"), |
| ("vcs", "verilog", "vpi"), |
| ] |
|
|
| return standard + special |
|
|
|
|
| def env_vars_for_test( |
| sim: Optional[str], toplevel_lang: Optional[str], gpi_interface: Optional[str] |
| ) -> Dict[str, str]: |
| """Prepare the environment variables controlling the test run.""" |
| e = {} |
| if sim is not None: |
| e["SIM"] = sim |
|
|
| if os.getenv("TOPLEVEL_LANG") is not None: |
| e["HDL_TOPLEVEL_LANG"] = os.getenv("TOPLEVEL_LANG") |
|
|
| if toplevel_lang is not None: |
| e["TOPLEVEL_LANG"] = toplevel_lang |
| e["HDL_TOPLEVEL_LANG"] = toplevel_lang |
|
|
| assert not (toplevel_lang == "verilog" and gpi_interface != "vpi") |
| if toplevel_lang == "vhdl" and gpi_interface is not None: |
| e["VHDL_GPI_INTERFACE"] = gpi_interface |
|
|
| return e |
|
|
|
|
| def stringify_dict(d: Dict[str, str]) -> str: |
| return ", ".join(f"{k}={v}" for k, v in d.items()) |
|
|
|
|
| def configure_env_for_dev_build(session: nox.session) -> None: |
| """Set environment variables for a development build. |
| |
| - Enable coverage collection. |
| - Build with more aggressive error checking. |
| """ |
| session.env["CFLAGS"] = " ".join( |
| [ |
| "-Werror", |
| "-Wno-deprecated-declarations", |
| "-Wsuggest-override", |
| "-g", |
| "--coverage", |
| ] |
| ) |
| session.env["COCOTB_LIBRARY_COVERAGE"] = "1" |
| session.env["CXXFLAGS"] = "-Werror" |
| session.env["LDFLAGS"] = "--coverage" |
|
|
|
|
| |
| |
| |
| |
| |
| |
| |
|
|
|
|
| @nox.session |
| def dev_build(session: nox.Session) -> None: |
| session.warn("No building is necessary for development sessions.") |
|
|
|
|
| @nox.session |
| def dev_test(session: nox.Session) -> None: |
| """Run all development tests as configured through environment variables.""" |
|
|
| dev_test_sim(session, sim=None, toplevel_lang=None, gpi_interface=None) |
| dev_test_nosim(session) |
| dev_coverage_combine(session) |
|
|
|
|
| @nox.session |
| @nox.parametrize("sim,toplevel_lang,gpi_interface", simulator_support_matrix()) |
| def dev_test_sim( |
| session: nox.Session, |
| sim: Optional[str], |
| toplevel_lang: Optional[str], |
| gpi_interface: Optional[str], |
| ) -> None: |
| """Test a development version of cocotb against a simulator.""" |
|
|
| configure_env_for_dev_build(session) |
|
|
| session.run("pip", "install", *test_deps, *coverage_deps) |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| session.run("pip", "install", ".") |
|
|
| env = env_vars_for_test(sim, toplevel_lang, gpi_interface) |
| config_str = stringify_dict(env) |
|
|
| |
| |
| |
| |
| coverage_file = Path(f".cov.test.sim-{sim}-{toplevel_lang}-{gpi_interface}") |
| with suppress(FileNotFoundError): |
| coverage_file.unlink() |
|
|
| session.log(f"Running 'make test' against a simulator {config_str}") |
| session.run("make", "test", external=True, env=env) |
|
|
| session.log(f"Running simulator-specific tests against a simulator {config_str}") |
| session.run( |
| "pytest", |
| "-v", |
| "--cov=cocotb", |
| "--cov-branch", |
| |
| "--cov-report=", |
| "-k", |
| "simulator_required", |
| env=env, |
| ) |
| Path(".coverage").rename(".coverage.pytest") |
|
|
| session.log(f"Running examples against a simulator {config_str}") |
| pytest_example_tree = [ |
| "examples/adder", |
| "examples/simple_dff", |
| "examples/matrix_multiplier", |
| "examples/mixed_language", |
| ] |
| session.run( |
| "pytest", |
| "-v", |
| *pytest_example_tree, |
| env=env, |
| ) |
|
|
| session.log(f"All tests and examples passed with configuration {config_str}!") |
|
|
| |
| |
| coverage_files = glob.glob("**/.coverage.cocotb", recursive=True) |
| if not coverage_files: |
| session.error( |
| "No coverage files found. Something went wrong during the test execution." |
| ) |
| coverage_files.append(".coverage.pytest") |
| session.run("coverage", "combine", "--append", *coverage_files) |
| Path(".coverage").rename(coverage_file) |
|
|
| session.log(f"Stored Python coverage for this test run in {coverage_file}.") |
|
|
|
|
| @nox.session |
| def dev_test_nosim(session: nox.Session) -> None: |
| """Run the simulator-agnostic tests against a cocotb development version.""" |
|
|
| configure_env_for_dev_build(session) |
|
|
| session.run("pip", "install", *test_deps, *coverage_deps) |
| session.run("pip", "install", ".") |
|
|
| |
| |
| |
| |
| coverage_file = Path(".cov.test.nosim") |
| with suppress(FileNotFoundError): |
| coverage_file.unlink() |
|
|
| |
| session.log("Running simulator-agnostic tests with pytest") |
| session.run( |
| "pytest", |
| "-v", |
| "--cov=cocotb", |
| "--cov-branch", |
| |
| "--cov-report=", |
| "-k", |
| "not simulator_required", |
| ) |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| session.log("All tests passed!") |
|
|
| |
| Path(".coverage").rename(coverage_file) |
|
|
| session.log(f"Stored Python coverage for this test run in {coverage_file}.") |
|
|
|
|
| @nox.session |
| def dev_coverage_combine(session: nox.Session) -> None: |
| """Combine coverage from previous dev_* runs into a .coverage file.""" |
| session.run("pip", "install", *coverage_report_deps) |
|
|
| coverage_files = glob.glob("**/.cov.test.*", recursive=True) |
| session.run("coverage", "combine", *coverage_files) |
| assert Path(".coverage").is_file() |
|
|
| session.log("Wrote combined coverage database for all tests to '.coverage'.") |
|
|
| session.notify("dev_coverage_report") |
|
|
|
|
| @nox.session |
| def dev_coverage_report(session: nox.Session) -> None: |
| """Report coverage results.""" |
| session.run("pip", "install", *coverage_report_deps) |
|
|
| |
| session.log("Producing Python and C/C++ coverage in Cobertura XML format") |
|
|
| coverage_python_xml = Path(".python_coverage.xml") |
| session.run("coverage", "xml", "-o", str(coverage_python_xml)) |
| assert coverage_python_xml.is_file() |
|
|
| coverage_cpp_xml = Path(".cpp_coverage.xml") |
| session.run( |
| "gcovr", |
| "--xml", |
| "--output", |
| str(coverage_cpp_xml), |
| ".", |
| ) |
| assert coverage_cpp_xml.is_file() |
|
|
| session.log( |
| f"Cobertura XML files written to {str(coverage_cpp_xml)!r} (C/C++) and {str(coverage_python_xml)!r} (Python)" |
| ) |
|
|
| |
| session.log("Python coverage") |
| session.run("coverage", "report") |
|
|
| session.log("Library coverage") |
| session.run("gcovr", "--print-summary", "--txt") |
|
|
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| dist_dir = "dist" |
|
|
|
|
| @nox.session |
| def release_clean(session: nox.Session) -> None: |
| """Remove all build artifacts from the dist directory.""" |
| shutil.rmtree(dist_dir, ignore_errors=True) |
|
|
|
|
| @nox.session |
| def release_build(session: nox.Session) -> None: |
| """Build a release (sdist and bdist).""" |
| session.notify("release_build_bdist") |
| session.notify("release_build_sdist") |
|
|
|
|
| @nox.session |
| def release_build_bdist(session: nox.Session) -> None: |
| """Build a binary distribution (wheels) on the current operating system.""" |
|
|
| |
| session.run("pip", "install", f"cibuildwheel=={cibuildwheel_version}") |
|
|
| |
| |
| if sys.platform.startswith("linux"): |
| platform = "linux" |
| elif sys.platform == "darwin": |
| platform = "macos" |
| elif sys.platform == "win32": |
| platform = "windows" |
| else: |
| session.error(f"Unknown platform: {sys.platform!r}") |
|
|
| session.log("Building binary distribution (wheels)") |
| session.run( |
| "cibuildwheel", |
| "--platform", |
| platform, |
| "--output-dir", |
| dist_dir, |
| ) |
|
|
| session.log( |
| f"Binary distribution in release mode for {platform!r} built into {dist_dir!r}" |
| ) |
|
|
|
|
| @nox.session |
| def release_build_sdist(session: nox.Session) -> None: |
| """Build the source distribution.""" |
|
|
| session.run("pip", "install", "build") |
|
|
| session.log("Building source distribution (sdist)") |
| session.run("python", "-m", "build", "--sdist", "--outdir", dist_dir, ".") |
|
|
| session.log(f"Source distribution in release mode built into {dist_dir!r}") |
|
|
|
|
| @nox.session |
| def release_test_sdist(session: nox.Session) -> None: |
| """Build and install the sdist.""" |
|
|
| |
| sdists = list(Path(dist_dir).glob("cocotb-*.tar.gz")) |
| if len(sdists) == 0: |
| session.error( |
| f"No *.tar.gz sdist file found in {dist_dir!r} " |
| f"Run the 'release_build' session first." |
| ) |
| if len(sdists) > 1: |
| session.error( |
| f"More than one potential sdist found in the {dist_dir!r} " |
| f"directory. Run the 'release_clean' session first!" |
| ) |
| sdist_path = sdists[0] |
| assert sdist_path.is_file() |
|
|
| session.log("Installing cocotb from sdist, which includes the build step") |
| session.run( |
| "pip", |
| "install", |
| str(sdist_path), |
| ) |
|
|
| session.log("Running cocotb-config as basic installation smoke test") |
| session.run("cocotb-config", "--version") |
|
|
|
|
| def release_install(session: nox.Session) -> None: |
| """Helper: Install cocotb from wheels and also install test dependencies.""" |
|
|
| |
| |
| |
| |
| |
|
|
| session.log("Installing cocotb dependencies from PyPi") |
| session.run("pip", "install", "find_libpython") |
|
|
| session.log(f"Installing cocotb from wheels in {dist_dir!r}") |
| session.run( |
| "pip", |
| "install", |
| "--force-reinstall", |
| "--only-binary", |
| "cocotb", |
| "--no-index", |
| "--no-dependencies", |
| "--find-links", |
| dist_dir, |
| "cocotb", |
| ) |
|
|
| session.log("Running cocotb-config as basic installation smoke test") |
| session.run("cocotb-config", "--version") |
|
|
| session.log("Installing test dependencies") |
| session.run("pip", "install", *test_deps) |
|
|
|
|
| @nox.session |
| @nox.parametrize("sim,toplevel_lang,gpi_interface", simulator_support_matrix()) |
| def release_test_sim( |
| session: nox.Session, sim: str, toplevel_lang: str, gpi_interface: str |
| ) -> None: |
| """Test a release version of cocotb against a simulator.""" |
|
|
| release_install(session) |
|
|
| env = env_vars_for_test(sim, toplevel_lang, gpi_interface) |
| config_str = stringify_dict(env) |
|
|
| session.log(f"Running tests against a simulator: {config_str}") |
| session.run("make", "test", external=True, env=env) |
|
|
| session.log(f"Running simulator-specific tests against a simulator {config_str}") |
| session.run( |
| "pytest", |
| "-v", |
| "-k", |
| "simulator_required", |
| env=env, |
| ) |
|
|
| session.log(f"All tests passed with configuration {config_str}!") |
|
|
|
|
| @nox.session |
| def release_test_nosim(session: nox.Session) -> None: |
| """Run the simulator-agnostic tests against a cocotb release.""" |
|
|
| release_install(session) |
|
|
| session.log("Running simulator-agnostic tests") |
| session.run( |
| "pytest", |
| "-v", |
| "-k", |
| "not simulator_required", |
| ) |
|
|
| session.log("All tests passed!") |
|
|
|
|
| def create_env_for_docs_build(session: nox.Session) -> None: |
| session.run( |
| "pip", "install", "docs/_vendor/domaintools" |
| ) |
| session.run("pip", "install", "-r", "docs/requirements.txt") |
|
|
|
|
| @nox.session |
| def docs(session: nox.Session) -> None: |
| """invoke sphinx-build to build the HTML docs""" |
| create_env_for_docs_build(session) |
| session.run("pip", "install", ".") |
| outdir = session.cache_dir / "docs_out" |
| session.run("sphinx-build", "./docs/source", str(outdir), "--color", "-b", "html") |
| index = (outdir / "index.html").resolve().as_uri() |
| session.log(f"Documentation is available at {index}") |
|
|
|
|
| @nox.session |
| def docs_preview(session: nox.Session) -> None: |
| """Build a live preview of the documentation""" |
| create_env_for_docs_build(session) |
| |
| session.run("pip", "install", "-e", ".") |
| session.run("pip", "install", "sphinx-autobuild") |
| outdir = session.cache_dir / "docs_out" |
| session.run( |
| "sphinx-autobuild", |
| |
| "--ignore", |
| "*/source/master-notes.rst", |
| "--ignore", |
| "*/doxygen/*", |
| |
| |
| "--watch", |
| "src/cocotb", |
| "./docs/source", |
| str(outdir), |
| ) |
|
|
|
|
| @nox.session |
| def docs_linkcheck(session: nox.Session) -> None: |
| """invoke sphinx-build to linkcheck the docs""" |
| create_env_for_docs_build(session) |
| session.run("pip", "install", ".") |
| outdir = session.cache_dir / "docs_out" |
| session.run( |
| "sphinx-build", |
| "./docs/source", |
| str(outdir), |
| "--color", |
| "-b", |
| "linkcheck", |
| ) |
|
|
|
|
| @nox.session |
| def docs_spelling(session: nox.Session) -> None: |
| """invoke sphinx-build to spellcheck the docs""" |
| create_env_for_docs_build(session) |
| session.run("pip", "install", ".") |
| outdir = session.cache_dir / "docs_out" |
| session.run( |
| "sphinx-build", |
| "./docs/source", |
| str(outdir), |
| "--color", |
| "-b", |
| "spelling", |
| ) |
|
|
|
|
| @nox.session(reuse_venv=True) |
| def dev(session: nox.Session) -> None: |
| """Build a development environment and optionally run a command given as extra args""" |
|
|
| configure_env_for_dev_build(session) |
| create_env_for_docs_build(session) |
|
|
| session.run( |
| "pip", "install", *test_deps, *dev_deps, *coverage_deps, *coverage_report_deps |
| ) |
| session.run("pip", "install", "-e", ".") |
| if session.posargs: |
| session.run(*session.posargs, external=True) |
|
|