'Suppress warnings from dependencies with Swift Package Manager

Assuming I have a Package.swift like this below, and SomePackage from the dependencies produces warnings during swift build.

// swift-tools-version:4.0
import PackageDescription

let package = Package(
    name: "my-app",
    dependencies: [
        .package(url: "https://some-package.git", .upToNextMajor(from: "1.0"))
    ],
    targets: [
        .target(name: "Run", dependencies: ["SomePackage"]
    ]
)

How can I suppress those warnings from the dependencies, but keep the ones coming from my code?



Solution 1:[1]

With Swift Tools Version 5 you may define compiler flags in the package file (see https://docs.swift.org/package-manager/PackageDescription/PackageDescription.html#swiftsetting). Here is an example for a Package.swift which suppresses compiler warnings during build:

// swift-tools-version:5.0

import PackageDescription

let package = Package(
    name: "Antlr4",
    products: [
        .library(
            name: "Antlr4",
            targets: ["Antlr4"]),
    ],
    targets: [
        .target(
            name: "Antlr4",
            dependencies: [],
            swiftSettings: [
                .unsafeFlags(["-suppress-warnings"]),
            ]),
        .testTarget(
            name: "Antlr4Tests",
            dependencies: ["Antlr4"]),
    ]
)

To suppress warnings only in foreign code you should split the code into two packages.

Solution 2:[2]

For Objective-C modules, you can use the following to disable all warnings:

cSettings: [
   .unsafeFlags(["-w"])
]

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 clemens
Solution 2 Patrick