From d15daf8a02b972948147536bc18c291bea2fd384 Mon Sep 17 00:00:00 2001
From: Andrei Litvin <andy314@gmail.com>
Date: Thu, 5 Oct 2023 13:12:33 -0400
Subject: [PATCH] Update python test CI: stop dirtying build env, cleanup build
 outputs (#29584)

* Prevent dirtying the buildenv, make sure things are cleaned

* Virtualenv seems to be installed in the buildenv ... use that so we do not need to globally install

* Force bash shell because source requires it

* Stop generating link map files for the python builds..they just take up space and this saves 2GB
---
 .github/workflows/build.yaml      | 29 +++++++++++++++++++++--------
 build/toolchain/gcc_toolchain.gni | 10 ++++++++++
 2 files changed, 31 insertions(+), 8 deletions(-)

diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml
index 569ff10a95c70b..d091dc346b9bf6 100644
--- a/.github/workflows/build.yaml
+++ b/.github/workflows/build.yaml
@@ -303,21 +303,34 @@ jobs:
 
             - name: Setup Build, Run Build and Run Tests
               run: |
-                  scripts/build/gn_gen.sh --args="enable_rtti=true enable_pylib=true chip_config_memory_debug_checks=false chip_config_memory_debug_dmalloc=false"
+                  scripts/build/gn_gen.sh --args="enable_rtti=true enable_pylib=true chip_config_memory_debug_checks=false chip_config_memory_debug_dmalloc=false chip_generate_link_map_file=false"
                   scripts/run_in_build_env.sh "ninja -C ./out"
                   scripts/tests/gn_tests.sh
-            - name: Run Python library specific unit tests
+            - name: Setup test python environment
+              shell: bash
               run: |
-                  scripts/run_in_build_env.sh 'pip3 install ./out/controller/python/chip_core-0.0-cp37-abi3-linux_x86_64.whl'
-                  scripts/run_in_build_env.sh 'pip3 install ./out/controller/python/chip_clusters-0.0-py3-none-any.whl'
-                  scripts/run_in_build_env.sh 'pip3 install ./out/controller/python/chip_repl-0.0-py3-none-any.whl'
-                  scripts/run_in_build_env.sh '(cd src/controller/python/test/unit_tests/ && python3 -m unittest -v)'
+                  scripts/run_in_build_env.sh 'virtualenv pyenv'
+                  source pyenv/bin/activate
+                  pip3 install ./out/controller/python/chip_core-0.0-cp37-abi3-linux_x86_64.whl
+                  pip3 install ./out/controller/python/chip_clusters-0.0-py3-none-any.whl
+                  pip3 install ./out/controller/python/chip_repl-0.0-py3-none-any.whl
 
+            - name: Run Python tests
+              shell: bash
+              run: |
+                  source pyenv/bin/activate
+                  cd src/controller/python/test/unit_tests/
+                  python3 -m unittest -v
+            - name: Clean previous outputs
+              run: rm -rf out pyenv
             - name: Run Python Setup Payload Generator Test
+              shell: bash
               run: |
                   scripts/run_in_build_env.sh 'scripts/examples/gn_build_example.sh examples/chip-tool out/'
-                  scripts/run_in_build_env.sh 'pip3 install -r src/setup_payload/python/requirements.txt'
-                  scripts/run_in_build_env.sh 'python3 src/setup_payload/tests/run_python_setup_payload_gen_test.py out/chip-tool'
+                  scripts/run_in_build_env.sh 'virtualenv pyenv'
+                  source pyenv/bin/activate
+                  pip3 install -r src/setup_payload/python/requirements.txt
+                  python3 src/setup_payload/tests/run_python_setup_payload_gen_test.py out/chip-tool
 
     build_darwin:
         name: Build on Darwin (clang, python_lib, simulated)
diff --git a/build/toolchain/gcc_toolchain.gni b/build/toolchain/gcc_toolchain.gni
index fba42eb8349f1f..b4b39daf711fb0 100644
--- a/build/toolchain/gcc_toolchain.gni
+++ b/build/toolchain/gcc_toolchain.gni
@@ -15,6 +15,14 @@
 import("//build_overrides/pigweed.gni")
 import("$dir_pw_toolchain/generate_toolchain.gni")
 
+declare_args() {
+  # Generate Linker map files. Can skip since they can
+  # be quite large.
+  #
+  # Note that toolchains can individually override this
+  chip_generate_link_map_file = true
+}
+
 template("gcc_toolchain") {
   invoker_toolchain_args = invoker.toolchain_args
 
@@ -42,6 +50,8 @@ template("gcc_toolchain") {
 
     if (defined(invoker.link_generate_map_file)) {
       link_generate_map_file = invoker.link_generate_map_file
+    } else {
+      link_generate_map_file = chip_generate_link_map_file
     }
 
     is_host_toolchain = invoker_toolchain_args.current_os == host_os