Swift软件包管理器无法编译通过Homebrew安装的ncurses

时间:2019-05-22 07:51:16

标签: swift ncurses swift-package-manager

我正在尝试使用Swift Package Manager在库中使用ncurses,我想使用ncurses的特定版本,而不是OS X中包含的版本。 为此,我使用Homebrew安装了最新版本(6.1)。 这就是我的Package.swift的样子:

// swift-tools-version:5.0
import PackageDescription

let package = Package(
    name: "NcursesExample",
    products: [
        .executable(name: "NcursesExample", targets: ["NcursesExample"]),
    ],
    dependencies: [
    ],
    targets: [
        .systemLibrary(name: "Cncurses"),
        .target(name: "NcursesExample", dependencies: ["Cncurses"]),
    ]
)

在Sources目录下,我有一个Cncurses的子目录,其中包含module.modulemapshim.h文件:

module.modulemap

module Cncurses {
  header "shim.h"
  link "ncurses"
  export *
}

shim.h

#include "/usr/local/Cellar/ncurses/6.1/include/ncurses.h"

但是,在编译时出现一些错误,抱怨类型冲突,这显然是因为macOS SDK还提供了ncurses:

shim.h:1:10: note: in file included from shim.h:1:
#include "/usr/local/Cellar/ncurses/6.1/include/ncurses.h"
         ^
/usr/local/Cellar/ncurses/6.1/include/ncurses.h:60:10: error: 'ncursesw/ncurses_dll.h' file not found with <angled> include; use "quotes" instead
#include <ncursesw/ncurses_dll.h>
         ^
<module-includes>:1:9: note: in file included from <module-includes>:1:
#import "shim.h"
        ^
shim.h:1:10: note: in file included from shim.h:1:
#include "/usr/local/Cellar/ncurses/6.1/include/ncurses.h"
         ^
/usr/local/Cellar/ncurses/6.1/include/ncurses.h:674:45: error: conflicting types for 'keyname'
extern NCURSES_EXPORT(NCURSES_CONST char *) keyname (int);              /* implemented */
                                            ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk/usr/include/curses.h:598:45: note: previous declaration is here
extern NCURSES_EXPORT(NCURSES_CONST char *) keyname (int);              /* implemented */

...

我正在尝试使用以下方法编译软件包:

swift build -Xcc -I/usr/local/Cellar/ncurses/6.1/include/ -Xlinker -L/usr/local/Cellar/ncurses/6.1/lib

我也遵循了在软件包定义中指定pkgConfig的方法,得到了相同的结果。有人可以帮忙吗?

仅供参考,值得一提的是,通过Homebrew安装ncurses时,由于OS X已经提供了ncurses,我会收到以下警告:

ncurses is keg-only, which means it was not symlinked into /usr/local,
because macOS already provides this software and installing another version in
parallel can cause all kinds of trouble.

If you need to have ncurses first in your PATH run:
  echo 'export PATH="/usr/local/opt/ncurses/bin:$PATH"' >> ~/.zshrc

For compilers to find ncurses you may need to set:
  export LDFLAGS="-L/usr/local/opt/ncurses/lib"
  export CPPFLAGS="-I/usr/local/opt/ncurses/include"

For pkg-config to find ncurses you may need to set:
  export PKG_CONFIG_PATH="/usr/local/opt/ncurses/lib/pkgconfig"

用于ncurses的Pkgconfig看起来像这样

# pkg-config file generated by gen-pkgconfig
# vile:makemode

prefix=/usr/local/Cellar/ncurses/6.1
exec_prefix=${prefix}
libdir=${exec_prefix}/lib
includedir=${prefix}/include/ncursesw
abi_version=6
major_version=6
version=6.1.20180127

Name: ncursesw
Description: ncurses 6.1 library
Version: ${version}
URL: https://invisible-island.net/ncurses
Requires.private:
Libs:  -L${libdir} -lncursesw
Libs.private:
Cflags:  -D_DARWIN_C_SOURCE -I/usr/local/Cellar/ncurses/6.1/include -I${includedir}

3 个答案:

答案 0 :(得分:1)

问题

主要问题是头文件冲突,因为ncurses也在/Applications/Xcode.app/.../MacOSX10.14.sdk/usr/include中提供。

在这种情况下,一种常见的纯C解决方案是只使用-I相应的-L指定自定义include和lib目录,这将起作用,请参见我对C ncurses的回答:https://stackoverflow.com/a/56623033/2331445

这种方法似乎不适用于Swift软件包管理器。但这并不意味着它不可能不费吹灰之力。

可能的解决方案

我们需要确保macOS SDK提供的ncurses头文件被忽略。为此,我们可以为swift build命令指定-Xcc -D__NCURSES_H参数。

之所以可行,是因为在头文件中有一个典型的例子:

#ifndef __NCURSES_H
#define __NCURSES_H
...
#endif

当然,问题在于,使用Brew的ncurses自定义安装也受到影响。但是我们可以解决它:

  • 将新的ncurses头文件复制到我们的Sources / Cncurses目录中
  • 通过其他方式替换__NCURSES_H,例如__CNCURSES_H(请注意以“ C”开头)
  • 然后确保首先在我们的本地包含目录中搜索所有进一步嵌套的包含,方法是将包含的尖括号替换为引号,例如代替#include <ncursesw/unctrl.h>使用'#include“ ncursesw / unctrl.h”'

这实际上可以通过以下命令行命令来完成:

cd Sources/Cncurses
cp -r /usr/local/Cellar/ncurses/6.1/include include
find . -name '*.h' -exec sed -i ''  's/__NCURSES_H/__CNCURSES_H/g' {} \;
find . -name '*.h' -exec sed -i '' -E -e "s/<(.*(`find . -name '*.h' -exec basename {} \; |  paste -sd "|" -`))>/\"\1\"/g"  {} \;

最后一个语句可能需要一些解释。借助echo命令,您可以查看生成的sed表达式,即是否执行

echo "s/<(.*(`find . -name '*.h' -exec basename {} \; |  paste -sd "|" -`))>/\"\1\"/g" 

您将获得以下输出:

s/<(.*(termcap.h|form.h|term.h|panel.h|ncurses.h|termcap.h|cursesp.h|cursesf.h|etip.h|form.h|cursesw.h|nc_tparm.h|unctrl.h|cursesapp.h|term.h|cursslk.h|panel.h|ncurses.h|tic.h|eti.h|ncurses_dll.h|term_entry.h|menu.h|cursesm.h|curses.h|curses.h|cncurses.h))>/"\1"/g

如您所见,它仅搜索和替换本地可用的包含文件。

测试

要进行测试,我们需要一个简单的ncurses示例程序。应该构建它,并且我们应该确保使用正确版本的库。

module.modulemap

我的头文件称为cncurses.h。 module.modulemap看起来像这样:

module cncurses [system] 
{
    umbrella header "cncurses.h"
    link "ncurses"
    export *
}

cncurses.h

cncurses.h是单行代码,它从我们的本地include文件夹导入我们复制并自定义的ncurses.h文件:

#include "include/ncurses.h"

main.swift

在NcursesExample文件夹中,我们有main.swift,其中有一个简单的cncurses swift应用程序:

import cncurses

initscr()
curs_set(0) 
move(5, 10)
addstr("NCURSES")
move(10, 10)
addstr("Hello World!")
refresh() 

select(0, nil, nil, nil, nil)

Package.swift

请在此处注意systemLibrary目标中的pkgConfig: "ncurses"

// swift-tools-version:5.0
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
    name: "NcursesExample",
    dependencies: [
    ],
    targets: [
        .systemLibrary(name: "cncurses", pkgConfig: "ncurses"),
        .target(name: "NcursesExample", dependencies: ["cncurses"]),
        .testTarget(
            name: "NcursesExampleTests",
            dependencies: ["NcursesExample"]),

    ]
)

构建

为使pkg-config正常工作,我们必须首先调用以下命令:

export PKG_CONFIG_PATH="/usr/local/opt/ncurses/lib/pkgconfig"

最后,我们使用以下命令启动构建:

swift build -Xcc -D__NCURSES_H 

因此,首先我们应该测试是否使用了正确的ncurses lib。我们可以这样:

otool -L .build/x86_64-apple-macosx/debug/NcursesExample

在其他行中,输出包含以下内容:

/usr/local/opt/ncurses/lib/libncursesw.6.dylib (compatibility version 6.0.0, current version 6.0.0)

看起来很有希望。

最后调用二进制文件:

ncurses demo

Xcode项目

如果要生成Xcode项目,请使用以下命令:

swift package generate-xcodeproj

然后在Xcode中加载项目并

  • 选择项目节点
  • 在“构建设置”中,在右上角的搜索字段中输入“预处理器”
  • 在Apple Clang下-预处理/预处理宏为调试和发布添加__NCURSES_H = 1

答案 1 :(得分:0)

我对这个主题不太了解,但是似乎您可能想要考虑为此添加一个构建脚本到您的项目中。在Xcode中,在所选项目下,转到Build Phases> New Run Script Phase

enter image description here

在该脚本中添加您的标志:

export LDFLAGS="-L/usr/local/opt/ncurses/lib"
export CPPFLAGS="-I/usr/local/opt/ncurses/include"

构建脚本应在安装时运行。我不确定这里是否需要pkg-config标志。希望这可以帮助您指出正确的方向。

答案 2 :(得分:0)

我会改变

#include "/usr/local/Cellar/ncurses/6.1/include/ncurses.h"

#include <ncurses.h>

因为您已经拥有

-I/usr/local/Cellar/ncurses/6.1/include/

阅读formula,您应该拥有

/usr/local/Cellar/ncurses/6.1/include/ncursesw/ncurses_dll.h

(和其他一些东西),但是使用带引号的包含内容可能会干扰-I选项设置的搜索路径(例如,请参见this)。

相关问题