我目前正在try 从GitHubLink安装gr-matchstiq,但遇到了问题.该代码不再符合CMake的标准.

具体地说,cmake 2.6引入了逻辑目标名称必须唯一的策略(见:CMP0002).但是,目标‘All’被重复使用.我相信是这样的,因为我得到了一个错误:

$ cmake -Wno-dev ../
-- Build type not specified: defaulting to release.
Traceback (most recent call last):
  File "<string>", line 2, in <module>
TypeError: Strings must be encoded before hashing
Traceback (most recent call last):
  File "<string>", line 2, in <module>
TypeError: Strings must be encoded before hashing
CMake Error at cmake/Modules/GrPython.cmake:115 (add_custom_target):
  add_custom_target cannot create target "ALL" because another target with
  the same name already exists.  The existing target is a custom target
  created in source directory "/home/me/Projects/gr-matchstiq/swig".
  See documentation for policy CMP0002 for more details.
Call Stack (most recent call first):
  cmake/Modules/GrPython.cmake:214 (GR_UNIQUE_TARGET)
  python/CMakeLists.txt:31 (GR_PYTHON_INSTALL)


Traceback (most recent call last):
  File "<string>", line 2, in <module>
TypeError: Strings must be encoded before hashing
CMake Error at cmake/Modules/GrPython.cmake:115 (add_custom_target):
  add_custom_target cannot create target "ALL" because another target with
  the same name already exists.  The existing target is a custom target
  created in source directory "/home/me/Projects/gr-matchstiq/swig".
  See documentation for policy CMP0002 for more details.
Call Stack (most recent call first):
  cmake/Modules/GrPython.cmake:214 (GR_UNIQUE_TARGET)
  apps/CMakeLists.txt:22 (GR_PYTHON_INSTALL)


-- Configuring incomplete, errors occurred!
See also "/home/me/Projects/gr-matchstiq/build/CMakeFiles/CMakeOutput.log".
See also "/home/me/Projects/gr-matchstiq/build/CMakeFiles/CMakeError.log".

Cmake/Modules/GrPython.cmake:115中的代码为:

add_custom_target(${_target} ALL DEPENDS ${ARGN})

Cmake/Modules/GrPython.cmake:214中的代码为:

GR_UNIQUE_TARGET("pygen" ${python_install_gen_targets})

我几乎没有使用cmake的经验,所以我也不确定哪个修复是最安全的

  1. 在根CMakelists.txt文件中,添加一行(注意:这不起作用,但可能我做错了什么):

    set_property(GLOBAL ALLOW_DUPLICATE_TARGETS TRUE)

  2. 将‘All’cmake/Modules/GrPython.cmake:115更改为类似‘ALL_PY’的内容-即

    add_custom_target(${_target} ALL_PY DEPENDS ${ARGN})

  3. 以某种方式修改GR_UNIQUE_TARGET函数(GrPython.cmake的第107-116行):

########################################################################
# Create an always-built target with a unique name
# Usage: GR_UNIQUE_TARGET(<description> <dependencies list>)
########################################################################
function(GR_UNIQUE_TARGET desc)
    file(RELATIVE_PATH reldir ${CMAKE_BINARY_DIR} ${CMAKE_CURRENT_BINARY_DIR})
    execute_process(COMMAND ${PYTHON_EXECUTABLE} -c "import re, hashlib
unique = hashlib.md5('${reldir}${ARGN}').hexdigest()[:5]
print(re.sub('\\W', '_', '${desc} ${reldir} ' + unique))"
    OUTPUT_VARIABLE _target OUTPUT_STRIP_TRAILING_WHITESPACE)
    add_custom_target(${_target} ALL DEPENDS ${ARGN})
endfunction(GR_UNIQUE_TARGET)

或者,还有什么我应该做的吗?

PS-我需要进行的另一个修复是在第95-102行:

########################################################################
# Sets the python installation directory GR_PYTHON_DIR
########################################################################
execute_process(COMMAND ${PYTHON_EXECUTABLE} -c "
from distutils import sysconfig
print (sysconfig.get_python_lib(plat_specific=True, prefix=''))
" OUTPUT_VARIABLE GR_PYTHON_DIR OUTPUT_STRIP_TRAILING_WHITESPACE
)

最初,Python print语句没有由Python3请求的"("&amp;")")

PPS-我不知道如何处理或找到类型错误,所以将在稍后处理它们.

推荐答案

One thing first: I'll update this answer as more we learn how to approach the problem. Using comments is getting tedious. The answer is a bit verbose so you can follow my steps to approach the problem.

TL;DR: Go with option 3: you need to hash a bytes object

要散列的字符串前缀为b:

unique = hashlib.md5(b'${reldir}${ARGN}').hexdigest()[:5]

我们到目前为止所了解到的(即在 comments 中):

CMake退出时出现错误,因为定义了多个名为"all"的目标.这要归功于这条线

add_custom_target(${_target} ALL DEPENDS ${ARGN})

其中_target是未设置的变量.因此该行变成(我将用伪符号<unset>表示未设置的变量):

add_custom_target(<unset> ALL DEPENDS ${ARGN})

而CMake只看到:

add_custom_target(ALL DEPENDS ${ARGN})

根据签名add_custom_target,第一个参数是目标的名称,而可选的ALL作为第二个参数将导致始终构建目标.如果没有第一个参数,ALL将成为第一个,并导致我们看到的错误.这只是一个次要错误,所以让我们更深入地挖掘.

自定义目标被添加到函数GR_UNIQUE_TARGET中,并且该函数被重复调用.设置静态字符串将不起作用,只会让您回到target name not unique状态.为了给每个目标起一个唯一的名称,最初的开发人员想出了一个很小的Python片段来从输入文件生成散列并从中生成目标名称.

查看一下Python代码

我复制了Python代码并运行了它,很快就出现了以下错误:

TypeError: Unicode-objects must be encoded before hashing

因为Python 3's hashlib预期的是Unicode bytes对象.如果您通过prefixing a b或调用encode方法给它赋值,代码就会正常工作.因此,请try 以下函数(请注意添加的b):

function(GR_UNIQUE_TARGET desc)
    file(RELATIVE_PATH reldir ${CMAKE_BINARY_DIR} ${CMAKE_CURRENT_BINARY_DIR})
    execute_process(COMMAND ${PYTHON_EXECUTABLE} -c "import re, hashlib
unique = hashlib.md5(b'${reldir}${ARGN}').hexdigest()[:5]
print(re.sub('\\W', '_', '${desc} ${reldir} ' + unique))"
    OUTPUT_VARIABLE _target OUTPUT_STRIP_TRAILING_WHITESPACE)
    add_custom_target(${_target} ALL DEPENDS ${ARGN})
endfunction(GR_UNIQUE_TARGET)

在回答"这有可能奏效吗?"时:是的,我确实这么认为.这可能不是最优雅的方式,它看起来有点像这if your only tool is a hammer ...个解决方案中的一个(然而,我抽屉里没有更好的主意),但绝对可行.巧合的是,Python 2.7's hashlib的人对str的物体很满意,所以它可以在那里工作.

Python相关问答推荐

Python:记录而不是在文件中写入询问在多文件项目中记录的最佳实践

Python panda拆分列保持连续多行

在matplotlib动画gif中更改配色方案

Pandas 填充条件是另一列

线性模型PanelOLS和statmodels OLS之间的区别

try 在树叶 map 上应用覆盖磁贴

管道冻结和管道卸载

聚合具有重复元素的Python字典列表,并添加具有重复元素数量的新键

如何获取TFIDF Transformer中的值?

迭代嵌套字典的值

形状弃用警告与组合多边形和多边形如何解决

处理具有多个独立头的CSV文件

如何排除prefecture_related中查询集为空的实例?

Maya Python脚本将纹理应用于所有对象,而不是选定对象

如果包含特定值,则筛选Groupby

根据客户端是否正在传输响应来更改基于Flask的API的行为

从嵌套极轴列的列表中删除元素

Python协议不兼容警告

read_csv分隔符正在创建无关的空列

为什么我只用exec()函数运行了一次文件,而Python却运行了两次?