2
0

protobuf_utils.cmake 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. cmake_minimum_required(VERSION 3.16)
  2. list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/components/spotify/cspot/bell/external/nanopb/extra")
  3. set(TOOLS_DIR "${CMAKE_SOURCE_DIR}/tools" )
  4. set(GENERATED_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/generated")
  5. set(GENERATED_PY_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/py")
  6. set(GENERATED_JS_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/js")
  7. find_package(PythonInterp REQUIRED)
  8. # Function to replace a placeholder in a list
  9. function(replace_in_list INPUT_LIST PLACEHOLDER REPLACEMENT OUTPUT_LIST)
  10. set(TEMP_LIST "")
  11. foreach(ITEM ${${INPUT_LIST}})
  12. string(REPLACE ${PLACEHOLDER} ${REPLACEMENT} ITEM ${ITEM})
  13. list(APPEND TEMP_LIST "${ITEM}")
  14. endforeach()
  15. set(${OUTPUT_LIST} ${TEMP_LIST} PARENT_SCOPE)
  16. endfunction()
  17. function(encode_special_chars INPUT_VAR)
  18. set(ENCODED_STRING "${${INPUT_VAR}}")
  19. # Encoding common special characters. Start with % so that
  20. # we don't collide with encodings if we process that char
  21. # later.
  22. string(REPLACE "%" "%25" ENCODED_STRING "${ENCODED_STRING}")
  23. string(REPLACE ":" "%3A" ENCODED_STRING "${ENCODED_STRING}")
  24. string(REPLACE "-" "%2D" ENCODED_STRING "${ENCODED_STRING}")
  25. string(REPLACE "=" "%3D" ENCODED_STRING "${ENCODED_STRING}")
  26. string(REPLACE "&" "%26" ENCODED_STRING "${ENCODED_STRING}")
  27. string(REPLACE "?" "%3F" ENCODED_STRING "${ENCODED_STRING}")
  28. string(REPLACE "/" "%2F" ENCODED_STRING "${ENCODED_STRING}")
  29. string(REPLACE " " "%20" ENCODED_STRING "${ENCODED_STRING}")
  30. string(REPLACE "!" "%21" ENCODED_STRING "${ENCODED_STRING}")
  31. string(REPLACE "@" "%40" ENCODED_STRING "${ENCODED_STRING}")
  32. string(REPLACE "#" "%23" ENCODED_STRING "${ENCODED_STRING}")
  33. string(REPLACE "$" "%24" ENCODED_STRING "${ENCODED_STRING}")
  34. string(REPLACE "^" "%5E" ENCODED_STRING "${ENCODED_STRING}")
  35. string(REPLACE "*" "%2A" ENCODED_STRING "${ENCODED_STRING}")
  36. string(REPLACE "(" "%28" ENCODED_STRING "${ENCODED_STRING}")
  37. string(REPLACE ")" "%29" ENCODED_STRING "${ENCODED_STRING}")
  38. string(REPLACE "+" "%2B" ENCODED_STRING "${ENCODED_STRING}")
  39. string(REPLACE "{" "%7B" ENCODED_STRING "${ENCODED_STRING}")
  40. string(REPLACE "}" "%7D" ENCODED_STRING "${ENCODED_STRING}")
  41. string(REPLACE "[" "%5B" ENCODED_STRING "${ENCODED_STRING}")
  42. string(REPLACE "]" "%5D" ENCODED_STRING "${ENCODED_STRING}")
  43. string(REPLACE "|" "%7C" ENCODED_STRING "${ENCODED_STRING}")
  44. string(REPLACE "\\" "%5C" ENCODED_STRING "${ENCODED_STRING}")
  45. string(REPLACE ";" "%3B" ENCODED_STRING "${ENCODED_STRING}")
  46. string(REPLACE "'" "%27" ENCODED_STRING "${ENCODED_STRING}")
  47. string(REPLACE "\"" "%22" ENCODED_STRING "${ENCODED_STRING}")
  48. string(REPLACE "<" "%3C" ENCODED_STRING "${ENCODED_STRING}")
  49. string(REPLACE ">" "%3E" ENCODED_STRING "${ENCODED_STRING}")
  50. string(REPLACE "," "%2C" ENCODED_STRING "${ENCODED_STRING}")
  51. string(REPLACE "`" "%60" ENCODED_STRING "${ENCODED_STRING}")
  52. # Add more replacements as needed
  53. # Set the result in the parent scope
  54. set(${INPUT_VAR} "${ENCODED_STRING}" PARENT_SCOPE)
  55. endfunction()
  56. function(array_to_delimited DELIMITER VAR_NAME)
  57. # Initialize the result variable
  58. set(RESULT "")
  59. set(ARR "${${VAR_NAME}}")
  60. # Determine if ARR is a list
  61. list(LENGTH ARR ARR_LENGTH)
  62. if(${ARR_LENGTH} GREATER 0)
  63. # Handle ARR as ITEMS
  64. foreach(ARRAY_ENTRY IN ITEMS ${ARR})
  65. set(RESULT "${RESULT}${ARRAY_ENTRY}${DELIMITER}")
  66. endforeach()
  67. else()
  68. # Handle ARR as a LIST
  69. foreach(ARRAY_ENTRY IN LISTS ${ARR})
  70. set(RESULT "${RESULT}${ARRAY_ENTRY}${DELIMITER}")
  71. endforeach()
  72. endif()
  73. # Remove the trailing delimiter
  74. # string(REGEX REPLACE "${DELIMITER}$" "" RESULT "${RESULT}")
  75. # encode_special_chars(RESULT)
  76. set(${VAR_NAME}_DELIMITED "${RESULT}" PARENT_SCOPE)
  77. endfunction()
  78. function(print_array MSG ARR)
  79. # Determine if ARR is a list
  80. list(LENGTH ARR ARR_LENGTH)
  81. # Check for the optional parameter to print each item on a new line
  82. list(LENGTH ARGN ARG_COUNT)
  83. if(ARG_COUNT EQUAL 1)
  84. # Get the first (and only) item in ARGN
  85. list(GET ARGN 0 ARGN_FIRST_ITEM)
  86. endif()
  87. if(ARG_COUNT EQUAL 1 AND ARGN_FIRST_ITEM STREQUAL "NEWLINE")
  88. if(${ARR_LENGTH} GREATER 0)
  89. message(STATUS "${MSG} [ITEMS]")
  90. foreach(ARRAY_ENTRY IN ITEMS ${ARR})
  91. message(STATUS " - ${ARRAY_ENTRY}")
  92. endforeach()
  93. else()
  94. message(STATUS "${MSG} [LISTS]")
  95. foreach(ARRAY_ENTRY IN LISTS ${ARR})
  96. message(STATUS " - ${ARRAY_ENTRY}")
  97. endforeach()
  98. endif()
  99. else()
  100. # Default behavior: concatenate the message and array entries
  101. set(OUTSTRING "")
  102. if(${ARR_LENGTH} GREATER 0)
  103. foreach(ARRAY_ENTRY IN ITEMS ${ARR})
  104. set(OUTSTRING "${OUTSTRING} ${ARRAY_ENTRY}")
  105. endforeach()
  106. else()
  107. foreach(ARRAY_ENTRY IN LISTS ${ARR})
  108. set(OUTSTRING "${OUTSTRING} ${ARRAY_ENTRY}")
  109. endforeach()
  110. endif()
  111. message(STATUS "${MSG}${OUTSTRING}")
  112. endif()
  113. endfunction()
  114. function(configure_env)
  115. if(CMAKE_HOST_WIN32)
  116. set(PROTODOT_BINARY "${TOOLS_DIR}/protodot/binaries/protodot-windows-amd64.exe" PARENT_SCOPE)
  117. set(CONFIG_FILE "${TOOLS_DIR}/protodot/config-win.json" PARENT_SCOPE)
  118. set(PROTOC_BINARY "${TOOLS_DIR}/protobuf/win64/bin/protoc.exe" PARENT_SCOPE)
  119. set(PROTOBUF_INCLUDE_DIR "${TOOLS_DIR}/protobuf/win64/include/google/protobuf" PARENT_SCOPE)
  120. set(PROTOC_PLUGIN_SUFFIX ".bat" PARENT_SCOPE)
  121. else()
  122. set(PROTODOT_BINARY "${TOOLS_DIR}/protodot/binaries/protodot-linux-amd64" PARENT_SCOPE)
  123. set(CONFIG_FILE "${TOOLS_DIR}/protodot/config.json" PARENT_SCOPE)
  124. set(PROTOC_BINARY "${TOOLS_DIR}/protobuf/linux-x86_64/bin/protoc" PARENT_SCOPE)
  125. set(PROTOBUF_INCLUDE_DIR "${TOOLS_DIR}/protobuf/linux-x86_64/include/google/protobuf" PARENT_SCOPE)
  126. set(PROTOC_PLUGIN_SUFFIX ".py" PARENT_SCOPE)
  127. # else()
  128. # message(FATAL_ERROR "Unsupported operating system")
  129. endif()
  130. endfunction()
  131. set_property(DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
  132. APPEND PROPERTY
  133. ADDITIONAL_MAKE_CLEAN_FILES "${CMAKE_CURRENT_BINARY_DIR}/protobuf"
  134. )
  135. set(PROTO_INCLUDE_DIRECTORIES "${CMAKE_CURRENT_SOURCE_DIR}/proto;${NANOPB_GENERATOR_SOURCE_DIR}/proto" )