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