Fix link error for windows release builds. https://ci.chromium.org/ui/p/gn/builders/try.shadow/win/b8674262571164089329/overview Change-Id: I7759b7f77491ac7195a6aa8bc4405d496a6a6964 Reviewed-on: https://gn-review.googlesource.com/c/gn/+/24960 Reviewed-by: Takuto Ikuta <tikuta@google.com> Commit-Queue: Matt Stark <msta@google.com>
diff --git a/build/gen.py b/build/gen.py index bf4e5db..0c5630f 100755 --- a/build/gen.py +++ b/build/gen.py
@@ -409,7 +409,7 @@ os.path.dirname(path)))), ' includes = %s' % ' '.join( ['-I' + escape_path_ninja(dirname) for dirname in include_dirs]), - ' cflags = %s' % ' '.join(cflags), + ' cflags = %s' % ' '.join(settings.get('cflags', cflags)), ]) for library, settings in static_libraries.items(): @@ -757,7 +757,13 @@ 'sources': [ 'src/gn/string_atom.cc', 'src/gn/ffi/intern_string.cc', - ] + ], + # Strip /GL from string_atom. + # The Rust types crate references intern_string via a raw extern "C" + # declaration. If compiled with /GL, the MSVC linker discards this + # symbol during the LTCG phase because it has no references from + # C++ /GL objects, causing LNK2001 link errors in Rust test binaries. + 'cflags': [f for f in cflags if f != '/GL'] }, 'gn_lib': { 'sources': [
diff --git a/build/run_cargo.py b/build/run_cargo.py index a5ba52a..2faadff 100755 --- a/build/run_cargo.py +++ b/build/run_cargo.py
@@ -203,6 +203,14 @@ out_path = Path(out_path_str) cargo_out_dir = Path(cargo_out_dir_str) + if sys.platform == 'win32': + # Strip /GL from Cargo C++ dependencies (like the cxx crate). + # Rust code compiles to standard COFF objects. If C++ FFI shims generated by Cargo + # are compiled with /GL, the MSVC linker (running with /LTCG) fails to match the + # standard COFF Rust references to these C++ symbols during the LTCG phase, + # causing it to discard the shims as unused and leading to LNK2001 link errors. + cxxflags = cxxflags.replace('/GL', '') + os.environ['CXX'] = cxx os.environ['CXXFLAGS'] = cxxflags # Since Ninja runs commands from the build output directory, CWD is the