Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / native_client_sdk / src / tools / tests / nacl_config_test.py
blobb240eceba84d977f2f05321bfaf1819e21fe945f
1 #!/usr/bin/env python
2 # Copyright 2013 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
6 import os
7 import sys
8 import unittest
10 SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
11 TOOLS_DIR = os.path.dirname(SCRIPT_DIR)
12 CHROME_SRC = os.path.dirname(os.path.dirname(os.path.dirname(TOOLS_DIR)))
13 MOCK_DIR = os.path.join(CHROME_SRC, 'third_party', 'pymock')
15 # For the mock library
16 sys.path.append(MOCK_DIR)
17 import mock
19 # For nacl_config, the module under test
20 sys.path.append(TOOLS_DIR)
21 import nacl_config
24 class TestNaclConfig(unittest.TestCase):
25 def setUp(self):
26 self.patches = []
28 get_sdk_path = self.AddAndStartPatch('getos.GetSDKPath')
29 get_sdk_path.return_value = '/sdk_root'
31 get_platform = self.AddAndStartPatch('getos.GetPlatform')
32 get_platform.return_value = 'mac'
34 def tearDown(self):
35 for patch in self.patches:
36 patch.stop()
38 def AddAndStartPatch(self, name):
39 patch = mock.patch(name)
40 self.patches.append(patch)
41 return patch.start()
43 @mock.patch('nacl_config.GetCFlags')
44 def testMainArgParsing(self, mock_get_cflags):
45 mock_get_cflags.return_value = 'flags'
46 with mock.patch('sys.stdout'):
47 nacl_config.main(['--cflags'])
48 mock_get_cflags.assert_called()
50 def testCFlags(self):
51 cases = {
52 'newlib': '-I/sdk_root/include -I/sdk_root/include/newlib',
53 'glibc': '-I/sdk_root/include -I/sdk_root/include/glibc',
54 'pnacl': '-I/sdk_root/include -I/sdk_root/include/pnacl',
55 'win': '-I/sdk_root/include -I/sdk_root/include/win',
56 'mac': '-I/sdk_root/include -I/sdk_root/include/mac',
57 'linux': '-I/sdk_root/include -I/sdk_root/include/linux'
59 for toolchain, expected in cases.iteritems():
60 self.assertEqual(expected, nacl_config.GetCFlags(toolchain))
61 self.assertRaises(nacl_config.Error, nacl_config.GetCFlags, 'foo')
63 def testIncludeDirs(self):
64 cases = {
65 'newlib': '/sdk_root/include /sdk_root/include/newlib',
66 'glibc': '/sdk_root/include /sdk_root/include/glibc',
67 'pnacl': '/sdk_root/include /sdk_root/include/pnacl',
68 'win': '/sdk_root/include /sdk_root/include/win',
69 'mac': '/sdk_root/include /sdk_root/include/mac',
70 'linux': '/sdk_root/include /sdk_root/include/linux'
72 for toolchain, expected in cases.iteritems():
73 self.assertEqual(expected, nacl_config.GetIncludeDirs(toolchain))
74 self.assertRaises(nacl_config.Error, nacl_config.GetIncludeDirs, 'foo')
76 def testLDFlags(self):
77 self.assertEqual('-L/sdk_root/lib', nacl_config.GetLDFlags())
79 def _TestTool(self, tool, nacl_tool=None, pnacl_tool=None):
80 nacl_tool = nacl_tool or tool
81 pnacl_tool = pnacl_tool or tool
83 cases = {
84 ('newlib', 'x86_32'):
85 '/sdk_root/toolchain/mac_x86_newlib/bin/i686-nacl-%s' % nacl_tool,
86 ('newlib', 'x86_64'):
87 '/sdk_root/toolchain/mac_x86_newlib/bin/x86_64-nacl-%s' % nacl_tool,
88 ('newlib', 'arm'):
89 '/sdk_root/toolchain/mac_arm_newlib/bin/arm-nacl-%s' % nacl_tool,
91 ('glibc', 'arm'):
92 '/sdk_root/toolchain/mac_arm_glibc/bin/arm-nacl-%s' % nacl_tool,
93 ('glibc', 'x86_32'):
94 '/sdk_root/toolchain/mac_x86_glibc/bin/i686-nacl-%s' % nacl_tool,
95 ('glibc', 'x86_64'):
96 '/sdk_root/toolchain/mac_x86_glibc/bin/x86_64-nacl-%s' % nacl_tool,
98 'pnacl': '/sdk_root/toolchain/mac_pnacl/bin/pnacl-%s' % pnacl_tool,
99 ('pnacl', 'pnacl'):
100 '/sdk_root/toolchain/mac_pnacl/bin/pnacl-%s' % pnacl_tool,
103 for tc_arch, expected in cases.iteritems():
104 if isinstance(tc_arch, tuple):
105 toolchain = tc_arch[0]
106 arch = tc_arch[1]
107 else:
108 toolchain = tc_arch
109 arch = None
110 self.assertEqual(expected, nacl_config.GetToolPath(toolchain, arch, tool))
112 for toolchain in ('host', 'mac', 'win', 'linux'):
113 self.assertRaises(nacl_config.Error,
114 nacl_config.GetToolPath, toolchain, None, tool)
116 # Using toolchain=pnacl with any arch other than None, or 'pnacl' is an
117 # error.
118 for arch in ('x86_32', 'x86_64', 'arm', 'foobar'):
119 self.assertRaises(nacl_config.Error,
120 nacl_config.GetToolPath, toolchain, arch, tool)
122 def testCC(self):
123 self._TestTool('cc', 'gcc', 'clang')
125 def testCXX(self):
126 self._TestTool('c++', 'g++', 'clang++')
128 def testLD(self):
129 self._TestTool('ld', 'g++', 'clang++')
131 def testStandardTool(self):
132 for tool in ('nm', 'strip', 'ar', 'ranlib'):
133 self._TestTool(tool)
135 def testGDB(self):
136 # We always use the same gdb (it supports multiple toolchains/architectures)
137 expected = '/sdk_root/toolchain/mac_x86_newlib/bin/x86_64-nacl-gdb'
138 for toolchain in ('newlib', 'glibc', 'pnacl'):
139 for arch in ('x86_32', 'x86_64', 'arm'):
140 self.assertEqual(expected,
141 nacl_config.GetToolPath(toolchain, arch, 'gdb'))
144 if __name__ == '__main__':
145 unittest.main()