1

The problem occurs with system modules where it's necessary to use pkgConfig and pkgConfig contains flag definitions.

ImageMagick (homebrewed)

I create two packages: CMagicWand, type system-module

module.modulemap

module CMagickWand [system] {
  header "/usr/local/Cellar/imagemagick/7.0.5-0/include/ImageMagick-7/MagickWand/MagickWand.h"
  link "MagickWand"
  export *
}

Package.swift

import PackageDescription

let package = Package(
    name: "CMagickWand",
    pkgConfig: "MagickWand"
)

Then I try to consume it from the package MagicWand type library

Package.swift

import PackageDescription

let package = Package(
    name: "MagickWand",
    dependencies: [
    .Package(url: "../CMagickWand", majorVersion: 1)
    ]
)

pkgConfig MagickWand.pc

prefix=/usr/local/Cellar/imagemagick/7.0.5-0
exec_prefix=${prefix}
libdir=${exec_prefix}/lib
includedir=${prefix}/include/ImageMagick-7
includearchdir=/usr/local/Cellar/imagemagick/7.0.5-0/include/ImageMagick-7
libname=MagickWand-7.Q16HDRI

Name: MagickWand
Description: MagickWand - C API for ImageMagick (ABI Q16HDRI)
URL: https://www.imagemagick.org
Version: 7.0.5
Requires: MagickCore
*Cflags: -I${includearchdir} -I${includedir} -DMAGICKCORE_HDRI_ENABLE=1 -DMAGICKCORE_QUANTUM_DEPTH=16*
Libs: -L${libdir} -l${libname}
Libs.private: -L${libdir} -l${libname}   -L/usr/local/opt/freetype/lib -lfreetype          -L/usr/local/Cellar/xz/5.2.3/lib -llzma -lbz2 -lz -lltdl  -lm      -lm

With this setup I execute swift build for the second package and the output is following

Cloning /bla-bla-bla/Libraries/CMagickWand
HEAD is now at 30ed4b4 Initial commit
Resolved version: 1.0.0
error: nonWhitelistedFlags("Non whitelisted flags found: [\"-DMAGICKCORE_HDRI_ENABLE=1\", \"-DMAGICKCORE_QUANTUM_DEPTH=16\", \"-DMAGICKCORE_HDRI_ENABLE=1\", \"-DMAGICKCORE_QUANTUM_DEPTH=16\"] in pc file MagickWand")

I tried to remove problematic CFlags from the corresponding .pc file and this doesn't help, even if I figure out how to remove them (I always can create my own .pc file) I don't find it sustainable. These flags are there for a reason.

I have the same problem with mysqlclient on my target system (Ubuntu), the problem is not reproducible on OS X but it doesn't help me:

error: nonWhitelistedFlags("Non whitelisted flags found: [\"-fabi-version=2\", \"-fno-omit-frame-pointer\"] in pc file mysqlclient")

The error comes from the func whitelist (https://github.com/apple/swift-package-manager/blob/master/Sources/PackageLoading/Module%2BPkgConfig.swift) and I don't see any way how to enhance the list during runtime. I don't believe that I'm the only one who struggles with this limitation but I can't find a workaround for a few days already.

4

2 回答 2

1

我用以下参数解决了这个问题:

swift build -Xcc -I/usr/local/include/ImageMagick-7/MagickWand/ -Xcc -I/usr/local/include/ImageMagick-7/ -Xcc -DMAGICKCORE_HDRI_ENABLE=0 -Xcc -DMAGICKCORE_QUANTUM_DEPTH=16 -Xlinker -L/usr/local/lib

我认为你必须调整路径。

模块.模块图:

module CMagickWand [system] {
  header "shim.h"
  header "/usr/local/include/ImageMagick-7/MagickWand/MagickWand.h"
  link "MagickWand-7.Q16HDRI"
  export *
}

链接具有来自 pkgConfig MagickWand.pc 的值libname

包.swift:

import PackageDescription

let package = Package(
    name: "CMagickWand"
)

shim.h(不知道是否需要这个文件):

#include <stdio.h>
于 2017-08-31T12:30:12.590 回答
0

我联系了这段代码的作者,回复如下:

这里的问题是我们不允许 pkg 配置文件中的所有标志,因为 SwiftPM 无法推理它们。我们很快就会提出构建设置提案,这将解决这些问题。现在,您可以使用 swift build -Xcc -Xswiftc -Xld 手动传递标志

因此,暂时解决它的唯一正确方法是替换 .pc 文件中的标志,在构建导入这些包的项目时明确指定它们,并祈祷这些标志永远不会干扰,以防您有多个依赖项.

我为 .pc 文件创建了副本(是的,文件,里面有依赖项),删除了标志并从/usr/local/lib/pkgconfig创建了新链接,因为我不希望这个更改有任何副作用。与 OS X 上的 ImageMagick 完美配合,稍后将在 Ubuntu 上尝试。

谢谢你,安基特!

于 2017-02-22T12:54:23.207 回答