Makefile 28 KB

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