Package.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  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 helloWorldModel: Target = .target(
  297. name: "HelloWorldModel",
  298. dependencies: [
  299. .grpc,
  300. .nio,
  301. .protobuf,
  302. ],
  303. path: "Sources/Examples/HelloWorld/Model",
  304. exclude: [
  305. "helloworld.proto",
  306. ]
  307. )
  308. static let helloWorldClient: Target = .executableTarget(
  309. name: "HelloWorldClient",
  310. dependencies: [
  311. .grpc,
  312. .helloWorldModel,
  313. .nioCore,
  314. .nioPosix,
  315. .argumentParser,
  316. ],
  317. path: "Sources/Examples/HelloWorld/Client"
  318. )
  319. static let helloWorldServer: Target = .executableTarget(
  320. name: "HelloWorldServer",
  321. dependencies: [
  322. .grpc,
  323. .helloWorldModel,
  324. .nioCore,
  325. .nioPosix,
  326. .argumentParser,
  327. ],
  328. path: "Sources/Examples/HelloWorld/Server"
  329. )
  330. static let routeGuideModel: Target = .target(
  331. name: "RouteGuideModel",
  332. dependencies: [
  333. .grpc,
  334. .nio,
  335. .protobuf,
  336. ],
  337. path: "Sources/Examples/RouteGuide/Model",
  338. exclude: [
  339. "route_guide.proto",
  340. ]
  341. )
  342. static let routeGuideClient: Target = .executableTarget(
  343. name: "RouteGuideClient",
  344. dependencies: [
  345. .grpc,
  346. .routeGuideModel,
  347. .nioCore,
  348. .nioPosix,
  349. .argumentParser,
  350. ],
  351. path: "Sources/Examples/RouteGuide/Client"
  352. )
  353. static let routeGuideServer: Target = .executableTarget(
  354. name: "RouteGuideServer",
  355. dependencies: [
  356. .grpc,
  357. .routeGuideModel,
  358. .nioCore,
  359. .nioConcurrencyHelpers,
  360. .nioPosix,
  361. .argumentParser,
  362. ],
  363. path: "Sources/Examples/RouteGuide/Server"
  364. )
  365. static let packetCapture: Target = .executableTarget(
  366. name: "PacketCapture",
  367. dependencies: [
  368. .grpc,
  369. .echoModel,
  370. .nioCore,
  371. .nioPosix,
  372. .nioExtras,
  373. .argumentParser,
  374. ],
  375. path: "Sources/Examples/PacketCapture",
  376. exclude: [
  377. "README.md",
  378. ]
  379. )
  380. }
  381. // MARK: - Products
  382. extension Product {
  383. static let grpc: Product = .library(
  384. name: grpcProductName,
  385. targets: [grpcTargetName]
  386. )
  387. static let cgrpcZlib: Product = .library(
  388. name: cgrpcZlibProductName,
  389. targets: [cgrpcZlibTargetName]
  390. )
  391. static let protocGenGRPCSwift: Product = .executable(
  392. name: "protoc-gen-grpc-swift",
  393. targets: ["protoc-gen-grpc-swift"]
  394. )
  395. }
  396. // MARK: - Package
  397. let package = Package(
  398. name: grpcPackageName,
  399. products: [
  400. .grpc,
  401. .cgrpcZlib,
  402. .protocGenGRPCSwift,
  403. ],
  404. dependencies: packageDependencies,
  405. targets: [
  406. // Products
  407. .grpc,
  408. .cgrpcZlib,
  409. .protocGenGRPCSwift,
  410. // Tests etc.
  411. .grpcTests,
  412. .interopTestModels,
  413. .interopTestImplementation,
  414. .interopTests,
  415. .backoffInteropTest,
  416. .perfTests,
  417. .grpcSampleData,
  418. // Examples
  419. .echoModel,
  420. .echoImplementation,
  421. .echo,
  422. .helloWorldModel,
  423. .helloWorldClient,
  424. .helloWorldServer,
  425. .routeGuideModel,
  426. .routeGuideClient,
  427. .routeGuideServer,
  428. .packetCapture,
  429. ]
  430. )
  431. extension Array {
  432. func appending(_ element: Element, if condition: Bool) -> [Element] {
  433. if condition {
  434. return self + [element]
  435. } else {
  436. return self
  437. }
  438. }
  439. }