我可以使用CMake成功构建SWIG模块,例如给出了一个简单的例子.i:
%module example
和一个空的example.cxx以下内容足以编译一个空的但工作正常的Python模块:
cmake_minimum_required(VERSION 2.8)
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 "-Wall")
SET_SOURCE_FILES_PROPERTIES(example.i PROPERTIES CPLUSPLUS ON)
SET_SOURCE_FILES_PROPERTIES(example.i PROPERTIES SWIG_FLAGS "-builtin")
SWIG_ADD_MODULE(example python example.i example.cxx)
SWIG_LINK_LIBRARIES(example ${PYTHON_LIBRARIES})
如果我想在构建期间调用更多选项到SWIG,那么显而易见的解决方案是更改CMakeLists.txt:
SET_SOURCE_FILES_PROPERTIES(example.i PROPERTIES SWIG_FLAGS "-py3 -builtin -extranative")
但是这不起作用,现在构建失败了:
[ 33%] Swig source
swig error : Unrecognized option -py3 -builtin -extranative
Use 'swig -help' for available options.
make[2]: *** [examplePYTHON_wrap.cxx] Error 1
make[1]: *** [CMakeFiles/_example.dir/all] Error 2
make: *** [all] Error 2
使用VERBOSE=99
运行构建,我们可以看到原因:
/usr/bin/swig2.0 -python -py3\ -builtin\ -extranative -Wall -outdir ...
它“有助于”逃离空间。我如何阻止CMake这样做?
我尝试了几个想法来修复它:
将参数分开
SET_SOURCE_FILES_PROPERTIES(example.i PROPERTIES SWIG_FLAGS "-py3", "-builtin", "-extranative")
但这只是忽略了第二个和第三个:
/usr/bin/swig2.0 -python -py3 -Wall -outdir ....
使用SEPARATE_ARGUMENTS
宏:
SET_SOURCE_FILES_PROPERTIES(example.i PROPERTIES SWIG_FLAGS SEPARATE_ARGUMENTS("-py3 -builtin -extranative"))
然而,CMake失败了:
set_source_files_properties called with incorrect number of arguments.
正确传递多个选项的方法是什么?
答案 0 :(得分:5)
尝试使用set_property功能代替set_source_files_properties
。这个允许属性值为CMake列表:
set_property(SOURCE example.i PROPERTY SWIG_FLAGS "-py3" "-builtin" "-extranative")
答案 1 :(得分:0)
当你用分号分隔标志时,它将SET_SOURCE_FILES_PROPERTIES
接受它们(在cmake 2.8.11上测试):
SET_SOURCE_FILES_PROPERTIES(example.i PROPERTIES SWIG_FLAGS "-py3;-builtin;-extranative;")