Makefile 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625
  1. #
  2. # Key targets in this makefile:
  3. #
  4. # make build
  5. # Build the runtime library and plugin
  6. # make test
  7. # Build everything, run both plugin and library tests:
  8. # Plugin test verifies that plugin output matches the "Reference" files
  9. # exactly
  10. # Library test exercises most features of the generated code
  11. # make regenerate
  12. # Recompile all the necessary protos
  13. # (requires protoc in path)
  14. #
  15. # Caution: 'test' does not 'regenerate', so if you've made changes to the code
  16. # generation, you'll need to do more than just 'test':
  17. # 1. 'make build' to build the plugin
  18. # 2. 'make regenerate' to rebuild the Swift code from protos using the new
  19. # plugin
  20. # 3. 'make build' again to recompile everything with the regenerated protos
  21. # 4. 'make test' to run the test suites
  22. #
  23. # How to run a 'swift' executable that supports the 'swift update', 'swift build',
  24. # 'swift test', etc commands.
  25. SWIFT=swift
  26. # How to run a working version of protoc. Invoke make with PROTOC=[path] to
  27. # override this value, i.e. -
  28. # make [TARGET] PROTOC=../protobuf/src/protoc
  29. PROTOC=protoc
  30. # How to run awk on your system
  31. AWK=awk
  32. # Installation directory
  33. BINDIR=/usr/local/bin
  34. # Install tool name
  35. INSTALL=install
  36. # Where to find a google/protobuf checkout. Defaults be being beside this
  37. # checkout. Invoke make with GOOGLE_PROTOBUF_CHECKOUT=[PATH_TO_CHECKOUT] to
  38. # override this value, i.e. -
  39. # make [TARGET] GOOGLE_PROTOBUF_CHECKOUT=[PATH_TO_CHECKOUT]
  40. GOOGLE_PROTOBUF_CHECKOUT?=../protobuf
  41. # Helpers for the common parts of source generation.
  42. #
  43. # To ensure that the local version of the plugin is always used (and not a
  44. # previously installed one), we use a custom output name (-tfiws_out).
  45. PROTOC_GEN_SWIFT=.build/debug/protoc-gen-swift
  46. GENERATE_SRCS_BASE=${PROTOC} --plugin=protoc-gen-tfiws=${PROTOC_GEN_SWIFT}
  47. # Search 'Protos/SwiftProtobuf/' so the WKTs can be found (google/protobuf/*).
  48. GENERATE_SRCS=${GENERATE_SRCS_BASE} -I Protos/SwiftProtobuf
  49. # Where to find the Swift conformance test runner executable.
  50. SWIFT_CONFORMANCE_PLUGIN=.build/debug/Conformance
  51. # Where to find the conformance-test-runner. Defaults to being in your protobuf
  52. # checkout. Invoke make with CONFORMANCE_TEST_RUNNER=[PATH_TO_BINARY] to
  53. # override this value.
  54. CONFORMANCE_TEST_RUNNER?=${GOOGLE_PROTOBUF_CHECKOUT}/conformance_test_runner
  55. # Hook to pass arge to swift build|test (mainly for the CI setup)
  56. SWIFT_BUILD_TEST_HOOK?=
  57. # The directories within Protos/ with the exception of "upstream". Use for the
  58. # maintenance of the 'Reference' target and test-plugin.
  59. PROTOS_DIRS=Conformance protoc-gen-swiftTests SwiftProtobuf SwiftProtobufPluginLibrary SwiftProtobufPluginLibraryTests SwiftProtobufTests
  60. .PHONY: \
  61. all \
  62. build \
  63. check \
  64. check-for-protobuf-checkout \
  65. check-proto-files \
  66. check-version-numbers \
  67. clean \
  68. compile-tests \
  69. compile-tests-multimodule \
  70. compile-tests-internalimportsbydefault \
  71. default \
  72. docs \
  73. install \
  74. pod-lib-lint \
  75. reference \
  76. regenerate \
  77. regenerate-compiletests-multimodule-protos \
  78. copy-compiletests-internalimportsbydefault-protos \
  79. regenerate-compiletests-protos \
  80. regenerate-conformance-protos \
  81. regenerate-fuzz-protos \
  82. regenerate-library-protos \
  83. regenerate-plugin-protos \
  84. regenerate-test-protos \
  85. test \
  86. test-all \
  87. test-everything \
  88. test-plugin \
  89. test-runtime \
  90. test-spm-plugin \
  91. update-proto-files
  92. default: build
  93. all: build
  94. # Builds all the targets of the package.
  95. build:
  96. ${SWIFT} build ${SWIFT_BUILD_TEST_HOOK}
  97. # Anything that needs the plugin should do a build.
  98. ${PROTOC_GEN_SWIFT}: build
  99. # Does it really make sense to install a debug build, or should this be forcing
  100. # a release build and then installing that instead?
  101. install: build
  102. ${INSTALL} ${PROTOC_GEN_SWIFT} ${BINDIR}
  103. clean:
  104. swift package clean
  105. rm -rf .build _test ${PROTOC_GEN_SWIFT} *DescriptorTestData.bin \
  106. Performance/_generated Performance/_results Protos/mined_words.txt \
  107. docs build
  108. find . -name '*~' | xargs rm -f
  109. # Build a local copy of the API documentation, using the same process used
  110. # by cocoadocs.org.
  111. docs:
  112. @if which jazzy >/dev/null; then \
  113. jazzy; \
  114. else \
  115. echo "Jazzy not installed, use 'gem install jazzy' or download from https://github.com/realm/jazzy"; \
  116. fi
  117. #
  118. # Test the runtime and the plugin
  119. #
  120. # This must pass before any commit.
  121. #
  122. check test: build test-runtime test-plugin test-conformance check-version-numbers
  123. # Test everything (runtime, plugin)
  124. test-all test-everything: test
  125. # Check the version numbers are all in sync.
  126. check-version-numbers:
  127. @DevTools/LibraryVersions.py --validate
  128. #
  129. # The Swift test suite includes unit tests for the runtime library
  130. # and functional tests for the Swift code generated by the plugin.
  131. #
  132. test-runtime: build
  133. ${SWIFT} test ${SWIFT_BUILD_TEST_HOOK}
  134. #
  135. # Test the plugin by itself:
  136. # * Translate every proto in Protos into Swift using local protoc-gen-swift
  137. # * Put result in _test directory
  138. # * Compare output with reference output in Reference directory
  139. # * If generated output and reference output don't match exactly, fail.
  140. #
  141. # Of course, this will fail if you've made any changes to the generated output.
  142. # In that case, you'll need to do the following before committing:
  143. # * `make regenerate` to rebuild the protos used by the runtime and plugin
  144. # * `make test-runtime` to verify that the runtime works correctly with the new changes
  145. # * `make reference` to update the Reference directory
  146. # * MANUALLY go through `git diff Reference` to verify that the generated Swift changed in the way you expect
  147. # * `make clean build test` to do a final check
  148. #
  149. # Note: Some of these protos define the same package.(message|enum)s, so they
  150. # can't be done in a single protoc/proto-gen-swift invoke and have to be done
  151. # one at a time instead.
  152. test-plugin: build ${PROTOC_GEN_SWIFT}
  153. @rm -rf _test && mkdir -p _test/upstream
  154. for p in `find Protos/upstream -type f -name '*.proto'`; do \
  155. ${GENERATE_SRCS_BASE} \
  156. -I Protos/upstream \
  157. --tfiws_out=_test/upstream $$p || exit 1; \
  158. done
  159. for d in ${PROTOS_DIRS}; do \
  160. mkdir _test/$$d ; \
  161. ${GENERATE_SRCS_BASE} \
  162. -I Protos/SwiftProtobuf \
  163. -I Protos/SwiftProtobufPluginLibrary \
  164. -I Protos/$$d \
  165. --tfiws_out=_test/$$d \
  166. `find Protos/$$d -type f -name "*.proto"` || exit 1; \
  167. done
  168. @mkdir -p _test/CompileTests/MultiModule
  169. ${GENERATE_SRCS} \
  170. -I Protos/CompileTests/MultiModule \
  171. --tfiws_opt=Visibility=Public \
  172. --tfiws_opt=ProtoPathModuleMappings=Protos/CompileTests/MultiModule/module_mappings.pbascii \
  173. --tfiws_out=_test/CompileTests/MultiModule \
  174. `(find Protos/CompileTests/MultiModule -type f -name "*.proto")`
  175. @mkdir -p _test/CompileTests/InternalImportsByDefault
  176. ${GENERATE_SRCS} \
  177. -I Protos/CompileTests/InternalImportsByDefault \
  178. --tfiws_opt=Visibility=Public \
  179. --tfiws_opt=UseAccessLevelOnImports=true \
  180. --tfiws_out=_test/CompileTests/InternalImportsByDefault \
  181. `(find Protos/CompileTests/InternalImportsByDefault -type f -name "*.proto")`
  182. diff -ru _test Reference
  183. # Test the SPM plugin.
  184. test-spm-plugin:
  185. env PROTOC_PATH=$(shell realpath ${PROTOC}) ${SWIFT} test --package-path PluginExamples
  186. compile-tests: \
  187. compile-tests-multimodule \
  188. compile-tests-internalimportsbydefault
  189. # Test that ensures generating public into multiple modules with `import public`
  190. # yields buildable code.
  191. compile-tests-multimodule:
  192. ${SWIFT} test --package-path CompileTests/MultiModule
  193. # Test that ensures that using access level modifiers on imports yields code that's buildable
  194. # when `InternalImportsByDefault` is enabled on the module.
  195. compile-tests-internalimportsbydefault:
  196. env PROTOC_PATH=$(shell realpath ${PROTOC}) ${SWIFT} build --package-path CompileTests/InternalImportsByDefault
  197. # Rebuild the reference files by running the local version of protoc-gen-swift
  198. # against our menagerie of sample protos.
  199. #
  200. # If you do this, you MUST MANUALLY verify these files before checking them in,
  201. # since the new checkin will become the new main reference.
  202. #
  203. # Note: Some of the upstream protos define the same package.(message|enum)s, so
  204. # they can't be done in a single protoc/proto-gen-swift invoke and have to be
  205. # done one at a time instead.
  206. reference: build ${PROTOC_GEN_SWIFT}
  207. @rm -rf Reference && mkdir -p Reference/upstream
  208. for p in `find Protos/upstream -type f -name '*.proto'`; do \
  209. ${GENERATE_SRCS_BASE} \
  210. -I Protos/upstream \
  211. --tfiws_out=Reference/upstream $$p || exit 1; \
  212. done
  213. for d in ${PROTOS_DIRS}; do \
  214. mkdir Reference/$$d ; \
  215. ${GENERATE_SRCS_BASE} \
  216. -I Protos/SwiftProtobuf \
  217. -I Protos/SwiftProtobufPluginLibrary \
  218. -I Protos/$$d \
  219. --tfiws_out=Reference/$$d \
  220. `find Protos/$$d -type f -name "*.proto"` || exit 1; \
  221. done
  222. @mkdir -p Reference/CompileTests/MultiModule
  223. ${GENERATE_SRCS} \
  224. -I Protos/CompileTests/MultiModule \
  225. --tfiws_opt=Visibility=Public \
  226. --tfiws_opt=ProtoPathModuleMappings=Protos/CompileTests/MultiModule/module_mappings.pbascii \
  227. --tfiws_out=Reference/CompileTests/MultiModule \
  228. `(find Protos/CompileTests/MultiModule -type f -name "*.proto")`
  229. @mkdir -p Reference/CompileTests/InternalImportsByDefault
  230. ${GENERATE_SRCS} \
  231. -I Protos/CompileTests/InternalImportsByDefault \
  232. --tfiws_opt=Visibility=Public \
  233. --tfiws_opt=UseAccessLevelOnImports=true \
  234. --tfiws_out=Reference/CompileTests/InternalImportsByDefault \
  235. `(find Protos/CompileTests/InternalImportsByDefault -type f -name "*.proto")`
  236. #
  237. # Rebuild the generated .pb.swift test files by running
  238. # protoc over all the relevant inputs.
  239. #
  240. # Before running this, ensure that:
  241. # * protoc-gen-swift is built and installed somewhere in your system PATH
  242. # * protoc is built and installed
  243. # * PROTOC at the top of this file is set correctly
  244. #
  245. regenerate: \
  246. regenerate-library-protos \
  247. regenerate-fuzz-protos \
  248. regenerate-plugin-protos \
  249. regenerate-test-protos \
  250. regenerate-compiletests-protos \
  251. regenerate-conformance-protos \
  252. Sources/SwiftProtobufPluginLibrary/PluginLibEditionDefaults.swift \
  253. Tests/protoc-gen-swiftTests/DescriptorTestData.swift \
  254. Tests/SwiftProtobufPluginLibraryTests/DescriptorTestData.swift \
  255. Tests/SwiftProtobufPluginLibraryTests/PluginLibTestingEditionDefaults.swift
  256. # Rebuild just the protos included in the runtime library
  257. # NOTE: dependencies doesn't include the source .proto files, should fix that;
  258. # would also need to list all the outputs.
  259. regenerate-library-protos: build ${PROTOC_GEN_SWIFT}
  260. find Sources/SwiftProtobuf -name "*.pb.swift" -exec rm -f {} \;
  261. ${GENERATE_SRCS} \
  262. --tfiws_opt=FileNaming=DropPath \
  263. --tfiws_opt=Visibility=Public \
  264. --tfiws_out=Sources/SwiftProtobuf \
  265. `find Protos/SwiftProtobuf -type f -name "*.proto"`
  266. # Rebuild just the protos used by the plugin
  267. # NOTE: dependencies doesn't include the source .proto files, should fix that;
  268. # would also need to list all the outputs.
  269. regenerate-plugin-protos: build ${PROTOC_GEN_SWIFT}
  270. find Sources/SwiftProtobufPluginLibrary -name "*.pb.swift" -exec rm -f {} \;
  271. ${GENERATE_SRCS} \
  272. -I Protos/SwiftProtobufPluginLibrary \
  273. --tfiws_opt=FileNaming=DropPath \
  274. --tfiws_opt=Visibility=Public \
  275. --tfiws_out=Sources/SwiftProtobufPluginLibrary \
  276. `find Protos/SwiftProtobufPluginLibrary -type f -name "*.proto"`
  277. # Is this based on the upstream bazel rules `compile_edition_defaults` and
  278. # `embed_edition_defaults`.
  279. Sources/SwiftProtobufPluginLibrary/PluginLibEditionDefaults.swift: build ${PROTOC_GEN_SWIFT} Protos/SwiftProtobuf/google/protobuf/descriptor.proto
  280. @${PROTOC} \
  281. --edition_defaults_out=PluginLibEditionDefaults.bin \
  282. --edition_defaults_minimum=PROTO2 \
  283. --edition_defaults_maximum=2023 \
  284. -I Protos/SwiftProtobuf \
  285. Protos/SwiftProtobuf/google/protobuf/descriptor.proto
  286. @rm -f $@
  287. @echo '// See Makefile how this is generated.' >> $@
  288. @echo '// swift-format-ignore-file' >> $@
  289. @echo 'import Foundation' >> $@
  290. @echo 'let bundledFeatureSetDefaultBytes: [UInt8] = [' >> $@
  291. @xxd -i < PluginLibEditionDefaults.bin >> $@
  292. @echo ']' >> $@
  293. # Some defaults for the testing of custom features
  294. Tests/SwiftProtobufPluginLibraryTests/PluginLibTestingEditionDefaults.swift: build ${PROTOC_GEN_SWIFT} Protos/SwiftProtobufPluginLibraryTests/test_features.proto
  295. @${PROTOC} \
  296. --edition_defaults_out=PluginLibTestingEditionDefaults.bin \
  297. --edition_defaults_minimum=PROTO2 \
  298. --edition_defaults_maximum=2023 \
  299. -I Protos/SwiftProtobuf \
  300. -I Protos/SwiftProtobufPluginLibraryTests \
  301. Protos/SwiftProtobufPluginLibraryTests/test_features.proto
  302. @rm -f $@
  303. @echo '// See Makefile how this is generated.' >> $@
  304. @echo '// swift-format-ignore-file' >> $@
  305. @echo 'import Foundation' >> $@
  306. @echo 'let testFeatureSetDefaultBytes: [UInt8] = [' >> $@
  307. @xxd -i < PluginLibTestingEditionDefaults.bin >> $@
  308. @echo ']' >> $@
  309. # Rebuild just the protos used by the tests
  310. # NOTE: dependencies doesn't include the source .proto files, should fix that;
  311. # would also need to list all the outputs.
  312. regenerate-test-protos: build ${PROTOC_GEN_SWIFT} Protos/SwiftProtobufTests/generated_swift_names_enums.proto Protos/SwiftProtobufTests/generated_swift_names_enum_cases.proto Protos/SwiftProtobufTests/generated_swift_names_fields.proto Protos/SwiftProtobufTests/generated_swift_names_messages.proto
  313. find Tests/SwiftProtobufTests -name "*.pb.swift" -exec rm -f {} \;
  314. ${GENERATE_SRCS} \
  315. -I Protos/SwiftProtobufTests \
  316. --tfiws_opt=FileNaming=DropPath \
  317. --tfiws_out=Tests/SwiftProtobufTests \
  318. `find Protos/SwiftProtobufTests -type f -name "*.proto"`
  319. find Tests/SwiftProtobufPluginLibraryTests -name "*.pb.swift" -exec rm -f {} \;
  320. ${GENERATE_SRCS} \
  321. -I Protos/SwiftProtobuf \
  322. -I Protos/SwiftProtobufPluginLibrary \
  323. -I Protos/SwiftProtobufPluginLibraryTests \
  324. --tfiws_opt=FileNaming=DropPath \
  325. --tfiws_out=Tests/SwiftProtobufPluginLibraryTests \
  326. `find Protos/SwiftProtobufPluginLibraryTests -type f -name "*.proto"`
  327. # Rebuild the protos for FuzzTesting/Sources/FuzzCommon, the file lives in the
  328. # Protos/SwiftProtobufTests to have just one copy.
  329. regenerate-fuzz-protos: build ${PROTOC_GEN_SWIFT}
  330. find FuzzTesting/Sources/FuzzCommon -name "*.pb.swift" -exec rm -f {} \;
  331. ${GENERATE_SRCS} \
  332. -I Protos/SwiftProtobufTests \
  333. --tfiws_opt=FileNaming=DropPath \
  334. --tfiws_opt=Visibility=Public \
  335. --tfiws_out=FuzzTesting/Sources/FuzzCommon \
  336. Protos/SwiftProtobufTests/fuzz_testing.proto
  337. SWIFT_PLUGINLIB_DESCRIPTOR_TEST_PROTOS= \
  338. Protos/SwiftProtobufPluginLibraryTests/pluginlib_descriptor_test.proto \
  339. Protos/SwiftProtobufPluginLibraryTests/pluginlib_descriptor_test2.proto \
  340. Protos/SwiftProtobufPluginLibraryTests/pluginlib_descriptor_test_import.proto \
  341. Protos/SwiftProtobufPluginLibraryTests/pluginlib_descriptor_delimited.proto \
  342. Protos/SwiftProtobufPluginLibraryTests/unittest_delimited.proto \
  343. Protos/SwiftProtobufPluginLibraryTests/unittest_delimited_import.proto \
  344. Protos/SwiftProtobufPluginLibrary/swift_protobuf_module_mappings.proto
  345. Tests/SwiftProtobufPluginLibraryTests/DescriptorTestData.swift: build ${PROTOC_GEN_SWIFT} ${SWIFT_PLUGINLIB_DESCRIPTOR_TEST_PROTOS}
  346. @${PROTOC} \
  347. --include_imports \
  348. --include_source_info \
  349. --descriptor_set_out=PluginLibDescriptorTestData.bin \
  350. -I Protos/SwiftProtobuf \
  351. -I Protos/SwiftProtobufPluginLibrary \
  352. -I Protos/SwiftProtobufPluginLibraryTests \
  353. ${SWIFT_PLUGINLIB_DESCRIPTOR_TEST_PROTOS}
  354. @rm -f $@
  355. @echo '// See Makefile how this is generated.' >> $@
  356. @echo '// swift-format-ignore-file' >> $@
  357. @echo 'import Foundation' >> $@
  358. @echo 'let fileDescriptorSetBytes: [UInt8] = [' >> $@
  359. @xxd -i < PluginLibDescriptorTestData.bin >> $@
  360. @echo ']' >> $@
  361. SWIFT_PLUGIN_DESCRIPTOR_TEST_PROTOS= \
  362. Protos/protoc-gen-swiftTests/plugin_descriptor_test.proto
  363. Tests/protoc-gen-swiftTests/DescriptorTestData.swift: build ${PROTOC_GEN_SWIFT} ${SWIFT_PLUGIN_DESCRIPTOR_TEST_PROTOS}
  364. @${PROTOC} \
  365. --include_imports \
  366. --descriptor_set_out=PluginDescriptorTestData.bin \
  367. -I Protos/protoc-gen-swiftTests \
  368. ${SWIFT_PLUGIN_DESCRIPTOR_TEST_PROTOS}
  369. @rm -f $@
  370. @echo '// See Makefile how this is generated.' >> $@
  371. @echo '// swift-format-ignore-file' >> $@
  372. @echo 'import Foundation' >> $@
  373. @echo 'let fileDescriptorSetBytes: [UInt8] = [' >> $@
  374. @xxd -i < PluginDescriptorTestData.bin >> $@
  375. @echo ']' >> $@
  376. #
  377. # Collect a list of words that appear in the SwiftProtobuf library
  378. # source. These are words that may cause problems for generated code.
  379. #
  380. # The logic here builds a word list as follows:
  381. # = Look at every Swift source file in the library
  382. # = Take every line with the word 'public', 'func', or 'var'
  383. # = Remove any comments from the line.
  384. # = Break each such line into words (stripping all punctuation)
  385. # = Remove words that differ only in case
  386. # = Remove anything that will cause proto parsing issues (things named "reserved")
  387. #
  388. # Selecting lines with 'public', 'func' or 'var' ensures we get every
  389. # public protocol, struct, enum, or class name, as well as every
  390. # method or property defined in a public protocol, struct, or class.
  391. # It also gives us a large collection of Swift names.
  392. Protos/mined_words.txt: Sources/SwiftProtobuf/*.swift
  393. @echo Building $@
  394. @cat $^ | \
  395. grep -E '\b(public|func|var)\b' | \
  396. grep -vE '\b(private|internal|fileprivate)\b' | \
  397. sed -e 's|//.*$$||g' | \
  398. sed -e 's/[^a-zA-Z0-9_]/ /g' | \
  399. tr " " "\n" | \
  400. sed -e 's/^_//' | \
  401. sort -uf | \
  402. grep -vE '(reserved)' | \
  403. grep '^[a-zA-Z_]' > $@
  404. # Build some proto files full of landmines
  405. #
  406. # This takes the word list Protos/mined_words.txt and uses
  407. # it to build several proto files:
  408. # = Build a message with one `int32` field for each word
  409. # = Build an enum with a case for each such word
  410. # = Build a message with a submessage named with each word
  411. # = Build a message with an enum named with each word
  412. #
  413. # If the Swift compiler can actually compile the result, that suggests
  414. # we can correctly handle every symbol in the library itself that
  415. # might cause problems. Failures compiling this indicate weaknesses
  416. # in protoc-gen-swift's name sanitization logic.
  417. #
  418. Protos/SwiftProtobufTests/generated_swift_names_fields.proto: Protos/mined_words.txt
  419. @echo Building $@
  420. @rm $@
  421. @echo '// See Makefile for the logic that generates this' >> $@
  422. @echo '// Protoc errors imply this file is being generated incorrectly' >> $@
  423. @echo '// Swift compile errors are probably bugs in protoc-gen-swift' >> $@
  424. @echo 'syntax = "proto3";' >> $@
  425. @echo 'package swift_proto_testing.generated;' >> $@
  426. @echo 'message GeneratedSwiftReservedFields {' >> $@
  427. @cat Protos/mined_words.txt | awk 'BEGIN{n = 1} {print " int32 " $$1 " = " n ";"; n += 1 }' >> $@
  428. @echo '}' >> $@
  429. Protos/SwiftProtobufTests/generated_swift_names_enum_cases.proto: Protos/mined_words.txt
  430. @echo Building $@
  431. @rm $@
  432. @echo '// See Makefile for the logic that generates this' >> $@
  433. @echo '// Protoc errors imply this file is being generated incorrectly' >> $@
  434. @echo '// Swift compile errors are probably bugs in protoc-gen-swift' >> $@
  435. @echo 'syntax = "proto3";' >> $@
  436. @echo 'package swift_proto_testing.generated;' >> $@
  437. @echo 'enum GeneratedSwiftReservedEnum {' >> $@
  438. @echo ' NONE = 0;' >> $@
  439. @cat Protos/mined_words.txt | awk 'BEGIN{n = 1} {print " " $$1 " = " n ";"; n += 1 }' >> $@
  440. @echo '}' >> $@
  441. Protos/SwiftProtobufTests/generated_swift_names_messages.proto: Protos/mined_words.txt
  442. @echo Building $@
  443. @rm $@
  444. @echo '// See Makefile for the logic that generates this' >> $@
  445. @echo '// Protoc errors imply this file is being generated incorrectly' >> $@
  446. @echo '// Swift compile errors are probably bugs in protoc-gen-swift' >> $@
  447. @echo 'syntax = "proto3";' >> $@
  448. @echo 'package swift_proto_testing.generated;' >> $@
  449. @echo 'message GeneratedSwiftReservedMessages {' >> $@
  450. @cat Protos/mined_words.txt | awk '{print " message " $$1 " { int32 " $$1 " = 1; }"}' >> $@
  451. @echo '}' >> $@
  452. Protos/SwiftProtobufTests/generated_swift_names_enums.proto: Protos/mined_words.txt
  453. @echo Building $@
  454. @rm $@
  455. @echo '// See Makefile for the logic that generates this' >> $@
  456. @echo '// Protoc errors imply this file is being generated incorrectly' >> $@
  457. @echo '// Swift compile errors are probably bugs in protoc-gen-swift' >> $@
  458. @echo 'syntax = "proto3";' >> $@
  459. @echo 'package swift_proto_testing.generated;' >> $@
  460. @echo 'message GeneratedSwiftReservedEnums {' >> $@
  461. @cat Protos/mined_words.txt | awk '{print " enum " $$1 " { NONE_" $$1 " = 0; }"}' >> $@
  462. @echo '}' >> $@
  463. # Rebuild just the protos used by the conformance test runner.
  464. regenerate-conformance-protos: build ${PROTOC_GEN_SWIFT}
  465. find Sources/Conformance -name "*.pb.swift" -exec rm -f {} \;
  466. ${GENERATE_SRCS} \
  467. -I Protos/Conformance \
  468. --tfiws_opt=FileNaming=DropPath \
  469. --tfiws_out=Sources/Conformance \
  470. `find Protos/Conformance -type f -name "*.proto"`
  471. # Rebuild just the protos used by the CompileTests.
  472. regenerate-compiletests-protos: \
  473. regenerate-compiletests-multimodule-protos \
  474. copy-compiletests-internalimportsbydefault-protos
  475. # Update the CompileTests/MultiModule files.
  476. # NOTE: Any changes here must also be done on the "test-plugin" target so it
  477. # generates in the same way.
  478. regenerate-compiletests-multimodule-protos: build ${PROTOC_GEN_SWIFT}
  479. find CompileTests/MultiModule -name "*.pb.swift" -exec rm -f {} \;
  480. ${GENERATE_SRCS} \
  481. -I Protos/CompileTests/MultiModule \
  482. --tfiws_opt=Visibility=Public \
  483. --tfiws_opt=ProtoPathModuleMappings=Protos/CompileTests/MultiModule/module_mappings.pbascii \
  484. --tfiws_out=CompileTests/MultiModule \
  485. `(find Protos/CompileTests/MultiModule -type f -name "*.proto")`
  486. # We use the plugin for the InternalImportsByDefault test, so we don't actually need to regenerate
  487. # anything. However, to keep the protos centralised in a single place (the Protos directory),
  488. # this simply copies those files to the InternalImportsByDefault package in case they change.
  489. copy-compiletests-internalimportsbydefault-protos:
  490. @cp Protos/CompileTests/InternalImportsByDefault/* CompileTests/InternalImportsByDefault/Sources/InternalImportsByDefault/Protos
  491. # Helper to check if there is a protobuf checkout as expected.
  492. check-for-protobuf-checkout:
  493. @if [ ! -d "${GOOGLE_PROTOBUF_CHECKOUT}/src/google/protobuf" ]; then \
  494. echo "ERROR: ${GOOGLE_PROTOBUF_CHECKOUT} does not appear to be a checkout of"; \
  495. echo "ERROR: github.com/protocolbuffers/protobuf. Please check it out or set"; \
  496. echo "ERROR: GOOGLE_PROTOBUF_CHECKOUT to point to a checkout."; \
  497. exit 1; \
  498. fi
  499. #
  500. # Helper to update the .proto files copied from the protocolbuffers/protobuf distro.
  501. #
  502. update-proto-files: check-for-protobuf-checkout
  503. @rm -rf Protos/upstream
  504. @mkdir -p \
  505. Protos/upstream/conformance/test_protos \
  506. Protos/upstream/google/protobuf/compiler \
  507. Protos/upstream/editions/golden
  508. @cp -v "${GOOGLE_PROTOBUF_CHECKOUT}"/conformance/*.proto Protos/upstream/conformance/
  509. @cp -v "${GOOGLE_PROTOBUF_CHECKOUT}"/conformance/test_protos/*.proto Protos/upstream/conformance/test_protos/
  510. @cp -v "${GOOGLE_PROTOBUF_CHECKOUT}"/src/google/protobuf/*.proto Protos/upstream/google/protobuf/
  511. @cp -v "${GOOGLE_PROTOBUF_CHECKOUT}"/src/google/protobuf/compiler/*.proto Protos/upstream/google/protobuf/compiler/
  512. @cp -v "${GOOGLE_PROTOBUF_CHECKOUT}"/editions/golden/test_messages_proto?_editions.proto Protos/upstream/editions/golden/
  513. # Now copy into the Proto directories for the local targets.
  514. @rm -rf Protos/Conformance/conformance/test_protos && mkdir -p Protos/Conformance/conformance/test_protos
  515. @cp -v Protos/upstream/conformance/*.proto Protos/Conformance/conformance
  516. @cp -v Protos/upstream/conformance/test_protos/*.proto Protos/Conformance/conformance/test_protos
  517. @rm -rf Protos/Conformance/google && mkdir -p Protos/Conformance/google/protobuf Protos/Conformance/editions
  518. @cp -v \
  519. Protos/upstream/google/protobuf/test_messages_proto2.proto \
  520. Protos/upstream/google/protobuf/test_messages_proto3.proto \
  521. Protos/Conformance/google/protobuf/
  522. @cp -v \
  523. Protos/upstream/editions/golden/test_messages_proto2_editions.proto \
  524. Protos/upstream/editions/golden/test_messages_proto3_editions.proto \
  525. Protos/Conformance/editions/
  526. @rm -rf Protos/SwiftProtobuf/google && mkdir -p Protos/SwiftProtobuf/google/protobuf
  527. @cp -v \
  528. Protos/upstream/google/protobuf/timestamp.proto \
  529. Protos/upstream/google/protobuf/field_mask.proto \
  530. Protos/upstream/google/protobuf/api.proto \
  531. Protos/upstream/google/protobuf/duration.proto \
  532. Protos/upstream/google/protobuf/struct.proto \
  533. Protos/upstream/google/protobuf/wrappers.proto \
  534. Protos/upstream/google/protobuf/source_context.proto \
  535. Protos/upstream/google/protobuf/any.proto \
  536. Protos/upstream/google/protobuf/type.proto \
  537. Protos/upstream/google/protobuf/empty.proto \
  538. Protos/upstream/google/protobuf/descriptor.proto \
  539. Protos/SwiftProtobuf/google/protobuf
  540. @rm -rf Protos/SwiftProtobufPluginLibrary/google && mkdir -p Protos/SwiftProtobufPluginLibrary/google/protobuf/compiler
  541. @cp -v Protos/upstream/google/protobuf/compiler/*.proto Protos/SwiftProtobufPluginLibrary/google/protobuf/compiler
  542. #
  543. # Helper to see if update-proto-files should be done
  544. #
  545. check-proto-files: check-for-protobuf-checkout
  546. @for p in `cd ${GOOGLE_PROTOBUF_CHECKOUT} && ls conformance/*.proto conformance/test_protos/*.proto`; do \
  547. diff -u "Protos/upstream/$$p" "${GOOGLE_PROTOBUF_CHECKOUT}/$$p" \
  548. || (echo "ERROR: Time to do a 'make update-proto-files'" && exit 1); \
  549. done
  550. @for p in `cd ${GOOGLE_PROTOBUF_CHECKOUT}/src && ls google/protobuf/*.proto | grep -v test`; do \
  551. diff -u "Protos/upstream/$$p" "${GOOGLE_PROTOBUF_CHECKOUT}/src/$$p" \
  552. || (echo "ERROR: Time to do a 'make update-proto-files'" && exit 1); \
  553. done
  554. @for p in `cd ${GOOGLE_PROTOBUF_CHECKOUT}/src && ls google/protobuf/compiler/*.proto`; do \
  555. diff -u "Protos/upstream/$$p" "${GOOGLE_PROTOBUF_CHECKOUT}/src/$$p" \
  556. || (echo "ERROR: Time to do a 'make update-proto-files'" && exit 1); \
  557. done
  558. # Runs the conformance tests.
  559. test-conformance: build check-for-protobuf-checkout Sources/Conformance/failure_list_swift.txt Sources/Conformance/text_format_failure_list_swift.txt
  560. $(CONFORMANCE_TEST_RUNNER) \
  561. --enforce_recommended \
  562. --failure_list Sources/Conformance/failure_list_swift.txt \
  563. --text_format_failure_list Sources/Conformance/text_format_failure_list_swift.txt \
  564. --maximum_edition 2023 \
  565. $(SWIFT_CONFORMANCE_PLUGIN)
  566. # Validate the CocoaPods podspec file against the current tree state.
  567. pod-lib-lint:
  568. @if [ `uname -s` = "Darwin" ] ; then \
  569. pod lib lint SwiftProtobuf.podspec ; \
  570. fi