1#!/usr/bin/env python
2
3# Copyright (c) 2012 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"""
8Tests things related to ARCHS.
9"""
10
11import TestGyp
12import TestMac
13
14import re
15import subprocess
16import sys
17
18if sys.platform == 'darwin':
19  test = TestGyp.TestGyp(formats=['ninja', 'make', 'xcode'])
20
21  test.run_gyp('test-no-archs.gyp', chdir='archs')
22  test.build('test-no-archs.gyp', test.ALL, chdir='archs')
23  result_file = test.built_file_path('Test', chdir='archs')
24  test.must_exist(result_file)
25
26  if TestMac.Xcode.Version() >= '0500':
27    expected_type = ['x86_64']
28  else:
29    expected_type = ['i386']
30  TestMac.CheckFileType(test, result_file, expected_type)
31
32  test.run_gyp('test-valid-archs.gyp', chdir='archs')
33  test.build('test-valid-archs.gyp', test.ALL, chdir='archs')
34  result_file = test.built_file_path('Test', chdir='archs')
35  test.must_exist(result_file)
36  TestMac.CheckFileType(test, result_file, ['x86_64'])
37
38  test.run_gyp('test-archs-x86_64.gyp', chdir='archs')
39  test.build('test-archs-x86_64.gyp', test.ALL, chdir='archs')
40  result_file = test.built_file_path('Test64', chdir='archs')
41  test.must_exist(result_file)
42  TestMac.CheckFileType(test, result_file, ['x86_64'])
43
44  if test.format != 'make':
45    # Build all targets except 'exe_32_64_no_sources' that does build
46    # but should not cause error when generating ninja files
47    targets = [
48        'static_32_64', 'shared_32_64', 'shared_32_64_bundle',
49        'module_32_64', 'module_32_64_bundle',
50        'exe_32_64', 'exe_32_64_bundle', 'precompiled_prefix_header_mm_32_64',
51    ]
52
53    test.run_gyp('test-archs-multiarch.gyp', chdir='archs')
54    for target in targets:
55      test.build('test-archs-multiarch.gyp', target=target, chdir='archs')
56
57    result_file = test.built_file_path(
58        'static_32_64', chdir='archs', type=test.STATIC_LIB)
59    test.must_exist(result_file)
60    TestMac.CheckFileType(test, result_file, ['i386', 'x86_64'])
61
62    result_file = test.built_file_path(
63        'shared_32_64', chdir='archs', type=test.SHARED_LIB)
64    test.must_exist(result_file)
65    TestMac.CheckFileType(test, result_file, ['i386', 'x86_64'])
66
67    result_file = test.built_file_path('My Framework.framework/My Framework',
68                                       chdir='archs')
69    test.must_exist(result_file)
70    TestMac.CheckFileType(test, result_file, ['i386', 'x86_64'])
71    # Check that symbol "_x" made it into both versions of the binary:
72    if not all(['D _x' in subprocess.check_output(
73        ['nm', '-arch', arch, result_file]) for arch in ['i386', 'x86_64']]):
74      # This can only flakily fail, due to process ordering issues. If this
75      # does fail flakily, then something's broken, it's not the test at fault.
76      test.fail_test()
77
78    result_file = test.built_file_path(
79        'exe_32_64', chdir='archs', type=test.EXECUTABLE)
80    test.must_exist(result_file)
81    TestMac.CheckFileType(test, result_file, ['i386', 'x86_64'])
82
83    result_file = test.built_file_path('Test App.app/Contents/MacOS/Test App',
84                                       chdir='archs')
85    test.must_exist(result_file)
86    TestMac.CheckFileType(test, result_file, ['i386', 'x86_64'])
87