今天我尝试使用" cmake + swig"为我的代码生成绑定。基本上它有效:
set(SWIG_EXECUTABLE "/usr/bin/swig")
find_package(SWIG REQUIRED)
include(${CMAKE_CURRENT_SOURCE_DIR}/UseSWIG.cmake)
set(CMAKE_SWIG_FLAGS -package example)
set(CMAKE_SWIG_OUTDIR "${CMAKE_CURRENT_SOURCE_DIR}/example")
set_source_files_properties(native.i PROPERTIES CPLUSPLUS ON)
SWIG_ADD_MODULE(core Java native.i lib.cpp)
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
有这样的native.i:
%module native
%include "lib.hpp"
%{
#include "lib.hpp"
%}
但是,如果我更改lib.hpp
构建系统cmake build
,则不要调用swig来重新生成代码。所以我必须touch native.i
让它工作,这非常烦人。
我找到-M
swig
选项来生成依赖项,
有什么想法用它来修复UseSWIG.cmake
?
UseSWIG.cmake
只使用add_custom_command
进行生成,
所以我需要某种方式add_custom_command
依赖于动态的文件集,
不是静止的?
答案 0 :(得分:2)
在配置期间,您可以使用-M标志运行swig以生成依赖项。然后,您可以解析该输出并将其作为DEPDENDS传递给add_custom_command。
输出如下:
test_wrap.c: \
.../swig.swg \
... \
test.i \
test.h
这可以使用execute_command生成,需要进一步处理:
execute_process(COMMAND swig -M <SWIG_ARGUMENTs> OUTPUT_VARIABLES swig_deps)
# Match all lines except the first one until " \"
string(REGEX MATCHALL "\n [^ ]+" temp ${swig_deps})
set(swig_deps)
foreach(t ${temp})
string(STRIP "${t}" t)
set(swig_deps ${swig_deps} "${t}")
endforeach()
...
add_custom_command(... DEPENDS ${swig_deps})
这使得swig依赖于.i文件中包含的所有标头。如果以添加新依赖关系的方式编辑.i或头文件之一,则需要重新配置,以便cmake了解它。如果您添加CMAKE_CONFIGURE_DEPENDS,则会自动执行此操作。
set_property(DIRECTORY ${CMAKE_SOURCE_DIR} APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS ${swig_deps} test.i)
答案 1 :(得分:2)
我相信我找到了您的请求的解决方案。
我目前正在使用一个解决方案,其中添加了所有相关的依赖项,以便SWIG
重新生成接口,无论何时修改任何解析的标头。
我们的想法是创建一个自定义目标,除了删除生成的接口文件外,还会触摸虚拟文件。我已将下面的解决方案放在一个名为fnm
的项目中,并带有包装器swig_fnm
。
# Method to make swig_fnm.i depend on input headers
execute_process(COMMAND swig -M -python -c++ -I${CMAKE_CURRENT_BINARY_DIR}/.. -I${CMAKE_CURRENT_SOURCE_DIR}/.. swig_fnm.i
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
OUTPUT_VARIABLE swig_deps
INPUT_FILE swig_fnm.i)
# Match all lines except the first one until " \"
string(REGEX MATCHALL "\n [^ ]+" temp ${swig_deps})
# Valid dependency extensions
set(valid_ext .h .hpp)
# Dependency list
set(swig_deps_actual)
foreach(t ${temp})
string(STRIP "${t}" t)
# Add to dependency list
if (EXISTS "${t}")
set(filter)
get_filename_component(filter "${t}" EXT)
if (";${valid_ext};" MATCHES ";${filter};")
set(swig_deps_actual ${swig_deps_actual} "${t}")
endif()
endif()
endforeach()
# This makes configure run again, but does not regenerate the SWIG interface.
set_property(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS ${swig_deps_actual})
# All headers except for the single .i file are ignored
swig_add_module(swig_fnm python swig_fnm.i ${swig_fnm_HEADERS} ${swig_deps_actual})
# Removes generated file (if any of the dependent files are changed)
add_custom_command(
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/swig.stamp
COMMAND ${CMAKE_COMMAND} -E remove ${swig_generated_file_fullname}
COMMAND ${CMAKE_COMMAND} -E touch ${CMAKE_CURRENT_BINARY_DIR}/swig.stamp
DEPENDS ${swig_deps_actual} # The dependent files
COMMENT "Removing old SWIG generated file" VERBATIM)
# Custom target for establishing dependency
add_custom_target(
swigtrick
DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/swig.stamp)
# Dependency
add_dependencies(_swig_fnm swigtrick)
答案 2 :(得分:1)
确实,查看UseSWIG.cmake的源代码,custom commands构建在.i文件上,并且错过了对other sources的依赖。您可以做的是将extra dependencies添加到自定义命令:
set(SWIG_MODULE_core_EXTRA_DEPS lib.hpp)
SWIG_ADD_MODULE(core Java native.i lib.cpp)
或者,您可以修改UseSWIG.cmake以自动添加依赖项:
macro(SWIG_ADD_SOURCE_TO_MODULE name outfiles infile other_sources)
# ...
add_custom_command(
# ...
DEPENDS ${SWIG_MODULE_${name}_EXTRA_DEPS} ${other_sources}
# ...
)
endmacro()
# ...
foreach(it ${swig_dot_i_sources})
SWIG_ADD_SOURCE_TO_MODULE(${name} swig_generated_source ${it} ${swig_other_sources})
set(swig_generated_sources ${swig_generated_sources} "${swig_generated_source}")
endforeach()
这导致我最初的评论:
SWIG_ADD_MODULE(core Java native.i lib.cpp lib.hpp)