James Kuszmaul | b13e13f | 2023-11-22 20:44:04 -0800 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | |
| 3 | import os |
| 4 | import shutil |
| 5 | |
| 6 | from upstream_utils import ( |
| 7 | get_repo_root, |
| 8 | clone_repo, |
| 9 | comment_out_invalid_includes, |
| 10 | copy_to, |
| 11 | walk_cwd_and_copy_if, |
| 12 | walk_if, |
| 13 | git_am, |
| 14 | ) |
| 15 | |
| 16 | protobuf_lite_sources = set( |
| 17 | [ |
| 18 | "google/protobuf/any_lite.cc", |
| 19 | "google/protobuf/arena.cc", |
| 20 | "google/protobuf/arenastring.cc", |
| 21 | "google/protobuf/arenaz_sampler.cc", |
| 22 | "google/protobuf/extension_set.cc", |
| 23 | "google/protobuf/generated_enum_util.cc", |
| 24 | "google/protobuf/generated_message_tctable_lite.cc", |
| 25 | "google/protobuf/generated_message_util.cc", |
| 26 | "google/protobuf/implicit_weak_message.cc", |
| 27 | "google/protobuf/inlined_string_field.cc", |
| 28 | "google/protobuf/io/coded_stream.cc", |
| 29 | "google/protobuf/io/io_win32.cc", |
| 30 | "google/protobuf/io/strtod.cc", |
| 31 | "google/protobuf/io/zero_copy_stream.cc", |
| 32 | "google/protobuf/io/zero_copy_stream_impl.cc", |
| 33 | "google/protobuf/io/zero_copy_stream_impl_lite.cc", |
| 34 | "google/protobuf/map.cc", |
| 35 | "google/protobuf/message_lite.cc", |
| 36 | "google/protobuf/parse_context.cc", |
| 37 | "google/protobuf/repeated_field.cc", |
| 38 | "google/protobuf/repeated_ptr_field.cc", |
| 39 | "google/protobuf/stubs/bytestream.cc", |
| 40 | "google/protobuf/stubs/common.cc", |
| 41 | "google/protobuf/stubs/int128.cc", |
| 42 | "google/protobuf/stubs/status.cc", |
| 43 | "google/protobuf/stubs/statusor.cc", |
| 44 | "google/protobuf/stubs/stringpiece.cc", |
| 45 | "google/protobuf/stubs/stringprintf.cc", |
| 46 | "google/protobuf/stubs/structurally_valid.cc", |
| 47 | "google/protobuf/stubs/strutil.cc", |
| 48 | "google/protobuf/stubs/time.cc", |
| 49 | "google/protobuf/wire_format_lite.cc", |
| 50 | ] |
| 51 | ) |
| 52 | |
| 53 | protobuf_lite_includes = set( |
| 54 | [ |
| 55 | "google/protobuf/any.h", |
| 56 | "google/protobuf/arena.h", |
| 57 | "google/protobuf/arena_impl.h", |
| 58 | "google/protobuf/arenastring.h", |
| 59 | "google/protobuf/arenaz_sampler.h", |
| 60 | "google/protobuf/endian.h", |
| 61 | "google/protobuf/explicitly_constructed.h", |
| 62 | "google/protobuf/extension_set.h", |
| 63 | "google/protobuf/extension_set_inl.h", |
| 64 | "google/protobuf/generated_enum_util.h", |
| 65 | "google/protobuf/generated_message_tctable_decl.h", |
| 66 | "google/protobuf/generated_message_tctable_impl.h", |
| 67 | "google/protobuf/generated_message_util.h", |
| 68 | "google/protobuf/has_bits.h", |
| 69 | "google/protobuf/implicit_weak_message.h", |
| 70 | "google/protobuf/inlined_string_field.h", |
| 71 | "google/protobuf/io/coded_stream.h", |
| 72 | "google/protobuf/io/io_win32.h", |
| 73 | "google/protobuf/io/strtod.h", |
| 74 | "google/protobuf/io/zero_copy_stream.h", |
| 75 | "google/protobuf/io/zero_copy_stream_impl.h", |
| 76 | "google/protobuf/io/zero_copy_stream_impl_lite.h", |
| 77 | "google/protobuf/map.h", |
| 78 | "google/protobuf/map_entry_lite.h", |
| 79 | "google/protobuf/map_field_lite.h", |
| 80 | "google/protobuf/map_type_handler.h", |
| 81 | "google/protobuf/message_lite.h", |
| 82 | "google/protobuf/metadata_lite.h", |
| 83 | "google/protobuf/parse_context.h", |
| 84 | "google/protobuf/port.h", |
| 85 | "google/protobuf/repeated_field.h", |
| 86 | "google/protobuf/repeated_ptr_field.h", |
| 87 | "google/protobuf/stubs/bytestream.h", |
| 88 | "google/protobuf/stubs/callback.h", |
| 89 | "google/protobuf/stubs/casts.h", |
| 90 | "google/protobuf/stubs/common.h", |
| 91 | "google/protobuf/stubs/hash.h", |
| 92 | "google/protobuf/stubs/logging.h", |
| 93 | "google/protobuf/stubs/macros.h", |
| 94 | "google/protobuf/stubs/map_util.h", |
| 95 | "google/protobuf/stubs/mutex.h", |
| 96 | "google/protobuf/stubs/once.h", |
| 97 | "google/protobuf/stubs/platform_macros.h", |
| 98 | "google/protobuf/stubs/port.h", |
| 99 | "google/protobuf/stubs/status.h", |
| 100 | "google/protobuf/stubs/stl_util.h", |
| 101 | "google/protobuf/stubs/stringpiece.h", |
| 102 | "google/protobuf/stubs/strutil.h", |
| 103 | "google/protobuf/stubs/template_util.h", |
| 104 | "google/protobuf/wire_format_lite.h", |
| 105 | ] |
| 106 | ) |
| 107 | |
| 108 | protobuf_sources = set( |
| 109 | [ |
| 110 | "google/protobuf/any.cc", |
| 111 | "google/protobuf/any.pb.cc", |
| 112 | "google/protobuf/api.pb.cc", |
| 113 | "google/protobuf/compiler/importer.cc", |
| 114 | "google/protobuf/compiler/parser.cc", |
| 115 | "google/protobuf/descriptor.cc", |
| 116 | "google/protobuf/descriptor.pb.cc", |
| 117 | "google/protobuf/descriptor_database.cc", |
| 118 | "google/protobuf/duration.pb.cc", |
| 119 | "google/protobuf/dynamic_message.cc", |
| 120 | "google/protobuf/empty.pb.cc", |
| 121 | "google/protobuf/extension_set_heavy.cc", |
| 122 | "google/protobuf/field_mask.pb.cc", |
| 123 | "google/protobuf/generated_message_bases.cc", |
| 124 | "google/protobuf/generated_message_reflection.cc", |
| 125 | "google/protobuf/generated_message_tctable_full.cc", |
| 126 | "google/protobuf/io/gzip_stream.cc", |
| 127 | "google/protobuf/io/printer.cc", |
| 128 | "google/protobuf/io/tokenizer.cc", |
| 129 | "google/protobuf/map_field.cc", |
| 130 | "google/protobuf/message.cc", |
| 131 | "google/protobuf/reflection_ops.cc", |
| 132 | "google/protobuf/service.cc", |
| 133 | "google/protobuf/source_context.pb.cc", |
| 134 | "google/protobuf/struct.pb.cc", |
| 135 | "google/protobuf/stubs/substitute.cc", |
| 136 | "google/protobuf/text_format.cc", |
| 137 | "google/protobuf/timestamp.pb.cc", |
| 138 | "google/protobuf/type.pb.cc", |
| 139 | "google/protobuf/unknown_field_set.cc", |
| 140 | "google/protobuf/util/delimited_message_util.cc", |
| 141 | "google/protobuf/util/field_comparator.cc", |
| 142 | "google/protobuf/util/field_mask_util.cc", |
| 143 | "google/protobuf/util/internal/datapiece.cc", |
| 144 | "google/protobuf/util/internal/default_value_objectwriter.cc", |
| 145 | "google/protobuf/util/internal/error_listener.cc", |
| 146 | "google/protobuf/util/internal/field_mask_utility.cc", |
| 147 | "google/protobuf/util/internal/json_escaping.cc", |
| 148 | "google/protobuf/util/internal/json_objectwriter.cc", |
| 149 | "google/protobuf/util/internal/json_stream_parser.cc", |
| 150 | "google/protobuf/util/internal/object_writer.cc", |
| 151 | "google/protobuf/util/internal/proto_writer.cc", |
| 152 | "google/protobuf/util/internal/protostream_objectsource.cc", |
| 153 | "google/protobuf/util/internal/protostream_objectwriter.cc", |
| 154 | "google/protobuf/util/internal/type_info.cc", |
| 155 | "google/protobuf/util/internal/utility.cc", |
| 156 | "google/protobuf/util/json_util.cc", |
| 157 | "google/protobuf/util/message_differencer.cc", |
| 158 | "google/protobuf/util/time_util.cc", |
| 159 | "google/protobuf/util/type_resolver_util.cc", |
| 160 | "google/protobuf/wire_format.cc", |
| 161 | "google/protobuf/wrappers.pb.cc", |
| 162 | ] |
| 163 | ) |
| 164 | |
| 165 | protobuf_includes = set( |
| 166 | [ |
| 167 | "google/protobuf/any.pb.h", |
| 168 | "google/protobuf/api.pb.h", |
| 169 | "google/protobuf/compiler/importer.h", |
| 170 | "google/protobuf/compiler/parser.h", |
| 171 | "google/protobuf/descriptor.h", |
| 172 | "google/protobuf/descriptor.pb.h", |
| 173 | "google/protobuf/descriptor_database.h", |
| 174 | "google/protobuf/duration.pb.h", |
| 175 | "google/protobuf/dynamic_message.h", |
| 176 | "google/protobuf/empty.pb.h", |
| 177 | "google/protobuf/field_access_listener.h", |
| 178 | "google/protobuf/field_mask.pb.h", |
| 179 | "google/protobuf/generated_enum_reflection.h", |
| 180 | "google/protobuf/generated_message_bases.h", |
| 181 | "google/protobuf/generated_message_reflection.h", |
| 182 | "google/protobuf/io/gzip_stream.h", |
| 183 | "google/protobuf/io/printer.h", |
| 184 | "google/protobuf/io/tokenizer.h", |
| 185 | "google/protobuf/map_entry.h", |
| 186 | "google/protobuf/map_field.h", |
| 187 | "google/protobuf/map_field_inl.h", |
| 188 | "google/protobuf/message.h", |
| 189 | "google/protobuf/metadata.h", |
| 190 | "google/protobuf/reflection.h", |
| 191 | "google/protobuf/reflection_internal.h", |
| 192 | "google/protobuf/reflection_ops.h", |
| 193 | "google/protobuf/service.h", |
| 194 | "google/protobuf/source_context.pb.h", |
| 195 | "google/protobuf/struct.pb.h", |
| 196 | "google/protobuf/text_format.h", |
| 197 | "google/protobuf/timestamp.pb.h", |
| 198 | "google/protobuf/type.pb.h", |
| 199 | "google/protobuf/unknown_field_set.h", |
| 200 | "google/protobuf/util/delimited_message_util.h", |
| 201 | "google/protobuf/util/field_comparator.h", |
| 202 | "google/protobuf/util/field_mask_util.h", |
| 203 | "google/protobuf/util/json_util.h", |
| 204 | "google/protobuf/util/message_differencer.h", |
| 205 | "google/protobuf/util/time_util.h", |
| 206 | "google/protobuf/util/type_resolver.h", |
| 207 | "google/protobuf/util/type_resolver_util.h", |
| 208 | "google/protobuf/wire_format.h", |
| 209 | "google/protobuf/wrappers.pb.h", |
| 210 | ] |
| 211 | ) |
| 212 | |
| 213 | protobuf_internal_includes = set( |
| 214 | [ |
| 215 | "google/protobuf/port_def.inc", |
| 216 | "google/protobuf/port_undef.inc", |
| 217 | "google/protobuf/stubs/int128.h", |
| 218 | "google/protobuf/stubs/mathutil.h", |
| 219 | "google/protobuf/stubs/statusor.h", |
| 220 | "google/protobuf/stubs/status_macros.h", |
| 221 | "google/protobuf/stubs/stringprintf.h", |
| 222 | "google/protobuf/stubs/substitute.h", |
| 223 | "google/protobuf/stubs/time.h", |
| 224 | "google/protobuf/util/internal/constants.h", |
| 225 | "google/protobuf/util/internal/datapiece.h", |
| 226 | "google/protobuf/util/internal/default_value_objectwriter.h", |
| 227 | "google/protobuf/util/internal/error_listener.h", |
| 228 | "google/protobuf/util/internal/field_mask_utility.h", |
| 229 | "google/protobuf/util/internal/json_escaping.h", |
| 230 | "google/protobuf/util/internal/json_objectwriter.h", |
| 231 | "google/protobuf/util/internal/json_stream_parser.h", |
| 232 | "google/protobuf/util/internal/location_tracker.h", |
| 233 | "google/protobuf/util/internal/object_location_tracker.h", |
| 234 | "google/protobuf/util/internal/object_source.h", |
| 235 | "google/protobuf/util/internal/object_writer.h", |
| 236 | "google/protobuf/util/internal/proto_writer.h", |
| 237 | "google/protobuf/util/internal/protostream_objectsource.h", |
| 238 | "google/protobuf/util/internal/protostream_objectwriter.h", |
| 239 | "google/protobuf/util/internal/structured_objectwriter.h", |
| 240 | "google/protobuf/util/internal/type_info.h", |
| 241 | "google/protobuf/util/internal/utility.h", |
| 242 | ] |
| 243 | ) |
| 244 | |
| 245 | use_src_files = protobuf_lite_sources | protobuf_sources |
| 246 | use_include_files = ( |
| 247 | protobuf_lite_includes | protobuf_includes | protobuf_internal_includes |
| 248 | ) |
| 249 | |
| 250 | |
| 251 | def matches(dp, f, files): |
| 252 | if not dp.startswith("./src/"): |
| 253 | return False |
| 254 | p = dp[6:] + "/" + f |
| 255 | return p in files |
| 256 | |
| 257 | |
| 258 | def main(): |
| 259 | upstream_root = clone_repo( |
| 260 | "https://github.com/protocolbuffers/protobuf", "v3.21.12" |
| 261 | ) |
| 262 | wpilib_root = get_repo_root() |
| 263 | wpiutil = os.path.join(wpilib_root, "wpiutil") |
| 264 | |
| 265 | # Apply patches to upstream Git repo |
| 266 | os.chdir(upstream_root) |
| 267 | for f in [ |
| 268 | "0001-Fix-sign-compare-warnings.patch", |
| 269 | "0002-Remove-redundant-move.patch", |
| 270 | "0003-Fix-maybe-uninitialized-warnings.patch", |
| 271 | "0004-Fix-coded_stream-WriteRaw.patch", |
| 272 | "0005-Suppress-enum-enum-conversion-warning.patch", |
| 273 | "0006-Fix-noreturn-function-returning.patch", |
| 274 | "0007-Work-around-GCC-12-restrict-warning-compiler-bug.patch", |
| 275 | "0008-Disable-MSVC-switch-warning.patch", |
| 276 | "0009-Disable-unused-function-warning.patch", |
| 277 | "0010-Disable-pedantic-warning.patch", |
| 278 | "0011-Avoid-use-of-sprintf.patch", |
Maxwell Henderson | 80bec32 | 2024-01-09 15:48:44 -0800 | [diff] [blame^] | 279 | "0012-Suppress-stringop-overflow-warning-false-positives.patch", |
James Kuszmaul | b13e13f | 2023-11-22 20:44:04 -0800 | [diff] [blame] | 280 | ]: |
| 281 | git_am(os.path.join(wpilib_root, "upstream_utils/protobuf_patches", f)) |
| 282 | |
| 283 | # Delete old install |
| 284 | for d in [ |
| 285 | "src/main/native/thirdparty/protobuf/src", |
| 286 | "src/main/native/thirdparty/protobuf/include", |
| 287 | ]: |
| 288 | shutil.rmtree(os.path.join(wpiutil, d), ignore_errors=True) |
| 289 | |
| 290 | # Copy protobuf source files into allwpilib |
| 291 | src_files = walk_if(".", lambda dp, f: matches(dp, f, use_src_files)) |
| 292 | src_files = [f[22:] for f in src_files] |
| 293 | os.chdir(os.path.join(upstream_root, "src/google/protobuf")) |
| 294 | copy_to(src_files, os.path.join(wpiutil, "src/main/native/thirdparty/protobuf/src")) |
| 295 | |
| 296 | # Copy protobuf header files into allwpilib |
| 297 | os.chdir(upstream_root) |
| 298 | include_files = walk_if(".", lambda dp, f: matches(dp, f, use_include_files)) |
| 299 | include_files = [f[6:] for f in include_files] |
| 300 | os.chdir(os.path.join(upstream_root, "src")) |
| 301 | copy_to( |
| 302 | include_files, |
| 303 | os.path.join(wpiutil, "src/main/native/thirdparty/protobuf/include"), |
| 304 | ) |
| 305 | |
| 306 | |
| 307 | if __name__ == "__main__": |
| 308 | main() |