pw_build/cmake: Enforce full path in CMake library names

Instead of only enforcing the parent directory to be in the CMake
library name, pw_add_module_library is updated to enforce that
the entire relative to PW root directory path is in the library
name in a dot separated format.

Change-Id: I29f3fa499474a53ff29a04ed2f353d560edd1451
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/94540
Pigweed-Auto-Submit: Ewout van Bekkum <ewout@google.com>
Reviewed-by: Keir Mierle <keir@google.com>
Commit-Queue: Auto-Submit <auto-submit@pigweed.google.com.iam.gserviceaccount.com>
diff --git a/pw_build/pigweed.cmake b/pw_build/pigweed.cmake
index cb5a193..852edf0 100644
--- a/pw_build/pigweed.cmake
+++ b/pw_build/pigweed.cmake
@@ -293,13 +293,15 @@
   _pw_add_library_multi_value_args(multi_value_args IMPLEMENTS_FACADES)
   pw_parse_arguments_strict(pw_add_module_library 1 "" "" "${multi_value_args}")
 
-  # Check that the library's name is prefixed by the module name.
-  get_filename_component(module "${CMAKE_CURRENT_SOURCE_DIR}" NAME)
-
-  if(NOT "${NAME}" MATCHES "${module}(\\.[^\\.]+)?(\\.facade)?$")
+  # Check that the library's name is prefixed by the relative PW path with dot
+  # separators instead of forward slashes.
+  file(RELATIVE_PATH rel_path $ENV{PW_ROOT} ${CMAKE_CURRENT_SOURCE_DIR})
+  string(REPLACE "/" "." dot_rel_path ${rel_path})
+  if(NOT "${NAME}" MATCHES "${dot_rel_path}(\\.[^\\.]+)?(\\.facade)?$")
     message(FATAL_ERROR
         "Module libraries must match the module name or be in the form "
-        "'MODULE_NAME.LIBRARY_NAME'. The library '${NAME}' does not match."
+        "'PATH_TO.THE_TARGET.NAME'. The library '${NAME}' does not match. "
+        "Expected ${dot_rel_path}.LIBRARY_NAME"
     )
   endif()