Skip to content

Replace GNU Make Codegen with Python - #2241

Open
ni-jfitzger wants to merge 12 commits into
ni:masterfrom
ni-jfitzger:replace-gnu-make-with-python
Open

ni-jfitzger wants to merge 12 commits into
ni:masterfrom
ni-jfitzger:replace-gnu-make-with-python

Conversation

@ni-jfitzger

@ni-jfitzger ni-jfitzger commented Oct 23, 2026 •

Copy link
Copy Markdown
Collaborator
  • This contribution adheres to CONTRIBUTING.md.
  • [ ] I've updated CHANGELOG.md if applicable.
  • I've added tests applicable for this pull request

What does this Pull Request accomplish?

The GNU Make code used for code generation was a barrier to entry for anyone not familar with GNU Make and, frankly, even for those who are familiar with it. Some of the code was very difficult to read. Variables and recipes were also scattered across the repo, making it more difficult to read.

This PR replaces those files and some of their Python code with a consolidated build/codegen.py, which implements the codegen, clean and installers steps.

Performance: This seems to be faster than the old codegen, though how much faster may vary depending on your machine and your environment. Why is it faster? Because we don't launch Python every time we need to render a template. We use the active Python process, instead.

List issues fixed by this Pull Request below, if any.

What testing has been done?

Ran tox -e clean, tox -e codegen, tox -e installers, tox -e codegen -- -driver nidmm. No difference seen in the generated files.

Ran newly generated unit tests. These don't cover all of codegen.py, but it's more than we had for the MAKEFILE code. Our primary method of catching issues remains ensure_codegen_up_to_date.py, diffs and code review of PRs.

@codecov-commenter

codecov-commenter commented Oct 23, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 59.33610% with 98 lines in your changes missing coverage. Please review.
✅ Project coverage is 87.89%. Comparing base (89108d2) to head (61b18e5).
⚠️ Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
build/codegen.py 58.64% 98 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #2241      +/-   ##
==========================================
- Coverage   88.12%   87.89%   -0.23%     
==========================================
  Files          88       89       +1     
  Lines       22830    23071     +241     
==========================================
+ Hits        20118    20278     +160     
- Misses       2712     2793      +81     
Flag Coverage Δ
codegenunittests 82.42% <59.33%> (-2.37%) ⬇️
nidcpowersystemtests 94.40% <ø> (+0.04%) ⬆️
nidcpowerunittests 89.53% <ø> (ø)
nidigitalsystemtests 92.04% <ø> (ø)
nidigitalunittests 68.43% <ø> (ø)
nidmmsystemtests 92.33% <ø> (ø)
nifakeunittests 86.08% <ø> (ø)
nifgensystemtests 94.34% <ø> (ø)
nimodinstsystemtests 73.85% <ø> (ø)
nimodinstunittests 94.20% <ø> (ø)
nirfsasystemtests 82.71% <ø> (ø)
nirfsaunittests 63.37% <ø> (ø)
nirfsgsystemtests 81.22% <ø> (ø)
niscopesystemtests 89.99% <ø> (ø)
niscopeunittests 43.20% <ø> (ø)
nisesystemtests 91.50% <ø> (ø)
niswitchsystemtests 81.66% <ø> (ø)
nitclksystemtests 94.87% <ø> (ø)
nitclkunittests 98.26% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@ni-jfitzger

Copy link
Copy Markdown
Collaborator Author

Something seems off about the reported code coverage for build/helper.

@ni-jfitzger

Copy link
Copy Markdown
Collaborator Author

Something seems off about the reported code coverage for build/helper.

Apparently --source is supposed to be a comma-separated list. You can't pass it twice in the same command.

@marcoskirsch

Copy link
Copy Markdown
Member

Using GitHub Copilot

This is (or should be) irrelevant.

@marcoskirsch

Copy link
Copy Markdown
Member

Included but unrleated: I've capped the pytest version used for system tests, because the latest release breaks our testing.

Please ensure there is an open issue for this.

@ni-jfitzger

Copy link
Copy Markdown
Collaborator Author

Included but unrleated: I've capped the pytest version used for system tests, because the latest release breaks our testing.

Please ensure there is an open issue for this.

Yes, I created one last week and we fixed and closed it.

Comment thread CONTRIBUTING.md
@@ -23,8 +23,6 @@ In order to have the ability to build and run the tests you will need a few thin

- Install and enable [Windows Subsystem for Linux](https://msdn.microsoft.com/en-us/commandline/wsl/install_guide)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Is WSL still needed? I would hope not.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Not sure. I'll need to try setting up without it.
For internal contributors, pyenv might make it difficult for tox to find multiple Python installations.

Comment thread CONTRIBUTING.md

tox -e clean

1. To regenerate only some drivers, pass arguments to the code generator through `tox`. For example:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

To generate the code for a subset of the driver APIs, pass arguments to the code generator through `tox`. For example:
  • We try to call these "driver APIs" not drivers, iirc. consistently.
  • I think this applies to first time generation, not only to re-generation.

Comment thread CONTRIBUTING.md

tox -e py312-codegen -- --driver nidmm --driver nitclk

`tox` isn't required: with the `codegen` dependencies installed (see `deps` in [tox.ini](./tox.ini)), you can run the

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

How about:

You can bypass `tox` and run the code generator directly if for some reason you prefer it. In order to do this, you must install all dependencies as listed in the `deps` section of  in [tox.ini](./tox.ini); then run:

    python -m build.codegen generate [--driver DRIVER ...]

The code generator also supports `clean` and `installers` commands. Use `--help` for details.

Comment thread build/codegen.py
"""Generate the driver bindings, documentation and installers.

Run from anywhere with ``python -m build.codegen {generate,clean,installers}``. Everything is
regenerated on every run; no attempt is made to skip work that is already up to date.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Everything is
regenerated on every run; no attempt is made to skip work that is already up to date.

I would remove this. It's distracting noise only relevant to someone familiar with the history of the repo. Someone looking at script would reasonably assume otherwise.

Comment thread build/codegen.py

ROOT_DIR = Path(__file__).resolve().parent.parent
BUILD_HELPER_DIR = ROOT_DIR / 'build'
TEMPLATE_DIR = BUILD_HELPER_DIR / 'templates'

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

TEMPLATES_DIR

weird to be inconsistent

Comment thread build/codegen.py


@dataclass(frozen=True)
class Driver:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Let's consistently call these things driver API or driver bindings
Please check the repo to see which one we use more, including the main README.md.

Comment thread build/codegen.py
proto_file_name: str = None

@property
def src_dir(self):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

How do you feel about adding type hints to this script?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

It shouldn't be too difficult.

Comment thread build/codegen.py

# In alphabetical order except put nifake first and nimodinst/nitclk last
# - nifake first to get the most code generator coverage
# - nimodinst last so that the version from nimodinst is used for any global versions (docs/conf.py)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I see nitclk as last

Comment thread build/codegen.py
print(f'{action:>15}: {path}')


def _find_template(driver, template_name):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

how come the arg driver is not self?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Consider calling this _get_template

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Replace GNU Make Code with Python

3 participants