vs: Manually link generated .o files

Fixes #12550 .

VS automatically links CustomBuild outputs ending in .obj or .res,
but others need to be included explicitly.
pull/12618/head
arch1t3cht 12 months ago committed by Eli Schwartz
parent 17c6d5eb47
commit af0464352a
  1. 21
      mesonbuild/backend/vs2010backend.py
  2. 10
      test cases/common/52 object generator/meson.build
  3. 3
      test cases/common/52 object generator/prog.c
  4. 3
      test cases/common/52 object generator/source4.c

@ -894,18 +894,6 @@ class Vs2010Backend(backends.Backend):
dirs = file_inc_dirs[lang]
ET.SubElement(parent_node, "AdditionalIncludeDirectories").text = ';'.join(dirs)
@staticmethod
def has_objects(objects, additional_objects, generated_objects):
# Ignore generated objects, those are automatically used by MSBuild because they are part of
# the CustomBuild Outputs.
return len(objects) + len(additional_objects) > 0
@staticmethod
def add_generated_objects(node, generated_objects):
# Do not add generated objects to project file. Those are automatically used by MSBuild, because
# they are part of the CustomBuild Outputs.
return
@staticmethod
def escape_preprocessor_define(define: str) -> str:
# See: https://msdn.microsoft.com/en-us/library/bb383819.aspx
@ -1779,17 +1767,20 @@ class Vs2010Backend(backends.Backend):
for o in custom_objs:
additional_objects.append(o)
# VS automatically links CustomBuild outputs whose name ends in .obj or .res,
# but the others need to be included explicitly
explicit_link_gen_objs = [obj for obj in gen_objs if not obj.endswith(('.obj', '.res'))]
previous_objects = []
if self.has_objects(objects, additional_objects, gen_objs):
if len(objects) + len(additional_objects) + len(explicit_link_gen_objs) > 0:
inc_objs = ET.SubElement(root, 'ItemGroup')
for s in objects:
relpath = os.path.join(proj_to_build_root, s.rel_to_builddir(self.build_to_src))
if path_normalize_add(relpath, previous_objects):
ET.SubElement(inc_objs, 'Object', Include=relpath)
for s in additional_objects:
for s in additional_objects + explicit_link_gen_objs:
if path_normalize_add(s, previous_objects):
ET.SubElement(inc_objs, 'Object', Include=s)
self.add_generated_objects(inc_objs, gen_objs)
ET.SubElement(root, 'Import', Project=r'$(VCTargetsPath)\Microsoft.Cpp.targets')
self.add_regen_dependency(root)

@ -29,6 +29,14 @@ gen2 = generator(python,
arguments : [comp, cc, '@INPUT@', '@OUTPUT0@'])
generated2 = gen2.process(['source3.c'])
e = executable('prog', 'prog.c', generated, generated2)
# Generate an object file ending with .o even on Windows.
# The VS backend needs to handle .o objects differently from .obj objects.
gen3 = generator(python,
output : '@BASENAME@.o',
arguments : [comp, cc, '@INPUT@', '@OUTPUT@'])
generated3 = gen3.process(['source4.c'])
e = executable('prog', 'prog.c', generated, generated2, generated3)
test('objgen', e)

@ -1,7 +1,8 @@
int func1_in_obj(void);
int func2_in_obj(void);
int func3_in_obj(void);
int func4_in_obj(void);
int main(void) {
return func1_in_obj() + func2_in_obj() + func3_in_obj();
return func1_in_obj() + func2_in_obj() + func3_in_obj() + func4_in_obj();
}

@ -0,0 +1,3 @@
int func4_in_obj(void) {
return 0;
}
Loading…
Cancel
Save