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.
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
)
19 # For nacl_config, the module under test
20 sys
.path
.append(TOOLS_DIR
)
24 class TestNaclConfig(unittest
.TestCase
):
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'
35 for patch
in self
.patches
:
38 def AddAndStartPatch(self
, name
):
39 patch
= mock
.patch(name
)
40 self
.patches
.append(patch
)
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()
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
):
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
85 '/sdk_root/toolchain/mac_x86_newlib/bin/i686-nacl-%s' % nacl_tool
,
87 '/sdk_root/toolchain/mac_x86_newlib/bin/x86_64-nacl-%s' % nacl_tool
,
89 '/sdk_root/toolchain/mac_arm_newlib/bin/arm-nacl-%s' % nacl_tool
,
92 '/sdk_root/toolchain/mac_arm_glibc/bin/arm-nacl-%s' % nacl_tool
,
94 '/sdk_root/toolchain/mac_x86_glibc/bin/i686-nacl-%s' % nacl_tool
,
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
,
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]
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
118 for arch
in ('x86_32', 'x86_64', 'arm', 'foobar'):
119 self
.assertRaises(nacl_config
.Error
,
120 nacl_config
.GetToolPath
, toolchain
, arch
, tool
)
123 self
._TestTool
('cc', 'gcc', 'clang')
126 self
._TestTool
('c++', 'g++', 'clang++')
129 self
._TestTool
('ld', 'g++', 'clang++')
131 def testStandardTool(self
):
132 for tool
in ('nm', 'strip', 'ar', 'ranlib'):
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__':