-
Notifications
You must be signed in to change notification settings - Fork 35
/
llvm39-julia.rb
342 lines (292 loc) · 11.6 KB
/
llvm39-julia.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
class CodesignRequirement < Requirement
include FileUtils
fatal true
satisfy(:build_env => false) do
mktemp do
cp "/usr/bin/false", "llvm_check"
quiet_system "/usr/bin/codesign", "-f", "-s", "lldb_codesign", "--dryrun", "llvm_check"
end
end
def message
<<-EOS
lldb_codesign identity must be available to build with LLDB.
See: https://llvm.org/svn/llvm-project/lldb/trunk/docs/code-signing.txt
EOS
end
end
class Llvm39Julia < Formula
desc "Next-gen compiler infrastructure"
homepage "http://llvm.org/"
stable do
url "http://llvm.org/releases/3.9.1/llvm-3.9.1.src.tar.xz"
sha256 "1fd90354b9cf19232e8f168faf2220e79be555df3aa743242700879e8fd329ee"
# Only required to build & run Compiler-RT tests on macOS, optional otherwise.
# http://clang.llvm.org/get_started.html
resource "libcxx" do
url "http://llvm.org/releases/3.9.1/libcxx-3.9.1.src.tar.xz"
sha256 "25e615e428f60e651ed09ffd79e563864e3f4bc69a9e93ee41505c419d1a7461"
end
resource "libunwind" do
url "http://llvm.org/releases/3.9.1/libunwind-3.9.1.src.tar.xz"
sha256 "0b0bc73264d7ab77d384f8a7498729e3c4da8ffee00e1c85ad02a2f85e91f0e6"
end
end
head do
url "http://llvm.org/git/llvm.git"
resource "libcxx" do
url "http://llvm.org/git/libcxx.git"
end
resource "libunwind" do
url "http://llvm.org/git/libunwind.git"
end
end
revision 1
bottle do
root_url 'https://juliabottles.s3.amazonaws.com'
cellar :any
sha256 "3ac3290a24931a6ddff686d8d695e4436920d008b49677473e739bfd4c4badbf" => :mavericks
sha256 "29a4b522e4ec3e4a30c3446ae801d288939cd37e3ab227c55c1e2bf13a8651c2" => :yosemite
sha256 "95ff13a6a74d39aae4392f4e6e527c70d8dad8422177538e5365a98fae1c0278" => :el_capitan
sha256 "1a63528a2050471cd7b4eddb2e69eb0b8cb8b30bfa74a45954a81485e8eec2c5" => :sierra
end
def patches
patch_list = []
for patch_name in ["PR22923",
"arm-fix-prel31",
"D25865-cmakeshlib",
"3.9.0_threads",
"3.9.0_win64-reloc-dwarf",
"3.9.0_D27296-libssp",
"D27609-AArch64-UABS_G3",
"D27629-AArch64-large_model",
"D9168_argument_alignment",
"D23597_sdag_names",
"D24300_ptx_intrinsics",
"D27389",
"D27397",
"D28009",
"D28215_FreeBSD_shlib",
"D28221-avx512",
"PR276266",
"PR278088",
"PR277939",
"PR278321",
"PR278923",
"D28759-loopclearance",
"D28786-callclearance",
"rL293230-icc17-cmake",
"D32593",
"D33179"]
patch_list << "https://raw.githubusercontent.com/JuliaLang/julia/9e3318c9840e7a9e387582ba861408cefe5a4f75/deps/patches/llvm-#{patch_name}.patch"
end
return patch_list
end
keg_only :provided_by_macos
option :universal
option "without-compiler-rt", "Do not build Clang runtime support libraries for code sanitizers, builtins, and profiling"
option "without-libcxx", "Do not build libc++ standard library"
option "with-toolchain", "Build with Toolchain to facilitate overriding system compiler"
option "with-lldb", "Build LLDB debugger"
option "with-python", "Build bindings against custom Python"
option "with-shared-libs", "Build shared instead of static libraries"
option "without-libffi", "Do not use libffi to call external functions"
option "with-all-targets", "Build all targets. Default targets: AMDGPU, ARM, NVPTX, and X86"
depends_on "libffi" => :recommended # http://llvm.org/docs/GettingStarted.html#requirement
depends_on "graphviz" => :optional # for the 'dot' tool (lldb)
depends_on "ocaml" => :optional
if build.with? "ocaml"
depends_on "opam" => :build
depends_on "pkg-config" => :build
end
if MacOS.version <= :snow_leopard
depends_on "python@2"
else
depends_on "python@2" => :optional
end
depends_on "cmake" => :build
if build.with? "lldb"
depends_on "swig" if MacOS.version >= :lion
depends_on CodesignRequirement
end
# According to the official llvm readme, GCC 4.7+ is required
#fails_with :gcc_4_0
#fails_with :gcc
#("4.3".."4.6").each do |n|
# fails_with :gcc => n
#end
def build_libcxx?
build.with?("libcxx") || !MacOS::CLT.installed?
end
def install
# Apple's libstdc++ is too old to build LLVM
ENV.libcxx if ENV.compiler == :clang
(buildpath/"projects/libcxx").install resource("libcxx") if build_libcxx?
(buildpath/"projects/libunwind").install resource("libunwind")
if build.with? "lldb"
if build.with? "python"
pyhome = `python-config --prefix`.chomp
ENV["PYTHONHOME"] = pyhome
pylib = "#{pyhome}/lib/libpython2.7.dylib"
pyinclude = "#{pyhome}/include/python2.7"
end
(buildpath/"tools/lldb").install resource("lldb")
# Building lldb requires a code signing certificate.
# The instructions provided by llvm creates this certificate in the
# user's login keychain. Unfortunately, the login keychain is not in
# the search path in a superenv build. The following three lines add
# the login keychain to ~/Library/Preferences/com.apple.security.plist,
# which adds it to the superenv keychain search path.
mkdir_p "#{ENV["HOME"]}/Library/Preferences"
username = ENV["USER"]
system "security", "list-keychains", "-d", "user", "-s", "/Users/#{username}/Library/Keychains/login.keychain"
end
args = %w[
-DLLVM_OPTIMIZED_TABLEGEN=ON
-DLLVM_INCLUDE_DOCS=OFF
-DLLVM_ENABLE_RTTI=ON
-DLLVM_ENABLE_EH=ON
-DLLVM_INSTALL_UTILS=ON
]
args << "-DLLVM_TARGETS_TO_BUILD=#{build.with?("all-targets") ? "all" : "AMDGPU;ARM;NVPTX;X86"}"
args << "-DLIBOMP_ARCH=x86_64"
args << "-DLLVM_CREATE_XCODE_TOOLCHAIN=ON" if build.with? "toolchain"
if build.with? "shared-libs"
args << "-DBUILD_SHARED_LIBS=ON"
args << "-DLIBOMP_ENABLE_SHARED=ON"
else
args << "-DLLVM_BUILD_LLVM_DYLIB=ON"
end
args << "-DLLVM_ENABLE_LIBCXX=ON" if build_libcxx?
if build.with?("lldb") && build.with?("python")
args << "-DLLDB_RELOCATABLE_PYTHON=ON"
args << "-DPYTHON_LIBRARY=#{pylib}"
args << "-DPYTHON_INCLUDE_DIR=#{pyinclude}"
end
if build.with? "libffi"
args << "-DLLVM_ENABLE_FFI=ON"
args << "-DFFI_INCLUDE_DIR=#{Formula["libffi"].opt_lib}/libffi-#{Formula["libffi"].version}/include"
args << "-DFFI_LIBRARY_DIR=#{Formula["libffi"].opt_lib}"
end
if build.universal?
ENV.permit_arch_flags
args << "-DCMAKE_OSX_ARCHITECTURES=#{Hardware::CPU.universal_archs.as_cmake_arch_flags}"
end
mktemp do
if build.with? "ocaml"
ENV["OPAMYES"] = "1"
ENV["OPAMROOT"] = Pathname.pwd/"opamroot"
(Pathname.pwd/"opamroot").mkpath
system "opam", "init", "--no-setup"
system "opam", "install", "ocamlfind", "ctypes"
system "opam", "config", "exec", "--",
"cmake", "-G", "Unix Makefiles", buildpath, *(std_cmake_args + args)
else
system "cmake", "-G", "Unix Makefiles", buildpath, *(std_cmake_args + args)
end
system "make"
system "make", "install"
system "make", "install-xcode-toolchain" if build.with? "toolchain"
end
# install llvm python bindings
(lib/"python2.7/site-packages").install buildpath/"bindings/python/llvm"
end
def caveats
s = <<-EOS
LLVM executables are installed in #{opt_bin}.
Extra tools are installed in #{opt_pkgshare}.
EOS
if build_libcxx?
s += <<-EOS
To use the bundled libc++ please add the following LDFLAGS:
LDFLAGS="-L#{opt_lib} -Wl,-rpath,#{opt_lib}"
EOS
end
s
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
system "#{bin}/clang", "-L#{lib}", "-fopenmp", "-nobuiltininc",
"-I#{lib}/clang/#{version}/include",
"omptest.c", "-o", "omptest"
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 Command Line Tools
if MacOS::CLT.installed?
libclangclt = Dir["/Library/Developer/CommandLineTools/usr/lib/clang/#{MacOS::CLT.version.to_i}*"].last { |f| File.directory? f }
system "#{bin}/clang++", "-v", "-nostdinc",
"-I/Library/Developer/CommandLineTools/usr/include/c++/v1",
"-I#{libclangclt}/include",
"-I/usr/include", # need it because /Library/.../usr/include/c++/v1/iosfwd refers to <wchar.h>, which CLT installs to /usr/include
"test.cpp", "-o", "testCLT++"
assert_match "/usr/lib/libc++.1.dylib", shell_output("otool -L ./testCLT++").chomp
assert_equal "Hello World!", shell_output("./testCLT++").chomp
system "#{bin}/clang", "-v", "-nostdinc",
"-I/usr/include", # this is where CLT installs stdio.h
"test.c", "-o", "testCLT"
assert_equal "Hello World!", shell_output("./testCLT").chomp
end
# Testing Xcode
if MacOS::Xcode.installed?
libclangxc = Dir["#{MacOS::Xcode.toolchain_path}/usr/lib/clang/#{DevelopmentTools.clang_version}*"].last { |f| File.directory? f }
system "#{bin}/clang++", "-v", "-nostdinc",
"-I#{MacOS::Xcode.toolchain_path}/usr/include/c++/v1",
"-I#{libclangxc}/include",
"-I#{MacOS.sdk_path}/usr/include",
"test.cpp", "-o", "testXC++"
assert_match "/usr/lib/libc++.1.dylib", shell_output("otool -L ./testXC++").chomp
assert_equal "Hello World!", shell_output("./testXC++").chomp
system "#{bin}/clang", "-v", "-nostdinc",
"-I#{MacOS.sdk_path}/usr/include",
"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
if build_libcxx?
system "#{bin}/clang++", "-v", "-nostdinc",
"-std=c++11", "-stdlib=libc++",
"-I#{MacOS::Xcode.toolchain_path}/usr/include/c++/v1",
"-I#{libclangxc}/include",
"-I#{MacOS.sdk_path}/usr/include",
"-L#{lib}",
"-Wl,-rpath,#{lib}", "test.cpp", "-o", "test"
assert_match "#{opt_lib}/libc++.1.dylib", shell_output("otool -L ./test").chomp
assert_equal "Hello World!", shell_output("./test").chomp
end
end
end