blob: 13e6d8ad96704e8be526115997fcfda902f16a95 [file] [log] [blame]
Joakim Bech427dd632015-05-04 15:52:33 +02001################################################################################
2# Toolchains
3################################################################################
Yunli Liu904f1482021-01-22 11:08:08 +08004SHELL = /bin/bash
Joakim Bechc9606632017-01-27 11:50:49 +01005ROOT ?= $(CURDIR)/..
Joakim Bech427dd632015-05-04 15:52:33 +02006TOOLCHAIN_ROOT ?= $(ROOT)/toolchains
Jens Wiklander68f7f8d2021-06-07 16:02:24 +00007UNAME_M := $(shell uname -m)
Alvin Chang344f7712023-07-25 23:28:32 +08008ARCH ?= arm
Joakim Bech427dd632015-05-04 15:52:33 +02009
Joakim Bechc9606632017-01-27 11:50:49 +010010# Download toolchain macro for saving some repetition
11# $(1) is $AARCH.._PATH : i.e., path to the destination
12# $(2) is $SRC_AARCH.._GCC : is the downloaded tar.gz file
13# $(3) is $.._GCC_VERSION : the name of the file to download
14define dltc
15 @if [ ! -d "$(1)" ]; then \
Joakim Bechc9606632017-01-27 11:50:49 +010016 echo "Downloading $(3) ..."; \
Aleksey Kazantsevc551e2a2021-07-15 11:08:47 +030017 mkdir -p $(1); \
Jerome Forissier3408c892023-05-04 13:58:35 +020018 curl --retry 5 -k -s -S -L $(2) -o $(TOOLCHAIN_ROOT)/$(3).tar.xz || \
Aleksey Kazantsevc551e2a2021-07-15 11:08:47 +030019 { rm -f $(TOOLCHAIN_ROOT)/$(3).tar.xz; cd $(TOOLCHAIN_ROOT) && rmdir $(1); echo Download failed; exit 1; }; \
20 tar xf $(TOOLCHAIN_ROOT)/$(3).tar.xz -C $(1) --strip-components=1 || \
21 { rm $(TOOLCHAIN_ROOT)/$(3).tar.xz; echo Downloaded file is damaged; \
22 cd $(TOOLCHAIN_ROOT) && rm -rf $(1); exit 1; }; \
Alvin Chang4a1fef82023-05-17 00:18:01 +080023 (cd $(1)/bin && shopt -s nullglob && for f in *-none-linux*; do ln -s $$f $${f//-none} ; done;) \
Joakim Bechc9606632017-01-27 11:50:49 +010024 fi
25endef
26
Sumit Garg495dc6a2021-12-23 14:45:36 +053027# Build buildroot toolchain macro for saving some repetition
28# $(1) is $ARCH : target architecture
29# $(2) is $AARCH.._PATH : i.e., path to the destination
30# $(3) & $(4) : parts of toolchain target triplet
31define build_toolchain
32 @echo Building $1 toolchain
33 @mkdir -p ../out-$1-sdk $2
Imre Kisdc34f492023-04-11 16:53:06 +020034 @(cd .. && $(PYTHON3) build/br-ext/scripts/make_def_config.py \
Sumit Garg495dc6a2021-12-23 14:45:36 +053035 --br buildroot --out out-$1-sdk --br-ext build/br-ext \
36 --top-dir "$(ROOT)" \
37 --br-defconfig build/br-ext/configs/sdk-$1 \
38 --br-defconfig build/br-ext/configs/sdk-common \
39 --make-cmd $(MAKE))
Jerome Forissier80164982022-08-03 14:59:39 +000040 +@$(MAKE) -C ../out-$1-sdk clean
41 +@$(MAKE) -C ../out-$1-sdk sdk
Sumit Garg495dc6a2021-12-23 14:45:36 +053042 @tar xf ../out-$1-sdk/images/$3-buildroot-linux-$4_sdk-buildroot.tar.gz \
43 -C $2 --strip-components=1
44 @touch $2/.done
45endef
46
47ifeq ($(UNAME_M),x86_64)
Alvin Chang344f7712023-07-25 23:28:32 +080048ifeq ($(ARCH),arm)
Sumit Garg495dc6a2021-12-23 14:45:36 +053049AARCH32_PATH ?= $(TOOLCHAIN_ROOT)/aarch32
50AARCH32_CROSS_COMPILE ?= $(AARCH32_PATH)/bin/arm-linux-gnueabihf-
Jerome Forissier19b7b072023-02-09 16:10:13 +010051AARCH32_GCC_VERSION ?= arm-gnu-toolchain-11.3.rel1-x86_64-arm-none-linux-gnueabihf
52SRC_AARCH32_GCC ?= https://developer.arm.com/-/media/Files/downloads/gnu/11.3.rel1/binrel/$(AARCH32_GCC_VERSION).tar.xz
Sumit Garg495dc6a2021-12-23 14:45:36 +053053
54AARCH64_PATH ?= $(TOOLCHAIN_ROOT)/aarch64
55AARCH64_CROSS_COMPILE ?= $(AARCH64_PATH)/bin/aarch64-linux-gnu-
Jerome Forissier19b7b072023-02-09 16:10:13 +010056AARCH64_GCC_VERSION ?= arm-gnu-toolchain-11.3.rel1-x86_64-aarch64-none-linux-gnu
57SRC_AARCH64_GCC ?= https://developer.arm.com/-/media/Files/downloads/gnu/11.3.rel1/binrel/$(AARCH64_GCC_VERSION).tar.xz
Sumit Garg495dc6a2021-12-23 14:45:36 +053058
Sumit Garg857eaa02024-01-25 11:43:29 +053059RUST_TOOLCHAIN_PATH ?= $(TOOLCHAIN_ROOT)/rust
60
Joakim Bechc9606632017-01-27 11:50:49 +010061.PHONY: toolchains
Sumit Garg857eaa02024-01-25 11:43:29 +053062toolchains: aarch32-toolchain aarch64-toolchain rust-toolchain
Joakim Bech427dd632015-05-04 15:52:33 +020063
Sumit Garg50458912024-01-25 11:37:50 +053064.PHONY: aarch32-toolchain
65aarch32-toolchain:
Joakim Bechc9606632017-01-27 11:50:49 +010066 $(call dltc,$(AARCH32_PATH),$(SRC_AARCH32_GCC),$(AARCH32_GCC_VERSION))
Joakim Bech427dd632015-05-04 15:52:33 +020067
Sumit Garg50458912024-01-25 11:37:50 +053068.PHONY: aarch64-toolchain
69aarch64-toolchain:
Joakim Bechc9606632017-01-27 11:50:49 +010070 $(call dltc,$(AARCH64_PATH),$(SRC_AARCH64_GCC),$(AARCH64_GCC_VERSION))
Jerome Forissierc3cd9f52020-05-25 18:47:00 +020071
Sumit Garg857eaa02024-01-25 11:43:29 +053072# Download the Rust toolchain
73define dl-rust-toolchain
74 @if [ ! -d "$(1)" ]; then \
75 mkdir -p $(1) && \
76 export RUSTUP_HOME=$(1)/.rustup && \
77 export CARGO_HOME=$(1)/.cargo && \
Sumit Gargf0a2eef2024-02-07 14:44:40 +053078 curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y --no-modify-path; \
Sumit Garg857eaa02024-01-25 11:43:29 +053079 fi
80endef
81
82.PHONY: rust-toolchain
83rust-toolchain:
84 $(call dl-rust-toolchain,$(RUST_TOOLCHAIN_PATH))
85
Jerome Forissierc09d34c2021-04-15 17:09:44 +020086CLANG_VER ?= 12.0.0
Jerome Forissier0ae3f0a2021-04-15 17:09:44 +020087CLANG_PATH ?= $(ROOT)/clang-$(CLANG_VER)
Jerome Forissierc3cd9f52020-05-25 18:47:00 +020088
89# Download the Clang compiler with LLVM tools and compiler-rt libraries
90define dl-clang
Jerome Forissier0ae3f0a2021-04-15 17:09:44 +020091 @if [ ! -d "$(2)" ]; then \
92 ./get_clang.sh $(1) $(2); \
Jerome Forissierc3cd9f52020-05-25 18:47:00 +020093 else \
Jerome Forissier0ae3f0a2021-04-15 17:09:44 +020094 echo "$(2) already exists"; \
Jerome Forissierc3cd9f52020-05-25 18:47:00 +020095 fi
96endef
97
98.PHONY: clang-toolchains
99clang-toolchains:
Jerome Forissier0ae3f0a2021-04-15 17:09:44 +0200100 $(call dl-clang,$(CLANG_VER),$(CLANG_PATH))
Jens Wiklander68f7f8d2021-06-07 16:02:24 +0000101
Alvin Chang344f7712023-07-25 23:28:32 +0800102else ifeq ($(ARCH),riscv)
Alvin Chang6902e462023-07-19 14:37:25 +0800103RISCV64_PATH ?= $(TOOLCHAIN_ROOT)/riscv64
104RISCV64_CROSS_COMPILE ?= $(RISCV64_PATH)/bin/riscv64-unknown-linux-gnu-
105RISCV64_GCC_RELEASE_DATE ?= 2023.07.07
106RISCV64_GCC_VERSION ?= riscv64-glibc-ubuntu-22.04-gcc-nightly-$(RISCV64_GCC_RELEASE_DATE)-nightly
107SRC_RISCV64_GCC ?= https://github.com/riscv-collab/riscv-gnu-toolchain/releases/download/$(RISCV64_GCC_RELEASE_DATE)/$(RISCV64_GCC_VERSION).tar.gz
108
109.PHONY: toolchains
Sumit Garg50458912024-01-25 11:37:50 +0530110toolchains: riscv64-toolchain
Alvin Chang6902e462023-07-19 14:37:25 +0800111
Sumit Garg50458912024-01-25 11:37:50 +0530112.PHONY: riscv64-toolchain
113riscv64-toolchain:
Alvin Chang6902e462023-07-19 14:37:25 +0800114 $(call dltc,$(RISCV64_PATH),$(SRC_RISCV64_GCC),$(RISCV64_GCC_VERSION))
115
116endif
117
Sumit Garg495dc6a2021-12-23 14:45:36 +0530118else ifeq ($(UNAME_M),aarch64)
119
120AARCH32_PATH ?= $(TOOLCHAIN_ROOT)/aarch32
121AARCH32_CROSS_COMPILE ?= $(AARCH32_PATH)/bin/arm-linux-gnueabihf-
122AARCH32_GCC_VERSION ?= gcc-arm-10.2-2020.11-aarch64-arm-none-linux-gnueabihf
123SRC_AARCH32_GCC ?= https://developer.arm.com/-/media/Files/downloads/gnu-a/10.2-2020.11/binrel/$(AARCH32_GCC_VERSION).tar.xz
124
125# There isn't any native aarch64 toolchain released from Arm and buildroot
126# doesn't support distribution toolchain [1]. So we are left with no choice
127# but to build buildroot toolchain from source and use it.
128#
129# [1] https://buildroot.org/downloads/manual/manual.html#_cross_compilation_toolchain
130AARCH64_PATH ?= $(TOOLCHAIN_ROOT)/aarch64
131AARCH64_CROSS_COMPILE ?= $(AARCH64_PATH)/bin/aarch64-linux-
132
133.PHONY: toolchains
Sumit Garg50458912024-01-25 11:37:50 +0530134toolchains: aarch32-toolchain $(AARCH64_PATH)/.done
Sumit Garg495dc6a2021-12-23 14:45:36 +0530135
Sumit Garg50458912024-01-25 11:37:50 +0530136.PHONY: aarch32-toolchain
137aarch32-toolchain:
Sumit Garg495dc6a2021-12-23 14:45:36 +0530138 $(call dltc,$(AARCH32_PATH),$(SRC_AARCH32_GCC),$(AARCH32_GCC_VERSION))
139
140$(AARCH64_PATH)/.done:
141 $(call build_toolchain,aarch64,$(AARCH64_PATH),aarch64,gnu)
142
143else # $(UNAME_M) != x86_64 or $(UNAME_M) != aarch64
Jens Wiklander68f7f8d2021-06-07 16:02:24 +0000144AARCH32_PATH := $(TOOLCHAIN_ROOT)/aarch32
145AARCH32_CROSS_COMPILE := $(AARCH32_PATH)/bin/arm-linux-
146AARCH64_PATH := $(TOOLCHAIN_ROOT)/aarch64
147AARCH64_CROSS_COMPILE := $(AARCH64_PATH)/bin/aarch64-linux-
148
149.PHONY: toolchains
150toolchains: $(AARCH64_PATH)/.done $(AARCH32_PATH)/.done
151
Jens Wiklander68f7f8d2021-06-07 16:02:24 +0000152$(AARCH64_PATH)/.done:
153 $(call build_toolchain,aarch64,$(AARCH64_PATH),aarch64,gnu)
154
155$(AARCH32_PATH)/.done:
156 $(call build_toolchain,aarch32,$(AARCH32_PATH),arm,gnueabihf)
157endif