Package.swift 14 KB

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