xref: /aosp_15_r20/external/pigweed/pw_package/py/pw_package/packages/boringssl.py (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1# Copyright 2021 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 BoringSSL + Chromium verifier."""
15
16import os
17import pathlib
18import subprocess
19from typing import Sequence
20import pw_package.git_repo
21import pw_package.package_manager
22
23
24def boringssl_repo_path(path: pathlib.Path) -> pathlib.Path:
25    return path / 'src'
26
27
28class BoringSSL(pw_package.package_manager.Package):
29    """Install and check status of BoringSSL and chromium verifier."""
30
31    def __init__(self, *args, **kwargs):
32        super().__init__(*args, name='boringssl', **kwargs)
33        self._boringssl = pw_package.git_repo.GitRepo(
34            name='boringssl',
35            url=''.join(
36                [
37                    'https://pigweed.googlesource.com',
38                    '/third_party/boringssl/boringssl',
39                ]
40            ),
41            commit='6d3db84c47643271cb553593ee67362be3820874',
42        )
43        self._allow_use_in_downstream = False
44
45    def status(self, path: pathlib.Path) -> bool:
46        if not self._boringssl.status(boringssl_repo_path(path)):
47            return False
48
49        # Check that necessary build files are generated.
50        build_files = ['BUILD.generated.gni', 'err_data.c']
51        return all(os.path.exists(path / file) for file in build_files)
52
53    def install(self, path: pathlib.Path) -> None:
54        # Checkout the library
55        repo_path = boringssl_repo_path(path)
56        self._boringssl.install(repo_path)
57
58        # BoringSSL provides a src/util/generate_build_files.py script for
59        # generating build files. Call the script after checkout so that
60        # our .gn build script can pick them up.
61        script = repo_path / 'util' / 'generate_build_files.py'
62        if not os.path.exists(script):
63            raise FileNotFoundError('Fail to find generate_build_files.py')
64        subprocess.run(['python', script, 'gn'], cwd=path)
65
66        # TODO(zyecheng): Add install logic for chromium certificate verifier.
67
68    def info(self, path: pathlib.Path) -> Sequence[str]:
69        return (
70            f'{self.name} installed in: {path}',
71            'Enable by running "gn args out" and adding this line:',
72            f'  dir_pw_third_party_boringssl = "{path}"',
73        )
74
75
76pw_package.package_manager.register(BoringSSL)
77