CMake: Set PW_ROOT environment variable if unset

Change-Id: Id7a74d3d446403e519dc045603b40cdcae602e65
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/30360
Commit-Queue: Auto-Submit <auto-submit@pigweed.google.com.iam.gserviceaccount.com>
Reviewed-by: Rob Mohr <mohrr@google.com>
Pigweed-Auto-Submit: Wyatt Hepler <hepler@google.com>
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 2968a94..ff37967 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -16,6 +16,14 @@
 
 cmake_minimum_required(VERSION 3.16)
 
+# The PW_ROOT environment variable should be set in bootstrap. If it is not set,
+# set it to this directory.
+if("$ENV{PW_ROOT}" STREQUAL "")
+  message("The PW_ROOT environment variable is not set; "
+          "using ${CMAKE_CURRENT_LIST_DIR} within CMake")
+  set(ENV{PW_ROOT} "${CMAKE_CURRENT_LIST_DIR}")
+endif()
+
 add_subdirectory(pw_assert EXCLUDE_FROM_ALL)
 add_subdirectory(pw_assert_basic EXCLUDE_FROM_ALL)
 add_subdirectory(pw_assert_log EXCLUDE_FROM_ALL)