homebrew-core/Formula/emscripten.rb

189 lines
6.8 KiB
Ruby

require "language/node"
class Emscripten < Formula
desc "LLVM bytecode to JavaScript compiler"
homepage "https://emscripten.org/"
# TODO: Remove from versioned dependency conflict allowlist when `python`
# symlink is migrated to `python@3.10`.
url "https://github.com/emscripten-core/emscripten/archive/3.1.17.tar.gz"
sha256 "dd74aa6155fd58fc7cbce78303bfd2014a2a33db23c948d885a0115507067b60"
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_monterey: "d159cd4f31c223a4cb0812f2ca8dca23d79a5c627db3eb06a66786caafaf5cab"
sha256 cellar: :any, arm64_big_sur: "00459b8ec6f3042722b9fd82d6169a1651f6c8eb5ebe75c8001c7f53764971e6"
sha256 cellar: :any, monterey: "448d92ef18a1d05054389d949023ab6abbad5192e1115da1893d27c6efb444fe"
sha256 cellar: :any, big_sur: "b9e579046ad649d9fc2f7a02e254ae85ea18457d0d666c2b31029175cc28e504"
sha256 cellar: :any, catalina: "d53a5b3ad75c5bff857b0d06dc7886870315573431673b289a532803b785227c"
sha256 cellar: :any_skip_relocation, x86_64_linux: "cbd572f3577984892b87d4373576a324d905097aa63b9574dc2a387373032497"
end
depends_on "cmake" => :build
depends_on "node"
depends_on "python@3.10"
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 "gcc"
depends_on "openjdk"
end
fails_with gcc: "5"
# Use emscripten's recommended binaryen revision to avoid build failures.
# See llvm resource below for instructions on how to update this.
resource "binaryen" do
url "https://github.com/WebAssembly/binaryen.git",
revision: "ed704448a6883e9ee0b2f6284f6b5a7b5e7b4aa9"
end
# emscripten needs argument '-fignore-exceptions', which is only available in llvm >= 12
# 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: "661577b5f40bb05534869b9635315e9265c37417"
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|
next false unless pn.file?
next false unless pn.executable?
next false if pn.extname == ".py" && pn.basename(".py").exist?
true
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 Dir["*"]
# 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
]
llvmpath = Pathname.pwd/"llvm"
# Apple's libstdc++ is too old to build LLVM
ENV.libcxx if ENV.compiler == :clang
# 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 = std_cmake_args(install_prefix: libexec/"llvm") + %W[
-DLLVM_ENABLE_PROJECTS=#{projects.join(";")}
-DLLVM_TARGETS_TO_BUILD=#{targets.join(";")}
-DLLVM_LINK_LLVM_DYLIB=ON
-DLLVM_BUILD_LLVM_DYLIB=ON
-DLLVM_INCLUDE_EXAMPLES=OFF
-DLLVM_INCLUDE_TESTS=OFF
-DLLVM_INSTALL_UTILS=OFF
]
sdk = MacOS.sdk_path_if_needed
args << "-DDEFAULT_SYSROOT=#{sdk}" if sdk
mkdir llvmpath/"build" do
# We can use `make` and `make install` here, but prefer these commands
# for consistency with the llvm formula.
system "cmake", "-G", "Unix Makefiles", "..", *args
system "cmake", "--build", "."
system "cmake", "--build", ".", "--target", "install"
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.10"].opt_bin/"python3" }
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 (libexec/".emscripten").exist?
system bin/"emcc", "--generate-config"
inreplace libexec/".emscripten" do |s|
s.gsub!(/^(LLVM_ROOT.*)/, "#\\1\nLLVM_ROOT = \"#{libexec}/llvm/bin\"\\2")
s.gsub!(/^(BINARYEN_ROOT.*)/, "#\\1\nBINARYEN_ROOT = \"#{libexec}/binaryen\"\\2")
end
end
test do
# Fixes "Unsupported architecture" Xcode prepocessor error
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