swig.cmake 3.9 KB
Newer Older
L
liaogang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
# Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
# 
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# 
# http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Look for system swig
L
liaogang 已提交
16
FIND_PACKAGE(SWIG)
L
liaogang 已提交
17

L
liaogang 已提交
18
IF(NOT ${SWIG_FOUND})
L
liaogang 已提交
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
    # build swig as an external project
    INCLUDE(ExternalProject)
    SET(SWIG_SOURCES_DIR ${CMAKE_CURRENT_SOURCE_DIR}/third_party/swig)
    SET(SWIG_INSTALL_DIR ${PROJECT_BINARY_DIR}/swig)
    SET(SWIG_TARGET_VERSION "3.0.2")
    SET(SWIG_DOWNLOAD_SRC_MD5 "62f9b0d010cef36a13a010dc530d0d41")
    SET(SWIG_DOWNLOAD_WIN_MD5 "3f18de4fc09ab9abb0d3be37c11fbc8f")

    IF(WIN32)
        # swig.exe available as pre-built binary on Windows:
        ExternalProject_Add(swig
            URL     http://prdownloads.sourceforge.net/swig/swigwin-${SWIG_TARGET_VERSION}.zip
            URL_MD5 ${SWIG_DOWNLOAD_WIN_MD5}
            SOURCE_DIR ${SWIG_SOURCES_DIR}
            CONFIGURE_COMMAND   ""
            BUILD_COMMAND       ""
            INSTALL_COMMAND     ""
        )
        SET(SWIG_DIR ${SWIG_SOURCES_DIR} CACHE FILEPATH "SWIG Directory" FORCE)
        SET(SWIG_EXECUTABLE ${SWIG_SOURCES_DIR}/swig.exe  CACHE FILEPATH "SWIG Executable" FORCE)

    ELSE(WIN32)
        # From PCRE configure
        ExternalProject_Add(pcre
            GIT_REPOSITORY https://github.com/svn2github/pcre.git
            PREFIX ${SWIG_SOURCES_DIR}/pcre
            UPDATE_COMMAND ""
            CMAKE_ARGS -DCMAKE_INSTALL_PREFIX:PATH=${SWIG_INSTALL_DIR}/pcre
        )

        # swig uses bison find it by cmake and pass it down
        FIND_PACKAGE(BISON)

        # From SWIG configure
        ExternalProject_Add(swig
            URL     https://github.com/swig/swig/archive/rel-3.0.10.tar.gz
            PREFIX  ${SWIG_SOURCES_DIR}
            UPDATE_COMMAND ""
            CONFIGURE_COMMAND cd ${SWIG_SOURCES_DIR}/src/swig && ./autogen.sh
            CONFIGURE_COMMAND cd ${SWIG_SOURCES_DIR}/src/swig &&
            env "PCRE_LIBS=${SWIG_INSTALL_DIR}/pcre/lib/libpcre.a \
                ${SWIG_INSTALL_DIR}/pcre/lib/libpcrecpp.a \
                ${SWIG_INSTALL_DIR}/pcre/lib/libpcreposix.a"
            ./configure
                --prefix=${SWIG_INSTALL_DIR}
                --with-pcre-prefix=${SWIG_INSTALL_DIR}/pcre
            BUILD_COMMAND cd ${SWIG_SOURCES_DIR}/src/swig && make
            INSTALL_COMMAND cd ${SWIG_SOURCES_DIR}/src/swig && make install
L
liaogang 已提交
67
            DEPENDS pcre
L
liaogang 已提交
68 69 70 71 72
        )

        set(SWIG_DIR ${SWIG_INSTALL_DIR}/share/swig/${SWIG_TARGET_VERSION} CACHE FILEPATH "SWIG Directory" FORCE)
        set(SWIG_EXECUTABLE ${SWIG_INSTALL_DIR}/bin/swig CACHE FILEPATH "SWIG Executable" FORCE)
    ENDIF(WIN32)
L
liaogang 已提交
73
ENDIF()
L
liaogang 已提交
74

L
liaogang 已提交
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
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_EXECUTABLE} -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
                ${PROJ_ROOT}/paddle/api/PaddleAPI.h
        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)