Skip to content

Guard against existing cache when configuring options #10984

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion tools/cmake/common/preset.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ macro(define_overridable_option NAME DESCRIPTION VALUE_TYPE DEFAULT_VALUE)
message(FATAL_ERROR "Invalid option (${NAME}) value type '${VALUE_TYPE}', must be either STRING or BOOL")
endif()

if(DEFINED ${NAME})
if(DEFINED ${NAME} AND NOT DEFINED CACHE{${NAME}})
set(${NAME} ${${NAME}} CACHE ${VALUE_TYPE} ${DESCRIPTION} FORCE)
else()
set(${NAME} ${DEFAULT_VALUE} CACHE ${VALUE_TYPE} ${DESCRIPTION})
Expand Down
14 changes: 10 additions & 4 deletions tools/cmake/common/preset_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

from tools.cmake.common import CMakeTestCase, TESTABLE_CMAKE_FILES

from . import _list_cmake_cache


class TestPreset(CMakeTestCase):

Expand Down Expand Up @@ -201,12 +203,11 @@ def test_define_overridable_option_set_override_after_with_cache(self):
# Setting the value after should not affect the cache.
self.assert_cmake_cache("EXECUTORCH_TEST_MESSAGE", "default value", "STRING")

def test_define_overridable_option_cli_override_with_set_override(self):
def test_define_overridable_option_override_existing_cache_with_cli(self):
_cmake_lists_txt = """
cmake_minimum_required(VERSION 3.24)
project(test_preset)
include(${PROJECT_SOURCE_DIR}/preset.cmake)
set(EXECUTORCH_TEST_MESSAGE "set value")
add_subdirectory(example)
"""
_example_cmake_lists_txt = """
Expand All @@ -220,9 +221,14 @@ def test_define_overridable_option_cli_override_with_set_override(self):
},
}
)
self.run_cmake()
self.assert_cmake_cache("EXECUTORCH_TEST_MESSAGE", "default value", "STRING")

# Since we cache the get operations, clear it so that it's read again for tests.
_list_cmake_cache.cache_clear()

self.run_cmake(cmake_args=["-DEXECUTORCH_TEST_MESSAGE='cli value'"])
# If an option is set through cmake, it should NOT be overridable from the CLI.
self.assert_cmake_cache("EXECUTORCH_TEST_MESSAGE", "set value", "STRING")
self.assert_cmake_cache("EXECUTORCH_TEST_MESSAGE", "cli value", "STRING")

def test_set_overridable_option_before(self):
_cmake_lists_txt = """
Expand Down
Loading