blob: 00b91c9e4dcee5e62c5611b9a29578a0125d26aa [file] [log] [blame]
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +01001#!/bin/bash
2#-------------------------------------------------------------------------------
Xinyu Zhang15362412023-02-10 15:57:07 +08003# Copyright (c) 2018-2023, Arm Limited and Contributors. All rights reserved.
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +01004#
5# SPDX-License-Identifier: BSD-3-Clause
6#
7#-------------------------------------------------------------------------------
8
9##
10##@file
11##@brief Common utility functions used by CMake related build and utility scripts
12##
13##This file can be "sourced" from other scripts to get access to variables and functions
14##defined here.
15##Example \code{.sh}. <path-to-tfm-ci-repo>/util_cmake.sh\endcode
16##or \code{.sh}source <path-to-tfm-ci-repo>/util_cmake.sh\endcode
17##
18
19##@fn fix_win_path(string path)
20##@brief Convert cygwin and msys path to windows like.
21##@param[in] path
22##@returns path in windows format
23##
24##This function converts MSYS and cygwin paths to windows like path. Can be used
25##to print paths in error message which can be used withouth conversion. This
26##way for example you can get "clickable" path in Eclipse error window.
27##
28##Usage:
29## Assuming current directory is <i>c:/somedir1/somedir2</i>
30## command | result
31## --------|-------
32## fix_win_path "/cygdrive/c/foo/bar"| c:/foo/bar
33## fix_win_path "/c/foo/bar"| c:/foo/bar
34## fix_win_path "../somedir1/foo/bar"| ../somedir1/foo/bar
35## fix_win_path `get_full_path "../somedir1/foo/bar"` | c:/somedir1/foo/bar
36##
37#This iis needed for doxygen for now.
38#!void fix_win_path(string path){};
39#
40function fix_win_path() {
41 local path="$@"
42 #See if we run on windows
43 if [ -e "c:/" ]
44 then
45 #sed:
46 # 1. match /cygdrive/c/ like paths and convert to the c:/ format
47 # 2. if 1 did not match conver /c/ path to c:/ format
48 path=`builtin echo "$path"|sed "s/\/cygdrive\/\([a-zA-Z]\)\//\1:\//;tx;s/\/\([a-zA-Z]\)\//\1:\//;:x"`
49 fi
50 builtin echo "$path"
51}
52
53##@fn get_full_path(string path)
54##@brief Convert the passed path to full path.
55##@param[in] path
56##@returns path converted to absolute full path.
57##
58##This function converts a path to absolute full path. The function will return
59##execution environment specific path (/cygdrive/ under Cygwin c:/ under MSys
60##and /foo/bar under Linux).
61##The patch to conver may or may not contain a file name.
62##
63##Usage:
64## Assuming current directory is <i>c:/somedir1/somedir2</i>
65## environment | command | result
66## --------|--------|-------
67## Cygwin|get_full_path "."| /cygdrive/c/somedir1/somedir2
68## MSys|get_full_path "."| c:/somedir1/somedir2
69## Linux|get_full_path "."| /somedir1/somedir2
70##
71#This iis needed for doxygen for now.
72#!void get_full_path(string path){};
73#
74function get_full_path {
75 local file=""
76 local dir=$1
77 #If the paramether is a file, split it to directory and file name component.
78 if [ -f "$dir" ]
79 then
80 dir=`dirname "$1"`
81 file=`basename "$1"`
82 fi
83
84 if [ -z "$dir" ]
85 then
86 dir="."
87 fi
88
89 #Enter the directory to get it's full path
90 pushd "$dir" >/dev/null
91 local path=$PWD
92 popd >/dev/null
93
94 #On windows further fixing is needed to get a windows path
95 case "$os_name" in
96 CYGWIN)
97 path=`cygpath -m $path`
98 ;;
99 MSYS)
100 path=`echo $path| sed "s/^\/\([a-zA-Z]\)\//\1:\//"`
101 ;;
102 esac
103
104 echo "$path/$file"
105}
106
107
108##@fn make_build_dir_name(path build_base_dir, string build_config_name)
109##@brief Create the location for the a build.
110##@param[in] build_base_dir
111##@param[in] build_config_name
112##@returns The generated path.
113##
114##This function will generate the name for a build directory. The generated name
115##follow the pattern "<build_base_dir>/build-<build_config_name>".
116##The generted path will be absolute.
117##
118##Usage:
119## Assuming CMakeList.txt file is in /foo/bar directory.
120## command | result
121## --------|-------
122## make_build_dir_name "/foo/bar" "test_build_st32" | Return /foo/bar/build-test_build_st32
123##
124#This iis needed for doxygen for now.
125#!void make_build_dir_name(path build_base_dir, string build_config_name){};
126#
127function make_build_dir_name() {
128 local build_base_dir=$(get_full_path $1)
129 local build_config_name=$2
130 echo "${build_base_dir}build-$build_config_name"
131}
132
133##@fn generate_project(string src_dir, string build_base_dir, string build_config_name, string cmake_params)
134##@brief Execute CMake generation phase for a project
135##@param[in] src_dir
136##@param[in] build_base_dir
137##@param[in] build_config_name
138##@param[in] cmake_params
139##@returns N/A
140##
141##This function will create a build directory named "build-<build_config_name>"
142##under the passed <build_base_dir> directory, and execute CMake inside to
143##generate "Unix Makefiles".
144##CMake output is saved to <build_base_dir>/build-<build_config_name>/build.log
145##
146##Usage:
147## Assuming CMakeList.txt file is in /foo/bar directory.
148## command | result
149## --------|-------
150## generate_project "/foo/bar" "/tmp/build" "test_build_st32" "-DCMAKE_BUILD_TYPE=Debug"| Generate makefiles under /tmp/buid/build-test_build_st32 for project /foo/bar/CMakeLists.txt
151##
152#This iis needed for doxygen for now.
153#!void generate_project(string dir, string build_base_dir, string build_config_name, string cmake_params){};
154#
155function generate_project {
156 local src_dir=$1
157 local build_base_dir=$2
158 local bcfg_name=$3
159 local cm_params=$4
160 local bdir=$(make_build_dir_name "$build_base_dir" "$bcfg_name")
161 local error=0
162
163 #If build ditrectory exists, clear it
164 if [ -e "$bdir" ]
165 then
166 rm -rf $bdir/*
167 else
168 #Create build directory
169 mkdir $bdir
170 fi
171 #Enter build directory
172 if pushd $bdir >/dev/null
173 then
174 #Start cmake to generate makefiles and start the build
175 cmake -G"Unix Makefiles" CMAKE_MAKE_PROGRAM=$CMAKE_MAKE_PROGRAM $cm_params "$src_dir" 2>&1 | tee -a build.log
176 error=$(( ${PIPESTATUS[0]} + ${PIPESTATUS[1]} ))
177 #Back to original location
178 popd >/dev/null
179 else
180 error=1
181 fi
182 return $error
183}
184
185##@fn build_project(string src_dir, string build_base_dir, string build_config_name, string cmake_params)
186##@brief Build a CMake project with gnumake.
187##@param[in] src_dir
188##@param[in] build_base_dir
189##@param[in] build_config_name
190##@param[in] cmake_params
191##@returns N/A
192##
193##This function will call \ref generate_project to generate makefiles with CMake
194##and will execute make to build the project.
195##Make output is saved to <dir>/build-<build_config_name>/build.log
196##
197##Usage:
198## Assuming CMakeList.txt file is in /foo/bar directory.
199## command | result
200## --------|-------
201## build_project "/foo/bar" "test_build_st32" "-DCMAKE_BUILD_TYPE=Debug"| Generate makefiles under /foo/bar/build-test_build_st32 for project /foo/bar/CMakeLists.txt
202##
203#This iis needed for doxygen for now.
204#!void build_project(string src_dir, string build_base_dir, string build_config_name, string cmake_params){};
205#
206function build_project {
207 local src_dir=$1
208 local build_base_dir=$2
209 local bcfg_name=$3
210 local cm_params=$4
211 local error=0
212
213 if generate_project "$src_dir" "$build_base_dir" "$bcfg_name" "$cm_params"
214 then
215 local bdir=$(make_build_dir_name "$build_base_dir" "$bcfg_name")
216 if pushd "$bdir" >/dev/null
217 then
218 cmake --build . -- -j VERBOSE=1 2>&1 | tee -a build.log
219 error=$(( ${PIPESTATUS[0]} + ${PIPESTATUS[1]} ))
220 fi
221 #Back to original location
222 popd >/dev/null
223 else
224 error=1
225 fi
226 return $error
227}
228
229##@fn proj_dir_to_name(path proj_dir)
230##@brief Convert a project directory to project name
231##@param[in] proj_dir
232##@returns The converted name.
233##
234##This function will convert a project path to project name. Conversion rules:
235## * the leading "./" is removed
236## * all '/' (directory separator's) are replaced by '-'
237## * if the result is empty, the name "top_level" is used.
238##
239##project_list.
240##
241##Usage:
242## Assuming CMakeList.txt file is in /foo/bar directory.
243## command | result
244## --------|-------
245## project_list=(./ app secure_fw test ); proj_dir_to_name "test_build_st32" "-DCMAKE_BUILD_TYPE=Debug" project_list | Build all projects listed in project_list array.
246##
247#This iis needed for doxygen for now.
248#!void proj_dir_to_name(path proj_dir){};
249#
250function proj_dir_to_name {
251 local proj=$1
252 local name=$(echo "$proj" | sed 's/^\.\///;s/\//-/g')
253 if [ -z "$name" ]
254 then
255 name="top_level"
256 fi
257 echo "$name"
258}
259
260##@fn build_proj_set(path build_base_dir, string build_config_name, string cmake_params, path project_list[])
261##@brief Build a CMake project with gnumake.
262##@param[in] build_base_dir
263##@param[in] build_config_name
264##@param[in] cmake_params
265##@param[in] project_list
266##@returns N/A
267##
268##This function will call \ref build_project for all CMake projects listed in
269##project_list.
270##
271##Usage:
272## Assuming CMakeList.txt file is in /foo/bar directory.
273## command | result
274## --------|-------
275## project_list=(./ app secure_fw test ); build_proj_set "test_build_st32" "-DCMAKE_BUILD_TYPE=Debug" project_list | Build all projects listed in project_list array.
276##
277#This iis needed for doxygen for now.
278#!void build_proj_set(path build_base_dir, string build_config_name, string cmake_params, path project_list[]){};
279#
280function build_proj_set {
281 local build_base_dir=$1
282 local bcfg_name=$2
283 local cm_params=$3
284 local -n ref_project_list
285 ref_project_list=$4
286 local error=0
287 #For all projects in the list
288 for proj in "${ref_project_list[@]}"
289 do
290 #Convert the project location to a name.
291 local bcfg_name_ext="${bcfg_name}"_$(proj_dir_to_name "$proj")
292 #Convert project location to absolute path.
293 proj=$(get_full_path "$proj")
294 echo "build_project $proj $build_base_dir $bcfg_name_ext $cm_params"
295 #Build the project
296 build_project "$proj" "$build_base_dir" "$bcfg_name_ext" "$cm_params" || error=1
297 done
298 return $error
299}
Xinyu Zhang15362412023-02-10 15:57:07 +0800300
301# Get CMake variable value from cache
302function get_cmake_cache {
303 local build_dir=$1
304 local cmake_var=$2
305
306 if [ ! -d ${REPO_PATH} ]; then
307 return
308 fi
309
310 cd $build_dir
311 cache_value="$(cmake -L 2> /dev/null | grep $cmake_var | awk -F '=' '{print $2}')"
312 echo $cache_value
313 cd $OLDPWD
314}