summaryrefslogtreecommitdiffstats
path: root/Tests/UseSWIG
diff options
context:
space:
mode:
authorThirumal Venkat <me@thirumal.in>2018-12-29 16:45:26 (GMT)
committerThirumal Venkat <me@thirumal.in>2019-01-03 04:06:15 (GMT)
commit9816748847fba47bb0633b49cff1432fd677897e (patch)
tree1ba36f77e79827041b0b80a7572d4b5f4d8138a9 /Tests/UseSWIG
parentefdc19c58b64cf2fece50cc241a186972e5afa19 (diff)
downloadCMake-9816748847fba47bb0633b49cff1432fd677897e.zip
CMake-9816748847fba47bb0633b49cff1432fd677897e.tar.gz
CMake-9816748847fba47bb0633b49cff1432fd677897e.tar.bz2
SWIG: Add support for custom Swig source file extensions
Diffstat (limited to 'Tests/UseSWIG')
-rw-r--r--Tests/UseSWIG/CMakeLists.txt12
-rw-r--r--Tests/UseSWIG/SwigSrcFileExtension/CMakeLists.txt28
-rw-r--r--Tests/UseSWIG/SwigSrcFileExtension/my_add.i9
-rw-r--r--Tests/UseSWIG/SwigSrcFileExtension/my_sub.swg9
-rwxr-xr-xTests/UseSWIG/SwigSrcFileExtension/runme.py24
5 files changed, 82 insertions, 0 deletions
diff --git a/Tests/UseSWIG/CMakeLists.txt b/Tests/UseSWIG/CMakeLists.txt
index f79cda6..434895e 100644
--- a/Tests/UseSWIG/CMakeLists.txt
+++ b/Tests/UseSWIG/CMakeLists.txt
@@ -111,3 +111,15 @@ add_test(NAME UseSWIG.ModuleName COMMAND
--build-options ${build_options}
--test-command ${CMAKE_CTEST_COMMAND} -V -C $<CONFIGURATION>
)
+
+
+add_test(NAME UseSWIG.SwigSrcFileExtension COMMAND
+ ${CMAKE_CTEST_COMMAND} -C $<CONFIGURATION>
+ --build-and-test
+ "${CMake_SOURCE_DIR}/Tests/UseSWIG/SwigSrcFileExtension"
+ "${CMake_BINARY_DIR}/Tests/UseSWIG/SwigSrcFileExtension"
+ ${build_generator_args}
+ --build-project SwigSrcFileExtension
+ --build-options ${build_options}
+ --test-command ${CMAKE_CTEST_COMMAND} -V -C $<CONFIGURATION>
+ )
diff --git a/Tests/UseSWIG/SwigSrcFileExtension/CMakeLists.txt b/Tests/UseSWIG/SwigSrcFileExtension/CMakeLists.txt
new file mode 100644
index 0000000..7eb73d4
--- /dev/null
+++ b/Tests/UseSWIG/SwigSrcFileExtension/CMakeLists.txt
@@ -0,0 +1,28 @@
+cmake_minimum_required(VERSION 3.1...3.14)
+
+project(SwigSrcFileExtension C)
+
+include(CTest)
+find_package(SWIG REQUIRED)
+find_package(Python COMPONENTS Interpreter Development REQUIRED)
+
+include(${SWIG_USE_FILE})
+
+# Use the newer target name preference
+set(UseSWIG_TARGET_NAME_PREFERENCE "STANDARD")
+
+# Set the custom source file extension to both .i and .swg
+set(SWIG_SOURCE_FILE_EXTENSIONS ".i" ".swg")
+
+# Generate a Python module out of `.i`
+swig_add_library(my_add LANGUAGE python SOURCES my_add.i)
+target_link_libraries(my_add Python::Python)
+
+# Generate a Python module out of `.swg`
+swig_add_library(my_sub LANGUAGE python SOURCES my_sub.swg)
+target_link_libraries(my_sub Python::Python)
+
+# Add a test
+add_test(NAME SwigSrcFileExtension
+ COMMAND "${CMAKE_COMMAND}" -E env "PYTHONPATH=${CMAKE_CURRENT_BINARY_DIR}"
+ "${Python_EXECUTABLE}" "${CMAKE_CURRENT_SOURCE_DIR}/runme.py")
diff --git a/Tests/UseSWIG/SwigSrcFileExtension/my_add.i b/Tests/UseSWIG/SwigSrcFileExtension/my_add.i
new file mode 100644
index 0000000..d087ab5
--- /dev/null
+++ b/Tests/UseSWIG/SwigSrcFileExtension/my_add.i
@@ -0,0 +1,9 @@
+%module my_add
+
+%{
+int add(int a, int b) {
+ return a + b;
+}
+%}
+
+int add(int a, int b);
diff --git a/Tests/UseSWIG/SwigSrcFileExtension/my_sub.swg b/Tests/UseSWIG/SwigSrcFileExtension/my_sub.swg
new file mode 100644
index 0000000..df34b44
--- /dev/null
+++ b/Tests/UseSWIG/SwigSrcFileExtension/my_sub.swg
@@ -0,0 +1,9 @@
+%module my_sub
+
+%{
+int sub(int a, int b) {
+ return a - b;
+}
+%}
+
+int sub(int a, int b);
diff --git a/Tests/UseSWIG/SwigSrcFileExtension/runme.py b/Tests/UseSWIG/SwigSrcFileExtension/runme.py
new file mode 100755
index 0000000..290175b
--- /dev/null
+++ b/Tests/UseSWIG/SwigSrcFileExtension/runme.py
@@ -0,0 +1,24 @@
+#!/usr/bin/env python
+
+from __future__ import print_function
+import random
+
+import my_add
+import my_sub
+
+
+# These can be changed, but make sure not to overflow `int`
+a = random.randint(1, 1024)
+b = random.randint(1, 1024)
+
+if my_add.add(a, b) == a + b:
+ print ("Test 1 Passed for SWIG custom source file extension")
+else:
+ print ("Test 1 FAILED for SWIG custom source file extension")
+ exit(1)
+
+if my_sub.sub(a, b) == a - b:
+ print ("Test 2 Passed for SWIG custom source file extension")
+else:
+ print ("Test 2 FAILED for SWIG custom source file extension")
+ exit(1)