dgpy.cmake 4.53 KB
Newer Older
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
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
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
find_package(SWIG REQUIRED)
find_package(PythonLibs REQUIRED)
find_package(PythonInterp REQUIRED)
if(SWIG_FOUND AND PYTHONLIBS_FOUND)
  message(STATUS "Found SWIG version " ${SWIG_VERSION})
  set_dg_config_option(HAVE_SWIG "Swig")
  include(${SWIG_USE_FILE})
  include_directories(${PYTHON_INCLUDE_PATH})
  include_directories(${CMAKE_CURRENT_BINARY_DIR}/..)
  if (NOT NUMPY_INC)
    EXEC_PROGRAM (${PYTHON_EXECUTABLE}
      ARGS "-c \"import numpy; print(numpy.get_include())\""
      OUTPUT_VARIABLE NUMPY_INC
      RETURN_VALUE NUMPY_NOT_FOUND)
  endif(NOT NUMPY_INC)
  if(NUMPY_INC)
    list(APPEND EXTERNAL_INCLUDES ${NUMPY_INC})
    set_dg_config_option(HAVE_NUMPY "Numpy")
  endif(NUMPY_INC)
endif(SWIG_FOUND AND PYTHONLIBS_FOUND)

#include_directories(${PYTHON_INCLUDE_PATH})
#include_directories(${CMAKE_CURRENT_BINARY_DIR}/..)
#
#include_directories(${CMAKE_BINARY_DIR})

#see CMake bug 4147 (modified to remove dgConfig.h)
MACRO(SWIG_GET_WRAPPER_DEPENDENCIES swigFile genWrapper language DEST_VARIABLE)
  GET_FILENAME_COMPONENT(swig_getdeps_basename ${swigFile} NAME_WE)
  GET_FILENAME_COMPONENT(swig_getdeps_outdir ${genWrapper} PATH)
  GET_SOURCE_FILE_PROPERTY(swig_getdeps_extra_flags "${swigFile}" SWIG_FLAGS)
  IF("${swig_getdeps_extra_flags}" STREQUAL "NOTFOUND")
    SET(swig_getdeps_extra_flags "")
  ENDIF("${swig_getdeps_extra_flags}" STREQUAL "NOTFOUND")

  IF(NOT swig_getdeps_outdir)
    SET(swig_getdeps_outdir ${CMAKE_CURRENT_BINARY_DIR})
  ENDIF(NOT swig_getdeps_outdir)
  SET(swig_getdeps_depsfile
    ${swig_getdeps_outdir}/swig_${swig_getdeps_basename}_deps.txt)
  GET_DIRECTORY_PROPERTY(swig_getdeps_include_directories INCLUDE_DIRECTORIES)
  SET(swig_getdeps_include_dirs)
  FOREACH(it ${swig_getdeps_include_directories})
    SET(swig_getdeps_include_dirs ${swig_getdeps_include_dirs} "-I${it}")
  ENDFOREACH(it)
  EXECUTE_PROCESS(
    COMMAND ${SWIG_EXECUTABLE}
    -MM -MF ${swig_getdeps_depsfile} ${swig_getdeps_extra_flags}
    ${CMAKE_SWIG_FLAGS} -${language}
    -o ${genWrapper} ${swig_getdeps_include_dirs} ${swigFile}
    WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
    RESULT_VARIABLE swig_getdeps_result
    ERROR_VARIABLE swig_getdeps_error
    OUTPUT_STRIP_TRAILING_WHITESPACE)
  IF(NOT EXISTS ${swig_getdeps_depsfile})
    SET(swig_getdeps_dependencies "")
  ELSE(NOT EXISTS ${swig_getdeps_depsfile})
    FILE(READ ${swig_getdeps_depsfile} ${DEST_VARIABLE})
    # Remove the first line
    STRING(REGEX REPLACE "^.+: +\\\\\n +" ""
      ${DEST_VARIABLE} "${${DEST_VARIABLE}}")
    # Clean the end of each line
    STRING(REGEX REPLACE " +(\\\\)?\n" "\n" ${DEST_VARIABLE}
      "${${DEST_VARIABLE}}")
    # Clean beginning of each line
    STRING(REGEX REPLACE "\n +" "\n"
      ${DEST_VARIABLE} "${${DEST_VARIABLE}}")
    # clean paths
    STRING(REGEX REPLACE "\\\\\\\\" "/" ${DEST_VARIABLE}
      "${${DEST_VARIABLE}}")
    STRING(REGEX REPLACE "\n" ";"
      ${DEST_VARIABLE} "${${DEST_VARIABLE}}")
  ENDIF(NOT EXISTS ${swig_getdeps_depsfile})
ENDMACRO(SWIG_GET_WRAPPER_DEPENDENCIES)

macro(dg_add_swig_module module filename libname)
  if(SWIG_FOUND AND PYTHONLIBS_FOUND)
    set(CMAKE_SWIG_OUTDIR ${CMAKE_BINARY_DIR}/dgpy)
    set(CMAKE_SWIG_FLAGS -features autodoc=1)
    get_directory_property(defs COMPILE_DEFINITIONS)
    set(ddefs "")
    foreach(d ${defs})
      list(APPEND ddefs "-D${d}")
    endforeach()
    set_property(SOURCE ${filename} PROPERTY CPLUSPLUS ON)
    set_property(SOURCE ${filename} PROPERTY SWIG_FLAGS "${ddefs}")
    # see CMake bug 4147
    SWIG_GET_WRAPPER_DEPENDENCIES(${filename} ${CMAKE_CURRENT_BINARY_DIR}/${module}PYTHON_wrap.cxx python swig_extra_dependencies)
    LIST(APPEND SWIG_MODULE_${module}_EXTRA_DEPS ${swig_extra_dependencies})
    swig_add_module(${module} python ${filename})
    swig_link_libraries(${module} ${PYTHON_LIBRARIES} ${libname})
    set_target_properties(${SWIG_MODULE_${module}_REAL_NAME} PROPERTIES COMPILE_FLAGS "-Wno-literal-suffix")
    include_directories(${CMAKE_CURRENT_SOURCE_DIR})
    if(APPLE)
      set_target_properties(${SWIG_MODULE_${module}_REAL_NAME} PROPERTIES LINK_FLAGS "-undefined suppress -flat_namespace")
    endif(APPLE)

    set_target_properties(${SWIG_MODULE_${module}_REAL_NAME}
      PROPERTIES
      LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/dgpy
      RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/dgpy
    )
    SET(DG_PYTHON_MODULES_INCLUDE_CODE "${DG_PYTHON_MODULES_INCLUDE_CODE}from dgpy.${module} import *\n")
    SET(DG_PYTHON_MODULES_INCLUDE_CODE "${DG_PYTHON_MODULES_INCLUDE_CODE}" PARENT_SCOPE)
    list(APPEND DGPY_DEPENDS "_${module}")
  endif()
endmacro(dg_add_swig_module)