An rtc_library with no source file should be an rtc_source_set

Bug: webrtc:9838
Change-Id: I9b8db581e3e180aff11ffc38b1c9155b2bd3671c
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/260060
Commit-Queue: Florent Castelli <orphis@webrtc.org>
Reviewed-by: Mirko Bonadei <mbonadei@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#36687}
diff --git a/webrtc.gni b/webrtc.gni
index a5da76c..d52497d 100644
--- a/webrtc.gni
+++ b/webrtc.gni
@@ -727,10 +727,7 @@
 # This template automatically switches the target type between source_set
 # and static_library.
 #
-# This should be the default target type for all the WebRTC targets with
-# one exception. Do not use this template for header only targets, in that case
-# rtc_source_set must be used in order to avoid build errors (e.g. libtool
-# complains if the output .a file is empty).
+# This should be the default target type for all the WebRTC targets.
 #
 # How does it work:
 # Since all files in a source_set are linked into a final binary, while files
@@ -742,7 +739,23 @@
 # For the same reason, testonly targets will always be expanded to
 # source_set in order to be sure that tests are present in the test binary.
 template("rtc_library") {
-  if (is_component_build || (defined(invoker.testonly) && invoker.testonly)) {
+  header_only = true
+  if (defined(invoker.sources)) {
+    non_header_sources = filter_exclude(invoker.sources,
+                                        [
+                                          "*.h",
+                                          "*.hh",
+                                          "*.inc",
+                                        ])
+    if (non_header_sources != []) {
+      header_only = false
+    }
+  }
+
+  # Header only libraries should use source_set as a static_library with no
+  # source files will cause issues with macOS libtool.
+  if (header_only || is_component_build ||
+      (defined(invoker.testonly) && invoker.testonly)) {
     target_type = "source_set"
   } else {
     target_type = "static_library"