Skip to content
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
20 changes: 20 additions & 0 deletions tools/cmake/common/preset.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,23 @@ function(check_required_options_on)
endforeach()
endif()
endfunction()


# Check if flags conflict with each other.
function(check_conflicting_options_on)
cmake_parse_arguments(
ARG
""
"IF_ON"
"CONFLICTS_WITH"
${ARGN}
)

if(${${ARG_IF_ON}})
foreach(conflict ${ARG_CONFLICTS_WITH})
if(${${conflict}})
message(FATAL_ERROR "Both '${ARG_IF_ON}' and '${conflict}' can't be ON")
endif()
endforeach()
endif()
endfunction()
85 changes: 85 additions & 0 deletions tools/cmake/common/preset_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -416,3 +416,88 @@ def test_check_required_options_on_multiple_required_off(self):
self.run_cmake(
error_contains="Use of 'FEATURE_FLAG' requires 'REQUIRED_OPTION1'"
)

def test_check_conflicting_options_on_if_on_off(self):
"""Test that when IF_ON is OFF, no conflict checks are performed."""

_cmake_lists_txt = """
cmake_minimum_required(VERSION 3.24)
project(test_preset)
include(${PROJECT_SOURCE_DIR}/preset.cmake)
set(FEATURE_FLAG OFF)
set(CONFLICTING_OPTION1 ON)
set(CONFLICTING_OPTION2 ON)
check_conflicting_options_on(
IF_ON
FEATURE_FLAG
CONFLICTS_WITH
CONFLICTING_OPTION1
CONFLICTING_OPTION2
)
"""
self.create_workspace({"CMakeLists.txt": _cmake_lists_txt})
self.run_cmake()

def test_check_conflicting_options_on_no_conflicts(self):
"""Test that when IF_ON is ON but no conflicting options are ON, no error occurs."""
_cmake_lists_txt = """
cmake_minimum_required(VERSION 3.24)
project(test_preset)
include(${PROJECT_SOURCE_DIR}/preset.cmake)
set(FEATURE_FLAG ON)
set(CONFLICTING_OPTION1 OFF)
set(CONFLICTING_OPTION2 OFF)
check_conflicting_options_on(
IF_ON
FEATURE_FLAG
CONFLICTS_WITH
CONFLICTING_OPTION1
CONFLICTING_OPTION2
)
"""
self.create_workspace({"CMakeLists.txt": _cmake_lists_txt})
self.run_cmake()

def test_check_conflicting_options_on_one_conflict(self):
"""Test that when IF_ON is ON and one conflicting option is also ON, a fatal error occurs."""
_cmake_lists_txt = """
cmake_minimum_required(VERSION 3.24)
project(test_preset)
include(${PROJECT_SOURCE_DIR}/preset.cmake)
set(FEATURE_FLAG ON)
set(CONFLICTING_OPTION1 ON)
set(CONFLICTING_OPTION2 OFF)
check_conflicting_options_on(
IF_ON
FEATURE_FLAG
CONFLICTS_WITH
CONFLICTING_OPTION1
CONFLICTING_OPTION2
)
"""
self.create_workspace({"CMakeLists.txt": _cmake_lists_txt})
self.run_cmake(
error_contains="Both 'FEATURE_FLAG' and 'CONFLICTING_OPTION1' can't be ON"
)

def test_check_conflicting_options_on_multiple_conflicts(self):
"""Test that when IF_ON is ON and multiple conflicting options are ON, a fatal error occurs for the first conflict."""
_cmake_lists_txt = """
cmake_minimum_required(VERSION 3.24)
project(test_preset)
include(${PROJECT_SOURCE_DIR}/preset.cmake)
set(FEATURE_FLAG ON)
set(CONFLICTING_OPTION1 ON)
set(CONFLICTING_OPTION2 ON)
check_conflicting_options_on(
IF_ON
FEATURE_FLAG
CONFLICTS_WITH
CONFLICTING_OPTION1
CONFLICTING_OPTION2
)
"""
self.create_workspace({"CMakeLists.txt": _cmake_lists_txt})
self.run_cmake(
error_contains="Both 'FEATURE_FLAG' and 'CONFLICTING_OPTION1' can't be ON"
)
Loading