mirror of
https://github.com/espressif/esp-idf.git
synced 2025-11-26 20:53:11 +00:00
cmake: Add support for test build
This commit is contained in:
@@ -82,14 +82,14 @@ endmacro()
|
||||
# return the path to the component in 'variable'
|
||||
#
|
||||
# Fatal error is printed if the component is not found.
|
||||
function(find_component_path find_name component_paths variable)
|
||||
foreach(path ${component_paths})
|
||||
get_filename_component(name "${path}" NAME)
|
||||
if("${name}" STREQUAL "${find_name}")
|
||||
set("${variable}" "${path}" PARENT_SCOPE)
|
||||
return()
|
||||
endif()
|
||||
endforeach()
|
||||
function(find_component_path find_name components component_paths variable)
|
||||
list(FIND components ${find_name} idx)
|
||||
if(NOT idx EQUAL -1)
|
||||
list(GET component_paths ${idx} path)
|
||||
set("${variable}" "${path}" PARENT_SCOPE)
|
||||
return()
|
||||
else()
|
||||
endif()
|
||||
# TODO: find a way to print the dependency chain that lead to this not-found component
|
||||
message(WARNING "Required component ${find_name} is not found in any of the provided COMPONENT_DIRS")
|
||||
endfunction()
|
||||
@@ -100,10 +100,11 @@ endfunction()
|
||||
#
|
||||
# component_paths contains only unique component names. Directories
|
||||
# earlier in the component_dirs list take precedence.
|
||||
function(components_find_all component_dirs component_paths component_names)
|
||||
function(components_find_all component_dirs component_paths component_names test_component_names)
|
||||
# component_dirs entries can be files or lists of files
|
||||
set(paths "")
|
||||
set(names "")
|
||||
set(test_names "")
|
||||
|
||||
# start by expanding the component_dirs list with all subdirectories
|
||||
foreach(dir ${component_dirs})
|
||||
@@ -123,10 +124,15 @@ function(components_find_all component_dirs component_paths component_names)
|
||||
get_filename_component(component "${component}" DIRECTORY)
|
||||
get_filename_component(name "${component}" NAME)
|
||||
if(NOT name IN_LIST names)
|
||||
set(names "${names};${name}")
|
||||
set(paths "${paths};${component}")
|
||||
endif()
|
||||
list(APPEND names "${name}")
|
||||
list(APPEND paths "${component}")
|
||||
|
||||
# Look for test component directory
|
||||
file(GLOB test "${component}/test/CMakeLists.txt")
|
||||
if(test)
|
||||
list(APPEND test_names "${name}")
|
||||
endif()
|
||||
endif()
|
||||
else() # no CMakeLists.txt file
|
||||
# test for legacy component.mk and warn
|
||||
file(GLOB legacy_component "${dir}/component.mk")
|
||||
@@ -136,25 +142,26 @@ function(components_find_all component_dirs component_paths component_names)
|
||||
"Component will be skipped.")
|
||||
endif()
|
||||
endif()
|
||||
|
||||
endforeach()
|
||||
|
||||
set(${component_paths} ${paths} PARENT_SCOPE)
|
||||
set(${component_names} ${names} PARENT_SCOPE)
|
||||
set(${test_component_names} ${test_names} PARENT_SCOPE)
|
||||
endfunction()
|
||||
|
||||
|
||||
# expand_component_requirements: Recursively expand a component's requirements,
|
||||
# setting global properties BUILD_COMPONENTS & BUILD_COMPONENT_PATHS and
|
||||
# also invoking the components to call register_component() above,
|
||||
# which will add per-component global properties with dependencies, etc.
|
||||
function(expand_component_requirements component)
|
||||
get_property(seen_components GLOBAL PROPERTY SEEN_COMPONENTS)
|
||||
if(${component} IN_LIST seen_components)
|
||||
if(component IN_LIST seen_components)
|
||||
return() # already added, or in process of adding, this component
|
||||
endif()
|
||||
set_property(GLOBAL APPEND PROPERTY SEEN_COMPONENTS ${component})
|
||||
|
||||
find_component_path("${component}" "${ALL_COMPONENT_PATHS}" component_path)
|
||||
find_component_path("${component}" "${ALL_COMPONENTS}" "${ALL_COMPONENT_PATHS}" component_path)
|
||||
debug("Expanding dependencies of ${component} @ ${component_path}")
|
||||
if(NOT component_path)
|
||||
set_property(GLOBAL APPEND PROPERTY COMPONENTS_NOT_FOUND ${component})
|
||||
@@ -176,27 +183,99 @@ function(expand_component_requirements component)
|
||||
expand_component_requirements(${req})
|
||||
endforeach()
|
||||
|
||||
list(FIND TEST_COMPONENTS ${component} idx)
|
||||
|
||||
if(NOT idx EQUAL -1)
|
||||
list(GET TEST_COMPONENTS ${idx} test_component)
|
||||
list(GET TEST_COMPONENT_PATHS ${idx} test_component_path)
|
||||
set_property(GLOBAL APPEND PROPERTY BUILD_TEST_COMPONENTS ${test_component})
|
||||
set_property(GLOBAL APPEND PROPERTY BUILD_TEST_COMPONENT_PATHS ${test_component_path})
|
||||
endif()
|
||||
|
||||
# Now append this component to the full list (after its dependencies)
|
||||
set_property(GLOBAL APPEND PROPERTY BUILD_COMPONENT_PATHS ${component_path})
|
||||
set_property(GLOBAL APPEND PROPERTY BUILD_COMPONENTS ${component})
|
||||
endfunction()
|
||||
|
||||
# filter_components_list: Filter the components included in the build
|
||||
# as specified by the user. Or, in the case of unit testing, filter out
|
||||
# the test components to be built.
|
||||
macro(filter_components_list)
|
||||
spaces2list(COMPONENTS)
|
||||
spaces2list(EXCLUDE_COMPONENTS)
|
||||
spaces2list(TEST_COMPONENTS)
|
||||
spaces2list(TEST_EXCLUDE_COMPONENTS)
|
||||
|
||||
list(LENGTH ALL_COMPONENTS all_components_length)
|
||||
math(EXPR all_components_length "${all_components_length} - 1")
|
||||
|
||||
foreach(component_idx RANGE 0 ${all_components_length})
|
||||
list(GET ALL_COMPONENTS ${component_idx} component)
|
||||
list(GET ALL_COMPONENT_PATHS ${component_idx} component_path)
|
||||
|
||||
if(COMPONENTS)
|
||||
if(${component} IN_LIST COMPONENTS)
|
||||
set(add_component 1)
|
||||
else()
|
||||
set(add_component 0)
|
||||
endif()
|
||||
else()
|
||||
set(add_component 1)
|
||||
|
||||
endif()
|
||||
|
||||
if(NOT ${component} IN_LIST EXCLUDE_COMPONENTS AND add_component EQUAL 1)
|
||||
list(APPEND components ${component})
|
||||
list(APPEND component_paths ${component_path})
|
||||
|
||||
if(TESTS_ALL EQUAL 1 OR TEST_COMPONENTS)
|
||||
if(NOT TESTS_ALL EQUAL 1 AND TEST_COMPONENTS)
|
||||
if(${component} IN_LIST TEST_COMPONENTS)
|
||||
set(add_test_component 1)
|
||||
else()
|
||||
set(add_test_component 0)
|
||||
endif()
|
||||
else()
|
||||
set(add_test_component 1)
|
||||
endif()
|
||||
|
||||
if(${component} IN_LIST ALL_TEST_COMPONENTS)
|
||||
if(NOT ${component} IN_LIST TEST_EXCLUDE_COMPONENTS AND add_test_component EQUAL 1)
|
||||
list(APPEND test_components ${component}_test)
|
||||
list(APPEND test_component_paths ${component_path}/test)
|
||||
|
||||
list(APPEND components ${component}_test)
|
||||
list(APPEND component_paths ${component_path}/test)
|
||||
endif()
|
||||
endif()
|
||||
endif()
|
||||
endif()
|
||||
endforeach()
|
||||
|
||||
set(COMPONENTS ${components})
|
||||
|
||||
set(TEST_COMPONENTS ${test_components})
|
||||
set(TEST_COMPONENT_PATHS ${test_component_paths})
|
||||
|
||||
list(APPEND ALL_COMPONENTS "${TEST_COMPONENTS}")
|
||||
list(APPEND ALL_COMPONENT_PATHS "${TEST_COMPONENT_PATHS}")
|
||||
endmacro()
|
||||
|
||||
# Main functionality goes here
|
||||
|
||||
# Find every available component in COMPONENT_DIRS, save as ALL_COMPONENT_PATHS and ALL_COMPONENTS
|
||||
components_find_all("${COMPONENT_DIRS}" ALL_COMPONENT_PATHS ALL_COMPONENTS)
|
||||
components_find_all("${COMPONENT_DIRS}" ALL_COMPONENT_PATHS ALL_COMPONENTS ALL_TEST_COMPONENTS)
|
||||
|
||||
if(NOT COMPONENTS)
|
||||
set(COMPONENTS "${ALL_COMPONENTS}")
|
||||
endif()
|
||||
spaces2list(COMPONENTS)
|
||||
filter_components_list()
|
||||
|
||||
debug("ALL_COMPONENT_PATHS ${ALL_COMPONENT_PATHS}")
|
||||
debug("ALL_COMPONENTS ${ALL_COMPONENTS}")
|
||||
debug("ALL_TEST_COMPONENTS ${ALL_TEST_COMPONENTS}")
|
||||
|
||||
set_property(GLOBAL PROPERTY SEEN_COMPONENTS "") # anti-infinite-recursion
|
||||
set_property(GLOBAL PROPERTY BUILD_COMPONENTS "")
|
||||
set_property(GLOBAL PROPERTY BUILD_COMPONENT_PATHS "")
|
||||
set_property(GLOBAL PROPERTY BUILD_TEST_COMPONENTS "")
|
||||
set_property(GLOBAL PROPERTY BUILD_TEST_COMPONENT_PATHS "")
|
||||
set_property(GLOBAL PROPERTY COMPONENTS_NOT_FOUND "")
|
||||
|
||||
# Indicate that the component CMakeLists.txt is being included in the early expansion phase of the build,
|
||||
@@ -210,6 +289,8 @@ unset(CMAKE_BUILD_EARLY_EXPANSION)
|
||||
|
||||
get_property(build_components GLOBAL PROPERTY BUILD_COMPONENTS)
|
||||
get_property(build_component_paths GLOBAL PROPERTY BUILD_COMPONENT_PATHS)
|
||||
get_property(build_test_components GLOBAL PROPERTY BUILD_TEST_COMPONENTS)
|
||||
get_property(build_test_component_paths GLOBAL PROPERTY BUILD_TEST_COMPONENT_PATHS)
|
||||
get_property(not_found GLOBAL PROPERTY COMPONENTS_NOT_FOUND)
|
||||
|
||||
debug("components in build: ${build_components}")
|
||||
@@ -223,6 +304,8 @@ endfunction()
|
||||
file(WRITE "${DEPENDENCIES_FILE}.tmp" "# Component requirements generated by expand_requirements.cmake\n\n")
|
||||
line("set(BUILD_COMPONENTS ${build_components})")
|
||||
line("set(BUILD_COMPONENT_PATHS ${build_component_paths})")
|
||||
line("set(BUILD_TEST_COMPONENTS ${build_test_components})")
|
||||
line("set(BUILD_TEST_COMPONENT_PATHS ${build_test_component_paths})")
|
||||
line("")
|
||||
|
||||
line("# get_component_requirements: Generated function to read the dependencies of a given component.")
|
||||
|
||||
Reference in New Issue
Block a user