homebrew-core/Formula/emscripten.rb

226 lines
8.6 KiB
Ruby

require "language/node"
class Emscripten < Formula
desc "LLVM bytecode to JavaScript compiler"
homepage "https://emscripten.org/"
url "https://github.com/emscripten-core/emscripten/archive/3.1.29.tar.gz"
sha256 "146cec5fe5c47dc9ac04e7a9a69434eac33fe153df8be05c03b3bfaaac6ea1cb"
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_ventura: "542c1aa2ec82224349d175dea10143f31d7072d3bf0b2c13c5a93e91a4e95303"
sha256 cellar: :any, arm64_monterey: "e0d475b62a48b06dc1f8d7fb9403f33a5bb5e235ffb87594431e2d101cad64ba"
sha256 cellar: :any, arm64_big_sur: "48cc3647c7e35ca549ec460fa2d7f498109756d68e6837b2909a538a4f6b4785"
sha256 cellar: :any, ventura: "26b21d2f2bee8398c572ff038e15f6c62e6e3ed02b34a913a672fc14680a042c"
sha256 cellar: :any, monterey: "26933595b156d14668648b9c1baca29b58f1ad8e9cd0476782b7db626a4a6282"
sha256 cellar: :any, big_sur: "074ad0169c4d228ec442c559103649c9308a0d9f5a929b2bb45851116673a7ab"
sha256 cellar: :any_skip_relocation, x86_64_linux: "622a94c532aa1cc2d8595c375180756748f3beb8fa243ceab6360b963fe723df"
end
depends_on "cmake" => :build
depends_on "node"
depends_on "python@3.11"
depends_on "yuicompressor"
# 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
fails_with gcc: "5"
# Use emscripten's recommended binaryen revision to avoid build failures.
# https://github.com/emscripten-core/emscripten/issues/12252
# See llvm resource below for instructions on how to update this.
resource "binaryen" do
url "https://github.com/WebAssembly/binaryen.git",
revision: "cec66beba45668dbad74abd2396bb80d33595ff0"
end
# emscripten does not support using the stable version of LLVM.
# https://github.com/emscripten-core/emscripten/issues/11362
# To find the correct llvm revision, find a corresponding 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 llvm_project_revision for the resource below.
resource "llvm" do
url "https://github.com/llvm/llvm-project.git",
revision: "947d529e4194e0567cfbbea99127066f76c87269"
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 unneded files. See `tools/install.py`.
(libexec/"test/third_party").rmtree
# 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", *std_cmake_args(install_prefix: libexec/"binaryen")
system "cmake", "--build", "build"
system "cmake", "--install", "build"
end
cd libexec do
system "npm", "install", *Language::Node.local_npm_install_args
rm_f "node_modules/ws/builderror.log" # Avoid references to Homebrew shims
# Delete native GraalVM image in incompatible platforms.
if OS.linux?
rm_rf "node_modules/google-closure-compiler-linux"
elsif Hardware::CPU.arm?
rm_rf "node_modules/google-closure-compiler-osx"
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.11"].opt_bin/"python3.11" }
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
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'"
s.change_make_var! "JAVA", "'#{Formula["openjdk"].opt_bin}/java'"
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
# We're targetting 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"
ENV["NODE_OPTIONS"] = "--no-experimental-fetch"
(testpath/"test.c").write <<~EOS
#include <stdio.h>
int main()
{
printf("Hello World!");
return 0;
}
EOS
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