Skip to content

Instantly share code, notes, and snippets.

@bvigueras
Forked from ffeu/clang-format@7.rb
Last active August 28, 2021 14:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save bvigueras/daf11aee6876fb9ba4c925c2c31bc04b to your computer and use it in GitHub Desktop.
Save bvigueras/daf11aee6876fb9ba4c925c2c31bc04b to your computer and use it in GitHub Desktop.
clang-format version 6 (6.0.0) formula for brew
class ClangFormatAT6 < Formula
desc "Formatting tool for C/C++/Java/JavaScript/Objective-C/Protobuf"
homepage "https://releases.llvm.org/6.0.1/tools/clang/docs/ClangFormat.html"
version "6.0.1"
if MacOS.version >= :sierra
url "https://llvm.org/svn/llvm-project/llvm/tags/RELEASE_601/final/", :using => :svn
else
url "http://llvm.org/svn/llvm-project/llvm/tags/RELEASE_601/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_601/final/", :using => :svn
else
url "http://llvm.org/svn/llvm-project/cfe/tags/RELEASE_601/final/", :using => :svn
end
end
resource "libcxx" do
url "https://releases.llvm.org/6.0.1/libcxx-6.0.1.src.tar.xz"
sha256 "7654fbc810a03860e6f01a54c2297a0b9efb04c0b9aa0409251d9bdb3726fc67"
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
@bvigueras
Copy link
Author

bvigueras commented Nov 26, 2019

To install this, download this formula locally and install it with brew. The formula will download the original clang-format code (version 6.0.1) 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/bvigueras/daf11aee6876fb9ba4c925c2c31bc04b/raw/526ff0eebbc0476f568c852a8cc5d4cc48281475/clang-format@6.rb -o $(brew --repo)/Library/Taps/homebrew/homebrew-core/Formula/clang-format@6.rb

brew install clang-format@6

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 6.

brew unlink clang-format
brew link clang-format@6

Validating

Run:

clang-format --version

If the result is:

clang-format version 6.0.1 (tags/RELEASE_601/final)

You're all set!!

Uninstalling

To uninstall it:

brew uninstall clang-format@6

@mmathys
Copy link

mmathys commented Jun 15, 2020

thanks so much

@jayvdb
Copy link

jayvdb commented Jun 9, 2021

@Ceylo
Copy link

Ceylo commented Aug 28, 2021

With your updated version and latest Homebrew, the install commands have become:

curl https://gist.githubusercontent.com/jayvdb/3e680baf7c265d0b1bfe704d72315e49/raw/f0879bc14746370a358ef8fe266e268c36f09a89/clang-format@6.rb -o $(brew --repo)/Library/Taps/homebrew/homebrew-core/Formula/clang-format@6.rb
brew install clang-format@6 --build-from-source

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