Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby and SWIG with CMake

Tags:

ruby

cmake

swig

Has anyone had luck using CMake to create Ruby bindings via SWIG? I have a working example for creating Python bindings via SWIG in my CMake file, but when I use the same approach to create a Ruby binding the actual Ruby file doesn't get created. With the working Python binding, a Python file does get created.

Here's the relevant portions of my CMakeLists.txt file:

if (${SWIG_FOUND})
  find_package( Ruby REQUIRED )

  include_directories(
    ${RUBY_INCLUDE_DIRS}
    )

  include (${SWIG_USE_FILE})

  set (CMAKE_SWIG_FLAGS "") # set the global SWIG flags to empty
  set_source_files_properties (TESTSWIG.i PROPERTIES CPLUSPLUS ON) # TESTSWIG.i is c++

  SWIG_ADD_MODULE (test-ruby ruby TESTSWIG.i src/Test.cpp)
  SWIG_LINK_LIBRARIES (test-ruby test ${RUBY_LIBRARY})

  set(swig_SOURCES
    ${CMAKE_CURRENT_BINARY_DIR}/libtest-ruby.so
    )

  install(FILES ${swig_SOURCES}
    DESTINATION lib/ruby
    )
endif(${SWIG_FOUND})

Anyone had luck creating Ruby bindings via SWIG using CMake?!

like image 313
Bryan Avatar asked May 21 '26 15:05

Bryan


1 Answers

We use it. You can give a look to these examples:

http://svn.opensuse.org/svn/yast/trunk/libyui-bindings/

However, we don't use the SWIG macros (except for finding SWIG itself), but we use ADD_CUSTOM_COMMAND:

SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-strict-aliasing")

EXECUTE_PROCESS(COMMAND ${RUBY_EXECUTABLE} -r rbconfig -e "print Config::CONFIG['vendorarchdir']" OUTPUT_VARIABLE RUBY_VENDOR_ARCH_DIR)

MESSAGE(STATUS "Ruby executable: ${RUBY_EXECUTABLE}")
MESSAGE(STATUS "Ruby vendor arch dir: ${RUBY_VENDOR_ARCH_DIR}")
MESSAGE(STATUS "Ruby include path: ${RUBY_INCLUDE_PATH}")

SET( SWIG_OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/yui_ruby.cxx" )

ADD_CUSTOM_COMMAND (
   OUTPUT  ${SWIG_OUTPUT}
   COMMAND ${CMAKE_COMMAND} -E echo_append "Creating wrapper code for ruby..."
   COMMAND ${SWIG_EXECUTABLE} -c++ -ruby -autorename -o ${SWIG_OUTPUT} -I${LIBYUI_INCLUDE_DIR} ${SWIG_INPUT}
   COMMAND ${CMAKE_COMMAND} -E echo "Done."
   WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
   DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/../*.i
)

ADD_LIBRARY( yui_ruby SHARED ${SWIG_OUTPUT} )
INCLUDE_DIRECTORIES( ${RUBY_INCLUDE_PATH} ${LIBYUI_INCLUDE_DIR} )

SET_TARGET_PROPERTIES( yui_ruby PROPERTIES PREFIX "" OUTPUT_NAME "yui")

TARGET_LINK_LIBRARIES( yui_ruby ${LIBYUI_LIBRARY} )
TARGET_LINK_LIBRARIES( yui_ruby ${RUBY_LIBRARY} )

INSTALL(TARGETS yui_ruby LIBRARY DESTINATION ${RUBY_VENDOR_ARCH_DIR})

I assume your version is not working because the "lib" prefix in the .so file. This is exactly the reason why we do

SET_TARGET_PROPERTIES( yui_ruby PROPERTIES PREFIX "" OUTPUT_NAME "yui")

In order to get yui.so instead of libyui.so.

Also make sure the path where you are installing the .so file is in the ruby load path.

like image 92
duncan Avatar answered May 23 '26 08:05

duncan