# This cmake file provides functionality to import additional out-of-tree, OoT
# CMakeLists.txt and Kconfig files into Zephyr build system.
# It uses -DZEPHYR_MODULES=<oot-path-to-module>[;<additional-oot-module(s)>]
# given to CMake for a list of folders to search.
# It looks for: <oot-module>/zephyr/module.yml or
#               <oot-module>/zephyr/CMakeLists.txt
# to load the oot-module into Zephyr build system.
# If west is available, it uses `west list` to obtain a list of projects to
# search for zephyr/module.yml

if(ZEPHYR_MODULES)
  set(west_project_list ${ZEPHYR_MODULES})
elseif(WEST)
  ## Use `west list` to fetch all west handled projects.
  execute_process(
    COMMAND
    ${WEST} list --format={posixpath}
    OUTPUT_VARIABLE
    west_project_output
  )
  string(REGEX REPLACE "[\r\n]+" ";" west_project_list "${west_project_output}")
endif()

if(ZEPHYR_EXTRA_MODULES)
  list(APPEND west_project_list ${ZEPHYR_EXTRA_MODULES})
endif()

# Clear the Kconfig.modules generated file in case modules has been removed.
# The Kconfig.modules contains a list of additional Kconfig files to be sourced
# based upon <module>/zephyr/module.yml files.
file(WRITE ${PROJECT_BINARY_DIR}/Kconfig.modules
  "# NOTE: THIS FILE IS AUTOGENERATED BY CMAKE\n"
  )
# For each west managed project, determine if the project is a zephyr module.
foreach(module ${west_project_list})
  set(cmake_subdir "zephyr")
  if(${ZEPHYR_BASE} STREQUAL ${module})
    # Ignore Zephyr project to avoid potential invalid looping
  elseif(EXISTS "${module}/zephyr/module.yml")
    set(kconfig_osource "osource \"${module}/zephyr/Kconfig\"\n")
    get_filename_component(module_name ${module} NAME)
    execute_process(
      COMMAND
      ${PYTHON_EXECUTABLE}
      ${ZEPHYR_BASE}/scripts/yaml_to_cmake.py
      -i "${module}/zephyr/module.yml"
      -o "${CMAKE_CURRENT_BINARY_DIR}/zephyr_module_${module_name}.txt"
      -s "build"
      )
    file(
      STRINGS
      "${CMAKE_CURRENT_BINARY_DIR}/zephyr_module_${module_name}.txt"
      zephyr_module
      )

    foreach(key_value ${zephyr_module})
      if(${key_value} MATCHES "^cmake=")
        string(REGEX REPLACE "^cmake=" "" cmake_subdir ${key_value})
      elseif(${key_value} MATCHES "^kconfig=")
        string(
          REGEX REPLACE
          "^kconfig="
          "osource \"${module}/"
          kconfig_osource
          ${key_value} "\"\n"
          )
      endif()
    endforeach()

    list(APPEND ZEPHYR_MODULES_NAME ${module_name})
    list(APPEND ZEPHYR_MODULES_DIR ${module}/${cmake_subdir})
    file(APPEND ${PROJECT_BINARY_DIR}/Kconfig.modules ${kconfig_osource})
  elseif(EXISTS "${module}/${cmake_subdir}/CMakeLists.txt")
    set(kconfig_osource "osource \"${module}/zephyr/Kconfig\"\n")
    get_filename_component(module_name ${module} NAME)
    list(APPEND ZEPHYR_MODULES_NAME ${module_name})
    list(APPEND ZEPHYR_MODULES_DIR ${module}/${cmake_subdir})
    file(APPEND ${PROJECT_BINARY_DIR}/Kconfig.modules ${kconfig_osource})
  else()
    # Not a Zephyr module, ignore.
  endif()

endforeach()

