|
| 1 | +# Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | +# All rights reserved. |
| 3 | +# |
| 4 | +# This source code is licensed under the BSD-style license found in the |
| 5 | +# LICENSE file in the root directory of this source tree. |
| 6 | + |
| 7 | +import os |
| 8 | + |
| 9 | +from tools.cmake.common import CMakeTestCase, TESTABLE_CMAKE_FILES |
| 10 | + |
| 11 | + |
| 12 | +class TestPreset(CMakeTestCase): |
| 13 | + |
| 14 | + def test_create_workspace(self): |
| 15 | + self.create_workspace( |
| 16 | + { |
| 17 | + ".gitignore": ".DS_Store", |
| 18 | + "CMakeLists.txt": "move fast", |
| 19 | + "README.md": "Meta Platforms", |
| 20 | + "example": { |
| 21 | + "CMakeLists.txt": "move faster", |
| 22 | + "cmake": { |
| 23 | + "README.md": "godspeed you!", |
| 24 | + }, |
| 25 | + }, |
| 26 | + } |
| 27 | + ) |
| 28 | + |
| 29 | + self.assertIsNotNone(self.workspace) |
| 30 | + self.assert_file_content("CMakeLists.txt", "move fast") |
| 31 | + self.assert_file_content("README.md", "Meta Platforms") |
| 32 | + self.assert_file_content(".gitignore", ".DS_Store") |
| 33 | + self.assert_file_content("example/CMakeLists.txt", "move faster") |
| 34 | + self.assert_file_content("example/cmake/README.md", "godspeed you!") |
| 35 | + |
| 36 | + # Test implicitly copied cmake files |
| 37 | + this_file_dir = os.path.dirname(os.path.abspath(__file__)) |
| 38 | + self.assertTrue(len(TESTABLE_CMAKE_FILES) > 0) |
| 39 | + for testable_cmake_file in TESTABLE_CMAKE_FILES: |
| 40 | + with open( |
| 41 | + os.path.join(this_file_dir, testable_cmake_file), "r" |
| 42 | + ) as source_file: |
| 43 | + self.assert_file_content(testable_cmake_file, source_file.read()) |
| 44 | + |
| 45 | + def test_set_option(self): |
| 46 | + _cmake_lists_txt = """ |
| 47 | + cmake_minimum_required(VERSION 3.24) |
| 48 | + project(test_preset) |
| 49 | + add_subdirectory(numbers) |
| 50 | + set(SECRET_MESSAGE "move fast" CACHE STRING "") |
| 51 | + """ |
| 52 | + _numbers_cmake_lists_txt = """ |
| 53 | + set(PI 3.14 CACHE STRING "") |
| 54 | + """ |
| 55 | + |
| 56 | + self.create_workspace( |
| 57 | + { |
| 58 | + "CMakeLists.txt": _cmake_lists_txt, |
| 59 | + "numbers": { |
| 60 | + "CMakeLists.txt": _numbers_cmake_lists_txt, |
| 61 | + }, |
| 62 | + } |
| 63 | + ) |
| 64 | + self.run_cmake() |
| 65 | + self.assert_cmake_cache("SECRET_MESSAGE", "move fast") |
| 66 | + self.assert_cmake_cache("PI", "3.14") |
| 67 | + |
| 68 | + def test_define_overridable_config_invalid_name(self): |
| 69 | + _cmake_lists_txt = """ |
| 70 | + cmake_minimum_required(VERSION 3.24) |
| 71 | + project(test_preset) |
| 72 | + include(${PROJECT_SOURCE_DIR}/preset.cmake) |
| 73 | + define_overridable_config(IAM_AN_INVALID_NAME "test example" "default value") |
| 74 | + """ |
| 75 | + self.create_workspace({"CMakeLists.txt": _cmake_lists_txt}) |
| 76 | + self.run_cmake( |
| 77 | + error_contains="Config name 'IAM_AN_INVALID_NAME' must start with EXECUTORCH_" |
| 78 | + ) |
| 79 | + |
| 80 | + def test_define_overridable_config_default(self): |
| 81 | + _cmake_lists_txt = """ |
| 82 | + cmake_minimum_required(VERSION 3.24) |
| 83 | + project(test_preset) |
| 84 | + include(${PROJECT_SOURCE_DIR}/preset.cmake) |
| 85 | + add_subdirectory(example) |
| 86 | + """ |
| 87 | + _example_cmake_lists_txt = """ |
| 88 | + define_overridable_config(EXECUTORCH_TEST_MESSAGE "test message" "default value") |
| 89 | + """ |
| 90 | + self.create_workspace( |
| 91 | + { |
| 92 | + "CMakeLists.txt": _cmake_lists_txt, |
| 93 | + "example": { |
| 94 | + "CMakeLists.txt": _example_cmake_lists_txt, |
| 95 | + }, |
| 96 | + } |
| 97 | + ) |
| 98 | + self.run_cmake() |
| 99 | + self.assert_cmake_cache("EXECUTORCH_TEST_MESSAGE", "default value") |
| 100 | + |
| 101 | + def test_define_overridable_config_cli_override(self): |
| 102 | + _cmake_lists_txt = """ |
| 103 | + cmake_minimum_required(VERSION 3.24) |
| 104 | + project(test_preset) |
| 105 | + include(${PROJECT_SOURCE_DIR}/preset.cmake) |
| 106 | + add_subdirectory(example) |
| 107 | + """ |
| 108 | + _example_cmake_lists_txt = """ |
| 109 | + define_overridable_config(EXECUTORCH_TEST_MESSAGE "test message" "default value") |
| 110 | + """ |
| 111 | + self.create_workspace( |
| 112 | + { |
| 113 | + "CMakeLists.txt": _cmake_lists_txt, |
| 114 | + "example": { |
| 115 | + "CMakeLists.txt": _example_cmake_lists_txt, |
| 116 | + }, |
| 117 | + } |
| 118 | + ) |
| 119 | + self.run_cmake(cmake_args=["-DEXECUTORCH_TEST_MESSAGE='cli value'"]) |
| 120 | + self.assert_cmake_cache("EXECUTORCH_TEST_MESSAGE", "cli value") |
| 121 | + |
| 122 | + def test_define_overridable_config_set_override_before(self): |
| 123 | + _cmake_lists_txt = """ |
| 124 | + cmake_minimum_required(VERSION 3.24) |
| 125 | + project(test_preset) |
| 126 | + include(${PROJECT_SOURCE_DIR}/preset.cmake) |
| 127 | + set(EXECUTORCH_TEST_MESSAGE "set value") |
| 128 | + add_subdirectory(example) |
| 129 | + """ |
| 130 | + _example_cmake_lists_txt = """ |
| 131 | + define_overridable_config(EXECUTORCH_TEST_MESSAGE "test message" "default value") |
| 132 | + """ |
| 133 | + self.create_workspace( |
| 134 | + { |
| 135 | + "CMakeLists.txt": _cmake_lists_txt, |
| 136 | + "example": { |
| 137 | + "CMakeLists.txt": _example_cmake_lists_txt, |
| 138 | + }, |
| 139 | + } |
| 140 | + ) |
| 141 | + self.run_cmake() |
| 142 | + self.assert_cmake_cache("EXECUTORCH_TEST_MESSAGE", "set value") |
| 143 | + |
| 144 | + def testdefine_overridable_config_set_override_after(self): |
| 145 | + _cmake_lists_txt = """ |
| 146 | + cmake_minimum_required(VERSION 3.24) |
| 147 | + project(test_preset) |
| 148 | + include(${PROJECT_SOURCE_DIR}/preset.cmake) |
| 149 | + add_subdirectory(example) |
| 150 | + set(EXECUTORCH_TEST_MESSAGE "set value") |
| 151 | + """ |
| 152 | + _example_cmake_lists_txt = """ |
| 153 | + define_overridable_config(EXECUTORCH_TEST_MESSAGE "test message" "default value") |
| 154 | + """ |
| 155 | + self.create_workspace( |
| 156 | + { |
| 157 | + "CMakeLists.txt": _cmake_lists_txt, |
| 158 | + "example": { |
| 159 | + "CMakeLists.txt": _example_cmake_lists_txt, |
| 160 | + }, |
| 161 | + } |
| 162 | + ) |
| 163 | + self.run_cmake() |
| 164 | + # Setting the value after should not affect the cache. |
| 165 | + self.assert_cmake_cache("EXECUTORCH_TEST_MESSAGE", "default value") |
| 166 | + |
| 167 | + def test_define_overridable_config_set_override_after_with_cache(self): |
| 168 | + _cmake_lists_txt = """ |
| 169 | + cmake_minimum_required(VERSION 3.24) |
| 170 | + project(test_preset) |
| 171 | + include(${PROJECT_SOURCE_DIR}/preset.cmake) |
| 172 | + add_subdirectory(example) |
| 173 | + set(EXECUTORCH_TEST_MESSAGE "set value" CACHE STRING "") |
| 174 | + """ |
| 175 | + _example_cmake_lists_txt = """ |
| 176 | + define_overridable_config(EXECUTORCH_TEST_MESSAGE "test message" "default value") |
| 177 | + """ |
| 178 | + self.create_workspace( |
| 179 | + { |
| 180 | + "CMakeLists.txt": _cmake_lists_txt, |
| 181 | + "example": { |
| 182 | + "CMakeLists.txt": _example_cmake_lists_txt, |
| 183 | + }, |
| 184 | + } |
| 185 | + ) |
| 186 | + self.run_cmake() |
| 187 | + # Setting the value after should not affect the cache. |
| 188 | + self.assert_cmake_cache("EXECUTORCH_TEST_MESSAGE", "default value") |
| 189 | + |
| 190 | + def test_define_overridable_config_cli_override_with_set_override(self): |
| 191 | + _cmake_lists_txt = """ |
| 192 | + cmake_minimum_required(VERSION 3.24) |
| 193 | + project(test_preset) |
| 194 | + include(${PROJECT_SOURCE_DIR}/preset.cmake) |
| 195 | + set(EXECUTORCH_TEST_MESSAGE "set value") |
| 196 | + add_subdirectory(example) |
| 197 | + """ |
| 198 | + _example_cmake_lists_txt = """ |
| 199 | + define_overridable_config(EXECUTORCH_TEST_MESSAGE "test message" "default value") |
| 200 | + """ |
| 201 | + self.create_workspace( |
| 202 | + { |
| 203 | + "CMakeLists.txt": _cmake_lists_txt, |
| 204 | + "example": { |
| 205 | + "CMakeLists.txt": _example_cmake_lists_txt, |
| 206 | + }, |
| 207 | + } |
| 208 | + ) |
| 209 | + self.run_cmake(cmake_args=["-DEXECUTORCH_TEST_MESSAGE='cli value'"]) |
| 210 | + # If an option is set through cmake, it should NOT be overridable from the CLI. |
| 211 | + self.assert_cmake_cache("EXECUTORCH_TEST_MESSAGE", "set value") |
0 commit comments