Skip to content

Instantly share code, notes, and snippets.

@Torrekie
Created February 14, 2022 09:56
Show Gist options
  • Select an option

  • Save Torrekie/ce430183d06274c0b3854bd2d517447b to your computer and use it in GitHub Desktop.

Select an option

Save Torrekie/ce430183d06274c0b3854bd2d517447b to your computer and use it in GitHub Desktop.
Terrible homebrew formulae script for installing Apple's LLVM fork with swift enabled
# Patches are modified from https://github.com/ProcursusTeam/Procursus
# Formulae modified from homebrew llvm@12
class LlvmAppleAT12 < Formula
desc ""
homepage ""
url "https://github.com/apple/llvm-project/archive/swift-5.5-RELEASE.tar.gz"
sha256 "87955764fb6cd83cb24e0421f249ce3fc817400edd3c0015eb840fe7fd7cf5e3"
license ""
keg_only :provided_by_macos
depends_on "cmake" => :build
depends_on "swig" => :build
depends_on "[email protected]"
uses_from_macos "libedit"
uses_from_macos "libffi", since: :catalina
uses_from_macos "libxml2"
uses_from_macos "ncurses"
uses_from_macos "zlib"
resource "swift" do
url "https://github.com/apple/swift/archive/refs/tags/swift-5.5-RELEASE.tar.gz"
end
resource "cmark" do
url "https://github.com/apple/swift-cmark/archive/refs/tags/swift-5.5-RELEASE.tar.gz"
end
resource "swift_patch" do
url "https://raw.githubusercontent.com/ProcursusTeam/Procursus/main/build_patch/swift/silly.diff"
end
patch do
url "https://raw.githubusercontent.com/ProcursusTeam/Procursus/main/build_patch/llvm/lldb-runcmd.diff"
end
patch :DATA
def install
swift_root = buildpath/"swift"
mkdir swift_root do
resource("swift").stage do
mv Dir[Pathname.pwd/"*"], swift_root
end
end
cmark_root = buildpath/"cmark"
mkdir cmark_root do
resource("cmark").stage do
mv Dir[Pathname.pwd/"*"], cmark_root
end
end
resource("swift_patch").stage do
system "patch", "-p1", "-d", swift_root, "-i", "silly.diff"
end
projects = %w[
clang
clang-tools-extra
libcxx
libcxxabi
lld
lldb
polly
libclc
parallel-libs
]
runtimes = %w[
compiler-rt
libunwind
openmp
]
py_ver = Language::Python.major_minor_version("python3")
site_packages = Language::Python.site_packages("python3").delete_prefix("lib/")
# 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
# we install the lldb Python module into libexec to prevent users from
# accidentally importing it with a non-Homebrew Python or a Homebrew Python
# in a non-default prefix. See https://lldb.llvm.org/resources/caveats.html
args = %W[
-DLLVM_ENABLE_PROJECTS=#{projects.join(";")}
-DLLVM_ENABLE_RUNTIMES=#{runtimes.join(";")}
-DLLVM_POLLY_LINK_INTO_TOOLS=ON
-DLLVM_BUILD_EXTERNAL_COMPILER_RT=ON
-DLLVM_LINK_LLVM_DYLIB=ON
-DLLVM_ENABLE_EH=ON
-DLLVM_ENABLE_FFI=ON
-DLLVM_ENABLE_RTTI=ON
-DLLVM_INCLUDE_DOCS=OFF
-DLLVM_INCLUDE_TESTS=OFF
-DLLVM_INSTALL_UTILS=ON
-DLLVM_ENABLE_Z3_SOLVER=OFF
-DLLVM_OPTIMIZED_TABLEGEN=ON
-DLLVM_TARGETS_TO_BUILD=all
-DLLDB_USE_SYSTEM_DEBUGSERVER=ON
-DLLDB_ENABLE_PYTHON=ON
-DLLDB_ENABLE_LUA=OFF
-DLLDB_ENABLE_LZMA=ON
-DLLDB_PYTHON_RELATIVE_PATH=libexec/#{site_packages}
-DLIBOMP_INSTALL_ALIASES=OFF
-DCLANG_VERSION=12.0.0
-DLLVM_BUILD_LLVM_DYLIB=ON
-DLLVM_LINK_LLVM_DYLIB=ON
-DLLVM_VERSION_SUFFIX=
-DLLVM_DEFAULT_TARGET_TRIPLE=arm64-apple-darwin
-DCLANG_LINK_CLANG_DYLIB=ON
-DLIBCXX_OVERRIDE_DARWIN_INSTALL=ON
-DLLVM_EXTERNAL_PROJECTS=cmark;swift
-DLLVM_EXTERNAL_SWIFT_SOURCE_DIR=#{swift_root}
-DLLVM_EXTERNAL_CMARK_SOURCE_DIR=#{cmark_root}
-DMIG_ARCHS=arm64;arm64e;armv7;x86_64
-DCFLAGS_DEPLOYMENT_VERSION_IOS=12.0
-DCFLAGS_DEPLOYMENT_VERSION_TVOS=12.0
-DCFLAGS_DEPLOYMENT_VERSION_WATCHOS=5.0
-DSWIFT_PRIMARY_VARIANT_ARCH=arm64
-DSWIFT_HOST_VARIANT=macosx
-DSWIFT_HOST_VARIANT_ARCH=arm64
-DCFLAGS_SDK=OSX
-DSWIFT_HOST_VARIANT_SDK=OSX
-DSWIFT_ENABLE_IOS32=ON
-DSWIFT_INCLUDE_TESTS=OFF
-DSWIFT_TOOLS_ENABLE_LTO=THIN
-DSWIFT_BUILD_RUNTIME_WITH_HOST_COMPILER=ON
-DSWIFT_BUILD_REMOTE_MIRROR=FALSE
-DSWIFT_BUILD_DYNAMIC_STDLIB=FALSE
-DSWIFT_BUILD_STDLIB_EXTRA_TOOLCHAIN_CONTENT=FALSE
-DCLANG_PYTHON_BINDINGS_VERSIONS=#{py_ver}
-DLLVM_CREATE_XCODE_TOOLCHAIN=#{MacOS::Xcode.installed? ? "ON" : "OFF"}
-DPACKAGE_VENDOR=#{tap.user}
-DBUG_REPORT_URL=#{tap.issues_url}
-DCLANG_VENDOR_UTI=org.#{tap.user.downcase}.clang
]
macos_sdk = MacOS.sdk_path_if_needed
if MacOS.version >= :catalina
args << "-DFFI_INCLUDE_DIR=#{macos_sdk}/usr/include/ffi"
args << "-DFFI_LIBRARY_DIR=#{macos_sdk}/usr/lib"
else
args << "-DFFI_INCLUDE_DIR=#{Formula["libffi"].opt_include}"
args << "-DFFI_LIBRARY_DIR=#{Formula["libffi"].opt_lib}"
end
# gcc-5 fails at building compiler-rt. Enable PGO
# build on Linux when we switch to Ubuntu 18.04.
if OS.mac?
args << "-DLLVM_BUILD_LLVM_C_DYLIB=ON"
args << "-DLLVM_ENABLE_LIBCXX=ON"
args << "-DRUNTIMES_CMAKE_ARGS=-DCMAKE_INSTALL_RPATH=#{rpath}"
args << "-DDEFAULT_SYSROOT=/"
end
if OS.linux?
ENV.append "CXXFLAGS", "-fpermissive -Wno-free-nonheap-object"
ENV.append "CFLAGS", "-fpermissive -Wno-free-nonheap-object"
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}"
# Parts of Polly fail to correctly build with PIC when being used for DSOs.
args << "-DCMAKE_POSITION_INDEPENDENT_CODE=ON"
runtime_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
-DLIBCXX_HAS_ATOMIC_LIB=OFF
-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
]
args << "-DRUNTIMES_CMAKE_ARGS=#{runtime_args.join(";")}"
end
llvmpath = buildpath/"llvm"
mkdir llvmpath/"build" do
system "cmake", "-G", "Unix Makefiles", "..", *(std_cmake_args + args)
system "cmake", "--build", "."
system "cmake", "--build", ".", "--target", "install"
system "cmake", "--build", ".", "--target", "install-xcode-toolchain" if MacOS::Xcode.installed?
end
# Install versioned symlink, or else `llvm-config` doesn't work properly
lib.install_symlink "libLLVM.dylib" => "libLLVM-#{version.major}.dylib" if OS.mac?
# Install LLVM Python bindings
# Clang Python bindings are installed by CMake
(lib/site_packages).install llvmpath/"bindings/python/llvm"
# Install Vim plugins
%w[ftdetect ftplugin indent syntax].each do |dir|
(share/"vim/vimfiles"/dir).install Dir["*/utils/vim/#{dir}/*.vim"]
end
# Install Emacs modes
elisp.install Dir[llvmpath/"utils/emacs/*.el"] + Dir[share/"clang/*.el"]
end
test do
# `test do` will create, run in and delete a temporary directory.
#
# This test will fail and we won't accept that! For Homebrew/homebrew-core
# this will need to be a test that verifies the functionality of the
# software. Run the test with `brew test llvm-apple@12`. Options passed
# to `brew install` such as `--HEAD` also need to be provided to `brew test`.
#
# The installed folder is not in the path, so use the entire path to any
# executables being tested: `system "#{bin}/program", "do", "something"`.
system "false"
end
end
__END__
diff -urN llvm/clang/lib/Driver/ToolChains/Darwin.cpp.old llvm/clang/lib/Driver/ToolChains/Darwin.cpp
--- llvm/clang/lib/Driver/ToolChains/Darwin.cpp.old 2021-06-23 16:31:00.000000000 -0400
+++ llvm/clang/lib/Driver/ToolChains/Darwin.cpp 2021-06-23 16:42:41.000000000 -0400
@@ -26,6 +26,7 @@
#include "llvm/Support/Threading.h"
#include "llvm/Support/VirtualFileSystem.h"
#include <cstdlib> // ::getenv
+#include <TargetConditionals.h>
using namespace clang::driver;
using namespace clang::driver::tools;
@@ -1738,10 +1738,8 @@
<< Triple.getOSName();
break;
case llvm::Triple::IOS:
- Triple.getiOSVersion(Major, Minor, Micro);
- break;
case llvm::Triple::TvOS:
- Triple.getOSVersion(Major, Minor, Micro);
+ Triple.getiOSVersion(Major, Minor, Micro);
break;
case llvm::Triple::WatchOS:
Triple.getWatchOSVersion(Major, Minor, Micro);
@@ -1765,11 +1763,13 @@
StringRef MachOArchName = Toolchain.getMachOArchName(Args);
if (MachOArchName == "arm64" || MachOArchName == "arm64e") {
-#if __arm64__
+#if (__arm64__) && (TARGET_OS_OSX)
// A clang running on an Apple Silicon mac defaults
// to building for mac when building for arm64 rather than
// defaulting to iOS.
OSTy = llvm::Triple::MacOSX;
+#elif (__arm64__) && (TARGET_OS_TVOS)
+ OSTy = llvm::Triple::TvOS;
#else
OSTy = llvm::Triple::IOS;
#endif
@@ -1976,6 +1976,29 @@
if (Environment == MacABI)
NativeTargetVersion = OSTarget->getNativeTargetVersion();
setTarget(Platform, Environment, Major, Minor, Micro, NativeTargetVersion);
+
+ if (!Args.getLastArg(options::OPT_isysroot)) {
+ llvm::Triple DefaultTriple(LLVM_DEFAULT_TARGET_TRIPLE);
+ switch (Platform) {
+ case DarwinPlatformKind::MacOS:
+ if (DefaultTriple.getOS() != llvm::Triple::MacOSX)
+ Args.append(Args.MakeSeparateArg(nullptr, Opts.getOption(options::OPT_isysroot), "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk"));
+ break;
+ case DarwinPlatformKind::IPhoneOS:
+ if (DefaultTriple.getOS() != llvm::Triple::IOS)
+ Args.append(Args.MakeSeparateArg(nullptr, Opts.getOption(options::OPT_isysroot), "/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk"));
+ break;
+ case DarwinPlatformKind::TvOS:
+ if (DefaultTriple.getOS() != llvm::Triple::TvOS)
+ Args.append(Args.MakeSeparateArg(nullptr, Opts.getOption(options::OPT_isysroot), "/Applications/Xcode.app/Contents/Developer/Platforms/AppleTVOS.platform/Developer/SDKs/AppleTVOS.sdk"));
+ break;
+ case DarwinPlatformKind::WatchOS:
+ if (DefaultTriple.getOS() != llvm::Triple::WatchOS)
+ Args.append(Args.MakeSeparateArg(nullptr, Opts.getOption(options::OPT_isysroot), "/Applications/Xcode.app/Contents/Developer/Platforms/WatchOS.platform/Developer/SDKs/WatchOS.sdk"));
+ break;
+ }
+ }
+
if (const Arg *A = Args.getLastArg(options::OPT_isysroot)) {
StringRef SDK = getSDKName(A->getValue());
@@ -2069,6 +2069,24 @@
SmallString<128> P(Sysroot);
llvm::sys::path::append(P, "usr", "include");
addExternCSystemInclude(DriverArgs, CC1Args, P.str());
+ if (Sysroot == "/") {
+ switch (getTriple().getOS()) {
+ case llvm::Triple::MacOSX:
+ addExternCSystemInclude(DriverArgs, CC1Args, "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include");
+ break;
+ case llvm::Triple::IOS:
+ addExternCSystemInclude(DriverArgs, CC1Args, "/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk/usr/include");
+ break;
+ case llvm::Triple::TvOS:
+ addExternCSystemInclude(DriverArgs, CC1Args, "/Applications/Xcode.app/Contents/Developer/Platforms/AppleTVOS.platform/Developer/SDKs/AppleTVOS.sdk/usr/include");
+ break;
+ case llvm::Triple::WatchOS:
+ addExternCSystemInclude(DriverArgs, CC1Args, "/Applications/Xcode.app/Contents/Developer/Platforms/WatchOS.platform/Developer/SDKs/WatchOS.sdk/usr/include");
+ break;
+ default:
+ break;
+ }
+ }
}
}
diff -urN llvm/clang/lib/Frontend/InitHeaderSearch.cpp.old llvm/clang/lib/Frontend/InitHeaderSearch.cpp
--- llvm/clang/lib/Frontend/InitHeaderSearch.cpp.old 2021-06-23 16:45:54.000000000 -0400
+++ llvm/clang/lib/Frontend/InitHeaderSearch.cpp 2021-06-23 16:49:52.000000000 -0400
@@ -446,6 +446,28 @@
if (triple.isOSDarwin()) {
if (HSOpts.UseStandardSystemIncludes) {
// Add the default framework include paths on Darwin.
+ if (!HasSysroot) {
+ switch (triple.getOS()) {
+ case llvm::Triple::MacOSX:
+ AddPath("/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks", System, true);
+ AddPath("/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/Library/Frameworks", System, true);
+ break;
+ case llvm::Triple::IOS:
+ AddPath("/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk/System/Library/Frameworks", System, true);
+ AddPath("/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk/Library/Frameworks", System, true);
+ break;
+ case llvm::Triple::TvOS:
+ AddPath("/Applications/Xcode.app/Contents/Developer/Platforms/AppleTVOS.platform/Developer/SDKs/AppleTVOS.sdk/System/Library/Frameworks", System, true);
+ AddPath("/Applications/Xcode.app/Contents/Developer/Platforms/AppleTVOS.platform/Developer/SDKs/AppleTVOS.sdk/Library/Frameworks", System, true);
+ break;
+ case llvm::Triple::WatchOS:
+ AddPath("/Applications/Xcode.app/Contents/Developer/Platforms/WatchOS.platform/Developer/SDKs/WatchOS.sdk/System/Library/Frameworks", System, true);
+ AddPath("/Applications/Xcode.app/Contents/Developer/Platforms/WatchOS.platform/Developer/SDKs/WatchOS.sdk/Library/Frameworks", System, true);
+ break;
+ default:
+ break;
+ }
+ }
AddPath("/System/Library/Frameworks", System, true);
AddPath("/Library/Frameworks", System, true);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment