-
-
Notifications
You must be signed in to change notification settings - Fork 12.1k
/
llvm@8.rb
433 lines (375 loc) 路 16.9 KB
/
llvm@8.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
class LlvmAT8 < Formula
desc "Next-gen compiler infrastructure"
homepage "https://llvm.org/"
url "https://github.com/llvm/llvm-project/releases/download/llvmorg-8.0.1/llvm-8.0.1.src.tar.xz"
sha256 "44787a6d02f7140f145e2250d56c9f849334e11f9ae379827510ed72f12b75e7"
license "NCSA"
revision 5
bottle do
sha256 cellar: :any, monterey: "045e8e7752cd57438c1ec0c7e6b403ca4de060e21cfa375d1368513acc86ce8e"
sha256 cellar: :any, big_sur: "08e7a41cb1ed469dfd676885680c0199c1ab075974d676773ec0e760e62af176"
sha256 cellar: :any, catalina: "d9b55390fcc29bf569ea4f08e9f9be44022926050f5a3e1cf9dcb557be7bd4b1"
sha256 cellar: :any_skip_relocation, x86_64_linux: "8e7e074d5e5c10ea82be31a9c1081d9cfd1e20972ba03e21f9a3a82379e207ee"
end
# Clang cannot find system headers if Xcode CLT is not installed
pour_bottle? only_if: :clt_installed
keg_only :versioned_formula
disable! date: "2023-06-19", because: :versioned_formula
# https://llvm.org/docs/GettingStarted.html#requirement
depends_on "cmake" => :build
depends_on arch: :x86_64
depends_on "python@3.8"
depends_on "swig"
uses_from_macos "libedit"
uses_from_macos "libffi", since: :catalina
uses_from_macos "libxml2"
uses_from_macos "ncurses"
uses_from_macos "zlib"
on_high_sierra :or_older do
depends_on xcode: :build
end
on_linux do
depends_on "binutils" # needed for gold and strip
depends_on "elfutils" # openmp requires <gelf.h>
depends_on "glibc" if Formula["glibc"].any_version_installed?
end
resource "clang" do
url "https://github.com/llvm/llvm-project/releases/download/llvmorg-8.0.1/cfe-8.0.1.src.tar.xz"
sha256 "70effd69f7a8ab249f66b0a68aba8b08af52aa2ab710dfb8a0fba102685b1646"
# Fix include paths on Big Sur
on_big_sur :or_newer do
# Refactor header search path logic into driver
# https://github.com/llvm/llvm-project/commit/e97b5f5cf37e382643b567affd714823215d0e75
patch :p2 do
url "https://github.com/llvm/llvm-project/commit/e97b5f5cf37e382643b567affd714823215d0e75.patch?full_index=1"
sha256 "ba4776b7a8c23c844d98c436011c24453ab6f740edd2c3bba2f8b909d2e59dcf"
end
# Respect --sysroot for header search
# https://github.com/llvm/llvm-project/commit/b2ece169ed609b9111c290254d831101d21cbf8f
patch :p2 do
url "https://github.com/llvm/llvm-project/commit/b2ece169ed609b9111c290254d831101d21cbf8f.patch?full_index=1"
sha256 "40a2689cd97bc85d666f74286526b5311015d030ce5a1feae627460805f7b5ca"
end
# Skip sysroot headers if they are found alongside the toolchain. Backported from
# https://github.com/llvm/llvm-project/commit/a3a24316087d0e1b4db0b8fee19cdee8b7968032
patch :p3 do
url "https://raw.githubusercontent.com/Homebrew/formula-patches/bc3176e6794efb9f2581ce4f9ede3ad34efb492c/llvm%409/llvm%409.patch?full_index=1"
sha256 "02fb21c26f468b0dab25c93b2802539133e06b0bcf19802a7ecdc227c454c4db"
end
end
end
resource "clang-extra-tools" do
url "https://github.com/llvm/llvm-project/releases/download/llvmorg-8.0.1/clang-tools-extra-8.0.1.src.tar.xz"
sha256 "187179b617e4f07bb605cc215da0527e64990b4a7dd5cbcc452a16b64e02c3e1"
end
resource "compiler-rt" do
url "https://github.com/llvm/llvm-project/releases/download/llvmorg-8.0.1/compiler-rt-8.0.1.src.tar.xz"
sha256 "11828fb4823387d820c6715b25f6b2405e60837d12a7469e7a8882911c721837"
end
resource "libcxx" do
url "https://github.com/llvm/llvm-project/releases/download/llvmorg-8.0.1/libcxx-8.0.1.src.tar.xz"
sha256 "7f0652c86a0307a250b5741ab6e82bb10766fb6f2b5a5602a63f30337e629b78"
end
resource "libcxxabi" do
on_linux do
url "https://github.com/llvm/llvm-project/releases/download/llvmorg-8.0.1/libcxxabi-8.0.1.src.tar.xz"
sha256 "b75bf3c8dc506e7d950d877eefc8b6120a4651aaa110f5805308861f2cfaf6ef"
end
end
resource "libunwind" do
url "https://github.com/llvm/llvm-project/releases/download/llvmorg-8.0.1/libunwind-8.0.1.src.tar.xz"
sha256 "1870161dda3172c63e632c1f60624564e1eb0f9233cfa8f040748ca5ff630f6e"
end
resource "lld" do
url "https://github.com/llvm/llvm-project/releases/download/llvmorg-8.0.1/lld-8.0.1.src.tar.xz"
sha256 "9fba1e94249bd7913e8a6c3aadcb308b76c8c3d83c5ce36c99c3f34d73873d88"
end
resource "lldb" do
url "https://github.com/llvm/llvm-project/releases/download/llvmorg-8.0.1/lldb-8.0.1.src.tar.xz"
sha256 "e8a79baa6d11dd0650ab4a1b479f699dfad82af627cbbcd49fa6f2dc14e131d7"
end
resource "openmp" do
url "https://github.com/llvm/llvm-project/releases/download/llvmorg-8.0.1/openmp-8.0.1.src.tar.xz"
sha256 "3e85dd3cad41117b7c89a41de72f2e6aa756ea7b4ef63bb10dcddf8561a7722c"
end
resource "polly" do
url "https://github.com/llvm/llvm-project/releases/download/llvmorg-8.0.1/polly-8.0.1.src.tar.xz"
sha256 "e8a1f7e8af238b32ce39ab5de1f3317a2e3f7d71a8b1b8bbacbd481ac76fd2d1"
end
def install
# Apple's libstdc++ is too old to build LLVM
ENV.libcxx if ENV.compiler == :clang
(buildpath/"tools/clang").install resource("clang")
(buildpath/"tools/clang/tools/extra").install resource("clang-extra-tools")
(buildpath/"projects/openmp").install resource("openmp")
(buildpath/"projects/libcxx").install resource("libcxx")
(buildpath/"projects/libcxxabi").install resource("libcxxabi") if OS.linux?
(buildpath/"projects/libunwind").install resource("libunwind")
(buildpath/"tools/lld").install resource("lld")
(buildpath/"tools/lldb").install resource("lldb")
(buildpath/"tools/polly").install resource("polly")
(buildpath/"projects/compiler-rt").install resource("compiler-rt")
# compiler-rt has some iOS simulator features that require i386 symbols
# I'm assuming the rest of clang needs support too for 32-bit compilation
# to work correctly, but if not, perhaps universal binaries could be
# limited to compiler-rt. llvm makes this somewhat easier because compiler-rt
# can almost be treated as an entirely different build from llvm.
ENV.permit_arch_flags
args = %w[
-DLIBOMP_ARCH=x86_64
-DLINK_POLLY_INTO_TOOLS=ON
-DLLVM_BUILD_LLVM_DYLIB=ON
-DLLVM_ENABLE_EH=ON
-DLLVM_ENABLE_FFI=ON
-DLLVM_ENABLE_RTTI=ON
-DCLANG_ANALYZER_ENABLE_Z3_SOLVER=OFF
-DLLVM_INCLUDE_DOCS=OFF
-DLLVM_INSTALL_UTILS=ON
-DLLVM_OPTIMIZED_TABLEGEN=ON
-DLLVM_TARGETS_TO_BUILD=all
-DWITH_POLLY=ON
-DLLDB_USE_SYSTEM_DEBUGSERVER=ON
-DLLDB_DISABLE_PYTHON=1
-DLIBOMP_INSTALL_ALIASES=OFF
]
if OS.mac?
if MacOS.version >= :catalina
args << "-DFFI_INCLUDE_DIR=#{MacOS.sdk_path}/usr/include/ffi"
args << "-DFFI_LIBRARY_DIR=#{MacOS.sdk_path}/usr/lib"
end
args << "-DLLVM_BUILD_EXTERNAL_COMPILER_RT=ON" if MacOS.version <= :mojave
args << "-DLLVM_CREATE_XCODE_TOOLCHAIN=ON"
args << "-DLLVM_ENABLE_LIBCXX=ON"
args << "-DDARWIN_osx_ARCHS=x86_64;x86_64h"
sdk = MacOS.sdk_path_if_needed
args << "-DDEFAULT_SYSROOT=#{sdk}" if sdk
else
args << "-DFFI_INCLUDE_DIR=#{Formula["libffi"].opt_include}"
args << "-DFFI_LIBRARY_DIR=#{Formula["libffi"].opt_lib}"
args << "-DLLVM_BUILD_EXTERNAL_COMPILER_RT=ON"
args << "-DLLVM_CREATE_XCODE_TOOLCHAIN=OFF"
args << "-DLLVM_ENABLE_LIBCXX=OFF"
args << "-DCLANG_DEFAULT_CXX_STDLIB=libstdc++"
# Enable llvm gold plugin for LTO
args << "-DLLVM_BINUTILS_INCDIR=#{Formula["binutils"].opt_include}"
# Add flags to build shared and static libc++ which are independent of GCC
args += %w[
-DLLVM_ENABLE_PER_TARGET_RUNTIME_DIR=OFF
-DCMAKE_POSITION_INDEPENDENT_CODE=ON
-DLIBCXX_ENABLE_STATIC_ABI_LIBRARY=ON
-DLIBCXX_STATICALLY_LINK_ABI_IN_SHARED_LIBRARY=OFF
-DLIBCXX_STATICALLY_LINK_ABI_IN_STATIC_LIBRARY=ON
-DLIBCXX_USE_COMPILER_RT=ON
-DLIBCXXABI_ENABLE_STATIC_UNWINDER=ON
-DLIBCXXABI_STATICALLY_LINK_UNWINDER_IN_SHARED_LIBRARY=OFF
-DLIBCXXABI_STATICALLY_LINK_UNWINDER_IN_STATIC_LIBRARY=ON
-DLIBCXXABI_USE_COMPILER_RT=ON
-DLIBCXXABI_USE_LLVM_UNWINDER=ON
-DLIBUNWIND_USE_COMPILER_RT=ON
]
# Don't pass -rtlib as an argument to GCC because it doesn't understand it.
inreplace (buildpath/"projects/libcxx/CMakeLists.txt"),
"list(APPEND LIBCXX_LINK_FLAGS \"-rtlib=compiler-rt\")", ""
inreplace (buildpath/"projects/libcxxabi/CMakeLists.txt"),
"list(APPEND LIBCXXABI_LINK_FLAGS \"-rtlib=compiler-rt\")", ""
inreplace (buildpath/"projects/libunwind/CMakeLists.txt"),
"list(APPEND LIBUNWIND_LINK_FLAGS \"-rtlib=compiler-rt\")", ""
end
mkdir "build" do
system "cmake", "-G", "Unix Makefiles", "..", *(std_cmake_args + args)
system "make"
system "make", "install"
system "make", "install-xcode-toolchain" if MacOS::Xcode.installed?
end
(share/"clang/tools").install Dir["tools/clang/tools/scan-{build,view}"]
(share/"cmake").install "cmake/modules"
inreplace "#{share}/clang/tools/scan-build/bin/scan-build", "$RealBin/bin/clang", "#{bin}/clang"
bin.install_symlink share/"clang/tools/scan-build/bin/scan-build", share/"clang/tools/scan-view/bin/scan-view"
man1.install_symlink share/"clang/tools/scan-build/man/scan-build.1"
# install llvm python bindings
(lib/"python3.8/site-packages").install buildpath/"bindings/python/llvm"
(lib/"python3.8/site-packages").install buildpath/"tools/clang/bindings/python/clang"
if OS.linux?
# Strip executables/libraries/object files to reduce their size
system("strip", "--strip-unneeded", "--preserve-dates", *(Dir[bin/"**/*", lib/"**/*"]).select do |f|
f = Pathname.new(f)
f.file? && (f.elf? || f.extname == ".a")
end)
end
end
def caveats
<<~EOS
To use the bundled libc++ please add the following LDFLAGS:
LDFLAGS="-L#{opt_lib} -Wl,-rpath,#{opt_lib}"
EOS
end
test do
assert_equal prefix.to_s, shell_output("#{bin}/llvm-config --prefix").chomp
(testpath/"omptest.c").write <<~EOS
#include <stdlib.h>
#include <stdio.h>
#include <omp.h>
int main() {
#pragma omp parallel num_threads(4)
{
printf("Hello from thread %d, nthreads %d\\n", omp_get_thread_num(), omp_get_num_threads());
}
return EXIT_SUCCESS;
}
EOS
clean_version = version.to_s[/(\d+\.?)+/]
args = [
"-L#{lib}",
"-fopenmp",
"-nobuiltininc",
"-I#{lib}/clang/#{clean_version}/include",
]
args << "-Wl,-rpath=#{lib}" if OS.linux?
system "#{bin}/clang", *args, "omptest.c", "-o", "omptest", *ENV["LDFLAGS"].split
testresult = shell_output("./omptest")
sorted_testresult = testresult.split("\n").sort.join("\n")
expected_result = <<~EOS
Hello from thread 0, nthreads 4
Hello from thread 1, nthreads 4
Hello from thread 2, nthreads 4
Hello from thread 3, nthreads 4
EOS
assert_equal expected_result.strip, sorted_testresult.strip
(testpath/"test.c").write <<~EOS
#include <stdio.h>
int main()
{
printf("Hello World!\\n");
return 0;
}
EOS
(testpath/"test.cpp").write <<~EOS
#include <iostream>
int main()
{
std::cout << "Hello World!" << std::endl;
return 0;
}
EOS
# Testing default toolchain and SDK location.
system "#{bin}/clang++", "-v",
"-std=c++11", "test.cpp", "-o", "test++"
assert_includes MachO::Tools.dylibs("test++"), "/usr/lib/libc++.1.dylib" if OS.mac?
assert_equal "Hello World!", shell_output("./test++").chomp
system "#{bin}/clang", "-v", "test.c", "-o", "test"
assert_equal "Hello World!", shell_output("./test").chomp
# Testing Command Line Tools
if OS.mac? && MacOS::CLT.installed?
toolchain_path = "/Library/Developer/CommandLineTools"
sdk_path = "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk"
system "#{bin}/clang++", "-v",
"-isysroot", sdk_path,
"-isystem", "#{toolchain_path}/usr/include/c++/v1",
"-isystem", "#{toolchain_path}/usr/include",
"-isystem", "#{sdk_path}/usr/include",
"-std=c++11", "test.cpp", "-o", "testCLT++"
assert_includes MachO::Tools.dylibs("testCLT++"), "/usr/lib/libc++.1.dylib"
assert_equal "Hello World!", shell_output("./testCLT++").chomp
system "#{bin}/clang", "-v", "test.c", "-o", "testCLT"
assert_equal "Hello World!", shell_output("./testCLT").chomp
end
# Testing Xcode
if OS.mac? && MacOS::Xcode.installed?
system "#{bin}/clang++", "-v",
"-isysroot", MacOS.sdk_path,
"-isystem", "#{MacOS::Xcode.toolchain_path}/usr/include/c++/v1",
"-isystem", "#{MacOS::Xcode.toolchain_path}/usr/include",
"-isystem", "#{MacOS.sdk_path}/usr/include",
"-std=c++11", "test.cpp", "-o", "testXC++"
assert_includes MachO::Tools.dylibs("testXC++"), "/usr/lib/libc++.1.dylib"
assert_equal "Hello World!", shell_output("./testXC++").chomp
system "#{bin}/clang", "-v",
"-isysroot", MacOS.sdk_path,
"test.c", "-o", "testXC"
assert_equal "Hello World!", shell_output("./testXC").chomp
end
# link against installed libc++
# related to https://github.com/Homebrew/legacy-homebrew/issues/47149
system "#{bin}/clang++", "-v",
"-isystem", "#{opt_include}/c++/v1",
"-std=c++11", "-stdlib=libc++", "test.cpp", "-o", "testlibc++",
"-rtlib=compiler-rt", "-L#{opt_lib}", "-Wl,-rpath,#{opt_lib}"
assert_includes (testpath/"testlibc++").dynamically_linked_libraries,
(opt_lib/shared_library("libc++", "1")).to_path
(testpath/"testlibc++").dynamically_linked_libraries.each do |lib|
refute_match(/libstdc\+\+/, lib)
refute_match(/libgcc/, lib)
refute_match(/libatomic/, lib)
end
assert_equal "Hello World!", shell_output("./testlibc++").chomp
if OS.linux?
# Link installed libc++, libc++abi, and libunwind archives both into
# a position independent executable (PIE), as well as into a fully
# position independent (PIC) DSO for things like plugins that export
# a C-only API but internally use C++.
#
# FIXME: It'd be nice to be able to use flags like `-static-libstdc++`
# together with `-stdlib=libc++` (the latter one we need anyways for
# headers) to achieve this but those flags don't set up the correct
# search paths or handle all of the libraries needed by `libc++` when
# linking statically.
system "#{bin}/clang++", "-v", "-o", "test_pie_runtimes",
"-pie", "-fPIC", "test.cpp", "-L#{opt_lib}",
"-stdlib=libc++", "-rtlib=compiler-rt",
"-static-libstdc++", "-lpthread", "-ldl"
assert_equal "Hello World!", shell_output("./test_pie_runtimes").chomp
(testpath/"test_pie_runtimes").dynamically_linked_libraries.each do |lib|
refute_match(/lib(std)?c\+\+/, lib)
refute_match(/libgcc/, lib)
refute_match(/libatomic/, lib)
refute_match(/libunwind/, lib)
end
(testpath/"test_plugin.cpp").write <<~EOS
#include <iostream>
__attribute__((visibility("default")))
extern "C" void run_plugin() {
std::cout << "Hello Plugin World!" << std::endl;
}
EOS
(testpath/"test_plugin_main.c").write <<~EOS
extern void run_plugin();
int main() {
run_plugin();
}
EOS
system "#{bin}/clang++", "-v", "-o", "test_plugin.so",
"-shared", "-fPIC", "test_plugin.cpp", "-L#{opt_lib}",
"-stdlib=libc++", "-rtlib=compiler-rt",
"-static-libstdc++", "-lpthread", "-ldl"
system "#{bin}/clang", "-v",
"test_plugin_main.c", "-o", "test_plugin_libc++",
"test_plugin.so", "-Wl,-rpath=#{testpath}", "-rtlib=compiler-rt"
assert_equal "Hello Plugin World!", shell_output("./test_plugin_libc++").chomp
(testpath/"test_plugin.so").dynamically_linked_libraries.each do |lib|
refute_match(/lib(std)?c\+\+/, lib)
refute_match(/libgcc/, lib)
refute_match(/libatomic/, lib)
refute_match(/libunwind/, lib)
end
end
(testpath/"scanbuildtest.cpp").write <<~EOS
#include <iostream>
int main() {
int *i = new int;
*i = 1;
delete i;
std::cout << *i << std::endl;
return 0;
}
EOS
assert_includes shell_output("#{bin}/scan-build --use-analyzer #{bin}/clang++ " \
"#{bin}/clang++ scanbuildtest.cpp 2>&1"),
"warning: Use of memory after it is freed"
(testpath/"clangformattest.c").write <<~EOS
int main() {
printf("Hello world!"); }
EOS
assert_equal "int main() { printf(\"Hello world!\"); }\n",
shell_output("#{bin}/clang-format -style=google clangformattest.c")
end
end