From patchwork Thu Jul 8 17:51:07 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jessica Clarke X-Patchwork-Id: 1502624 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (no SPF record) smtp.mailfrom=lists.infradead.org (client-ip=2607:7c80:54:e::133; helo=bombadil.infradead.org; envelope-from=opensbi-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org; receiver=) Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; secure) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20210309 header.b=ElIkr9/k; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=jrtc27.com header.i=@jrtc27.com header.a=rsa-sha256 header.s=gmail.jrtc27.user header.b=fLwCDh5T; dkim-atps=neutral Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:e::133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4GLP404xmCz9sWc for ; Fri, 9 Jul 2021 03:51:28 +1000 (AEST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=ehjF1ssS3HSfVS21hSlvCoHZ34VCvmQ5tUpmJilyYcw=; b=ElIkr9/kvXsYYy SRLm3RXpO2oFxMP5AYmKDMLCFq+i3efJ7Cv0/KGdAwaZODFXf7Lpv76qQAcZYoSfLljLk84hKIHdw xdIrqBh+TcNxT+HIaOSo+V1FxJomGLYWyGc0tQsD+vHdn9ko7AJC0UlnavhPUW+py3j5A0nETKVKH MjhjTZaCDrzA+RImP08vEAMsaNuWdtDbmPWQ3/awcCHKmMI6ygk8kkewQQ7dxfCcfEjP7ZVbjjCiK 7fEQ5qDuXFQg93LFVAfPDEnwpvXNA2Qf4KX0HWkF0TpGsmjq1bE0wKjLiMLf3L0DNp2PgSZP0dhLi NvQEbK4H9EfwP2UDob/A==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1m1YBD-0000Cr-1J; Thu, 08 Jul 2021 17:51:19 +0000 Received: from mail-wr1-x42f.google.com ([2a00:1450:4864:20::42f]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1m1YB9-0000C9-US for opensbi@lists.infradead.org; Thu, 08 Jul 2021 17:51:17 +0000 Received: by mail-wr1-x42f.google.com with SMTP id r11so3315109wro.9 for ; Thu, 08 Jul 2021 10:51:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=jrtc27.com; s=gmail.jrtc27.user; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=XLr9tMesb9lX8P/HaFMFLtmvUw3OVjhrAD6uYCxuNbQ=; b=fLwCDh5TZMd8GCgc3w6WokZnxphTqtiJ0WtPwIsYnUBkKuPPiRRtn4bxc/Fs4A5nLx mMDTYC+VM+lGQSzk0ItHuTroJ40xXrmLbmz0qoXcLDqBBnf7pYdxN/yMqtGZvQ0ga7HM ISYabTtROWw9ZwFXNONr/h5kD/ZUmsHUi8g8RkYGNtlCi5qqdZfRAojMrblujARjtdgo LvvDRzvv0Gfqv5BbKUOp87hdItgV7LnwbXODwvJb+NHVdjOpoBY+2wOGJrgChQxuYrc2 HD/Nw5OrLUY8nO3G07lIUMNI4A1wwsgV0j+J38bRrMKqvJX0WL0ml/7r70fEwUm4YHGW KUEQ== 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:in-reply-to :references:mime-version:content-transfer-encoding; bh=XLr9tMesb9lX8P/HaFMFLtmvUw3OVjhrAD6uYCxuNbQ=; b=T6nU2/h4HcC+3ntJuwwn2Xy8FLB9DhyNkXVL2WoAoo0p8SKQie9ILUaPLXX6stMbKy jIlhgl//JGZkny7656kLSS9TUJ9xISCLafRKyY2u6oyM3TXujNK7Zaa72aYhrYStOmY1 40v/DmEa6hXRHZOkNoVDQ/Z4SRnXpXHb/Rg1IBRQwGPIBN5VC742wiVv+8Ay0V3T+xUw b7dRlt4ZCifnIm5VIpPYQFPyaEBYq3UDAEjDeVHg6Ef+rLQ+cfY9OLjx8sl4bIfY77QS fcb2HNmC303vMJx9+HHJ2z+p7TmQEzRDZqiWYg5JdSo/n+3Ahvv0Cl5EMFInVqMFnUMl 7vjA== X-Gm-Message-State: AOAM533cen3NyPu+etK+9Qcr4Nwgujjv+W1QjHZEHK4+SR6PAOu4VhJk oFxJgM9HE1VYSjzxrIVwoJ4m+Q== X-Google-Smtp-Source: ABdhPJzWV/MAxFXyi+rkpHb9pz/pRzYMuOXr9ZZ6ygY2BuOLB9sin6jSMAarbvky3DjJJqp7kFL6HA== X-Received: by 2002:adf:f502:: with SMTP id q2mr36193028wro.77.1625766674670; Thu, 08 Jul 2021 10:51:14 -0700 (PDT) Received: from Jessicas-MacBook-Pro.local (trinity-students-nat.trin.cam.ac.uk. [131.111.193.104]) by smtp.gmail.com with ESMTPSA id p15sm2472313wmq.43.2021.07.08.10.51.14 (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Thu, 08 Jul 2021 10:51:14 -0700 (PDT) Received: by Jessicas-MacBook-Pro.local (Postfix, from userid 501) id C248D2822B2D; Thu, 8 Jul 2021 18:51:13 +0100 (BST) From: Jessica Clarke To: opensbi@lists.infradead.org Cc: Jessica Clarke Subject: [PATCH v2 1/2] Makefile: Support building with Clang and LLVM binutils Date: Thu, 8 Jul 2021 18:51:07 +0100 Message-Id: <20210708175108.82365-2-jrtc27@jrtc27.com> X-Mailer: git-send-email 2.31.0 In-Reply-To: <20210708175108.82365-1-jrtc27@jrtc27.com> References: <20210708175108.82365-1-jrtc27@jrtc27.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20210708_105116_024592_735A8D6E X-CRM114-Status: GOOD ( 19.85 ) X-Spam-Score: -0.2 (/) X-Spam-Report: Spam detection software, running on the system "bombadil.infradead.org", has NOT identified this incoming email as spam. The original message has been attached to this so you can view it or label similar future email. If you have any questions, see the administrator of that system for details. Content preview: This is intended to mirror the Linux kernel. Building with CC=clang will use Clang as the compiler but default to using the existing binutils. Building with LLVM=1 will default to using Clang and LLVM [...] Content analysis details: (-0.2 points, 5.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 RCVD_IN_DNSWL_NONE RBL: Sender listed at https://www.dnswl.org/, no trust [2a00:1450:4864:20:0:0:0:42f listed in] [list.dnswl.org] -0.0 SPF_PASS SPF: sender matches SPF record 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -0.1 DKIM_VALID_EF Message has a valid DKIM or DK signature from envelope-from domain X-BeenThere: opensbi@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "opensbi" Errors-To: opensbi-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org This is intended to mirror the Linux kernel. Building with CC=clang will use Clang as the compiler but default to using the existing binutils. Building with LLVM=1 will default to using Clang and LLVM binutils. Whilst GCC will accept the -N linker option and forward it on to the linker, Clang will not, and so in order to support both compilers we must use -Wl, to forward it to the linker as is required for most other linker options. Signed-off-by: Jessica Clarke --- Makefile | 57 +++++++++++++++++++++++++++++++++++++++++++++++++------ README.md | 39 +++++++++++++++++++++++++++++++++++-- 2 files changed, 88 insertions(+), 8 deletions(-) diff --git a/Makefile b/Makefile index 6b64205..3fe8153 100644 --- a/Makefile +++ b/Makefile @@ -76,26 +76,54 @@ OPENSBI_VERSION_MINOR=`grep "define OPENSBI_VERSION_MINOR" $(include_dir)/sbi/sb OPENSBI_VERSION_GIT=$(shell if [ -d $(src_dir)/.git ]; then git describe 2> /dev/null; fi) # Setup compilation commands +ifneq ($(LLVM),) +CC = clang +AR = llvm-ar +LD = ld.lld +OBJCOPY = llvm-objcopy +else ifdef CROSS_COMPILE CC = $(CROSS_COMPILE)gcc -CPP = $(CROSS_COMPILE)cpp AR = $(CROSS_COMPILE)ar LD = $(CROSS_COMPILE)ld OBJCOPY = $(CROSS_COMPILE)objcopy else CC ?= gcc -CPP ?= cpp AR ?= ar LD ?= ld OBJCOPY ?= objcopy endif +endif +CPP = $(CC) -E AS = $(CC) DTC = dtc -# Guess the compillers xlen -OPENSBI_CC_XLEN := $(shell TMP=`$(CC) -dumpmachine | sed 's/riscv\([0-9][0-9]\).*/\1/'`; echo $${TMP}) +ifneq ($(shell $(CC) --version 2>&1 | head -n 1 | grep clang),) +CC_IS_CLANG = y +else +CC_IS_CLANG = n +endif + +ifneq ($(shell $(LD) --version 2>&1 | head -n 1 | grep LLD),) +LD_IS_LLD = y +else +LD_IS_LLD = n +endif + +ifeq ($(CC_IS_CLANG),y) +ifneq ($(CROSS_COMPILE),) +CLANG_TARGET = -target $(notdir $(CROSS_COMPILE:%-=%)) +endif +endif + +# Guess the compiler's XLEN +OPENSBI_CC_XLEN := $(shell TMP=`$(CC) $(CLANG_TARGET) -dumpmachine | sed 's/riscv\([0-9][0-9]\).*/\1/'`; echo $${TMP}) + +# Guess the compiler's ABI and ISA +ifneq ($(CC_IS_CLANG),y) OPENSBI_CC_ABI := $(shell TMP=`$(CC) -v 2>&1 | sed -n 's/.*\(with\-abi=\([a-zA-Z0-9]*\)\).*/\2/p'`; echo $${TMP}) OPENSBI_CC_ISA := $(shell TMP=`$(CC) -v 2>&1 | sed -n 's/.*\(with\-arch=\([a-zA-Z0-9]*\)\).*/\2/p'`; echo $${TMP}) +endif # Setup platform XLEN ifndef PLATFORM_RISCV_XLEN @@ -194,7 +222,11 @@ else endif # Setup compilation commands flags -GENFLAGS = -I$(platform_src_dir)/include +ifeq ($(CC_IS_CLANG),y) +GENFLAGS += $(CLANG_TARGET) +GENFLAGS += -Wno-unused-command-line-argument +endif +GENFLAGS += -I$(platform_src_dir)/include GENFLAGS += -I$(include_dir) ifneq ($(OPENSBI_VERSION_GIT),) GENFLAGS += -DOPENSBI_VERSION_GIT="\"$(OPENSBI_VERSION_GIT)\"" @@ -208,6 +240,9 @@ CFLAGS += -fno-omit-frame-pointer -fno-optimize-sibling-calls CFLAGS += -mno-save-restore -mstrict-align CFLAGS += -mabi=$(PLATFORM_RISCV_ABI) -march=$(PLATFORM_RISCV_ISA) CFLAGS += -mcmodel=$(PLATFORM_RISCV_CODE_MODEL) +ifeq ($(LD_IS_LLD),y) +CFLAGS += -mno-relax +endif CFLAGS += $(GENFLAGS) CFLAGS += $(platform-cflags-y) CFLAGS += -fno-pie -no-pie @@ -222,18 +257,28 @@ ASFLAGS += -fno-omit-frame-pointer -fno-optimize-sibling-calls ASFLAGS += -mno-save-restore -mstrict-align ASFLAGS += -mabi=$(PLATFORM_RISCV_ABI) -march=$(PLATFORM_RISCV_ISA) ASFLAGS += -mcmodel=$(PLATFORM_RISCV_CODE_MODEL) +ifeq ($(LD_IS_LLD),y) +ASFLAGS += -mno-relax +endif ASFLAGS += $(GENFLAGS) ASFLAGS += $(platform-asflags-y) ASFLAGS += $(firmware-asflags-y) ARFLAGS = rcs -ELFFLAGS += -Wl,--build-id=none -N -static-libgcc -lgcc +ifeq ($(LD_IS_LLD),y) +ELFFLAGS += -fuse-ld=lld +endif +ELFFLAGS += -Wl,--build-id=none -Wl,-N -static-libgcc -lgcc ELFFLAGS += $(platform-ldflags-y) ELFFLAGS += $(firmware-ldflags-y) MERGEFLAGS += -r +ifeq ($(LD_IS_LLD),y) +MERGEFLAGS += -b elf +else MERGEFLAGS += -b elf$(PLATFORM_RISCV_XLEN)-littleriscv +endif MERGEFLAGS += -m elf$(PLATFORM_RISCV_XLEN)lriscv DTSCPPFLAGS = $(CPPFLAGS) -nostdinc -nostdlib -fno-builtin -D__DTS__ -x assembler-with-cpp diff --git a/README.md b/README.md index 03c02fb..e97dcc4 100644 --- a/README.md +++ b/README.md @@ -96,8 +96,13 @@ Required Toolchain ------------------ OpenSBI can be compiled natively or cross-compiled on a x86 host. For -cross-compilation, you can build your own toolchain or just download -a prebuilt one from the [Bootlin toolchain repository]. +cross-compilation, you can build your own toolchain, download a prebuilt one +from the [Bootlin toolchain repository] or install a distribution-provided +toolchain; if you opt to use LLVM/Clang, most distribution toolchains will +support cross-compiling for RISC-V using the same toolchain as your native +LLVM/Clang toolchain due to LLVM's ability to support multiple backends in the +same binary, so is often an easy way to obtain a working cross-compilation +toolchain. Please note that only a 64-bit version of the toolchain is available in the Bootlin toolchain repository for now. @@ -202,6 +207,36 @@ export PLATFORM_RISCV_XLEN=32 will generate 32-bit OpenSBI images. And vice vesa. +Building with Clang/LLVM +------------------------ + +OpenSBI can also be built with Clang/LLVM. To build with just Clang but keep +the default binutils (which will still use the *CROSS_COMPILE* prefix if +defined), override the *CC* make variable with: +``` +make CC=clang +``` + +To build with a full LLVM-based toolchain, not just Clang, enable the *LLVM* +option with: +``` +make LLVM=1 +``` + +When using Clang, *CROSS_COMPILE* must be defined if the default target for the +used Clang is not RISC-V. For Clang, rather than being used as a prefix for the +executable name, it will instead be passed via the `-target` option with the +trailing `-` removed, so must be a valid triple. + +These can also be mixed; for example using a GCC cross-compiler but LLVM +binutils would be: +``` +make CC=riscv64-unknown-elf-gcc LLVM=1 +``` + +These variables must be passed for all the make invocations described in this +document. + Contributing to OpenSBI -----------------------