使用 Swift 包管理器和 libevent 干净地处理/usr/local/ [英] Cleanly handling /usr/local/ with Swift package manager and libevent

查看:40
本文介绍了使用 Swift 包管理器和 libevent 干净地处理/usr/local/的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目 libeventlibressl 中有 2 个依赖项.两者都安装在本地(分别在 /usr/local/include/usr/local/opt/libressl/include 下)

I have 2 dependencies in my project libevent and libressl. Both of which are installed locally ( respectively under /usr/local/include and /usr/local/opt/libressl/include )

我想要实现的是让 SPM 自动理解在这些目录中进行搜索.

What I am looking for to achieve is for SPM to automatically understand to search in those directories.

我知道我可以将标志传递给 swift build 来实现这一点;但我的最终目标是我可以从命令行正确生成 xcode 项目,而不必在 Xcode 中不断添加自定义构建标志.

I know I can pass flags to swift build to achieve this; but my ultimate goal is that I can properly generate xcode projects from the command line without having to constantly add custom build flags in Xcode.

我很确定这是可能的,因为我不必为 PostgreSQL 输入自定义设置.

I'm pretty sure it is possible, since I do not have to enter the custom settings for PostgreSQL.

Swift-tools 版本为 4.0.x

Swift-tools version is at 4.0.x

Package.swift 供参考:

Package.swift for reference:

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

import PackageDescription

let package = Package(
    name: "CEvent",
    providers: [
        .brew(["libevent"]),
        .apt(["libevent-dev"])
    ],
    products: [
        // Products define the executables and libraries produced by a package, and make them visible to other packages.
        .library(
            name: "CEvent",
            targets: ["CEvent"]),
        ],
    dependencies: [
    ],
    targets: [
        .target(
            name: "CEvent",
            dependencies: []
        ),
    ]
)

模块图:

module CEvent [system] {
    header "shim.h"
    link "event"
    export *
}

和我当前的构建脚本( build.sh ):

And my current build script ( build.sh ):

#!/usr/local/bin/fish
swift build -Xcc -O0 -Xcc -fblocks -Xswiftc -lbcrypt -Xswiftc -I/usr/local/include -Xswiftc -L/usr/local/lib -Xswiftc -ltls -Xswiftc -lcrypto -Xswiftc -lssl -Xswiftc -L/usr/local/opt/postgresql/lib -Xswi$

至于我想要这个的原因.如果我在 swift 中添加/更新/删除依赖项,我想生成一个新的 xcode 项目,而不必在各自的构建机器上修复其设置;以及 apt/ubuntu/usr/lib 代替.

As for the reason that I want this. If I add/update/remove dependencies in swift I want to generate a new xcode project, and not have to fix its settings on respective build machines; as well as apt/ubuntu /usr/lib instead.

推荐答案

您发现并记录在您的答案中的是一个好的开始,但不是全部.是的,SwiftPM 使用 pkg-config 来确定某些库的安装位置.是的,SwiftPM 使用 pkgConfig 名称,它将传递给 pkg-config.然而,搜索路径涉及更多.在 macOS 上,它使用以下列表作为 基本搜索路径:

What you've found out, and documented in your answer, is a good start but not the full story. Yes, SwiftPM uses pkg-config to determine where certain libraries are installed. Yes, SwiftPM uses the pkgConfig name which it'll pass on to pkg-config. However the search paths are a bit more involved. On macOS it uses the following list as a base search path:

然而,SwiftPM 不使用 pkg-config 命令,而是直接解析 .pc 文件.通过在您的包上设置 pkgConfig 参数,它知道要在上面列出的路径中查找什么文件名.而且,对于您答案中的示例,故事到此为止.如果找到了 libevent.pc 文件 解析该文件,并将返回的任何标志传递给编译器和链接器.

However SwiftPM doesn't use the pkg-config command, but instead parses .pc files directly. By setting the pkgConfig parameter on your package, it knows what filename to look for in the paths listed above. And, for the example in your answer, the story stops here. If there's a libevent.pc file found it parses that file, and any flags returned are passed on to the compiler and linker.

但是,如果您要定义包提供程序,例如:

However if you were to define package providers, e.g.:

providers: [
    .Brew("libsodium"),
    .Apt("libsodium-dev")
]

然后 SwiftPM 根据其构建平台的包提供程序添加额外的搜索路径.继续以 macOS 为例,SwiftPM 将运行 brew --prefix.如果返回路径,则以下路径为 添加为附加搜索路径:

Then SwiftPM adds additional search paths depending on the package provider for the platform it is building for. Continuing the example of macOS, SwiftPM will run brew --prefix. If this returns a path, the following path is added as a additional search path:

  • [brewPrefix]/opt/[packageName]/lib/pkgconfig

在我的 libsodium 示例中,SwiftPM 现在能够推断出库的位置,而根本不需要 brew link 或符号链接.在我的详细构建输出中,它列出了我地窖中的 libsodium 库路径:-L/usr/local/Cellar/libsodium/1.0.11/lib.

In my example of libsodium, SwiftPM is now able to infer the location of the library without requiring brew link or symlinks at all. In my verbose build output it lists the libsodium library path in my cellar: -L/usr/local/Cellar/libsodium/1.0.11/lib.

这篇关于使用 Swift 包管理器和 libevent 干净地处理/usr/local/的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆