Makefile 27 KB

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