Files
build/orchestrator/ninja/test_ninja_writer.py
Spandan Das aacf2376a5 Lightweight ninja writer in Python
Summary:
- Create python classes for ninja vocbulary in `ninja_syntax.py`. These
  classes will be serialized to a ninja file
- Create a Writer class in `ninja_writer.py`. The current API supports
  adding variables,rules,build actions, etc. This can be extended in the
  future (See `test_ninja_writer.py` for examples)

Future Work:
 - Update the `Subninja` class once chDir is supported (aosp/2064612)
 - Support a width parameter that will be used to wrap long lines of
   text. This will improve readability of the generated files

Expected Use Case: Multi-tree build orchestrator

Test: python ./test_ninja_syntax.py
Test: python ./test_ninja_writer.py

Change-Id: I90c7ee69ddeb7c20c3fd4fca5a911dddbf2253bd
2022-05-12 17:47:29 +00:00

55 lines
1.6 KiB
Python

#!/usr/bin/env python
#
# Copyright (C) 2022 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import unittest
from io import StringIO
from ninja_writer import Writer
from ninja_syntax import Variable, Rule, BuildAction
class TestWriter(unittest.TestCase):
def test_simple_writer(self):
with StringIO() as f:
writer = Writer(f)
writer.add_variable(Variable(name="cflags", value="-Wall"))
writer.add_newline()
cc = Rule(name="cc")
cc.add_variable(name="command", value="gcc $cflags -c $in -o $out")
writer.add_rule(cc)
writer.add_newline()
build_action = BuildAction(output="foo.o", rule="cc", inputs=["foo.c"])
writer.add_build_action(build_action)
writer.write()
self.assertEqual('''cflags = -Wall
rule cc
command = gcc $cflags -c $in -o $out
build foo.o: cc foo.c
''', f.getvalue())
def test_comment(self):
with StringIO() as f:
writer = Writer(f)
writer.add_comment("This is a comment in a ninja file")
writer.write()
self.assertEqual("# This is a comment in a ninja file\n", f.getvalue())
if __name__ == "__main__":
unittest.main()