Package.swift 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. // swift-tools-version:5.8
  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.65.0"
  32. ),
  33. .package(
  34. url: "https://github.com/apple/swift-nio-http2.git",
  35. from: "1.32.0"
  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-collections.git",
  47. from: "1.0.5"
  48. ),
  49. .package(
  50. url: "https://github.com/apple/swift-protobuf.git",
  51. from: "1.27.0"
  52. ),
  53. .package(
  54. url: "https://github.com/apple/swift-log.git",
  55. from: "1.4.4"
  56. ),
  57. .package(
  58. url: "https://github.com/apple/swift-argument-parser.git",
  59. // Version is higher than in other Package@swift manifests: 1.1.0 raised the minimum Swift
  60. // version and indluded async support.
  61. from: "1.1.1"
  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. static let reflectionService: Self = .target(name: "GRPCReflectionService")
  77. // Target dependencies; internal
  78. static let grpcSampleData: Self = .target(name: "GRPCSampleData")
  79. static let echoModel: Self = .target(name: "EchoModel")
  80. static let echoImplementation: Self = .target(name: "EchoImplementation")
  81. static let helloWorldModel: Self = .target(name: "HelloWorldModel")
  82. static let routeGuideModel: Self = .target(name: "RouteGuideModel")
  83. static let interopTestModels: Self = .target(name: "GRPCInteroperabilityTestModels")
  84. static let interopTestImplementation: Self =
  85. .target(name: "GRPCInteroperabilityTestsImplementation")
  86. static let interoperabilityTests: Self = .target(name: "InteroperabilityTests")
  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 nioTestUtils: Self = .product(name: "NIOTestUtils", package: "swift-nio")
  111. static let nioFileSystem: Self = .product(name: "_NIOFileSystem", package: "swift-nio")
  112. static let logging: Self = .product(name: "Logging", package: "swift-log")
  113. static let protobuf: Self = .product(name: "SwiftProtobuf", package: "swift-protobuf")
  114. static let protobufPluginLibrary: Self = .product(
  115. name: "SwiftProtobufPluginLibrary",
  116. package: "swift-protobuf"
  117. )
  118. static let dequeModule: Self = .product(name: "DequeModule", package: "swift-collections")
  119. }
  120. // MARK: - Targets
  121. extension Target {
  122. static let grpc: Target = .target(
  123. name: grpcTargetName,
  124. dependencies: [
  125. .cgrpcZlib,
  126. .nio,
  127. .nioCore,
  128. .nioPosix,
  129. .nioEmbedded,
  130. .nioFoundationCompat,
  131. .nioTLS,
  132. .nioTransportServices,
  133. .nioHTTP1,
  134. .nioHTTP2,
  135. .nioExtras,
  136. .logging,
  137. .protobuf,
  138. .dequeModule,
  139. ].appending(
  140. .nioSSL, if: includeNIOSSL
  141. ),
  142. path: "Sources/GRPC"
  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. .reflectionService
  188. ].appending(
  189. .nioSSL, if: includeNIOSSL
  190. ),
  191. exclude: [
  192. "Codegen/Serialization/echo.grpc.reflection"
  193. ]
  194. )
  195. static let interopTestModels: Target = .target(
  196. name: "GRPCInteroperabilityTestModels",
  197. dependencies: [
  198. .grpc,
  199. .nio,
  200. .protobuf,
  201. ],
  202. exclude: [
  203. "README.md",
  204. "generate.sh",
  205. "src/proto/grpc/testing/empty.proto",
  206. "src/proto/grpc/testing/empty_service.proto",
  207. "src/proto/grpc/testing/messages.proto",
  208. "src/proto/grpc/testing/test.proto",
  209. "unimplemented_call.patch",
  210. ]
  211. )
  212. static let interopTestImplementation: Target = .target(
  213. name: "GRPCInteroperabilityTestsImplementation",
  214. dependencies: [
  215. .grpc,
  216. .interopTestModels,
  217. .nioCore,
  218. .nioPosix,
  219. .nioHTTP1,
  220. .logging,
  221. ].appending(
  222. .nioSSL, if: includeNIOSSL
  223. )
  224. )
  225. static let interopTests: Target = .executableTarget(
  226. name: "GRPCInteroperabilityTests",
  227. dependencies: [
  228. .grpc,
  229. .interopTestImplementation,
  230. .nioCore,
  231. .nioPosix,
  232. .logging,
  233. .argumentParser,
  234. ]
  235. )
  236. static let backoffInteropTest: Target = .executableTarget(
  237. name: "GRPCConnectionBackoffInteropTest",
  238. dependencies: [
  239. .grpc,
  240. .interopTestModels,
  241. .nioCore,
  242. .nioPosix,
  243. .logging,
  244. .argumentParser,
  245. ],
  246. exclude: [
  247. "README.md",
  248. ]
  249. )
  250. static let perfTests: Target = .executableTarget(
  251. name: "GRPCPerformanceTests",
  252. dependencies: [
  253. .grpc,
  254. .grpcSampleData,
  255. .nioCore,
  256. .nioEmbedded,
  257. .nioPosix,
  258. .nioHTTP2,
  259. .argumentParser,
  260. ]
  261. )
  262. static let grpcSampleData: Target = .target(
  263. name: "GRPCSampleData",
  264. dependencies: includeNIOSSL ? [.nioSSL] : [],
  265. exclude: [
  266. "bundle.p12",
  267. ]
  268. )
  269. static let echoModel: Target = .target(
  270. name: "EchoModel",
  271. dependencies: [
  272. .grpc,
  273. .nio,
  274. .protobuf,
  275. ],
  276. path: "Sources/Examples/v1/Echo/Model"
  277. )
  278. static let echoImplementation: Target = .target(
  279. name: "EchoImplementation",
  280. dependencies: [
  281. .echoModel,
  282. .grpc,
  283. .nioCore,
  284. .nioHTTP2,
  285. .protobuf,
  286. ],
  287. path: "Sources/Examples/v1/Echo/Implementation"
  288. )
  289. static let echo: Target = .executableTarget(
  290. name: "Echo",
  291. dependencies: [
  292. .grpc,
  293. .echoModel,
  294. .echoImplementation,
  295. .grpcSampleData,
  296. .nioCore,
  297. .nioPosix,
  298. .logging,
  299. .argumentParser,
  300. ].appending(
  301. .nioSSL, if: includeNIOSSL
  302. ),
  303. path: "Sources/Examples/v1/Echo/Runtime"
  304. )
  305. static let helloWorldModel: Target = .target(
  306. name: "HelloWorldModel",
  307. dependencies: [
  308. .grpc,
  309. .nio,
  310. .protobuf,
  311. ],
  312. path: "Sources/Examples/v1/HelloWorld/Model"
  313. )
  314. static let helloWorldClient: Target = .executableTarget(
  315. name: "HelloWorldClient",
  316. dependencies: [
  317. .grpc,
  318. .helloWorldModel,
  319. .nioCore,
  320. .nioPosix,
  321. .argumentParser,
  322. ],
  323. path: "Sources/Examples/v1/HelloWorld/Client"
  324. )
  325. static let helloWorldServer: Target = .executableTarget(
  326. name: "HelloWorldServer",
  327. dependencies: [
  328. .grpc,
  329. .helloWorldModel,
  330. .nioCore,
  331. .nioPosix,
  332. .argumentParser,
  333. ],
  334. path: "Sources/Examples/v1/HelloWorld/Server"
  335. )
  336. static let routeGuideModel: Target = .target(
  337. name: "RouteGuideModel",
  338. dependencies: [
  339. .grpc,
  340. .nio,
  341. .protobuf,
  342. ],
  343. path: "Sources/Examples/v1/RouteGuide/Model"
  344. )
  345. static let routeGuideClient: Target = .executableTarget(
  346. name: "RouteGuideClient",
  347. dependencies: [
  348. .grpc,
  349. .routeGuideModel,
  350. .nioCore,
  351. .nioPosix,
  352. .argumentParser,
  353. ],
  354. path: "Sources/Examples/v1/RouteGuide/Client"
  355. )
  356. static let routeGuideServer: Target = .executableTarget(
  357. name: "RouteGuideServer",
  358. dependencies: [
  359. .grpc,
  360. .routeGuideModel,
  361. .nioCore,
  362. .nioConcurrencyHelpers,
  363. .nioPosix,
  364. .argumentParser,
  365. ],
  366. path: "Sources/Examples/v1/RouteGuide/Server"
  367. )
  368. static let packetCapture: Target = .executableTarget(
  369. name: "PacketCapture",
  370. dependencies: [
  371. .grpc,
  372. .echoModel,
  373. .nioCore,
  374. .nioPosix,
  375. .nioExtras,
  376. .argumentParser,
  377. ],
  378. path: "Sources/Examples/v1/PacketCapture",
  379. exclude: [
  380. "README.md",
  381. ]
  382. )
  383. static let reflectionService: Target = .target(
  384. name: "GRPCReflectionService",
  385. dependencies: [
  386. .grpc,
  387. .nio,
  388. .protobuf,
  389. ],
  390. path: "Sources/GRPCReflectionService"
  391. )
  392. static let reflectionServer: Target = .executableTarget(
  393. name: "ReflectionServer",
  394. dependencies: [
  395. .grpc,
  396. .reflectionService,
  397. .helloWorldModel,
  398. .nioCore,
  399. .nioPosix,
  400. .argumentParser,
  401. .echoModel,
  402. .echoImplementation
  403. ],
  404. path: "Sources/Examples/v1/ReflectionService",
  405. resources: [
  406. .copy("Generated")
  407. ]
  408. )
  409. }
  410. // MARK: - Products
  411. extension Product {
  412. static let grpc: Product = .library(
  413. name: grpcProductName,
  414. targets: [grpcTargetName]
  415. )
  416. static let cgrpcZlib: Product = .library(
  417. name: cgrpcZlibProductName,
  418. targets: [cgrpcZlibTargetName]
  419. )
  420. static let grpcReflectionService: Product = .library(
  421. name: "GRPCReflectionService",
  422. targets: ["GRPCReflectionService"]
  423. )
  424. static let protocGenGRPCSwift: Product = .executable(
  425. name: "protoc-gen-grpc-swift",
  426. targets: ["protoc-gen-grpc-swift"]
  427. )
  428. static let grpcSwiftPlugin: Product = .plugin(
  429. name: "GRPCSwiftPlugin",
  430. targets: ["GRPCSwiftPlugin"]
  431. )
  432. }
  433. // MARK: - Package
  434. let package = Package(
  435. name: grpcPackageName,
  436. products: [
  437. .grpc,
  438. .cgrpcZlib,
  439. .grpcReflectionService,
  440. .protocGenGRPCSwift,
  441. .grpcSwiftPlugin,
  442. ],
  443. dependencies: packageDependencies,
  444. targets: [
  445. // Products
  446. .grpc,
  447. .cgrpcZlib,
  448. .protocGenGRPCSwift,
  449. .grpcSwiftPlugin,
  450. .reflectionService,
  451. // Tests etc.
  452. .grpcTests,
  453. .interopTestModels,
  454. .interopTestImplementation,
  455. .interopTests,
  456. .backoffInteropTest,
  457. .perfTests,
  458. .grpcSampleData,
  459. // Examples
  460. .echoModel,
  461. .echoImplementation,
  462. .echo,
  463. .helloWorldModel,
  464. .helloWorldClient,
  465. .helloWorldServer,
  466. .routeGuideModel,
  467. .routeGuideClient,
  468. .routeGuideServer,
  469. .packetCapture,
  470. .reflectionServer,
  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. }