Add env setup

Bug: b/249997815
Change-Id: I5ced93efa00dabc2732e1161dc492e154b1e380b
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/zephyr-integration/+/112750
Commit-Queue: Rob Mohr <mohrr@google.com>
Reviewed-by: Anthony DiGirolamo <tonymd@google.com>
diff --git a/.gitignore b/.gitignore
index 000a9af..5b81d21 100644
--- a/.gitignore
+++ b/.gitignore
@@ -10,3 +10,7 @@
 
 # IDE specific directories
 .idea/
+
+# Pigweed Environment
+build_overrides/pigweed_environment.gni
+environment/
diff --git a/.gn b/.gn
new file mode 100644
index 0000000..a9e56c3
--- /dev/null
+++ b/.gn
@@ -0,0 +1,20 @@
+# Copyright 2020 The Pigweed 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
+#
+#     https://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.
+
+buildconfig = "//BUILDCONFIG.gn"
+
+default_args = {
+  # Use the new Python build and merged 'pigweed' Python package.
+  pw_build_USE_NEW_PYTHON_BUILD = true
+}
diff --git a/BUILD.gn b/BUILD.gn
new file mode 100644
index 0000000..1eb7a72
--- /dev/null
+++ b/BUILD.gn
@@ -0,0 +1,79 @@
+# Copyright 2022 The Pigweed 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
+#
+#     https://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("//build_overrides/pigweed.gni")
+import("$dir_pw_build/python.gni")
+import("$dir_pw_build/python_dist.gni")
+import("$dir_pw_build/python_venv.gni")
+
+# NOTE: All the `deps` listed in `//BUILD.gn` must either be instantiated with a
+# toolchain, e.g. `":my_host_target(//path/to/host:toolchain)"`, or listed
+# within an `if (current_toolchain != default_toolchain)` statement.
+# It also prevents the default toolchain from parsing any unnecessary BUILD.gn
+# files.
+
+# This group is built during bootstrap to setup the interactive Python
+# environment.
+pw_python_group("python") {
+  python_deps = [
+    # Generate and pip install _all_python_packages
+    ":pip_install_zephyr_integration_tools",
+  ]
+}
+
+# In-tree Python packages
+_zephyr_integration_python_packages = []
+
+# Pigweed Python packages to include
+_pigweed_python_packages = [ "$dir_pw_env_setup:core_pigweed_python_packages" ]
+
+_all_python_packages =
+    _zephyr_integration_python_packages + _pigweed_python_packages
+
+# The default venv for Python actions in GN
+# Set this gn arg in a declare_args block in this file 'BUILD.gn' or in '.gn' to
+# use this venv.
+#
+#   pw_build_PYTHON_BUILD_VENV = "//:zephyr_integration_build_venv"
+#
+pw_python_venv("zephyr_integration_build_venv") {
+  path = "$root_build_dir/python-venv"
+  constraints = pw_build_PIP_CONSTRAINTS
+  requirements = pw_build_PIP_REQUIREMENTS
+
+  # Ensure all third party Python dependencies are installed into this venv.
+  # This works by checking the setup.cfg files for all packages listed here and
+  # installing the packages listed in the [options].install_requires field.
+  source_packages = _all_python_packages
+}
+
+# This template collects all python packages and their dependencies into a
+# single super Python package for installation into the bootstrapped virtual
+# environment.
+pw_create_python_source_tree(
+    "generate_zephyr_integration_tools_python_distribution") {
+  packages = _all_python_packages
+  generate_setup_cfg = {
+    name = "sample-project-tools"
+    version = "0.0.1"
+    append_date_to_version = true
+    include_default_pyproject_file = true
+  }
+}
+
+# Install the sample-project-tools super Python package into the bootstrapped
+# Python venv.
+pw_internal_pip_install("pip_install_zephyr_integration_tools") {
+  packages = [ ":generate_zephyr_integration_tools_python_distribution" ]
+}
diff --git a/BUILDCONFIG.gn b/BUILDCONFIG.gn
new file mode 100644
index 0000000..ea6cf58
--- /dev/null
+++ b/BUILDCONFIG.gn
@@ -0,0 +1,23 @@
+# Copyright 2020 The Pigweed 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
+#
+#     https://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.
+
+# This is imported into a scope so as not to pollute the global variable space.
+_pigweed_directory = {
+  import("//build_overrides/pigweed.gni")
+}
+
+# The default toolchain is not used in Pigweed builds, so it is set to an empty
+# toolchain. The top-level BUILD.gn should stamp a group with all of the build
+# targets and their toolchains.
+set_default_toolchain("${_pigweed_directory.dir_pw_toolchain}/default")
diff --git a/activate.sh b/activate.sh
new file mode 120000
index 0000000..eba5389
--- /dev/null
+++ b/activate.sh
@@ -0,0 +1 @@
+bootstrap.sh
\ No newline at end of file
diff --git a/bootstrap.sh b/bootstrap.sh
new file mode 100644
index 0000000..fa36033
--- /dev/null
+++ b/bootstrap.sh
@@ -0,0 +1,134 @@
+# Copyright 2020 The Pigweed 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
+#
+#     https://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.
+
+# This script must be sourced, not executed.
+#
+# Create a new environment:
+#
+#   source bootstrap.sh
+#
+# Activate an existing environment:
+#
+#   source activate.sh
+
+_bootstrap_abspath () {
+  $(command -v python3 || command -v python2 || command -v python) -c "import os.path; print(os.path.abspath('$@'))"
+}
+
+# Shell: bash.
+if test -n "$BASH"; then
+  _BOOTSTRAP_PATH="$(_bootstrap_abspath "$BASH_SOURCE")"
+# Shell: zsh.
+elif test -n "$ZSH_NAME"; then
+  _BOOTSTRAP_PATH="$(_bootstrap_abspath "${(%):-%N}")"
+# Shell: dash.
+elif test ${0##*/} = dash; then
+  _BOOTSTRAP_PATH="$(_bootstrap_abspath \
+    "$(lsof -p $$ -Fn0 | tail -1 | sed 's#^[^/]*##;')")"
+# If everything else fails, try $0. It could work.
+else
+  _BOOTSTRAP_PATH="$(_bootstrap_abspath "$0")"
+fi
+
+# Check if this file is being executed or sourced.
+_pw_sourced=0
+if [ -n "$SWARMING_BOT_ID" ]; then
+  # If set we're running on swarming and don't need this check.
+  _pw_sourced=1
+elif [ -n "$ZSH_EVAL_CONTEXT" ]; then
+  case $ZSH_EVAL_CONTEXT in *:file) _pw_sourced=1;; esac
+elif [ -n "$KSH_VERSION" ]; then
+  [ "$(cd $(dirname -- $0) && pwd -P)/$(basename -- $0)" != \
+    "$(cd $(dirname -- ${.sh.file}) && pwd -P)/$(basename -- ${.sh.file})" ] \
+    && _pw_sourced=1
+elif [ -n "$BASH_VERSION" ]; then
+  (return 0 2>/dev/null) && _pw_sourced=1
+else  # All other shells: examine $0 for known shell binary filenames
+  # Detects `sh` and `dash`; add additional shell filenames as needed.
+  case ${0##*/} in sh|dash) _pw_sourced=1;; esac
+fi
+
+# Downstream projects need to set something other than PW_ROOT here, like
+# YOUR_PROJECT_ROOT. Please also set PW_PROJECT_ROOT and PW_ROOT before
+# invoking pw_bootstrap or pw_activate.
+######### BEGIN PROJECT-SPECIFIC CODE #########
+ZEPHYR_INTEGRATION_ROOT="$(_bootstrap_abspath "$(dirname "$_BOOTSTRAP_PATH")")"
+export ZEPHYR_INTEGRATION_ROOT
+PW_PROJECT_ROOT="$ZEPHYR_INTEGRATION_ROOT"
+PW_ROOT="$ZEPHYR_INTEGRATION_ROOT/pigweed"
+
+# Set your project's banner and color.
+export PW_BRANDING_BANNER="$ZEPHYR_INTEGRATION_ROOT/config/banner.txt"
+export PW_BRANDING_BANNER_COLOR=magenta
+
+zephyr_integration_banner() {
+  cat "$PW_BRANDING_BANNER"
+  echo
+}
+
+PW_BANNER_FUNC="zephyr_integration_banner"
+########## END PROJECT-SPECIFIC CODE ##########
+export PW_BANNER_FUNC
+export PW_PROJECT_ROOT
+export PW_ROOT
+
+_util_sh="$PW_ROOT/pw_env_setup/util.sh"
+
+# Double-check that the Pigweed submodule has been checked out.
+if [ ! -e "$_util_sh" ]; then
+  echo "Error: $_util_sh not found."
+  echo "Did you forget to initialize the git submodules?"
+  echo "To setup the git submodules run:"
+  echo "  git submodule update --init"
+  return
+fi
+
+. "$_util_sh"
+
+pw_deactivate
+pw_eval_sourced "$_pw_sourced"
+pw_check_root "$PW_ROOT"
+_PW_ACTUAL_ENVIRONMENT_ROOT="$(pw_get_env_root)"
+export _PW_ACTUAL_ENVIRONMENT_ROOT
+SETUP_SH="$_PW_ACTUAL_ENVIRONMENT_ROOT/activate.sh"
+
+# Run full bootstrap when invoked as bootstrap, or env file is missing/empty.
+if [ "$(basename "$_BOOTSTRAP_PATH")" = "bootstrap.sh" ] || \
+  [ ! -f "$SETUP_SH" ] || \
+  [ ! -s "$SETUP_SH" ]; then
+# This is where pw_bootstrap is called. Most small projects will include
+# --use-pigweed-defaults.
+######### BEGIN PROJECT-SPECIFIC CODE #########
+  pw_bootstrap --shell-file "$SETUP_SH" --install-dir "$_PW_ACTUAL_ENVIRONMENT_ROOT" --config-file "$PW_PROJECT_ROOT/config/env_setup.json"
+########## END PROJECT-SPECIFIC CODE ##########
+  pw_finalize bootstrap "$SETUP_SH"
+else
+  pw_activate
+  pw_finalize activate "$SETUP_SH"
+fi
+
+if [ "$_PW_ENV_SETUP_STATUS" -eq 0 ]; then
+# This is where any additional checks about the environment should go.
+######### BEGIN PROJECT-SPECIFIC CODE #########
+  echo -n
+########## END PROJECT-SPECIFIC CODE ##########
+fi
+
+unset _pw_sourced
+unset _BOOTSTRAP_PATH
+unset SETUP_SH
+unset _bootstrap_abspath
+unset _util_sh
+
+pw_cleanup
diff --git a/build_overrides/pigweed.gni b/build_overrides/pigweed.gni
new file mode 100644
index 0000000..eb4fd7c
--- /dev/null
+++ b/build_overrides/pigweed.gni
@@ -0,0 +1,20 @@
+# Copyright 2020 The Pigweed 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
+#
+#     https://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.
+
+declare_args() {
+  # Location of the Pigweed repository.
+  dir_pigweed = "//pigweed/"
+}
+
+import("$dir_pigweed/modules.gni")
diff --git a/config/banner.txt b/config/banner.txt
new file mode 100644
index 0000000..1ada100
--- /dev/null
+++ b/config/banner.txt
@@ -0,0 +1,5 @@
+ _____ _                     _      _    _____         _           
+|  _  |_|___ _ _ _ ___ ___ _| |    / |  |__   |___ ___| |_ _ _ ___ 
+|   __| | . | | | | -_| -_| . |   / /   |   __| -_| . |   | | |  _|
+|__|  |_|_  |_____|___|___|___|  |_/    |_____|___|  _|_|_|_  |_|  
+        |___|                                     |_|     |___|    
diff --git a/config/env_setup.json b/config/env_setup.json
new file mode 100644
index 0000000..2dd6bde
--- /dev/null
+++ b/config/env_setup.json
@@ -0,0 +1,14 @@
+{
+  "root_variable": "SAMPLE_PROJECT_ROOT",
+  "cipd_package_files": [
+    "pigweed/pw_env_setup/py/pw_env_setup/cipd_setup/default.json",
+    "config/zephyr_integration.json"
+  ],
+  "virtualenv": {
+    "gn_root": ".",
+    "gn_targets": [
+      ":python.install"
+    ]
+  },
+  "gni_file": "build_overrides/pigweed_environment.gni"
+}
diff --git a/config/zephyr_integration.json b/config/zephyr_integration.json
new file mode 100644
index 0000000..0967ef4
--- /dev/null
+++ b/config/zephyr_integration.json
@@ -0,0 +1 @@
+{}
diff --git a/test.sh b/test.sh
index d4257ff..da7ca7c 100755
--- a/test.sh
+++ b/test.sh
@@ -7,7 +7,7 @@
 cd $(dirname "$BASH_SOURCE")
 
 # Set up Pigweed's environment.
-. ./pigweed/bootstrap.sh
+. ./bootstrap.sh
 
 # Set up Zephyr's environment.
 echo "TODO(b/236263182) Add Zephyr environment setup."