1*7794d6c8SGovindraj Raja# 2*7794d6c8SGovindraj Raja# Copyright (c) 2023, Arm Limited. All rights reserved. 3*7794d6c8SGovindraj Raja# 4*7794d6c8SGovindraj Raja# SPDX-License-Identifier: BSD-3-Clause 5*7794d6c8SGovindraj Raja# 6*7794d6c8SGovindraj Raja 7*7794d6c8SGovindraj Raja# This build helper makefile is used to determine a suitable march build 8*7794d6c8SGovindraj Raja# option to be used, based on platform provided ARM_ARCH_MAJOR/MINOR 9*7794d6c8SGovindraj Raja# data and compiler supported march version. 10*7794d6c8SGovindraj Raja 11*7794d6c8SGovindraj Raja# Set the compiler's target architecture profile based on 12*7794d6c8SGovindraj Raja# ARM_ARCH_MAJOR and ARM_ARCH_MINOR options. 13*7794d6c8SGovindraj Raja 14*7794d6c8SGovindraj Raja 15*7794d6c8SGovindraj Raja# This is used to collect available march values from compiler. 16*7794d6c8SGovindraj Raja# Example on a gcc-12.2 arm64 toolchain it will look like: 17*7794d6c8SGovindraj Raja# [...] 18*7794d6c8SGovindraj Raja#./aarch64-none-elf-gcc --target-help -march=foo 19*7794d6c8SGovindraj Raja# cc1: error: unknown value 'foo' for '-march' 20*7794d6c8SGovindraj Raja# cc1: note: valid arguments are: armv8-a armv8.1-a armv8.2-a armv8.3-a armv8.4-a armv8.5-a 21*7794d6c8SGovindraj Raja# armv8.6-a armv8.7-a armv8.8-a armv8-r armv9-a 22*7794d6c8SGovindraj Raja# [...] 23*7794d6c8SGovindraj Raja# 24*7794d6c8SGovindraj RajaGCC_MARCH_OUTPUT := $(shell $(CC) -march=foo -Q --help=target -v 2>&1) 25*7794d6c8SGovindraj Raja 26*7794d6c8SGovindraj Raja# This function is used to find the best march value supported by the given compiler. 27*7794d6c8SGovindraj Raja# We try to use `GCC_MARCH_OUTPUT` which has verbose message with supported march values we filter that 28*7794d6c8SGovindraj Raja# to find armvX.Y-a or armvX-a values, then filter the best supported arch based on ARM_ARCH_MAJOR. 29*7794d6c8SGovindraj Raja# 30*7794d6c8SGovindraj Raja# Example on a gcc-12.2 arm64 toolchain this will return armv9-a if platform requested for armv9.2-a 31*7794d6c8SGovindraj Raja# Similarly on gcc 11.3 it would return armv8.6-a if platform requested armv8.8-a 32*7794d6c8SGovindraj Rajadefine major_best_avail_march 33*7794d6c8SGovindraj Raja$(1) := $(lastword $(filter armv$(ARM_ARCH_MAJOR)% ,$(filter armv%-a, $(GCC_MARCH_OUTPUT)))) 34*7794d6c8SGovindraj Rajaendef 35*7794d6c8SGovindraj Raja 36*7794d6c8SGovindraj Raja# This function is used to just return latest march value supported by the given compiler. 37*7794d6c8SGovindraj Raja# 38*7794d6c8SGovindraj Raja# Example: this would return armv8.6-a on a gcc-11.3 when platform requested for armv9.0-a 39*7794d6c8SGovindraj Raja# 40*7794d6c8SGovindraj Raja# Thus this function should be used in conjunction with major_best_avail_march, when best match 41*7794d6c8SGovindraj Raja# is not found it should be ok to try with lastest known supported march value from the 42*7794d6c8SGovindraj Raja# compiler. 43*7794d6c8SGovindraj Rajadefine latest_match_march 44*7794d6c8SGovindraj Raja$(1) := $(lastword $(filter armv%-a, $(GCC_MARCH_OUTPUT))) 45*7794d6c8SGovindraj Rajaendef 46*7794d6c8SGovindraj Raja 47*7794d6c8SGovindraj Rajaifdef MARCH_DIRECTIVE 48*7794d6c8SGovindraj Raja march-directive := $(MARCH_DIRECTIVE) 49*7794d6c8SGovindraj Rajaelse 50*7794d6c8SGovindraj Raja 51*7794d6c8SGovindraj Rajaifeq (${ARM_ARCH_MINOR},0) 52*7794d6c8SGovindraj Raja provided-march = armv${ARM_ARCH_MAJOR}-a 53*7794d6c8SGovindraj Rajaelse 54*7794d6c8SGovindraj Raja provided-march = armv${ARM_ARCH_MAJOR}.${ARM_ARCH_MINOR}-a 55*7794d6c8SGovindraj Rajaendif 56*7794d6c8SGovindraj Raja 57*7794d6c8SGovindraj Rajaifeq ($(findstring clang,$(notdir $(CC))),) 58*7794d6c8SGovindraj Raja 59*7794d6c8SGovindraj Raja# We expect from Platform to provide a correct Major/Minor value but expecting something 60*7794d6c8SGovindraj Raja# from compiler with unsupported march means we shouldn't fail without trying anything, 61*7794d6c8SGovindraj Raja# so here we try to find best supported march value and use that for compilation. 62*7794d6c8SGovindraj Raja# If we don't support current march version from gcc compiler, try with lower arch based on 63*7794d6c8SGovindraj Raja# availability. In TF-A there is no hard rule for need of higher version march for basic 64*7794d6c8SGovindraj Raja# functionality, denying a build on only this fact doesn't look correct, so try with best 65*7794d6c8SGovindraj Raja# or latest march values from whats available from compiler. 66*7794d6c8SGovindraj Rajaifeq (,$(findstring $(provided-march), $(GCC_MARCH_OUTPUT))) 67*7794d6c8SGovindraj Raja $(eval $(call major_best_avail_march, available-march)) 68*7794d6c8SGovindraj Raja 69*7794d6c8SGovindraj Rajaifeq (, $(available-march)) 70*7794d6c8SGovindraj Raja $(eval $(call latest_match_march, available-march)) 71*7794d6c8SGovindraj Rajaendif 72*7794d6c8SGovindraj Raja 73*7794d6c8SGovindraj Raja# If we fail to come up with any available-march value so far, don't update 74*7794d6c8SGovindraj Raja# provided-march and thus allow the build to fail using the provided-march 75*7794d6c8SGovindraj Raja# which is derived based on arch_major and arch_minor values. 76*7794d6c8SGovindraj Rajaifneq (,$(available-march)) 77*7794d6c8SGovindraj Raja provided-march := ${available-march} 78*7794d6c8SGovindraj Rajaendif 79*7794d6c8SGovindraj Raja 80*7794d6c8SGovindraj Rajaendif # provided-march supported 81*7794d6c8SGovindraj Rajaendif # not clang 82*7794d6c8SGovindraj Raja 83*7794d6c8SGovindraj Rajamarch-directive := -march=${provided-march} 84*7794d6c8SGovindraj Raja 85*7794d6c8SGovindraj Rajaendif # MARCH_DIRECTIVE 86