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