Skip to content

Commit 77a4373

Browse files
committed
Add tool to automate updating to latest SDK from NXP.
This script grabs the latest release (or tag passed as argument) from NXP's github and updates the contents of existing target chip drivers from the download. It also updates the common drivers from the downloaded SDK and symlinks them as appropriate into the target chip drivers dirs.
1 parent 4ba9f0d commit 77a4373

File tree

2 files changed

+132
-0
lines changed

2 files changed

+132
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
release_details.json
2+
mcux_sdk_*
3+
backup

update_sdk.py

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
#!/usr/bin/env python3
2+
3+
# This repository contains a trimmed down version of NXP's MCUXpresso SDK
4+
# for use by imx-rt based projects compiling firmware outside ot the
5+
# MCUXpresso environment.
6+
7+
# This tool grabs the latest (or specified) release from NXP and updates the
8+
# drivers here for currently-included target chips along with common drivers.
9+
10+
# To add support for a new target chip simply create an empty folder with the
11+
# desired target name in devices folder and re-run this tool, it will be updated
12+
# along with the other existing targets.
13+
14+
# Release / tag name can be passed on command line, otherwise the latest release
15+
# will be used from: https://github.com/nxp-mcuxpresso/mcux-sdk/releases
16+
17+
import argparse
18+
import itertools
19+
import json
20+
import os
21+
import re
22+
import shutil
23+
import subprocess
24+
import tarfile
25+
from pathlib import Path
26+
from urllib.request import urlretrieve
27+
28+
def update_mcux_sdk(release=None, symlink=False):
29+
"""
30+
Downloads and updates the MCUXpresso SDK for nxp_driver folder.
31+
32+
Args:
33+
release (str, optional): Release tag name. Defaults to "latest".
34+
symlink (bool): If enabled, common drivers will by linked into target dirs to emulate previous repo structure
35+
"""
36+
37+
script_dir = Path(__file__).parent.resolve()
38+
os.chdir(script_dir)
39+
40+
# Get release details
41+
print(f"Downloading official mcux-sdk from:")
42+
if not release or release == "latest":
43+
url = f"https://api.github.com/repos/nxp-mcuxpresso/mcux-sdk/releases/latest"
44+
response, _ = urlretrieve(url)
45+
with open(response, 'r') as f:
46+
release_details = json.load(f)
47+
48+
download_url = release_details.get('tarball_url')
49+
release_tag = release_details.get('tag_name')
50+
else:
51+
release_tag = release
52+
download_url = f"https://github.com/nxp-mcuxpresso/mcux-sdk/archive/refs/tags/{release_tag}.tar.gz"
53+
print(f" {download_url}")
54+
55+
# Create and extract SDK directory
56+
sdk_new_dir = Path(f"mcux_sdk_{release_tag}")
57+
if not sdk_new_dir.exists():
58+
sdk_new_dir.mkdir(exist_ok=True)
59+
print("Downloading and extracting SDK...")
60+
urlretrieve(download_url, filename='sdk.tar.gz')
61+
with tarfile.open('sdk.tar.gz', 'r:gz') as tar:
62+
tar.extractall(path=str(sdk_new_dir))
63+
os.remove('sdk.tar.gz')
64+
65+
# Find drivers folder in downloaded directory
66+
sdk_new = sdk_new_dir.name
67+
if not (sdk_new_dir / 'drivers').exists():
68+
sdk_new = next(sdk_new_dir.iterdir())
69+
if not (sdk_new / 'drivers').exists():
70+
print(f"ERROR: Unknown directory structure / cannot find drivers in {sdk_new}")
71+
exit(1)
72+
73+
print(f"\nUpdating sdk to {release_tag}")
74+
(script_dir / 'sdk' / 'version.txt').write_text(release_tag)
75+
76+
backup = script_dir / "backup"
77+
if backup.exists():
78+
shutil.rmtree(backup)
79+
backup.mkdir()
80+
81+
# Update common drivers
82+
print(f"Updating common sdk/drivers from {sdk_new / 'drivers'}")
83+
common_drivers = script_dir / 'sdk' / 'drivers'
84+
if common_drivers.exists():
85+
common_drivers.rename(backup / 'drivers')
86+
shutil.copytree(sdk_new / 'drivers', common_drivers)
87+
88+
# Update drivers for existing target chips
89+
for target_dir in (script_dir / 'sdk' / 'devices').iterdir():
90+
target = target_dir.name
91+
source = sdk_new / 'devices' / target
92+
if not source.exists():
93+
print(f"WARNING: No new drivers available for {target} in {source}")
94+
continue
95+
96+
print(f"Updating {target} from {source}")
97+
target_dir.rename(backup / target)
98+
shutil.copytree(source, target_dir)
99+
100+
if not symlink:
101+
continue
102+
103+
# symlink common drivers used by each target into the target folders
104+
common_driver_dirs = set()
105+
for cmake_script in itertools.chain(target_dir.glob("all_lib_device_*.cmake"), target_dir.glob("set_device_*.cmake")):
106+
common_driver_dirs |= set(re.findall(r"\$\{CMAKE_CURRENT_LIST_DIR\}/../../drivers/(\S+?)[/\n]", cmake_script.read_text()))
107+
for cdd in common_driver_dirs:
108+
for driver in (common_drivers / cdd).glob("*.[hc]"):
109+
target = target_dir / "drivers" / driver.name
110+
if not target.exists():
111+
target.symlink_to(f"../../../drivers/{driver.relative_to(common_drivers)}")
112+
113+
# Git operations
114+
subprocess.run(['git', 'checkout', '-b', f'sdk_update_{release_tag}'], check=True)
115+
subprocess.run(['git', 'add', 'sdk'], check=True)
116+
message = f"Update sdk to {release_tag}\n\nFrom: {download_url}"
117+
subprocess.run(['git', 'commit', '--quiet', '-F-'], input=message.encode(), check=True)
118+
119+
print("Finished")
120+
121+
if __name__ == "__main__":
122+
parser = argparse.ArgumentParser(
123+
description="This tool creates a trimmed down version of NXP's MCUXpresso SDK for use by imx-rt based projects compiling firmware outside of the MCUXpresso environment.",
124+
formatter_class=argparse.ArgumentDefaultsHelpFormatter
125+
)
126+
parser.add_argument("-r", "--release", default="latest", help="Release tag name to update to. Defaults to 'latest'.")
127+
args = parser.parse_args()
128+
129+
update_mcux_sdk(args.release)

0 commit comments

Comments
 (0)