-
-
Notifications
You must be signed in to change notification settings - Fork 12.7k
/
Copy pathemscripten.rb
285 lines (244 loc) · 11.4 KB
/
emscripten.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
class Emscripten < Formula
desc "LLVM bytecode to JavaScript compiler"
homepage "https://emscripten.org/"
# To automate fetching the required resource revisions, you can use this helper script:
# https://gist.github.com/carlocab/2db1d7245fa0cd3e92e01fe37b164021
url "https://github.com/emscripten-core/emscripten/archive/refs/tags/4.0.7.tar.gz"
sha256 "432874e8d2c14a2fe7b19d5dbf31a5fe42c953a15d1a9ffc1db83b93cd2babfc"
license all_of: [
"Apache-2.0", # binaryen
"Apache-2.0" => { with: "LLVM-exception" }, # llvm
any_of: ["MIT", "NCSA"], # emscripten
]
head "https://github.com/emscripten-core/emscripten.git", branch: "main"
livecheck do
url :stable
regex(/^v?(\d+(?:\.\d+)+)$/i)
end
bottle do
sha256 cellar: :any, arm64_sequoia: "b91aa1250e7bdc9a3193aed31b04d6dc11b426f70beb48e6725478dea8e2457a"
sha256 cellar: :any, arm64_sonoma: "f2bc173a7a331ca587daa3a8dfb40990b397d1ff18ef3a4471c1825649ce3196"
sha256 cellar: :any, arm64_ventura: "5afe49c397677899f749d1dda0c0cdfc6ab454b7b4d2b591da49f5c4635cca36"
sha256 cellar: :any, sonoma: "469f5d696ce51eca6578624798a949ad40dd3911ef7e68d3c50bc64c57cda317"
sha256 cellar: :any, ventura: "a0dbd479467bda5df9d7f4951b9b08af960db675ffc9f92839adccd498e37c46"
sha256 cellar: :any_skip_relocation, arm64_linux: "b4297a8d13fe8059333fc8176d3e309e0889d68399018ad320003ed0647f7942"
sha256 cellar: :any_skip_relocation, x86_64_linux: "fb9c17ab87e778992a2f4e5a2ec489f4743197add350a7156825f5c67d9f2539"
end
depends_on "cmake" => :build
depends_on "node"
depends_on "python@3.13"
depends_on "yuicompressor"
uses_from_macos "llvm" => :build
uses_from_macos "zlib"
# OpenJDK is needed as a dependency on Linux and ARM64 for google-closure-compiler,
# an emscripten dependency, because the native GraalVM image will not work.
on_macos do
on_arm do
depends_on "openjdk"
end
end
on_linux do
depends_on "openjdk"
end
# We use LLVM to work around an error while building bundled `google-benchmark` with GCC
fails_with :gcc do
cause <<~EOS
.../third-party/benchmark/src/thread_manager.h:50:31: error: expected ‘)’ before ‘(’ token
50 | GUARDED_BY(GetBenchmarkMutex()) Result results;
| ^
EOS
end
# Use emscripten's recommended binaryen revision to avoid build failures.
# https://github.com/emscripten-core/emscripten/issues/12252
# To find the correct binaryen revision, find the corresponding version commit at:
# https://github.com/emscripten-core/emsdk/blob/main/emscripten-releases-tags.json
# Then take this commit and go to:
# https://chromium.googlesource.com/emscripten-releases/+/<commit>/DEPS
# Then use the listed binaryen_revision for the revision below.
resource "binaryen" do
url "https://github.com/WebAssembly/binaryen/archive/e6f1c53a2052d7c1e9e06ace64c7c2833aa82a7d.tar.gz"
version "e6f1c53a2052d7c1e9e06ace64c7c2833aa82a7d"
sha256 "6a9d5737c936c40bef0e3da4bfc5f00f2fdd2a9f893853043c25968415f5a5b6"
livecheck do
url "https://raw.githubusercontent.com/emscripten-core/emsdk/refs/tags/#{LATEST_VERSION}/emscripten-releases-tags.json"
regex(/["']binaryen_revision["']:\s*["']([0-9a-f]+)["']/i)
strategy :json do |json, regex|
# TODO: Find a way to replace `json.dig("aliases", "latest")` with substituted LATEST_VERSION
release_hash = json.dig("releases", json.dig("aliases", "latest"))
next if release_hash.blank?
release_url = "https://chromium.googlesource.com/emscripten-releases/+/#{release_hash}/DEPS?format=TEXT"
match = Base64.decode64(Homebrew::Livecheck::Strategy.page_content(release_url)[:content]).match(regex)
next if match.blank?
match[1]
end
end
end
# emscripten does not support using the stable version of LLVM.
# https://github.com/emscripten-core/emscripten/issues/11362
# See binaryen resource above for instructions on how to update this.
# Then use the listed llvm_project_revision for the tarball below.
resource "llvm" do
url "https://github.com/llvm/llvm-project/archive/57025b42c43b2f14f7e58692bc19cd53d1b8a45e.tar.gz"
version "57025b42c43b2f14f7e58692bc19cd53d1b8a45e"
sha256 "bf1ff430f4ccbe8233885676e11479ae299d34393b9343a45d362485a11370f7"
livecheck do
url "https://raw.githubusercontent.com/emscripten-core/emsdk/refs/tags/#{LATEST_VERSION}/emscripten-releases-tags.json"
regex(/["']llvm_project_revision["']:\s*["']([0-9a-f]+)["']/i)
strategy :json do |json, regex|
# TODO: Find a way to replace `json.dig("aliases", "latest")` with substituted LATEST_VERSION
release_hash = json.dig("releases", json.dig("aliases", "latest"))
next if release_hash.blank?
release_url = "https://chromium.googlesource.com/emscripten-releases/+/#{release_hash}/DEPS?format=TEXT"
match = Base64.decode64(Homebrew::Livecheck::Strategy.page_content(release_url)[:content]).match(regex)
next if match.blank?
match[1]
end
end
end
def install
# Avoid hardcoding the executables we pass to `write_env_script` below.
# Prefer executables without `.py` extensions, but include those with `.py`
# extensions if there isn't a matching executable without the `.py` extension.
emscripts = buildpath.children.select do |pn|
pn.file? && pn.executable? && !(pn.extname == ".py" && pn.basename(".py").exist?)
end.map(&:basename)
# All files from the repository are required as emscripten is a collection
# of scripts which need to be installed in the same layout as in the Git
# repository.
libexec.install buildpath.children
# Remove unneeded files. See `tools/install.py`.
rm_r(libexec/"test/third_party")
# emscripten needs an llvm build with the following executables:
# https://github.com/emscripten-core/emscripten/blob/#{version}/docs/packaging.md#dependencies
resource("llvm").stage do
projects = %w[
clang
lld
]
targets = %w[
host
WebAssembly
]
# Apple's libstdc++ is too old to build LLVM
ENV.libcxx if ENV.compiler == :clang
# See upstream configuration in `src/build.py` at
# https://chromium.googlesource.com/emscripten-releases/
args = %W[
-DLLVM_ENABLE_LIBXML2=OFF
-DLLVM_INCLUDE_EXAMPLES=OFF
-DLLVM_LINK_LLVM_DYLIB=OFF
-DLLVM_BUILD_LLVM_DYLIB=OFF
-DCMAKE_BUILD_WITH_INSTALL_RPATH=ON
-DLLVM_ENABLE_BINDINGS=OFF
-DLLVM_TOOL_LTO_BUILD=OFF
-DLLVM_INSTALL_TOOLCHAIN_ONLY=ON
-DLLVM_TARGETS_TO_BUILD=#{targets.join(";")}
-DLLVM_ENABLE_PROJECTS=#{projects.join(";")}
-DLLVM_ENABLE_TERMINFO=#{!OS.linux?}
-DCLANG_ENABLE_ARCMT=OFF
-DCLANG_ENABLE_STATIC_ANALYZER=OFF
-DLLVM_INCLUDE_TESTS=OFF
-DLLVM_INSTALL_UTILS=OFF
-DLLVM_ENABLE_ZSTD=OFF
-DLLVM_ENABLE_Z3_SOLVER=OFF
]
args << "-DLLVM_ENABLE_LIBEDIT=OFF" if OS.linux?
system "cmake", "-S", "llvm", "-B", "build",
"-G", "Unix Makefiles",
*args, *std_cmake_args(install_prefix: libexec/"llvm")
system "cmake", "--build", "build"
system "cmake", "--build", "build", "--target", "install"
# Remove unneeded tools. Taken from upstream `src/build.py`.
unneeded = %w[
check cl cpp extef-mapping format func-mapping import-test offload-bundler refactor rename scan-deps
].map { |suffix| "clang-#{suffix}" }
unneeded += %w[lld-link ld.lld ld64.lld llvm-lib ld64.lld.darwinnew ld64.lld.darwinold]
(libexec/"llvm/bin").glob("{#{unneeded.join(",")}}").map(&:unlink)
(libexec/"llvm/lib").glob("libclang.{dylib,so.*}").map(&:unlink)
# Include needed tools omitted by `LLVM_INSTALL_TOOLCHAIN_ONLY`.
# Taken from upstream `src/build.py`.
extra_tools = %w[FileCheck llc llvm-as llvm-dis llvm-link llvm-mc
llvm-nm llvm-objdump llvm-readobj llvm-size opt
llvm-dwarfdump llvm-dwp]
(libexec/"llvm/bin").install extra_tools.map { |tool| "build/bin/#{tool}" }
%w[clang clang++].each do |compiler|
(libexec/"llvm/bin").install_symlink compiler => "wasm32-#{compiler}"
(libexec/"llvm/bin").install_symlink compiler => "wasm32-wasi-#{compiler}"
bin.install_symlink libexec/"llvm/bin/wasm32-#{compiler}"
bin.install_symlink libexec/"llvm/bin/wasm32-wasi-#{compiler}"
end
end
resource("binaryen").stage do
system "cmake", "-S", ".", "-B", "build",
"-DBUILD_TESTS=OFF",
*std_cmake_args(install_prefix: libexec/"binaryen")
system "cmake", "--build", "build"
system "cmake", "--install", "build"
end
cd libexec do
system "npm", "install", *std_npm_args(prefix: false)
# Delete native GraalVM image in incompatible platforms.
if OS.linux? && Hardware::CPU.intel?
rm_r("node_modules/google-closure-compiler-linux")
elsif OS.mac? && Hardware::CPU.arm?
rm_r("node_modules/google-closure-compiler-osx")
end
# Remove incompatible pre-built binaries
os = OS.kernel_name.downcase
arch = Hardware::CPU.intel? ? "x64" : Hardware::CPU.arch.to_s
rollup = libexec/"node_modules/@rollup"
platform = OS.linux? ? "#{os}-#{arch}-gnu" : "#{os}-#{arch}"
permitted_dir = "rollup-#{platform}"
rollup.glob(rollup/"rollup-*").each do |dir|
next if Dir.glob("#{dir}/rollup.*.node").empty?
rm_r(dir) if permitted_dir != dir.basename.to_s
end
end
# Add JAVA_HOME to env_script on ARM64 macOS and Linux, so that google-closure-compiler
# can find OpenJDK
emscript_env = { PYTHON: Formula["python@3.13"].opt_bin/"python3.13" }
emscript_env.merge! Language::Java.overridable_java_home_env if OS.linux? || Hardware::CPU.arm?
emscripts.each do |emscript|
(bin/emscript).write_env_script libexec/emscript, emscript_env
end
# Replace universal binaries with their native slices
deuniversalize_machos libexec/"node_modules/fsevents/fsevents.node"
end
def post_install
return if File.exist?("#{Dir.home}/.emscripten")
return if (libexec/".emscripten").exist?
system bin/"emcc", "--generate-config"
inreplace libexec/".emscripten" do |s|
s.change_make_var! "LLVM_ROOT", "'#{libexec}/llvm/bin'"
s.change_make_var! "BINARYEN_ROOT", "'#{libexec}/binaryen'"
s.change_make_var! "NODE_JS", "'#{Formula["node"].opt_bin}/node'"
end
end
def caveats
return unless File.exist?("#{Dir.home}/.emscripten")
return if (libexec/".emscripten").exist?
<<~EOS
You have a ~/.emscripten configuration file, so the default configuration
file was not generated. To generate the default configuration:
rm ~/.emscripten
brew postinstall emscripten
EOS
end
test do
ENV["EM_CACHE"] = testpath
# We're targeting WASM, so we don't want to use the macOS SDK here.
ENV.remove_macosxsdk if OS.mac?
# Avoid errors on Linux when other formulae like `sdl12-compat` are installed
ENV.delete "CPATH"
(testpath/"test.c").write <<~C
#include <stdio.h>
int main()
{
printf("Hello World!");
return 0;
}
C
system bin/"emcc", "test.c", "-o", "test.js", "-s", "NO_EXIT_RUNTIME=0"
assert_equal "Hello World!", shell_output("node test.js").chomp
end
end