Package.swift 12 KB

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