update_file_lists.sh 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. #!/bin/bash
  2. # This script copies source file lists from src/Makefile.am to cmake files.
  3. get_variable_value() {
  4. local FILENAME=$1
  5. local VARNAME=$2
  6. awk "
  7. BEGIN { start = 0; }
  8. /^$VARNAME =/ { start = 1; }
  9. { if (start) { print \$0; } }
  10. /\\\\\$/ { next; }
  11. { start = 0; }
  12. " $FILENAME \
  13. | sed "s/^$VARNAME =//" \
  14. | sed "s/[ \\]//g" \
  15. | grep -v "^\\$" \
  16. | grep -v "^$" \
  17. | LC_ALL=C sort | uniq
  18. }
  19. get_header_files() {
  20. get_variable_value $@ | grep '\.h$'
  21. }
  22. get_source_files() {
  23. get_variable_value $@ | grep "cc$\|inc$"
  24. }
  25. get_proto_files_blacklisted() {
  26. get_proto_files $@ | sed '/^google\/protobuf\/unittest_enormous_descriptor.proto$/d'
  27. }
  28. get_proto_files() {
  29. get_variable_value $@ | grep "pb.cc$" | sed "s/pb.cc/proto/"
  30. }
  31. sort_files() {
  32. for FILE in $@; do
  33. echo $FILE
  34. done | LC_ALL=C sort | uniq
  35. }
  36. MAKEFILE=src/Makefile.am
  37. [ -f "$MAKEFILE" ] || {
  38. echo "Cannot find: $MAKEFILE"
  39. exit 1
  40. }
  41. # Extract file lists from src/Makefile.am
  42. GZHEADERS=$(get_variable_value $MAKEFILE GZHEADERS)
  43. HEADERS=$(get_variable_value $MAKEFILE nobase_include_HEADERS)
  44. PUBLIC_HEADERS=$(sort_files $GZHEADERS $HEADERS)
  45. LIBPROTOBUF_LITE_SOURCES=$(get_source_files $MAKEFILE libprotobuf_lite_la_SOURCES)
  46. LIBPROTOBUF_SOURCES=$(get_source_files $MAKEFILE libprotobuf_la_SOURCES)
  47. LIBPROTOC_SOURCES=$(get_source_files $MAKEFILE libprotoc_la_SOURCES)
  48. LIBPROTOC_HEADERS=$(get_header_files $MAKEFILE libprotoc_la_SOURCES)
  49. LITE_PROTOS=$(get_proto_files $MAKEFILE protoc_lite_outputs)
  50. PROTOS=$(get_proto_files $MAKEFILE protoc_outputs)
  51. PROTOS_BLACKLISTED=$(get_proto_files_blacklisted $MAKEFILE protoc_outputs)
  52. WKT_PROTOS=$(get_variable_value $MAKEFILE nobase_dist_proto_DATA)
  53. COMMON_TEST_SOURCES=$(get_source_files $MAKEFILE COMMON_TEST_SOURCES)
  54. COMMON_LITE_TEST_SOURCES=$(get_source_files $MAKEFILE COMMON_LITE_TEST_SOURCES)
  55. TEST_SOURCES=$(get_source_files $MAKEFILE protobuf_test_SOURCES)
  56. NON_MSVC_TEST_SOURCES=$(get_source_files $MAKEFILE NON_MSVC_TEST_SOURCES)
  57. LITE_TEST_SOURCES=$(get_source_files $MAKEFILE protobuf_lite_test_SOURCES)
  58. LITE_ARENA_TEST_SOURCES=$(get_source_files $MAKEFILE protobuf_lite_arena_test_SOURCES)
  59. TEST_PLUGIN_SOURCES=$(get_source_files $MAKEFILE test_plugin_SOURCES)
  60. ################################################################################
  61. # Update cmake files.
  62. ################################################################################
  63. CMAKE_DIR=cmake
  64. EXTRACT_INCLUDES_BAT=cmake/extract_includes.bat.in
  65. [ -d "$CMAKE_DIR" ] || {
  66. echo "Cannot find: $CMAKE_DIR"
  67. exit 1
  68. }
  69. [ -f "$EXTRACT_INCLUDES_BAT" ] || {
  70. echo "Cannot find: $EXTRACT_INCLUDES_BAT"
  71. exit 1
  72. }
  73. set_cmake_value() {
  74. local FILENAME=$1
  75. local VARNAME=$2
  76. local PREFIX=$3
  77. shift
  78. shift
  79. shift
  80. awk -v values="$*" -v prefix="$PREFIX" "
  81. BEGIN { start = 0; }
  82. /^set\\($VARNAME/ {
  83. start = 1;
  84. print \$0;
  85. len = split(values, vlist, \" \");
  86. for (i = 1; i <= len; ++i) {
  87. printf(\" %s%s\\n\", prefix, vlist[i]);
  88. }
  89. next;
  90. }
  91. start && /^\\)/ {
  92. start = 0;
  93. }
  94. !start {
  95. print \$0;
  96. }
  97. " $FILENAME > /tmp/$$
  98. cp /tmp/$$ $FILENAME
  99. }
  100. # Replace file lists in cmake files.
  101. CMAKE_PREFIX="\${protobuf_source_dir}/src/"
  102. set_cmake_value $CMAKE_DIR/libprotobuf-lite.cmake libprotobuf_lite_files $CMAKE_PREFIX $LIBPROTOBUF_LITE_SOURCES
  103. set_cmake_value $CMAKE_DIR/libprotobuf.cmake libprotobuf_files $CMAKE_PREFIX $LIBPROTOBUF_SOURCES
  104. set_cmake_value $CMAKE_DIR/libprotoc.cmake libprotoc_files $CMAKE_PREFIX $LIBPROTOC_SOURCES
  105. set_cmake_value $CMAKE_DIR/libprotoc.cmake libprotoc_headers $CMAKE_PREFIX $LIBPROTOC_HEADERS
  106. set_cmake_value $CMAKE_DIR/tests.cmake lite_test_protos "" $LITE_PROTOS
  107. set_cmake_value $CMAKE_DIR/tests.cmake tests_protos "" $PROTOS_BLACKLISTED
  108. set_cmake_value $CMAKE_DIR/tests.cmake common_test_files $CMAKE_PREFIX $COMMON_TEST_SOURCES
  109. set_cmake_value $CMAKE_DIR/tests.cmake common_lite_test_files $CMAKE_PREFIX $COMMON_LITE_TEST_SOURCES
  110. set_cmake_value $CMAKE_DIR/tests.cmake tests_files $CMAKE_PREFIX $TEST_SOURCES
  111. set_cmake_value $CMAKE_DIR/tests.cmake non_msvc_tests_files $CMAKE_PREFIX $NON_MSVC_TEST_SOURCES
  112. set_cmake_value $CMAKE_DIR/tests.cmake lite_test_files $CMAKE_PREFIX $LITE_TEST_SOURCES
  113. set_cmake_value $CMAKE_DIR/tests.cmake lite_arena_test_files $CMAKE_PREFIX $LITE_ARENA_TEST_SOURCES
  114. # Generate extract_includes.bat
  115. echo "mkdir include" > $EXTRACT_INCLUDES_BAT
  116. for INCLUDE in $PUBLIC_HEADERS $WKT_PROTOS; do
  117. INCLUDE_DIR=$(dirname "$INCLUDE")
  118. while [ ! "$INCLUDE_DIR" = "." ]; do
  119. echo "mkdir include\\${INCLUDE_DIR//\//\\}"
  120. INCLUDE_DIR=$(dirname "$INCLUDE_DIR")
  121. done
  122. done | sort | uniq >> $EXTRACT_INCLUDES_BAT
  123. for INCLUDE in $PUBLIC_HEADERS $WKT_PROTOS; do
  124. WINPATH=${INCLUDE//\//\\}
  125. echo "copy \"\${PROTOBUF_SOURCE_WIN32_PATH}\\..\\src\\$WINPATH\" include\\$WINPATH" >> $EXTRACT_INCLUDES_BAT
  126. done
  127. ################################################################################
  128. # Update bazel BUILD files.
  129. ################################################################################
  130. set_bazel_value() {
  131. local FILENAME=$1
  132. local VARNAME=$2
  133. local PREFIX=$3
  134. shift
  135. shift
  136. shift
  137. awk -v values="$*" -v prefix="$PREFIX" "
  138. BEGIN { start = 0; }
  139. /# AUTOGEN\\($VARNAME\\)/ {
  140. start = 1;
  141. print \$0;
  142. # replace \$0 with indent.
  143. sub(/#.*/, \"\", \$0)
  144. len = split(values, vlist, \" \");
  145. for (i = 1; i <= len; ++i) {
  146. printf(\"%s\\\"%s%s\\\",\n\", \$0, prefix, vlist[i]);
  147. }
  148. next;
  149. }
  150. start && /\]/ {
  151. start = 0
  152. }
  153. !start {
  154. print \$0;
  155. }
  156. " $FILENAME > /tmp/$$
  157. cp /tmp/$$ $FILENAME
  158. }
  159. BAZEL_BUILD=./BUILD
  160. BAZEL_PREFIX="src/"
  161. if [ -f "$BAZEL_BUILD" ]; then
  162. set_bazel_value $BAZEL_BUILD protobuf_lite_srcs $BAZEL_PREFIX $LIBPROTOBUF_LITE_SOURCES
  163. set_bazel_value $BAZEL_BUILD protobuf_srcs $BAZEL_PREFIX $LIBPROTOBUF_SOURCES
  164. set_bazel_value $BAZEL_BUILD protoc_lib_srcs $BAZEL_PREFIX $LIBPROTOC_SOURCES
  165. set_bazel_value $BAZEL_BUILD lite_test_protos "" $LITE_PROTOS
  166. set_bazel_value $BAZEL_BUILD well_known_protos "" $WKT_PROTOS
  167. set_bazel_value $BAZEL_BUILD test_protos "" $PROTOS
  168. set_bazel_value $BAZEL_BUILD common_test_srcs $BAZEL_PREFIX $COMMON_TEST_SOURCES
  169. set_bazel_value $BAZEL_BUILD test_srcs $BAZEL_PREFIX $TEST_SOURCES
  170. set_bazel_value $BAZEL_BUILD non_msvc_test_srcs $BAZEL_PREFIX $NON_MSVC_TEST_SOURCES
  171. set_bazel_value $BAZEL_BUILD test_plugin_srcs $BAZEL_PREFIX $TEST_PLUGIN_SOURCES
  172. else
  173. echo "Skipped BUILD file update."
  174. fi