├── .github ├── dependabot.yml └── workflows │ └── release.yml ├── .gitignore ├── CMakeLists.txt ├── COPYING.md ├── LICENSE.md ├── README.md ├── clang-format_version.txt ├── clang_format └── __init__.py ├── pyproject.toml └── test ├── __init__.py ├── helloworld.cc ├── helloworld_format.cc └── test_clang_format.py /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | version: 2 2 | updates: 3 | - package-ecosystem: "github-actions" 4 | directory: "/" 5 | schedule: 6 | interval: "monthly" 7 | -------------------------------------------------------------------------------- /.github/workflows/release.yml: -------------------------------------------------------------------------------- 1 | name: Build + Release Wheels 2 | 3 | on: 4 | push: 5 | tags: 6 | - 'v?[0-9]+.[0-9]+.[0-9]+' 7 | - 'v?[0-9]+.[0-9]+.[0-9]+.[0-9]+' 8 | workflow_dispatch: 9 | inputs: 10 | llvm_version: 11 | description: "LLVM version to build" 12 | required: false 13 | default: "" 14 | wheel_version: 15 | description: "Version of the wheel packaging (appended to LLVM version)" 16 | required: false 17 | default: "0" 18 | skip_emulation: 19 | description: "Emulation builds to skip (e.g. qemu)" 20 | required: false 21 | default: "" 22 | deploy_to_testpypi: 23 | description: "Whether the build should be deployed to test.pypi.org instead regular PyPI" 24 | required: true 25 | default: false 26 | 27 | jobs: 28 | build-wheels: 29 | name: "${{ matrix.os }} :: ${{ matrix.platform }}-${{ matrix.arch }}" 30 | runs-on: ${{ matrix.os }} 31 | 32 | strategy: 33 | matrix: 34 | # emulated linux: generate 4 matrix combinations with qemu on ubuntu: 35 | arch: ["ppc64le", "s390x"] 36 | platform: ["manylinux", "musllinux"] 37 | os: [ubuntu-latest] 38 | emulation: ["qemu"] 39 | exclude: 40 | # conditionally skip jobs requiring emulation: 41 | - os: ubuntu-latest 42 | emulation: ${{ github.event.inputs.skip_emulation }} 43 | include: 44 | # linux 45 | - os: ubuntu-latest 46 | platform: "manylinux" 47 | arch: "x86_64" 48 | - os: ubuntu-latest 49 | platform: "manylinux" 50 | arch: "i686" 51 | - os: ubuntu-latest 52 | platform: "musllinux" 53 | arch: "x86_64" 54 | - os: ubuntu-latest 55 | platform: "musllinux" 56 | arch: "i686" 57 | - os: ubuntu-24.04-arm 58 | platform: "manylinux" 59 | arch: "aarch64" 60 | - os: ubuntu-24.04-arm 61 | platform: "musllinux" 62 | arch: "aarch64" 63 | # windows 64 | - os: windows-2019 65 | platform: "win" 66 | arch: "AMD64" 67 | - os: windows-2019 68 | platform: "win" 69 | arch: "x86" 70 | - os: windows-11-arm 71 | platform: "win" 72 | arch: "ARM64" 73 | # macos 74 | - os: macos-13 75 | platform: "macos" 76 | arch: "x86_64" 77 | - os: macos-latest 78 | platform: "macos" 79 | arch: "arm64" 80 | 81 | steps: 82 | - uses: actions/checkout@v4 83 | 84 | - name: Support long paths on Windows 85 | if: runner.os == 'Windows' 86 | run: git config --system core.longpaths true 87 | 88 | - name: Set up msvc on Windows 89 | if: runner.os == 'Windows' 90 | uses: ilammy/msvc-dev-cmd@v1 91 | with: 92 | arch: ${{ matrix.arch }} 93 | 94 | - name: Remove strip binaries on arm64 windows runner to avoid "file format not recognized" error when running strip 95 | if: matrix.platform == 'win' && matrix.arch == 'ARM64' 96 | run: | 97 | rm C:\mingw64\bin\strip.exe 98 | rm C:\Strawberry\c\bin\strip.exe 99 | 100 | - name: Override LLVM version (${{ github.event.inputs.llvm_version }}) 101 | if: github.event.inputs.llvm_version 102 | run: | 103 | echo "${{ github.event.inputs.llvm_version }}.${{ github.event.inputs.wheel_version }}" > clang-format_version.txt 104 | cat clang-format_version.txt 105 | 106 | - name: Set up QEMU 107 | uses: docker/setup-qemu-action@v3.6.0 108 | with: 109 | image: tonistiigi/binfmt:qemu-v8.1.5 110 | if: runner.os == 'Linux' && matrix.emulation == 'qemu' 111 | 112 | - name: Build wheels 113 | uses: pypa/cibuildwheel@v2.23 114 | env: 115 | CIBW_ARCHS: "${{ matrix.arch }}" 116 | # restrict to a single Python version as wheel does not depend on Python: 117 | CIBW_BUILD: "cp311-${{ matrix.platform }}*" 118 | 119 | - uses: actions/upload-artifact@v4 120 | with: 121 | name: artifacts-wheels-${{ matrix.platform }}-${{ matrix.arch }} 122 | path: ./wheelhouse/*.whl 123 | 124 | build-sdist: 125 | name: Build source distribution 126 | runs-on: ubuntu-latest 127 | 128 | steps: 129 | - uses: actions/checkout@v4 130 | 131 | - name: Override LLVM version (${{ github.event.inputs.llvm_version }}) 132 | if: github.event.inputs.llvm_version 133 | run: | 134 | echo "${{ github.event.inputs.llvm_version }}.${{ github.event.inputs.wheel_version }}" > clang-format_version.txt 135 | cat clang-format_version.txt 136 | 137 | - name: Build SDist 138 | run: pipx run build --sdist 139 | 140 | - uses: actions/upload-artifact@v4 141 | with: 142 | name: artifacts-sdist 143 | path: dist/*.tar.gz 144 | 145 | test-sdist: 146 | name: Test build from source distribution 147 | needs: [build-sdist] 148 | runs-on: ubuntu-latest 149 | 150 | steps: 151 | - uses: actions/checkout@v4 152 | 153 | - uses: actions/setup-python@v5 154 | name: Install Python 155 | with: 156 | python-version: '3.13' 157 | 158 | - uses: astral-sh/setup-uv@v6 159 | 160 | - uses: actions/download-artifact@v4 161 | with: 162 | name: artifacts-sdist 163 | path: sdist 164 | 165 | - name: Install from SDist 166 | run: 167 | pip install sdist/*.tar.gz 168 | 169 | - name: Install test requirements 170 | run: 171 | # temporary workaround until pip supports installing dependency-groups: 172 | uvx dependency-groups dev | xargs python -m pip install 173 | 174 | - name: Set up Git identity 175 | run: | 176 | git config --global user.name Name 177 | git config --global user.email foo@bar.com 178 | 179 | - name: Run test suite 180 | working-directory: test 181 | run: 182 | python -m pytest --import-mode=importlib -vvv 183 | 184 | upload_pypi: 185 | name: Upload to PyPI 186 | needs: [build-wheels, build-sdist, test-sdist] 187 | runs-on: ubuntu-latest 188 | permissions: 189 | id-token: write 190 | contents: write 191 | if: github.repository_owner == 'ssciwr' 192 | 193 | steps: 194 | - uses: actions/download-artifact@v4 195 | with: 196 | pattern: artifacts-* 197 | merge-multiple: true 198 | path: dist 199 | 200 | - name: Upload to PyPI 201 | uses: pypa/gh-action-pypi-publish@v1.12.4 202 | if: (startsWith(github.event.ref, 'refs/tags/')) || (github.event.inputs.deploy_to_testpypi == 'false') 203 | 204 | - name: Upload to TestPyPI 205 | uses: pypa/gh-action-pypi-publish@v1.12.4 206 | if: github.event.inputs.deploy_to_testpypi == 'true' 207 | with: 208 | repository-url: https://test.pypi.org/legacy/ 209 | 210 | - name: GitHub release for tagged commits 211 | uses: softprops/action-gh-release@v2 212 | if: startsWith(github.ref, 'refs/tags/') 213 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | wheels/ 23 | share/python-wheels/ 24 | *.egg-info/ 25 | .installed.cfg 26 | *.egg 27 | MANIFEST 28 | 29 | # PyInstaller 30 | # Usually these files are written by a python script from a template 31 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 32 | *.manifest 33 | *.spec 34 | 35 | # Installer logs 36 | pip-log.txt 37 | pip-delete-this-directory.txt 38 | 39 | # Unit test / coverage reports 40 | htmlcov/ 41 | .tox/ 42 | .nox/ 43 | .coverage 44 | .coverage.* 45 | .cache 46 | nosetests.xml 47 | coverage.xml 48 | *.cover 49 | *.py,cover 50 | .hypothesis/ 51 | .pytest_cache/ 52 | cover/ 53 | 54 | # Translations 55 | *.mo 56 | *.pot 57 | 58 | # Django stuff: 59 | *.log 60 | local_settings.py 61 | db.sqlite3 62 | db.sqlite3-journal 63 | 64 | # Flask stuff: 65 | instance/ 66 | .webassets-cache 67 | 68 | # Scrapy stuff: 69 | .scrapy 70 | 71 | # Sphinx documentation 72 | docs/_build/ 73 | 74 | # PyBuilder 75 | .pybuilder/ 76 | target/ 77 | 78 | # Jupyter Notebook 79 | .ipynb_checkpoints 80 | 81 | # IPython 82 | profile_default/ 83 | ipython_config.py 84 | 85 | # pyenv 86 | # For a library or package, you might want to ignore these files since the code is 87 | # intended to run in multiple environments; otherwise, check them in: 88 | # .python-version 89 | 90 | # pipenv 91 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. 92 | # However, in case of collaboration, if having platform-specific dependencies or dependencies 93 | # having no cross-platform support, pipenv may install dependencies that don't work, or not 94 | # install all needed dependencies. 95 | #Pipfile.lock 96 | 97 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow 98 | __pypackages__/ 99 | 100 | # Celery stuff 101 | celerybeat-schedule 102 | celerybeat.pid 103 | 104 | # SageMath parsed files 105 | *.sage.py 106 | 107 | # Environments 108 | .env 109 | .venv 110 | env/ 111 | venv/ 112 | ENV/ 113 | env.bak/ 114 | venv.bak/ 115 | 116 | # Spyder project settings 117 | .spyderproject 118 | .spyproject 119 | 120 | # Rope project settings 121 | .ropeproject 122 | 123 | # mkdocs documentation 124 | /site 125 | 126 | # mypy 127 | .mypy_cache/ 128 | .dmypy.json 129 | dmypy.json 130 | 131 | # Pyre type checker 132 | .pyre/ 133 | 134 | # pytype static type analyzer 135 | .pytype/ 136 | 137 | # Cython debug symbols 138 | cython_debug/ 139 | # Prerequisites 140 | *.d 141 | 142 | # Compiled Object files 143 | *.slo 144 | *.lo 145 | *.o 146 | *.obj 147 | 148 | # Precompiled Headers 149 | *.gch 150 | *.pch 151 | 152 | # Compiled Dynamic libraries 153 | *.so 154 | *.dylib 155 | *.dll 156 | 157 | # Fortran module files 158 | *.mod 159 | *.smod 160 | 161 | # Compiled Static libraries 162 | *.lai 163 | *.la 164 | *.a 165 | *.lib 166 | 167 | # Executables 168 | *.exe 169 | *.out 170 | *.app 171 | 172 | CMakeLists.txt.user 173 | CMakeCache.txt 174 | CMakeFiles 175 | CMakeScripts 176 | Testing 177 | Makefile 178 | cmake_install.cmake 179 | install_manifest.txt 180 | compile_commands.json 181 | CTestTestfile.cmake 182 | _deps 183 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.16...3.31) 2 | project(${SKBUILD_PROJECT_NAME} VERSION ${SKBUILD_PROJECT_VERSION}) 3 | 4 | message(STATUS "clang-format-wheel version: ${SKBUILD_PROJECT_VERSION}") 5 | string(REGEX MATCH "^([0-9]+)\.([0-9]+)\.([0-9]+)" CLANG_FORMAT_VERSION "${SKBUILD_PROJECT_VERSION}") 6 | message(STATUS "clang-format version: ${CLANG_FORMAT_VERSION}") 7 | 8 | # Define a build rule clang-format 9 | set(LLVM_DOWNLOAD_URL "https://github.com/llvm/llvm-project/releases/download/llvmorg-${CLANG_FORMAT_VERSION}/llvm-project-${CLANG_FORMAT_VERSION}.src.tar.xz") 10 | include(ExternalProject) 11 | ExternalProject_add(build-clang-format 12 | URL "${LLVM_DOWNLOAD_URL}" 13 | SOURCE_SUBDIR llvm 14 | SOURCE_DIR ${CMAKE_BINARY_DIR}/llvm-project 15 | BINARY_DIR ${CMAKE_BINARY_DIR}/llvm 16 | UPDATE_COMMAND "" 17 | INSTALL_COMMAND "" 18 | USES_TERMINAL_DOWNLOAD 1 19 | USES_TERMINAL_CONFIGURE 1 20 | USES_TERMINAL_BUILD 1 21 | CMAKE_ARGS -DCMAKE_BUILD_TYPE=Release -DBUILD_SHARED_LIBS=OFF -DLLVM_ENABLE_ZLIB=OFF -DLLVM_ENABLE_ZSTD=OFF -DLLVM_ENABLE_PROJECTS=clang -DLLVM_TARGETS_TO_BUILD= 22 | BUILD_COMMAND ${CMAKE_COMMAND} --build . --target clang-format --config Release 23 | ) 24 | set(config-subfolder "") 25 | if(CMAKE_GENERATOR MATCHES "Visual Studio") 26 | set(config-subfolder "Release") 27 | endif() 28 | set(clang-format-executable ${CMAKE_BINARY_DIR}/llvm/${config-subfolder}/bin/clang-format${CMAKE_EXECUTABLE_SUFFIX}) 29 | 30 | # Reduce the size of the executable by executing strip if it is present on the system 31 | find_program(STRIP_EXECUTABLE strip) 32 | if(STRIP_EXECUTABLE) 33 | add_custom_target( 34 | strip-clang-format 35 | ALL 36 | COMMAND ${STRIP_EXECUTABLE} ${clang-format-executable} 37 | COMMENT "Stripping clang-format executable for size reduction" 38 | ) 39 | add_dependencies(strip-clang-format build-clang-format) 40 | endif() 41 | 42 | # Define an installation rule that copies the executable to our Python package 43 | install( 44 | PROGRAMS 45 | ${clang-format-executable} 46 | DESTINATION clang_format/data/bin 47 | ) 48 | install( 49 | PROGRAMS 50 | ${CMAKE_BINARY_DIR}/llvm-project/clang/tools/clang-format/clang-format-diff.py 51 | RENAME clang_format_diff.py 52 | DESTINATION clang_format 53 | ) 54 | install( 55 | PROGRAMS 56 | ${CMAKE_BINARY_DIR}/llvm-project/clang/tools/clang-format/git-clang-format 57 | RENAME git_clang_format.py 58 | DESTINATION clang_format 59 | ) 60 | -------------------------------------------------------------------------------- /COPYING.md: -------------------------------------------------------------------------------- 1 | Copyright 2021 Dominic Kempf, Heidelberg University and contributors 2 | 3 | Some parts of this project are heavily inspired by the packaging of 4 | Ninja provided by scikit-build under the Apache 2.0 license. The list 5 | of copyright owners for this project can be found here: 6 | https://github.com/scikit-build/ninja-python-distributions/blob/master/AUTHORS.rst 7 | 8 | Licensed under the Apache License, Version 2.0 (the "License"); 9 | you may not use this file except in compliance with the License. 10 | You may obtain a copy of the License at 11 | 12 | http://www.apache.org/licenses/LICENSE-2.0 13 | 14 | Unless required by applicable law or agreed to in writing, software 15 | distributed under the License is distributed on an "AS IS" BASIS, 16 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | See the License for the specific language governing permissions and 18 | limitations under the License. 19 | 20 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, and 10 | distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by the copyright 13 | owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all other entities 16 | that control, are controlled by, or are under common control with that entity. 17 | For the purposes of this definition, "control" means (i) the power, direct or 18 | indirect, to cause the direction or management of such entity, whether by 19 | contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the 20 | outstanding shares, or (iii) beneficial ownership of such entity. 21 | 22 | "You" (or "Your") shall mean an individual or Legal Entity exercising 23 | permissions granted by this License. 24 | 25 | "Source" form shall mean the preferred form for making modifications, including 26 | but not limited to software source code, documentation source, and configuration 27 | files. 28 | 29 | "Object" form shall mean any form resulting from mechanical transformation or 30 | translation of a Source form, including but not limited to compiled object code, 31 | generated documentation, and conversions to other media types. 32 | 33 | "Work" shall mean the work of authorship, whether in Source or Object form, made 34 | available under the License, as indicated by a copyright notice that is included 35 | in or attached to the work (an example is provided in the Appendix below). 36 | 37 | "Derivative Works" shall mean any work, whether in Source or Object form, that 38 | is based on (or derived from) the Work and for which the editorial revisions, 39 | annotations, elaborations, or other modifications represent, as a whole, an 40 | original work of authorship. For the purposes of this License, Derivative Works 41 | shall not include works that remain separable from, or merely link (or bind by 42 | name) to the interfaces of, the Work and Derivative Works thereof. 43 | 44 | "Contribution" shall mean any work of authorship, including the original version 45 | of the Work and any modifications or additions to that Work or Derivative Works 46 | thereof, that is intentionally submitted to Licensor for inclusion in the Work 47 | by the copyright owner or by an individual or Legal Entity authorized to submit 48 | on behalf of the copyright owner. For the purposes of this definition, 49 | "submitted" means any form of electronic, verbal, or written communication sent 50 | to the Licensor or its representatives, including but not limited to 51 | communication on electronic mailing lists, source code control systems, and 52 | issue tracking systems that are managed by, or on behalf of, the Licensor for 53 | the purpose of discussing and improving the Work, but excluding communication 54 | that is conspicuously marked or otherwise designated in writing by the copyright 55 | owner as "Not a Contribution." 56 | 57 | "Contributor" shall mean Licensor and any individual or Legal Entity on behalf 58 | of whom a Contribution has been received by Licensor and subsequently 59 | incorporated within the Work. 60 | 61 | 2. Grant of Copyright License. 62 | 63 | Subject to the terms and conditions of this License, each Contributor hereby 64 | grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, 65 | irrevocable copyright license to reproduce, prepare Derivative Works of, 66 | publicly display, publicly perform, sublicense, and distribute the Work and such 67 | Derivative Works in Source or Object form. 68 | 69 | 3. Grant of Patent License. 70 | 71 | Subject to the terms and conditions of this License, each Contributor hereby 72 | grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, 73 | irrevocable (except as stated in this section) patent license to make, have 74 | made, use, offer to sell, sell, import, and otherwise transfer the Work, where 75 | such license applies only to those patent claims licensable by such Contributor 76 | that are necessarily infringed by their Contribution(s) alone or by combination 77 | of their Contribution(s) with the Work to which such Contribution(s) was 78 | submitted. If You institute patent litigation against any entity (including a 79 | cross-claim or counterclaim in a lawsuit) alleging that the Work or a 80 | Contribution incorporated within the Work constitutes direct or contributory 81 | patent infringement, then any patent licenses granted to You under this License 82 | for that Work shall terminate as of the date such litigation is filed. 83 | 84 | 4. Redistribution. 85 | 86 | You may reproduce and distribute copies of the Work or Derivative Works thereof 87 | in any medium, with or without modifications, and in Source or Object form, 88 | provided that You meet the following conditions: 89 | 90 | You must give any other recipients of the Work or Derivative Works a copy of 91 | this License; and 92 | You must cause any modified files to carry prominent notices stating that You 93 | changed the files; and 94 | You must retain, in the Source form of any Derivative Works that You distribute, 95 | all copyright, patent, trademark, and attribution notices from the Source form 96 | of the Work, excluding those notices that do not pertain to any part of the 97 | Derivative Works; and 98 | If the Work includes a "NOTICE" text file as part of its distribution, then any 99 | Derivative Works that You distribute must include a readable copy of the 100 | attribution notices contained within such NOTICE file, excluding those notices 101 | that do not pertain to any part of the Derivative Works, in at least one of the 102 | following places: within a NOTICE text file distributed as part of the 103 | Derivative Works; within the Source form or documentation, if provided along 104 | with the Derivative Works; or, within a display generated by the Derivative 105 | Works, if and wherever such third-party notices normally appear. The contents of 106 | the NOTICE file are for informational purposes only and do not modify the 107 | License. You may add Your own attribution notices within Derivative Works that 108 | You distribute, alongside or as an addendum to the NOTICE text from the Work, 109 | provided that such additional attribution notices cannot be construed as 110 | modifying the License. 111 | You may add Your own copyright statement to Your modifications and may provide 112 | additional or different license terms and conditions for use, reproduction, or 113 | distribution of Your modifications, or for any such Derivative Works as a whole, 114 | provided Your use, reproduction, and distribution of the Work otherwise complies 115 | with the conditions stated in this License. 116 | 117 | 5. Submission of Contributions. 118 | 119 | Unless You explicitly state otherwise, any Contribution intentionally submitted 120 | for inclusion in the Work by You to the Licensor shall be under the terms and 121 | conditions of this License, without any additional terms or conditions. 122 | Notwithstanding the above, nothing herein shall supersede or modify the terms of 123 | any separate license agreement you may have executed with Licensor regarding 124 | such Contributions. 125 | 126 | 6. Trademarks. 127 | 128 | This License does not grant permission to use the trade names, trademarks, 129 | service marks, or product names of the Licensor, except as required for 130 | reasonable and customary use in describing the origin of the Work and 131 | reproducing the content of the NOTICE file. 132 | 133 | 7. Disclaimer of Warranty. 134 | 135 | Unless required by applicable law or agreed to in writing, Licensor provides the 136 | Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, 137 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, 138 | including, without limitation, any warranties or conditions of TITLE, 139 | NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are 140 | solely responsible for determining the appropriateness of using or 141 | redistributing the Work and assume any risks associated with Your exercise of 142 | permissions under this License. 143 | 144 | 8. Limitation of Liability. 145 | 146 | In no event and under no legal theory, whether in tort (including negligence), 147 | contract, or otherwise, unless required by applicable law (such as deliberate 148 | and grossly negligent acts) or agreed to in writing, shall any Contributor be 149 | liable to You for damages, including any direct, indirect, special, incidental, 150 | or consequential damages of any character arising as a result of this License or 151 | out of the use or inability to use the Work (including but not limited to 152 | damages for loss of goodwill, work stoppage, computer failure or malfunction, or 153 | any and all other commercial damages or losses), even if such Contributor has 154 | been advised of the possibility of such damages. 155 | 156 | 9. Accepting Warranty or Additional Liability. 157 | 158 | While redistributing the Work or Derivative Works thereof, You may choose to 159 | offer, and charge a fee for, acceptance of support, warranty, indemnity, or 160 | other liability obligations and/or rights consistent with this License. However, 161 | in accepting such obligations, You may act only on Your own behalf and on Your 162 | sole responsibility, not on behalf of any other Contributor, and only if You 163 | agree to indemnify, defend, and hold each Contributor harmless for any liability 164 | incurred by, or claims asserted against, such Contributor by reason of your 165 | accepting any such warranty or additional liability. 166 | 167 | END OF TERMS AND CONDITIONS 168 | 169 | APPENDIX: How to apply the Apache License to your work 170 | 171 | To apply the Apache License to your work, attach the following boilerplate 172 | notice, with the fields enclosed by brackets "[]" replaced with your own 173 | identifying information. (Don't include the brackets!) The text should be 174 | enclosed in the appropriate comment syntax for the file format. We also 175 | recommend that a file or class name and description of purpose be included on 176 | the same "printed page" as the copyright notice for easier identification within 177 | third-party archives. 178 | 179 | Copyright [yyyy] [name of copyright owner] 180 | 181 | Licensed under the Apache License, Version 2.0 (the "License"); 182 | you may not use this file except in compliance with the License. 183 | You may obtain a copy of the License at 184 | 185 | http://www.apache.org/licenses/LICENSE-2.0 186 | 187 | Unless required by applicable law or agreed to in writing, software 188 | distributed under the License is distributed on an "AS IS" BASIS, 189 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 190 | See the License for the specific language governing permissions and 191 | limitations under the License. 192 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # clang-format Python distribution 2 | 3 | [![PyPI Release](https://img.shields.io/pypi/v/clang-format.svg)](https://pypi.org/project/clang-format) 4 | 5 | This project packages the `clang-format` utility as a Python package. It allows you to install `clang-format` directly from PyPI: 6 | 7 | ``` 8 | python -m pip install clang-format 9 | ``` 10 | 11 | This projects intends to release a new PyPI package for each major and minor release of `clang-format`. 12 | 13 | ## Use with pipx 14 | 15 | You can use `pipx` to run clang-format, as well. For example, `pipx run clang-format ` 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`). 16 | 17 | ## Use from pre-commit 18 | 19 | A [pre-commit](https://pre-commit.com) hook is also [provided](https://github.com/pre-commit/mirrors-clang-format), use like this: 20 | 21 | ```yaml 22 | - repo: https://github.com/pre-commit/mirrors-clang-format 23 | rev: v20.1.5 24 | hooks: 25 | - id: clang-format 26 | types_or: [c++, c, cuda] 27 | ``` 28 | 29 | In contrast to many other pre-commit hooks, the versioning of the hook matches the versioning of `clang-format`. 30 | 31 | 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](https://github.com/ssciwr/clang-format-hook) that also receives backports of older versions of clang-format. 32 | Currently, all major/minor versions of LLVM >= 10 are supported. 33 | 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. 34 | 35 | ## Building new releases 36 | 37 | The [clang-format-wheel repository](https://github.com/ssciwr/clang-format-wheel) provides the logic to build and publish binary wheels of the `clang-format` utility. 38 | 39 | In order to add a new release, the following steps are necessary: 40 | 41 | * Edit the [version file](https://github.com/ssciwr/clang-format-wheel/blob/main/clang-format_version.txt) 42 | * In the form `llvm_version.wheel_version`, e.g. `18.0.2.1` 43 | * Tag the commit with this version to trigger the [GitHub Actions release workflow](https://github.com/ssciwr/clang-format-wheel/actions/workflows/release.yml) 44 | * e.g. `git tag v18.0.2.1 && git push origin v18.0.2.1` 45 | 46 | Alternatively, the workflow can be triggered manually: 47 | 48 | On manual triggers, the following input variables are available: 49 | * `llvm_version`: Override the LLVM version (default: `""`) 50 | * `wheel_version`: Override the wheel packaging version (default `"0"`) 51 | * `skip_emulation`: Set which emulation builds to skip, e.g. `"qemu"` (default: `""`) 52 | * `deploy_to_testpypi`: Whether to deploy to TestPyPI instead of PyPI (default: `false`) 53 | 54 | The repository with the precommit hook is automatically updated using a scheduled Github Actions workflow. 55 | 56 | ## Acknowledgements 57 | 58 | This repository extends the great work of several other projects: 59 | 60 | * `clang-format` itself is [provided by the LLVM project](https://github.com/llvm/llvm-project) under the Apache 2.0 License with LLVM exceptions. 61 | * The build logic is based on [scikit-build-core](https://github.com/scikit-build/scikit-build-core) which greatly reduces the amount of low level code necessary to package `clang-format`. 62 | * The `scikit-build` packaging examples of [CMake](https://github.com/scikit-build/cmake-python-distributions) and [Ninja](https://github.com/scikit-build/ninja-python-distributions) were very helpful in packaging `clang-format`. 63 | * The CI build process is controlled by [cibuildwheel](https://github.com/pypa/cibuildwheel) which makes building wheels across a number of platforms a pleasant experience (!) 64 | 65 | Special thanks goes to mgevaert who initiated this project and maintained it until 2021. 66 | 67 | We are grateful for the generous provisioning with CI resources that GitHub currently offers to Open Source projects. 68 | -------------------------------------------------------------------------------- /clang-format_version.txt: -------------------------------------------------------------------------------- 1 | 20.1.5.0 2 | -------------------------------------------------------------------------------- /clang_format/__init__.py: -------------------------------------------------------------------------------- 1 | import os 2 | import subprocess 3 | import sys 4 | import sysconfig 5 | 6 | 7 | def get_executable(name): 8 | return os.path.join(os.path.dirname(__file__), "data", "bin", name + sysconfig.get_config_var("EXE")) 9 | 10 | def _run(name): 11 | executable = get_executable(name) 12 | return subprocess.call([executable] + sys.argv[1:]) 13 | 14 | def clang_format(): 15 | raise SystemExit(_run("clang-format")) 16 | -------------------------------------------------------------------------------- /pyproject.toml: -------------------------------------------------------------------------------- 1 | [build-system] 2 | requires = ["scikit-build-core"] 3 | build-backend = "scikit_build_core.build" 4 | 5 | [project] 6 | name = "clang-format" 7 | dynamic = ["version"] 8 | authors = [{name = "Dominic Kempf", email = "ssc@iwr.uni-heidelberg.de"}] 9 | license = { text = "Apache 2.0" } 10 | description = "Clang-Format is an LLVM-based code formatting tool" 11 | readme = "README.md" 12 | classifiers = [ 13 | "Programming Language :: C", 14 | "Programming Language :: C++", 15 | "Operating System :: OS Independent", 16 | "License :: OSI Approved :: Apache Software License", 17 | "Intended Audience :: Developers", 18 | "Topic :: Software Development :: Quality Assurance", 19 | ] 20 | 21 | [tool.scikit-build.metadata.version] 22 | provider = "scikit_build_core.metadata.regex" 23 | regex = '^(?P\d+\.\d+\.\d+(\.[1-9]\d*)?)' 24 | input = "clang-format_version.txt" 25 | 26 | [project.urls] 27 | Clang = "http://clang.llvm.org/" 28 | Documentation = "https://clang.llvm.org/docs/ClangFormat.html" 29 | Download = "https://github.com/llvm/llvm-project/releases" 30 | Source = "https://github.com/ssciwr/clang-format-wheel" 31 | 32 | [project.scripts] 33 | "clang-format" = "clang_format:clang_format" 34 | "git-clang-format" = "clang_format.git_clang_format:main" 35 | "clang-format-diff.py" = "clang_format.clang_format_diff:main" 36 | 37 | [tool.scikit-build] 38 | wheel.packages = ["clang_format"] 39 | wheel.py-api = "py2.py3" 40 | cmake.version = ">=3.16.0" 41 | ninja.version = ">=1.10.0" 42 | build.verbose = true 43 | logging.level = "DEBUG" 44 | 45 | [dependency-groups] 46 | dev = ["pytest", "pytest-git"] 47 | 48 | [tool.pytest.ini_options] 49 | # use importlib pytest import mode to avoid adding local directory to sys.path 50 | addopts = "--import-mode=importlib" 51 | 52 | [tool.cibuildwheel] 53 | # Super-verbose output for debugging purpose 54 | build-verbosity = 3 55 | # Set CMAKE_GENERATOR env var which is respected by scikit-build-core to use Ninja on all platforms 56 | environment = "CMAKE_GENERATOR=Ninja" 57 | 58 | # Testing commands for our wheels 59 | test-groups = ["dev"] 60 | before-test = [ 61 | "git config --global user.name Name", 62 | "git config --global user.email foo@bar.com" 63 | ] 64 | test-command = "pytest {package}/test -vvv" 65 | -------------------------------------------------------------------------------- /test/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ssciwr/clang-format-wheel/ab7ce725b5028065125850e312e30c6c2bf78f9b/test/__init__.py -------------------------------------------------------------------------------- /test/helloworld.cc: -------------------------------------------------------------------------------- 1 | #include 2 | int main(){std::cout<< "Hello World"< 2 | int main() { 3 | std::cout << "Hello World" << std::endl; 4 | 5 | return 0; 6 | } -------------------------------------------------------------------------------- /test/test_clang_format.py: -------------------------------------------------------------------------------- 1 | import filecmp 2 | import os 3 | import pytest 4 | import subprocess 5 | import tempfile 6 | import pathlib 7 | import clang_format 8 | 9 | 10 | @pytest.fixture 11 | def repo(tmp_path, monkeypatch): 12 | monkeypatch.chdir(tmp_path) 13 | subprocess.run("git init", shell=True) 14 | return tmp_path 15 | 16 | 17 | @pytest.mark.parametrize("command", ["clang-format", clang_format.get_executable('clang-format')]) 18 | @pytest.mark.parametrize("testcase", [("helloworld.cc", "helloworld_format.cc")]) 19 | def test_clang_format(command, testcase): 20 | # Get full paths to the test data 21 | test_input, test_output = testcase 22 | test_input = os.path.join(os.path.dirname(__file__), test_input) 23 | test_output = os.path.join(os.path.dirname(__file__), test_output) 24 | 25 | with tempfile.TemporaryDirectory() as tmp: 26 | outname = os.path.join(tmp, "formatted") 27 | with open(outname, "w") as out: 28 | subprocess.run([command, test_input], stdout=out, check=True) 29 | 30 | # Check that the content is equal 31 | assert filecmp.cmp(outname, test_output) 32 | 33 | 34 | def test_git_clang_format(repo): 35 | # Test whether the git-clang-format tool is properly executable 36 | # on an empty git repository. 37 | 38 | # Create a commit with an empty file 39 | open(repo / "test", "w").close() 40 | subprocess.run("git add test", shell=True) 41 | subprocess.run("git commit -m initial", shell=True) 42 | 43 | # Check that the clang-format tool runs on the test repo 44 | subprocess.run("git clang-format", shell=True) 45 | 46 | 47 | def test_get_executable(): 48 | clang_format_exe = pathlib.Path(clang_format.get_executable('clang-format')) 49 | assert clang_format_exe.exists() 50 | assert os.access(clang_format_exe, os.X_OK) 51 | --------------------------------------------------------------------------------