From 9816748847fba47bb0633b49cff1432fd677897e Mon Sep 17 00:00:00 2001 From: Thirumal Venkat Date: Sat, 29 Dec 2018 22:15:26 +0530 Subject: SWIG: Add support for custom Swig source file extensions --- Help/release/dev/UseSWIG-source-file-ext.rst | 5 ++++ Modules/UseSWIG.cmake | 26 +++++++++++++++++++-- Tests/UseSWIG/CMakeLists.txt | 12 ++++++++++ Tests/UseSWIG/SwigSrcFileExtension/CMakeLists.txt | 28 +++++++++++++++++++++++ Tests/UseSWIG/SwigSrcFileExtension/my_add.i | 9 ++++++++ Tests/UseSWIG/SwigSrcFileExtension/my_sub.swg | 9 ++++++++ Tests/UseSWIG/SwigSrcFileExtension/runme.py | 24 +++++++++++++++++++ 7 files changed, 111 insertions(+), 2 deletions(-) create mode 100644 Help/release/dev/UseSWIG-source-file-ext.rst create mode 100644 Tests/UseSWIG/SwigSrcFileExtension/CMakeLists.txt create mode 100644 Tests/UseSWIG/SwigSrcFileExtension/my_add.i create mode 100644 Tests/UseSWIG/SwigSrcFileExtension/my_sub.swg create mode 100755 Tests/UseSWIG/SwigSrcFileExtension/runme.py diff --git a/Help/release/dev/UseSWIG-source-file-ext.rst b/Help/release/dev/UseSWIG-source-file-ext.rst new file mode 100644 index 0000000..5d11dc6 --- /dev/null +++ b/Help/release/dev/UseSWIG-source-file-ext.rst @@ -0,0 +1,5 @@ +UseSWIG-source-file-ext +----------------------- + +* The :module:`UseSWIG` module gains capability to specify + ``SWIG`` source file extensions. diff --git a/Modules/UseSWIG.cmake b/Modules/UseSWIG.cmake index a3d6d9e..18ea55c 100644 --- a/Modules/UseSWIG.cmake +++ b/Modules/UseSWIG.cmake @@ -75,7 +75,8 @@ Defines the following command for use with ``SWIG``: ``SOURCES`` List of sources for the library. Files with extension ``.i`` will be identified as sources for the ``SWIG`` tool. Other files will be handled in - the standard way. + the standard way. This behavior can be overriden by specifying the variable + ``SWIG_SOURCE_FILE_EXTENSIONS``. .. note:: @@ -222,6 +223,15 @@ as well as ``SWIG``: ``SWIG_MODULE__EXTRA_DEPS`` Specify extra dependencies for the generated module for ````. + +``SWIG_SOURCE_FILE_EXTENSIONS`` + Specify a list of source file extensions to override the default + behavior of considering only ``.i`` files as sources for the ``SWIG`` + tool. For example: + + .. code-block:: cmake + + set(SWIG_SOURCE_FILE_EXTENSIONS ".i" ".swg") #]=======================================================================] cmake_policy(GET CMP0078 target_name_policy) @@ -659,8 +669,20 @@ function(SWIG_ADD_LIBRARY name) set(CMAKE_SWIG_OUTDIR "${outputdir}") set(SWIG_OUTFILE_DIR "${outfiledir}") + # See if the user has specified source extensions for swig files? + if (NOT DEFINED SWIG_SOURCE_FILE_EXTENSIONS) + # Assume the default (*.i) file extension for Swig source files + set(SWIG_SOURCE_FILE_EXTENSIONS ".i") + endif() + + # Generate a regex out of file extensions. + string(REGEX REPLACE "([$^.*+?|()-])" "\\\\\\1" swig_source_ext_regex "${SWIG_SOURCE_FILE_EXTENSIONS}") + list (JOIN swig_source_ext_regex "|" swig_source_ext_regex) + string (PREPEND swig_source_ext_regex "(") + string (APPEND swig_source_ext_regex ")$") + set(swig_dot_i_sources ${_SAM_SOURCES}) - list(FILTER swig_dot_i_sources INCLUDE REGEX "\\.i$") + list(FILTER swig_dot_i_sources INCLUDE REGEX ${swig_source_ext_regex}) if (NOT swig_dot_i_sources) message(FATAL_ERROR "SWIG_ADD_LIBRARY: no SWIG interface files specified") endif() 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 $ ) + + +add_test(NAME UseSWIG.SwigSrcFileExtension COMMAND + ${CMAKE_CTEST_COMMAND} -C $ + --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 $ + ) 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) -- cgit v0.12