1#!/usr/bin/env python
2
3# Copyright (c) 2013 Google Inc. All rights reserved.
4# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
6
7"""
8Make sure the link order of object files is the same between msvs and ninja.
9"""
10
11import TestGyp
12
13import sys
14
15if sys.platform == 'win32':
16  test = TestGyp.TestGyp(formats=['msvs', 'ninja'])
17
18  CHDIR = 'linker-flags'
19  test.run_gyp('link-ordering.gyp', chdir=CHDIR)
20  test.build('link-ordering.gyp', test.ALL, chdir=CHDIR)
21
22  def GetDisasm(exe):
23    full_path = test.built_file_path(exe, chdir=CHDIR)
24    # Get disassembly and drop int3 padding between functions.
25    return '\n'.join(
26        x for x in test.run_dumpbin('/disasm', full_path).splitlines()
27                   if 'CC' not in x)
28
29  # This is the full dump that we expect. The source files in the .gyp match
30  # this order which is what determines the ordering in the binary.
31
32  expected_disasm_basic = '''
33_mainCRTStartup:
34  00401000: B8 05 00 00 00     mov         eax,5
35  00401005: C3                 ret
36?z@@YAHXZ:
37  00401010: B8 03 00 00 00     mov         eax,3
38  00401015: C3                 ret
39?x@@YAHXZ:
40  00401020: B8 01 00 00 00     mov         eax,1
41  00401025: C3                 ret
42?y@@YAHXZ:
43  00401030: B8 02 00 00 00     mov         eax,2
44  00401035: C3                 ret
45_main:
46  00401040: 33 C0              xor         eax,eax
47  00401042: C3                 ret
48'''
49
50  if expected_disasm_basic not in GetDisasm('test_ordering_exe.exe'):
51    print GetDisasm('test_ordering_exe.exe')
52    test.fail_test()
53
54  # Similar to above. The VS generator handles subdirectories differently.
55
56  expected_disasm_subdirs = '''
57_mainCRTStartup:
58  00401000: B8 05 00 00 00     mov         eax,5
59  00401005: C3                 ret
60_main:
61  00401010: 33 C0              xor         eax,eax
62  00401012: C3                 ret
63?y@@YAHXZ:
64  00401020: B8 02 00 00 00     mov         eax,2
65  00401025: C3                 ret
66?z@@YAHXZ:
67  00401030: B8 03 00 00 00     mov         eax,3
68  00401035: C3                 ret
69'''
70
71  if expected_disasm_subdirs not in GetDisasm('test_ordering_subdirs.exe'):
72    print GetDisasm('test_ordering_subdirs.exe')
73    test.fail_test()
74
75  # Similar, but with directories mixed into folders (crt and main at the same
76  # level, but with a subdir in the middle).
77
78  expected_disasm_subdirs_mixed = '''
79_mainCRTStartup:
80  00401000: B8 05 00 00 00     mov         eax,5
81  00401005: C3                 ret
82?x@@YAHXZ:
83  00401010: B8 01 00 00 00     mov         eax,1
84  00401015: C3                 ret
85_main:
86  00401020: 33 C0              xor         eax,eax
87  00401022: C3                 ret
88?z@@YAHXZ:
89  00401030: B8 03 00 00 00     mov         eax,3
90  00401035: C3                 ret
91?y@@YAHXZ:
92  00401040: B8 02 00 00 00     mov         eax,2
93  00401045: C3                 ret
94'''
95
96  if (expected_disasm_subdirs_mixed not in
97      GetDisasm('test_ordering_subdirs_mixed.exe')):
98    print GetDisasm('test_ordering_subdirs_mixed.exe')
99    test.fail_test()
100
101  test.pass_test()
102