Improve CMake installation tests (#10615)

* Using glob to remove headers instead of cyclic file_lists

* Simplify CMake config and include missing files

* Don't remove generated proto headers

* Fix broken CMake proto dependencies instead of opting out pb.h files.

* Fixing cyclic dependency
diff --git a/cmake/tests.cmake b/cmake/tests.cmake
index a9fdd52..530a66b 100644
--- a/cmake/tests.cmake
+++ b/cmake/tests.cmake
@@ -43,19 +43,20 @@
 include(${protobuf_SOURCE_DIR}/src/file_lists.cmake)
 
 set(lite_test_protos
-  ${protobuf_lite_test_protos_proto_srcs}
+  ${protobuf_lite_test_protos_files}
 )
 
 set(tests_protos
-  ${protobuf_test_protos_proto_srcs}
+  ${protobuf_test_protos_files}
   ${compiler_test_protos_files}
   ${util_test_protos_files}
 )
 
 macro(compile_proto_file filename)
-  string(REPLACE .proto .pb.cc pb_file ${filename})
+  string(REPLACE .proto .pb.h pb_hdr ${filename})
+  string(REPLACE .proto .pb.cc pb_src ${filename})
   add_custom_command(
-    OUTPUT ${pb_file}
+    OUTPUT ${pb_hdr} ${pb_src}
     DEPENDS ${protobuf_PROTOC_EXE} ${filename}
     COMMAND ${protobuf_PROTOC_EXE} ${filename}
         --proto_path=${protobuf_SOURCE_DIR}/src
@@ -67,13 +68,13 @@
 set(lite_test_proto_files)
 foreach(proto_file ${lite_test_protos})
   compile_proto_file(${proto_file})
-  set(lite_test_proto_files ${lite_test_proto_files} ${pb_file})
+  set(lite_test_proto_files ${lite_test_proto_files} ${pb_src} ${pb_hdr})
 endforeach(proto_file)
 
 set(tests_proto_files)
 foreach(proto_file ${tests_protos})
   compile_proto_file(${proto_file})
-  set(tests_proto_files ${tests_proto_files} ${pb_file})
+  set(tests_proto_files ${tests_proto_files} ${pb_src} ${pb_hdr})
 endforeach(proto_file)
 
 add_library(protobuf-lite-test-common STATIC
@@ -85,8 +86,8 @@
 set(common_test_files
   ${test_util_hdrs}
   ${test_util_srcs}
-  ${mock_code_generator_srcs}
-  ${testing_srcs}
+  ${common_test_hdrs}
+  ${common_test_srcs}
 )
 
 add_library(protobuf-test-common STATIC
@@ -138,8 +139,8 @@
 
 set(test_plugin_files
   ${test_plugin_files}
-  ${mock_code_generator_srcs}
-  ${testing_srcs}
+  ${common_test_hdrs}
+  ${common_test_srcs}
 )
 
 add_executable(test_plugin ${test_plugin_files})
@@ -172,13 +173,23 @@
 add_custom_target(remove-installed-headers)
 add_custom_target(restore-installed-headers)
 
-# Explicitly skip the bootstrapping headers as it's directly used in tests
-set(_installed_hdrs ${libprotobuf_hdrs} ${libprotoc_hdrs})
-list(REMOVE_ITEM _installed_hdrs
+file(GLOB_RECURSE _local_hdrs
+  "${PROJECT_SOURCE_DIR}/src/*.h"
+  "${PROJECT_SOURCE_DIR}/src/*.inc")
+
+# Exclude the bootstrapping that are directly used by tests.
+set(_exclude_hdrs
   "${protobuf_SOURCE_DIR}/src/google/protobuf/descriptor.pb.h"
   "${protobuf_SOURCE_DIR}/src/google/protobuf/compiler/plugin.pb.h")
 
-foreach(_hdr ${_installed_hdrs})
+# Exclude test library headers.
+list(APPEND _exclude_hdrs ${test_util_hdrs} ${lite_test_util_hdrs} ${common_test_hdrs}
+  ${compiler_test_utils_hdrs})
+foreach(_hdr ${_exclude_hdrs})
+  list(REMOVE_ITEM _local_hdrs ${_hdr})
+endforeach()
+
+foreach(_hdr ${_local_hdrs})
   string(REPLACE "${protobuf_SOURCE_DIR}/src" "" _file ${_hdr})
   set(_tmp_file "${CMAKE_BINARY_DIR}/tmp-install-test/${_file}")
   add_custom_command(TARGET remove-installed-headers PRE_BUILD
@@ -193,6 +204,6 @@
 
 add_dependencies(remove-installed-headers save-installed-headers)
 if(protobuf_REMOVE_INSTALLED_HEADERS)
-  add_dependencies(protobuf-lite-test-common remove-installed-headers)
-  add_dependencies(protobuf-test-common remove-installed-headers)
+  # Make sure we remove all the headers *before* any codegen occurs.
+  add_dependencies(${protobuf_PROTOC_EXE} remove-installed-headers)
 endif()