Browse Source

kernel.mk: Support kernels that use LTO

Google added support for LTO + CFI in the Pixel 3 kernel, which requires
not only Clang but a couple of additional binaries, namely llvm-ar and
llvm-dis for IR generation. Google expects these binaries to be in PATH
according to their definitions and the build.config.common file:

https://android.googlesource.com/kernel/msm/+/android-9.0.0_r0.31/Makefile#637
https://android.googlesource.com/kernel/msm/+/android-9.0.0_r0.31/build.config.common

However, kernel.mk does not add the LLVM bin directory to PATH so the
build fails because the binaries can't be found. We could add LLVM_AR
and LLVM_DIS to the make commands like CC and CROSS_COMPILE; however,
adding the bin directory to PATH is a more sustainable solution as
Google might require new binaries in the future.

Additionally, LTO needs access to the LLVMgold library so it needs to be
available in LD_LIBRARY_PATH.

Add a PATH_OVERRIDE variable that will set up PATH and LD_LIBRARY_PATH
with the bin and lib64 folders from the requested LLVM toolchain
respectively when building with Clang. This won't affect kernels that
don't use LTO like Wahoo.

Change-Id: Ib16fa0d3180de2f96accb2f7648b07a017f8f98b
Signed-off-by: Nathan Chancellor <natechancellor@gmail.com>
Nathan Chancellor 1 year ago
parent
commit
f3e49d3e0a
1 changed files with 16 additions and 15 deletions
  1. 16
    15
      build/tasks/kernel.mk

+ 16
- 15
build/tasks/kernel.mk View File

@@ -178,8 +178,9 @@ ifeq ($(TARGET_KERNEL_CLANG_COMPILE),true)
178 178
     else ifeq ($(KERNEL_ARCH),x86)
179 179
         KERNEL_CLANG_TRIPLE ?= CLANG_TRIPLE=x86_64-linux-gnu-
180 180
     endif
181
+    PATH_OVERRIDE := PATH=$(TARGET_KERNEL_CLANG_PATH):$$PATH LD_LIBRARY_PATH=$(BUILD_TOP)/prebuilts/clang/host/$(HOST_OS)-x86/$(KERNEL_CLANG_VERSION)/lib64:$$LD_LIBRARY_PATH
181 182
     ifeq ($(KERNEL_CC),)
182
-        KERNEL_CC := CC="$(CCACHE_BIN) $(TARGET_KERNEL_CLANG_PATH)/clang"
183
+        KERNEL_CC := CC="$(CCACHE_BIN) clang"
183 184
     endif
184 185
 endif
185 186
 
@@ -196,17 +197,17 @@ $(KERNEL_ADDITIONAL_CONFIG_OUT): force_additional_config
196 197
 $(KERNEL_CONFIG): $(KERNEL_DEFCONFIG_SRC) $(KERNEL_ADDITIONAL_CONFIG_OUT)
197 198
 	@echo "Building Kernel Config"
198 199
 	$(hide) mkdir -p $(KERNEL_OUT)
199
-	$(MAKE) $(KERNEL_MAKE_FLAGS) -C $(KERNEL_SRC) O=$(KERNEL_OUT) ARCH=$(KERNEL_ARCH) $(KERNEL_CROSS_COMPILE) $(KERNEL_CLANG_TRIPLE) $(KERNEL_CC) VARIANT_DEFCONFIG=$(VARIANT_DEFCONFIG) SELINUX_DEFCONFIG=$(SELINUX_DEFCONFIG) $(KERNEL_DEFCONFIG)
200
+	$(PATH_OVERRIDE) $(MAKE) $(KERNEL_MAKE_FLAGS) -C $(KERNEL_SRC) O=$(KERNEL_OUT) ARCH=$(KERNEL_ARCH) $(KERNEL_CROSS_COMPILE) $(KERNEL_CLANG_TRIPLE) $(KERNEL_CC) VARIANT_DEFCONFIG=$(VARIANT_DEFCONFIG) SELINUX_DEFCONFIG=$(SELINUX_DEFCONFIG) $(KERNEL_DEFCONFIG)
200 201
 	$(hide) if [ ! -z "$(KERNEL_CONFIG_OVERRIDE)" ]; then \
201 202
 			echo "Overriding kernel config with '$(KERNEL_CONFIG_OVERRIDE)'"; \
202 203
 			echo $(KERNEL_CONFIG_OVERRIDE) >> $(KERNEL_OUT)/.config; \
203
-			$(MAKE) -C $(KERNEL_SRC) O=$(KERNEL_OUT) ARCH=$(KERNEL_ARCH) $(KERNEL_CROSS_COMPILE) $(KERNEL_CLANG_TRIPLE) $(KERNEL_CC) oldconfig; fi
204
+			$(PATH_OVERRIDE) $(MAKE) -C $(KERNEL_SRC) O=$(KERNEL_OUT) ARCH=$(KERNEL_ARCH) $(KERNEL_CROSS_COMPILE) $(KERNEL_CLANG_TRIPLE) $(KERNEL_CC) oldconfig; fi
204 205
 	# Create defconfig build artifact
205
-	$(hide) $(MAKE) -C $(KERNEL_SRC) O=$(KERNEL_OUT) ARCH=$(KERNEL_ARCH) $(KERNEL_CROSS_COMPILE) $(KERNEL_CLANG_TRIPLE) $(KERNEL_CC) savedefconfig
206
+	$(hide) $(PATH_OVERRIDE) $(MAKE) -C $(KERNEL_SRC) O=$(KERNEL_OUT) ARCH=$(KERNEL_ARCH) $(KERNEL_CROSS_COMPILE) $(KERNEL_CLANG_TRIPLE) $(KERNEL_CC) savedefconfig
206 207
 	$(hide) if [ ! -z "$(KERNEL_ADDITIONAL_CONFIG)" ]; then \
207 208
 			echo "Using additional config '$(KERNEL_ADDITIONAL_CONFIG)'"; \
208 209
 			$(KERNEL_SRC)/scripts/kconfig/merge_config.sh -m -O $(KERNEL_OUT) $(KERNEL_OUT)/.config $(KERNEL_SRC)/arch/$(KERNEL_ARCH)/configs/$(KERNEL_ADDITIONAL_CONFIG); \
209
-			$(MAKE) -C $(KERNEL_SRC) O=$(KERNEL_OUT) ARCH=$(KERNEL_ARCH) $(KERNEL_CROSS_COMPILE) $(KERNEL_CLANG_TRIPLE) $(KERNEL_CC) KCONFIG_ALLCONFIG=$(KERNEL_OUT)/.config alldefconfig; fi
210
+			$(PATH_OVERRIDE) $(MAKE) -C $(KERNEL_SRC) O=$(KERNEL_OUT) ARCH=$(KERNEL_ARCH) $(KERNEL_CROSS_COMPILE) $(KERNEL_CLANG_TRIPLE) $(KERNEL_CC) KCONFIG_ALLCONFIG=$(KERNEL_OUT)/.config alldefconfig; fi
210 211
 
211 212
 .PHONY: TARGET_KERNEL_BINARIES
212 213
 TARGET_KERNEL_BINARIES: $(KERNEL_CONFIG)
@@ -214,21 +215,21 @@ TARGET_KERNEL_BINARIES: $(KERNEL_CONFIG)
214 215
 	$(hide) rm -rf $(KERNEL_MODULES_OUT)
215 216
 	$(hide) mkdir -p $(KERNEL_MODULES_OUT)
216 217
 	$(hide) rm -rf $(KERNEL_DEPMOD_STAGING_DIR)
217
-	$(MAKE) $(KERNEL_MAKE_FLAGS) -C $(KERNEL_SRC) O=$(KERNEL_OUT) ARCH=$(KERNEL_ARCH) $(KERNEL_CROSS_COMPILE) $(KERNEL_CLANG_TRIPLE) $(KERNEL_CC) $(BOARD_KERNEL_IMAGE_NAME)
218
+	$(PATH_OVERRIDE) $(MAKE) $(KERNEL_MAKE_FLAGS) -C $(KERNEL_SRC) O=$(KERNEL_OUT) ARCH=$(KERNEL_ARCH) $(KERNEL_CROSS_COMPILE) $(KERNEL_CLANG_TRIPLE) $(KERNEL_CC) $(BOARD_KERNEL_IMAGE_NAME)
218 219
 	$(hide) if grep -q '^CONFIG_OF=y' $(KERNEL_CONFIG); then \
219 220
 			echo "Building DTBs"; \
220
-			$(MAKE) $(KERNEL_MAKE_FLAGS) -C $(KERNEL_SRC) O=$(KERNEL_OUT) ARCH=$(KERNEL_ARCH) $(KERNEL_CROSS_COMPILE) $(KERNEL_CLANG_TRIPLE) $(KERNEL_CC) dtbs; \
221
+			$(PATH_OVERRIDE) $(MAKE) $(KERNEL_MAKE_FLAGS) -C $(KERNEL_SRC) O=$(KERNEL_OUT) ARCH=$(KERNEL_ARCH) $(KERNEL_CROSS_COMPILE) $(KERNEL_CLANG_TRIPLE) $(KERNEL_CC) dtbs; \
221 222
 		fi
222 223
 	$(hide) if grep -q '^CONFIG_MODULES=y' $(KERNEL_CONFIG); then \
223 224
 			echo "Building Kernel Modules"; \
224
-			$(MAKE) $(KERNEL_MAKE_FLAGS) -C $(KERNEL_SRC) O=$(KERNEL_OUT) ARCH=$(KERNEL_ARCH) $(KERNEL_CROSS_COMPILE) $(KERNEL_CLANG_TRIPLE) $(KERNEL_CC) modules; \
225
+			$(PATH_OVERRIDE) $(MAKE) $(KERNEL_MAKE_FLAGS) -C $(KERNEL_SRC) O=$(KERNEL_OUT) ARCH=$(KERNEL_ARCH) $(KERNEL_CROSS_COMPILE) $(KERNEL_CLANG_TRIPLE) $(KERNEL_CC) modules; \
225 226
 		fi
226 227
 
227 228
 .PHONY: INSTALLED_KERNEL_MODULES
228 229
 INSTALLED_KERNEL_MODULES: depmod-host
229 230
 	$(hide) if grep -q '^CONFIG_MODULES=y' $(KERNEL_CONFIG); then \
230 231
 			echo "Installing Kernel Modules"; \
231
-			$(MAKE) $(KERNEL_MAKE_FLAGS) -C $(KERNEL_SRC) O=$(KERNEL_OUT) ARCH=$(KERNEL_ARCH) $(KERNEL_CROSS_COMPILE) $(KERNEL_CLANG_TRIPLE) $(KERNEL_CC) INSTALL_MOD_PATH=../../$(KERNEL_MODULES_INSTALL) modules_install && \
232
+			$(PATH_OVERRIDE) $(MAKE) $(KERNEL_MAKE_FLAGS) -C $(KERNEL_SRC) O=$(KERNEL_OUT) ARCH=$(KERNEL_ARCH) $(KERNEL_CROSS_COMPILE) $(KERNEL_CLANG_TRIPLE) $(KERNEL_CC) INSTALL_MOD_PATH=../../$(KERNEL_MODULES_INSTALL) modules_install && \
232 233
 			mofile=$$(find $(KERNEL_MODULES_OUT) -type f -name modules.order) && \
233 234
 			mpath=$$(dirname $$mofile) && \
234 235
 			for f in $$(find $$mpath/kernel -type f -name '*.ko'); do \
@@ -249,29 +250,29 @@ $(TARGET_PREBUILT_INT_KERNEL): $(TARGET_KERNEL_MODULES)
249 250
 .PHONY: kerneltags
250 251
 kerneltags: $(KERNEL_CONFIG)
251 252
 	$(hide) mkdir -p $(KERNEL_OUT)
252
-	$(MAKE) -C $(KERNEL_SRC) O=$(KERNEL_OUT) ARCH=$(KERNEL_ARCH) $(KERNEL_CROSS_COMPILE) $(KERNEL_CLANG_TRIPLE) $(KERNEL_CC) tags
253
+	$(PATH_OVERRIDE) $(MAKE) -C $(KERNEL_SRC) O=$(KERNEL_OUT) ARCH=$(KERNEL_ARCH) $(KERNEL_CROSS_COMPILE) $(KERNEL_CLANG_TRIPLE) $(KERNEL_CC) tags
253 254
 
254 255
 .PHONY: kernelsavedefconfig alldefconfig
255 256
 
256 257
 kernelsavedefconfig:
257 258
 	$(hide) mkdir -p $(KERNEL_OUT)
258
-	$(MAKE) $(KERNEL_MAKE_FLAGS) -C $(KERNEL_SRC) O=$(KERNEL_OUT) ARCH=$(KERNEL_ARCH) $(KERNEL_CROSS_COMPILE) $(KERNEL_CLANG_TRIPLE) $(KERNEL_CC) $(KERNEL_DEFCONFIG)
259
+	$(PATH_OVERRIDE) $(MAKE) $(KERNEL_MAKE_FLAGS) -C $(KERNEL_SRC) O=$(KERNEL_OUT) ARCH=$(KERNEL_ARCH) $(KERNEL_CROSS_COMPILE) $(KERNEL_CLANG_TRIPLE) $(KERNEL_CC) $(KERNEL_DEFCONFIG)
259 260
 	env KCONFIG_NOTIMESTAMP=true \
260
-		 $(MAKE) -C $(KERNEL_SRC) O=$(KERNEL_OUT) ARCH=$(KERNEL_ARCH) $(KERNEL_CROSS_COMPILE) $(KERNEL_CLANG_TRIPLE) $(KERNEL_CC) savedefconfig
261
+		 $(PATH_OVERRIDE) $(MAKE) -C $(KERNEL_SRC) O=$(KERNEL_OUT) ARCH=$(KERNEL_ARCH) $(KERNEL_CROSS_COMPILE) $(KERNEL_CLANG_TRIPLE) $(KERNEL_CC) savedefconfig
261 262
 	cp $(KERNEL_OUT)/defconfig $(KERNEL_DEFCONFIG_SRC)
262 263
 
263 264
 alldefconfig:
264 265
 	$(hide) mkdir -p $(KERNEL_OUT)
265 266
 	env KCONFIG_NOTIMESTAMP=true \
266
-		 $(MAKE) -C $(KERNEL_SRC) O=$(KERNEL_OUT) ARCH=$(KERNEL_ARCH) $(KERNEL_CROSS_COMPILE) $(KERNEL_CLANG_TRIPLE) $(KERNEL_CC) alldefconfig
267
+		 $(PATH_OVERRIDE) $(MAKE) -C $(KERNEL_SRC) O=$(KERNEL_OUT) ARCH=$(KERNEL_ARCH) $(KERNEL_CROSS_COMPILE) $(KERNEL_CLANG_TRIPLE) $(KERNEL_CC) alldefconfig
267 268
 
268 269
 endif # FULL_KERNEL_BUILD
269 270
 
270 271
 TARGET_PREBUILT_DTBO = $(PRODUCT_OUT)/dtbo/arch/$(KERNEL_ARCH)/boot/dtbo.img
271 272
 $(TARGET_PREBUILT_DTBO): $(AVBTOOL)
272 273
 	echo -e ${CL_GRN}"Building DTBO.img"${CL_RST}
273
-	$(MAKE) $(KERNEL_MAKE_FLAGS) -C $(KERNEL_SRC) O=$(PRODUCT_OUT)/dtbo ARCH=$(KERNEL_ARCH) $(KERNEL_CROSS_COMPILE) $(KERNEL_CLANG_TRIPLE) $(KERNEL_CC) $(KERNEL_DEFCONFIG)
274
-	$(MAKE) $(KERNEL_MAKE_FLAGS) -C $(KERNEL_SRC) O=$(PRODUCT_OUT)/dtbo ARCH=$(KERNEL_ARCH) $(KERNEL_CROSS_COMPILE) $(KERNEL_CLANG_TRIPLE) $(KERNEL_CC) dtbo.img
274
+	$(PATH_OVERRIDE) $(MAKE) $(KERNEL_MAKE_FLAGS) -C $(KERNEL_SRC) O=$(PRODUCT_OUT)/dtbo ARCH=$(KERNEL_ARCH) $(KERNEL_CROSS_COMPILE) $(KERNEL_CLANG_TRIPLE) $(KERNEL_CC) $(KERNEL_DEFCONFIG)
275
+	$(PATH_OVERRIDE) $(MAKE) $(KERNEL_MAKE_FLAGS) -C $(KERNEL_SRC) O=$(PRODUCT_OUT)/dtbo ARCH=$(KERNEL_ARCH) $(KERNEL_CROSS_COMPILE) $(KERNEL_CLANG_TRIPLE) $(KERNEL_CC) dtbo.img
275 276
 	$(AVBTOOL) add_hash_footer \
276 277
 		--image $@ \
277 278
 		--partition_size $(BOARD_DTBOIMG_PARTITION_SIZE) \