1################################################################################
2#
3# clang
4#
5################################################################################
6
7CLANG_VERSION_MAJOR = $(LLVM_PROJECT_VERSION_MAJOR)
8CLANG_VERSION = $(LLVM_PROJECT_VERSION)
9CLANG_SITE = $(LLVM_PROJECT_SITE)
10CLANG_SOURCE = clang-$(CLANG_VERSION).src.tar.xz
11CLANG_LICENSE = Apache-2.0 with exceptions
12CLANG_LICENSE_FILES = LICENSE.TXT
13CLANG_CPE_ID_VENDOR = llvm
14CLANG_SUPPORTS_IN_SOURCE_BUILD = NO
15CLANG_INSTALL_STAGING = YES
16
17HOST_CLANG_DEPENDENCIES = host-llvm host-libxml2
18CLANG_DEPENDENCIES = llvm host-clang
19
20# This option is needed, otherwise multiple shared libs
21# (libclangAST.so, libclangBasic.so, libclangFrontend.so, etc.) will
22# be generated. As a final shared lib containing all these components
23# (libclang.so) is also generated, this resulted in the following
24# error when trying to use tools that use libclang:
25# $ CommandLine Error: Option 'track-memory' registered more than once!
26# $ LLVM ERROR: inconsistency in registered CommandLine options
27# By setting BUILD_SHARED_LIBS to OFF, we generate multiple static
28# libraries (the same way as host's clang build) and finally
29# libclang.so to be installed on the target.
30HOST_CLANG_CONF_OPTS += -DBUILD_SHARED_LIBS=OFF
31CLANG_CONF_OPTS += -DBUILD_SHARED_LIBS=OFF
32
33# Default is Debug build, which requires considerably more disk space
34# and build time. Release build is selected for host and target
35# because the linker can run out of memory in Debug mode.
36HOST_CLANG_CONF_OPTS += -DCMAKE_BUILD_TYPE=Release
37CLANG_CONF_OPTS += -DCMAKE_BUILD_TYPE=Release
38
39CLANG_CONF_OPTS += -DCMAKE_CROSSCOMPILING=1
40
41# We need to build tools because libclang is a tool
42HOST_CLANG_CONF_OPTS += -DCLANG_BUILD_TOOLS=ON
43CLANG_CONF_OPTS += -DCLANG_BUILD_TOOLS=ON
44
45HOST_CLANG_CONF_OPTS += \
46	-DCLANG_BUILD_EXAMPLES=OFF \
47	-DCLANG_INCLUDE_DOCS=OFF \
48	-DCLANG_INCLUDE_TESTS=OFF
49
50CLANG_CONF_OPTS += \
51	-DCLANG_BUILD_EXAMPLES=OFF \
52	-DCLANG_INCLUDE_DOCS=OFF \
53	-DCLANG_INCLUDE_TESTS=OFF
54
55HOST_CLANG_CONF_OPTS += -DLLVM_DIR=$(HOST_DIR)/lib/cmake/llvm \
56	-DCLANG_DEFAULT_LINKER=$(TARGET_LD)
57CLANG_CONF_OPTS += -DLLVM_DIR=$(STAGING_DIR)/usr/lib/cmake/llvm \
58	-DCMAKE_MODULE_PATH=$(HOST_DIR)/lib/cmake/llvm \
59	-DCLANG_TABLEGEN:FILEPATH=$(HOST_DIR)/bin/clang-tblgen \
60	-DLLVM_TABLEGEN_EXE:FILEPATH=$(HOST_DIR)/bin/llvm-tblgen
61
62# Clang can't be used as compiler on the target since there are no
63# development files (headers) and other build tools. So remove clang
64# binaries and some other unnecessary files from target.
65CLANG_FILES_TO_REMOVE = \
66	/usr/bin/clang* \
67	/usr/bin/c-index-test \
68	/usr/bin/git-clang-format \
69	/usr/bin/scan-build \
70	/usr/bin/scan-view \
71	/usr/libexec/c++-analyzer \
72	/usr/libexec/ccc-analyzer \
73	/usr/share/clang \
74	/usr/share/opt-viewer \
75	/usr/share/scan-build \
76	/usr/share/scan-view \
77	/usr/share/man/man1/scan-build.1 \
78	/usr/lib/clang
79
80define CLANG_CLEANUP_TARGET
81	rm -rf $(addprefix $(TARGET_DIR),$(CLANG_FILES_TO_REMOVE))
82endef
83CLANG_POST_INSTALL_TARGET_HOOKS += CLANG_CLEANUP_TARGET
84
85# clang-tblgen is not installed by default, however it is necessary
86# for cross-compiling clang
87define HOST_CLANG_INSTALL_CLANG_TBLGEN
88	$(INSTALL) -D -m 0755 $(HOST_CLANG_BUILDDIR)/bin/clang-tblgen \
89		$(HOST_DIR)/bin/clang-tblgen
90endef
91HOST_CLANG_POST_INSTALL_HOOKS = HOST_CLANG_INSTALL_CLANG_TBLGEN
92
93# This option must be enabled to link libclang dynamically against libLLVM.so
94HOST_CLANG_CONF_OPTS += -DLLVM_LINK_LLVM_DYLIB=ON
95CLANG_CONF_OPTS += -DLLVM_LINK_LLVM_DYLIB=ON
96
97# Prevent clang binaries from linking against LLVM static libs
98HOST_CLANG_CONF_OPTS += -DLLVM_DYLIB_COMPONENTS=all
99CLANG_CONF_OPTS += -DLLVM_DYLIB_COMPONENTS=all
100
101# Help host-clang to find our external toolchain, use a relative path from the clang
102# installation directory to the external toolchain installation directory in order to
103# not hardcode the toolchain absolute path.
104ifeq ($(BR2_TOOLCHAIN_EXTERNAL),y)
105HOST_CLANG_CONF_OPTS += -DGCC_INSTALL_PREFIX:PATH=`realpath --relative-to=$(HOST_DIR)/bin/ $(TOOLCHAIN_EXTERNAL_INSTALL_DIR)`
106endif
107
108define HOST_CLANG_INSTALL_WRAPPER_AND_SIMPLE_SYMLINKS
109	$(Q)cd $(HOST_DIR)/bin; \
110	rm -f clang-$(CLANG_VERSION_MAJOR).br_real; \
111	mv clang-$(CLANG_VERSION_MAJOR) clang-$(CLANG_VERSION_MAJOR).br_real; \
112	ln -sf toolchain-wrapper-clang clang-$(CLANG_VERSION_MAJOR); \
113	for i in clang clang++ clang-cl clang-cpp; do \
114		ln -snf toolchain-wrapper-clang $$i; \
115		ln -snf clang-$(CLANG_VERSION_MAJOR).br_real $$i.br_real; \
116	done
117endef
118
119define HOST_CLANG_TOOLCHAIN_WRAPPER_BUILD
120	$(HOSTCC) $(HOST_CFLAGS) $(TOOLCHAIN_WRAPPER_ARGS) \
121		-s -Wl,--hash-style=$(TOOLCHAIN_WRAPPER_HASH_STYLE) \
122		toolchain/toolchain-wrapper.c \
123		-o $(@D)/toolchain-wrapper-clang
124endef
125
126define HOST_CLANG_TOOLCHAIN_WRAPPER_INSTALL
127	$(INSTALL) -D -m 0755 $(@D)/toolchain-wrapper-clang \
128		$(HOST_DIR)/bin/toolchain-wrapper-clang
129endef
130
131HOST_CLANG_TOOLCHAIN_WRAPPER_ARGS += -DBR_CROSS_PATH_SUFFIX='".br_real"'
132HOST_CLANG_POST_BUILD_HOOKS += HOST_CLANG_TOOLCHAIN_WRAPPER_BUILD
133HOST_CLANG_POST_INSTALL_HOOKS += HOST_CLANG_TOOLCHAIN_WRAPPER_INSTALL
134HOST_CLANG_POST_INSTALL_HOOKS += HOST_CLANG_INSTALL_WRAPPER_AND_SIMPLE_SYMLINKS
135
136$(eval $(cmake-package))
137$(eval $(host-cmake-package))
138