User:Sam/Drafts/macOS Clang

From Gentoo Wiki
Jump to:navigation Jump to:search

Notes on hackily using a GCC-bootstrapped macOS Prefix kind of work using Clang.

Installing Clang

Bootstrap

Mask an unusable flag:

FILE ${EPREFIX}/etc/portage/package.use.mask
# sys-devel/binutils is masked on Darwin
sys-devel/llvm binutils-plugin

The system (not Prefix) Clang is used to bootstrap LLVM itself:

user $CC=/usr/bin/clang CXX=/usr/bin/clang++ emerge -v1O sys-devel/llvm

Then Clang:

user $CC=/usr/bin/clang CXX=/usr/bin/clang++ emerge -v1O sys-devel/clang

Add clang and friends to PATH:

user $. ${EPREFIX}/etc/profile

Test it out

Do a test run:

FILE ${EPREFIX}/etc/portage/profile/package.use.mask
app-editors/emacs -aqua
user $CC=clang CXX=clang++ USE=aqua emerge -v1 emacs

If that works, create an environment:

FILE ${EPREFIX}/etc/portage/env/clang
CC=clang
CXX=clang++

# Needed to avoid errors with ld:
# "ld: unknown option: -platform_version"
LDFLAGS="${LDFLAGS} -mlinker-version=305"

And configure say, Emacs to use it:

FILE ${EPREFIX}/etc/portage/package.env/clang
app-editors/emacs clang

At this point, one should have a working Emacs with Aqua support!

user $emacs

Building Clang with itself

Enhance the environment created earlier:

FILE ${EPREFIX}/etc/portage/env/clang
AR=llvm-ar
CC=clang
CXX=clang++
NM=llvm-nm
RANLIB=llvm-ranlib
READELF=llvm-readelf
STRIP=llvm-strip
OBJDUMP=llvm-objdump
OBJCOPY=llvm-objcopy

LDFLAGS="${LDFLAGS} -mlinker-version=305"


See Also

  • Prefix/Darwin — brings the power of Gentoo to Darwin (macOS/OS X) based systems, similar to pkgsrc, macports, and homebrew.

DarthGandalf 's steps

Of installing Clang using AppleClang on the already installed prefix which was using GCC. Hopefully something similar can be done to bootstrap without bootstrapping GCC first.

First, add this to make.conf:

FILE make.conf
...
CC=clang
CXX=clang++

PATH="/Library/Developer/CommandLineTools/usr/bin:$PATH"
CFLAGS="-L /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib -isystem /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include ${CFLAGS}"
CXXFLAGS="${CFLAGS}"
LDFLAGS="${LDFLAGS} -mlinker-version=305"

I don't know whether NM, RANLIB etc overrides are any useful; it worked for me without them.

Note: some of the following packages require temporarily removing call to llvm_pkg_setup from pkg_setup (until we figure out how to make it work better).

Add the following:

FILE package.use
*/* -binutils-plugin -libffi -ncurses -extra -static-analyzer libcxx default-libcxx default-compiler-rt default-lld

TODO: do this per package instead

Then, install sys-libs/compiler-rt only:

user $emerge -av1O sys-libs/compiler-rt

Note: this is letter O, not zero. Then add a workaround, which allows AppleClang to compile sys-devel/llvm:

user $cd $EPREFIX/usr/lib; ln -s clang/16/lib/darwin/libclang_rt.osx.a libatomic.dylib

Then install other packages in order:

user $emerge -av1O sys-libs/libcxxabi
user $emerge -av1O sys-libs/libcxx
user $emerge -av1O sys-devel/llvm
user $emerge -av1O sys-devel/clang
user $emerge -av1O sys-devel/clang-common
user $emerge -av1O sys-devel/lld

Update clang config:

FILE etc/clang/gentoo-common
-isystem "/Users/user/Gentoo/usr/include/c++/v1"
-isystem "/Users/user/Gentoo/MacOSX.sdk/usr/include"
-L "/Users/user/Gentoo/MacOSX.sdk/usr/lib"
# This file contains flags common to clang, clang++ and clang-cpp.
@gentoo-runtimes.cfg
@gentoo-gcc-install.cfg
@gentoo-hardened.cfg
# bug #870001
-include "/Users/user/Gentoo/usr/include/gentoo/maybe-stddefs.h"
-isystem "/Users/user/Gentoo/usr/include"
-L "/Users/user/Gentoo/usr/lib"
-isysroot "/Users/user/Gentoo/MacOSX.sdk"

Perhaps some of these additions are not necessary, TODO: try to remove some of them. Replace path to prefix /Users/user/Gentoo with the correct one for your setup.

Now you can remove PATH and *FLAGS vars added above from make.conf, re-enter prefix, remove the symlink to libatomic.

The next step requires temporarily adding -DLLVM_ENABLE_LIBCXX=ON to llvm and clang ebuilds. llvm ebuild already has it but it's broken due to tc-get-cxx-stdlib missing from the ::gentoo_prefix's copy of toolchain-funcs.eclass. TODO: fix this.

After that you can rebuild all these packages using clang installed via Gentoo.