Skip to content

Commit

Permalink
vs: Manually link generated .o files
Browse files Browse the repository at this point in the history
Fixes #12550 .

VS automatically links CustomBuild outputs ending in .obj or .res,
but others need to be included explicitly.
  • Loading branch information
arch1t3cht authored and eli-schwartz committed Dec 11, 2023
1 parent 17c6d5e commit af04643
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 17 deletions.
21 changes: 6 additions & 15 deletions mesonbuild/backend/vs2010backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -894,18 +894,6 @@ def add_include_dirs(self, lang, parent_node, file_inc_dirs):
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
Expand Down Expand Up @@ -1779,17 +1767,20 @@ def path_normalize_add(path, lis):
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)
Expand Down
10 changes: 9 additions & 1 deletion test cases/common/52 object generator/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -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)
3 changes: 2 additions & 1 deletion test cases/common/52 object generator/prog.c
Original file line number Diff line number Diff line change
@@ -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();
}
3 changes: 3 additions & 0 deletions test cases/common/52 object generator/source4.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
int func4_in_obj(void) {
return 0;
}

0 comments on commit af04643

Please sign in to comment.