blob: d76a1610a3d88d62e42809b1c42fb47f7da238f9 [file] [log] [blame]
Feng Xiao4333edb2015-05-31 02:28:34 -07001#!/bin/sh
2
3# This script copies source file lists from src/Makefile.am to cmake files.
4
5get_variable_value() {
Jisi Liud19604f2015-06-17 17:37:58 -07006 local FILENAME=$1
7 local VARNAME=$2
Feng Xiao4333edb2015-05-31 02:28:34 -07008 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
Jisi Liud19604f2015-06-17 17:37:58 -070022get_header_files() {
23 get_variable_value $@ | grep '\.h$'
24}
25
Feng Xiao4333edb2015-05-31 02:28:34 -070026get_source_files() {
27 get_variable_value $@ | grep "cc$"
28}
29
Bo Yangff7bdad2015-08-23 10:45:14 -070030get_proto_files_blacklisted() {
31 get_proto_files $@ | sed '/^google\/protobuf\/unittest_enormous_descriptor.proto$/d'
32}
33
Feng Xiao4333edb2015-05-31 02:28:34 -070034get_proto_files() {
35 get_variable_value $@ | grep "pb.cc$" | sed "s/pb.cc/proto/"
36}
37
Jisi Liud19604f2015-06-17 17:37:58 -070038sort_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)
58LITE_PROTOS=$(get_proto_files $MAKEFILE protoc_lite_outputs)
Jisi Liud19604f2015-06-17 17:37:58 -070059PROTOS=$(get_proto_files $MAKEFILE protoc_outputs)
Bo Yangff7bdad2015-08-23 10:45:14 -070060PROTOS_BLACKLISTED=$(get_proto_files_blacklisted $MAKEFILE protoc_outputs)
Jisi Liud19604f2015-06-17 17:37:58 -070061WKT_PROTOS=$(get_variable_value $MAKEFILE nobase_dist_proto_DATA)
62COMMON_TEST_SOURCES=$(get_source_files $MAKEFILE COMMON_TEST_SOURCES)
Feng Xiaob17ec3c2015-08-23 17:50:38 -070063COMMON_LITE_TEST_SOURCES=$(get_source_files $MAKEFILE COMMON_LITE_TEST_SOURCES)
Jisi Liud19604f2015-06-17 17:37:58 -070064TEST_SOURCES=$(get_source_files $MAKEFILE protobuf_test_SOURCES)
65LITE_TEST_SOURCES=$(get_source_files $MAKEFILE protobuf_lite_test_SOURCES)
Feng Xiaob17ec3c2015-08-23 17:50:38 -070066LITE_ARENA_TEST_SOURCES=$(get_source_files $MAKEFILE protobuf_lite_arena_test_SOURCES)
Jisi Liu7a0c4312015-06-18 16:45:27 -070067TEST_PLUGIN_SOURCES=$(get_source_files $MAKEFILE test_plugin_SOURCES)
Jisi Liud19604f2015-06-17 17:37:58 -070068
69################################################################################
70# Update cmake files.
71################################################################################
72
73CMAKE_DIR=cmake
74EXTRACT_INCLUDES_BAT=cmake/extract_includes.bat.in
75[ -d "$CMAKE_DIR" ] || {
76 echo "Cannot find: $CMAKE_DIR"
77 exit 1
78}
79
80[ -f "$EXTRACT_INCLUDES_BAT" ] || {
81 echo "Cannot find: $EXTRACT_INCLUDES_BAT"
82 exit 1
83}
84
85set_cmake_value() {
86 local FILENAME=$1
87 local VARNAME=$2
88 local PREFIX=$3
Feng Xiao4333edb2015-05-31 02:28:34 -070089 shift
90 shift
91 shift
92 awk -v values="$*" -v prefix="$PREFIX" "
93 BEGIN { start = 0; }
94 /^set\\($VARNAME/ {
95 start = 1;
96 print \$0;
Feng Xiaodffd5422015-06-05 17:59:09 -070097 len = split(values, vlist, \" \");
98 for (i = 1; i <= len; ++i) {
Feng Xiao4333edb2015-05-31 02:28:34 -070099 printf(\" %s%s\\n\", prefix, vlist[i]);
100 }
101 next;
102 }
103 start && /^\\)/ {
104 start = 0;
105 }
106 !start {
107 print \$0;
108 }
109 " $FILENAME > /tmp/$$
110 cp /tmp/$$ $FILENAME
111}
112
Feng Xiao4333edb2015-05-31 02:28:34 -0700113
114# Replace file lists in cmake files.
Jisi Liud19604f2015-06-17 17:37:58 -0700115CMAKE_PREFIX="\${protobuf_source_dir}/src/"
116set_cmake_value $CMAKE_DIR/libprotobuf-lite.cmake libprotobuf_lite_files $CMAKE_PREFIX $LIBPROTOBUF_LITE_SOURCES
117set_cmake_value $CMAKE_DIR/libprotobuf.cmake libprotobuf_files $CMAKE_PREFIX $LIBPROTOBUF_SOURCES
118set_cmake_value $CMAKE_DIR/libprotoc.cmake libprotoc_files $CMAKE_PREFIX $LIBPROTOC_SOURCES
119set_cmake_value $CMAKE_DIR/tests.cmake lite_test_protos "" $LITE_PROTOS
Bo Yangff7bdad2015-08-23 10:45:14 -0700120set_cmake_value $CMAKE_DIR/tests.cmake tests_protos "" $PROTOS_BLACKLISTED
Jisi Liud19604f2015-06-17 17:37:58 -0700121set_cmake_value $CMAKE_DIR/tests.cmake common_test_files $CMAKE_PREFIX $COMMON_TEST_SOURCES
Feng Xiaob17ec3c2015-08-23 17:50:38 -0700122set_cmake_value $CMAKE_DIR/tests.cmake common_lite_test_files $CMAKE_PREFIX $COMMON_LITE_TEST_SOURCES
Jisi Liud19604f2015-06-17 17:37:58 -0700123set_cmake_value $CMAKE_DIR/tests.cmake tests_files $CMAKE_PREFIX $TEST_SOURCES
124set_cmake_value $CMAKE_DIR/tests.cmake lite_test_files $CMAKE_PREFIX $LITE_TEST_SOURCES
Feng Xiaob17ec3c2015-08-23 17:50:38 -0700125set_cmake_value $CMAKE_DIR/tests.cmake lite_arena_test_files $CMAKE_PREFIX $LITE_ARENA_TEST_SOURCES
Feng Xiao4333edb2015-05-31 02:28:34 -0700126
127# Generate extract_includes.bat
Feng Xiaodffd5422015-06-05 17:59:09 -0700128echo "mkdir include" > $EXTRACT_INCLUDES_BAT
129for HEADER in $PUBLIC_HEADERS; do
130 HEADER_DIR=$(dirname $HEADER)
131 while [ ! "$HEADER_DIR" = "." ]; do
132 echo $HEADER_DIR | sed "s/\\//\\\\/g"
133 HEADER_DIR=$(dirname $HEADER_DIR)
134 done
135done | sort | uniq | sed "s/^/mkdir include\\\\/" >> $EXTRACT_INCLUDES_BAT
136for HEADER in $PUBLIC_HEADERS; do
137 WINPATH=$(echo $HEADER | sed 's;/;\\;g')
138 echo "copy \${PROTOBUF_SOURCE_WIN32_PATH}\\..\\src\\$WINPATH include\\$WINPATH" >> $EXTRACT_INCLUDES_BAT
Feng Xiao4333edb2015-05-31 02:28:34 -0700139done
Jisi Liud19604f2015-06-17 17:37:58 -0700140
141################################################################################
142# Update bazel BUILD files.
143################################################################################
144
Jisi Liud19604f2015-06-17 17:37:58 -0700145set_bazel_value() {
146 local FILENAME=$1
147 local VARNAME=$2
148 local PREFIX=$3
149 shift
150 shift
151 shift
152 awk -v values="$*" -v prefix="$PREFIX" "
153 BEGIN { start = 0; }
154 /# AUTOGEN\\($VARNAME\\)/ {
155 start = 1;
156 print \$0;
157 # replace \$0 with indent.
158 sub(/#.*/, \"\", \$0)
159 len = split(values, vlist, \" \");
160 for (i = 1; i <= len; ++i) {
161 printf(\"%s\\\"%s%s\\\",\n\", \$0, prefix, vlist[i]);
162 }
163 next;
164 }
165 start && /\]/ {
166 start = 0
167 }
168 !start {
169 print \$0;
170 }
171 " $FILENAME > /tmp/$$
172 cp /tmp/$$ $FILENAME
173}
174
175
Jisi Liub0f66112015-08-21 11:18:45 -0700176BAZEL_BUILD=./BUILD
Jisi Liud19604f2015-06-17 17:37:58 -0700177BAZEL_PREFIX="src/"
Jisi Liub0f66112015-08-21 11:18:45 -0700178if [ -f "$BAZEL_BUILD" ]; then
179 set_bazel_value $BAZEL_BUILD protobuf_lite_srcs $BAZEL_PREFIX $LIBPROTOBUF_LITE_SOURCES
180 set_bazel_value $BAZEL_BUILD protobuf_srcs $BAZEL_PREFIX $LIBPROTOBUF_SOURCES
181 set_bazel_value $BAZEL_BUILD protoc_lib_srcs $BAZEL_PREFIX $LIBPROTOC_SOURCES
182 set_bazel_value $BAZEL_BUILD lite_test_protos "" $LITE_PROTOS
183 set_bazel_value $BAZEL_BUILD well_known_protos "" $WKT_PROTOS
184 set_bazel_value $BAZEL_BUILD test_protos "" $PROTOS
185 set_bazel_value $BAZEL_BUILD common_test_srcs $BAZEL_PREFIX $COMMON_TEST_SOURCES
186 set_bazel_value $BAZEL_BUILD test_srcs $BAZEL_PREFIX $TEST_SOURCES
187 set_bazel_value $BAZEL_BUILD test_plugin_srcs $BAZEL_PREFIX $TEST_PLUGIN_SOURCES
188else
189 echo "Skipped BUILD file update."
190fi
Jisi Liu7a0c4312015-06-18 16:45:27 -0700191