Skip to content

Commit 57469a2

Browse files
committed
Add check to verify the version of cloned repos with expected ones if
the tf-m build directory already exists. Fix other review comments. Signed-off-by: Devaraj Ranganna <[email protected]>
1 parent 8a4f687 commit 57469a2

File tree

1 file changed

+64
-39
lines changed

1 file changed

+64
-39
lines changed

tools/psa/build_tfm.py

Lines changed: 64 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -22,31 +22,30 @@
2222
import sys
2323
import subprocess
2424
import logging
25-
import argparse
2625

2726
logger = logging.getLogger('TF-M-Builder')
2827
logging.basicConfig(level=logging.INFO,
2928
format='[%(name)s] %(asctime)s: %(message)s.',
3029
datefmt='%H:%M:%S')
3130

32-
ROOT = abspath(join(dirname(__file__),
33-
os.pardir, os.pardir))
31+
ROOT = abspath(join(dirname(__file__), os.pardir, os.pardir))
3432
sys.path.insert(0, ROOT)
35-
from tools.targets import Target, TARGET_MAP, TARGET_NAMES
3633

3734
TF_M_BUILD_DIR = join(ROOT, os.pardir, 'tfm_build_dir')
38-
VERSION_FILE_PATH = join(ROOT,'components/TARGET_PSA/TARGET_TFM')
39-
40-
TFM_GIT = 'https://git.trustedfirmware.org/trusted-firmware-m.git'
41-
TFM_GIT_BRANCH = 'feature-twincpu'
42-
MBEDTLS_GIT = 'https://github.com/ARMmbed/mbedtls.git'
43-
MBEDTLS_GIT_BRANCH = 'mbedtls-2.7.9'
44-
MBEDCRYPTO_GIT = 'https://github.com/ARMmbed/mbed-crypto.git'
45-
MBEDCRYPTO_GIT_BRANCH = 'mbedcrypto-1.1.0'
46-
CMSIS_GIT = 'https://github.com/ARM-software/CMSIS_5.git'
47-
CMSIS_GIT_BRANCH = '5.5.0'
48-
49-
def run_cmd_return_output(command):
35+
VERSION_FILE_PATH = join(ROOT, 'features/FEATURE_PSA/FEATURE_TFM')
36+
37+
dependencies = {
38+
"trusted-firmware-m":
39+
['https://git.trustedfirmware.org/trusted-firmware-m.git',
40+
'feature-twincpu'],
41+
"mbedtls": ['https://github.com/ARMmbed/mbedtls.git',
42+
'mbedtls-2.7.9'],
43+
"mbed-crypto": ['https://github.com/ARMmbed/mbed-crypto.git',
44+
'mbedcrypto-1.1.0'],
45+
"CMSIS_5": ['https://github.com/ARM-software/CMSIS_5.git', '5.5.0'],
46+
}
47+
48+
def run_cmd_and_return_output(command):
5049
"""
5150
Run the command in the sytem and return output.
5251
Commands are passed as a list of tokens.
@@ -62,57 +61,83 @@ def run_cmd_return_output(command):
6261
output = subprocess.check_output(command, stderr=fnull)
6362
except subprocess.CalledProcessError as e:
6463
logger.error("The command %s failed with return code: %s",
65-
(' '.join(command)), e.returncode)
64+
(' '.join(command)), e.returncode)
6665
sys.exit(1)
6766
return output
6867

69-
def detect_write_tfm_version(tfm_dir, ):
68+
def detect_and_write_tfm_version(tfm_dir):
7069
"""
71-
Identify the version of TF-M and write it to VERSION.TXT
70+
Identify the version of TF-M and write it to VERSION.txt
7271
:param tfm_dir: The filesystem path where TF-M repo is cloned
7372
"""
74-
cmd = ['git', '-C', tfm_dir, 'describe', '--tags', '--abbrev=12', '--dirty', '--always']
75-
tfm_version = run_cmd_return_output(cmd)
76-
logger.debug('TF-M version: %s', tfm_version)
77-
with open(join(VERSION_FILE_PATH, 'VERSION.TXT'), 'w+') as f:
78-
f.writelines(tfm_version.splitlines(True)[0])
73+
cmd = ['git', '-C', tfm_dir, 'describe', '--tags',
74+
'--abbrev=12', '--dirty', '--always']
75+
tfm_version = run_cmd_and_return_output(cmd)
76+
logger.info('TF-M version: %s', tfm_version)
77+
if not isdir(VERSION_FILE_PATH):
78+
os.makedirs(VERSION_FILE_PATH)
79+
with open(join(VERSION_FILE_PATH, 'VERSION.txt'), 'w+') as f:
80+
f.write(tfm_version)
81+
82+
def check_repo_version(name, deps):
83+
"""
84+
Compare the version of cloned and expected and exit if they dont match
85+
:param name: Name of the git repository
86+
:param deps: Dictionary containing dependency details
87+
"""
88+
basedir = TF_M_BUILD_DIR
89+
if name == 'trusted-firmware-m':
90+
cmd = ['git', '-C', join(basedir, name),
91+
'rev-parse', '--abbrev-ref', 'HEAD']
92+
else:
93+
cmd = ['git', '-C', join(basedir, name),
94+
'describe', '--tags']
95+
_out = run_cmd_and_return_output(cmd)
96+
if _out.strip('\n') != deps.get(name)[1]:
97+
logger.error('Conflict: cloned "%s" and expected "%s"',
98+
_out.strip('\n'), deps.get(name)[1])
99+
logger.error('check and remove folder %s',
100+
join(basedir, name))
101+
sys.exit(1)
102+
else:
103+
logger.info('%s: version check OK', name)
79104

80-
def check_clone_repo(basedir, name, git_branch, git):
105+
def check_and_clone_repo(name, deps):
81106
"""
82107
Test if the repositories are already cloned. If not clone them
83-
:param basedir: Base directory into which the repos will be cloned
84108
:param name: Name of the git repository
85-
:param git_branch: Git branch to be cloned
86-
:param git: Web link to the git respository
109+
:param deps: Dictionary containing dependency details
87110
"""
111+
basedir = TF_M_BUILD_DIR
88112
if not isdir(join(basedir, name)):
89113
logger.info('Cloning %s repo', name)
90-
cmd = ['git', '-C', basedir, 'clone', '-b', git_branch, git]
91-
_out = run_cmd_return_output(cmd)
114+
cmd = ['git', '-C', basedir, 'clone', '-b',
115+
deps.get(name)[1], deps.get(name)[0]]
116+
_out = run_cmd_and_return_output(cmd)
92117
logger.info('Cloned %s repo successfully', name)
93118
else:
94-
logger.info('%s repo exists', name)
119+
logger.info('%s repo exists, checking git version...', name)
120+
check_repo_version(name, deps)
95121

96122
def clone_tfm_repo():
97123
"""
98124
Clone TF-M git repos and it's dependencies
99125
"""
100-
check_clone_repo(TF_M_BUILD_DIR, 'trusted-firmware-m', TFM_GIT_BRANCH, TFM_GIT)
101-
check_clone_repo(TF_M_BUILD_DIR, 'mbedtls', MBEDTLS_GIT_BRANCH, MBEDTLS_GIT)
102-
check_clone_repo(TF_M_BUILD_DIR, 'mbed-crypto', MBEDCRYPTO_GIT_BRANCH, MBEDCRYPTO_GIT)
103-
check_clone_repo(TF_M_BUILD_DIR, 'CMSIS_5', CMSIS_GIT_BRANCH, CMSIS_GIT)
104-
105-
detect_write_tfm_version(join(TF_M_BUILD_DIR, 'trusted-firmware-m'), )
126+
check_and_clone_repo('trusted-firmware-m', dependencies)
127+
check_and_clone_repo('mbedtls', dependencies)
128+
check_and_clone_repo('mbed-crypto', dependencies)
129+
check_and_clone_repo('CMSIS_5', dependencies)
130+
detect_and_write_tfm_version(join(TF_M_BUILD_DIR, 'trusted-firmware-m'), )
106131

107132
def main():
108133
"""
109134
Build Trusted Firmware M (TF-M) image for mbed-os supported TF-M targets.
110135
Current version of the script only clones TF-M git repo and dependencies
111-
and creates a VERSION.TXT file under 'components/TARGET_PSA/TARGET_TFM'
136+
and creates a VERSION.txt file under 'features/FEATURE_PSA/FEATURE_TFM'
112137
"""
113138
if not isdir(TF_M_BUILD_DIR):
114139
os.mkdir(TF_M_BUILD_DIR)
115140
clone_tfm_repo()
116141

117142
if __name__ == '__main__':
118-
main()
143+
main()

0 commit comments

Comments
 (0)