Skip to content

Commit 20aa496

Browse files
committed
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>
1 parent 3d674ef commit 20aa496

File tree

809 files changed

+3425
-8073
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

809 files changed

+3425
-8073
lines changed

docs/creating_java_bindings.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ We extend the sensor class with another method, _installISR\(jobject runnable\)_
259259

260260
Java callbacks are added at the SWIG interface level. For ease-of-use, a collection of macros are available in src/_upm.i.
261261

262-
src/a110x/javaupm_a110x.i:
262+
src/a110x/a110x.i:
263263
```c++
264264
JAVA_ADD_INSTALLISR(upm::A110X)
265265
```

docs/max31855.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ First thing to do is to create a tree structure like this in upm/src/max31855:
1414

1515
* max31855.cxx
1616
* max31855.hpp
17-
* jsupm_max31855.i
18-
* pyupm_max31855.i
17+
* max31855.i
1918
* CMakeLists.txt
2019

2120
And then an example file to use & test our lib with in upm/examples/max31855.cxx.

scripts/build-android.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ cmake \
6666
make -j8 -Cbuild
6767

6868
# Anotate the .java src from doxygen
69-
find src/ -name "javaupm_*.i" > build/upm.i.list
69+
find src/ -name "*.i" > build/upm.i.list
7070
#../doxy/doxyport build/upm.i.list \
7171
# -s src/interfaces/,src/bacnetmstp,src/bmg160,src/bma250e,src/bmm150 \
7272
# -m doxy/samples.mapping.txt \

scripts/build-doc.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ mkdir -p ${BUILD_LOGS_DIR}
2222
cd ${ROOT_DIR} && make -j8 -Cbuild 2> ${BUILD_LOGS_DIR}/build-doc.log
2323

2424
# Make Java Documentation
25-
cd ${BUILD_DIR} && find ../src/ -name "javaupm_*.i" > upm.i.list && \
25+
cd ${BUILD_DIR} && find ../src/ -name "*.i" > upm.i.list && \
2626
../doxy/doxyport/doxyport upm.i.list \
2727
--cmake ./compile_commands.json \
28-
--source ../src/interfaces/,../src/bacnetmstp \
28+
--source ../src/interfaces/,../src/bacnetmstp,src \
2929
--destination src/ \
3030
--convert-protected-to-private \
3131
--output upm-java-files.txt \

src/CMakeLists.txt

Lines changed: 92 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -228,18 +228,63 @@ function (_get_target_dependency_interface_include_dirs target varIncludeDirs)
228228
set(${varIncludeDirs} "${${varIncludeDirs}}" PARENT_SCOPE)
229229
endfunction (_get_target_dependency_interface_include_dirs target varIncludeDirs)
230230

231+
# This function decides which swig interface file to use for the
232+
# CMAKE_CURRENT_SOURCE_DIR. If no .i file exists, create one.
233+
#
234+
# if ${filePrefix}_${libname}.i exists
235+
# return its full path
236+
# elif ${libname}.i exists
237+
# return its full path
238+
# else
239+
# create ${libname}.i via template and return its full path
240+
#
241+
function (_get_current_dot_i_file filePrefix varDotIFile)
242+
set(${varDotIFile} "${CMAKE_CURRENT_SOURCE_DIR}/${libname}.i")
243+
if (EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/${filePrefix}_${libname}.i")
244+
set(${varDotIFile} "${CMAKE_CURRENT_SOURCE_DIR}/${filePrefix}_${libname}.i")
245+
endif()
246+
# If a SWIG .i file DOES NOT exist, make a simple one from the template
247+
if (NOT EXISTS "${${varDotIFile}}")
248+
# This file will exist OUTSIDE the source tree
249+
set(${varDotIFile} "${CMAKE_CURRENT_BINARY_DIR}/${libname}.i")
250+
251+
# First, create the include variables
252+
set(SWIG_HASH_INCLUDES)
253+
foreach (_hpp ${module_hpp})
254+
set(SWIG_HASH_INCLUDES "#include \"${_hpp}\"")
255+
endforeach()
256+
string(REPLACE "#" "%" SWIG_PERCENT_INCLUDES "${SWIG_HASH_INCLUDES}")
257+
258+
# Write the interface file
259+
configure_file (${PROJECT_SOURCE_DIR}/src/swigme.i.in "${${varDotIFile}}" @ONLY)
260+
endif()
261+
262+
set(${varDotIFile} "${${varDotIFile}}" PARENT_SCOPE)
263+
endfunction()
264+
231265
# Create a single swig target for python
266+
# This is a macro since the function which calls it (upm_swig_python) depends
267+
# on values set inside this macro
232268
macro(_upm_swig_python)
233269
# Transitive headers don't appear to get picked up by swig_add_module call for
234270
# wrapper generation. Get them manually and add them here.
235271
_get_target_dependency_interface_include_dirs(${libname} DEPEND_DIRS)
236272

237-
include_directories (${CMAKE_CURRENT_SOURCE_DIR}/..
273+
include_directories (${CMAKE_CURRENT_SOURCE_DIR}
238274
${CMAKE_CURRENT_BINARY_DIR}/..
239275
${DEPEND_DIRS})
240276

241-
set_source_files_properties (pyupm_${libname}.i PROPERTIES CPLUSPLUS ON)
242-
set_source_files_properties (pyupm_${libname}.i PROPERTIES SWIG_FLAGS "-I${CMAKE_CURRENT_BINARY_DIR}/..")
277+
# Decide between ${libname}.i or a language-specific .i
278+
_get_current_dot_i_file(pyupm SWIG_CURRENT_DOT_I_FILE)
279+
# If this module is using ${libname}.i, provide a module name for UseSWIG AND SWIG_FLAGS
280+
if (${SWIG_CURRENT_DOT_I_FILE} MATCHES ${libname}.i)
281+
set_source_files_properties(${SWIG_CURRENT_DOT_I_FILE} PROPERTIES
282+
SWIG_MODULE_NAME pyupm_${libname}
283+
SWIG_FLAGS ";-module;pyupm_${libname};")
284+
endif()
285+
286+
# Tell UseSWIG that this is CXX
287+
set_property(SOURCE ${SWIG_CURRENT_DOT_I_FILE} PROPERTY CPLUSPLUS ON)
243288

244289
# Set current python binary dir
245290
set (CMAKE_CURRENT_PYTHON_BINARY_DIR
@@ -255,9 +300,9 @@ macro(_upm_swig_python)
255300
# Swig module name (example: pyupm_a110x-python2)
256301
set (python_wrapper_name pyupm_${libname}-python${PYTHON_VERSION_MAJOR})
257302
if (CMAKE_VERSION VERSION_LESS "3.8")
258-
swig_add_module (${python_wrapper_name} python pyupm_${libname}.i)
303+
swig_add_module (${python_wrapper_name} python ${SWIG_CURRENT_DOT_I_FILE})
259304
else ()
260-
swig_add_library (${python_wrapper_name} LANGUAGE python SOURCES pyupm_${libname}.i)
305+
swig_add_library (${python_wrapper_name} LANGUAGE python SOURCES ${SWIG_CURRENT_DOT_I_FILE})
261306
endif ()
262307
# Get target library name (example _pyupm_a110x-python2)
263308
set (python_wrapper_target ${SWIG_MODULE_${python_wrapper_name}_REAL_NAME})
@@ -361,16 +406,29 @@ function(upm_swig_node)
361406
# wrapper generation. Get them manually and add them here.
362407
_get_target_dependency_interface_include_dirs(${libname} DEPEND_DIRS)
363408

364-
include_directories (${CMAKE_CURRENT_SOURCE_DIR}/..
409+
include_directories (${CMAKE_CURRENT_SOURCE_DIR}
365410
${CMAKE_CURRENT_BINARY_DIR}/..
366411
${DEPEND_DIRS})
367412

368-
set_property (SOURCE jsupm_${libname}.i PROPERTY SWIG_FLAGS "-node" "-DV8_VERSION=${V8_VERSION_HEX}")
369-
set_source_files_properties (jsupm_${libname}.i PROPERTIES CPLUSPLUS ON)
413+
# Decide between ${libname}.i or a language-specific .i
414+
_get_current_dot_i_file(jsupm SWIG_CURRENT_DOT_I_FILE)
415+
# If this module is using ${libname}.i, provide a module name for UseSWIG AND SWIG_FLAGS
416+
if (${SWIG_CURRENT_DOT_I_FILE} MATCHES ${libname}.i)
417+
set_source_files_properties(${SWIG_CURRENT_DOT_I_FILE} PROPERTIES
418+
SWIG_MODULE_NAME jsupm_${libname}
419+
SWIG_FLAGS ";-module;jsupm_${libname};")
420+
endif()
421+
422+
# Tell UseSWIG that this is CXX
423+
set_property(SOURCE ${SWIG_CURRENT_DOT_I_FILE} PROPERTY CPLUSPLUS ON)
424+
# Append additional flags for NodeJS
425+
set_property(SOURCE ${SWIG_CURRENT_DOT_I_FILE} APPEND PROPERTY
426+
SWIG_FLAGS ";-node;-DV8_VERSION=${V8_VERSION_HEX}")
427+
370428
if (CMAKE_VERSION VERSION_LESS "3.8")
371-
swig_add_module (jsupm_${libname} javascript jsupm_${libname}.i)
429+
swig_add_module (jsupm_${libname} javascript ${SWIG_CURRENT_DOT_I_FILE})
372430
else ()
373-
swig_add_library (jsupm_${libname} LANGUAGE javascript SOURCES jsupm_${libname}.i)
431+
swig_add_library (jsupm_${libname} LANGUAGE javascript SOURCES ${SWIG_CURRENT_DOT_I_FILE})
374432
endif ()
375433
add_dependencies(jsupm_${libname} ${libname})
376434
swig_link_libraries (jsupm_${libname} ${NODE_LIBRARIES} ${libname})
@@ -427,20 +485,33 @@ function(upm_swig_java)
427485
# wrapper generation. Get them manually and add them here.
428486
_get_target_dependency_interface_include_dirs(${libname} DEPEND_DIRS)
429487

430-
include_directories (
431-
${JAVA_INCLUDE_PATH}
432-
${JAVA_INCLUDE_PATH2}
433-
${CMAKE_CURRENT_BINARY_DIR}/..
434-
${CMAKE_CURRENT_BINARY_DIR}/..
435-
${DEPEND_DIRS}
436-
)
488+
include_directories (${JAVA_INCLUDE_PATH}
489+
${JAVA_INCLUDE_PATH2}
490+
${CMAKE_CURRENT_SOURCE_DIR}
491+
${CMAKE_CURRENT_BINARY_DIR}/..
492+
${DEPEND_DIRS})
493+
494+
# Decide between ${libname}.i or a language-specific .i
495+
_get_current_dot_i_file(javaupm SWIG_CURRENT_DOT_I_FILE)
496+
# If this module is using ${libname}.i, provide a module name for UseSWIG AND SWIG_FLAGS
497+
if (${SWIG_CURRENT_DOT_I_FILE} MATCHES ${libname}.i)
498+
set_source_files_properties(${SWIG_CURRENT_DOT_I_FILE} PROPERTIES
499+
SWIG_MODULE_NAME javaupm_${libname}
500+
SWIG_FLAGS ";-module;javaupm_${libname};")
501+
endif()
502+
503+
# Tell UseSWIG that this is CXX
504+
set_property(SOURCE ${SWIG_CURRENT_DOT_I_FILE} PROPERTY CPLUSPLUS ON)
505+
506+
# Set a -package as well (which is slightly different than the module name,
507+
# module: javaupm_a110x vs package: upm_a110x
508+
set_property(SOURCE ${SWIG_CURRENT_DOT_I_FILE} APPEND PROPERTY
509+
SWIG_FLAGS ";-package;upm_${libname}")
437510

438-
set_source_files_properties (javaupm_${libname}.i PROPERTIES CPLUSPLUS ON)
439-
set_source_files_properties (javaupm_${libname}.i PROPERTIES SWIG_FLAGS ";-package;upm_${libname};-I${CMAKE_BINARY_DIR}/src")
440511
if (CMAKE_VERSION VERSION_LESS "3.8")
441-
swig_add_module (javaupm_${libname} java javaupm_${libname}.i)
512+
swig_add_module (javaupm_${libname} java ${SWIG_CURRENT_DOT_I_FILE})
442513
else ()
443-
swig_add_library (javaupm_${libname} LANGUAGE java SOURCES javaupm_${libname}.i)
514+
swig_add_library (javaupm_${libname} LANGUAGE java SOURCES ${SWIG_CURRENT_DOT_I_FILE})
444515
endif ()
445516

446517
# If the C++ target depends on C++ interfaces, make the JAVA target

src/_upm.i

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* This file provides additional non-wrapper code functionality for use
33
* throughout the UPM repository. This file is intended to be %imported
4-
* from the top-level upm.i (not directly by UPM libraries).
4+
* from the top-level common_top.i (not directly by UPM libraries).
55
*
66
* No wrapper code will be generated from importing this .i file.
77
*/

src/a110x/a110x.i

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
%include "../common_top.i"
2+
3+
/* BEGIN Java syntax ------------------------------------------------------- */
4+
#ifdef SWIGJAVA
5+
/* Macro for loading javaupm_a110x */
6+
JAVA_JNI_LOADLIBRARY(javaupm_a110x)
7+
8+
/* Macro adding an installisr call w/JAVA runnable to A110X */
9+
JAVA_ADD_INSTALLISR(upm::A110X)
10+
#endif
11+
/* END Java syntax */
12+
13+
/* BEGIN Common SWIG syntax ------------------------------------------------- */
14+
%{
15+
#include "a110x.hpp"
16+
%}
17+
%include "a110x.hpp"
18+
/* END Common SWIG syntax */

src/a110x/common.i

Lines changed: 0 additions & 6 deletions
This file was deleted.

src/a110x/javaupm_a110x.i

Lines changed: 0 additions & 11 deletions
This file was deleted.

src/a110x/jsupm_a110x.i

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)