diff --git a/examples/build_overrides/pigweed_environment.gni b/examples/build_overrides/pigweed_environment.gni index bcc75edcff777b..538e35fca20efc 100644 --- a/examples/build_overrides/pigweed_environment.gni +++ b/examples/build_overrides/pigweed_environment.gni @@ -20,69 +20,34 @@ _bootstrap_root = "//third_party/connectedhomeip" import("${_bootstrap_root}/build_overrides/pigweed_environment.gni") # Rebase paths to our root. -# -# If out of tree, the paths will look like: -# -# pw_env_setup_CIPD_PIGWEED = "//../home/vscode/pigweed/env/cipd/packages/pigweed" -# -# and these paths are used by things like protoc since -# https://github.com/google/pigweed/commit/ddbc9fc7f5c601ab417db37e02cbe5294f21ad6d -# -# See https://github.com/project-chip/connectedhomeip/issues/30475 -# -# Existing logic: -# - this file is imported from `examples/common/pigweed` or similar -# - we transform paths from "//../" into "//../../../" -# -# TODO: need a better expansion here. This replacement logic seems very brittle and -# it is unclear how we know exactly 3 levels of indirections are correct -# +# This rebasing is only done if the paths are repo-relative +# (i.e. start with "//") if (defined(pw_env_setup_CIPD_ARM)) { - _split_arm = string_split(pw_env_setup_CIPD_ARM, "//../") - if (_split_arm[0] == "") { - pw_env_setup_CIPD_ARM = get_path_info( - string_replace(pw_env_setup_CIPD_ARM, "//../", "//../../../"), - "abspath") - } else { + _split_CIPD_ARM = string_split(pw_env_setup_CIPD_ARM, "//") + if (_split_CIPD_ARM[0] == "") { pw_env_setup_CIPD_ARM = get_path_info("${_bootstrap_root}/${pw_env_setup_CIPD_ARM}", "abspath") } } if (defined(pw_env_setup_CIPD_PIGWEED)) { - _split_pigweed = string_split(pw_env_setup_CIPD_PIGWEED, "//../") - if (_split_pigweed[0] == "") { - pw_env_setup_CIPD_PIGWEED = - get_path_info( - string_replace(pw_env_setup_CIPD_PIGWEED, "//../", "//../../../"), - "abspath") - } else { + _split_CIPD_PIGWEED = string_split(pw_env_setup_CIPD_PIGWEED, "//") + if (_split_CIPD_PIGWEED[0] == "") { pw_env_setup_CIPD_PIGWEED = get_path_info("${_bootstrap_root}/${pw_env_setup_CIPD_PIGWEED}", "abspath") } } - if (defined(pw_env_setup_CIPD_PYTHON)) { - _split_python = string_split(pw_env_setup_CIPD_PYTHON, "//../") - if (_split_python[0] == "") { - pw_env_setup_CIPD_PYTHON = - get_path_info( - string_replace(pw_env_setup_CIPD_PYTHON, "//../", "//../../../"), - "abspath") - } else { + _split_CIPD_PYTHON = string_split(pw_env_setup_CIPD_PYTHON, "//") + if (_split_CIPD_PYTHON[0] == "") { pw_env_setup_CIPD_PYTHON = get_path_info("${_bootstrap_root}/${pw_env_setup_CIPD_PYTHON}", "abspath") } } if (defined(pw_env_setup_VIRTUAL_ENV)) { - _split_env = string_split(pw_env_setup_VIRTUAL_ENV, "//../") - if (_split_env[0] == "") { - pw_env_setup_VIRTUAL_ENV = - get_path_info( - string_replace(pw_env_setup_VIRTUAL_ENV, "//../", "//../../../"), - "abspath") - } else { + _split_VIRTUAL_ENV = string_split(pw_env_setup_VIRTUAL_ENV, "//") + if (_split_VIRTUAL_ENV[0] == "") { pw_env_setup_VIRTUAL_ENV = get_path_info("${_bootstrap_root}/${pw_env_setup_VIRTUAL_ENV}", "abspath") diff --git a/scripts/setup/bootstrap.sh b/scripts/setup/bootstrap.sh index 1b813216222efe..eda6e97ad9c9bd 100644 --- a/scripts/setup/bootstrap.sh +++ b/scripts/setup/bootstrap.sh @@ -165,6 +165,15 @@ if [ -n "$BASH" ]; then . "$_CHIP_ROOT/scripts/helpers/bash-completion.sh" fi +# Update relative paths to absolute (if they exist) +# to make sure loading of paths works in build_examples +# +# See https://github.com/project-chip/connectedhomeip/issues/30475 +# for details +scripts/setup/gni_make_paths_absolute.py \ + --root "$_CHIP_ROOT" \ + build_overrides/pigweed_environment.gni + unset -f _bootstrap_or_activate unset -f _install_additional_pip_requirements diff --git a/scripts/setup/gni_make_paths_absolute.py b/scripts/setup/gni_make_paths_absolute.py new file mode 100755 index 00000000000000..17f03820458bba --- /dev/null +++ b/scripts/setup/gni_make_paths_absolute.py @@ -0,0 +1,54 @@ +#!/usr/bin/env python3 + +# Copyright (c) 2023 Project CHIP Authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +import argparse + + +def _make_paths_absolute(gni_file: str, root: str): + with open(gni_file, "rt", encoding="utf8") as f: + data = f.read() + + # Data will contain key/value pairs like: + # pw_env_setup_CIPD_PIGWEED = "//../home/vscode/pigweed/env/cipd/packages/pigweed" + # + # Looking to replace relative paths (starting with "//../") + # to absolute paths from the root + if not root.endswith("/"): + root = root + "/" + + new_data = data.replace('"//../', f'"{root}../') + + if new_data == data: + return + + with open(gni_file, "wt", encoding="utf8") as f: + f.write(new_data) + + +def main(): + parser = argparse.ArgumentParser( + description=__doc__, + formatter_class=argparse.ArgumentDefaultsHelpFormatter, + ) + parser.add_argument( + '--root', '-r', required=True, + help="Set the root used by the build (usually CHIP_ROOT)" + ) + parser.add_argument('gni_file', type=str, help="GNI file to process") + _make_paths_absolute(**vars(parser.parse_args())) + + +if __name__ == '__main__': + main()