update_file_lists.sh 6.37 KB
Newer Older
1
#!/bin/bash
Feng Xiao's avatar
Feng Xiao committed
2 3 4 5

# This script copies source file lists from src/Makefile.am to cmake files.

get_variable_value() {
6 7
  local FILENAME=$1
  local VARNAME=$2
Feng Xiao's avatar
Feng Xiao committed
8 9 10 11 12 13 14 15 16 17 18 19 20 21
  awk "
    BEGIN { start = 0; }
    /^$VARNAME =/ { start = 1; }
    { if (start) { print \$0; } }
    /\\\\\$/ { next; }
    { start = 0; }
  " $FILENAME \
    | sed "s/^$VARNAME =//" \
    | sed "s/[ \\]//g" \
    | grep -v "^\\$" \
    | grep -v "^$" \
    | LC_ALL=C sort | uniq
}

22 23 24 25
get_header_files() {
  get_variable_value $@ | grep '\.h$'
}

Feng Xiao's avatar
Feng Xiao committed
26
get_source_files() {
Feng Xiao's avatar
Feng Xiao committed
27
  get_variable_value $@ | grep "cc$\|inc$"
Feng Xiao's avatar
Feng Xiao committed
28 29
}

Bo Yang's avatar
Bo Yang committed
30 31 32 33
get_proto_files_blacklisted() {
  get_proto_files $@ | sed '/^google\/protobuf\/unittest_enormous_descriptor.proto$/d'
}

Feng Xiao's avatar
Feng Xiao committed
34 35 36 37 38 39 40
get_proto_files() {
  get_variable_value $@ | grep "pb.cc$" | sed "s/pb.cc/proto/"
}

sort_files() {
  for FILE in $@; do
    echo $FILE
41
  done | LC_ALL=C sort | uniq
Feng Xiao's avatar
Feng Xiao committed
42 43
}

44
MAKEFILE=src/Makefile.am
Feng Xiao's avatar
Feng Xiao committed
45 46 47 48 49 50 51 52 53 54 55 56 57

[ -f "$MAKEFILE" ] || {
  echo "Cannot find: $MAKEFILE"
  exit 1
}

# Extract file lists from src/Makefile.am
GZHEADERS=$(get_variable_value $MAKEFILE GZHEADERS)
HEADERS=$(get_variable_value $MAKEFILE nobase_include_HEADERS)
PUBLIC_HEADERS=$(sort_files $GZHEADERS $HEADERS)
LIBPROTOBUF_LITE_SOURCES=$(get_source_files $MAKEFILE libprotobuf_lite_la_SOURCES)
LIBPROTOBUF_SOURCES=$(get_source_files $MAKEFILE libprotobuf_la_SOURCES)
LIBPROTOC_SOURCES=$(get_source_files $MAKEFILE libprotoc_la_SOURCES)
Feng Xiao's avatar
Feng Xiao committed
58
LIBPROTOC_HEADERS=$(get_header_files $MAKEFILE libprotoc_la_SOURCES)
Feng Xiao's avatar
Feng Xiao committed
59 60
LITE_PROTOS=$(get_proto_files $MAKEFILE protoc_lite_outputs)
PROTOS=$(get_proto_files $MAKEFILE protoc_outputs)
Bo Yang's avatar
Bo Yang committed
61
PROTOS_BLACKLISTED=$(get_proto_files_blacklisted $MAKEFILE protoc_outputs)
62
WKT_PROTOS=$(get_variable_value $MAKEFILE nobase_dist_proto_DATA)
Feng Xiao's avatar
Feng Xiao committed
63
COMMON_TEST_SOURCES=$(get_source_files $MAKEFILE COMMON_TEST_SOURCES)
64
COMMON_LITE_TEST_SOURCES=$(get_source_files $MAKEFILE COMMON_LITE_TEST_SOURCES)
Feng Xiao's avatar
Feng Xiao committed
65
TEST_SOURCES=$(get_source_files $MAKEFILE protobuf_test_SOURCES)
66
NON_MSVC_TEST_SOURCES=$(get_source_files $MAKEFILE NON_MSVC_TEST_SOURCES)
Feng Xiao's avatar
Feng Xiao committed
67
LITE_TEST_SOURCES=$(get_source_files $MAKEFILE protobuf_lite_test_SOURCES)
68
LITE_ARENA_TEST_SOURCES=$(get_source_files $MAKEFILE protobuf_lite_arena_test_SOURCES)
69
TEST_PLUGIN_SOURCES=$(get_source_files $MAKEFILE test_plugin_SOURCES)
Feng Xiao's avatar
Feng Xiao committed
70

71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
################################################################################
# Update cmake files.
################################################################################

CMAKE_DIR=cmake
EXTRACT_INCLUDES_BAT=cmake/extract_includes.bat.in
[ -d "$CMAKE_DIR" ] || {
  echo "Cannot find: $CMAKE_DIR"
  exit 1
}

[ -f "$EXTRACT_INCLUDES_BAT" ] || {
  echo "Cannot find: $EXTRACT_INCLUDES_BAT"
  exit 1
}

set_cmake_value() {
  local FILENAME=$1
  local VARNAME=$2
  local PREFIX=$3
  shift
  shift
  shift
  awk -v values="$*" -v prefix="$PREFIX" "
    BEGIN { start = 0; }
    /^set\\($VARNAME/ {
      start = 1;
      print \$0;
      len = split(values, vlist, \" \");
      for (i = 1; i <= len; ++i) {
        printf(\"  %s%s\\n\", prefix, vlist[i]);
      }
      next;
    }
    start && /^\\)/ {
      start = 0;
    }
    !start {
      print \$0;
    }
  " $FILENAME > /tmp/$$
  cp /tmp/$$ $FILENAME
}


Feng Xiao's avatar
Feng Xiao committed
116
# Replace file lists in cmake files.
117 118 119 120
CMAKE_PREFIX="\${protobuf_source_dir}/src/"
set_cmake_value $CMAKE_DIR/libprotobuf-lite.cmake libprotobuf_lite_files $CMAKE_PREFIX $LIBPROTOBUF_LITE_SOURCES
set_cmake_value $CMAKE_DIR/libprotobuf.cmake libprotobuf_files $CMAKE_PREFIX $LIBPROTOBUF_SOURCES
set_cmake_value $CMAKE_DIR/libprotoc.cmake libprotoc_files $CMAKE_PREFIX $LIBPROTOC_SOURCES
Feng Xiao's avatar
Feng Xiao committed
121
set_cmake_value $CMAKE_DIR/libprotoc.cmake libprotoc_headers $CMAKE_PREFIX $LIBPROTOC_HEADERS
122
set_cmake_value $CMAKE_DIR/tests.cmake lite_test_protos "" $LITE_PROTOS
Bo Yang's avatar
Bo Yang committed
123
set_cmake_value $CMAKE_DIR/tests.cmake tests_protos "" $PROTOS_BLACKLISTED
124
set_cmake_value $CMAKE_DIR/tests.cmake common_test_files $CMAKE_PREFIX $COMMON_TEST_SOURCES
125
set_cmake_value $CMAKE_DIR/tests.cmake common_lite_test_files $CMAKE_PREFIX $COMMON_LITE_TEST_SOURCES
126
set_cmake_value $CMAKE_DIR/tests.cmake tests_files $CMAKE_PREFIX $TEST_SOURCES
127
set_cmake_value $CMAKE_DIR/tests.cmake non_msvc_tests_files $CMAKE_PREFIX $NON_MSVC_TEST_SOURCES
128
set_cmake_value $CMAKE_DIR/tests.cmake lite_test_files $CMAKE_PREFIX $LITE_TEST_SOURCES
129
set_cmake_value $CMAKE_DIR/tests.cmake lite_arena_test_files $CMAKE_PREFIX $LITE_ARENA_TEST_SOURCES
Feng Xiao's avatar
Feng Xiao committed
130 131

# Generate extract_includes.bat
132
echo "mkdir include" > $EXTRACT_INCLUDES_BAT
133 134 135 136 137
for INCLUDE in $PUBLIC_HEADERS $WKT_PROTOS; do
  INCLUDE_DIR=$(dirname "$INCLUDE")
  while [ ! "$INCLUDE_DIR" = "." ]; do
    echo "mkdir include\\${INCLUDE_DIR//\//\\}"
    INCLUDE_DIR=$(dirname "$INCLUDE_DIR")
138 139
  done
done | sort | uniq >> $EXTRACT_INCLUDES_BAT
140 141
for INCLUDE in $PUBLIC_HEADERS $WKT_PROTOS; do
  WINPATH=${INCLUDE//\//\\}
142 143
  echo "copy \"\${PROTOBUF_SOURCE_WIN32_PATH}\\..\\src\\$WINPATH\" include\\$WINPATH" >> $EXTRACT_INCLUDES_BAT
done
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179

################################################################################
# Update bazel BUILD files.
################################################################################

set_bazel_value() {
  local FILENAME=$1
  local VARNAME=$2
  local PREFIX=$3
  shift
  shift
  shift
  awk -v values="$*" -v prefix="$PREFIX" "
    BEGIN { start = 0; }
    /# AUTOGEN\\($VARNAME\\)/ {
      start = 1;
      print \$0;
      # replace \$0 with indent.
      sub(/#.*/, \"\", \$0)
      len = split(values, vlist, \" \");
      for (i = 1; i <= len; ++i) {
        printf(\"%s\\\"%s%s\\\",\n\", \$0, prefix, vlist[i]);
      }
      next;
    }
    start && /\]/ {
      start = 0
    }
    !start {
      print \$0;
    }
  " $FILENAME > /tmp/$$
  cp /tmp/$$ $FILENAME
}


180
BAZEL_BUILD=./BUILD
181
BAZEL_PREFIX="src/"
182 183 184 185 186 187 188 189 190
if [ -f "$BAZEL_BUILD" ]; then
  set_bazel_value $BAZEL_BUILD protobuf_lite_srcs $BAZEL_PREFIX $LIBPROTOBUF_LITE_SOURCES
  set_bazel_value $BAZEL_BUILD protobuf_srcs $BAZEL_PREFIX $LIBPROTOBUF_SOURCES
  set_bazel_value $BAZEL_BUILD protoc_lib_srcs $BAZEL_PREFIX $LIBPROTOC_SOURCES
  set_bazel_value $BAZEL_BUILD lite_test_protos "" $LITE_PROTOS
  set_bazel_value $BAZEL_BUILD well_known_protos "" $WKT_PROTOS
  set_bazel_value $BAZEL_BUILD test_protos "" $PROTOS
  set_bazel_value $BAZEL_BUILD common_test_srcs $BAZEL_PREFIX $COMMON_TEST_SOURCES
  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 193 194 195
  set_bazel_value $BAZEL_BUILD test_plugin_srcs $BAZEL_PREFIX $TEST_PLUGIN_SOURCES
else
  echo "Skipped BUILD file update."
fi
196