blob: 5454c688199698a841e5570d20b7c3278c54dfad [file] [log] [blame]
Mate Toth-Pal76867262018-03-09 13:15:36 +01001#-------------------------------------------------------------------------------
2# Copyright (c) 2017-2018, Arm Limited. All rights reserved.
3#
4# SPDX-License-Identifier: BSD-3-Clause
5#
6#-------------------------------------------------------------------------------
7
8#Find the location of the GNUARM C/C++ compiler.
9#
10# Find gnuarm on the specified location or on the PATH and optionally validate
11# its version.
12#
13#Inputs:
14# GNUARM_PATH - (optional)- install path of GNUARM compiler to use. If not set
15# the compiler on the PATH is used.
16# GNUARM_VER - (optional)- version number. If set the module will validate
17# the compiler version.
18#
19#outputs:
20# GNUARM_PATH - will be set to the root directory of the compiler. Only set
21# if undefined.
22# GNUARM_VER - will be set to the version number found. Only set if
23# undefined.
24# GNUARM_MODULE - set to the name of the cmake module to be included for this
25# GNUARM version.
26#
27
28#Include some dependencies
29Include(Common/Utils)
30
31set(_GCC_BINARY_NAME "arm-none-eabi-gcc")
32
33#Get the version of armgcc.
34#
35# Execute gcc and extract the version number for its output.
36#
37#Examples:
38# Get the version reported by gcc at location c:/foo/bin/ to
39# variable VER get_armgcc_version(GCC "c:/foo/bin/arm-none-eabi-gcc" RES VER)
40#
41#INPUTS:
42# GCC - (mandatory) - gcc executable
43# RES - (mandatory) - variable name to put result to
44#
45#OUTPUTS
46# The variable named after "RES" will be set to the version number
47#
48function(get_armgcc_version)
49 ###Parse our arguments
50 #No option (on/off) arguments (e.g. IGNORE_CASE)
51 set( _OPTIONS_ARGS )
52 #Single option arguments (e.g. PATH "./foo/bar")
53 set( _ONE_VALUE_ARGS GCC RES)
54 #List arguments (e.g. LANGUAGES C ASM CXX)
55 set( _MULTI_VALUE_ARGS )
56 cmake_parse_arguments(_MY_PARAMS "${_OPTIONS_ARGS}"
57 "${_ONE_VALUE_ARGS}"
58 "${_MULTI_VALUE_ARGS}"
59 ${ARGN} )
60
61 #Check mandatory parameters
62 if(NOT _MY_PARAMS_RES)
63 message (FATAL_ERROR "get_armgcc_version(): Missing result parameter!")
64 endif()
65 set (_RES ${_MY_PARAMS_RES})
66
67 if(NOT _MY_PARAMS_GCC)
68 message (FATAL_ERROR "get_armgcc_version(): Missing GCC parameter!")
69 endif()
70 set (_GCC ${_MY_PARAMS_GCC})
71
72 #Call specified executable
73 execute_process(COMMAND "${_GCC}" -v
74 OUTPUT_VARIABLE _OUTPUT
75 ERROR_VARIABLE _OUTPUT
76 )
77 #Cut off version number. Just the numbers ignore anything after.
78 STRING(REGEX REPLACE ".*gcc version ([0-9.]+) .*" "\\1" _VER "${_OUTPUT}")
79
80 if (NOT _VER)
81 string(CONCAT _msg "get_armgcc_version(): Failed to extract version"
82 " number from ${_GCC_BINARY_NAME} output.")
83 message (FATAL_ERROR "${_msg}")
84 endif()
85
86 set(${_RES} ${_VER} PARENT_SCOPE)
87endfunction()
88
89#Check if the executable is present
90if(NOT DEFINED GNUARM_PATH)
91 #If the location is not set, try to find executable on PATH.
92 #Set GNUARM_PATH to default value.
93 set (GNUARM_PATH "GNUARM_PATH-NOTFOUND")
94
95 #First check if gcc is on the PATH
96 #find_program puts() its output to the cmake cache. We don't want that, so
97 # we use a local variable, which is unset later.
98 find_program (
99 _GNUARM_PATH
100 ${_GCC_BINARY_NAME}
101 PATHS env PATH
102 DOC "GNUARM compiler location."
103 )
104else()
105 #Check executable is available on the specified path.
106 #find_program puts() its output to the cmake cache. We don't want that, so
107 # we use a local variable, which is unset later.
108 find_program (
109 _GNUARM_PATH
110 ${_GCC_BINARY_NAME}
Kumar Gala588316c2019-03-27 13:46:45 -0500111 PATHS ${GNUARM_PATH}/bin NO_DEFAULT_PATH
Mate Toth-Pal76867262018-03-09 13:15:36 +0100112 DOC "GNUARM compiler location."
113 )
114endif()
115
116#Is executable present?
117if(_GNUARM_PATH STREQUAL "_GNUARM_PATH-NOTFOUND")
118 string(CONCAT _msg "${_GCC_BINARY_NAME} can not be found. Either put"
119 " ${_GCC_BINARY_NAME} on the PATH or set GNUARM_PATH"
120 " properly.")
121 message (FATAL_ERROR ${_msg})
122endif()
123
124#Cut off executable and directory name to get install location.
125STRING(REGEX REPLACE "(.*)/bin/${_GCC_BINARY_NAME}.*"
126 "\\1" GNUARM_PATH "${_GNUARM_PATH}")
127
128#Remove unwanted junk from CMake cache.
129unset(_GNUARM_PATH CACHE)
130
131get_armgcc_version(GCC "${GNUARM_PATH}/bin/${_GCC_BINARY_NAME}" RES _VER)
132
133#Check the version if needed
134if(NOT DEFINED GNUARM_VER)
135 set(GNUARM_VER ${_VER})
136endif()
137
138if(NOT "${GNUARM_VER}" VERSION_EQUAL "${_VER}")
139 string(CONCAT _msg "FindGNUARM.cmake: ${_GCC_BINARY_NAME} compiler version"
140 " ${_VER} does not match ${GNUARM_VER}.")
141 message (FATAL_ERROR "${_msg}")
142endif()
143
144
145STRING(REGEX REPLACE "([0-9]+)\.([0-9]+)(\.[0-9]+)*.*" "CompilerGNUARM\\1\\2"
146 GNUARM_MODULE "${GNUARM_VER}")
147
148if(NOT EXISTS "${CMAKE_CURRENT_LIST_DIR}/${GNUARM_MODULE}.cmake")
149 string(CONCAT _msg "ERROR: Unsupported GNUARM compiler version found on"
150 " PATH.")
151 message(FATAL_ERROR "${_msg}")
152endif()