Package.swift 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  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-protobuf.git",
  51. from: "1.20.2"
  52. ),
  53. .package(
  54. url: "https://github.com/apple/swift-log.git",
  55. from: "1.4.4"
  56. ),
  57. .package(
  58. url: "https://github.com/apple/swift-argument-parser.git",
  59. // Version is higher than in other Package@swift manifests: 1.1.0 raised the minimum Swift
  60. // version and indluded async support.
  61. from: "1.1.1"
  62. ),
  63. .package(
  64. url: "https://github.com/apple/swift-docc-plugin",
  65. from: "1.0.0"
  66. ),
  67. ].appending(
  68. .package(
  69. url: "https://github.com/apple/swift-nio-ssl.git",
  70. from: "2.23.0"
  71. ),
  72. if: includeNIOSSL
  73. )
  74. // MARK: - Target Dependencies
  75. extension Target.Dependency {
  76. // Target dependencies; external
  77. static let grpc: Self = .target(name: grpcTargetName)
  78. static let cgrpcZlib: Self = .target(name: cgrpcZlibTargetName)
  79. static let protocGenGRPCSwift: Self = .target(name: "protoc-gen-grpc-swift")
  80. // Target dependencies; internal
  81. static let grpcSampleData: Self = .target(name: "GRPCSampleData")
  82. static let echoModel: Self = .target(name: "EchoModel")
  83. static let echoImplementation: Self = .target(name: "EchoImplementation")
  84. static let helloWorldModel: Self = .target(name: "HelloWorldModel")
  85. static let routeGuideModel: Self = .target(name: "RouteGuideModel")
  86. static let interopTestModels: Self = .target(name: "GRPCInteroperabilityTestModels")
  87. static let interopTestImplementation: Self =
  88. .target(name: "GRPCInteroperabilityTestsImplementation")
  89. static let reflectionService: Self = .target(name: "GRPCReflectionService")
  90. // Product dependencies
  91. static let argumentParser: Self = .product(
  92. name: "ArgumentParser",
  93. package: "swift-argument-parser"
  94. )
  95. static let nio: Self = .product(name: "NIO", package: "swift-nio")
  96. static let nioConcurrencyHelpers: Self = .product(
  97. name: "NIOConcurrencyHelpers",
  98. package: "swift-nio"
  99. )
  100. static let nioCore: Self = .product(name: "NIOCore", package: "swift-nio")
  101. static let nioEmbedded: Self = .product(name: "NIOEmbedded", package: "swift-nio")
  102. static let nioExtras: Self = .product(name: "NIOExtras", package: "swift-nio-extras")
  103. static let nioFoundationCompat: Self = .product(name: "NIOFoundationCompat", package: "swift-nio")
  104. static let nioHTTP1: Self = .product(name: "NIOHTTP1", package: "swift-nio")
  105. static let nioHTTP2: Self = .product(name: "NIOHTTP2", package: "swift-nio-http2")
  106. static let nioPosix: Self = .product(name: "NIOPosix", package: "swift-nio")
  107. static let nioSSL: Self = .product(name: "NIOSSL", package: "swift-nio-ssl")
  108. static let nioTLS: Self = .product(name: "NIOTLS", package: "swift-nio")
  109. static let nioTransportServices: Self = .product(
  110. name: "NIOTransportServices",
  111. package: "swift-nio-transport-services"
  112. )
  113. static let logging: Self = .product(name: "Logging", package: "swift-log")
  114. static let protobuf: Self = .product(name: "SwiftProtobuf", package: "swift-protobuf")
  115. static let protobufPluginLibrary: Self = .product(
  116. name: "SwiftProtobufPluginLibrary",
  117. package: "swift-protobuf"
  118. )
  119. static let dequeModule: Self = .product(name: "DequeModule", package: "swift-collections")
  120. static let grpcCore: Self = .target(name: "GRPCCore")
  121. }
  122. // MARK: - Targets
  123. extension Target {
  124. static let grpc: Target = .target(
  125. name: grpcTargetName,
  126. dependencies: [
  127. .cgrpcZlib,
  128. .nio,
  129. .nioCore,
  130. .nioPosix,
  131. .nioEmbedded,
  132. .nioFoundationCompat,
  133. .nioTLS,
  134. .nioTransportServices,
  135. .nioHTTP1,
  136. .nioHTTP2,
  137. .nioExtras,
  138. .logging,
  139. .protobuf,
  140. .dequeModule,
  141. ].appending(
  142. .nioSSL, if: includeNIOSSL
  143. ),
  144. path: "Sources/GRPC"
  145. )
  146. static let grpcCore: Target = .target(
  147. name: "GRPCCore",
  148. dependencies: [
  149. .dequeModule
  150. ],
  151. path: "Sources/GRPCCore"
  152. )
  153. static let cgrpcZlib: Target = .target(
  154. name: cgrpcZlibTargetName,
  155. path: "Sources/CGRPCZlib",
  156. linkerSettings: [
  157. .linkedLibrary("z"),
  158. ]
  159. )
  160. static let protocGenGRPCSwift: Target = .executableTarget(
  161. name: "protoc-gen-grpc-swift",
  162. dependencies: [
  163. .protobuf,
  164. .protobufPluginLibrary,
  165. ],
  166. exclude: [
  167. "README.md",
  168. ]
  169. )
  170. static let grpcSwiftPlugin: Target = .plugin(
  171. name: "GRPCSwiftPlugin",
  172. capability: .buildTool(),
  173. dependencies: [
  174. .protocGenGRPCSwift,
  175. ]
  176. )
  177. static let grpcTests: Target = .testTarget(
  178. name: "GRPCTests",
  179. dependencies: [
  180. .grpc,
  181. .echoModel,
  182. .echoImplementation,
  183. .helloWorldModel,
  184. .interopTestModels,
  185. .interopTestImplementation,
  186. .grpcSampleData,
  187. .nioCore,
  188. .nioConcurrencyHelpers,
  189. .nioPosix,
  190. .nioTLS,
  191. .nioHTTP1,
  192. .nioHTTP2,
  193. .nioEmbedded,
  194. .nioTransportServices,
  195. .logging,
  196. .reflectionService,
  197. ].appending(
  198. .nioSSL, if: includeNIOSSL
  199. ),
  200. exclude: [
  201. "Codegen/Normalization/normalization.proto",
  202. "Codegen/Serialization/echo.grpc.reflection.txt",
  203. ]
  204. )
  205. static let grpcCoreTests: Target = .testTarget(
  206. name: "GRPCCoreTests",
  207. dependencies: [
  208. .grpcCore,
  209. .dequeModule,
  210. ]
  211. )
  212. static let interopTestModels: Target = .target(
  213. name: "GRPCInteroperabilityTestModels",
  214. dependencies: [
  215. .grpc,
  216. .nio,
  217. .protobuf,
  218. ],
  219. exclude: [
  220. "README.md",
  221. "generate.sh",
  222. "src/proto/grpc/testing/empty.proto",
  223. "src/proto/grpc/testing/empty_service.proto",
  224. "src/proto/grpc/testing/messages.proto",
  225. "src/proto/grpc/testing/test.proto",
  226. "unimplemented_call.patch",
  227. ]
  228. )
  229. static let interopTestImplementation: Target = .target(
  230. name: "GRPCInteroperabilityTestsImplementation",
  231. dependencies: [
  232. .grpc,
  233. .interopTestModels,
  234. .nioCore,
  235. .nioPosix,
  236. .nioHTTP1,
  237. .logging,
  238. ].appending(
  239. .nioSSL, if: includeNIOSSL
  240. )
  241. )
  242. static let interopTests: Target = .executableTarget(
  243. name: "GRPCInteroperabilityTests",
  244. dependencies: [
  245. .grpc,
  246. .interopTestImplementation,
  247. .nioCore,
  248. .nioPosix,
  249. .logging,
  250. .argumentParser,
  251. ]
  252. )
  253. static let backoffInteropTest: Target = .executableTarget(
  254. name: "GRPCConnectionBackoffInteropTest",
  255. dependencies: [
  256. .grpc,
  257. .interopTestModels,
  258. .nioCore,
  259. .nioPosix,
  260. .logging,
  261. .argumentParser,
  262. ],
  263. exclude: [
  264. "README.md",
  265. ]
  266. )
  267. static let perfTests: Target = .executableTarget(
  268. name: "GRPCPerformanceTests",
  269. dependencies: [
  270. .grpc,
  271. .grpcSampleData,
  272. .nioCore,
  273. .nioEmbedded,
  274. .nioPosix,
  275. .nioHTTP2,
  276. .argumentParser,
  277. ]
  278. )
  279. static let grpcSampleData: Target = .target(
  280. name: "GRPCSampleData",
  281. dependencies: includeNIOSSL ? [.nioSSL] : [],
  282. exclude: [
  283. "bundle.p12",
  284. ]
  285. )
  286. static let echoModel: Target = .target(
  287. name: "EchoModel",
  288. dependencies: [
  289. .grpc,
  290. .nio,
  291. .protobuf,
  292. ],
  293. path: "Sources/Examples/Echo/Model",
  294. exclude: [
  295. "echo.proto",
  296. ]
  297. )
  298. static let echoImplementation: Target = .target(
  299. name: "EchoImplementation",
  300. dependencies: [
  301. .echoModel,
  302. .grpc,
  303. .nioCore,
  304. .nioHTTP2,
  305. .protobuf,
  306. ],
  307. path: "Sources/Examples/Echo/Implementation"
  308. )
  309. static let echo: Target = .executableTarget(
  310. name: "Echo",
  311. dependencies: [
  312. .grpc,
  313. .echoModel,
  314. .echoImplementation,
  315. .grpcSampleData,
  316. .nioCore,
  317. .nioPosix,
  318. .logging,
  319. .argumentParser,
  320. ].appending(
  321. .nioSSL, if: includeNIOSSL
  322. ),
  323. path: "Sources/Examples/Echo/Runtime"
  324. )
  325. static let helloWorldModel: Target = .target(
  326. name: "HelloWorldModel",
  327. dependencies: [
  328. .grpc,
  329. .nio,
  330. .protobuf,
  331. ],
  332. path: "Sources/Examples/HelloWorld/Model",
  333. exclude: [
  334. "helloworld.proto",
  335. ]
  336. )
  337. static let helloWorldClient: Target = .executableTarget(
  338. name: "HelloWorldClient",
  339. dependencies: [
  340. .grpc,
  341. .helloWorldModel,
  342. .nioCore,
  343. .nioPosix,
  344. .argumentParser,
  345. ],
  346. path: "Sources/Examples/HelloWorld/Client"
  347. )
  348. static let helloWorldServer: Target = .executableTarget(
  349. name: "HelloWorldServer",
  350. dependencies: [
  351. .grpc,
  352. .helloWorldModel,
  353. .nioCore,
  354. .nioPosix,
  355. .argumentParser,
  356. ],
  357. path: "Sources/Examples/HelloWorld/Server"
  358. )
  359. static let routeGuideModel: Target = .target(
  360. name: "RouteGuideModel",
  361. dependencies: [
  362. .grpc,
  363. .nio,
  364. .protobuf,
  365. ],
  366. path: "Sources/Examples/RouteGuide/Model",
  367. exclude: [
  368. "route_guide.proto",
  369. ]
  370. )
  371. static let routeGuideClient: Target = .executableTarget(
  372. name: "RouteGuideClient",
  373. dependencies: [
  374. .grpc,
  375. .routeGuideModel,
  376. .nioCore,
  377. .nioPosix,
  378. .argumentParser,
  379. ],
  380. path: "Sources/Examples/RouteGuide/Client"
  381. )
  382. static let routeGuideServer: Target = .executableTarget(
  383. name: "RouteGuideServer",
  384. dependencies: [
  385. .grpc,
  386. .routeGuideModel,
  387. .nioCore,
  388. .nioConcurrencyHelpers,
  389. .nioPosix,
  390. .argumentParser,
  391. ],
  392. path: "Sources/Examples/RouteGuide/Server"
  393. )
  394. static let packetCapture: Target = .executableTarget(
  395. name: "PacketCapture",
  396. dependencies: [
  397. .grpc,
  398. .echoModel,
  399. .nioCore,
  400. .nioPosix,
  401. .nioExtras,
  402. .argumentParser,
  403. ],
  404. path: "Sources/Examples/PacketCapture",
  405. exclude: [
  406. "README.md",
  407. ]
  408. )
  409. static let reflectionService: Target = .target(
  410. name: "GRPCReflectionService",
  411. dependencies: [
  412. .grpc,
  413. .nio,
  414. .protobuf,
  415. ],
  416. path: "Sources/GRPCReflectionService",
  417. exclude: [
  418. "Model/reflection.proto",
  419. ]
  420. )
  421. }
  422. // MARK: - Products
  423. extension Product {
  424. static let grpc: Product = .library(
  425. name: grpcProductName,
  426. targets: [grpcTargetName]
  427. )
  428. static let cgrpcZlib: Product = .library(
  429. name: cgrpcZlibProductName,
  430. targets: [cgrpcZlibTargetName]
  431. )
  432. static let protocGenGRPCSwift: Product = .executable(
  433. name: "protoc-gen-grpc-swift",
  434. targets: ["protoc-gen-grpc-swift"]
  435. )
  436. static let grpcSwiftPlugin: Product = .plugin(
  437. name: "GRPCSwiftPlugin",
  438. targets: ["GRPCSwiftPlugin"]
  439. )
  440. }
  441. // MARK: - Package
  442. let package = Package(
  443. name: grpcPackageName,
  444. products: [
  445. .grpc,
  446. .cgrpcZlib,
  447. .protocGenGRPCSwift,
  448. .grpcSwiftPlugin,
  449. ],
  450. dependencies: packageDependencies,
  451. targets: [
  452. // Products
  453. .grpc,
  454. .cgrpcZlib,
  455. .protocGenGRPCSwift,
  456. .grpcSwiftPlugin,
  457. .reflectionService,
  458. // Tests etc.
  459. .grpcTests,
  460. .interopTestModels,
  461. .interopTestImplementation,
  462. .interopTests,
  463. .backoffInteropTest,
  464. .perfTests,
  465. .grpcSampleData,
  466. // Examples
  467. .echoModel,
  468. .echoImplementation,
  469. .echo,
  470. .helloWorldModel,
  471. .helloWorldClient,
  472. .helloWorldServer,
  473. .routeGuideModel,
  474. .routeGuideClient,
  475. .routeGuideServer,
  476. .packetCapture,
  477. // v2
  478. .grpcCore,
  479. // v2 tests
  480. .grpcCoreTests,
  481. ]
  482. )
  483. extension Array {
  484. func appending(_ element: Element, if condition: Bool) -> [Element] {
  485. if condition {
  486. return self + [element]
  487. } else {
  488. return self
  489. }
  490. }
  491. }