user
user

Reputation: 5390

CMake treat file extension as if it were another file extension

How do I convince CMake that the file extension .swig is equivalent to .i?

How would I do this for other file extensions?

Context

I am using CMake (version 2.8, minimum 2.6) with Swig. If I set my swig interface file to having the .i extension, everything automagically works (I end up with a working .dll 'module' loadable from my target language interpreter tclsh). If I give it the .swig extension, CMake doesn't know what to do with it. I'd like to use one extension over another to abate syntax-highlighting-hell in my editor.

Existing CMake file

At the request of a commenter, here's mah' file:

find_package(swig REQUIRED)
include(${SWIG_USE_FILE})

find_package(tcl REQUIRED)
include_directories(${TCL_INCLUDE_PATH})

set(CMAKE_SWIG_FLAGS -prefix thoughtjack -namespace)

link_directories(${OEEG_BINARY_DIR}/oeeg)

set(swig_interface_files native.i)
set_source_files_properties(${swig_interface_files} PROPERTIES CPLUSPLUS ON)
file(GLOB_RECURSE native_source_files *.cpp *.c)

swig_add_module(thoughtjack_native tcl ${swig_interface_files} ${native_source_files})
swig_link_libraries(thoughtjack_native ${TCL_LIBRARY} oeeg)

file(GLOB script_files ${CMAKE_CURRENT_SOURCE_DIR}/*.tcl)
install(FILES ${script_files} DESTINATION opt/thoughtjack)
install(TARGETS thoughtjack_native DESTINATION opt/thoughtjack)

add_custom_target(
  thoughtjack
  DEPENDS thoughtjack_native
  SOURCES ${script_files}
)

The custom_xyz bit is there with the 'sources' bit and other decorations to tell CMake to make those available through the MSVS solution file. As stated, simply renaming the file from .swig to .i (as it's shown here) gives me a working, loadable, Tcl module that exports all the functionality I expect it to as per the native.(swig|i) file.

Upvotes: 3

Views: 690

Answers (1)

Guillaume
Guillaume

Reputation: 10971

After looking at the source code of UseSwig module, it's hard coded to only use filenames that matches .i, so you can't make it use something else.

Now, CMake can help you by adding a rule to copy .swig files to .i when it's needed:

set(swig_dot_swig_files your.swig files.swig)

foreach(item IN LISTS swig_swig_files)
    get_filename_component(itembase ${item} NAME_WE)
    add_custom_command(
        OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${itembase}.i"
        COMMAND ${CMAKE_COMMAND} -E copy "${CMAKE_CURRENT_SOURCE_DIR}/${item}" "${CMAKE_CURRENT_BINARY_DIR}/${itembase}.i"
        DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/${item}"
    )
endforeach()

Then you can provide those files to swig_add_module

set(swig_dot_i_files your.i files.i)
swig_add_module(thoughtjack_native tcl ${swig_dot_i_files})

This solution has some good properties: for instance, a modification of the .swig will make the target rebuilt as there's a dependency from .i files to .swig files.

Upvotes: 5

Related Questions