-
Notifications
You must be signed in to change notification settings - Fork 3
Migration: From Clang Tools Binaries to Python Wheels #87
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
f345167
Migrate from clang-tools binaries to wheels
shenxianpeng 7e13841
allow run in parallel and pin clang-tools version
shenxianpeng 9579df6
update run.sh
shenxianpeng c03ae0f
fix utils.py tests and others
shenxianpeng b67718a
fix: add mising package
shenxianpeng 99f4501
revert mian.c change
shenxianpeng fbb7eb9
revert mian.c change
shenxianpeng d1ee4f5
docs: update docs
shenxianpeng 4dcaef1
add @pytest.mark.benchmark to test
shenxianpeng ebc62b6
pre-commit autoupdate
shenxianpeng 6610e8d
update docs
shenxianpeng 02670a3
update docs
shenxianpeng f34d1e5
increase test coverage
shenxianpeng File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
# Migration Guide: From clang-tools to Python Wheels | ||
|
||
## Overview | ||
|
||
Starting from version 0.9.0, `cpp-linter-hooks` has migrated from using the `clang-tools` package to using Python wheel packages for `clang-format` and `clang-tidy`. This change provides better cross-platform compatibility, easier installation, and more reliable dependency management. | ||
|
||
## What Changed | ||
|
||
### Dependencies | ||
- **Before**: Used `clang-tools==0.15.1` package | ||
- **After**: Uses `clang-format` and `clang-tidy` wheel packages from PyPI | ||
|
||
### Installation Method | ||
- **Before**: clang-format and clang-tidy were installed via `clang-tools` package which managed binaries | ||
- **After**: clang-format and clang-tidy are installed as Python packages and available as executables | ||
|
||
### Benefits of Migration | ||
|
||
1. **Better Cross-Platform Support**: Python wheels work consistently across different operating systems | ||
2. **Simplified Installation**: No need to manage binary installations separately | ||
3. **More Reliable**: No more issues with binary compatibility or single threaded execution | ||
4. **Better Version Management**: Each tool version is a separate package release | ||
|
||
## Breaking Changes | ||
|
||
### For End Users | ||
|
||
- **No breaking changes**: The pre-commit hook configuration remains exactly the same | ||
- All existing `.pre-commit-config.yaml` files will continue to work without modification | ||
|
||
### For Developers | ||
- The internal `ensure_installed()` function now returns the tool name instead of a Path object | ||
- The `util.py` module has been rewritten to use `shutil.which()` instead of `clang_tools.install` | ||
- Tests have been updated to mock the new wheel-based installation | ||
|
||
## Migration Steps | ||
|
||
### For End Users | ||
No action required! Your existing configuration will continue to work. | ||
|
||
### For Developers/Contributors | ||
1. Update your development environment: | ||
```bash | ||
pip install clang-format clang-tidy | ||
``` | ||
|
||
2. If you were importing from the utility module: | ||
```python | ||
# Before | ||
from cpp_linter_hooks.util import ensure_installed | ||
path = ensure_installed("clang-format", "18") | ||
command = [str(path), "--version"] | ||
|
||
# After | ||
from cpp_linter_hooks.util import ensure_installed | ||
tool_name = ensure_installed("clang-format", "18") | ||
command = [tool_name, "--version"] | ||
``` | ||
|
||
## Version Support | ||
|
||
The wheel packages support the same LLVM versions as before: | ||
- LLVM 16, 17, 18, 19, 20+ | ||
- The `--version` argument continues to work as expected | ||
|
||
## Troubleshooting | ||
|
||
### Tool Not Found Error | ||
If you encounter "command not found" errors: | ||
|
||
1. Ensure the wheel packages are installed: | ||
```bash | ||
pip install clang-format clang-tidy | ||
``` | ||
|
||
2. Verify the tools are available: | ||
```bash | ||
clang-format --version | ||
clang-tidy --version | ||
``` | ||
|
||
3. Check that the tools are in your PATH: | ||
```bash | ||
which clang-format | ||
which clang-tidy | ||
``` | ||
|
||
### Version Mismatch | ||
If you need a specific version, you can install it explicitly: | ||
```bash | ||
pip install clang-format==18.1.8 | ||
pip install clang-tidy==18.1.8 | ||
``` | ||
|
||
## Support | ||
|
||
If you encounter any issues after the migration, please: | ||
1. Check this migration guide | ||
2. Search existing [issues](https://github.com/cpp-linter/cpp-linter-hooks/issues) | ||
3. Create a new issue with: | ||
- Your operating system | ||
- Python version | ||
- The exact error message | ||
- Your `.pre-commit-config.yaml` configuration | ||
|
||
## References | ||
|
||
- [clang-format wheel package](https://github.com/ssciwr/clang-format-wheel) | ||
- [clang-tidy wheel package](https://github.com/ssciwr/clang-tidy-wheel) | ||
- [PyPI clang-format](https://pypi.org/project/clang-format/) | ||
- [PyPI clang-tidy](https://pypi.org/project/clang-tidy/) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,64 @@ | ||
import sys | ||
import shutil | ||
from pathlib import Path | ||
import logging | ||
from typing import Optional | ||
|
||
from clang_tools.util import Version | ||
from clang_tools.install import is_installed as _is_installed, install_tool | ||
|
||
|
||
LOG = logging.getLogger(__name__) | ||
|
||
DEFAULT_CLANG_VERSION = "20" # Default version for clang tools, can be overridden | ||
|
||
DEFAULT_CLANG_VERSION = "18" # Default version for clang tools, can be overridden | ||
|
||
|
||
def is_installed(tool_name: str, version: str) -> Optional[Path]: | ||
def is_installed(tool_name: str, version: str = "") -> Optional[Path]: | ||
"""Check if tool is installed. | ||
|
||
Checks the current python prefix and PATH via clang_tools.install.is_installed. | ||
With wheel packages, the tools are installed as regular Python packages | ||
and available via shutil.which(). | ||
""" | ||
# check in current python prefix (usual situation when we installed into pre-commit venv) | ||
directory = Path(sys.executable).parent | ||
path = directory / f"{tool_name}-{version}" | ||
if path.is_file(): | ||
return path | ||
|
||
# parse the user-input version as a string | ||
parsed_ver = Version(version) | ||
# also check using clang_tools | ||
path = _is_installed(tool_name, parsed_ver) | ||
if path is not None: | ||
return Path(path) | ||
# Check if tool is available in PATH | ||
tool_path = shutil.which(tool_name) | ||
if tool_path is not None: | ||
return Path(tool_path) | ||
|
||
# Check if tool is available in current Python environment | ||
if sys.executable: | ||
python_dir = Path(sys.executable).parent | ||
tool_path = python_dir / tool_name | ||
if tool_path.is_file(): | ||
return tool_path | ||
|
||
# Also check Scripts directory on Windows | ||
scripts_dir = python_dir / "Scripts" | ||
if scripts_dir.exists(): | ||
tool_path = scripts_dir / tool_name | ||
if tool_path.is_file(): | ||
return tool_path | ||
# Try with .exe extension on Windows | ||
tool_path = scripts_dir / f"{tool_name}.exe" | ||
if tool_path.is_file(): | ||
return tool_path | ||
|
||
# not found | ||
return None | ||
shenxianpeng marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
def ensure_installed(tool_name: str, version: str = DEFAULT_CLANG_VERSION) -> Path: | ||
def ensure_installed(tool_name: str, version: str = "") -> str: | ||
""" | ||
Ensure tool is available at given version. | ||
Ensure tool is available. With wheel packages, we assume the tools are | ||
installed as dependencies and available in PATH. | ||
|
||
Returns the tool name (not path) since the wheel packages install the tools | ||
as executables that can be called directly. | ||
""" | ||
LOG.info("Checking for %s, version %s", tool_name, version) | ||
LOG.info("Checking for %s", tool_name) | ||
path = is_installed(tool_name, version) | ||
if path is not None: | ||
LOG.info("%s, version %s is already installed", tool_name, version) | ||
return path | ||
LOG.info("%s is available at %s", tool_name, path) | ||
return tool_name # Return tool name for direct execution | ||
|
||
LOG.info("Installing %s, version %s", tool_name, version) | ||
directory = Path(sys.executable).parent | ||
install_tool(tool_name, version, directory=str(directory), no_progress_bar=True) | ||
return directory / f"{tool_name}-{version}" | ||
# If not found, we'll still return the tool name and let subprocess handle the error | ||
LOG.warning( | ||
"%s not found in PATH. Make sure the %s wheel package is installed.", | ||
tool_name, | ||
tool_name, | ||
) | ||
return tool_name |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,6 @@ | ||
#include <stdio.h> | ||
int main() {for (;;) break; printf("Hello world!\n");return 0;} | ||
int main() { | ||
for (;;) break; | ||
printf("Hello world!\n"); | ||
return 0; | ||
} | ||
shenxianpeng marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.