Giter Club home page Giter Club logo

clang-format-wheel's Introduction

clang-format Python distribution

PyPI Release

This project packages the clang-format utility as a Python package. It allows you to install clang-format directly from PyPI:

python -m pip install clang-format

This projects intends to release a new PyPI package for each major and minor release of clang-format.

Use with pipx

You can use pipx to run clang-format, as well. For example, pipx run clang-format <args> will run clang-format without any previous install required on any machine with pipx (including all default GitHub Actions / Azure runners, avoiding requiring a pre-install step or even actions/setup-python).

Use from pre-commit

A pre-commit hook is also provided, use like this:

- repo: https://github.com/pre-commit/mirrors-clang-format
  rev: v18.1.7
  hooks:
  - id: clang-format
    types_or: [c++, c, cuda]

In contrast to many other pre-commit hooks, the versioning of the hook matches the versioning of clang-format.

If you are required to stick with a given major/minor version of clang-format with your pre-commit-hook, you can use this alternative hook repository that also receives backports of older versions of clang-format. Currently, all major/minor versions of LLVM >= 10 are supported. It is best to subscribe to releases of the hook repository to get notified of new backport releases, as pre-commit's auto-upgrade functionality will not work in that case.

Building new releases

The clang-format-wheel repository provides the logic to build and publish binary wheels of the clang-format utility.

In order to add a new release, the following steps are necessary:

Alternatively, the workflow can be triggered manually:

On manual triggers, the following input variables are available:

  • llvm_version: Override the LLVM version (default: "")
  • wheel_version: Override the wheel packaging version (default "0")
  • skip_emulation: Set which emulation builds to skip, e.g. "qemu" (default: "")
  • deploy_to_testpypi: Whether to deploy to TestPyPI instead of PyPI (default: false)

The repository with the precommit hook is automatically updated using a scheduled Github Actions workflow.

Acknowledgements

This repository extends the great work of several other projects:

  • clang-format itself is provided by the LLVM project under the Apache 2.0 License with LLVM exceptions.
  • The build logic is based on scikit-build-core which greatly reduces the amount of low level code necessary to package clang-format.
  • The scikit-build packaging examples of CMake and Ninja were very helpful in packaging clang-format.
  • The CI build process is controlled by cibuildwheel which makes building wheels across a number of platforms a pleasant experience (!)

Special thanks goes to mgevaert who initiated this project and maintained it until 2021.

We are grateful for the generous provisioning with CI resources that GitHub currently offers to Open Source projects.

clang-format-wheel's People

Contributors

dependabot[bot] avatar dokempf avatar fgrie avatar henryiii avatar lkeegan avatar mgevaert avatar nightlark avatar skylion007 avatar yanqd0 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

clang-format-wheel's Issues

Could you expose clang-format-diff.py (or git-clang-format) to the precommit hook?

Hello,

I'd like to request a way to run either of these from the precommit hook, with the goal of only reformatting the changed lines. This would pair very nicely with https://github.com/akaihola/darker, and I would hope it's usable in a similar way as their precommit hook.

It might already be possible to do what is desired - but if so, would you be able to add it to the documentation? I would be happy to help if needed.

How to run on all files in a repository?

The pre-commit configuration works fine:

  - repo: https://github.com/pre-commit/mirrors-clang-format
    rev: "v18.1.2"
    hooks:
      - id: clang-format
        # Supports a lot more filetypes, but only tagging those we use
        # https://github.com/pre-commit/mirrors-clang-format/blob/main/.pre-commit-hooks.yaml#L6
        types: [file]
        files: ".*\\.(cpp|cxx|h)"

pre-commit run --all

But I'd like to have this as a github action as well. Running locally, I try pipx run clang-format --dry-run **/*.h **/*.cpp and it gives the following output:

⚠️  clang-format is already on your PATH and installed at C:\Program Files\Python39\Scripts\clang-format.EXE. Downloading and running anyway.
invalid argument
invalid argument

I also tried to pass all the files in (powershell):

clang-format --dry-run $(git ls-files '*.cpp' '*.cxx' "*.h")

But that just resulted in

ResourceUnavailable: Program 'clang-format.exe' failed to run: An error occurred trying to start process 'C:\Program Files\Python39\Scripts\clang-format.exe' with working directory 'E:\Users\Avasam\Documents\Git\pywin32'. The filename or extension is too long.At line:1 char:1
+ clang-format --dry-run $files
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~.

Support NixOS

Comes from https://github.com/pre-commit/mirrors-clang-format/issues/26:

This hook will download .cache/pre-commit/repon0nie2em/py_env-python3.10/lib/python3.10/site-packages/clang_format/data/bin/clang-format for me. However, My OS is NixOS, which doesn't respect HFS so it must patchelf every program. Inspired by sass-contrib/sass-embedded-host-ruby#116, I NIX_BINTOOLS=/nix/store/ngh5s155lcm1hwsakrfhla536x9g5v4z-binutils-wrapper-2.40 pre-commit run -a. It works for https://github.com/scop/pre-commit-shfmt, but cannot work for this hook. How to make it work?

Pip install does not work

Unable to pip install...

Python 3.8.0
pip 19.3.1
macOS 10.14.6

$ pip install clang-format
ERROR: Could not find a version that satisfies the requirement clang-format (from versions: none)
ERROR: No matching distribution found for clang-format

Missing aarch64 wheel for Linux

First of all, thank you for all your efforts for packaging LLVM's clang-format and upload to PyPi. Recently, this package is used as "extra" package by Zephyr project and can also be installed very easily on Linux, macOS and Windows through the provided wheels without waiting for the rebuild as long as it is an x86 system.

Now, unfortunately, the ARM 64 bit architecture is not (yet) packaged as a wheel. So on a Raspberry Pi 4 this means an extremely long installation time, because a native C++ compile has to build the wheel from the sdist package first. This could be saved if there would be an aarch64 wheel already on PyPi.

I have seen in the GitHub worker that exactly this architecture is skipped. Is there a technical reason for this? What is required to ship manylinux-aarch64 as a wheel as well?

Maintenance of clang-format wheel?

Hi @mgevaert, I noticed there hasn't been much GitHub activity in a few years or updates to the clang-format PyPI wheel in a few years. Are you around to accept PRs and make releases on PyPI? If not, would you be willing to add me to the PyPI project?

I'm interested in getting the package updated to include wheels for newer versions of clang-format, along with binary wheels for macOS and Windows -- it would be much more convenient to use with pre-commit hooks than a Docker image (not to mention the new pre-commit.ci service doesn't support docker); part of this would involve getting working sdist that can build clang-format on Windows/Linux/macOS, and setting up GitHub actions build and publish binary wheels for those platforms to PyPI.

Regards,
Ryan

(I sent an email to the one listed on PyPI for the wheel, though I wasn't sure if you still own the name due to the expired SSL cert)

Better explanation of version pinning

For this PR, @henryiii has talked about how you can version pin with this repo. I'm not seeing anything about it in the README. Can you be more explicit in the README with a section on "Using a specific version", with an example?

Usage from pre-commit

Currently, this works perfectly like this:

- id: clang-format
  name: Clang Format
  language: python
  types:
  - c++
  entry: clang-format
  additional_dependencies: [clang-format==12.*]
  args:
  - -style=file
  - -i

However, this will not get update PRs from pre-commit.ci, or support pre-commit autoupdate. Ideally, it would be nice to have a hook repository for this. The "obvious" thing, adding a .pre-commit-hooks.yml to this repository, would not work, since it will try to build from source. The ideal thing is likely to follow the method used by https://github.com/cheshirekow/cmake-format-precommit, and have a clang-format-precommit repository. It would be very simple, but would require a bit of maintenance every time there's a new release (update a line, make a tag/release). Would you be interested if I set up such a repository and then moved it here? Or I could set this up elsewhere (like in pybind).

Latest release, 14.0.0 isn't available on PyPI

Hi,

The latest release of this project doesn't seem to be available on PyPI 1 but shows up on TestPyPI 2.
I looked at the CI configuration briefly but couldn't pinpoint the issue.

Cheers,
Liam

Providing of packed **and** unpacked executables as GitHub release

Thanks for the effort and the hard ground work of build and packaging clang-format for so many platforms.

As you are already doing all the heavy lifting when building clang-format (14 platforms as of 18.1.3), I wanted to ask whether you could additionally to the PyPI release also provide

  • the raw binaries and
  • the wheels

as part of the GitHub release.

This would make clang-format easily usable on platforms where it is otherwise not available outside of LLVM (e.g., Windows) without the requirement for python and accompanied with the wheels, everything would be available at one central place.

Of course, one could just fork you repository, remove the wheel build step, but this seems like a waste of energy, compared to just upload a few more artifacts to the GitHub release.

If this is considered a good idea, I would be willing to help.

18.1.7 windows build failure

The windows builds are failing with llvm version 18.1.7

This is a workflow dispatch CI job based on the 18.1.6 tag (to make sure this wasn't caused by my latest change to the CI):

https://github.com/ssciwr/clang-format-wheel/actions/runs/9460825538/job/26060343775

    FAILED: include/llvm/CodeGen/GenVT.inc C:/Users/runneradmin/AppData/Local/Temp/tmpsfbi7bvb/build/llvm/include/llvm/CodeGen/GenVT.inc

These .inc files are generated by the tblgen binary that is built as part of the compilation.

Any chance for a clang-tidy version of this repository?

The clang-format package and the corresponding hook are great.

I also rely on clang-tidy to enforce code style specifically through the readability-identifier-naming check.
However, there doesn't seem to be any freestanding package for clang-tidy that could be used with pre-commit.

Unfortunately, I have only minimal Python background so I'm unable to make a fork and do it myself.

Is .clang-format supported?

Hey! I'm trying to use the pre-commit clang-format mirror in CI https://github.com/pre-commit/mirrors-clang-format
which points to this wheel repository for further docs and info.

I can't see anywhere mentioned whether the CI precommit bot uses the .clang-format file from within a repository when pushing the changes commit. Is that supported, can it be configured somehow?

Thanks and sorry for opening an issue for this.

Clang-format V15 Release

G'day! Thanks for this project!

I noticed that the actions to deploy version 15 failed due to timing out the maximum action execution time. Are there any plans to get the execution time down or retry?

git-clang-format does not work on MS Windows

As mentioned in the title, git-clang-format does not work on MS Windows.

PS > git clang-format
Traceback (most recent call last):
  File "C:\Program Files\Python37\lib\runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "C:\Program Files\Python37\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "C:\Users\username\directory\.venv\Scripts\git-clang-format.exe\__main__.py", line 7, in <module>
  File "c:\users\username\directory\.venv\lib\site-packages\clang_format\__init__.py", line 20, in git_clang_format
  File "c:\users\username\directory\.venv\lib\site-packages\clang_format\__init__.py", line 8, in _run
  File "C:\Program Files\Python37\lib\subprocess.py", line 323, in call
    with Popen(*popenargs, **kwargs) as p:
  File "C:\Program Files\Python37\lib\subprocess.py", line 775, in __init__
    restore_signals, start_new_session)
  File "C:\Program Files\Python37\lib\subprocess.py", line 1178, in _execute_child
    startupinfo)
OSError: [WinError 193] %1 ist keine zulässige Win32-Anwendung

This happens because Windows, unlike Linux/UNIIX, is not able to execute a Python file directly, instead, the Python interpreter has to be explicitely called with the script as argument (python git-clang-format).

An attempt to fix this is provided in fgrie@57a4e94

I only tested it on my Windows machine where it solves the problem:

PS > git clang-format
no modified files to format

Validation Linux/UNIX is still required.

Wheels for 15.0.4 depend on HomeBrew

Noticed this when updating the pre-commit hook for clang-format to the latest version:

clang-format.............................................................Failed
- hook id: clang-format
- exit code: 250

dyld[45432]: Library not loaded: /usr/local/opt/zstd/lib/libzstd.1.dylib
  Referenced from: <4E913C59-89FC-3A87-A878-6F1FAD905D38> /Users/ronald/.cache/pre-commit/repofwxj8esu/py_env-python3.9/lib/python3.9/site-packages/clang_format/data/bin/clang-format
  Reason: tried: '/usr/local/opt/zstd/lib/libzstd.1.dylib' (no such file), '/System/Volumes/Preboot/Cryptexes/OS/usr/local/opt/zstd/lib/libzstd.1.dylib' (no such file), '/usr/local/opt/zstd/lib/libzstd.1.dylib' (no such file), '/usr/local/lib/libzstd.1.dylib' (no such file), '/usr/lib/libzstd.1.dylib' (no such file, not in dyld cache)

This is on a machine that does not have homebrew on it. The pre-commit hook installs the clang-format wheel from PyPI.

Proper way to test shipped binaries

Over on the https://github.com/ssciwr/clang-tidy-wheel I've run into trouble testing the binary that's shipped with the wheel.
The get_executable stuff in the package does not append .exe on windows. If we use subprocess.run on the resulting absolute path that fails. If you use subprocess.run just on the command name and rely on it being in PATH, windows will silently add .exe. I've found this out because in the test I use something like

exe = clang_tidy._get_executable("clang-tidy")
subprocess.run([exe])

Note that in the format tests here, the command name form is used. In fact the clang_format package is never imported.
This might execute the right binary, but depends on ordering in PATH. clang-format is also installed in the windows base image.

I suspect the situation on MacOS might be similar if the build results in a clang-tidy.{bin,dmg}, but haven't checked yet.

I think using the full absolute path in the tests is the correct way to do it. And once I got it all fixed on the tidy side, I would PR a matching update here.

How to format .h.in files

Hello,
Thx for this nice pre-commit hook,

I have some header files that are generated by CMake. The input file has a .h.in extension and clang-format don't format it. How it is possible to add this extenssion to the hook ?

Deployment Requirement Bugs

We started trying to use the clang-format precommit hook on habitat-sim and came across a few bumps that could be easily fixed. First of all, on OSX unless you use a very recent version of pip (>=22) it will not use the wheel. This can be fixed by specifying pip>=21 in the additional dependencies, but it may better to specify it in the setup.py itself, somewhere.

Also for the build from source, it really should include the pip version cmake so it doesn't try to use the old system's cmake as the version needed to build clang-format from source is also relatively recent (3.16).

Windows: Always returns exit code 0 even in failure cases

I am trying to roll out the clang-format pre-commit hook to my mac+windows dev team, which uses clang-format-wheel. Everything works great on the mac side, but there are a few issues on the Windows side.

A known issue is that the Windows side is missing dependencies. I appreciate that is it being worked on.

An additional issue, however, is that clang-format-wheel isn't correctly bubbling up the error case. It seems that if DLLs are missing, clang-format should return a non-zero exit code. I opened up an issue in mirrors-clang-format and was redirected to post the issue here.

A few other observations I made:

  • When running clang-format via pre-commit through cmd.exe, I get a ton of the "missing DLL" dialog popups as seen in #22. I expect pre-commit to fail (based on the clang-format exit code), but it succeeds.
  • When running clang-format via pre-commit through Powershell, I don't get any of the "missing DLL" dialog popups, but clang-format definitely did not run because the corrections weren't made. pre-commit continues to show success, which indicates that the error code wasn't bubbled up.

Need to bump minimum `identify` version?

Hi there @dokempf,

I was encountering the following issue

==> At Hook(id='clang-format')
==> At key: types_or
==> At index 9
=====> Type tag 'textproto' is not recognized.  Try upgrading identify and pre-commit?

As was seen here: pre-commit/pre-commit#1946 (comment)

The issue was that I had installed pre-commit using pipx and the identify dependency was not updated within the pre-commit pipx venv.

so pipx upgrade pre-commit didn't do the trick, what was needed was

pipx runpip pre-commit install -U identify

Looking at the history of the inclusion of this textproto parsing, I think this specific pre-commit hook should include a minimum version of identify such that this error shouldn't appear.

I am not super familiar with how the pre-commit hooks work, but if it is as trivial as including a minimum version in this package that would be ideal. I believe it would look like: identify>2.5.20, according to this commit pre-commit/identify@99968e4

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.