| from modules.llvm import LLVMBuildConfig |
| |
| import os |
| |
| from shutil import rmtree |
| from tempfile import mkdtemp |
| |
| from unittest import TestCase |
| from unittest.mock import MagicMock |
| |
| |
| def create_empty_dir(): |
| return mkdtemp() |
| |
| |
| def create_dir_with_empty_file(filename): |
| dir = create_empty_dir() |
| open(os.path.join(dir, filename), "wt").close() |
| return dir |
| |
| |
| class TestBuildLLVM(TestCase): |
| |
| def tearDown(self): |
| rmtree(self.buildDir) |
| |
| def test_invalid_build_dir(self): |
| self.buildDir = create_empty_dir() |
| |
| buildConfig = LLVMBuildConfig(None, self.buildDir, None) |
| with self.assertRaises(RuntimeError) as context: |
| buildConfig.build() |
| |
| self.assertRegex( |
| str(context.exception), |
| "(.*\n)*Couldn't identify build system to use for {}(.*\n)*".format( |
| self.buildDir)) |
| |
| def test_ninja_build_dir(self): |
| self.buildDir = create_dir_with_empty_file("build.ninja") |
| consumer = MagicMock() |
| |
| buildConfig = LLVMBuildConfig(None, self.buildDir, consumer) |
| buildConfig.build() |
| command, directory = consumer.consume.call_args[0] |
| |
| self.assertEqual(command, ["ninja"]) |
| self.assertEqual(directory, self.buildDir) |
| |
| def test_make_build_dir(self): |
| self.buildDir = create_dir_with_empty_file("Makefile") |
| consumer = MagicMock() |
| |
| buildConfig = LLVMBuildConfig(None, self.buildDir, consumer) |
| buildConfig.build() |
| command, directory = consumer.consume.call_args[0] |
| |
| self.assertEqual(command, ["make"]) |
| self.assertEqual(directory, self.buildDir) |
| |
| def test_flags(self): |
| self.buildDir = create_dir_with_empty_file("build.ninja") |
| consumer = MagicMock() |
| |
| buildConfig = LLVMBuildConfig(None, self.buildDir, consumer) |
| buildConfig.build(["-t", "targets"]) |
| command, directory = consumer.consume.call_args[0] |
| |
| self.assertEqual(command, ["ninja", "-t", "targets"]) |
| self.assertEqual(directory, self.buildDir) |
| |
| def test_cmake_then_build(self): |
| """ |
| Test that when using the same build config to both configure and build, |
| we use the build tool corresponding to the generator used for cmake |
| regardless of what the build directory already contains. |
| """ |
| self.buildDir = create_dir_with_empty_file("Makefile") |
| sourceConfig = MagicMock() |
| consumer = MagicMock() |
| |
| buildConfig = LLVMBuildConfig(sourceConfig, self.buildDir, consumer) |
| buildConfig.cmake([], "Ninja") |
| buildConfig.build() |
| |
| consumer.consume.assert_called_with(["ninja"], self.buildDir) |