是否有一种方法可以使用“swig -MM”生成的依赖关系? [英] Is there a way for CMake to utilize dependencies generated by `swig -MM`?

查看:186
本文介绍了是否有一种方法可以使用“swig -MM”生成的依赖关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

SWIG使用指定要包装的输入代码的接口(.i)文件,以所需的目标语言(Python,Java,C#等)从C / C ++生成包装代码,如SWIG教程。 CMake可用于调用swig,以便从.i界面生成目标代码,如 SWIG中所述文档

SWIG generates wrapper code from your C/C++ in a desired target language (Python, Java, C#, etc) using an interface (.i) file that specifies the input code to be wrapped as described in the SWIG tutorial. CMake can be used to call swig in order to generate target code from the .i interface, as described in the SWIG documentation.

但是,使用此方法,CMake仅为接口文件本身生成依赖关系,但不会为其包含的源文件生成依赖关系。可以手动添加依赖关系,但SWIG可以使用-MM选项自动生成依赖关系,我想让这些被CMake利用。

However, using this method CMake only generates a dependency for the interface file itself, but not for the source files it includes. One can manually add dependencies, but SWIG can generate dependencies automatically with the -MM option, and I would like for these to be utilized by CMake.

有一个提交到使用由 swig -MM 生成的依赖关系的CMake ,但后来被还原。在这个时候,这个问题似乎还没有解决。

There was a commit to CMake that utilized dependencies generated by swig -MM but it was later reverted due to a problem with generated sources that didn't exist at the time of the call to swig. At this time the problem seems to remain unsolved.

所以我把这个问题交给了辉煌的StackOverflow社区: 目前的CMake有没有办法当接口文件(a)不包括生成的代码(例如config.h)和())时,利用 swig -MM b)包括生成的代码?

So I put the issue to the brilliant StackOverflow community: Is there a way with the current CMake to utilize dependencies generated by swig -MM when the interface file (a) does not include generated code (e.g. config.h), and (b) includes generated code?

这是一个可以用于实验的小例子(在此下载)。

Here is a small example that can be used for experimentation (download it here).

// swig_example.h
int foo(int n);
//*** comment this declaration after compiling once to witness dependency failure ***/
int another_function();





// swig_example.cpp
#include "swig_example.h"
int another_function() {return -1;}
int foo(int n) 
{
    if (n <= 1) return 1;
    else return another_function();
}





// swig_example: example.i
%module example
%{
#include "swig_example.h"
%}
%include "swig_example.h"





# swig_example: CMakeLists.txt
FIND_PACKAGE(SWIG REQUIRED)
INCLUDE(${SWIG_USE_FILE})
FIND_PACKAGE(PythonLibs)
INCLUDE_DIRECTORIES(${PYTHON_INCLUDE_PATH})
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR})
SET(CMAKE_SWIG_FLAGS "")

SET_SOURCE_FILES_PROPERTIES(example.i PROPERTIES CPLUSPLUS ON)

#add manual dependencies (must be called before SWIG_ADD_MODULE)
#SET(SWIG_MODULE_example_EXTRA_DEPS ${CMAKE_SOURCE_DIR}/swig_example.h)

SWIG_ADD_MODULE(example python example.i swig_example.cpp)
SWIG_LINK_LIBRARIES(example ${PYTHON_LIBRARIES})

编译一次,然后注释 another_function 的声明,并尝试再次编译因为swig接口没有重新生成,尝试编译examplePYTHON_wrap.cxx时出现错误。

Compile it once, then comment the declaration of another_function and try to compile again. Because the swig interface is not regenerated an error occurs trying to compile examplePYTHON_wrap.cxx.

examplePYTHON_wrap.cxx:3220:17: error: use of undeclared identifier 'another_function'
  result = (int)another_function();

取消注释CMakeLists.txt和界面的添加手动依赖项行将被适当地再生。但是,我希望这可以使用从 swig -MM 生成的依赖关系,而不需要手动指定依赖关系。

Uncomment the add manual dependencies line of the CMakeLists.txt and the interface will be properly regenerated. However, I want this to work using dependencies generated from swig -MM instead of needing to manually specify dependencies.

$ swig -python -MM -c++ ../example.i
../example_wrap.cxx: \
  ../example.i \
  ../swig_example.h \


推荐答案

将我的意见转化为答案

我不认为 - 如果你想自动执行,例如想要使用 swig -MM - 这可以在不改变 UseSWIG.cmake 代码。

I don't think - if you want to do this automatically and e.g. want to utilize swig -MM - that this can be done without changing the UseSWIG.cmake code.

我看看为什么以前尝试的链接已被恢复 - 即讨论 0012307:2.8.5 rc2中的回归:UseSWIG.cmake broken - 我不认为 SWIG_GET_WRAPPER_DEPENDENCIES()实际上是破坏了它刚刚介绍了一个新的限制:在调用 SWIG_ADD_MODULE()之前,这只需要swig模块的所有标题才能存在。

When I look at why the previous attempt you have linked was reverted - namely the discussion of on "0012307: regression in 2.8.5 rc2: UseSWIG.cmake broken" - I don't believe that SWIG_GET_WRAPPER_DEPENDENCIES() was actually broken it just introduced a new restriction: "this simply require all headers for swig module to be present" before calling SWIG_ADD_MODULE().

所以我建议添加 -ignoremissing SWIG选项,但这需要进一步测试。

So I suggested to add the -ignoremissing SWIG option, but this would need further testing.

使用CMake版本3.8.0修复自动扫描SWIG文件的Makefile生成器的依赖关系,适用于 makefile 生成器

With CMake version 3.8.0 there came a fix "Automatically scan dependencies of SWIG files for Makefile generators" that works for makefile generators.

关于如何解决这个问题的一般性讨论(包括我的建议)在问题#4147:[MODULES] [UseSWIG]使用swig来计算依赖关系。该票仍然开放(重新开放),所以请随时在那里添加您的支持,建议或测试结果。

The general discussion of how to fix this (including my suggestion) is discussed at "Issue #4147: [MODULES][UseSWIG] Use swig to compute dependencies". The ticket is still open (was reopened), so please feel free to add your support, suggestions or test results there.

这篇关于是否有一种方法可以使用“swig -MM”生成的依赖关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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