view test/testzcc.py @ 352:899ae3aea803

First kernel run for vexpressA9
author Windel Bouwman
date Sun, 09 Mar 2014 11:55:55 +0100
parents 3bb7dcfe5529
children b8ad45b3a573
line wrap: on
line source

import unittest
import os
import sys

import zcc
from ppci.objectfile import ObjectFile
import ppci
import io
from ppci.target import target_list

# Store testdir for safe switch back to directory:
testdir = os.path.dirname(os.path.abspath(__file__))

def relpath(*args):
    return os.path.join(testdir, *args)

class ZccBaseTestCase(unittest.TestCase):
    def callZcc(self, arg_list):
        parser = zcc.make_parser()
        arg_list = ['--log', 'warn'] + arg_list
        args = parser.parse_args(arg_list)
        self.assertEqual(0, zcc.main(args))

    def buildRecipe(self, recipe):
        arg_list = ['recipe', recipe]
        self.callZcc(arg_list)


class ZccTestCase(ZccBaseTestCase):
    """ Tests the compiler driver """
    def setUp(self):
        os.chdir(testdir)

    def tearDown(self):
        os.chdir(testdir)

    def do(self, filenames, imps=[], extra_args=[]):
        basedir = os.path.join('..', 'examples', 'c3')
        arg_list = ['compile']
        arg_list += [os.path.join(basedir, fn) for fn in filenames]
        for fn in imps:
            arg_list.append('-i')
            arg_list.append(os.path.join(basedir, fn))
        arg_list.append('--target')
        arg_list.append('thumb')
        arg_list += extra_args
        self.callZcc(arg_list)


    def testDumpIr(self):
        basedir = relpath('..', 'examples', 'c3', 'comments.c3')
        arg_list = ['compile', basedir]
        arg_list.append('--target')
        arg_list.append('thumb')
        self.callZcc(arg_list)

    def testThumbKernel(self):
        """ Build kernel using zcc: """
        recipe = relpath('..', 'kernel', 'thumb.yaml')
        self.buildRecipe(recipe)

    def testArmKernel(self):
        """ Build kernel using zcc: """
        recipe = relpath('..', 'kernel', 'arm.yaml')
        self.buildRecipe(recipe)

    @unittest.skip('Too difficult to fix')
    def testKernelBuildsEqualTwice(self):
        """ Build kernel two times and check the output is equal """
        recipe = relpath('..', 'kernel', 'recipe.yaml')
        bin_filename = relpath('..', 'kernel', 'kernel.bin')
        self.buildRecipe(recipe)
        with open(bin_filename, 'rb') as f:
            a = f.read()
        self.buildRecipe(recipe)
        with open(bin_filename, 'rb') as f:
            b = f.read()
        self.assertSequenceEqual(a, b)

    def testUser(self):
        """ Build userspace using zcc: """
        recipe = relpath('..', 'user', 'recipe.yaml')
        self.buildRecipe(recipe)

    def testBurn2(self):
        self.do(['burn2.c3'], ['stm32f4xx.c3'])

    def testBurn2_recipe(self):
        recipe = relpath('..', 'examples', 'c3', 'recipe.yaml')
        self.buildRecipe(recipe)

    def test_hello_A9_c3_recipe(self):
        recipe = relpath('..', 'examples', 'qemu_a9_hello', 'recipe.yaml')
        self.buildRecipe(recipe)

    @unittest.skip('Skip because of logfile')
    def testBurn2WithLogging(self):
        self.do(['burn2.c3'], ['stm32f4xx.c3'], extra_args=['--report', 'x.rst'])

    def testCommentsExample(self):
        self.do(['comments.c3'])

    def testCast(self):
        self.do(['cast.c3'])

    def testFunctions(self):
        self.do(['functions.c3'])

    @unittest.skip('Revise this test')
    def testSectionAddress(self):
        src = """module tst;
            function void t2() {var int t3; t3 = 2;}
        """
        f = io.StringIO(src)
        out = ObjectFile()
        tg = target_list.armtarget
        tr = ppci.tasks.TaskRunner()
        tr.add_task(ppci.buildtasks.Compile([f], [], tg, out))
        tr.run_tasks()
        code = out.get_section('code')
        self.assertEqual(0x0, code.address)


if __name__ == '__main__':
    unittest.main()