From patchwork Mon Apr 10 21:09:04 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Matthias Kaehlcke X-Patchwork-Id: 9674287 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 92DD9600CB for ; Mon, 10 Apr 2017 21:09:55 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 834E9284F3 for ; Mon, 10 Apr 2017 21:09:55 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 7685E284F7; Mon, 10 Apr 2017 21:09:55 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 10E47284F3 for ; Mon, 10 Apr 2017 21:09:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751799AbdDJVJx (ORCPT ); Mon, 10 Apr 2017 17:09:53 -0400 Received: from mail-pf0-f170.google.com ([209.85.192.170]:33142 "EHLO mail-pf0-f170.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751434AbdDJVJw (ORCPT ); Mon, 10 Apr 2017 17:09:52 -0400 Received: by mail-pf0-f170.google.com with SMTP id s16so38777201pfs.0 for ; Mon, 10 Apr 2017 14:09:52 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=1ITYdbXZoD3ZF0akS7Vw/1jMFtiLfTn/lum/G2BVRXc=; b=jmLdyhXgynLJrdrn0B31VCOC9d1A9/+OXEYmv+uUZdqWqly2m2aMm9aw+qSAOUBFCL wF/T2TUVblmgzlyA78xNVjGzYDS2xquRGy5aEoUlR+Z/xGMuaw/9Ok4NKDJmlUS6eJj3 DPZyc7QTo0wx7JQGMyz4t7JyJZ5QZnkZuEtzuPIR5sG1uDcX52zcnHsAvvFfGZoRLXn7 t7YN60Psb2rsCE4rQBzwLDaWFStduHpt7iURIM8mMxMxf4+Zg2NzLUqiLnGG+QrTUovs 2SmgUl+az4GrNgpwS91b+bB4qjEIm1MscbVlEoKrMsX8KuA9pZyExA+fp1OW8dL8q+Xo 6w1Q== X-Gm-Message-State: AFeK/H23JdifDTcCuT5vvdKs+mMCVTSAn50VrXhLsX56WA8HkV0oPk5h5uB5GAW6PRZFSSBe X-Received: by 10.84.215.23 with SMTP id k23mr68607490pli.58.1491858591546; Mon, 10 Apr 2017 14:09:51 -0700 (PDT) Received: from mka.mtv.corp.google.com ([172.22.64.162]) by smtp.gmail.com with ESMTPSA id p28sm5160640pfd.53.2017.04.10.14.09.50 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 10 Apr 2017 14:09:50 -0700 (PDT) From: Matthias Kaehlcke To: Michal Marek Cc: linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org, Behan Webster , Grant Grundler , Greg Hackmann , Michael Davidson , Peter Foley , Matthias Kaehlcke Subject: [PATCH v2] kbuild: Add better clang cross build support Date: Mon, 10 Apr 2017 14:09:04 -0700 Message-Id: <20170410210904.68211-1-mka@chromium.org> X-Mailer: git-send-email 2.12.2.715.g7642488e1d-goog MIME-Version: 1.0 Sender: linux-kbuild-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kbuild@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Add cross target to CC if using clang. Also add custom gcc toolchain path for fallback gcc tools. COMPILER is previously set to "clang" if CC=clang was set from the make command line. So -target and -gcc-toolchain can be added to CC, since we already know that it is set. Clang will fallback to using things like ld, as, and libgcc if (respectively) one of the llvm linkers isn't available, the integrated assembler is turned off, or an appropriately cross-compiled version of compiler-rt isn't available. To this end, you can specify the path to this fallback gcc toolchain with GCC_TOOLCHAIN. From: Behan Webster Signed-off-by: Behan Webster Reviewed-by: Jan-Simon Möller Reviewed-by: Mark Charlebois Signed-off-by: Greg Hackmann Signed-off-by: Matthias Kaehlcke --- Changes in v2: - Fix GCC_TOOLCHAIN definition. $(LD) is needed to link the kernel, use its path to determine the installation prefix of the toolchain. (From: Greg Hackmann ) Makefile | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index e11989d36c87..d3a02786bb53 100644 --- a/Makefile +++ b/Makefile @@ -375,6 +375,16 @@ LDFLAGS_vmlinux = CFLAGS_GCOV = -fprofile-arcs -ftest-coverage -fno-tree-loop-im -Wno-maybe-uninitialized CFLAGS_KCOV := $(call cc-option,-fsanitize-coverage=trace-pc,) +ifeq ($(cc-name),clang) +ifneq ($(CROSS_COMPILE),) +CLANG_TARGET := -target $(notdir $(CROSS_COMPILE:%-=%)) +GCC_TOOLCHAIN := $(realpath $(dir $(shell which $(LD)))/..) +endif +ifneq ($(GCC_TOOLCHAIN),) +CLANG_GCC_TC := -gcc-toolchain $(GCC_TOOLCHAIN) +endif +CLANG_FLAGS := $(CLANG_TARGET) $(CLANG_GCC_TC) +endif # Use USERINCLUDE when you must reference the UAPI directories only. USERINCLUDE := \ @@ -395,18 +405,18 @@ LINUXINCLUDE := \ LINUXINCLUDE += $(filter-out $(LINUXINCLUDE),$(USERINCLUDE)) -KBUILD_CPPFLAGS := -D__KERNEL__ +KBUILD_CPPFLAGS := -D__KERNEL__ $(CLANG_FLAGS) KBUILD_CFLAGS := -Wall -Wundef -Wstrict-prototypes -Wno-trigraphs \ -fno-strict-aliasing -fno-common \ -Werror-implicit-function-declaration \ -Wno-format-security \ - -std=gnu89 $(call cc-option,-fno-PIE) - + -std=gnu89 $(call cc-option,-fno-PIE) \ + $(CLANG_FLAGS) KBUILD_AFLAGS_KERNEL := KBUILD_CFLAGS_KERNEL := -KBUILD_AFLAGS := -D__ASSEMBLY__ $(call cc-option,-fno-PIE) +KBUILD_AFLAGS := -D__ASSEMBLY__ $(call cc-option,-fno-PIE) $(CLANG_FLAGS) KBUILD_AFLAGS_MODULE := -DMODULE KBUILD_CFLAGS_MODULE := -DMODULE KBUILD_LDFLAGS_MODULE := -T $(srctree)/scripts/module-common.lds