Unix

cmake 로 swig python 모듈 빌드하기

ForceCore 2016. 10. 20. 03:50

Makefile을 만들다보면 결국은 열받게 되어있다... ㅋㅋ


CMakeLists.txt


# CMake for Swig Python

cmake_minimum_required(VERSION 3.1.0 FATAL_ERROR)


find_package(SWIG REQUIRED)

include(${SWIG_USE_FILE})


find_package(PythonLibs)

include_directories(${PYTHON_INCLUDE_PATH})

include_directories(${CMAKE_CURRENT_SOURCE_DIR})


# language i'm using (C++, not C)

set_source_files_properties(graph.i PROPERTIES CPLUSPLUS ON)

set_source_files_properties(graph.cpp PROPERTIES CPLUSPLUS11 ON)


set(CMAKE_SWIG_OUTDIR ${PROJECT_BINARY_DIR}/..) # must come before add_module haha


# Since I'm just "including" graph.h in graph.i,

# any change in .h file is equivalent to .i file but in the file system,

# this can't be detected as time stamp change. To compensate:

SET(SWIG_MODULE_graph_EXTRA_DEPS graph.h)


# To get _graph.so, the target is graph

# and the files to build from are provided.

swig_add_module(graph python graph.i graph.cpp)

swig_link_libraries(graph ${PYTHON_LIBRARIES})


# set where _graph.so will be located.

set_target_properties(_graph PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/..)


Makefile:

all: bld
   cd bld && cmake .. && make

bld:
   mkdir bld

clean:
   rm -rf bld _graph.so graph.py


이런 식이다. 염두에 둔 구조는... 흐음...


cxx/graph.i, graph.h, graph.cpp 파일을 열심히 코딩한다. 이 안에 위에서 제시한 Makefile도 있다.

make 명령어를 여기서 내리면 cmake/graph.py, _graph.so 파일이 생긴다.