Package.swift 12 KB

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