SWIG: Moved common SWIG syntax to ${libname}.i

This commit moves common SWIG syntax to a ${libname}.i for sensor
libraries.  Much of the swig content was originally duplicated for
each wrapper language which has lead to inconsistencies between wrappers
over time.  This commit moves all swig syntax to a common file.  Language
specific swig syntax can be added with #ifdef SWIG<LANGUAGE>.

The src/CMakeLists.txt will look first for a language-specific .i file,
then fall back to ${libname}.i.  In this way, it's possible to override
the common ${libname}.i file.  If a fallback .i file does NOT exist,
UPM CMake will generate a simple interface file for all languages.

Example:
    If no src/abp/pyupm_abp.i and no src/abp/abp.i then
    generate ${CMAKE_CURRENT_BINARY_DIR}/abp.i

When src/CMakeLists.txt uses a common ${libname}.i, it adds a -module
<language>upm_${libname} to the swig command line.

In the example below, a -module argument is provided for both Java and
Javascript, while the python module takes all syntax from pyupm_abp.i.

    SWIG FILE              Language       CMake added SWIG args
    ---------------        ----------     ---------------------
    src/abp/abp.i          java           -module javaupm_abp
    src/abp/abp.i          javascript     -module jsupm_abp
    src/abp/pyupm_abp.i    python

This commit removes ~4500 redundant lines for the UPM repository and
helps promote uniformity for the SWIG'ed languages.

Signed-off-by: Noel Eck <noel.eck@intel.com>
This commit is contained in:
Noel Eck
2018-01-26 11:52:34 -08:00
parent 3d674efb51
commit 20aa4962f0
809 changed files with 3421 additions and 8069 deletions

View File

@ -228,18 +228,63 @@ function (_get_target_dependency_interface_include_dirs target varIncludeDirs)
set(${varIncludeDirs} "${${varIncludeDirs}}" PARENT_SCOPE)
endfunction (_get_target_dependency_interface_include_dirs target varIncludeDirs)
# This function decides which swig interface file to use for the
# CMAKE_CURRENT_SOURCE_DIR. If no .i file exists, create one.
#
# if ${filePrefix}_${libname}.i exists
# return its full path
# elif ${libname}.i exists
# return its full path
# else
# create ${libname}.i via template and return its full path
#
function (_get_current_dot_i_file filePrefix varDotIFile)
set(${varDotIFile} "${CMAKE_CURRENT_SOURCE_DIR}/${libname}.i")
if (EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/${filePrefix}_${libname}.i")
set(${varDotIFile} "${CMAKE_CURRENT_SOURCE_DIR}/${filePrefix}_${libname}.i")
endif()
# If a SWIG .i file DOES NOT exist, make a simple one from the template
if (NOT EXISTS "${${varDotIFile}}")
# This file will exist OUTSIDE the source tree
set(${varDotIFile} "${CMAKE_CURRENT_BINARY_DIR}/${libname}.i")
# First, create the include variables
set(SWIG_HASH_INCLUDES)
foreach (_hpp ${module_hpp})
set(SWIG_HASH_INCLUDES "#include \"${_hpp}\"")
endforeach()
string(REPLACE "#" "%" SWIG_PERCENT_INCLUDES "${SWIG_HASH_INCLUDES}")
# Write the interface file
configure_file (${PROJECT_SOURCE_DIR}/src/swigme.i.in "${${varDotIFile}}" @ONLY)
endif()
set(${varDotIFile} "${${varDotIFile}}" PARENT_SCOPE)
endfunction()
# Create a single swig target for python
# This is a macro since the function which calls it (upm_swig_python) depends
# on values set inside this macro
macro(_upm_swig_python)
# Transitive headers don't appear to get picked up by swig_add_module call for
# wrapper generation. Get them manually and add them here.
_get_target_dependency_interface_include_dirs(${libname} DEPEND_DIRS)
include_directories (${CMAKE_CURRENT_SOURCE_DIR}/..
include_directories (${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_CURRENT_BINARY_DIR}/..
${DEPEND_DIRS})
set_source_files_properties (pyupm_${libname}.i PROPERTIES CPLUSPLUS ON)
set_source_files_properties (pyupm_${libname}.i PROPERTIES SWIG_FLAGS "-I${CMAKE_CURRENT_BINARY_DIR}/..")
# Decide between ${libname}.i or a language-specific .i
_get_current_dot_i_file(pyupm SWIG_CURRENT_DOT_I_FILE)
# If this module is using ${libname}.i, provide a module name for UseSWIG AND SWIG_FLAGS
if (${SWIG_CURRENT_DOT_I_FILE} MATCHES ${libname}.i)
set_source_files_properties(${SWIG_CURRENT_DOT_I_FILE} PROPERTIES
SWIG_MODULE_NAME pyupm_${libname}
SWIG_FLAGS ";-module;pyupm_${libname};")
endif()
# Tell UseSWIG that this is CXX
set_property(SOURCE ${SWIG_CURRENT_DOT_I_FILE} PROPERTY CPLUSPLUS ON)
# Set current python binary dir
set (CMAKE_CURRENT_PYTHON_BINARY_DIR
@ -255,9 +300,9 @@ macro(_upm_swig_python)
# Swig module name (example: pyupm_a110x-python2)
set (python_wrapper_name pyupm_${libname}-python${PYTHON_VERSION_MAJOR})
if (CMAKE_VERSION VERSION_LESS "3.8")
swig_add_module (${python_wrapper_name} python pyupm_${libname}.i)
swig_add_module (${python_wrapper_name} python ${SWIG_CURRENT_DOT_I_FILE})
else ()
swig_add_library (${python_wrapper_name} LANGUAGE python SOURCES pyupm_${libname}.i)
swig_add_library (${python_wrapper_name} LANGUAGE python SOURCES ${SWIG_CURRENT_DOT_I_FILE})
endif ()
# Get target library name (example _pyupm_a110x-python2)
set (python_wrapper_target ${SWIG_MODULE_${python_wrapper_name}_REAL_NAME})
@ -361,16 +406,29 @@ function(upm_swig_node)
# wrapper generation. Get them manually and add them here.
_get_target_dependency_interface_include_dirs(${libname} DEPEND_DIRS)
include_directories (${CMAKE_CURRENT_SOURCE_DIR}/..
include_directories (${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_CURRENT_BINARY_DIR}/..
${DEPEND_DIRS})
set_property (SOURCE jsupm_${libname}.i PROPERTY SWIG_FLAGS "-node" "-DV8_VERSION=${V8_VERSION_HEX}")
set_source_files_properties (jsupm_${libname}.i PROPERTIES CPLUSPLUS ON)
# Decide between ${libname}.i or a language-specific .i
_get_current_dot_i_file(jsupm SWIG_CURRENT_DOT_I_FILE)
# If this module is using ${libname}.i, provide a module name for UseSWIG AND SWIG_FLAGS
if (${SWIG_CURRENT_DOT_I_FILE} MATCHES ${libname}.i)
set_source_files_properties(${SWIG_CURRENT_DOT_I_FILE} PROPERTIES
SWIG_MODULE_NAME jsupm_${libname}
SWIG_FLAGS ";-module;jsupm_${libname};")
endif()
# Tell UseSWIG that this is CXX
set_property(SOURCE ${SWIG_CURRENT_DOT_I_FILE} PROPERTY CPLUSPLUS ON)
# Append additional flags for NodeJS
set_property(SOURCE ${SWIG_CURRENT_DOT_I_FILE} APPEND PROPERTY
SWIG_FLAGS ";-node;-DV8_VERSION=${V8_VERSION_HEX}")
if (CMAKE_VERSION VERSION_LESS "3.8")
swig_add_module (jsupm_${libname} javascript jsupm_${libname}.i)
swig_add_module (jsupm_${libname} javascript ${SWIG_CURRENT_DOT_I_FILE})
else ()
swig_add_library (jsupm_${libname} LANGUAGE javascript SOURCES jsupm_${libname}.i)
swig_add_library (jsupm_${libname} LANGUAGE javascript SOURCES ${SWIG_CURRENT_DOT_I_FILE})
endif ()
add_dependencies(jsupm_${libname} ${libname})
swig_link_libraries (jsupm_${libname} ${NODE_LIBRARIES} ${libname})
@ -427,20 +485,33 @@ function(upm_swig_java)
# wrapper generation. Get them manually and add them here.
_get_target_dependency_interface_include_dirs(${libname} DEPEND_DIRS)
include_directories (
${JAVA_INCLUDE_PATH}
${JAVA_INCLUDE_PATH2}
${CMAKE_CURRENT_BINARY_DIR}/..
${CMAKE_CURRENT_BINARY_DIR}/..
${DEPEND_DIRS}
)
include_directories (${JAVA_INCLUDE_PATH}
${JAVA_INCLUDE_PATH2}
${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_CURRENT_BINARY_DIR}/..
${DEPEND_DIRS})
# Decide between ${libname}.i or a language-specific .i
_get_current_dot_i_file(javaupm SWIG_CURRENT_DOT_I_FILE)
# If this module is using ${libname}.i, provide a module name for UseSWIG AND SWIG_FLAGS
if (${SWIG_CURRENT_DOT_I_FILE} MATCHES ${libname}.i)
set_source_files_properties(${SWIG_CURRENT_DOT_I_FILE} PROPERTIES
SWIG_MODULE_NAME javaupm_${libname}
SWIG_FLAGS ";-module;javaupm_${libname};")
endif()
# Tell UseSWIG that this is CXX
set_property(SOURCE ${SWIG_CURRENT_DOT_I_FILE} PROPERTY CPLUSPLUS ON)
# Set a -package as well (which is slightly different than the module name,
# module: javaupm_a110x vs package: upm_a110x
set_property(SOURCE ${SWIG_CURRENT_DOT_I_FILE} APPEND PROPERTY
SWIG_FLAGS ";-package;upm_${libname}")
set_source_files_properties (javaupm_${libname}.i PROPERTIES CPLUSPLUS ON)
set_source_files_properties (javaupm_${libname}.i PROPERTIES SWIG_FLAGS ";-package;upm_${libname};-I${CMAKE_BINARY_DIR}/src")
if (CMAKE_VERSION VERSION_LESS "3.8")
swig_add_module (javaupm_${libname} java javaupm_${libname}.i)
swig_add_module (javaupm_${libname} java ${SWIG_CURRENT_DOT_I_FILE})
else ()
swig_add_library (javaupm_${libname} LANGUAGE java SOURCES javaupm_${libname}.i)
swig_add_library (javaupm_${libname} LANGUAGE java SOURCES ${SWIG_CURRENT_DOT_I_FILE})
endif ()
# If the C++ target depends on C++ interfaces, make the JAVA target