pw_thread_stl: Add CMake support

Change-Id: Ic6d8b0d8de748236fbb8d718a2bec5dbdebedb55
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/79475
Reviewed-by: Wyatt Hepler <hepler@google.com>
Commit-Queue: Ewout van Bekkum <ewout@google.com>
Pigweed-Auto-Submit: Ewout van Bekkum <ewout@google.com>
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 0f27885..9815ca1 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -71,6 +71,7 @@
 add_subdirectory(pw_system EXCLUDE_FROM_ALL)
 add_subdirectory(pw_thread EXCLUDE_FROM_ALL)
 add_subdirectory(pw_thread_freertos EXCLUDE_FROM_ALL)
+add_subdirectory(pw_thread_stl EXCLUDE_FROM_ALL)
 add_subdirectory(pw_tokenizer EXCLUDE_FROM_ALL)
 add_subdirectory(pw_trace EXCLUDE_FROM_ALL)
 add_subdirectory(pw_trace_tokenized EXCLUDE_FROM_ALL)
diff --git a/pw_build/pigweed.cmake b/pw_build/pigweed.cmake
index 3266f49..7a8ee9c 100644
--- a/pw_build/pigweed.cmake
+++ b/pw_build/pigweed.cmake
@@ -390,6 +390,10 @@
       pw_unit_test.main
       ${arg_DEPS}
   )
+  # Tests require at least one source file.
+  if(NOT arg_SOURCES)
+    target_sources("${NAME}" PRIVATE $<TARGET_PROPERTY:pw_build.empty,SOURCES>)
+  endif()
 
   # Define a target for running the test. The target creates a stamp file to
   # indicate successful test completion. This allows running tests in parallel
diff --git a/pw_thread_stl/CMakeLists.txt b/pw_thread_stl/CMakeLists.txt
new file mode 100644
index 0000000..86f916e
--- /dev/null
+++ b/pw_thread_stl/CMakeLists.txt
@@ -0,0 +1,93 @@
+# 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.
+
+include($ENV{PW_ROOT}/pw_build/pigweed.cmake)
+
+# This target provides the backend for pw::thread::Id & pw::this_thread::get_id.
+pw_add_module_library(pw_thread_stl.id
+  IMPLEMENTS_FACADES
+    pw_thread.id
+  HEADERS
+    public/pw_thread_stl/id_inline.h
+    public/pw_thread_stl/id_native.h
+    public_overrides/pw_thread_backend/id_inline.h
+    public_overrides/pw_thread_backend/id_native.h
+  PUBLIC_INCLUDES
+    public
+    public_overrides
+)
+
+# This target provides the backend for pw::thread::Thread with joining
+# joining capability.
+pw_add_module_library(pw_thread_stl.thread
+  IMPLEMENTS_FACADES
+    pw_thread.thread
+  HEADERS
+    public/pw_thread_stl/options.h
+    public/pw_thread_stl/thread_inline.h
+    public/pw_thread_stl/thread_native.h
+    public_overrides/pw_thread_backend/thread_inline.h
+    public_overrides/pw_thread_backend/thread_native.h
+  PUBLIC_INCLUDES
+    public
+    public_overrides
+)
+
+
+# This target provides the backend for pw::this_thread::sleep_{for,until}.
+pw_add_module_library(pw_thread_stl.sleep
+  IMPLEMENTS_FACADES
+    pw_thread.sleep
+  HEADERS
+    public/pw_thread_stl/sleep_inline.h
+    public_overrides/pw_thread_backend/sleep_inline.h
+  PUBLIC_INCLUDES
+    public
+    public_overrides
+  PUBLIC_DEPS
+    pw_chrono.system_clock
+)
+
+# This target provides the backend for pw::this_thread::yield.
+pw_add_module_library(pw_thread_stl.yield
+  IMPLEMENTS_FACADES
+    pw_thread.yield
+  HEADERS
+    public/pw_thread_stl/yield_inline.h
+    public_overrides/pw_thread_backend/yield_inline.h
+  PUBLIC_INCLUDES
+    public
+    public_overrides
+)
+
+pw_add_module_library(pw_thread_stl.test_threads
+  PUBLIC_DEPS
+    pw_thread.test_threads
+  SOURCES
+    test_threads.cc
+  PRIVATE_DEPS
+    pw_thread.thread
+)
+
+if(("${pw_thread.thread_BACKEND}" STREQUAL "pw_thread_stl.thread") AND
+   (NOT "${pw_thread.sleep_BACKEND}" STREQUAL "pw_thread.sleep.NO_BACKEND_SET"))
+  pw_add_test(pw_thread_stl.thread_backend_test
+    DEPS
+      pw_thread_stl.test_threads
+      pw_thread.thread_facade_test
+    GROUPS
+      modules
+      pw_thread_stl
+  )
+endif()