带有 ccache 的 Xcode Address Sanitizer

问题描述 投票:0回答:2

我正在尝试在 Xcode 中使用 Address Sanitizer,但我也使用 ccache 来加速我的构建。为此,我在 Xcode 项目级别设置了

CC
标志,以指向我存储在
~/projects/support/cmake/ccache-clang
的脚本。剧本是这么写的

#!/bin/sh
if type -p /usr/local/bin/ccache >/dev/null 2>&1; then
  export CCACHE_CPP2=true
  exec /usr/local/bin/ccache "${DEVELOPER_DIR}/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang" "$@"
else
  exec "${DEVELOPER_DIR}/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang" "$@"
fi

这样就达到了预期的效果(如果开发者系统上安装了ccache,则使用ccache)。这运作良好。

现在,当我们为给定方案打开地址消毒器时,问题就出现了。构建成功完成,但是当我们尝试运行时,我们终于看到了这个错误:

Error: Check dependencies

Could not determine version of clang to find its Address Sanitizer library: ~/projects/support/cmake/ccache-clang

这可能是由于 Xcode 中的一些 hacky 实现导致的,它尝试解析编译器的路径来确定版本?

想知道如何修改我的脚本以允许 Address Sanitizer 工作。当前的解决方法是当我们需要进行消毒工作时删除被覆盖的

CC
标志。

使用 Xcode 8.3.1

xcode xcode8 address-sanitizer ccache
2个回答
2
投票

直接链接Asan库,而不是在方案中勾选复选框。

链接器编译器标志应包含以下内容。

-fsanitize=address -fno-omit-frame-pointer

如果找不到库,找到asan动态库的路径,一般在

${CMAKE_LINKER}/../../lib/clang/version/lib/darwin/libclang_rt.asan_osx_dynamic.dylib

并将其添加到链接器标志中。


0
投票

我能够让它发挥作用。使用 Xcode 14.2 进行测试

  1. 脚本

    ccache-clang
    必须重命名为
    clang
    clang++
    (即
    clang++
    clang
    的副本)。看来 Xcode 首先会尝试检测编译器的名称:clang、gcc 等,因此这样命名脚本会使 Xcode 确定这是 clang。

  2. 在 CMake 配置过程中,必须将消毒程序所需的库复制到 bin 目录中。比如:

set (_toolchain_root "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain")
# I tested on iOS/Simulator
file (GLOB_RECURSE _asan_libs
      "${_toolchain_root}/*/libclang_rt.*an_ios*_dynamic.dylib")

foreach (_asan_lib IN LISTS _asan_libs)
  file (RELATIVE_PATH _asan_relative_to_toolchain
        "${_toolchain_root}/usr/" "${_asan_lib}")
  configure_file ("${_asan_lib}"
                  "${CMAKE_BINARY_DIR}/../${_asan_relative_to_toolchain}"
                  COPYONLY)
endforeach ()

此后,我能够从 Xcode 启用消毒程序,编译并运行该应用程序。

© www.soinside.com 2019 - 2024. All rights reserved.