63

I am having a hard time finding clang-format and clang-format.py on my Mac OS 10.8.5 with Xcode 5 and its Command Line Tools. I tried find / -name "clang-format.py" without luck. Are they already here somewhere hiding, or how would I get this toolchain?

(I'm surprised it doesn't seem readily available since clang is Apple's compiler---in that Apple uses it.)

UpAndAdam
  • 4,515
  • 3
  • 28
  • 46
Ahmed Fasih
  • 6,458
  • 7
  • 54
  • 95

7 Answers7

125

Seems like Apple doesn't ship clang-format with the Command Line Tools. Right now you have at least three options:

  • Using Homebrew (That's the way to go)
  • Using prebuild packages
  • Build it yourself

Homebrew

As user johnhaley81 pointed out, clang-format is now in homebrew:

clang-format is now on brew. brew install clang-format

Please consider upvoting his post if you find this answer useful.


Prebuild packages

Pre-build bundles are available at llvm.org. For Mac OSX, you need to download the darwin package.


Build instructions:

Here's an instruction on how to do this: http://nacho4d-nacho4d.blogspot.de/2013/11/clang-format.html

And here's another one: http://blog.hardcodes.de/articles/63/building-clang-format-and-friends-on-osx-mountain-lion

Community
  • 1
  • 1
  • The first flag was required and I think the second flag sped things up by skipping the Debug build: `./llvm/configure --disable-compiler-version-checks --enable-optimized` but other than that, it worked as advertised. Thanks! – Ahmed Fasih Jan 17 '14 at 20:36
  • I struggled to get it installed (initially), wrote up a post with some helpful tips (mostly for myself) http://staxmanade.com/2015/01/how-to-install-clang-format-and-formatting-objective-c-files/ – Jason Jarrett Jan 26 '15 at 22:32
  • 9
    clang-format is now on brew. `brew install clang-format` – johnhaley81 Feb 04 '15 at 15:28
  • `clang-format-diff.py` the problem is, it doesn't find one in your environment – Denis Denisov Mar 17 '15 at 11:35
  • Do `brew update` if homebrew can't find it at first. – Andrew May 03 '15 at 23:50
  • 3
    Even with fresh `brew update`, brew doesn't seem to install clang-format.py for use with vim. I got it from the LLVM website -> "Clang source code" under `tools/clang-format/clang-format.py` (the `el` version is also there, for emacs). – Ahmed Fasih May 07 '15 at 22:54
  • brew install clang-format , that didn't wok on macOS 10.12.6 and Xcode 8.3.3 – Vittore Marcas Dec 14 '19 at 10:19
  • Homebrew is the way to go, sure, if you want it to take ownership of a directory that shouldn't have a user owner....Bad idea though I know many would say there's no risk or it doesn't matter or they don't care. – Pryftan Jan 13 '20 at 18:50
53

You can use brew now to install clang-format.

brew install clang-format

johnhaley81
  • 1,153
  • 10
  • 14
4

I know this is not a direct answer to the question but if you are just looking to format your code in Xcode. I just used this plug in.

https://github.com/travisjeffery/ClangFormat-Xcode

Gustavo Barcena
  • 396
  • 2
  • 8
  • 1
    Currently I am not (emacs + vim user), but this is certainly handy to know about. – Ahmed Fasih Jan 19 '14 at 21:20
  • 4
    If you use this plugin you can find clang-format in the installed plugin at: ~/Library/Application Support/Developer/Shared/Xcode/Plug-ins/ClangFormat.xcplugin/Contents/Resources/clang-format – Shane Arney Jun 26 '14 at 15:31
4

Specific versions of clang-format

As of november/2018, current clang-format formula on Homebrew points to version 8.0.0. Run clang-format --version to check yours.

If you need older versions of clang-format, consider following these steps:

Installing clang-format version 5 (5.0.2) with homebrew

Installing clang-format version 7 (7.0.0) with homebrew

Feu
  • 5,372
  • 1
  • 31
  • 57
  • Hi @heLomaN , the default `clang-format` on brew installs the latest version from 2018-12-18. (check the [PR](https://github.com/Homebrew/homebrew-core/pull/35794)). – Feu Feb 18 '19 at 12:39
4

In my case I installed clang-format with brew install and located here: /usr/local/bin/clang-format

2

If you have the llvm toolchain already installed, you can find the clang-format.py file in /usr/local/opt/llvm/share/clang/clang-format.py without having to install a separate clang-format binary through Homebrew.

0

If you have installed clang-format via brew, the clang-format has been downloaded under

(Just use my MacOS12.3.1+XCode13.4+clang-format@5.0 with upgrade from macOS10.12.6+Xcode8+clang-format@5.0 as an example)

/usr/local/Cellar/clang-format@5/

with it's executable file clang-format under

/usr/local/Cellar/clang-format@5/5.0.2/bin/clang-format

with it's configuration file clang-format.py under

/usr/local/Cellar/clang-format@5/5.0.2/share/clang/clang-format.py

ALso,

the clang-format@5.0 will create soft link executable file clang-format under

/usr/local/bin/clang-format -> ../Cellar/clang-format@5/5.0.2/bin/clang-format

(that's why while I check where the clang-format is no matter using where clang-format or which clang-format or even whereis clang-format, that always shows /usr/local/bin/clang-format.)

(If you want to check where soft link /usr/local/bin/clang-format linked to? Just use the command to check cd /usr/local/bin/ && pwd && ls -al | grep "clang-format", liking mine showing:)

/usr/local/bin/clang-format -> ../Cellar/clang-format@5/5.0.2/bin/clang-format

and the clang-format@5.0 soft link create configuration file clang-format.py under

/usr/local/share/clang/clang-format.py

the upstairs soft link linked to

/usr/local/Cellar/clang-format@5/5.0.2/share/clang/clang-format.py

Others,

somebody's download clang-format@3.8 maybe under

/usr/local/Cellar/clang-format

with executable file clang-formatunder

/usr/local/Cellar/clang-format/2019-01-18/bin/clang-format

with configuration file clang-format.py under

/usr/local/Cellar/clang-format/2019-01-18/share/clang/clang-format.py

withe the upstairs soft link clang-format.py under

/usr/share/clang/clang-format-3.8/clang-format.py
Vittore Marcas
  • 1,007
  • 8
  • 11