Skip to content

Commit 2bb567f

Browse files
authored
Create a cmake function to test for conflicting flags (#11460)
### Summary We currently have some flags that cannot be turned on with other flags. We currently work around this be implicitly flipping the other flags. Let's make this violation explicit now and expect the users to exclude the conflicting flag. Example: https://github.com/pytorch/executorch/blob/b63290657e63f8264e7a03b1b2e37a02eea7c687/CMakeLists.txt#L550-L563 ### Test plan ``` $ python -m unittest tools.cmake.common.preset_test.TestPreset ``` cc @larryliu0820
1 parent b632906 commit 2bb567f

File tree

2 files changed

+105
-0
lines changed

2 files changed

+105
-0
lines changed

tools/cmake/common/preset.cmake

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,3 +124,23 @@ function(check_required_options_on)
124124
endforeach()
125125
endif()
126126
endfunction()
127+
128+
129+
# Check if flags conflict with each other.
130+
function(check_conflicting_options_on)
131+
cmake_parse_arguments(
132+
ARG
133+
""
134+
"IF_ON"
135+
"CONFLICTS_WITH"
136+
${ARGN}
137+
)
138+
139+
if(${${ARG_IF_ON}})
140+
foreach(conflict ${ARG_CONFLICTS_WITH})
141+
if(${${conflict}})
142+
message(FATAL_ERROR "Both '${ARG_IF_ON}' and '${conflict}' can't be ON")
143+
endif()
144+
endforeach()
145+
endif()
146+
endfunction()

tools/cmake/common/preset_test.py

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,3 +416,88 @@ def test_check_required_options_on_multiple_required_off(self):
416416
self.run_cmake(
417417
error_contains="Use of 'FEATURE_FLAG' requires 'REQUIRED_OPTION1'"
418418
)
419+
420+
def test_check_conflicting_options_on_if_on_off(self):
421+
"""Test that when IF_ON is OFF, no conflict checks are performed."""
422+
423+
_cmake_lists_txt = """
424+
cmake_minimum_required(VERSION 3.24)
425+
project(test_preset)
426+
include(${PROJECT_SOURCE_DIR}/preset.cmake)
427+
set(FEATURE_FLAG OFF)
428+
set(CONFLICTING_OPTION1 ON)
429+
set(CONFLICTING_OPTION2 ON)
430+
check_conflicting_options_on(
431+
IF_ON
432+
FEATURE_FLAG
433+
CONFLICTS_WITH
434+
CONFLICTING_OPTION1
435+
CONFLICTING_OPTION2
436+
)
437+
"""
438+
self.create_workspace({"CMakeLists.txt": _cmake_lists_txt})
439+
self.run_cmake()
440+
441+
def test_check_conflicting_options_on_no_conflicts(self):
442+
"""Test that when IF_ON is ON but no conflicting options are ON, no error occurs."""
443+
_cmake_lists_txt = """
444+
cmake_minimum_required(VERSION 3.24)
445+
project(test_preset)
446+
include(${PROJECT_SOURCE_DIR}/preset.cmake)
447+
set(FEATURE_FLAG ON)
448+
set(CONFLICTING_OPTION1 OFF)
449+
set(CONFLICTING_OPTION2 OFF)
450+
check_conflicting_options_on(
451+
IF_ON
452+
FEATURE_FLAG
453+
CONFLICTS_WITH
454+
CONFLICTING_OPTION1
455+
CONFLICTING_OPTION2
456+
)
457+
"""
458+
self.create_workspace({"CMakeLists.txt": _cmake_lists_txt})
459+
self.run_cmake()
460+
461+
def test_check_conflicting_options_on_one_conflict(self):
462+
"""Test that when IF_ON is ON and one conflicting option is also ON, a fatal error occurs."""
463+
_cmake_lists_txt = """
464+
cmake_minimum_required(VERSION 3.24)
465+
project(test_preset)
466+
include(${PROJECT_SOURCE_DIR}/preset.cmake)
467+
set(FEATURE_FLAG ON)
468+
set(CONFLICTING_OPTION1 ON)
469+
set(CONFLICTING_OPTION2 OFF)
470+
check_conflicting_options_on(
471+
IF_ON
472+
FEATURE_FLAG
473+
CONFLICTS_WITH
474+
CONFLICTING_OPTION1
475+
CONFLICTING_OPTION2
476+
)
477+
"""
478+
self.create_workspace({"CMakeLists.txt": _cmake_lists_txt})
479+
self.run_cmake(
480+
error_contains="Both 'FEATURE_FLAG' and 'CONFLICTING_OPTION1' can't be ON"
481+
)
482+
483+
def test_check_conflicting_options_on_multiple_conflicts(self):
484+
"""Test that when IF_ON is ON and multiple conflicting options are ON, a fatal error occurs for the first conflict."""
485+
_cmake_lists_txt = """
486+
cmake_minimum_required(VERSION 3.24)
487+
project(test_preset)
488+
include(${PROJECT_SOURCE_DIR}/preset.cmake)
489+
set(FEATURE_FLAG ON)
490+
set(CONFLICTING_OPTION1 ON)
491+
set(CONFLICTING_OPTION2 ON)
492+
check_conflicting_options_on(
493+
IF_ON
494+
FEATURE_FLAG
495+
CONFLICTS_WITH
496+
CONFLICTING_OPTION1
497+
CONFLICTING_OPTION2
498+
)
499+
"""
500+
self.create_workspace({"CMakeLists.txt": _cmake_lists_txt})
501+
self.run_cmake(
502+
error_contains="Both 'FEATURE_FLAG' and 'CONFLICTING_OPTION1' can't be ON"
503+
)

0 commit comments

Comments
 (0)