Package.swift 12 KB

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