Package.swift 12 KB

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