Package.swift 15 KB

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