@@ -77,27 +77,33 @@ function(_cpp_library_track_find_package package_name)
7777 set (FIND_DEP_CALL "${BASE_CALL} " )
7878 list (JOIN MERGED_COMPONENTS " " MERGED_COMPONENTS_STR)
7979 string (APPEND FIND_DEP_CALL " COMPONENTS ${MERGED_COMPONENTS_STR} " )
80-
81- # Add OPTIONAL_COMPONENTS if present in either old or new
82- set (OPT_COMPONENTS ${FP_OPTIONAL_COMPONENTS} )
83- if (EXISTING_CALL MATCHES "OPTIONAL_COMPONENTS +([^ ]+( +[^ ]+)*)" )
84- string (REGEX REPLACE " +" ";" EXISTING_OPT "${CMAKE_MATCH_1} " )
85- foreach (comp IN LISTS EXISTING_OPT)
86- if (NOT comp IN_LIST OPT_COMPONENTS)
87- list (APPEND OPT_COMPONENTS "${comp} " )
88- endif ()
89- endforeach ()
90- endif ()
91- if (OPT_COMPONENTS)
92- list (JOIN OPT_COMPONENTS " " OPT_COMPONENTS_STR)
93- string (APPEND FIND_DEP_CALL " OPTIONAL_COMPONENTS ${OPT_COMPONENTS_STR} " )
94- endif ()
95-
96- # Preserve CONFIG flag if present in either
97- if (EXISTING_CALL MATCHES "CONFIG" OR FP_CONFIG OR FP_NO_MODULE)
98- if (NOT FIND_DEP_CALL MATCHES "CONFIG" )
99- string (APPEND FIND_DEP_CALL " CONFIG" )
80+ endif ()
81+
82+ # Preserve OPTIONAL_COMPONENTS if present in either old or new
83+ # This must be done outside the MERGED_COMPONENTS block to handle cases
84+ # where there are no regular COMPONENTS but OPTIONAL_COMPONENTS exist
85+ set (OPT_COMPONENTS ${FP_OPTIONAL_COMPONENTS} )
86+ if (EXISTING_CALL MATCHES "OPTIONAL_COMPONENTS +([^ ]+( +[^ ]+)*)" )
87+ string (REGEX REPLACE " +" ";" EXISTING_OPT "${CMAKE_MATCH_1} " )
88+ foreach (comp IN LISTS EXISTING_OPT)
89+ if (NOT comp IN_LIST OPT_COMPONENTS)
90+ list (APPEND OPT_COMPONENTS "${comp} " )
10091 endif ()
92+ endforeach ()
93+ endif ()
94+ if (OPT_COMPONENTS)
95+ # Remove existing OPTIONAL_COMPONENTS to avoid duplication
96+ string (REGEX REPLACE " OPTIONAL_COMPONENTS.*$" "" FIND_DEP_CALL "${FIND_DEP_CALL} " )
97+ list (JOIN OPT_COMPONENTS " " OPT_COMPONENTS_STR)
98+ string (APPEND FIND_DEP_CALL " OPTIONAL_COMPONENTS ${OPT_COMPONENTS_STR} " )
99+ endif ()
100+
101+ # Preserve CONFIG flag if present in either old or new call
102+ # This must be done outside the MERGED_COMPONENTS block to handle cases
103+ # where neither call has COMPONENTS but one has CONFIG
104+ if (EXISTING_CALL MATCHES "CONFIG" OR FP_CONFIG OR FP_NO_MODULE)
105+ if (NOT FIND_DEP_CALL MATCHES "CONFIG" )
106+ string (APPEND FIND_DEP_CALL " CONFIG" )
101107 endif ()
102108 endif ()
103109 endif ()
@@ -175,6 +181,40 @@ else()
175181 message (FATAL_ERROR "✗ FAIL: Expected '${EXPECTED3} ' but got '${DEDUP_CALL} '" )
176182endif ()
177183
184+ # Test: CONFIG flag preserved when neither call has COMPONENTS
185+ message (STATUS "" )
186+ message (STATUS "Test: CONFIG preserved when neither call has COMPONENTS" )
187+
188+ # Clear state
189+ set_property (GLOBAL PROPERTY "_CPP_LIBRARY_TRACKED_DEP_MyPackage" )
190+ set_property (GLOBAL PROPERTY _CPP_LIBRARY_ALL_TRACKED_DEPS "" )
191+
192+ # First call: find_package(MyPackage 1.0 CONFIG)
193+ _cpp_library_track_find_package ("MyPackage" "1.0" "CONFIG" )
194+
195+ get_property (CONFIG_FIRST GLOBAL PROPERTY "_CPP_LIBRARY_TRACKED_DEP_MyPackage" )
196+ message (STATUS "After first call: ${CONFIG_FIRST} " )
197+
198+ # Verify CONFIG was stored
199+ set (EXPECTED_CONFIG1 "MyPackage 1.0 CONFIG" )
200+ if (NOT "${CONFIG_FIRST} " STREQUAL "${EXPECTED_CONFIG1} " )
201+ message (FATAL_ERROR "✗ FAIL: Expected '${EXPECTED_CONFIG1} ' but got '${CONFIG_FIRST} '" )
202+ endif ()
203+
204+ # Second call: find_package(MyPackage 1.0) - no CONFIG flag
205+ _cpp_library_track_find_package ("MyPackage" "1.0" )
206+
207+ get_property (CONFIG_MERGED GLOBAL PROPERTY "_CPP_LIBRARY_TRACKED_DEP_MyPackage" )
208+ message (STATUS "After second call: ${CONFIG_MERGED} " )
209+
210+ # Verify CONFIG was preserved (this was the bug - it would be lost)
211+ set (EXPECTED_CONFIG2 "MyPackage 1.0 CONFIG" )
212+ if ("${CONFIG_MERGED} " STREQUAL "${EXPECTED_CONFIG2} " )
213+ message (STATUS "✓ PASS: CONFIG flag preserved without components" )
214+ else ()
215+ message (FATAL_ERROR "✗ FAIL: Expected '${EXPECTED_CONFIG2} ' but got '${CONFIG_MERGED} '" )
216+ endif ()
217+
178218message (STATUS "" )
179219message (STATUS "===========================================" )
180220message (STATUS "All provider merging tests passed!" )
0 commit comments