swig.cmake 1.5 KB
Newer Older
Z
zhangjinchao01 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
find_program(
    SWIG_BINARY_PATH
    swig)

if(${SWIG_BINARY_PATH} STREQUAL "SWIG_BINARY_PATH-NOTFOUND")
    set(SWIG_FOUND OFF)
else()
    set(SWIG_FOUND ON)
endif()

set(MIN_SWIG_VERSION 2)
if(SWIG_FOUND)
    execute_process(COMMAND sh -c "${SWIG_BINARY_PATH} -version | grep Version | cut -f3 -d' '"
        OUTPUT_VARIABLE _SWIG_VERSION
        OUTPUT_STRIP_TRAILING_WHITESPACE)
    if(${_SWIG_VERSION} VERSION_LESS ${MIN_SWIG_VERSION})
        message("swig version ${MIN_SWIG_VERSION} or greater is needed for generating python api. "
                 "Only version ${_SWIG_VERSION} is found. Set SWIG_FOUND to FALSE")
        set(SWIG_FOUND FALSE)
    endif(${_SWIG_VERSION} VERSION_LESS ${MIN_SWIG_VERSION})
endif(SWIG_FOUND)

function(generate_python_api target_name)
    add_custom_command(OUTPUT ${PROJ_ROOT}/paddle/py_paddle/swig_paddle.py
                              ${PROJ_ROOT}/paddle/Paddle_wrap.cxx
                              ${PROJ_ROOT}/paddle/Paddle_wrap.h
        COMMAND swig -python -c++ -outcurrentdir -I../ api/Paddle.swig
                && mv ${PROJ_ROOT}/paddle/swig_paddle.py ${PROJ_ROOT}/paddle/py_paddle/swig_paddle.py
        DEPENDS ${PROJ_ROOT}/paddle/api/Paddle.swig
        WORKING_DIRECTORY ${PROJ_ROOT}/paddle
        COMMENT "Generate Python API from swig")
    add_custom_target(${target_name} ALL DEPENDS
                ${PROJ_ROOT}/paddle/Paddle_wrap.cxx
                ${PROJ_ROOT}/paddle/Paddle_wrap.h
                ${PROJ_ROOT}/paddle/py_paddle/swig_paddle.py)
endfunction(generate_python_api)