• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 os
18from pathlib import Path
19from typing import Sequence
20import subprocess
21
22import pw_package.git_repo
23import pw_package.package_manager
24
25
26@contextmanager
27def change_working_dir(directory: Path):
28    original_dir = Path.cwd()
29    try:
30        os.chdir(directory)
31        yield directory
32    finally:
33        os.chdir(original_dir)
34
35
36class PiPicoSdk(pw_package.package_manager.Package):
37    """Install and check status of the Raspberry Pi Pico SDK."""
38
39    def __init__(self, *args, **kwargs):
40        super().__init__(*args, name='pico_sdk', **kwargs)
41        self._pico_sdk = pw_package.git_repo.GitRepo(
42            name='pico_sdk',
43            url='https://github.com/raspberrypi/pico-sdk',
44            commit='2e6142b15b8a75c1227dd3edbe839193b2bf9041',
45        )
46
47    def install(self, path: Path) -> None:
48        self._pico_sdk.install(path)
49
50        # Run submodule update --init to fetch tinyusb.
51        with change_working_dir(path) as _pico_sdk_repo:
52            subprocess.run(
53                ['git', 'submodule', 'update', '--init'], capture_output=True
54            )
55
56    def info(self, path: Path) -> Sequence[str]:
57        return (
58            f'{self.name} installed in: {path}',
59            "Enable by running 'gn args out' and adding this line:",
60            f'  PICO_SRC_DIR = "{path}"',
61        )
62
63
64pw_package.package_manager.register(PiPicoSdk)
65