Add an apply-include-cleaner tool

Since iwyu is now deprecated, we need to enable use of include-cleaner.
This approach gives some error messages when running, but does the job.

Bug: webrtc:15874
Change-Id: I431deef0f2e5ce99eb256a4d82aa32769ae58b41
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/347642
Commit-Queue: Harald Alvestrand <hta@webrtc.org>
Reviewed-by: Mirko Bonadei <mbonadei@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#42085}
diff --git a/api/BUILD.gn b/api/BUILD.gn
index c6ef19a..709d242 100644
--- a/api/BUILD.gn
+++ b/api/BUILD.gn
@@ -76,6 +76,7 @@
     ]
     deps = [
       ":enable_media",
+      ":field_trials_view",
       ":libjingle_peerconnection_api",
       ":scoped_refptr",
       "../api/rtc_event_log:rtc_event_log_factory",
@@ -84,6 +85,7 @@
       "../pc:peer_connection_factory",
       "../pc:webrtc_sdp",
       "../rtc_base:threading",
+      "../rtc_base/system:rtc_export",
       "../stats:rtc_stats",
       "audio:audio_mixer_api",
       "audio_codecs:audio_codecs_api",
diff --git a/api/create_peerconnection_factory.h b/api/create_peerconnection_factory.h
index d829bc1..6ca4cb0 100644
--- a/api/create_peerconnection_factory.h
+++ b/api/create_peerconnection_factory.h
@@ -16,10 +16,12 @@
 #include "api/audio/audio_mixer.h"
 #include "api/audio_codecs/audio_decoder_factory.h"
 #include "api/audio_codecs/audio_encoder_factory.h"
+#include "api/field_trials_view.h"
 #include "api/peer_connection_interface.h"
 #include "api/scoped_refptr.h"
 #include "api/video_codecs/video_decoder_factory.h"
 #include "api/video_codecs/video_encoder_factory.h"
+#include "rtc_base/system/rtc_export.h"
 
 namespace rtc {
 // TODO(bugs.webrtc.org/9987): Move rtc::Thread to api/ or expose a better
diff --git a/tools_webrtc/iwyu/apply-include-cleaner b/tools_webrtc/iwyu/apply-include-cleaner
new file mode 100755
index 0000000..2ba11b7
--- /dev/null
+++ b/tools_webrtc/iwyu/apply-include-cleaner
@@ -0,0 +1,84 @@
+#!/usr/bin/env bash
+#
+# Run the include-cleaner tool (iwyu replacement) on a file in the webrtc source
+# directory.
+#
+#
+# In order to handle include paths correctly, you need to provide
+# a compile DB (aka compile_commands.json).
+# You can create it in one of the following ways:
+# "gn gen --export-compile-commands path/to/out"
+# "tools/clang/scripts/generate_compdb.py -p path/to/out > compile_commands.json"
+# If "out/Default" exists, the script will attempt to generate it for you.
+#
+# To get include-cleaner:
+# - in google3: define an alias and/or point to it below
+# - in debian distros: sudo apt install clang-tools-17 (or later, and fix below)
+# Ignore errors of the type "this argument doesn't exist".
+
+CLEANER=/usr/bin/clang-include-cleaner-17
+
+# Debug level, also controlled by the "-d" argument.
+# Set this to 1 to get more debug information.
+# Set this to 2 to also get a dump of the iwyu tool output.
+DEBUG=0
+
+set -e
+if [ $DEBUG -gt 0 ]; then
+  set -x
+fi
+
+error() {
+  echo "$*" >&2
+  exit 1
+}
+
+WORKDIR=out/Default
+
+usage() {
+  echo "Usage: $0 [ -c compile-commands-file.json ] [-r] file.cc"
+  echo "Runs the include-cleaner tool on a file"
+  echo "Arguments:"
+  echo " -n : just print changes, don't do them"
+  echo " -r : Remove non-required includes from .h file"
+  echo " -d <n> : Set debug level to <n>"
+  echo " -h : Print this help message"
+}
+
+COMMAND=" --edit"
+INCLUDE_ARGS=""
+
+while getopts 'd:rnh' opts; do
+  case "${opts}" in
+    n) COMMAND=" --print=changes" ;;
+    r) INCLUDE_ARGS=" --remove" ;;
+    d) DEBUG=${OPTARG};if [ $DEBUG -gt 0 ]; then set -x; fi  ;;
+    h) usage; exit 1 ;;
+    *) error "Unexpected option ${opts}" ;;
+  esac
+done
+shift $(expr $OPTIND - 1 )
+
+if [[ -z "$COMPILE_COMMANDS" ]]; then
+  if [ -d "$WORKDIR" ]; then
+    if [ ! -f "$WORKDIR/compile_commands.json" ]; then
+      echo "Generating compile commands file"
+      gn gen --export-compile-commands $WORKDIR
+    fi
+    COMPILE_COMMANDS="$WORKDIR/compile_commands.json"
+  else
+    error "Could not generate $WORKDIR/compile_commands.json."
+  fi
+fi
+
+FILE="$1"
+
+if [ ! -f $FILE ]; then
+  error "File $FILE is not found"
+fi
+
+cd $WORKDIR
+$CLEANER $INCLUDE_ARGS $COMMAND ../../$FILE
+
+echo "Finished. Check diff, compile, gn gen --check and git cl format"
+echo "before uploading."