1342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch#!/usr/bin/env python
2342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch
3342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch# Copyright (c) 2012 Google Inc. All rights reserved.
4342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch# Use of this source code is governed by a BSD-style license that can be
5342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch# found in the LICENSE file.
6342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch
7342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch"""
8342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen MurdochTests things related to ARCHS.
9342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch"""
10342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch
11342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdochimport TestGyp
12342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdochimport TestMac
13342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch
14342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdochimport re
15342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdochimport subprocess
16342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdochimport sys
17342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch
18342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdochif sys.platform == 'darwin':
19342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch  test = TestGyp.TestGyp(formats=['ninja', 'make', 'xcode'])
20342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch
21342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch  test.run_gyp('test-no-archs.gyp', chdir='archs')
22342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch  test.build('test-no-archs.gyp', test.ALL, chdir='archs')
23342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch  result_file = test.built_file_path('Test', chdir='archs')
24342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch  test.must_exist(result_file)
25342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch
26342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch  if TestMac.Xcode.Version() >= '0500':
27342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch    expected_type = ['x86_64']
28342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch  else:
29342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch    expected_type = ['i386']
30342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch  TestMac.CheckFileType(test, result_file, expected_type)
31342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch
32342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch  test.run_gyp('test-valid-archs.gyp', chdir='archs')
33342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch  test.build('test-valid-archs.gyp', test.ALL, chdir='archs')
34342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch  result_file = test.built_file_path('Test', chdir='archs')
35342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch  test.must_exist(result_file)
36342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch  TestMac.CheckFileType(test, result_file, ['x86_64'])
37342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch
38342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch  test.run_gyp('test-archs-x86_64.gyp', chdir='archs')
39342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch  test.build('test-archs-x86_64.gyp', test.ALL, chdir='archs')
40342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch  result_file = test.built_file_path('Test64', chdir='archs')
41342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch  test.must_exist(result_file)
42342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch  TestMac.CheckFileType(test, result_file, ['x86_64'])
43342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch
44342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch  test.run_gyp('test-dependencies.gyp', chdir='archs')
45342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch  test.build('test-dependencies.gyp', target=test.ALL, chdir='archs')
46342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch  products = ['c_standalone', 'd_standalone']
47342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch  for product in products:
48342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch    result_file = test.built_file_path(
49342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch        product, chdir='archs', type=test.STATIC_LIB)
50342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch    test.must_exist(result_file)
51342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch
52342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch  if test.format != 'make':
53342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch    # Build all targets except 'exe_32_64_no_sources' that does build
54342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch    # but should not cause error when generating ninja files
55342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch    targets = [
56342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch        'static_32_64', 'shared_32_64', 'shared_32_64_bundle',
57342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch        'module_32_64', 'module_32_64_bundle',
58342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch        'exe_32_64', 'exe_32_64_bundle', 'precompiled_prefix_header_mm_32_64',
59342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch    ]
60342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch
61342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch    test.run_gyp('test-archs-multiarch.gyp', chdir='archs')
62342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch
63342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch    for target in targets:
64342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch      test.build('test-archs-multiarch.gyp', target=target, chdir='archs')
65342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch
66342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch    result_file = test.built_file_path(
67342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch        'static_32_64', chdir='archs', type=test.STATIC_LIB)
68342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch    test.must_exist(result_file)
69342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch    TestMac.CheckFileType(test, result_file, ['i386', 'x86_64'])
70342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch
71342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch    result_file = test.built_file_path(
72342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch        'shared_32_64', chdir='archs', type=test.SHARED_LIB)
73342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch    test.must_exist(result_file)
74342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch    TestMac.CheckFileType(test, result_file, ['i386', 'x86_64'])
75342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch
76342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch    result_file = test.built_file_path('My Framework.framework/My Framework',
77342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch                                       chdir='archs')
78342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch    test.must_exist(result_file)
79342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch    TestMac.CheckFileType(test, result_file, ['i386', 'x86_64'])
80342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch    # Check that symbol "_x" made it into both versions of the binary:
81342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch    if not all(['D _x' in subprocess.check_output(
82342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch        ['nm', '-arch', arch, result_file]) for arch in ['i386', 'x86_64']]):
83342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch      # This can only flakily fail, due to process ordering issues. If this
84342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch      # does fail flakily, then something's broken, it's not the test at fault.
85342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch      test.fail_test()
86342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch
87342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch    result_file = test.built_file_path(
88342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch        'exe_32_64', chdir='archs', type=test.EXECUTABLE)
89342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch    test.must_exist(result_file)
90342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch    TestMac.CheckFileType(test, result_file, ['i386', 'x86_64'])
91342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch
92342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch    result_file = test.built_file_path('Test App.app/Contents/MacOS/Test App',
93342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch                                       chdir='archs')
94342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch    test.must_exist(result_file)
95342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch    TestMac.CheckFileType(test, result_file, ['i386', 'x86_64'])
96