• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python
2#
3# Copyright (C) 2022 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#      http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# ibuted under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17"""Unit tests for library.py."""
18
19import unittest
20
21from ninja_tools import Ninja
22from ninja_syntax import Rule, BuildAction
23
24from cc.library import CompileContext, Compiler, LinkContext, Linker
25from utils import ContextTest
26
27class TestCompiler(unittest.TestCase):
28
29    def setUp(self):
30        self.ninja_context = ContextTest("test_dir", self.id())
31
32    def test_clang_is_implicit_dep(self):
33        compiler = Compiler()
34        compile_context = CompileContext("src", "flags", "out", frontend="my/clang")
35        ninja = Ninja(context=self.ninja_context, file=None)
36        compiler.compile(ninja, compile_context)
37        compile_action_nodes = [node for node in ninja.nodes if isinstance(node, BuildAction)]
38        self.assertTrue(all(["my/clang" in node.implicits for node in compile_action_nodes]))
39
40    def test_compile_flags_are_added(self):
41        compiler = Compiler()
42        compile_context = CompileContext("src", "myflag1 myflag2 myflag3", "out", frontend="my/clang")
43        ninja = Ninja(context=self.ninja_context, file=None)
44        compiler.compile(ninja, compile_context)
45        compile_action_nodes = [node for node in ninja.nodes if isinstance(node, BuildAction)]
46        self.assertEqual(len(compile_action_nodes), 1)
47        compile_action_node = compile_action_nodes[0]
48        variables = sorted(compile_action_node.variables, key=lambda x: x.name)
49        self.assertEqual(len(variables), 2)
50        self.assertEqual(variables[0].name, "cFlags")
51        self.assertEqual(variables[0].value, compile_context.flags)
52        self.assertEqual(variables[1].name, "cFrontend")
53        self.assertEqual(variables[1].value, compile_context.frontend)
54
55class TestLinker(unittest.TestCase):
56
57    def setUp(self):
58        self.ninja_context = ContextTest("test_dir", self.id())
59
60    def test_clang_is_implicit_dep(self):
61        linker = Linker()
62        link_context = LinkContext("objs", "flags", "out", frontend="my/clang")
63        ninja = Ninja(context=self.ninja_context, file=None)
64        linker.link(ninja, link_context)
65        link_action_nodes = [node for node in ninja.nodes if isinstance(node, BuildAction)]
66        self.assertTrue(all(["my/clang" in node.implicits for node in link_action_nodes]))
67
68    def test_link_flags_are_added(self):
69        linker = Linker()
70        link_context = LinkContext("src", "myflag1 myflag2 myflag3", "out", frontend="my/clang")
71        ninja = Ninja(context=self.ninja_context, file=None)
72        linker.link(ninja, link_context)
73        link_action_nodes = [node for node in ninja.nodes if isinstance(node, BuildAction)]
74        self.assertEqual(len(link_action_nodes), 1)
75        link_action_node = link_action_nodes[0]
76        variables = sorted(link_action_node.variables, key=lambda x: x.name)
77        self.assertEqual(len(variables), 2)
78        self.assertEqual(variables[0].name, "ldFlags")
79        self.assertEqual(variables[0].value, link_context.flags)
80        self.assertEqual(variables[1].name, "ldFrontend")
81        self.assertEqual(variables[1].value, link_context.frontend)
82
83
84if __name__ == "__main__":
85    unittest.main()
86