Skip to content

Allow options to be set by presets #10767

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 3 commits into from
May 8, 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: 0 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,6 @@ option(EXECUTORCH_BUILD_ARM_BAREMETAL
"Build the Arm Baremetal flow for Cortex-M and Ethos-U" OFF
)

option(EXECUTORCH_BUILD_COREML "Build the Core ML backend" OFF)

option(EXECUTORCH_BUILD_KERNELS_CUSTOM "Build the custom kernels" OFF)

option(EXECUTORCH_BUILD_KERNELS_CUSTOM_AOT "Build the custom ops lib for AOT"
Expand Down
4 changes: 0 additions & 4 deletions tools/cmake/Utils.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,6 @@ function(executorch_print_configuration_summary)
message(STATUS " EXECUTORCH_BUILD_CADENCE : "
"${EXECUTORCH_BUILD_CADENCE}"
)
message(
STATUS
" EXECUTORCH_BUILD_COREML : ${EXECUTORCH_BUILD_COREML}"
)
message(
STATUS
" EXECUTORCH_BUILD_CPUINFO : ${EXECUTORCH_BUILD_CPUINFO}"
Expand Down
14 changes: 14 additions & 0 deletions tools/cmake/common/preset.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ function(announce_configured_options NAME)
endif()
endfunction()


# Print the configured options.
function(print_configured_options)
get_property(_options GLOBAL PROPERTY _announce_configured_options)
Expand All @@ -52,13 +53,15 @@ function(print_configured_options)
message(STATUS "---------------------------")
endfunction()


# Enforce option names to always start with EXECUTORCH.
function(enforce_executorch_option_name NAME)
if(NOT "${NAME}" MATCHES "^EXECUTORCH_")
message(FATAL_ERROR "Option name '${NAME}' must start with EXECUTORCH_")
endif()
endfunction()


# Define an overridable option.
# 1) If the option is already defined in the process, then store that in cache
# 2) If the option is NOT set, then store the default value in cache
Expand All @@ -77,3 +80,14 @@ macro(define_overridable_option NAME DESCRIPTION VALUE_TYPE DEFAULT_VALUE)

announce_configured_options(${NAME})
endmacro()


# Set an overridable option.
macro(set_overridable_option NAME VALUE)
# If the user has explitily set the option, do not override it.
if(DEFINED ${NAME})
return()
endif()

set(${NAME} ${VALUE} CACHE STRING "")
endmacro()
67 changes: 67 additions & 0 deletions tools/cmake/common/preset_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,3 +223,70 @@ def test_define_overridable_option_cli_override_with_set_override(self):
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")

def test_set_overridable_option_before(self):
_cmake_lists_txt = """
cmake_minimum_required(VERSION 3.24)
project(test_preset)
include(${PROJECT_SOURCE_DIR}/preset.cmake)
set_overridable_option(EXECUTORCH_TEST_MESSAGE "from set_overridable_option")
add_subdirectory(build)
"""
_build_cmake_lists_txt = """
define_overridable_option(EXECUTORCH_TEST_MESSAGE "test message" STRING "move fast")
"""
self.create_workspace(
{
"CMakeLists.txt": _cmake_lists_txt,
"build": {
"CMakeLists.txt": _build_cmake_lists_txt,
},
}
)
self.run_cmake()
self.assert_cmake_cache(
"EXECUTORCH_TEST_MESSAGE", "from set_overridable_option", "STRING"
)

def test_set_overridable_option_after(self):
_cmake_lists_txt = """
cmake_minimum_required(VERSION 3.24)
project(test_preset)
include(${PROJECT_SOURCE_DIR}/preset.cmake)
add_subdirectory(build)
set_overridable_option(EXECUTORCH_TEST_MESSAGE "from set_overridable_option")
"""
_build_cmake_lists_txt = """
define_overridable_option(EXECUTORCH_TEST_MESSAGE "test message" STRING "move fast")
"""
self.create_workspace(
{
"CMakeLists.txt": _cmake_lists_txt,
"build": {
"CMakeLists.txt": _build_cmake_lists_txt,
},
}
)
self.run_cmake()
self.assert_cmake_cache("EXECUTORCH_TEST_MESSAGE", "move fast", "STRING")

def test_set_overridable_option_with_cli_override(self):
_cmake_lists_txt = """
cmake_minimum_required(VERSION 3.24)
project(test_preset)
include(${PROJECT_SOURCE_DIR}/preset.cmake)
add_subdirectory(build)
"""
_build_cmake_lists_txt = """
define_overridable_option(EXECUTORCH_TEST_MESSAGE "test message" STRING "move fast")
"""
self.create_workspace(
{
"CMakeLists.txt": _cmake_lists_txt,
"build": {
"CMakeLists.txt": _build_cmake_lists_txt,
},
}
)
self.run_cmake(cmake_args=["-DEXECUTORCH_TEST_MESSAGE='from the cli'"])
self.assert_cmake_cache("EXECUTORCH_TEST_MESSAGE", "from the cli", "STRING")
1 change: 1 addition & 0 deletions tools/cmake/preset/default.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ endif()
# MARK: - Definitions

define_overridable_option(EXECUTORCH_ENABLE_LOGGING "Build with ET_LOG_ENABLED" BOOL ${_is_build_type_debug})
define_overridable_option(EXECUTORCH_BUILD_COREML "Build the Core ML backend" BOOL OFF)
7 changes: 7 additions & 0 deletions tools/cmake/preset/macos-arm64.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.

set_overridable_option(EXECUTORCH_BUILD_COREML ON)
Loading