pw_build: Always set the output_dir in pw targets

Some projects may use toolchains the dump all executables and libraries
into a common directory. This breaks pigweed's build as pigweed contains
multiple tests with the same name.

Fix this by always setting the output directory instead of taking the
toolchain default.

This also makes overriding the output_dir for pw_test() work properly.
Previously the runner could not find the test in this case.

Change-Id: Id69c78384454f798d4a425348926df31a16da8ec
diff --git a/pw_build/target_types.gni b/pw_build/target_types.gni
index 51c094a..1022b1c 100644
--- a/pw_build/target_types.gni
+++ b/pw_build/target_types.gni
@@ -86,6 +86,11 @@
 template("pw_static_library") {
   import("$dir_pw_build/defaults.gni")
 
+  _library_output_dir = target_out_dir
+  if (defined(invoker.output_dir)) {
+    _library_output_dir = invoker.output_dir
+  }
+
   static_library(target_name) {
     forward_variables_from(invoker, "*", _supported_toolchain_defaults)
 
@@ -121,12 +126,19 @@
     if (defined(invoker.public_deps)) {
       public_deps += invoker.public_deps
     }
+
+    output_dir = _library_output_dir
   }
 }
 
 template("pw_shared_library") {
   import("$dir_pw_build/defaults.gni")
 
+  _library_output_dir = target_out_dir
+  if (defined(invoker.output_dir)) {
+    _library_output_dir = invoker.output_dir
+  }
+
   shared_library(target_name) {
     forward_variables_from(invoker, "*", _supported_toolchain_defaults)
 
@@ -162,6 +174,8 @@
     if (defined(invoker.public_deps)) {
       public_deps += invoker.public_deps
     }
+
+    output_dir = _library_output_dir
   }
 }
 
@@ -170,6 +184,11 @@
 template("pw_executable") {
   import("$dir_pw_build/defaults.gni")
 
+  _executable_output_dir = target_out_dir
+  if (defined(invoker.output_dir)) {
+    _executable_output_dir = invoker.output_dir
+  }
+
   target(pw_build_EXECUTABLE_TARGET_TYPE, target_name) {
     forward_variables_from(invoker, "*", _supported_toolchain_defaults)
 
@@ -205,5 +224,7 @@
     if (defined(invoker.public_deps)) {
       public_deps += invoker.public_deps
     }
+
+    output_dir = _executable_output_dir
   }
 }
diff --git a/pw_unit_test/test.gni b/pw_unit_test/test.gni
index 481f56b..33ebdd1 100644
--- a/pw_unit_test/test.gni
+++ b/pw_unit_test/test.gni
@@ -95,6 +95,13 @@
 
   _test_is_enabled = !defined(invoker.enable_if) || invoker.enable_if
 
+  # Always set the output_dir as pigweed is not compatible with shared
+  # bin directories for tests.
+  _test_output_dir = target_out_dir
+  if (defined(invoker.output_dir)) {
+    _test_output_dir = invoker.output_dir
+  }
+
   _pw_disableable_target(_test_target_name) {
     target_type = "pw_executable"
     enable_if = _test_is_enabled
@@ -105,7 +112,7 @@
         {
           type = "test"
           test_name = _test_target_name
-          test_directory = rebase_path(target_out_dir, root_build_dir)
+          test_directory = rebase_path(_test_output_dir, root_build_dir)
         },
       ]
     }
@@ -116,6 +123,8 @@
       deps = []
     }
     deps += [ pw_unit_test_MAIN ]
+
+    output_dir = _test_output_dir
   }
 
   if (pw_unit_test_AUTOMATIC_RUNNER != "") {
@@ -141,7 +150,7 @@
         "--runner",
         pw_unit_test_AUTOMATIC_RUNNER,
         "--test",
-        get_path_info("$target_out_dir:$_test_to_run", "abspath"),
+        get_path_info("$_test_output_dir:$_test_to_run", "abspath"),
       ]
       stamp = true
     }