Statistics
| Branch: | Tag: | Revision:

root / cmake / Modules / GrSwig.cmake @ 4f8d9d56

History | View | Annotate | Download (4.8 kB)

1
# Copyright 2010-2011 Free Software Foundation, Inc.
2
# 
3
# This file is part of GNU Radio
4
# 
5
# GNU Radio is free software; you can redistribute it and/or modify
6
# it under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 3, or (at your option)
8
# any later version.
9
# 
10
# GNU Radio is distributed in the hope that it will be useful,
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
# GNU General Public License for more details.
14
# 
15
# You should have received a copy of the GNU General Public License
16
# along with GNU Radio; see the file COPYING.  If not, write to
17
# the Free Software Foundation, Inc., 51 Franklin Street,
18
# Boston, MA 02110-1301, USA.
19
20
IF(DEFINED __INCLUDED_GR_SWIG_CMAKE)
21
    RETURN()
22
ENDIF()
23
SET(__INCLUDED_GR_SWIG_CMAKE TRUE)
24
25
INCLUDE(GrPython)
26
27
########################################################################
28
# Build a swig target for the common gnuradio use case. Usage:
29
# GR_SWIG_MAKE(target ifile ifile ifile...)
30
#
31
# Set the following variables before calling:
32
#   - GR_SWIG_FLAGS
33
#   - GR_SWIG_INCLUDE_DIRS
34
#   - GR_SWIG_LIBRARIES
35
#   - GR_SWIG_SOURCE_DEPS
36
#   - GR_SWIG_TARGET_DEPS
37
########################################################################
38
MACRO(GR_SWIG_MAKE name)
39
    SET(ifiles ${ARGN})
40
41
    #determine include dependencies for swig file
42
    EXECUTE_PROCESS(
43
        COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_BINARY_DIR}/get_swig_deps.py
44
        "${ifiles}" "${GR_SWIG_INCLUDE_DIRS}"
45
        OUTPUT_STRIP_TRAILING_WHITESPACE
46
        OUTPUT_VARIABLE SWIG_MODULE_${name}_EXTRA_DEPS
47
        WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
48
    )
49
50
    #append the specified include directories
51
    INCLUDE_DIRECTORIES(${GR_SWIG_INCLUDE_DIRS})
52
    LIST(APPEND SWIG_MODULE_${name}_EXTRA_DEPS ${GR_SWIG_SOURCE_DEPS})
53
54
    FIND_PACKAGE(PythonLibs)
55
    INCLUDE_DIRECTORIES(${PYTHON_INCLUDE_DIRS})
56
57
    #setup the swig flags with flags and include directories
58
    SET(CMAKE_SWIG_FLAGS -fvirtual -modern -keyword -w511 -module ${name} ${GR_SWIG_FLAGS})
59
    FOREACH(dir ${GR_SWIG_INCLUDE_DIRS})
60
        LIST(APPEND CMAKE_SWIG_FLAGS "-I${dir}")
61
    ENDFOREACH(dir)
62
63
    #set the C++ property on the swig .i file so it builds
64
    SET_SOURCE_FILES_PROPERTIES(${ifiles} PROPERTIES CPLUSPLUS ON)
65
66
    #setup the actual swig library target to be built
67
    INCLUDE(UseSWIG)
68
    SWIG_ADD_MODULE(${name} python ${ifiles})
69
    SWIG_LINK_LIBRARIES(${name} ${PYTHON_LIBRARIES} ${GR_SWIG_LIBRARIES})
70
    IF(GR_SWIG_TARGET_DEPS)
71
        ADD_DEPENDENCIES(${SWIG_MODULE_${name}_REAL_NAME} ${GR_SWIG_TARGET_DEPS})
72
    ENDIF(GR_SWIG_TARGET_DEPS)
73
74
ENDMACRO(GR_SWIG_MAKE)
75
76
########################################################################
77
# Install swig targets generated by GR_SWIG_MAKE. Usage:
78
# GR_SWIG_INSTALL(
79
#   TARGETS target target target...
80
#   [DESTINATION destination]
81
#   [COMPONENT component]
82
# )
83
########################################################################
84
MACRO(GR_SWIG_INSTALL)
85
86
    INCLUDE(CMakeParseArgumentsCopy)
87
    CMAKE_PARSE_ARGUMENTS(GR_SWIG_INSTALL "" "DESTINATION;COMPONENT" "TARGETS" ${ARGN})
88
89
    FOREACH(name ${GR_SWIG_INSTALL_TARGETS})
90
        INSTALL(TARGETS ${SWIG_MODULE_${name}_REAL_NAME}
91
            DESTINATION ${GR_SWIG_INSTALL_DESTINATION}
92
            COMPONENT ${GR_SWIG_INSTALL_COMPONENT}
93
        )
94
95
        INCLUDE(GrPython)
96
        GR_PYTHON_INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/${name}.py
97
            DESTINATION ${GR_SWIG_INSTALL_DESTINATION}
98
            COMPONENT ${GR_SWIG_INSTALL_COMPONENT}
99
        )
100
    ENDFOREACH(name)
101
102
ENDMACRO(GR_SWIG_INSTALL)
103
104
########################################################################
105
# Generate a python file that can determine swig dependencies.
106
# Used by the make macro above to determine extra dependencies.
107
# When you build C++, CMake figures out the header dependencies.
108
# This code essentially performs that logic for swig includes.
109
########################################################################
110
FILE(WRITE ${CMAKE_BINARY_DIR}/get_swig_deps.py "
111
112
import os, sys, re
113
114
include_matcher = re.compile('[#|%]include\\s*[<|\"](.*)[>|\"]')
115
include_dirs = sys.argv[2].split(';')
116
117
def get_swig_incs(file_path):
118
    file_contents = open(file_path, 'r').read()
119
    return include_matcher.findall(file_contents, re.MULTILINE)
120
121
def get_swig_deps(file_path, level):
122
    deps = [file_path]
123
    if level == 0: return deps
124
    for inc_file in get_swig_incs(file_path):
125
        for inc_dir in include_dirs:
126
            inc_path = os.path.join(inc_dir, inc_file)
127
            if not os.path.exists(inc_path): continue
128
            deps.extend(get_swig_deps(inc_path, level-1))
129
    return deps
130
131
if __name__ == '__main__':
132
    ifiles = sys.argv[1].split(';')
133
    deps = sum([get_swig_deps(ifile, 3) for ifile in ifiles], [])
134
    #sys.stderr.write(';'.join(set(deps)) + '\\n\\n')
135
    print(';'.join(set(deps)))
136
")