kumquat-buildroot/package/clang/clang.mk

145 lines
5.5 KiB
Makefile
Raw Normal View History

################################################################################
#
# clang
#
################################################################################
# LLVM, Clang and lld should be version bumped together
CLANG_VERSION_MAJOR = 11
CLANG_VERSION = $(CLANG_VERSION_MAJOR).1.0
CLANG_SITE = https://github.com/llvm/llvm-project/releases/download/llvmorg-$(CLANG_VERSION)
CLANG_SOURCE = clang-$(CLANG_VERSION).src.tar.xz
CLANG_LICENSE = Apache-2.0 with exceptions
CLANG_LICENSE_FILES = LICENSE.TXT
CLANG_CPE_ID_VENDOR = llvm
CLANG_SUPPORTS_IN_SOURCE_BUILD = NO
CLANG_INSTALL_STAGING = YES
HOST_CLANG_DEPENDENCIES = host-llvm host-libxml2
CLANG_DEPENDENCIES = llvm host-clang
# LLVM >= 9.0 will soon require C++14 support, building llvm 8.x using a
# toolchain using gcc < 5.1 gives an error but actually still works. Setting
# this option makes it still build with gcc >= 4.8.
# https://reviews.llvm.org/D57264
HOST_CLANG_CONF_OPTS += -DLLVM_TEMPORARILY_ALLOW_OLD_TOOLCHAIN=ON
CLANG_CONF_OPTS += -DLLVM_TEMPORARILY_ALLOW_OLD_TOOLCHAIN=ON
# This option is needed, otherwise multiple shared libs
# (libclangAST.so, libclangBasic.so, libclangFrontend.so, etc.) will
# be generated. As a final shared lib containing all these components
# (libclang.so) is also generated, this resulted in the following
# error when trying to use tools that use libclang:
# $ CommandLine Error: Option 'track-memory' registered more than once!
# $ LLVM ERROR: inconsistency in registered CommandLine options
# By setting BUILD_SHARED_LIBS to OFF, we generate multiple static
# libraries (the same way as host's clang build) and finally
# libclang.so to be installed on the target.
HOST_CLANG_CONF_OPTS += -DBUILD_SHARED_LIBS=OFF
CLANG_CONF_OPTS += -DBUILD_SHARED_LIBS=OFF
# Default is Debug build, which requires considerably more disk space
# and build time. Release build is selected for host and target
# because the linker can run out of memory in Debug mode.
HOST_CLANG_CONF_OPTS += -DCMAKE_BUILD_TYPE=Release
CLANG_CONF_OPTS += -DCMAKE_BUILD_TYPE=Release
CLANG_CONF_OPTS += -DCMAKE_CROSSCOMPILING=1
# We need to build tools because libclang is a tool
HOST_CLANG_CONF_OPTS += -DCLANG_BUILD_TOOLS=ON
CLANG_CONF_OPTS += -DCLANG_BUILD_TOOLS=ON
HOST_CLANG_CONF_OPTS += \
-DCLANG_BUILD_EXAMPLES=OFF \
-DCLANG_INCLUDE_DOCS=OFF \
-DCLANG_INCLUDE_TESTS=OFF
CLANG_CONF_OPTS += \
-DCLANG_BUILD_EXAMPLES=OFF \
-DCLANG_INCLUDE_DOCS=OFF \
-DCLANG_INCLUDE_TESTS=OFF
HOST_CLANG_CONF_OPTS += -DLLVM_DIR=$(HOST_DIR)/lib/cmake/llvm \
-DCLANG_DEFAULT_LINKER=$(TARGET_LD)
CLANG_CONF_OPTS += -DLLVM_DIR=$(STAGING_DIR)/usr/lib/cmake/llvm \
-DCLANG_TABLEGEN:FILEPATH=$(HOST_DIR)/bin/clang-tblgen \
-DLLVM_TABLEGEN_EXE:FILEPATH=$(HOST_DIR)/bin/llvm-tblgen
# Clang can't be used as compiler on the target since there are no
# development files (headers) and other build tools. So remove clang
# binaries and some other unnecessary files from target.
CLANG_FILES_TO_REMOVE = \
/usr/bin/clang* \
/usr/bin/c-index-test \
/usr/bin/git-clang-format \
/usr/bin/scan-build \
/usr/bin/scan-view \
/usr/libexec/c++-analyzer \
/usr/libexec/ccc-analyzer \
/usr/share/clang \
/usr/share/opt-viewer \
/usr/share/scan-build \
/usr/share/scan-view \
/usr/share/man/man1/scan-build.1 \
/usr/lib/clang
define CLANG_CLEANUP_TARGET
rm -rf $(addprefix $(TARGET_DIR),$(CLANG_FILES_TO_REMOVE))
endef
CLANG_POST_INSTALL_TARGET_HOOKS += CLANG_CLEANUP_TARGET
# clang-tblgen is not installed by default, however it is necessary
# for cross-compiling clang
define HOST_CLANG_INSTALL_CLANG_TBLGEN
$(INSTALL) -D -m 0755 $(HOST_CLANG_BUILDDIR)/bin/clang-tblgen \
$(HOST_DIR)/bin/clang-tblgen
endef
HOST_CLANG_POST_INSTALL_HOOKS = HOST_CLANG_INSTALL_CLANG_TBLGEN
# This option must be enabled to link libclang dynamically against libLLVM.so
HOST_CLANG_CONF_OPTS += -DLLVM_LINK_LLVM_DYLIB=ON
CLANG_CONF_OPTS += -DLLVM_LINK_LLVM_DYLIB=ON
# Prevent clang binaries from linking against LLVM static libs
HOST_CLANG_CONF_OPTS += -DLLVM_DYLIB_COMPONENTS=all
CLANG_CONF_OPTS += -DLLVM_DYLIB_COMPONENTS=all
package/clang: help host-clang to find our external toolchain To build libfuzzer package Matthew Weber noticed that (host) clang doesn't run on the host without "-B $(HOST_DIR)/opt/ext-toolchain" option. This option add a new search path for binaries and object files used implicitly. Without -B clang fail to link due to missing crtbeging.o file and libgcc: output/host/bin/aarch64-linux-gnu-ld: cannot find crtbegin.o: No such file or directory output/host/bin/aarch64-linux-gnu-ld: cannot find -lgcc Indeed, clang search path doesn't include the dafault cross-gcc's search paths: $ output/host/bin/clang -print-search-dirs programs: = output/host/bin:output/host/bin:/..//bin libraries: = output/host/lib/clang/8.0.0: output/host/bin/../lib64: /lib/../lib64: /usr/lib/../lib64: output/host/bin/../lib: /lib:/usr/lib Here is the same command for cross-gcc: $ output/host/bin/aarch64-linux-gnu-gcc -print-search-dirs install: output/host/opt/ext-toolchain/bin/../lib/gcc/aarch64-linux-gnu/8.3.0/ programs: = output/host/opt/ext-toolchain/bin/../libexec/gcc/aarch64-linux-gnu/8.3.0/: output/host/opt/ext-toolchain/bin/../libexec/gcc/: output/host/opt/ext-toolchain/bin/../lib/gcc/aarch64-linux-gnu/8.3.0/../../../../aarch64-linux-gnu/bin/aarch64-linux-gnu/8.3.0/: output/host/opt/ext-toolchain/bin/../lib/gcc/aarch64-linux-gnu/8.3.0/../../../../aarch64-linux-gnu/bin/ libraries: = output/opt/ext-toolchain/bin/../lib/gcc/aarch64-linux-gnu/8.3.0/: output/host/opt/ext-toolchain/bin/../lib/gcc/: output/host/opt/ext-toolchain/bin/../lib/gcc/aarch64-linux-gnu/8.3.0/../../../../aarch64-linux-gnu/lib/aarch64-linux-gnu/8.3.0/: output/host/opt/ext-toolchain/bin/../lib/gcc/aarch64-linux-gnu/8.3.0/../../../../aarch64-linux-gnu/lib/../lib64/: output/host/aarch64-buildroot-linux-gnu/sysroot/lib/aarch64-linux-gnu/8.3.0/: output/host/aarch64-buildroot-linux-gnu/sysroot/lib/../lib64/: output/host/aarch64-buildroot-linux-gnu/sysroot/usr/lib/aarch64-linux-gnu/8.3.0/: output/host/aarch64-buildroot-linux-gnu/sysroot/usr/lib/../lib64/: output/host/opt/ext-toolchain/bin/../lib/gcc/aarch64-linux-gnu/8.3.0/../../../../aarch64-linux-gnu/lib/: output/host/aarch64-buildroot-linux-gnu/sysroot/lib/: output/host/aarch64-buildroot-linux-gnu/sysroot/usr/lib/ We can see that gcc default search path contains "output/host/opt/ext-toolchain" directory where the external toolchain has been extracted. Since we want to use clang without additional option like -B, patch clang in order to use GCC_INSTALL_PREFIX instead of using automatic detection (which doesn't work for Buildroot). We eventually want to relocate the Buildroot SDK containing the clang cross-compiler, so we provide a relative path to GCC_INSTALL_PREFIX in order to avoid to hardcode the path to the GCC toolchain. Also the path between clang and the GCC external toolchain is not always the same, we have the following case: * Toolchain to be downloaded and installed The toolchain is extracted into $(HOST_DIR)/opt/ext-toolchain, so the path is "../opt/ext-toolchain". * Pre-installed toolchain The toolchain is localed somewhere in the host filesystem and defined by the user using BR2_TOOLCHAIN_EXTERNAL_PATH. So, set GCC_INSTALL_PREFIX using realpath: -DGCC_INSTALL_PREFIX:PATH=`realpath --relative-to=$(HOST_DIR)/bin/ $(TOOLCHAIN_EXTERNAL_INSTALL_DIR)` When we use a Buildroot's internal toolchain, clang will find theses crt*.o files and libgcc. http://lists.busybox.net/pipermail/buildroot/2019-August/256204.html Signed-off-by: Romain Naour <romain.naour@smile.fr> Cc: Matthew Weber <matthew.weber@rockwellcollins.com> Cc: Valentin Korenblit <valentinkorenblit@gmail.com> Tested-by: Matt Weber <matthew.weber@rockwellcollins.com> Signed-off-by: Arnout Vandecappelle (Essensium/Mind) <arnout@mind.be>
2022-03-23 00:22:22 +01:00
# Help host-clang to find our external toolchain, use a relative path from the clang
# installation directory to the external toolchain installation directory in order to
# not hardcode the toolchain absolute path.
ifeq ($(BR2_TOOLCHAIN_EXTERNAL),y)
HOST_CLANG_CONF_OPTS += -DGCC_INSTALL_PREFIX:PATH=`realpath --relative-to=$(HOST_DIR)/bin/ $(TOOLCHAIN_EXTERNAL_INSTALL_DIR)`
endif
package/clang: install a toolchain-wrapper for the host clang cross-compiler In order to use Clang as a host cross-compiler for Buildroot, we need to provide at least the path to the sysroot (using --sysroot) and some other compiler flags. This series looks to reuse the toolchain wrapper for GCC since Clang support most of the gcc flags used in the Buildroot's toolchain wrapper. The only flag -mfused-madd (deprecated since gcc 4.6) for mips is not supported by clang. Since Clang require gcc >= 5.x this flag can never be used. host-clang refers to an existing GCC-based toolchain (internal or external) for libstdc++. However, a Buildroot external toolchain gets a different BR_CROSS_PATH_SUFFIX. Therefore, we can't reuse the toolchain-wrapper that gets built for the GCC-based toolchain, but instead have to compile an additional clang-specific wrapper, called toolchain-wrapper-clang. After building the clang toolchain wrapper, create the symlinks needed to force package infrastructure to use clang througt the wrapper. Initially clang install the clang-8 binary and create all other symlinks: # clang -> clang-8 # clang++ -> clang # clang-8 # clang-cl -> clang # clang-cpp -> clang Use a post install hook to rename the clang-8 binary to clang-8.br_real and recreate all symlinks: # clang -> toolchain-wrapper-clang # clang++ -> toolchain-wrapper-clang # clang-8 -> toolchain-wrapper-clang # clang-8.br_real # clang++.br_real -> clang-8.br_real # clang.br_real -> clang-8.br_real # clang-cl -> toolchain-wrapper-clang # clang-cl.br_real -> clang-8.br_real # clang-cpp -> toolchain-wrapper-clang # clang-cpp.br_real -> clang-8.br_real NOTE: *.br_real symlinks are needed as the wrapper references them Use the previously introduced CLANG_VERSION_MAJOR variable to create theses symlinks. Set BR_CROSS_PATH_SUFFIX to ".br_real" as for the Buildroot's internal GCC toolchain backend to find the "real" clang binary installed in $(HOST_DIR)/bin. Borrow TOOLCHAIN_WRAPPER_BUILD and TOOLCHAIN_WRAPPER_INSTALL to build and install the specific clang toolchain wrapper. Signed-off-by: Romain Naour <romain.naour@smile.fr> Cc: Valentin Korenblit <valentinkorenblit@gmail.com> Signed-off-by: Matthew Weber <matthew.weber@rockwellcollins.com> Signed-off-by: Arnout Vandecappelle (Essensium/Mind) <arnout@mind.be>
2022-03-23 00:22:23 +01:00
define HOST_CLANG_INSTALL_WRAPPER_AND_SIMPLE_SYMLINKS
$(Q)cd $(HOST_DIR)/bin; \
rm -f clang-$(CLANG_VERSION_MAJOR).br_real; \
mv clang-$(CLANG_VERSION_MAJOR) clang-$(CLANG_VERSION_MAJOR).br_real; \
ln -sf toolchain-wrapper-clang clang-$(CLANG_VERSION_MAJOR); \
for i in clang clang++ clang-cl clang-cpp; do \
ln -snf toolchain-wrapper-clang $$i; \
ln -snf clang-$(CLANG_VERSION_MAJOR).br_real $$i.br_real; \
done
endef
define HOST_CLANG_TOOLCHAIN_WRAPPER_BUILD
$(HOSTCC) $(HOST_CFLAGS) $(TOOLCHAIN_WRAPPER_ARGS) \
-s -Wl,--hash-style=$(TOOLCHAIN_WRAPPER_HASH_STYLE) \
toolchain/toolchain-wrapper.c \
-o $(@D)/toolchain-wrapper-clang
endef
define HOST_CLANG_TOOLCHAIN_WRAPPER_INSTALL
$(INSTALL) -D -m 0755 $(@D)/toolchain-wrapper-clang \
$(HOST_DIR)/bin/toolchain-wrapper-clang
endef
HOST_CLANG_TOOLCHAIN_WRAPPER_ARGS += -DBR_CROSS_PATH_SUFFIX='".br_real"'
HOST_CLANG_POST_BUILD_HOOKS += HOST_CLANG_TOOLCHAIN_WRAPPER_BUILD
HOST_CLANG_POST_INSTALL_HOOKS += HOST_CLANG_TOOLCHAIN_WRAPPER_INSTALL
HOST_CLANG_POST_INSTALL_HOOKS += HOST_CLANG_INSTALL_WRAPPER_AND_SIMPLE_SYMLINKS
$(eval $(cmake-package))
$(eval $(host-cmake-package))