Diana Picus | 6b1935f | 2018-02-07 16:44:11 +0100 | [diff] [blame] | 1 | from modules.llvm import LLVMBuildConfig |
Diana Picus | 37126b8 | 2018-01-19 16:14:26 +0100 | [diff] [blame] | 2 | |
| 3 | import os |
| 4 | |
| 5 | from shutil import rmtree |
| 6 | from tempfile import mkdtemp |
| 7 | |
| 8 | from unittest import TestCase |
| 9 | from unittest.mock import MagicMock |
| 10 | |
| 11 | |
| 12 | def create_empty_dir(): |
| 13 | return mkdtemp() |
| 14 | |
| 15 | |
| 16 | def create_dir_with_empty_file(filename): |
| 17 | dir = create_empty_dir() |
| 18 | open(os.path.join(dir, filename), "wt").close() |
| 19 | return dir |
| 20 | |
| 21 | |
| 22 | class TestBuildLLVM(TestCase): |
| 23 | |
| 24 | def tearDown(self): |
| 25 | rmtree(self.buildDir) |
| 26 | |
| 27 | def test_invalid_build_dir(self): |
| 28 | self.buildDir = create_empty_dir() |
| 29 | |
Diana Picus | 6b1935f | 2018-02-07 16:44:11 +0100 | [diff] [blame] | 30 | buildConfig = LLVMBuildConfig(None, self.buildDir, None) |
Diana Picus | 37126b8 | 2018-01-19 16:14:26 +0100 | [diff] [blame] | 31 | with self.assertRaises(RuntimeError) as context: |
Diana Picus | 6b1935f | 2018-02-07 16:44:11 +0100 | [diff] [blame] | 32 | buildConfig.build() |
Diana Picus | 37126b8 | 2018-01-19 16:14:26 +0100 | [diff] [blame] | 33 | |
| 34 | self.assertRegex( |
| 35 | str(context.exception), |
| 36 | "(.*\n)*Couldn't identify build system to use for {}(.*\n)*".format( |
| 37 | self.buildDir)) |
| 38 | |
| 39 | def test_ninja_build_dir(self): |
| 40 | self.buildDir = create_dir_with_empty_file("build.ninja") |
| 41 | consumer = MagicMock() |
| 42 | |
Diana Picus | 6b1935f | 2018-02-07 16:44:11 +0100 | [diff] [blame] | 43 | buildConfig = LLVMBuildConfig(None, self.buildDir, consumer) |
| 44 | buildConfig.build() |
Diana Picus | 37126b8 | 2018-01-19 16:14:26 +0100 | [diff] [blame] | 45 | command, directory = consumer.consume.call_args[0] |
| 46 | |
| 47 | self.assertEqual(command, ["ninja"]) |
| 48 | self.assertEqual(directory, self.buildDir) |
| 49 | |
| 50 | def test_make_build_dir(self): |
| 51 | self.buildDir = create_dir_with_empty_file("Makefile") |
| 52 | consumer = MagicMock() |
| 53 | |
Diana Picus | 6b1935f | 2018-02-07 16:44:11 +0100 | [diff] [blame] | 54 | buildConfig = LLVMBuildConfig(None, self.buildDir, consumer) |
| 55 | buildConfig.build() |
Diana Picus | 37126b8 | 2018-01-19 16:14:26 +0100 | [diff] [blame] | 56 | command, directory = consumer.consume.call_args[0] |
| 57 | |
| 58 | self.assertEqual(command, ["make"]) |
| 59 | self.assertEqual(directory, self.buildDir) |
| 60 | |
| 61 | def test_flags(self): |
| 62 | self.buildDir = create_dir_with_empty_file("build.ninja") |
| 63 | consumer = MagicMock() |
| 64 | |
Diana Picus | 6b1935f | 2018-02-07 16:44:11 +0100 | [diff] [blame] | 65 | buildConfig = LLVMBuildConfig(None, self.buildDir, consumer) |
| 66 | buildConfig.build(["-t", "targets"]) |
Diana Picus | 37126b8 | 2018-01-19 16:14:26 +0100 | [diff] [blame] | 67 | command, directory = consumer.consume.call_args[0] |
| 68 | |
| 69 | self.assertEqual(command, ["ninja", "-t", "targets"]) |
| 70 | self.assertEqual(directory, self.buildDir) |
Diana Picus | 6b1935f | 2018-02-07 16:44:11 +0100 | [diff] [blame] | 71 | |
| 72 | def test_cmake_then_build(self): |
| 73 | """ |
| 74 | Test that when using the same build config to both configure and build, |
| 75 | we use the build tool corresponding to the generator used for cmake |
| 76 | regardless of what the build directory already contains. |
| 77 | """ |
| 78 | self.buildDir = create_dir_with_empty_file("Makefile") |
| 79 | sourceConfig = MagicMock() |
| 80 | consumer = MagicMock() |
| 81 | |
| 82 | buildConfig = LLVMBuildConfig(sourceConfig, self.buildDir, consumer) |
| 83 | buildConfig.cmake([], "Ninja") |
| 84 | buildConfig.build() |
| 85 | |
| 86 | consumer.consume.assert_called_with(["ninja"], self.buildDir) |