From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Anonymous <anonymous@electronjs.org>
Date: Wed, 19 Sep 2018 18:55:58 -0700
Subject: build_toolchain_win_patch.patch

Patch the Windows build toolchain to generate unique PDB names

When the PDB files generated by the `static_library` build are
packaged for distribution they are all copied to a single folder,
some of the PDB files have identical names so they end up
overwriting each other. The missing PDB files cause linker warnings
when building Electron in Release mode, and make it more difficult
to debug release builds.

This patch modifies the PDB naming convention for the
`static_library` build configuration to ensure PDB names are unique.
For example, instead of generating `obj/ui/base/base_cc.pdb` the
build will now generate `obj/ui/base/obj_ui_base_base_cc.pdb`.

diff --git a/build/toolchain/win/BUILD.gn b/build/toolchain/win/BUILD.gn
index eb3e2b2b377dde31e062be46837bf509ecab0325..fdffcdbdbbfe1280f50c5b8401b117e24ea5267f 100644
--- a/build/toolchain/win/BUILD.gn
+++ b/build/toolchain/win/BUILD.gn
@@ -173,6 +173,12 @@ template("msvc_toolchain") {
       ]
 
       command = "$env_wrapper$cl /nologo /showIncludes ${clflags} $sys_include_flags{{defines}} {{include_dirs}} {{cflags}} {{cflags_c}} /c {{source}} /Fo{{output}} /Fd\"$pdbname\""
+
+      if (is_electron_build && !is_component_build) {
+        pdbdir = "{{target_out_dir}}"
+        pdbname = "{{label_name}}_c.pdb"
+        command = "$python_path $tool_wrapper_path cl-wrapper $env_wrapper$cl /nologo /showIncludes ${clflags} $sys_include_flags{{defines}} {{include_dirs}} {{cflags}} {{cflags_c}} /c {{source}} /Fo{{output}} $pdbdir \"$pdbname\""
+      }
     }
 
     tool("cxx") {
@@ -189,6 +195,12 @@ template("msvc_toolchain") {
       ]
 
       command = "$env_wrapper$cl /nologo /showIncludes ${clflags} $sys_include_flags{{defines}} {{include_dirs}} {{cflags}} {{cflags_cc}} /c {{source}} /Fo{{output}} /Fd\"$pdbname\""
+
+      if (is_electron_build && !is_component_build) {
+        pdbdir = "{{target_out_dir}}"
+        pdbname = "{{label_name}}_cc.pdb"
+        command = "$python_path $tool_wrapper_path cl-wrapper $env_wrapper$cl /nologo /showIncludes ${clflags} $sys_include_flags{{defines}} {{include_dirs}} {{cflags}} {{cflags_cc}} /c {{source}} /Fo{{output}} $pdbdir \"$pdbname\""
+      }
     }
 
     tool("rc") {
diff --git a/build/toolchain/win/tool_wrapper.py b/build/toolchain/win/tool_wrapper.py
index cb0393ecd507b865169e9d7c3037d7d5523ae30e..ee21eb4b194b35576883b31c94c9a1f56075e89b 100644
--- a/build/toolchain/win/tool_wrapper.py
+++ b/build/toolchain/win/tool_wrapper.py
@@ -247,6 +247,25 @@ class WinTool(object):
     dirname = dirname[0] if dirname else None
     return subprocess.call(args, shell=True, env=env, cwd=dirname)
 
+  def ExecClWrapper(self, *args):
+    """Invokes cl.exe to compile a C/C++ source file."""
+    args = list(args)
+    # Incorporate the PDB output dir into the PDB name to ensure the PDB name
+    # is unique (see https://github.com/electron/libchromiumcontent/issues/287)
+    pdb_name = args.pop()
+    pdb_dir = args.pop()
+    pdb_filename = '%s/%s_%s' % (pdb_dir, pdb_dir.replace('/', '_'), pdb_name)
+    # On Windows when args is a sequence instead of a single string
+    # subprocess.call() will use subprocess.list2cmdline() to convert the
+    # sequence to a string. Unfortunately the double-quote escaping done by
+    # subprocess.list2cmdline() mangles the /Fd"path/to/some.pdb" arg to
+    # /Fd\"path/to/some.pdb\", and cl.exe then fails to parse the PDB filename
+    # correctly. To work around this we use subprocess.list2cmdline()
+    # (even though it's not part of the public API) to construct most of the
+    # command line, and then append the /Fd flag.
+    pdb_flag = '/Fd"%s"' % pdb_filename
+    cmdline = '%s %s' % (subprocess.list2cmdline(args), pdb_flag)
+    return subprocess.call(cmdline, shell=False)
 
 if __name__ == '__main__':
   sys.exit(main(sys.argv[1:]))
