Makefile 27 KB

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