Skip to content

[Windows] Add git-clang-format wrapper bat file #69228

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 1 commit into from
Oct 21, 2023

Conversation

llvm-beanz
Copy link
Collaborator

@llvm-beanz llvm-beanz commented Oct 16, 2023

This allows git-clang-format to be used on a Windows terminal without manually needing to find the path and invoke the python interpreter. We have a similar script for scan-build.

Fixes #69643

This allows git-clang-format to be used on a Windows terminal without
manually needing to find the path and invoke the python interpreter. We
have a similar script for `scan-build`.
@llvmbot
Copy link
Member

llvmbot commented Oct 16, 2023

@llvm/pr-subscribers-clang-format

Author: Chris B (llvm-beanz)

Changes

This allows git-clang-format to be used on a Windows terminal without manually needing to find the path and invoke the python interpreter. We have a similar script for scan-build.


Full diff: https://github.com/llvm/llvm-project/pull/69228.diff

2 Files Affected:

  • (modified) clang/tools/clang-format/CMakeLists.txt (+6)
  • (added) clang/tools/clang-format/git-clang-format.bat (+1)
diff --git a/clang/tools/clang-format/CMakeLists.txt b/clang/tools/clang-format/CMakeLists.txt
index 30c93f8667c8359..1c61a3c8fb80368 100644
--- a/clang/tools/clang-format/CMakeLists.txt
+++ b/clang/tools/clang-format/CMakeLists.txt
@@ -38,3 +38,9 @@ install(FILES clang-format.py
 install(PROGRAMS git-clang-format
   DESTINATION "${CMAKE_INSTALL_BINDIR}"
   COMPONENT clang-format)
+
+if (WIN32 AND NOT CYGWIN)
+  install(PROGRAMS git-clang-format.bat
+    DESTINATION "${CMAKE_INSTALL_BINDIR}"
+    COMPONENT clang-format)
+endif()
diff --git a/clang/tools/clang-format/git-clang-format.bat b/clang/tools/clang-format/git-clang-format.bat
new file mode 100644
index 000000000000000..d4bc5172989cb09
--- /dev/null
+++ b/clang/tools/clang-format/git-clang-format.bat
@@ -0,0 +1 @@
+py -3 git-clang-format %*

@owenca
Copy link
Contributor

owenca commented Oct 16, 2023

This allows git-clang-format to be used on a Windows terminal without manually needing to find the path and invoke the python interpreter. We have a similar script for scan-build.

Can you open an issue and link to it from here? It seems git clang-format works in Command Prompt without this new batch file.

@llvm-beanz
Copy link
Collaborator Author

Can you open an issue and link to it from here?

Filed #69643.

It seems git clang-format works in Command Prompt without this new batch file.

IIUC, git clang-format works if you have WSL installed, but not with a default Windows installation. We definitely have users on my team that are Windows-only users who encounter an error running that command even if the git-clang-format script is on the path, and the presence of the .bat file solves the issue.

@owenca
Copy link
Contributor

owenca commented Oct 20, 2023

@mydeveloperday what do you think?

@@ -0,0 +1 @@
py -3 git-clang-format %*
Copy link
Contributor

Choose a reason for hiding this comment

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

What is py? I have no program called py on my machine (Windows with msys2), so you would possibly break that? Why not write python or python3?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

py.exe is the Windows python launcher. It comes with Python for Windows (not msys2 or cygwin where the shebang line would work). It is the Windows way of choosing which python version you want. You need to use this approach because the standard Python for Windows install always installs python.exe regardless of version because Windows generally doesn't version binaries.

[1] Python for Windows docs
[2] Microsoft docs

I'm going to go take a long shower now to try and forget that I learned this much about Windows.

Copy link
Contributor

Choose a reason for hiding this comment

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

@llvm-beanz This doesn't seem to work if git-clang-format is run from another directory:

C:\Users\Owen\llvm-project>clang\tools\clang-format\git-clang-format

C:\Users\Owen\llvm-project>py -3 git-clang-format
C:\Users\Owen\AppData\Local\Programs\Python\Python39\python.exe: can't open file 'C:\Users\Owen\llvm-project\git-clang-format': [Errno 2] No such file or directory

C:\Users\Owen\llvm-project>

@HazardyKnusperkeks
Copy link
Contributor

I have no real opinion on this matter, as I don't use that program/script. But when it helps the windows users, why not? I also see no harm.

@llvm-beanz llvm-beanz merged commit 7d7e4d2 into llvm:main Oct 21, 2023
Guzhu-AMD pushed a commit to GPUOpen-Drivers/llvm-project that referenced this pull request Oct 26, 2023
Local branch amd-gfx b98ebf3 Merged main:c2f02e3bd6a5 into amd-gfx:f41124e56306
Remote branch main 7d7e4d2 [Windows] Add git-clang-format wrapper bat file (llvm#69228)
@owenca
Copy link
Contributor

owenca commented Dec 13, 2023

See #75268.

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

Successfully merging this pull request may close these issues.

git clang-format doesn't work on stock Windows
5 participants