Skip to content

Instantly share code, notes, and snippets.

@ffeu
Last active May 17, 2022 04:50
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ffeu/6ffb75d8e8c7d92c0fbeb4b036599c33 to your computer and use it in GitHub Desktop.
Save ffeu/6ffb75d8e8c7d92c0fbeb4b036599c33 to your computer and use it in GitHub Desktop.
EDIT 2021: NOT WORKING ANYMORE!! clang-format version 5 (5.0.2) formula for brew (check comment below on how to install it)
class ClangFormatAT5 < Formula
desc "Formatting tool for C/C++/Java/JavaScript/Objective-C/Protobuf"
homepage "https://releases.llvm.org/5.0.2/tools/clang/docs/ClangFormat.html"
version "5.0.2"
if MacOS.version >= :sierra
url "https://llvm.org/svn/llvm-project/llvm/tags/RELEASE_502/final/", :using => :svn
else
url "http://llvm.org/svn/llvm-project/llvm/tags/RELEASE_502/final/", :using => :svn
end
depends_on "cmake" => :build
depends_on "ninja" => :build
depends_on "subversion" => :build
resource "clang" do
if MacOS.version >= :sierra
url "https://llvm.org/svn/llvm-project/cfe/tags/RELEASE_502/final/", :using => :svn
else
url "http://llvm.org/svn/llvm-project/cfe/tags/RELEASE_502/final/", :using => :svn
end
end
resource "libcxx" do
url "https://releases.llvm.org/5.0.0/libcxx-5.0.0.src.tar.xz"
sha256 "eae5981e9a21ef0decfcac80a1af584ddb064a32805f95a57c7c83a5eb28c9b1"
end
def install
(buildpath/"projects/libcxx").install resource("libcxx")
(buildpath/"tools/clang").install resource("clang")
mkdir "build" do
args = std_cmake_args
args << "-DCMAKE_OSX_SYSROOT=/" unless MacOS::Xcode.installed?
args << "-DLLVM_ENABLE_LIBCXX=ON"
args << ".."
system "cmake", "-G", "Ninja", *args
system "ninja", "clang-format"
bin.install "bin/clang-format"
end
bin.install "tools/clang/tools/clang-format/git-clang-format"
(share/"clang").install Dir["tools/clang/tools/clang-format/clang-format*"]
end
test do
# NB: below C code is messily formatted on purpose.
(testpath/"test.c").write <<~EOS
int main(char *args) { \n \t printf("hello"); }
EOS
assert_equal "int main(char *args) { printf(\"hello\"); }\n",
shell_output("#{bin}/clang-format -style=Google test.c")
# below code is messily formatted on purpose.
(testpath/"test2.h").write <<~EOS
#import "package/file.h"
@interface SomePlugin : NSObject < ParentPlugin >
@end
EOS
# NOTE! different formatting depending on version
# clang-format 5.x
# @interface SomePlugin : NSObject<ParentPlugin>
# clang-format 6.x, 7.x
# @interface SomePlugin : NSObject <ParentPlugin>
assert_equal "#import \"package/file.h\"\n@interface SomePlugin : NSObject<ParentPlugin>\n@end\n",
shell_output("#{bin}/clang-format -style=Google test2.h")
end
end
@ffeu
Copy link
Author

ffeu commented Nov 16, 2018

Before you ask me why I didn't PR this to Homebrew, see this. So basically they don't want version 5 to be there, even with a lot of people requesting it.

Yeah, I'm frustrated, specially because I was a mission to fix an error in brew to be able to PR that! :)

Nah, that's life!

@ffeu
Copy link
Author

ffeu commented Nov 16, 2018

To install this, download this formula locally and install it with brew. The formula will download the original clang-format code (version 5.0.2) from their SVN and build it locally - it takes about 10 minutes.

Installing

These are the commands to download and install:

curl https://gist.githubusercontent.com/ffeu/6ffb75d8e8c7d92c0fbeb4b036599c33/raw/7004b4955961528a22b4642c125d395ef7e054c7/clang-format@5.rb -o $(brew --repo)/Library/Taps/homebrew/homebrew-core/Formula/clang-format@5.rb

brew install clang-format@5

If you already have clang-format installed (for instance with a newer version), you'll need to run an additional command, that keep both versions installed but switches the clang-format command line to point to the version 5.

brew unlink clang-format
brew link clang-format@5

Validating

Run:

clang-format --version

If the result is:

clang-format version 5.0.2 (tags/RELEASE_502/final)

You're all set!!

Uninstalling

To uninstall it:

brew uninstall clang-format@5

@kousu
Copy link

kousu commented May 14, 2019

Thanks for this, I appreciate it. Alpine is on clang-format v5 so anything done in a container pretty much is going to want it.

@feuerste
Copy link

Thanks a lot @ffeu for this, unfortunately this doesn't work any more, as llvm moved to github

@ffeu
Copy link
Author

ffeu commented Mar 12, 2021

@feuerste thanks for letting me know

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