|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# |
| 3 | +# Copyright (c) 2024 Project CHIP Authors |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | +# |
| 17 | + |
| 18 | +import argparse |
| 19 | +import os |
| 20 | +import subprocess |
| 21 | +import sys |
| 22 | + |
| 23 | +CHIP_ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), '../../..')) |
| 24 | + |
| 25 | +# Array listing all SDK and default targeted path for NXP SDK storage |
| 26 | +ALL_PLATFORM_SDK = [ |
| 27 | + {'plat_name': 'k32w0', 'plat_sdk_manifest_path': 'third_party/nxp/nxp_matter_support/github_sdk/k32w0'}, |
| 28 | +] |
| 29 | + |
| 30 | +all_platform_sdk_list = list(map(lambda plat: plat["plat_name"], ALL_PLATFORM_SDK)) |
| 31 | + |
| 32 | + |
| 33 | +def clean_sdk(sdk_target_location): |
| 34 | + print("SDK will be cleaned all local modification(s) will be lost") |
| 35 | + # Cleaning all local modifications |
| 36 | + git_clean_command = "git reset --hard && git clean -xdf" |
| 37 | + command = ['west', 'forall', '-c', git_clean_command, '-a'] |
| 38 | + subprocess.run(command, cwd=sdk_target_location, check=True) |
| 39 | + # Retrying an update |
| 40 | + update_platform_sdk_version(sdk_target_location, False) |
| 41 | + |
| 42 | + |
| 43 | +def init_platform_sdk_version(sdk_target_location, force): |
| 44 | + print("Init SDK in: " + sdk_target_location) |
| 45 | + if not force and os.path.exists(os.path.join(sdk_target_location, '.west')): |
| 46 | + print("Error SDK is already initialized, use --force to force init") |
| 47 | + print("WARNING -- All local SDK modification(s) will be lost") |
| 48 | + sys.exit(1) |
| 49 | + command = ['rm', '-rf', '.west'] |
| 50 | + subprocess.run(command, cwd=sdk_target_location, check=True) |
| 51 | + |
| 52 | + command = ['west', 'init', '-l', 'manifest', '--mf', 'west.yml'] |
| 53 | + subprocess.run(command, cwd=sdk_target_location, check=True) |
| 54 | + update_platform_sdk_version(sdk_target_location, force) |
| 55 | + |
| 56 | + |
| 57 | +def update_platform_sdk_version(sdk_target_location, force): |
| 58 | + print("Update SDK in " + sdk_target_location) |
| 59 | + if not os.path.exists(os.path.join(sdk_target_location, '.west')): |
| 60 | + print("--update-only error SDK is not initialized") |
| 61 | + sys.exit(1) |
| 62 | + command = ['west', 'update', '--fetch', 'smart'] |
| 63 | + try: |
| 64 | + subprocess.run(command, cwd=sdk_target_location, check=True) |
| 65 | + except (RuntimeError, subprocess.CalledProcessError) as exception: |
| 66 | + if force: |
| 67 | + clean_sdk(sdk_target_location) |
| 68 | + else: |
| 69 | + print(exception) |
| 70 | + print("Error SDK cannot be updated, local changes should be cleaned manually or use --force to force update") |
| 71 | + print("WARNING -- With --force all local SDK modification(s) will be lost") |
| 72 | + |
| 73 | + |
| 74 | +def main(): |
| 75 | + |
| 76 | + parser = argparse.ArgumentParser(description='Checkout or update relevant NXP SDK') |
| 77 | + parser.add_argument( |
| 78 | + "--update-only", help="Update NXP SDK to the correct version. Would fail if the SDK does not exist", action="store_true") |
| 79 | + parser.add_argument('--platform', nargs='+', choices=all_platform_sdk_list, default=all_platform_sdk_list, |
| 80 | + help='Allows to select which SDK for a particular NXP platform to initialize') |
| 81 | + parser.add_argument('--force', action='store_true', |
| 82 | + help='Force SDK initialization, hard clean will be done in case of failure - WARNING -- All local SDK modification(s) will be lost') |
| 83 | + |
| 84 | + args = parser.parse_args() |
| 85 | + |
| 86 | + for current_plat in args.platform: |
| 87 | + plat_path = list(map(lambda res: res['plat_sdk_manifest_path'], filter( |
| 88 | + lambda cmd: cmd["plat_name"] == current_plat, ALL_PLATFORM_SDK)))[0] |
| 89 | + if args.update_only: |
| 90 | + update_platform_sdk_version(os.path.join(CHIP_ROOT, plat_path), args.force) |
| 91 | + else: |
| 92 | + init_platform_sdk_version(os.path.join(CHIP_ROOT, plat_path), args.force) |
| 93 | + |
| 94 | + |
| 95 | +if __name__ == '__main__': |
| 96 | + main() |
0 commit comments