Package.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. // swift-tools-version:5.2
  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 sortedImports
  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.32.0"
  32. ),
  33. .package(
  34. url: "https://github.com/apple/swift-nio-http2.git",
  35. from: "1.19.2"
  36. ),
  37. .package(
  38. url: "https://github.com/apple/swift-nio-transport-services.git",
  39. from: "1.11.1"
  40. ),
  41. .package(
  42. url: "https://github.com/apple/swift-nio-extras.git",
  43. from: "1.4.0"
  44. ),
  45. .package(
  46. name: "SwiftProtobuf",
  47. url: "https://github.com/apple/swift-protobuf.git",
  48. from: "1.19.0"
  49. ),
  50. .package(
  51. url: "https://github.com/apple/swift-log.git",
  52. from: "1.4.0"
  53. ),
  54. .package(
  55. url: "https://github.com/apple/swift-argument-parser.git",
  56. from: "1.0.0"
  57. ),
  58. ].appending(
  59. .package(
  60. url: "https://github.com/apple/swift-nio-ssl.git",
  61. from: "2.14.0"
  62. ),
  63. if: includeNIOSSL
  64. )
  65. // MARK: - Target Dependencies
  66. extension Target.Dependency {
  67. // Target dependencies; external
  68. static let grpc: Self = .target(name: grpcTargetName)
  69. static let cgrpcZlib: Self = .target(name: cgrpcZlibTargetName)
  70. static let protocGenGRPCSwift: Self = .target(name: "protoc-gen-grpc-swift")
  71. // Target dependencies; internal
  72. static let grpcSampleData: Self = .target(name: "GRPCSampleData")
  73. static let echoModel: Self = .target(name: "EchoModel")
  74. static let echoImplementation: Self = .target(name: "EchoImplementation")
  75. static let helloWorldModel: Self = .target(name: "HelloWorldModel")
  76. static let routeGuideModel: Self = .target(name: "RouteGuideModel")
  77. static let interopTestModels: Self = .target(name: "GRPCInteroperabilityTestModels")
  78. static let interopTestImplementation: Self =
  79. .target(name: "GRPCInteroperabilityTestsImplementation")
  80. // Product dependencies
  81. static let argumentParser: Self = .product(
  82. name: "ArgumentParser",
  83. package: "swift-argument-parser"
  84. )
  85. static let nio: Self = .product(name: "NIO", package: "swift-nio")
  86. static let nioConcurrencyHelpers: Self = .product(
  87. name: "NIOConcurrencyHelpers",
  88. package: "swift-nio"
  89. )
  90. static let nioCore: Self = .product(name: "NIOCore", package: "swift-nio")
  91. static let nioEmbedded: Self = .product(name: "NIOEmbedded", package: "swift-nio")
  92. static let nioExtras: Self = .product(name: "NIOExtras", package: "swift-nio-extras")
  93. static let nioFoundationCompat: Self = .product(name: "NIOFoundationCompat", package: "swift-nio")
  94. static let nioHTTP1: Self = .product(name: "NIOHTTP1", package: "swift-nio")
  95. static let nioHTTP2: Self = .product(name: "NIOHTTP2", package: "swift-nio-http2")
  96. static let nioPosix: Self = .product(name: "NIOPosix", package: "swift-nio")
  97. static let nioSSL: Self = .product(name: "NIOSSL", package: "swift-nio-ssl")
  98. static let nioTLS: Self = .product(name: "NIOTLS", package: "swift-nio")
  99. static let nioTransportServices: Self = .product(
  100. name: "NIOTransportServices",
  101. package: "swift-nio-transport-services"
  102. )
  103. static let logging: Self = .product(name: "Logging", package: "swift-log")
  104. static let protobuf: Self = .product(name: "SwiftProtobuf", package: "SwiftProtobuf")
  105. static let protobufPluginLibrary: Self = .product(
  106. name: "SwiftProtobufPluginLibrary",
  107. package: "SwiftProtobuf"
  108. )
  109. }
  110. // MARK: - Targets
  111. extension Target {
  112. static let grpc: Target = .target(
  113. name: grpcTargetName,
  114. dependencies: [
  115. .cgrpcZlib,
  116. .nio,
  117. .nioCore,
  118. .nioPosix,
  119. .nioEmbedded,
  120. .nioFoundationCompat,
  121. .nioTLS,
  122. .nioTransportServices,
  123. .nioHTTP1,
  124. .nioHTTP2,
  125. .nioExtras,
  126. .logging,
  127. .protobuf,
  128. ].appending(
  129. .nioSSL, if: includeNIOSSL
  130. ),
  131. path: "Sources/GRPC"
  132. )
  133. static let cgrpcZlib: Target = .target(
  134. name: cgrpcZlibTargetName,
  135. path: "Sources/CGRPCZlib",
  136. linkerSettings: [
  137. .linkedLibrary("z"),
  138. ]
  139. )
  140. static let protocGenGRPCSwift: Target = .target(
  141. name: "protoc-gen-grpc-swift",
  142. dependencies: [
  143. .protobuf,
  144. .protobufPluginLibrary,
  145. ]
  146. )
  147. static let grpcTests: Target = .testTarget(
  148. name: "GRPCTests",
  149. dependencies: [
  150. .grpc,
  151. .echoModel,
  152. .echoImplementation,
  153. .helloWorldModel,
  154. .interopTestModels,
  155. .interopTestImplementation,
  156. .grpcSampleData,
  157. .nioCore,
  158. .nioConcurrencyHelpers,
  159. .nioPosix,
  160. .nioTLS,
  161. .nioHTTP1,
  162. .nioHTTP2,
  163. .nioEmbedded,
  164. .nioTransportServices,
  165. .logging,
  166. ].appending(
  167. .nioSSL, if: includeNIOSSL
  168. )
  169. )
  170. static let interopTestModels: Target = .target(
  171. name: "GRPCInteroperabilityTestModels",
  172. dependencies: [
  173. .grpc,
  174. .nio,
  175. .protobuf,
  176. ]
  177. )
  178. static let interopTestImplementation: Target = .target(
  179. name: "GRPCInteroperabilityTestsImplementation",
  180. dependencies: [
  181. .grpc,
  182. .interopTestModels,
  183. .nioCore,
  184. .nioPosix,
  185. .nioHTTP1,
  186. .logging,
  187. ].appending(
  188. .nioSSL, if: includeNIOSSL
  189. )
  190. )
  191. static let interopTests: Target = .target(
  192. name: "GRPCInteroperabilityTests",
  193. dependencies: [
  194. .grpc,
  195. .interopTestImplementation,
  196. .nioCore,
  197. .nioPosix,
  198. .logging,
  199. .argumentParser,
  200. ]
  201. )
  202. static let backoffInteropTest: Target = .target(
  203. name: "GRPCConnectionBackoffInteropTest",
  204. dependencies: [
  205. .grpc,
  206. .interopTestModels,
  207. .nioCore,
  208. .nioPosix,
  209. .logging,
  210. .argumentParser,
  211. ]
  212. )
  213. static let perfTests: Target = .target(
  214. name: "GRPCPerformanceTests",
  215. dependencies: [
  216. .grpc,
  217. .grpcSampleData,
  218. .nioCore,
  219. .nioEmbedded,
  220. .nioPosix,
  221. .nioHTTP2,
  222. .argumentParser,
  223. ]
  224. )
  225. static let grpcSampleData: Target = .target(
  226. name: "GRPCSampleData",
  227. dependencies: includeNIOSSL ? [.nioSSL] : []
  228. )
  229. static let echoModel: Target = .target(
  230. name: "EchoModel",
  231. dependencies: [
  232. .grpc,
  233. .nio,
  234. .protobuf,
  235. ],
  236. path: "Sources/Examples/Echo/Model"
  237. )
  238. static let echoImplementation: Target = .target(
  239. name: "EchoImplementation",
  240. dependencies: [
  241. .echoModel,
  242. .grpc,
  243. .nioCore,
  244. .nioHTTP2,
  245. .protobuf,
  246. ],
  247. path: "Sources/Examples/Echo/Implementation"
  248. )
  249. static let echo: Target = .target(
  250. name: "Echo",
  251. dependencies: [
  252. .grpc,
  253. .echoModel,
  254. .echoImplementation,
  255. .grpcSampleData,
  256. .nioCore,
  257. .nioPosix,
  258. .logging,
  259. .argumentParser,
  260. ].appending(
  261. .nioSSL, if: includeNIOSSL
  262. ),
  263. path: "Sources/Examples/Echo/Runtime"
  264. )
  265. static let echoAsync: Target = .target(
  266. name: "AsyncAwaitEcho",
  267. dependencies: [
  268. .grpc,
  269. .echoModel,
  270. .echoImplementation,
  271. .grpcSampleData,
  272. .nioCore,
  273. .nioPosix,
  274. .logging,
  275. .argumentParser,
  276. ].appending(
  277. .nioSSL, if: includeNIOSSL
  278. ),
  279. path: "Sources/Examples/Echo/AsyncAwaitRuntime"
  280. )
  281. static let helloWorldModel: Target = .target(
  282. name: "HelloWorldModel",
  283. dependencies: [
  284. .grpc,
  285. .nio,
  286. .protobuf,
  287. ],
  288. path: "Sources/Examples/HelloWorld/Model"
  289. )
  290. static let helloWorldClient: Target = .target(
  291. name: "HelloWorldClient",
  292. dependencies: [
  293. .grpc,
  294. .helloWorldModel,
  295. .nioCore,
  296. .nioPosix,
  297. .argumentParser,
  298. ],
  299. path: "Sources/Examples/HelloWorld/Client"
  300. )
  301. static let helloWorldServer: Target = .target(
  302. name: "HelloWorldServer",
  303. dependencies: [
  304. .grpc,
  305. .helloWorldModel,
  306. .nioCore,
  307. .nioPosix,
  308. .argumentParser,
  309. ],
  310. path: "Sources/Examples/HelloWorld/Server"
  311. )
  312. static let routeGuideModel: Target = .target(
  313. name: "RouteGuideModel",
  314. dependencies: [
  315. .grpc,
  316. .nio,
  317. .protobuf,
  318. ],
  319. path: "Sources/Examples/RouteGuide/Model"
  320. )
  321. static let routeGuideClient: Target = .target(
  322. name: "RouteGuideClient",
  323. dependencies: [
  324. .grpc,
  325. .routeGuideModel,
  326. .nioCore,
  327. .nioPosix,
  328. .argumentParser,
  329. ],
  330. path: "Sources/Examples/RouteGuide/Client"
  331. )
  332. static let routeGuideServer: Target = .target(
  333. name: "RouteGuideServer",
  334. dependencies: [
  335. .grpc,
  336. .routeGuideModel,
  337. .nioCore,
  338. .nioConcurrencyHelpers,
  339. .nioPosix,
  340. .argumentParser,
  341. ],
  342. path: "Sources/Examples/RouteGuide/Server"
  343. )
  344. static let packetCapture: Target = .target(
  345. name: "PacketCapture",
  346. dependencies: [
  347. .grpc,
  348. .echoModel,
  349. .nioCore,
  350. .nioPosix,
  351. .nioExtras,
  352. .logging,
  353. .argumentParser,
  354. ],
  355. path: "Sources/Examples/PacketCapture"
  356. )
  357. }
  358. // MARK: - Products
  359. extension Product {
  360. static let grpc: Product = .library(
  361. name: grpcProductName,
  362. targets: [grpcTargetName]
  363. )
  364. static let cgrpcZlib: Product = .library(
  365. name: cgrpcZlibProductName,
  366. targets: [cgrpcZlibTargetName]
  367. )
  368. static let protocGenGRPCSwift: Product = .executable(
  369. name: "protoc-gen-grpc-swift",
  370. targets: ["protoc-gen-grpc-swift"]
  371. )
  372. }
  373. // MARK: - Package
  374. let package = Package(
  375. name: grpcPackageName,
  376. products: [
  377. .grpc,
  378. .cgrpcZlib,
  379. .protocGenGRPCSwift,
  380. ],
  381. dependencies: packageDependencies,
  382. targets: [
  383. // Products
  384. .grpc,
  385. .cgrpcZlib,
  386. .protocGenGRPCSwift,
  387. // Tests etc.
  388. .grpcTests,
  389. .interopTestModels,
  390. .interopTestImplementation,
  391. .interopTests,
  392. .backoffInteropTest,
  393. .perfTests,
  394. .grpcSampleData,
  395. // Examples
  396. .echoModel,
  397. .echoImplementation,
  398. .echo,
  399. .echoAsync,
  400. .helloWorldModel,
  401. .helloWorldClient,
  402. .helloWorldServer,
  403. .routeGuideModel,
  404. .routeGuideClient,
  405. .routeGuideServer,
  406. .packetCapture,
  407. ]
  408. )
  409. extension Array {
  410. func appending(_ element: Element, if condition: Bool) -> [Element] {
  411. if condition {
  412. return self + [element]
  413. } else {
  414. return self
  415. }
  416. }
  417. }