1#!/bin/sh 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$" 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) 58LITE_PROTOS=$(get_proto_files $MAKEFILE protoc_lite_outputs) 59PROTOS=$(get_proto_files $MAKEFILE protoc_outputs) 60PROTOS_BLACKLISTED=$(get_proto_files_blacklisted $MAKEFILE protoc_outputs) 61WKT_PROTOS=$(get_variable_value $MAKEFILE nobase_dist_proto_DATA) 62COMMON_TEST_SOURCES=$(get_source_files $MAKEFILE COMMON_TEST_SOURCES) 63COMMON_LITE_TEST_SOURCES=$(get_source_files $MAKEFILE COMMON_LITE_TEST_SOURCES) 64TEST_SOURCES=$(get_source_files $MAKEFILE protobuf_test_SOURCES) 65LITE_TEST_SOURCES=$(get_source_files $MAKEFILE protobuf_lite_test_SOURCES) 66LITE_ARENA_TEST_SOURCES=$(get_source_files $MAKEFILE protobuf_lite_arena_test_SOURCES) 67TEST_PLUGIN_SOURCES=$(get_source_files $MAKEFILE test_plugin_SOURCES) 68 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 89 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; 97 len = split(values, vlist, \" \"); 98 for (i = 1; i <= len; ++i) { 99 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 113 114# Replace file lists in cmake files. 115CMAKE_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 120set_cmake_value $CMAKE_DIR/tests.cmake tests_protos "" $PROTOS_BLACKLISTED 121set_cmake_value $CMAKE_DIR/tests.cmake common_test_files $CMAKE_PREFIX $COMMON_TEST_SOURCES 122set_cmake_value $CMAKE_DIR/tests.cmake common_lite_test_files $CMAKE_PREFIX $COMMON_LITE_TEST_SOURCES 123set_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 125set_cmake_value $CMAKE_DIR/tests.cmake lite_arena_test_files $CMAKE_PREFIX $LITE_ARENA_TEST_SOURCES 126 127# Generate extract_includes.bat 128echo "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 139done 140 141################################################################################ 142# Update bazel BUILD files. 143################################################################################ 144 145set_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 176BAZEL_BUILD=./BUILD 177BAZEL_PREFIX="src/" 178if [ -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 191 192