diff options
author | Thirumal Venkat <me@thirumal.in> | 2018-12-29 16:45:26 (GMT) |
---|---|---|
committer | Thirumal Venkat <me@thirumal.in> | 2019-01-03 04:06:15 (GMT) |
commit | 9816748847fba47bb0633b49cff1432fd677897e (patch) | |
tree | 1ba36f77e79827041b0b80a7572d4b5f4d8138a9 /Tests/UseSWIG/SwigSrcFileExtension | |
parent | efdc19c58b64cf2fece50cc241a186972e5afa19 (diff) | |
download | CMake-9816748847fba47bb0633b49cff1432fd677897e.zip CMake-9816748847fba47bb0633b49cff1432fd677897e.tar.gz CMake-9816748847fba47bb0633b49cff1432fd677897e.tar.bz2 |
SWIG: Add support for custom Swig source file extensions
Diffstat (limited to 'Tests/UseSWIG/SwigSrcFileExtension')
-rw-r--r-- | Tests/UseSWIG/SwigSrcFileExtension/CMakeLists.txt | 28 | ||||
-rw-r--r-- | Tests/UseSWIG/SwigSrcFileExtension/my_add.i | 9 | ||||
-rw-r--r-- | Tests/UseSWIG/SwigSrcFileExtension/my_sub.swg | 9 | ||||
-rwxr-xr-x | Tests/UseSWIG/SwigSrcFileExtension/runme.py | 24 |
4 files changed, 70 insertions, 0 deletions
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) |