Makefile 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  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. test-spm-plugin:
  190. @SWIFT_VERSION=$$(${SWIFT} --version | head -n1 | sed 's/.*Swift version \([0-9]*\)\..*/\1/'); \
  191. if [ "$$SWIFT_VERSION" -lt 6 ]; then \
  192. env PROTOC_PATH=$$(realpath ${PROTOC}) ${SWIFT} test --package-path PluginExamples; \
  193. else \
  194. ${SWIFT} test --package-path PluginExamples; \
  195. fi
  196. compile-tests: \
  197. compile-tests-multimodule \
  198. compile-tests-internalimportsbydefault
  199. # Test that ensures generating public into multiple modules with `import public`
  200. # yields buildable code.
  201. compile-tests-multimodule:
  202. ${SWIFT} test --package-path CompileTests/MultiModule
  203. # Test that ensures that using access level modifiers on imports yields code that's buildable
  204. # when `InternalImportsByDefault` is enabled on the module.
  205. compile-tests-internalimportsbydefault:
  206. env PROTOC_PATH=$(shell realpath ${PROTOC}) ${SWIFT} build --package-path CompileTests/InternalImportsByDefault
  207. # Rebuild the reference files by running the local version of protoc-gen-swift
  208. # against our menagerie of sample protos.
  209. #
  210. # If you do this, you MUST MANUALLY verify these files before checking them in,
  211. # since the new checkin will become the new main reference.
  212. #
  213. # Note: Some of the upstream protos define the same package.(message|enum)s, so
  214. # they can't be done in a single protoc/proto-gen-swift invoke and have to be
  215. # done one at a time instead.
  216. reference: build ${PROTOC_GEN_SWIFT} ${PROTOC}
  217. @rm -rf Reference && mkdir -p Reference/upstream
  218. for p in `find Protos/upstream -type f -name '*.proto'`; do \
  219. ${GENERATE_SRCS_BASE} \
  220. -I Protos/upstream \
  221. --tfiws_out=Reference/upstream $$p || exit 1; \
  222. done
  223. for d in ${PROTOS_DIRS}; do \
  224. mkdir -p Reference/$$d ; \
  225. ${GENERATE_SRCS_BASE} \
  226. -I Protos/Sources/SwiftProtobuf \
  227. -I Protos/Sources/SwiftProtobufPluginLibrary \
  228. -I Protos/$$d \
  229. --tfiws_out=Reference/$$d \
  230. `find Protos/$$d -type f -name "*.proto"` || exit 1; \
  231. done
  232. @mkdir -p Reference/CompileTests/MultiModule
  233. ${GENERATE_SRCS} \
  234. -I Protos/CompileTests/MultiModule \
  235. --tfiws_opt=Visibility=Public \
  236. --tfiws_opt=ProtoPathModuleMappings=Protos/CompileTests/MultiModule/module_mappings.pbascii \
  237. --tfiws_out=Reference/CompileTests/MultiModule \
  238. `(find Protos/CompileTests/MultiModule -type f -name "*.proto")`
  239. @mkdir -p Reference/CompileTests/InternalImportsByDefault
  240. ${GENERATE_SRCS} \
  241. -I Protos/CompileTests/InternalImportsByDefault \
  242. --tfiws_opt=Visibility=Public \
  243. --tfiws_opt=UseAccessLevelOnImports=true \
  244. --tfiws_out=Reference/CompileTests/InternalImportsByDefault \
  245. `(find Protos/CompileTests/InternalImportsByDefault -type f -name "*.proto")`
  246. #
  247. # Rebuild the generated .pb.swift test files by running
  248. # protoc over all the relevant inputs.
  249. #
  250. # Before running this, ensure that:
  251. # * protoc-gen-swift is built and installed somewhere in your system PATH
  252. # * protoc is built and installed
  253. # * PROTOC at the top of this file is set correctly
  254. #
  255. regenerate: \
  256. regenerate-library-protos \
  257. regenerate-fuzz-protos \
  258. regenerate-plugin-protos \
  259. regenerate-test-protos \
  260. regenerate-compiletests-protos \
  261. regenerate-conformance-protos \
  262. Sources/SwiftProtobufPluginLibrary/PluginLibEditionDefaults.swift \
  263. Tests/protoc-gen-swiftTests/DescriptorTestData.swift \
  264. Tests/SwiftProtobufPluginLibraryTests/DescriptorTestData.swift \
  265. Tests/SwiftProtobufPluginLibraryTests/PluginLibTestingEditionDefaults.swift
  266. # Rebuild just the protos included in the runtime library
  267. # NOTE: dependencies doesn't include the source .proto files, should fix that;
  268. # would also need to list all the outputs.
  269. regenerate-library-protos: build ${PROTOC_GEN_SWIFT} ${PROTOC}
  270. find Sources/SwiftProtobuf -name "*.pb.swift" -exec rm -f {} \;
  271. ${GENERATE_SRCS} \
  272. --tfiws_opt=FileNaming=DropPath \
  273. --tfiws_opt=Visibility=Public \
  274. --tfiws_out=Sources/SwiftProtobuf \
  275. `find Protos/Sources/SwiftProtobuf -type f -name "*.proto"`
  276. # Rebuild just the protos used by the plugin
  277. # NOTE: dependencies doesn't include the source .proto files, should fix that;
  278. # would also need to list all the outputs.
  279. regenerate-plugin-protos: build ${PROTOC_GEN_SWIFT} ${PROTOC}
  280. find Sources/SwiftProtobufPluginLibrary -name "*.pb.swift" -exec rm -f {} \;
  281. ${GENERATE_SRCS} \
  282. -I Protos/Sources/SwiftProtobufPluginLibrary \
  283. --tfiws_opt=FileNaming=DropPath \
  284. --tfiws_opt=Visibility=Public \
  285. --tfiws_out=Sources/SwiftProtobufPluginLibrary \
  286. `find Protos/Sources/SwiftProtobufPluginLibrary -type f -name "*.proto"`
  287. # Is this based on the upstream bazel rules `compile_edition_defaults` and
  288. # `embed_edition_defaults`.
  289. Sources/SwiftProtobufPluginLibrary/PluginLibEditionDefaults.swift: build ${PROTOC_GEN_SWIFT} ${PROTOC} Protos/Sources/SwiftProtobuf/google/protobuf/descriptor.proto
  290. @${PROTOC} \
  291. --edition_defaults_out=PluginLibEditionDefaults.bin \
  292. --edition_defaults_minimum=PROTO2 \
  293. --edition_defaults_maximum=2024 \
  294. -I Protos/Sources/SwiftProtobuf \
  295. Protos/Sources/SwiftProtobuf/google/protobuf/descriptor.proto
  296. @rm -f $@
  297. @echo '// See Makefile how this is generated.' >> $@
  298. @echo '// swift-format-ignore-file' >> $@
  299. @echo 'import Foundation' >> $@
  300. @echo 'let bundledFeatureSetDefaultBytes: [UInt8] = [' >> $@
  301. @xxd -i < PluginLibEditionDefaults.bin >> $@
  302. @echo ']' >> $@
  303. # Some defaults for the testing of custom features
  304. Tests/SwiftProtobufPluginLibraryTests/PluginLibTestingEditionDefaults.swift: build ${PROTOC_GEN_SWIFT} ${PROTOC} Protos/Tests/SwiftProtobufPluginLibraryTests/test_features.proto
  305. @${PROTOC} \
  306. --edition_defaults_out=PluginLibTestingEditionDefaults.bin \
  307. --edition_defaults_minimum=PROTO2 \
  308. --edition_defaults_maximum=2024 \
  309. -I Protos/Sources/SwiftProtobuf \
  310. -I Protos/Tests/SwiftProtobufPluginLibraryTests \
  311. Protos/Tests/SwiftProtobufPluginLibraryTests/test_features.proto
  312. @rm -f $@
  313. @echo '// See Makefile how this is generated.' >> $@
  314. @echo '// swift-format-ignore-file' >> $@
  315. @echo 'import Foundation' >> $@
  316. @echo 'let testFeatureSetDefaultBytes: [UInt8] = [' >> $@
  317. @xxd -i < PluginLibTestingEditionDefaults.bin >> $@
  318. @echo ']' >> $@
  319. # Rebuild just the protos used by the tests
  320. # NOTE: dependencies doesn't include the source .proto files, should fix that;
  321. # would also need to list all the outputs.
  322. 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
  323. find Tests/SwiftProtobufTests -name "*.pb.swift" -exec rm -f {} \;
  324. ${GENERATE_SRCS} \
  325. -I Protos/Tests/SwiftProtobufTests \
  326. --tfiws_opt=FileNaming=DropPath \
  327. --tfiws_out=Tests/SwiftProtobufTests \
  328. `find Protos/Tests/SwiftProtobufTests -type f -name "*.proto"`
  329. find Tests/SwiftProtobufPluginLibraryTests -name "*.pb.swift" -exec rm -f {} \;
  330. ${GENERATE_SRCS} \
  331. -I Protos/Tests/SwiftProtobufPluginLibraryTests \
  332. --tfiws_opt=FileNaming=DropPath \
  333. --tfiws_out=Tests/SwiftProtobufPluginLibraryTests \
  334. `find Protos/Tests/SwiftProtobufPluginLibraryTests -type f -name "*.proto"`
  335. # Rebuild the protos for FuzzTesting/Sources/FuzzCommon, the file lives in the
  336. # Protos/Tests/SwiftProtobufTests to have just one copy.
  337. regenerate-fuzz-protos: build ${PROTOC_GEN_SWIFT} ${PROTOC}
  338. find FuzzTesting/Sources/FuzzCommon -name "*.pb.swift" -exec rm -f {} \;
  339. ${GENERATE_SRCS} \
  340. -I Protos/Tests/SwiftProtobufTests \
  341. --tfiws_opt=FileNaming=DropPath \
  342. --tfiws_opt=Visibility=Public \
  343. --tfiws_out=FuzzTesting/Sources/FuzzCommon \
  344. Protos/Tests/SwiftProtobufTests/fuzz_testing.proto
  345. SWIFT_PLUGINLIB_DESCRIPTOR_TEST_PROTOS= \
  346. Protos/Tests/SwiftProtobufPluginLibraryTests/pluginlib_descriptor_test.proto \
  347. Protos/Tests/SwiftProtobufPluginLibraryTests/pluginlib_descriptor_test2.proto \
  348. Protos/Tests/SwiftProtobufPluginLibraryTests/pluginlib_descriptor_test_import.proto \
  349. Protos/Tests/SwiftProtobufPluginLibraryTests/pluginlib_descriptor_delimited.proto \
  350. Protos/Tests/SwiftProtobufPluginLibraryTests/unittest_delimited.proto \
  351. Protos/Tests/SwiftProtobufPluginLibraryTests/unittest_delimited_import.proto \
  352. Protos/Sources/SwiftProtobufPluginLibrary/swift_protobuf_module_mappings.proto
  353. Tests/SwiftProtobufPluginLibraryTests/DescriptorTestData.swift: build ${PROTOC_GEN_SWIFT} ${PROTOC} ${SWIFT_PLUGINLIB_DESCRIPTOR_TEST_PROTOS}
  354. @${PROTOC} \
  355. --include_source_info \
  356. --descriptor_set_out=PluginLibDescriptorTestData.bin \
  357. -I Protos/Sources/SwiftProtobuf \
  358. -I Protos/Sources/SwiftProtobufPluginLibrary \
  359. -I Protos/Tests/SwiftProtobufPluginLibraryTests \
  360. ${SWIFT_PLUGINLIB_DESCRIPTOR_TEST_PROTOS}
  361. @rm -f $@
  362. @echo '// See Makefile how this is generated.' >> $@
  363. @echo '// swift-format-ignore-file' >> $@
  364. @echo 'import Foundation' >> $@
  365. @echo 'let fileDescriptorSetBytes: [UInt8] = [' >> $@
  366. @xxd -i < PluginLibDescriptorTestData.bin >> $@
  367. @echo ']' >> $@
  368. SWIFT_PLUGIN_DESCRIPTOR_TEST_PROTOS= \
  369. Protos/Tests/protoc-gen-swiftTests/plugin_descriptor_test.proto
  370. Tests/protoc-gen-swiftTests/DescriptorTestData.swift: build ${PROTOC_GEN_SWIFT} ${PROTOC} ${SWIFT_PLUGIN_DESCRIPTOR_TEST_PROTOS}
  371. @${PROTOC} \
  372. --descriptor_set_out=PluginDescriptorTestData.bin \
  373. -I Protos/Tests/protoc-gen-swiftTests \
  374. ${SWIFT_PLUGIN_DESCRIPTOR_TEST_PROTOS}
  375. @rm -f $@
  376. @echo '// See Makefile how this is generated.' >> $@
  377. @echo '// swift-format-ignore-file' >> $@
  378. @echo 'import Foundation' >> $@
  379. @echo 'let fileDescriptorSetBytes: [UInt8] = [' >> $@
  380. @xxd -i < PluginDescriptorTestData.bin >> $@
  381. @echo ']' >> $@
  382. #
  383. # Collect a list of words that appear in the SwiftProtobuf library
  384. # source. These are words that may cause problems for generated code.
  385. #
  386. # The logic here builds a word list as follows:
  387. # = Look at every Swift source file in the library
  388. # = Take every line with the word 'public', 'func', or 'var'
  389. # = Remove any comments from the line.
  390. # = Remove any string literals from the line.
  391. # = Break each such line into words (stripping all punctuation)
  392. # = Remove words that differ only in case
  393. # = Remove anything that will cause proto parsing issues (things named "reserved")
  394. #
  395. # Selecting lines with 'public', 'func' or 'var' ensures we get every
  396. # public protocol, struct, enum, or class name, as well as every
  397. # method or property defined in a public protocol, struct, or class.
  398. # It also gives us a large collection of Swift names.
  399. Protos/mined_words.txt: Sources/SwiftProtobuf/*.swift
  400. @echo Building $@
  401. @cat $^ | \
  402. grep -E '\b(public|func|var)\b' | \
  403. grep -vE '\b(private|internal|fileprivate)\b' | \
  404. sed -e 's|//.*$$||g' | \
  405. sed -e 's|"\([^"\\]*\\.\)*[^"]*"||g' | \
  406. sed -e 's/[^a-zA-Z0-9_]/ /g' | \
  407. tr " " "\n" | \
  408. sed -e 's/^_//' | \
  409. sort -uf | \
  410. grep -vE '(reserved)' | \
  411. grep '^[a-zA-Z_]' > $@
  412. # Build some proto files full of landmines
  413. #
  414. # This takes the word list Protos/mined_words.txt and uses
  415. # it to build several proto files:
  416. # = Build a message with one `int32` field for each word
  417. # = Build an enum with a case for each such word
  418. # = Build a message with a submessage named with each word
  419. # = Build a message with an enum named with each word
  420. #
  421. # If the Swift compiler can actually compile the result, that suggests
  422. # we can correctly handle every symbol in the library itself that
  423. # might cause problems. Failures compiling this indicate weaknesses
  424. # in protoc-gen-swift's name sanitization logic.
  425. #
  426. Protos/Tests/SwiftProtobufTests/generated_swift_names_fields.proto: Protos/mined_words.txt
  427. @echo Building $@
  428. @rm $@
  429. @echo '// See Makefile for the logic that generates this' >> $@
  430. @echo '// Protoc errors imply this file is being generated incorrectly' >> $@
  431. @echo '// Swift compile errors are probably bugs in protoc-gen-swift' >> $@
  432. @echo 'syntax = "proto3";' >> $@
  433. @echo 'package swift_proto_testing.generated;' >> $@
  434. @echo 'message GeneratedSwiftReservedFields {' >> $@
  435. @cat Protos/mined_words.txt | awk 'BEGIN{n = 1} {print " int32 " $$1 " = " n ";"; n += 1 }' >> $@
  436. @echo '}' >> $@
  437. Protos/Tests/SwiftProtobufTests/generated_swift_names_enum_cases.proto: Protos/mined_words.txt
  438. @echo Building $@
  439. @rm $@
  440. @echo '// See Makefile for the logic that generates this' >> $@
  441. @echo '// Protoc errors imply this file is being generated incorrectly' >> $@
  442. @echo '// Swift compile errors are probably bugs in protoc-gen-swift' >> $@
  443. @echo 'syntax = "proto3";' >> $@
  444. @echo 'package swift_proto_testing.generated;' >> $@
  445. @echo 'enum GeneratedSwiftReservedEnum {' >> $@
  446. @echo ' NONE = 0;' >> $@
  447. @cat Protos/mined_words.txt | awk 'BEGIN{n = 1} {print " " $$1 " = " n ";"; n += 1 }' >> $@
  448. @echo '}' >> $@
  449. Protos/Tests/SwiftProtobufTests/generated_swift_names_messages.proto: Protos/mined_words.txt
  450. @echo Building $@
  451. @rm $@
  452. @echo '// See Makefile for the logic that generates this' >> $@
  453. @echo '// Protoc errors imply this file is being generated incorrectly' >> $@
  454. @echo '// Swift compile errors are probably bugs in protoc-gen-swift' >> $@
  455. @echo 'syntax = "proto3";' >> $@
  456. @echo 'package swift_proto_testing.generated;' >> $@
  457. @echo 'message GeneratedSwiftReservedMessages {' >> $@
  458. @cat Protos/mined_words.txt | awk '{print " message " $$1 " { int32 " $$1 " = 1; }"}' >> $@
  459. @echo '}' >> $@
  460. Protos/Tests/SwiftProtobufTests/generated_swift_names_enums.proto: Protos/mined_words.txt
  461. @echo Building $@
  462. @rm $@
  463. @echo '// See Makefile for the logic that generates this' >> $@
  464. @echo '// Protoc errors imply this file is being generated incorrectly' >> $@
  465. @echo '// Swift compile errors are probably bugs in protoc-gen-swift' >> $@
  466. @echo 'syntax = "proto3";' >> $@
  467. @echo 'package swift_proto_testing.generated;' >> $@
  468. @echo 'message GeneratedSwiftReservedEnums {' >> $@
  469. @cat Protos/mined_words.txt | awk '{print " enum " $$1 " { NONE_" $$1 " = 0; }"}' >> $@
  470. @echo '}' >> $@
  471. # Rebuild just the protos used by the conformance test runner.
  472. regenerate-conformance-protos: build ${PROTOC_GEN_SWIFT} ${PROTOC}
  473. find Sources/Conformance -name "*.pb.swift" -exec rm -f {} \;
  474. ${GENERATE_SRCS} \
  475. -I Protos/Sources/Conformance \
  476. --tfiws_opt=FileNaming=DropPath \
  477. --tfiws_out=Sources/Conformance \
  478. `find Protos/Sources/Conformance -type f -name "*.proto"`
  479. # Rebuild just the protos used by the CompileTests.
  480. regenerate-compiletests-protos: \
  481. regenerate-compiletests-multimodule-protos \
  482. copy-compiletests-internalimportsbydefault-protos
  483. # Update the CompileTests/MultiModule files.
  484. # NOTE: Any changes here must also be done on the "test-plugin" target so it
  485. # generates in the same way.
  486. regenerate-compiletests-multimodule-protos: build ${PROTOC_GEN_SWIFT} ${PROTOC}
  487. find CompileTests/MultiModule -name "*.pb.swift" -exec rm -f {} \;
  488. ${GENERATE_SRCS} \
  489. -I Protos/CompileTests/MultiModule \
  490. --tfiws_opt=Visibility=Public \
  491. --tfiws_opt=ProtoPathModuleMappings=Protos/CompileTests/MultiModule/module_mappings.pbascii \
  492. --tfiws_out=CompileTests/MultiModule \
  493. `(find Protos/CompileTests/MultiModule -type f -name "*.proto")`
  494. # We use the plugin for the InternalImportsByDefault test, so we don't actually need to regenerate
  495. # anything. However, to keep the protos centralised in a single place (the Protos directory),
  496. # this simply copies those files to the InternalImportsByDefault package in case they change.
  497. copy-compiletests-internalimportsbydefault-protos:
  498. @cp Protos/CompileTests/InternalImportsByDefault/* CompileTests/InternalImportsByDefault/Sources/InternalImportsByDefault/Protos
  499. # Helper to check if there is a protobuf checkout as expected.
  500. check-for-protobuf-checkout:
  501. @if [ ! -d "${GOOGLE_PROTOBUF_CHECKOUT}/src/google/protobuf" ]; then \
  502. echo "ERROR: ${GOOGLE_PROTOBUF_CHECKOUT} does not appear to be a checkout of"; \
  503. echo "ERROR: github.com/protocolbuffers/protobuf. Please check it out or set"; \
  504. echo "ERROR: GOOGLE_PROTOBUF_CHECKOUT to point to a checkout."; \
  505. exit 1; \
  506. fi
  507. #
  508. # Helper to update the .proto files copied from the protocolbuffers/protobuf distro.
  509. #
  510. # (We also have to pick up some the the [LANG]_features.proto files for language
  511. # specific Editions, if when generating we used a release protoc, then they would
  512. # be copied like the WKTs to live "next too" the compiler and we wouldn't need to
  513. # provide them on input paths.)
  514. #
  515. update-proto-files: check-for-protobuf-checkout
  516. @rm -rf Protos/upstream
  517. @mkdir -p \
  518. Protos/upstream/conformance/test_protos \
  519. Protos/upstream/google/protobuf/compiler \
  520. Protos/upstream/editions/golden \
  521. Protos/upstream/go/google/protobuf \
  522. Protos/upstream/java/core/src/main/resources/google/protobuf
  523. @cp -v "${GOOGLE_PROTOBUF_CHECKOUT}"/conformance/*.proto Protos/upstream/conformance/
  524. @cp -v "${GOOGLE_PROTOBUF_CHECKOUT}"/conformance/test_protos/*.proto Protos/upstream/conformance/test_protos/
  525. @cp -v "${GOOGLE_PROTOBUF_CHECKOUT}"/src/google/protobuf/*.proto Protos/upstream/google/protobuf/
  526. @cp -v "${GOOGLE_PROTOBUF_CHECKOUT}"/src/google/protobuf/compiler/*.proto Protos/upstream/google/protobuf/compiler/
  527. @cp -v "${GOOGLE_PROTOBUF_CHECKOUT}"/editions/golden/test_messages_proto?_editions.proto Protos/upstream/editions/golden/
  528. @cp -v "${GOOGLE_PROTOBUF_CHECKOUT}"/go/google/protobuf/*_features.proto Protos/upstream/go/google/protobuf/
  529. @cp -v "${GOOGLE_PROTOBUF_CHECKOUT}"/java/core/src/main/resources/google/protobuf/*_features.proto Protos/upstream/java/core/src/main/resources/google/protobuf/
  530. # Now copy into the Proto directories for the local targets.
  531. @rm -rf Protos/Sources/Conformance/conformance/test_protos && mkdir -p Protos/Sources/Conformance/conformance/test_protos
  532. @cp -v Protos/upstream/conformance/*.proto Protos/Sources/Conformance/conformance
  533. @cp -v Protos/upstream/conformance/test_protos/*.proto Protos/Sources/Conformance/conformance/test_protos
  534. @rm -rf Protos/Sources/Conformance/google && mkdir -p Protos/Sources/Conformance/google/protobuf Protos/Sources/Conformance/editions
  535. @cp -v \
  536. Protos/upstream/google/protobuf/test_messages_proto2.proto \
  537. Protos/upstream/google/protobuf/test_messages_proto3.proto \
  538. Protos/Sources/Conformance/google/protobuf/
  539. @cp -v \
  540. Protos/upstream/editions/golden/test_messages_proto2_editions.proto \
  541. Protos/upstream/editions/golden/test_messages_proto3_editions.proto \
  542. Protos/Sources/Conformance/editions/
  543. @rm -rf Protos/Sources/SwiftProtobuf/google && mkdir -p Protos/Sources/SwiftProtobuf/google/protobuf
  544. @cp -v \
  545. Protos/upstream/google/protobuf/timestamp.proto \
  546. Protos/upstream/google/protobuf/field_mask.proto \
  547. Protos/upstream/google/protobuf/api.proto \
  548. Protos/upstream/google/protobuf/duration.proto \
  549. Protos/upstream/google/protobuf/struct.proto \
  550. Protos/upstream/google/protobuf/wrappers.proto \
  551. Protos/upstream/google/protobuf/source_context.proto \
  552. Protos/upstream/google/protobuf/any.proto \
  553. Protos/upstream/google/protobuf/type.proto \
  554. Protos/upstream/google/protobuf/empty.proto \
  555. Protos/upstream/google/protobuf/descriptor.proto \
  556. Protos/Sources/SwiftProtobuf/google/protobuf
  557. @rm -rf Protos/Sources/SwiftProtobufPluginLibrary/google && mkdir -p Protos/Sources/SwiftProtobufPluginLibrary/google/protobuf/compiler
  558. @cp -v Protos/upstream/google/protobuf/compiler/*.proto Protos/Sources/SwiftProtobufPluginLibrary/google/protobuf/compiler
  559. #
  560. # Helper to see if update-proto-files should be done
  561. #
  562. check-proto-files: check-for-protobuf-checkout
  563. @for p in `cd ${GOOGLE_PROTOBUF_CHECKOUT} && ls conformance/*.proto conformance/test_protos/*.proto`; do \
  564. diff -u "Protos/upstream/$$p" "${GOOGLE_PROTOBUF_CHECKOUT}/$$p" \
  565. || (echo "ERROR: Time to do a 'make update-proto-files'" && exit 1); \
  566. done
  567. @for p in `cd ${GOOGLE_PROTOBUF_CHECKOUT}/src && ls google/protobuf/*.proto | grep -v test`; do \
  568. diff -u "Protos/upstream/$$p" "${GOOGLE_PROTOBUF_CHECKOUT}/src/$$p" \
  569. || (echo "ERROR: Time to do a 'make update-proto-files'" && exit 1); \
  570. done
  571. @for p in `cd ${GOOGLE_PROTOBUF_CHECKOUT}/src && ls google/protobuf/compiler/*.proto`; do \
  572. diff -u "Protos/upstream/$$p" "${GOOGLE_PROTOBUF_CHECKOUT}/src/$$p" \
  573. || (echo "ERROR: Time to do a 'make update-proto-files'" && exit 1); \
  574. done
  575. # Runs the conformance tests.
  576. test-conformance: build check-for-protobuf-checkout Sources/Conformance/failure_list_swift.txt Sources/Conformance/text_format_failure_list_swift.txt
  577. $(CONFORMANCE_TEST_RUNNER) \
  578. --enforce_recommended \
  579. --failure_list Sources/Conformance/failure_list_swift.txt \
  580. --text_format_failure_list Sources/Conformance/text_format_failure_list_swift.txt \
  581. --maximum_edition 2024 \
  582. $(SWIFT_CONFORMANCE_PLUGIN)
  583. # Validate the CocoaPods podspec file against the current tree state.
  584. pod-lib-lint:
  585. @if [ `uname -s` = "Darwin" ] ; then \
  586. pod lib lint SwiftProtobuf.podspec ; \
  587. fi