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
)
45 'newlib': '-I/sdk_root/include -I/sdk_root/include/newlib',
46 'glibc': '-I/sdk_root/include -I/sdk_root/include/glibc',
47 'pnacl': '-I/sdk_root/include -I/sdk_root/include/pnacl',
48 'win': '-I/sdk_root/include -I/sdk_root/include/win',
49 'mac': '-I/sdk_root/include -I/sdk_root/include/mac',
50 'linux': '-I/sdk_root/include -I/sdk_root/include/linux'
52 for toolchain
, expected
in cases
.iteritems():
53 self
.assertEqual(expected
, nacl_config
.GetCFlags(toolchain
))
54 self
.assertRaises(nacl_config
.Error
, nacl_config
.GetCFlags
, 'foo')
56 def testIncludeDirs(self
):
58 'newlib': '/sdk_root/include /sdk_root/include/newlib',
59 'glibc': '/sdk_root/include /sdk_root/include/glibc',
60 'pnacl': '/sdk_root/include /sdk_root/include/pnacl',
61 'win': '/sdk_root/include /sdk_root/include/win',
62 'mac': '/sdk_root/include /sdk_root/include/mac',
63 'linux': '/sdk_root/include /sdk_root/include/linux'
65 for toolchain
, expected
in cases
.iteritems():
66 self
.assertEqual(expected
, nacl_config
.GetIncludeDirs(toolchain
))
67 self
.assertRaises(nacl_config
.Error
, nacl_config
.GetIncludeDirs
, 'foo')
69 def testLDFlags(self
):
70 self
.assertEqual('-L/sdk_root/lib', nacl_config
.GetLDFlags())
72 def _TestTool(self
, tool
, nacl_tool
=None, pnacl_tool
=None):
73 nacl_tool
= nacl_tool
or tool
74 pnacl_tool
= pnacl_tool
or tool
78 '/sdk_root/toolchain/mac_x86_newlib/bin/i686-nacl-%s' % nacl_tool
,
80 '/sdk_root/toolchain/mac_x86_newlib/bin/x86_64-nacl-%s' % nacl_tool
,
82 '/sdk_root/toolchain/mac_arm_newlib/bin/arm-nacl-%s' % nacl_tool
,
85 '/sdk_root/toolchain/mac_x86_glibc/bin/i686-nacl-%s' % nacl_tool
,
87 '/sdk_root/toolchain/mac_x86_glibc/bin/x86_64-nacl-%s' % nacl_tool
,
89 'pnacl': '/sdk_root/toolchain/mac_pnacl/bin/pnacl-%s' % pnacl_tool
,
91 '/sdk_root/toolchain/mac_pnacl/bin/pnacl-%s' % pnacl_tool
,
94 for tc_arch
, expected
in cases
.iteritems():
95 if isinstance(tc_arch
, tuple):
96 toolchain
= tc_arch
[0]
101 self
.assertEqual(expected
, nacl_config
.GetToolPath(toolchain
, arch
, tool
))
103 for toolchain
in ('host', 'mac', 'win', 'linux'):
104 self
.assertRaises(nacl_config
.Error
,
105 nacl_config
.GetToolPath
, toolchain
, None, tool
)
107 # Using toolchain=pnacl with any arch other than None, or 'pnacl' is an
109 for arch
in ('x86_32', 'x86_64', 'arm', 'foobar'):
110 self
.assertRaises(nacl_config
.Error
,
111 nacl_config
.GetToolPath
, toolchain
, arch
, tool
)
114 self
.assertRaises(nacl_config
.Error
,
115 nacl_config
.GetToolPath
, 'glibc', 'arm', tool
)
118 self
._TestTool
('cc', 'gcc', 'clang')
121 self
._TestTool
('c++', 'g++', 'clang++')
124 self
._TestTool
('ld', 'g++', 'clang++')
126 def testStandardTool(self
):
127 for tool
in ('nm', 'strip', 'ar', 'ranlib'):
131 # We always use the same gdb (it supports multiple toolchains/architectures)
132 expected
= '/sdk_root/toolchain/mac_x86_newlib/bin/x86_64-nacl-gdb'
133 for toolchain
in ('newlib', 'glibc', 'pnacl'):
134 for arch
in ('x86_32', 'x86_64', 'arm'):
135 self
.assertEqual(expected
,
136 nacl_config
.GetToolPath(toolchain
, arch
, 'gdb'))
139 if __name__
== '__main__':