Package.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. // swift-tools-version:5.4
  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 = .executableTarget(
  141. name: "protoc-gen-grpc-swift",
  142. dependencies: [
  143. .protobuf,
  144. .protobufPluginLibrary,
  145. ],
  146. exclude: [
  147. "README.md",
  148. ]
  149. )
  150. static let grpcTests: Target = .testTarget(
  151. name: "GRPCTests",
  152. dependencies: [
  153. .grpc,
  154. .echoModel,
  155. .echoImplementation,
  156. .helloWorldModel,
  157. .interopTestModels,
  158. .interopTestImplementation,
  159. .grpcSampleData,
  160. .nioCore,
  161. .nioConcurrencyHelpers,
  162. .nioPosix,
  163. .nioTLS,
  164. .nioHTTP1,
  165. .nioHTTP2,
  166. .nioEmbedded,
  167. .nioTransportServices,
  168. .logging,
  169. ].appending(
  170. .nioSSL, if: includeNIOSSL
  171. ),
  172. exclude: [
  173. "Codegen/Normalization/normalization.proto",
  174. ]
  175. )
  176. static let interopTestModels: Target = .target(
  177. name: "GRPCInteroperabilityTestModels",
  178. dependencies: [
  179. .grpc,
  180. .nio,
  181. .protobuf,
  182. ],
  183. exclude: [
  184. "README.md",
  185. "generate.sh",
  186. "src/proto/grpc/testing/empty.proto",
  187. "src/proto/grpc/testing/empty_service.proto",
  188. "src/proto/grpc/testing/messages.proto",
  189. "src/proto/grpc/testing/test.proto",
  190. "unimplemented_call.patch",
  191. ]
  192. )
  193. static let interopTestImplementation: Target = .target(
  194. name: "GRPCInteroperabilityTestsImplementation",
  195. dependencies: [
  196. .grpc,
  197. .interopTestModels,
  198. .nioCore,
  199. .nioPosix,
  200. .nioHTTP1,
  201. .logging,
  202. ].appending(
  203. .nioSSL, if: includeNIOSSL
  204. )
  205. )
  206. static let interopTests: Target = .executableTarget(
  207. name: "GRPCInteroperabilityTests",
  208. dependencies: [
  209. .grpc,
  210. .interopTestImplementation,
  211. .nioCore,
  212. .nioPosix,
  213. .logging,
  214. .argumentParser,
  215. ]
  216. )
  217. static let backoffInteropTest: Target = .executableTarget(
  218. name: "GRPCConnectionBackoffInteropTest",
  219. dependencies: [
  220. .grpc,
  221. .interopTestModels,
  222. .nioCore,
  223. .nioPosix,
  224. .logging,
  225. .argumentParser,
  226. ],
  227. exclude: [
  228. "README.md",
  229. ]
  230. )
  231. static let perfTests: Target = .executableTarget(
  232. name: "GRPCPerformanceTests",
  233. dependencies: [
  234. .grpc,
  235. .grpcSampleData,
  236. .nioCore,
  237. .nioEmbedded,
  238. .nioPosix,
  239. .nioHTTP2,
  240. .argumentParser,
  241. ]
  242. )
  243. static let grpcSampleData: Target = .target(
  244. name: "GRPCSampleData",
  245. dependencies: includeNIOSSL ? [.nioSSL] : [],
  246. exclude: [
  247. "bundle.p12",
  248. ]
  249. )
  250. static let echoModel: Target = .target(
  251. name: "EchoModel",
  252. dependencies: [
  253. .grpc,
  254. .nio,
  255. .protobuf,
  256. ],
  257. path: "Sources/Examples/Echo/Model",
  258. exclude: [
  259. "echo.proto",
  260. ]
  261. )
  262. static let echoImplementation: Target = .target(
  263. name: "EchoImplementation",
  264. dependencies: [
  265. .echoModel,
  266. .grpc,
  267. .nioCore,
  268. .nioHTTP2,
  269. .protobuf,
  270. ],
  271. path: "Sources/Examples/Echo/Implementation"
  272. )
  273. static let echo: Target = .executableTarget(
  274. name: "Echo",
  275. dependencies: [
  276. .grpc,
  277. .echoModel,
  278. .echoImplementation,
  279. .grpcSampleData,
  280. .nioCore,
  281. .nioPosix,
  282. .logging,
  283. .argumentParser,
  284. ].appending(
  285. .nioSSL, if: includeNIOSSL
  286. ),
  287. path: "Sources/Examples/Echo/Runtime"
  288. )
  289. static let echoAsync: Target = .executableTarget(
  290. name: "AsyncAwaitEcho",
  291. dependencies: [
  292. .grpc,
  293. .echoModel,
  294. .echoImplementation,
  295. .grpcSampleData,
  296. .nioCore,
  297. .nioPosix,
  298. .logging,
  299. .argumentParser,
  300. ].appending(
  301. .nioSSL, if: includeNIOSSL
  302. ),
  303. path: "Sources/Examples/Echo/AsyncAwaitRuntime"
  304. )
  305. static let helloWorldModel: Target = .target(
  306. name: "HelloWorldModel",
  307. dependencies: [
  308. .grpc,
  309. .nio,
  310. .protobuf,
  311. ],
  312. path: "Sources/Examples/HelloWorld/Model",
  313. exclude: [
  314. "helloworld.proto",
  315. ]
  316. )
  317. static let helloWorldClient: Target = .executableTarget(
  318. name: "HelloWorldClient",
  319. dependencies: [
  320. .grpc,
  321. .helloWorldModel,
  322. .nioCore,
  323. .nioPosix,
  324. .argumentParser,
  325. ],
  326. path: "Sources/Examples/HelloWorld/Client"
  327. )
  328. static let helloWorldServer: Target = .executableTarget(
  329. name: "HelloWorldServer",
  330. dependencies: [
  331. .grpc,
  332. .helloWorldModel,
  333. .nioCore,
  334. .nioPosix,
  335. .argumentParser,
  336. ],
  337. path: "Sources/Examples/HelloWorld/Server"
  338. )
  339. static let routeGuideModel: Target = .target(
  340. name: "RouteGuideModel",
  341. dependencies: [
  342. .grpc,
  343. .nio,
  344. .protobuf,
  345. ],
  346. path: "Sources/Examples/RouteGuide/Model",
  347. exclude: [
  348. "route_guide.proto",
  349. ]
  350. )
  351. static let routeGuideClient: Target = .executableTarget(
  352. name: "RouteGuideClient",
  353. dependencies: [
  354. .grpc,
  355. .routeGuideModel,
  356. .nioCore,
  357. .nioPosix,
  358. .argumentParser,
  359. ],
  360. path: "Sources/Examples/RouteGuide/Client"
  361. )
  362. static let routeGuideServer: Target = .executableTarget(
  363. name: "RouteGuideServer",
  364. dependencies: [
  365. .grpc,
  366. .routeGuideModel,
  367. .nioCore,
  368. .nioConcurrencyHelpers,
  369. .nioPosix,
  370. .argumentParser,
  371. ],
  372. path: "Sources/Examples/RouteGuide/Server"
  373. )
  374. static let packetCapture: Target = .executableTarget(
  375. name: "PacketCapture",
  376. dependencies: [
  377. .grpc,
  378. .echoModel,
  379. .nioCore,
  380. .nioPosix,
  381. .nioExtras,
  382. .logging,
  383. .argumentParser,
  384. ],
  385. path: "Sources/Examples/PacketCapture",
  386. exclude: [
  387. "README.md",
  388. ]
  389. )
  390. }
  391. // MARK: - Products
  392. extension Product {
  393. static let grpc: Product = .library(
  394. name: grpcProductName,
  395. targets: [grpcTargetName]
  396. )
  397. static let cgrpcZlib: Product = .library(
  398. name: cgrpcZlibProductName,
  399. targets: [cgrpcZlibTargetName]
  400. )
  401. static let protocGenGRPCSwift: Product = .executable(
  402. name: "protoc-gen-grpc-swift",
  403. targets: ["protoc-gen-grpc-swift"]
  404. )
  405. }
  406. // MARK: - Package
  407. let package = Package(
  408. name: grpcPackageName,
  409. products: [
  410. .grpc,
  411. .cgrpcZlib,
  412. .protocGenGRPCSwift,
  413. ],
  414. dependencies: packageDependencies,
  415. targets: [
  416. // Products
  417. .grpc,
  418. .cgrpcZlib,
  419. .protocGenGRPCSwift,
  420. // Tests etc.
  421. .grpcTests,
  422. .interopTestModels,
  423. .interopTestImplementation,
  424. .interopTests,
  425. .backoffInteropTest,
  426. .perfTests,
  427. .grpcSampleData,
  428. // Examples
  429. .echoModel,
  430. .echoImplementation,
  431. .echo,
  432. .echoAsync,
  433. .helloWorldModel,
  434. .helloWorldClient,
  435. .helloWorldServer,
  436. .routeGuideModel,
  437. .routeGuideClient,
  438. .routeGuideServer,
  439. .packetCapture,
  440. ]
  441. )
  442. extension Array {
  443. func appending(_ element: Element, if condition: Bool) -> [Element] {
  444. if condition {
  445. return self + [element]
  446. } else {
  447. return self
  448. }
  449. }
  450. }