Windows: add WIN32_LEAN_AND_MEAN everywhere

Some projects need to include our C++ code and if
they need to build with WIN32_LEAN_AND_MEAN then
Bazel's C++ code should also compile with it.

Closes #8416.

PiperOrigin-RevId: 249226341
diff --git a/src/main/cpp/blaze_util_windows.cc b/src/main/cpp/blaze_util_windows.cc
index fe13876..9a919eb 100644
--- a/src/main/cpp/blaze_util_windows.cc
+++ b/src/main/cpp/blaze_util_windows.cc
@@ -12,8 +12,6 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
-#include "src/main/cpp/blaze_util_platform.h"
-
 #ifndef WIN32_LEAN_AND_MEAN
 #define WIN32_LEAN_AND_MEAN
 #endif
@@ -26,7 +24,6 @@
 #include <objbase.h>         // CoTaskMemFree
 #include <shlobj.h>          // SHGetKnownFolderPath
 #include <stdarg.h>          // va_start, va_end, va_list
-#include <versionhelpers.h>  // IsWindows8OrGreater
 
 #include <algorithm>
 #include <cstdio>
@@ -40,6 +37,7 @@
 #include <vector>
 
 #include "src/main/cpp/blaze_util.h"
+#include "src/main/cpp/blaze_util_platform.h"
 #include "src/main/cpp/global_variables.h"
 #include "src/main/cpp/startup_options.h"
 #include "src/main/cpp/util/errors.h"
@@ -767,12 +765,6 @@
   return processInfo.dwProcessId;
 }
 
-// Returns whether nested jobs are not available on the current system.
-static bool NestedJobsSupported() {
-  // Nested jobs are supported from Windows 8
-  return IsWindows8OrGreater();
-}
-
 // Run the given program in the current working directory, using the given
 // argument vector, wait for it to finish, then exit ourselves with the exitcode
 // of that program.
diff --git a/src/main/cpp/util/errors_windows.cc b/src/main/cpp/util/errors_windows.cc
index 292bb8a..b7e0148 100644
--- a/src/main/cpp/util/errors_windows.cc
+++ b/src/main/cpp/util/errors_windows.cc
@@ -12,9 +12,14 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
+#ifndef WIN32_LEAN_AND_MEAN
+#define WIN32_LEAN_AND_MEAN
+#endif
 #include <windows.h>
+
 #include <sstream>
 #include <string>
+
 #include "src/main/cpp/util/errors.h"
 
 namespace blaze_util {
diff --git a/src/main/cpp/util/file_windows.cc b/src/main/cpp/util/file_windows.cc
index cdcfa54..b47e222 100644
--- a/src/main/cpp/util/file_windows.cc
+++ b/src/main/cpp/util/file_windows.cc
@@ -11,8 +11,11 @@
 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 // See the License for the specific language governing permissions and
 // limitations under the License.
-#include <ctype.h>  // isalpha
-#include <wchar.h>  // wcslen
+#ifndef WIN32_LEAN_AND_MEAN
+#define WIN32_LEAN_AND_MEAN
+#endif
+#include <ctype.h>   // isalpha
+#include <wchar.h>   // wcslen
 #include <wctype.h>  // iswalpha
 #include <windows.h>
 
diff --git a/src/main/cpp/util/path_windows.cc b/src/main/cpp/util/path_windows.cc
index 80deae6..dad839d 100644
--- a/src/main/cpp/util/path_windows.cc
+++ b/src/main/cpp/util/path_windows.cc
@@ -12,6 +12,10 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
+#ifndef WIN32_LEAN_AND_MEAN
+#define WIN32_LEAN_AND_MEAN
+#endif
+
 #include "src/main/cpp/util/path_platform.h"
 
 #include <assert.h>
diff --git a/src/main/cpp/util/profiler_windows.cc b/src/main/cpp/util/profiler_windows.cc
index c57a94b..d4d702f 100644
--- a/src/main/cpp/util/profiler_windows.cc
+++ b/src/main/cpp/util/profiler_windows.cc
@@ -12,6 +12,10 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
+#ifndef WIN32_LEAN_AND_MEAN
+#define WIN32_LEAN_AND_MEAN
+#endif
+
 #include "src/main/cpp/util/profiler.h"
 
 #include <windows.h>
diff --git a/src/main/cpp/util/strings.cc b/src/main/cpp/util/strings.cc
index d46ddc3..b5cca4e 100644
--- a/src/main/cpp/util/strings.cc
+++ b/src/main/cpp/util/strings.cc
@@ -11,6 +11,13 @@
 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 // See the License for the specific language governing permissions and
 // limitations under the License.
+
+#if defined(_WIN32) || defined(__CYGWIN__)
+#ifndef WIN32_LEAN_AND_MEAN
+#define WIN32_LEAN_AND_MEAN
+#endif
+#endif  // defined(_WIN32) || defined(__CYGWIN__)
+
 #include "src/main/cpp/util/strings.h"
 
 #if defined(_WIN32) || defined(__CYGWIN__)
diff --git a/src/main/native/windows/file.h b/src/main/native/windows/file.h
index 0e0c415..8063a4c 100644
--- a/src/main/native/windows/file.h
+++ b/src/main/native/windows/file.h
@@ -14,6 +14,10 @@
 #ifndef BAZEL_SRC_MAIN_NATIVE_WINDOWS_FILE_H_
 #define BAZEL_SRC_MAIN_NATIVE_WINDOWS_FILE_H_
 
+#ifndef WIN32_LEAN_AND_MEAN
+#define WIN32_LEAN_AND_MEAN
+#endif
+
 #include <windows.h>
 
 #include <memory>
diff --git a/src/main/native/windows/process.cc b/src/main/native/windows/process.cc
index 4d61a39..05bb0f4 100644
--- a/src/main/native/windows/process.cc
+++ b/src/main/native/windows/process.cc
@@ -12,8 +12,13 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
+#ifndef WIN32_LEAN_AND_MEAN
+#define WIN32_LEAN_AND_MEAN
+#endif
+
 #include "src/main/native/windows/process.h"
 
+#include <windows.h>
 #include <VersionHelpers.h>
 
 #include <memory>
diff --git a/src/main/native/windows/util.cc b/src/main/native/windows/util.cc
index ea3a59b..0aac172 100644
--- a/src/main/native/windows/util.cc
+++ b/src/main/native/windows/util.cc
@@ -12,6 +12,10 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
+#ifndef WIN32_LEAN_AND_MEAN
+#define WIN32_LEAN_AND_MEAN
+#endif
+
 #include "src/main/native/windows/util.h"
 
 #include <stdio.h>
diff --git a/src/main/native/windows/util.h b/src/main/native/windows/util.h
index f5bf048..2380314 100644
--- a/src/main/native/windows/util.h
+++ b/src/main/native/windows/util.h
@@ -15,6 +15,10 @@
 #ifndef BAZEL_SRC_MAIN_NATIVE_WINDOWS_UTIL_H__
 #define BAZEL_SRC_MAIN_NATIVE_WINDOWS_UTIL_H__
 
+#ifndef WIN32_LEAN_AND_MEAN
+#define WIN32_LEAN_AND_MEAN
+#endif
+
 #include <windows.h>
 
 #include <memory>
diff --git a/src/main/tools/build-runfiles-windows.cc b/src/main/tools/build-runfiles-windows.cc
index be7cfd4..297116d 100644
--- a/src/main/tools/build-runfiles-windows.cc
+++ b/src/main/tools/build-runfiles-windows.cc
@@ -12,8 +12,13 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
-#include <string.h>
+#ifndef WIN32_LEAN_AND_MEAN
+#define WIN32_LEAN_AND_MEAN
+#endif
 #include <windows.h>
+
+#include <string.h>
+
 #include <fstream>
 #include <iostream>
 #include <sstream>
diff --git a/src/test/cpp/blaze_util_windows_test.cc b/src/test/cpp/blaze_util_windows_test.cc
index 47d0aa9..bee1e05 100644
--- a/src/test/cpp/blaze_util_windows_test.cc
+++ b/src/test/cpp/blaze_util_windows_test.cc
@@ -12,6 +12,9 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
+#ifndef WIN32_LEAN_AND_MEAN
+#define WIN32_LEAN_AND_MEAN
+#endif
 #include <windows.h>
 
 #include <algorithm>
diff --git a/src/test/cpp/util/file_windows_test.cc b/src/test/cpp/util/file_windows_test.cc
index 9f23445..a86d664 100644
--- a/src/test/cpp/util/file_windows_test.cc
+++ b/src/test/cpp/util/file_windows_test.cc
@@ -11,9 +11,14 @@
 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 // See the License for the specific language governing permissions and
 // limitations under the License.
+
+#ifndef WIN32_LEAN_AND_MEAN
+#define WIN32_LEAN_AND_MEAN
+#endif
+#include <windows.h>
+
 #include <stdio.h>
 #include <string.h>
-#include <windows.h>
 
 #include <algorithm>
 #include <memory>
diff --git a/src/test/cpp/util/path_windows_test.cc b/src/test/cpp/util/path_windows_test.cc
index fa30e39..1da465b 100644
--- a/src/test/cpp/util/path_windows_test.cc
+++ b/src/test/cpp/util/path_windows_test.cc
@@ -11,9 +11,14 @@
 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 // See the License for the specific language governing permissions and
 // limitations under the License.
+
+#ifndef WIN32_LEAN_AND_MEAN
+#define WIN32_LEAN_AND_MEAN
+#endif
+#include <windows.h>
+
 #include <stdio.h>
 #include <string.h>
-#include <windows.h>
 
 #include <algorithm>
 #include <memory>
diff --git a/src/test/cpp/util/windows_test_util.cc b/src/test/cpp/util/windows_test_util.cc
index 8b78d86..c4e0259 100644
--- a/src/test/cpp/util/windows_test_util.cc
+++ b/src/test/cpp/util/windows_test_util.cc
@@ -11,6 +11,10 @@
 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 // See the License for the specific language governing permissions and
 // limitations under the License.
+
+#ifndef WIN32_LEAN_AND_MEAN
+#define WIN32_LEAN_AND_MEAN
+#endif
 #include <windows.h>
 
 #include <algorithm>
diff --git a/src/test/cpp/util/windows_test_util_test.cc b/src/test/cpp/util/windows_test_util_test.cc
index 7ee97f1..d8c9503 100644
--- a/src/test/cpp/util/windows_test_util_test.cc
+++ b/src/test/cpp/util/windows_test_util_test.cc
@@ -11,6 +11,10 @@
 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 // See the License for the specific language governing permissions and
 // limitations under the License.
+
+#ifndef WIN32_LEAN_AND_MEAN
+#define WIN32_LEAN_AND_MEAN
+#endif
 #include <windows.h>
 
 #include <string>
diff --git a/src/test/native/windows/file_test.cc b/src/test/native/windows/file_test.cc
index c4a9022..08d2675 100644
--- a/src/test/native/windows/file_test.cc
+++ b/src/test/native/windows/file_test.cc
@@ -11,6 +11,13 @@
 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 // See the License for the specific language governing permissions and
 // limitations under the License.
+
+#ifndef WIN32_LEAN_AND_MEAN
+#define WIN32_LEAN_AND_MEAN
+#endif
+
+#include "src/main/native/windows/file.h"
+
 #include <stdlib.h>
 #include <string.h>
 #include <windows.h>
@@ -20,7 +27,6 @@
 #include <string>
 
 #include "gtest/gtest.h"
-#include "src/main/native/windows/file.h"
 #include "src/test/cpp/util/windows_test_util.h"
 
 #if !defined(_WIN32) && !defined(__CYGWIN__)
diff --git a/src/test/native/windows/util_test.cc b/src/test/native/windows/util_test.cc
index 295729f..9f6a3ca 100644
--- a/src/test/native/windows/util_test.cc
+++ b/src/test/native/windows/util_test.cc
@@ -11,6 +11,11 @@
 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 // See the License for the specific language governing permissions and
 // limitations under the License.
+#ifndef WIN32_LEAN_AND_MEAN
+#define WIN32_LEAN_AND_MEAN
+#endif
+#include "src/main/native/windows/util.h"
+
 #include <stdlib.h>
 #include <string.h>
 #include <windows.h>
@@ -21,7 +26,6 @@
 #include <string>
 
 #include "gtest/gtest.h"
-#include "src/main/native/windows/util.h"
 
 #if !defined(_WIN32) && !defined(__CYGWIN__)
 #error("This test should only be run on Windows")
diff --git a/src/test/py/bazel/testdata/runfiles_test/foo/foo.cc b/src/test/py/bazel/testdata/runfiles_test/foo/foo.cc
index 07ec268..ef87e50 100644
--- a/src/test/py/bazel/testdata/runfiles_test/foo/foo.cc
+++ b/src/test/py/bazel/testdata/runfiles_test/foo/foo.cc
@@ -14,6 +14,12 @@
 
 // Mock C++ binary, only used in tests.
 
+#ifdef IS_WINDOWS
+#ifndef WIN32_LEAN_AND_MEAN
+#define WIN32_LEAN_AND_MEAN
+#endif
+#endif  // IS_WINDOWS
+
 #include "tools/cpp/runfiles/runfiles.h"
 
 #ifdef IS_WINDOWS
diff --git a/src/tools/launcher/launcher.cc b/src/tools/launcher/launcher.cc
index f666657..67f1333 100644
--- a/src/tools/launcher/launcher.cc
+++ b/src/tools/launcher/launcher.cc
@@ -12,7 +12,13 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
+#ifndef WIN32_LEAN_AND_MEAN
+#define WIN32_LEAN_AND_MEAN
+#endif
+#include "src/tools/launcher/launcher.h"
+
 #include <windows.h>
+
 #include <algorithm>
 #include <fstream>
 #include <iostream>
@@ -22,7 +28,6 @@
 
 #include "src/main/cpp/util/path_platform.h"
 #include "src/main/cpp/util/strings.h"
-#include "src/tools/launcher/launcher.h"
 #include "src/tools/launcher/util/data_parser.h"
 #include "src/tools/launcher/util/launcher_util.h"
 
diff --git a/src/tools/launcher/util/launcher_util.cc b/src/tools/launcher/util/launcher_util.cc
index a5f86d6..ce4f549 100644
--- a/src/tools/launcher/util/launcher_util.cc
+++ b/src/tools/launcher/util/launcher_util.cc
@@ -12,6 +12,11 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
+#ifndef WIN32_LEAN_AND_MEAN
+#define WIN32_LEAN_AND_MEAN
+#endif
+#include <windows.h>
+
 // For rand_s function, https://msdn.microsoft.com/en-us/library/sxtz2fa8.aspx
 #define _CRT_RAND_S
 #include <fcntl.h>
@@ -20,7 +25,6 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <windows.h>
 #include <algorithm>
 #include <sstream>
 #include <string>
diff --git a/src/tools/launcher/util/launcher_util_test.cc b/src/tools/launcher/util/launcher_util_test.cc
index 86beaca..efc6666 100644
--- a/src/tools/launcher/util/launcher_util_test.cc
+++ b/src/tools/launcher/util/launcher_util_test.cc
@@ -12,7 +12,13 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
+#ifndef WIN32_LEAN_AND_MEAN
+#define WIN32_LEAN_AND_MEAN
+#endif
+#include "src/tools/launcher/util/launcher_util.h"
+
 #include <windows.h>
+
 #include <cstdlib>
 #include <fstream>
 #include <iostream>
@@ -24,7 +30,6 @@
 #include "src/main/cpp/util/path_platform.h"
 #include "src/main/cpp/util/strings.h"
 #include "src/main/native/windows/util.h"
-#include "src/tools/launcher/util/launcher_util.h"
 #include "tools/cpp/runfiles/runfiles.h"
 
 namespace bazel {
diff --git a/src/tools/singlejar/diag.h b/src/tools/singlejar/diag.h
index b659f4a..c4ac36b 100644
--- a/src/tools/singlejar/diag.h
+++ b/src/tools/singlejar/diag.h
@@ -29,9 +29,13 @@
 
 #elif defined(_WIN32)
 
+#ifndef WIN32_LEAN_AND_MEAN
+#define WIN32_LEAN_AND_MEAN
+#endif
+#include <windows.h>
+
 #include <stdio.h>
 #include <string.h>
-#include <windows.h>
 #define _diag_msg(prefix, msg, ...) \
   { fprintf(stderr, prefix msg "\n", __VA_ARGS__); }
 #define _diag_msgx(exit_value, prefix, msg, ...) \