Package.swift 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  1. // swift-tools-version:5.7
  2. /*
  3. * Copyright 2017, gRPC Authors All rights reserved.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. import PackageDescription
  18. // swiftformat puts the next import before the tools version.
  19. // swiftformat:disable:next sortImports
  20. import class Foundation.ProcessInfo
  21. let grpcPackageName = "grpc-swift"
  22. let grpcProductName = "GRPC"
  23. let cgrpcZlibProductName = "CGRPCZlib"
  24. let grpcTargetName = grpcProductName
  25. let cgrpcZlibTargetName = cgrpcZlibProductName
  26. let includeNIOSSL = ProcessInfo.processInfo.environment["GRPC_NO_NIO_SSL"] == nil
  27. // MARK: - Package Dependencies
  28. let packageDependencies: [Package.Dependency] = [
  29. .package(
  30. url: "https://github.com/apple/swift-nio.git",
  31. from: "2.58.0"
  32. ),
  33. .package(
  34. url: "https://github.com/apple/swift-nio-http2.git",
  35. from: "1.24.1"
  36. ),
  37. .package(
  38. url: "https://github.com/apple/swift-nio-transport-services.git",
  39. from: "1.15.0"
  40. ),
  41. .package(
  42. url: "https://github.com/apple/swift-nio-extras.git",
  43. from: "1.4.0"
  44. ),
  45. .package(
  46. url: "https://github.com/apple/swift-collections.git",
  47. from: "1.0.5"
  48. ),
  49. .package(
  50. url: "https://github.com/apple/swift-atomics.git",
  51. from: "1.2.0"
  52. ),
  53. .package(
  54. url: "https://github.com/apple/swift-protobuf.git",
  55. from: "1.20.2"
  56. ),
  57. .package(
  58. url: "https://github.com/apple/swift-log.git",
  59. from: "1.4.4"
  60. ),
  61. .package(
  62. url: "https://github.com/apple/swift-argument-parser.git",
  63. // Version is higher than in other Package@swift manifests: 1.1.0 raised the minimum Swift
  64. // version and indluded async support.
  65. from: "1.1.1"
  66. ),
  67. .package(
  68. url: "https://github.com/apple/swift-docc-plugin",
  69. from: "1.0.0"
  70. ),
  71. ].appending(
  72. .package(
  73. url: "https://github.com/apple/swift-nio-ssl.git",
  74. from: "2.23.0"
  75. ),
  76. if: includeNIOSSL
  77. )
  78. // MARK: - Target Dependencies
  79. extension Target.Dependency {
  80. // Target dependencies; external
  81. static let grpc: Self = .target(name: grpcTargetName)
  82. static let cgrpcZlib: Self = .target(name: cgrpcZlibTargetName)
  83. static let protocGenGRPCSwift: Self = .target(name: "protoc-gen-grpc-swift")
  84. static let reflectionService: Self = .target(name: "GRPCReflectionService")
  85. static let grpcCodeGen: Self = .target(name: "GRPCCodeGen")
  86. // Target dependencies; internal
  87. static let grpcSampleData: Self = .target(name: "GRPCSampleData")
  88. static let echoModel: Self = .target(name: "EchoModel")
  89. static let echoImplementation: Self = .target(name: "EchoImplementation")
  90. static let helloWorldModel: Self = .target(name: "HelloWorldModel")
  91. static let routeGuideModel: Self = .target(name: "RouteGuideModel")
  92. static let interopTestModels: Self = .target(name: "GRPCInteroperabilityTestModels")
  93. static let interopTestImplementation: Self =
  94. .target(name: "GRPCInteroperabilityTestsImplementation")
  95. // Product dependencies
  96. static let argumentParser: Self = .product(
  97. name: "ArgumentParser",
  98. package: "swift-argument-parser"
  99. )
  100. static let nio: Self = .product(name: "NIO", package: "swift-nio")
  101. static let nioConcurrencyHelpers: Self = .product(
  102. name: "NIOConcurrencyHelpers",
  103. package: "swift-nio"
  104. )
  105. static let nioCore: Self = .product(name: "NIOCore", package: "swift-nio")
  106. static let nioEmbedded: Self = .product(name: "NIOEmbedded", package: "swift-nio")
  107. static let nioExtras: Self = .product(name: "NIOExtras", package: "swift-nio-extras")
  108. static let nioFoundationCompat: Self = .product(name: "NIOFoundationCompat", package: "swift-nio")
  109. static let nioHTTP1: Self = .product(name: "NIOHTTP1", package: "swift-nio")
  110. static let nioHTTP2: Self = .product(name: "NIOHTTP2", package: "swift-nio-http2")
  111. static let nioPosix: Self = .product(name: "NIOPosix", package: "swift-nio")
  112. static let nioSSL: Self = .product(name: "NIOSSL", package: "swift-nio-ssl")
  113. static let nioTLS: Self = .product(name: "NIOTLS", package: "swift-nio")
  114. static let nioTransportServices: Self = .product(
  115. name: "NIOTransportServices",
  116. package: "swift-nio-transport-services"
  117. )
  118. static let logging: Self = .product(name: "Logging", package: "swift-log")
  119. static let protobuf: Self = .product(name: "SwiftProtobuf", package: "swift-protobuf")
  120. static let protobufPluginLibrary: Self = .product(
  121. name: "SwiftProtobufPluginLibrary",
  122. package: "swift-protobuf"
  123. )
  124. static let dequeModule: Self = .product(name: "DequeModule", package: "swift-collections")
  125. static let atomics: Self = .product(name: "Atomics", package: "swift-atomics")
  126. static let grpcCore: Self = .target(name: "GRPCCore")
  127. static let grpcInProcessTransport: Self = .target(name: "GRPCInProcessTransport")
  128. }
  129. // MARK: - Targets
  130. extension Target {
  131. static let grpc: Target = .target(
  132. name: grpcTargetName,
  133. dependencies: [
  134. .cgrpcZlib,
  135. .nio,
  136. .nioCore,
  137. .nioPosix,
  138. .nioEmbedded,
  139. .nioFoundationCompat,
  140. .nioTLS,
  141. .nioTransportServices,
  142. .nioHTTP1,
  143. .nioHTTP2,
  144. .nioExtras,
  145. .logging,
  146. .protobuf,
  147. .dequeModule,
  148. ].appending(
  149. .nioSSL, if: includeNIOSSL
  150. ),
  151. path: "Sources/GRPC"
  152. )
  153. static let grpcCore: Target = .target(
  154. name: "GRPCCore",
  155. dependencies: [
  156. .dequeModule,
  157. .atomics
  158. ],
  159. path: "Sources/GRPCCore"
  160. )
  161. static let grpcInProcessTransport: Target = .target(
  162. name: "GRPCInProcessTransport",
  163. dependencies: [
  164. .grpcCore
  165. ]
  166. )
  167. static let cgrpcZlib: Target = .target(
  168. name: cgrpcZlibTargetName,
  169. path: "Sources/CGRPCZlib",
  170. linkerSettings: [
  171. .linkedLibrary("z"),
  172. ]
  173. )
  174. static let protocGenGRPCSwift: Target = .executableTarget(
  175. name: "protoc-gen-grpc-swift",
  176. dependencies: [
  177. .protobuf,
  178. .protobufPluginLibrary,
  179. ],
  180. exclude: [
  181. "README.md",
  182. ]
  183. )
  184. static let grpcSwiftPlugin: Target = .plugin(
  185. name: "GRPCSwiftPlugin",
  186. capability: .buildTool(),
  187. dependencies: [
  188. .protocGenGRPCSwift,
  189. ]
  190. )
  191. static let grpcTests: Target = .testTarget(
  192. name: "GRPCTests",
  193. dependencies: [
  194. .grpc,
  195. .echoModel,
  196. .echoImplementation,
  197. .helloWorldModel,
  198. .interopTestModels,
  199. .interopTestImplementation,
  200. .grpcSampleData,
  201. .nioCore,
  202. .nioConcurrencyHelpers,
  203. .nioPosix,
  204. .nioTLS,
  205. .nioHTTP1,
  206. .nioHTTP2,
  207. .nioEmbedded,
  208. .nioTransportServices,
  209. .logging,
  210. .reflectionService
  211. ].appending(
  212. .nioSSL, if: includeNIOSSL
  213. ),
  214. exclude: [
  215. "Codegen/Normalization/normalization.proto",
  216. "Codegen/Serialization/echo.grpc.reflection",
  217. ]
  218. )
  219. static let grpcCoreTests: Target = .testTarget(
  220. name: "GRPCCoreTests",
  221. dependencies: [
  222. .grpcCore,
  223. .grpcInProcessTransport,
  224. .dequeModule,
  225. .atomics
  226. ]
  227. )
  228. static let grpcInProcessTransportTests: Target = .testTarget(
  229. name: "GRPCInProcessTransportTests",
  230. dependencies: [
  231. .grpcCore,
  232. .grpcInProcessTransport,
  233. ]
  234. )
  235. static let grpcCodeGenTests: Target = .testTarget(
  236. name: "GRPCCodeGenTests",
  237. dependencies: [
  238. .grpcCodeGen
  239. ]
  240. )
  241. static let interopTestModels: Target = .target(
  242. name: "GRPCInteroperabilityTestModels",
  243. dependencies: [
  244. .grpc,
  245. .nio,
  246. .protobuf,
  247. ],
  248. exclude: [
  249. "README.md",
  250. "generate.sh",
  251. "src/proto/grpc/testing/empty.proto",
  252. "src/proto/grpc/testing/empty_service.proto",
  253. "src/proto/grpc/testing/messages.proto",
  254. "src/proto/grpc/testing/test.proto",
  255. "unimplemented_call.patch",
  256. ]
  257. )
  258. static let interopTestImplementation: Target = .target(
  259. name: "GRPCInteroperabilityTestsImplementation",
  260. dependencies: [
  261. .grpc,
  262. .interopTestModels,
  263. .nioCore,
  264. .nioPosix,
  265. .nioHTTP1,
  266. .logging,
  267. ].appending(
  268. .nioSSL, if: includeNIOSSL
  269. )
  270. )
  271. static let interopTests: Target = .executableTarget(
  272. name: "GRPCInteroperabilityTests",
  273. dependencies: [
  274. .grpc,
  275. .interopTestImplementation,
  276. .nioCore,
  277. .nioPosix,
  278. .logging,
  279. .argumentParser,
  280. ]
  281. )
  282. static let backoffInteropTest: Target = .executableTarget(
  283. name: "GRPCConnectionBackoffInteropTest",
  284. dependencies: [
  285. .grpc,
  286. .interopTestModels,
  287. .nioCore,
  288. .nioPosix,
  289. .logging,
  290. .argumentParser,
  291. ],
  292. exclude: [
  293. "README.md",
  294. ]
  295. )
  296. static let perfTests: Target = .executableTarget(
  297. name: "GRPCPerformanceTests",
  298. dependencies: [
  299. .grpc,
  300. .grpcSampleData,
  301. .nioCore,
  302. .nioEmbedded,
  303. .nioPosix,
  304. .nioHTTP2,
  305. .argumentParser,
  306. ]
  307. )
  308. static let grpcSampleData: Target = .target(
  309. name: "GRPCSampleData",
  310. dependencies: includeNIOSSL ? [.nioSSL] : [],
  311. exclude: [
  312. "bundle.p12",
  313. ]
  314. )
  315. static let echoModel: Target = .target(
  316. name: "EchoModel",
  317. dependencies: [
  318. .grpc,
  319. .nio,
  320. .protobuf,
  321. ],
  322. path: "Sources/Examples/Echo/Model",
  323. exclude: [
  324. "echo.proto",
  325. ]
  326. )
  327. static let echoImplementation: Target = .target(
  328. name: "EchoImplementation",
  329. dependencies: [
  330. .echoModel,
  331. .grpc,
  332. .nioCore,
  333. .nioHTTP2,
  334. .protobuf,
  335. ],
  336. path: "Sources/Examples/Echo/Implementation"
  337. )
  338. static let echo: Target = .executableTarget(
  339. name: "Echo",
  340. dependencies: [
  341. .grpc,
  342. .echoModel,
  343. .echoImplementation,
  344. .grpcSampleData,
  345. .nioCore,
  346. .nioPosix,
  347. .logging,
  348. .argumentParser,
  349. ].appending(
  350. .nioSSL, if: includeNIOSSL
  351. ),
  352. path: "Sources/Examples/Echo/Runtime"
  353. )
  354. static let helloWorldModel: Target = .target(
  355. name: "HelloWorldModel",
  356. dependencies: [
  357. .grpc,
  358. .nio,
  359. .protobuf,
  360. ],
  361. path: "Sources/Examples/HelloWorld/Model",
  362. exclude: [
  363. "helloworld.proto",
  364. ]
  365. )
  366. static let helloWorldClient: Target = .executableTarget(
  367. name: "HelloWorldClient",
  368. dependencies: [
  369. .grpc,
  370. .helloWorldModel,
  371. .nioCore,
  372. .nioPosix,
  373. .argumentParser,
  374. ],
  375. path: "Sources/Examples/HelloWorld/Client"
  376. )
  377. static let helloWorldServer: Target = .executableTarget(
  378. name: "HelloWorldServer",
  379. dependencies: [
  380. .grpc,
  381. .helloWorldModel,
  382. .nioCore,
  383. .nioPosix,
  384. .argumentParser,
  385. ],
  386. path: "Sources/Examples/HelloWorld/Server"
  387. )
  388. static let routeGuideModel: Target = .target(
  389. name: "RouteGuideModel",
  390. dependencies: [
  391. .grpc,
  392. .nio,
  393. .protobuf,
  394. ],
  395. path: "Sources/Examples/RouteGuide/Model",
  396. exclude: [
  397. "route_guide.proto",
  398. ]
  399. )
  400. static let routeGuideClient: Target = .executableTarget(
  401. name: "RouteGuideClient",
  402. dependencies: [
  403. .grpc,
  404. .routeGuideModel,
  405. .nioCore,
  406. .nioPosix,
  407. .argumentParser,
  408. ],
  409. path: "Sources/Examples/RouteGuide/Client"
  410. )
  411. static let routeGuideServer: Target = .executableTarget(
  412. name: "RouteGuideServer",
  413. dependencies: [
  414. .grpc,
  415. .routeGuideModel,
  416. .nioCore,
  417. .nioConcurrencyHelpers,
  418. .nioPosix,
  419. .argumentParser,
  420. ],
  421. path: "Sources/Examples/RouteGuide/Server"
  422. )
  423. static let packetCapture: Target = .executableTarget(
  424. name: "PacketCapture",
  425. dependencies: [
  426. .grpc,
  427. .echoModel,
  428. .nioCore,
  429. .nioPosix,
  430. .nioExtras,
  431. .argumentParser,
  432. ],
  433. path: "Sources/Examples/PacketCapture",
  434. exclude: [
  435. "README.md",
  436. ]
  437. )
  438. static let reflectionService: Target = .target(
  439. name: "GRPCReflectionService",
  440. dependencies: [
  441. .grpc,
  442. .nio,
  443. .protobuf,
  444. ],
  445. path: "Sources/GRPCReflectionService",
  446. exclude: [
  447. "v1/reflection-v1.proto",
  448. "v1Alpha/reflection-v1alpha.proto"
  449. ]
  450. )
  451. static let reflectionServer: Target = .executableTarget(
  452. name: "ReflectionServer",
  453. dependencies: [
  454. .grpc,
  455. .reflectionService,
  456. .helloWorldModel,
  457. .nioCore,
  458. .nioPosix,
  459. .argumentParser,
  460. .echoModel,
  461. .echoImplementation
  462. ],
  463. path: "Sources/Examples/ReflectionService",
  464. resources: [
  465. .copy("Generated")
  466. ]
  467. )
  468. static let grpcCodeGen: Target = .target(
  469. name: "GRPCCodeGen",
  470. path: "Sources/GRPCCodeGen"
  471. )
  472. }
  473. // MARK: - Products
  474. extension Product {
  475. static let grpc: Product = .library(
  476. name: grpcProductName,
  477. targets: [grpcTargetName]
  478. )
  479. static let grpcCore: Product = .library(
  480. name: "_GRPCCore",
  481. targets: ["GRPCCore"]
  482. )
  483. static let cgrpcZlib: Product = .library(
  484. name: cgrpcZlibProductName,
  485. targets: [cgrpcZlibTargetName]
  486. )
  487. static let grpcReflectionService: Product = .library(
  488. name: "GRPCReflectionService",
  489. targets: ["GRPCReflectionService"]
  490. )
  491. static let protocGenGRPCSwift: Product = .executable(
  492. name: "protoc-gen-grpc-swift",
  493. targets: ["protoc-gen-grpc-swift"]
  494. )
  495. static let grpcSwiftPlugin: Product = .plugin(
  496. name: "GRPCSwiftPlugin",
  497. targets: ["GRPCSwiftPlugin"]
  498. )
  499. }
  500. // MARK: - Package
  501. let package = Package(
  502. name: grpcPackageName,
  503. products: [
  504. .grpc,
  505. .grpcCore,
  506. .cgrpcZlib,
  507. .grpcReflectionService,
  508. .protocGenGRPCSwift,
  509. .grpcSwiftPlugin,
  510. ],
  511. dependencies: packageDependencies,
  512. targets: [
  513. // Products
  514. .grpc,
  515. .cgrpcZlib,
  516. .protocGenGRPCSwift,
  517. .grpcSwiftPlugin,
  518. .reflectionService,
  519. // Tests etc.
  520. .grpcTests,
  521. .interopTestModels,
  522. .interopTestImplementation,
  523. .interopTests,
  524. .backoffInteropTest,
  525. .perfTests,
  526. .grpcSampleData,
  527. // Examples
  528. .echoModel,
  529. .echoImplementation,
  530. .echo,
  531. .helloWorldModel,
  532. .helloWorldClient,
  533. .helloWorldServer,
  534. .routeGuideModel,
  535. .routeGuideClient,
  536. .routeGuideServer,
  537. .packetCapture,
  538. .reflectionServer,
  539. // v2
  540. .grpcCore,
  541. .grpcInProcessTransport,
  542. .grpcCodeGen,
  543. // v2 tests
  544. .grpcCoreTests,
  545. .grpcInProcessTransportTests,
  546. .grpcCodeGenTests
  547. ]
  548. )
  549. extension Array {
  550. func appending(_ element: Element, if condition: Bool) -> [Element] {
  551. if condition {
  552. return self + [element]
  553. } else {
  554. return self
  555. }
  556. }
  557. }