package_manager.cmake 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. # cu_pkg_get_version
  2. #
  3. # @brief Get the package's version information, the package is installed by component manager.
  4. #
  5. # @param[in] pkg_path the package's path, normally it's ${CMAKE_CURRENT_LIST_DIR}.
  6. #
  7. # @param[out] ver_major the major version of the package
  8. # @param[out] ver_minor the minor version of the package
  9. # @param[out] ver_patch the patch version of the package
  10. function(cu_pkg_get_version pkg_path ver_major ver_minor ver_patch)
  11. set(yml_file "${pkg_path}/idf_component.yml")
  12. if(EXISTS ${yml_file})
  13. file(READ ${yml_file} ver)
  14. string(REGEX MATCH "(^|\n)version: \"?([0-9]+).([0-9]+).([0-9]+)\"?" _ ${ver})
  15. set(${ver_major} ${CMAKE_MATCH_2} PARENT_SCOPE)
  16. set(${ver_minor} ${CMAKE_MATCH_3} PARENT_SCOPE)
  17. set(${ver_patch} ${CMAKE_MATCH_4} PARENT_SCOPE)
  18. else()
  19. message(WARNING " ${yml_file} not exist")
  20. endif()
  21. endfunction()
  22. # cu_pkg_define_version
  23. #
  24. # @brief Add the package's version definitions using format ${NAME}_VER_MAJOR ${NAME}_VER_MINOR ${NAME}_VER_PATCH,
  25. # the ${NAME} will be inferred from package path, and namespace like `espressif__` will be removed if the package download from esp-registry
  26. # eg. espressif__usb_stream and usb_stream will generate same version definitions USB_STREAM_VER_MAJOR ...
  27. #
  28. # @param[in] pkg_path the package's path, normally it's ${CMAKE_CURRENT_LIST_DIR}.
  29. #
  30. macro(cu_pkg_define_version pkg_path)
  31. cu_pkg_get_version(${pkg_path} ver_major ver_minor ver_patch)
  32. get_filename_component(pkg_name ${pkg_path} NAME)
  33. string(FIND ${pkg_name} "__" pkg_name_pos)
  34. if(pkg_name_pos GREATER -1)
  35. math(EXPR pkg_name_pos "${pkg_name_pos} + 2")
  36. string(SUBSTRING ${pkg_name} ${pkg_name_pos} -1 pkg_name)
  37. endif()
  38. string(TOUPPER ${pkg_name} pkg_name)
  39. string(REPLACE "-" "_" pkg_name ${pkg_name})
  40. message(STATUS "${pkg_name}: ${ver_major}.${ver_minor}.${ver_patch}")
  41. list(LENGTH pkg_name_list len)
  42. target_compile_options(${COMPONENT_LIB} PUBLIC
  43. -D${pkg_name}_VER_MAJOR=${ver_major} -D${pkg_name}_VER_MINOR=${ver_minor} -D${pkg_name}_VER_PATCH=${ver_patch})
  44. endmacro()