Diana Picus | 052b7d3 | 2017-11-24 16:19:41 +0100 | [diff] [blame] | 1 | from modules.llvm import LLVMBuildConfig |
| 2 | |
| 3 | from os import makedirs |
| 4 | from shutil import rmtree |
| 5 | |
| 6 | from unittest import TestCase |
| 7 | from unittest.mock import MagicMock |
| 8 | from uuid import uuid4 |
| 9 | |
| 10 | |
| 11 | class TestLLVMBuildConfig(TestCase): |
| 12 | testdirprefix = "BuildConfigUT" |
| 13 | |
| 14 | def setUp(self): |
| 15 | self.sourcePath = "llvm" + str(uuid4()) |
| 16 | self.buildPath = "build" + str(uuid4()) |
| 17 | |
| 18 | sourceConfigAttrs = {"get_path.return_value": self.sourcePath} |
| 19 | self.sourceConfig = MagicMock(**sourceConfigAttrs) |
| 20 | |
| 21 | makedirs(self.buildPath) |
| 22 | |
| 23 | def tearDown(self): |
| 24 | rmtree(self.buildPath) |
| 25 | |
| 26 | def test_configure_generator(self): |
| 27 | """Test that we can use a custom generator for our CMake command.""" |
Diana Picus | 052b7d3 | 2017-11-24 16:19:41 +0100 | [diff] [blame] | 28 | consumer = MagicMock() |
Diana Picus | 6b1935f | 2018-02-07 16:44:11 +0100 | [diff] [blame] | 29 | buildConfig = LLVMBuildConfig(self.sourceConfig, self.buildPath, |
| 30 | consumer) |
| 31 | |
| 32 | buildConfig.cmake([], "Unix Makefiles") |
Diana Picus | 052b7d3 | 2017-11-24 16:19:41 +0100 | [diff] [blame] | 33 | command, directory = consumer.consume.call_args[0] |
| 34 | |
| 35 | self.assertEqual(directory, self.buildPath) |
| 36 | |
| 37 | self.assertEqual(command[0], "cmake") |
| 38 | self.assertIn(self.sourcePath, command) |
| 39 | self.assertEqual( |
| 40 | command.index("-G") + 1, |
| 41 | command.index("Unix Makefiles")) |
| 42 | |
| 43 | def test_configure_definitions(self): |
| 44 | """Test that we can define custom CMake variables.""" |
Diana Picus | 6b1935f | 2018-02-07 16:44:11 +0100 | [diff] [blame] | 45 | consumer = MagicMock() |
| 46 | buildConfig = LLVMBuildConfig(self.sourceConfig, self.buildPath, |
| 47 | consumer) |
Diana Picus | 052b7d3 | 2017-11-24 16:19:41 +0100 | [diff] [blame] | 48 | flags = ["-DCMAKE_BUILD_TYPE=Release", |
| 49 | "-DCMAKE_CXX_FLAGS=\"-Oz -g\""] |
| 50 | |
Diana Picus | 6b1935f | 2018-02-07 16:44:11 +0100 | [diff] [blame] | 51 | buildConfig.cmake(flags, "Ninja") |
Diana Picus | 052b7d3 | 2017-11-24 16:19:41 +0100 | [diff] [blame] | 52 | command, directory = consumer.consume.call_args[0] |
| 53 | |
| 54 | self.assertEqual(directory, self.buildPath) |
| 55 | |
| 56 | self.assertEqual(command[0], "cmake") |
| 57 | self.assertEqual(command.index("-G") + 1, command.index("Ninja")) |
| 58 | self.assertIn(self.sourcePath, command) |
| 59 | self.assertEqual(command.index(flags[0]) + 1, command.index(flags[1])) |
| 60 | |
| 61 | def test_update_projects(self): |
| 62 | """ |
| 63 | Test that we explicitly enable/disable subprojects based on what is |
| 64 | enabled in the source config. |
| 65 | """ |
| 66 | |
| 67 | def for_each_subproj(action): |
| 68 | # Pretend that clang is enabled and lld isn't. |
| 69 | clang_subproj = MagicMock() |
| 70 | clang_subproj.cmake_var = "BUILD_CLANG" |
| 71 | action(clang_subproj, True) |
| 72 | |
| 73 | lld_subproj = MagicMock() |
| 74 | lld_subproj.cmake_var = "BUILD_LLD" |
| 75 | action(lld_subproj, False) |
| 76 | |
| 77 | self.sourceConfig.for_each_subproj.side_effect = for_each_subproj |
| 78 | |
Diana Picus | 052b7d3 | 2017-11-24 16:19:41 +0100 | [diff] [blame] | 79 | consumer = MagicMock() |
Diana Picus | 6b1935f | 2018-02-07 16:44:11 +0100 | [diff] [blame] | 80 | buildConfig = LLVMBuildConfig(self.sourceConfig, self.buildPath, |
| 81 | consumer) |
| 82 | |
| 83 | buildConfig.cmake([], "Ninja") |
Diana Picus | 052b7d3 | 2017-11-24 16:19:41 +0100 | [diff] [blame] | 84 | command, directory = consumer.consume.call_args[0] |
| 85 | |
| 86 | self.assertEqual(directory, self.buildPath) |
| 87 | |
| 88 | self.assertEqual(command[0], "cmake") |
| 89 | self.assertEqual(command.index("-G") + 1, command.index("Ninja")) |
| 90 | self.assertIn(self.sourcePath, command) |
| 91 | self.assertIn("-DBUILD_CLANG=ON", command) |
| 92 | self.assertIn("-DBUILD_LLD=OFF", command) |