xref: /aosp_15_r20/external/pigweed/pw_package/py/pw_package/packages/pico_sdk.py (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1# Copyright 2022 The Pigweed Authors
2#
3# Licensed under the Apache License, Version 2.0 (the "License"); you may not
4# use this file except in compliance with the License. You may obtain a copy of
5# the License at
6#
7#     https://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12# License for the specific language governing permissions and limitations under
13# the License.
14"""Install and check status of the Raspberry Pi Pico SDK."""
15
16from contextlib import contextmanager
17import logging
18import os
19from pathlib import Path
20from typing import Sequence
21import subprocess
22
23import pw_package.git_repo
24import pw_package.package_manager
25
26_LOG = logging.getLogger(__package__)
27
28
29@contextmanager
30def change_working_dir(directory: Path):
31    original_dir = Path.cwd()
32    try:
33        os.chdir(directory)
34        yield directory
35    finally:
36        os.chdir(original_dir)
37
38
39class PiPicoSdk(pw_package.package_manager.Package):
40    """Install and check status of the Raspberry Pi Pico SDK."""
41
42    def __init__(self, *args, **kwargs):
43        super().__init__(*args, name='pico_sdk', **kwargs)
44        self._pico_sdk = pw_package.git_repo.GitRepo(
45            name='pico_sdk',
46            url=(
47                'https://pigweed.googlesource.com/'
48                'third_party/github/raspberrypi/pico-sdk'
49            ),
50            commit='6a7db34ff63345a7badec79ebea3aaef1712f374',
51        )
52
53    def install(self, path: Path) -> None:
54        self._pico_sdk.install(path)
55
56        # Run submodule update --init to fetch tinyusb.
57        with change_working_dir(path) as _pico_sdk_repo:
58            command = ['git', 'submodule', 'update', '--init']
59            _LOG.info('==> %s', ' '.join(command))
60            subprocess.run(command)
61
62    def info(self, path: Path) -> Sequence[str]:
63        return (
64            f'{self.name} installed in: {path}',
65            "Enable by running 'gn args out' and adding this line:",
66            f'  PICO_SRC_DIR = "{path}"',
67        )
68
69
70pw_package.package_manager.register(PiPicoSdk)
71