Swig Ruby documentation mods. 
Author Message
 Swig Ruby documentation mods.

I have been trying to use Swig Ruby recently, and in attempting to
understand it I have made some mods to the documentation, because others
will also want to use it.  I have put the modifications at
http://www.*-*-*.com/ ~hgs/ruby/Swig/index.html
Can anyone else using Swig with Ruby tell me if this is about right?
I have basically taken the stuff from the examples directory and aslo
knowledge from reading README.EXT, and put them together...
        Hugh


Sat, 03 Jul 2004 20:18:54 GMT  
 Swig Ruby documentation mods.

Quote:
> I have been trying to use Swig Ruby recently, and in attempting to
> understand it I have made some mods to the documentation, because others
> will also want to use it.  I have put the modifications at
> http://www.eng.cse.dmu.ac.uk/~hgs/ruby/Swig/index.html
> Can anyone else using Swig with Ruby tell me if this is about right?
> I have basically taken the stuff from the examples directory and aslo
> knowledge from reading README.EXT, and put them together...
>    Hugh

Have you thought about sending to the swig project?

--
Alan Chen
Digikata LLC
http://digikata.com



Sun, 04 Jul 2004 03:26:30 GMT  
 Swig Ruby documentation mods.


Quote:
> I have been trying to use Swig Ruby recently, and in attempting to
> understand it I have made some mods to the documentation, because others
> will also want to use it.  I have put the modifications at
> http://www.eng.cse.dmu.ac.uk/~hgs/ruby/Swig/index.html
> Can anyone else using Swig with Ruby tell me if this is about right?
> I have basically taken the stuff from the examples directory and aslo
> knowledge from reading README.EXT, and put them together...
> Hugh

I use Ruby SWIG quite a bit, but have not built anything in the manner you
describe using mkmf, so I can't comment on that.  Does the mkmf approach
work for C++ as well?

I do know that the page you modified is very new.  I believe Lyle Johnson
has just started on it.



Sun, 04 Jul 2004 10:57:35 GMT  
 Swig Ruby documentation mods.

Quote:


> > I have been trying to use Swig Ruby recently, and in attempting to
> > understand it I have made some mods to the documentation, because others
> > will also want to use it.  I have put the modifications at
> > http://www.eng.cse.dmu.ac.uk/~hgs/ruby/Swig/index.html
> > Can anyone else using Swig with Ruby tell me if this is about right?
> > I have basically taken the stuff from the examples directory and aslo
> > knowledge from reading README.EXT, and put them together...
> >       Hugh

> Have you thought about sending to the swig project?

Of course.  No feedback so far from swig-dev on this. But I need
feedback from the Ruby side of this, as clearly there are few Rubyists
on the Swig-dev list.  Also I have found a few more "niggles":

* if you don't use -module in the swig -ruby command then swig
  complains.  This is probably always correct, but needs mentioning.
* It is not clear what should be done about integrating several C
  functions into the same module.  Does this depend on one writing a .i
  file to connect them together?  How?
* Can you make swig roll the C functions into methods of a class, or
  must one do this via including a module.
* Can extconf.rb be made to make other preliminary stuff, that is not
  part of the external interface of the class?  Eg, can one make it run
  swig if necessary?

Quote:

> --
> Alan Chen
> Digikata LLC
> http://digikata.com

        Hugh


Sun, 04 Jul 2004 18:01:10 GMT  
 Swig Ruby documentation mods.

Quote:
> > Have you thought about sending to the swig project?

>Of course.  No feedback so far from swig-dev on this.

Well, Dave's feedback is here, I think:
http://mailman.cs.uchicago.edu/pipermail/swig-dev/2002-January/016502...
However, it might be that he sent the mail to the list and not to you
personally, so that you might have missed it.

As for you questions:

Quote:
>* if you don't use -module in the swig -ruby command then swig
>   complains.  This is probably always correct, but needs mentioning.

Are you sure you are including the %module directive in your .i file?
I'm not using -module and SWIG doesn't complain.

Quote:
>* It is not clear what should be done about integrating several C
>   functions into the same module.  Does this depend on one writing a .i
>   file to connect them together?  How?

What I usually do is:
- somefuncs.i contains the declarations for some functions
- someotherfuncs.i contains the declarations for some other functions
- I write a file mymodule.i containing:

%module MyModule
%include somefuncs.i
%include someotherfuncs.i

- by running SWIG on the latter file, all functions from the two original
ones will be exported in MyModule.

Quote:
>* Can you make swig roll the C functions into methods of a class, or
>   must one do this via including a module.

If the functions all refer to a struct, you can do it in the usual SWIG way
(documented for other languages, so you can probably save yourself some
work by referring to the relevant places):
let's say you have a struct SomeStruct and a bunch of functions:

struct SomeStruct {
     // whatever

Quote:
};

void f(SomeStruct*);
int g(SomeStruct*);
double h(SomeStruct*, int i);

You can write in the interface file:

%module MyModule

struct SomeStruct {
     // whatever

Quote:
};

%addmethods SomeStruct {
     void f() {
         f(self);
     }
     int g() {
         return g(self);
     }
     double h(int i) {
         return h(self,i);
     }

Quote:
}

After creating the extension you'll be able to use the struct as:

s = MyModule::SomeStruct.new
s.f()
i = f.g()
x = f.h(2)

If the functions are unrelated, You can declare an empty struct and do the
same thing, but you'll end up with instance methods, not static methods. If
the latter is what you want, you might be better off doing it on the Ruby
side. Finally, if you are not forced to use C only and can use C++, you can
create a class whose methods proxy the C functions and wrap such class with
SWIG.

Quote:
>* Can extconf.rb be made to make other preliminary stuff, that is not
>   part of the external interface of the class?  Eg, can one make it run
>   swig if necessary?

The roughest way would be to make a system call before creating the
makefile, i.e., write extconf.rb as:

require 'mkmf'
system('swig -ruby MyModule.i')
create_makefile('MyModule')

If you want to get fancy, you can compare the modification times of
MyModule.i and mymodule_wrap.cpp and run swig only when needed, as in:

require 'mkmf'
require 'ftools'
system('swig -ruby MyModule.i') unless File.exists? "mymodule_wrap.cpp" and
File.mtime("mymodule_wrap.cpp") > File.mtime("MyModule.i")
create_makefile('MyModule')

I didn't actually run the above code, so it might need corrections.
However, you get the drift.

Hope this helped,
                         Luigi



Sun, 04 Jul 2004 18:54:17 GMT  
 Swig Ruby documentation mods.

Quote:


> > > Have you thought about sending to the swig project?

> >Of course.  No feedback so far from swig-dev on this.

> Well, Dave's feedback is here, I think:
> http://mailman.cs.uchicago.edu/pipermail/swig-dev/2002-January/016502...

This was since then. I have joined the list, then started on the mods.
http://mailman.cs.uchicago.edu/pipermail/swig-dev/2002-January/016586...

Quote:
> However, it might be that he sent the mail to the list and not to you
> personally, so that you might have missed it.

> As for you questions:

> >* if you don't use -module in the swig -ruby command then swig
> >   complains.  This is probably always correct, but needs mentioning.

> Are you sure you are including the %module directive in your .i file?
> I'm not using -module and SWIG doesn't complain.

I'm not using a .i file -- I'm using the fact that swig -ruby example.c
will generally work, as described elsewhere in the docs.

Quote:

> >* It is not clear what should be done about integrating several C
> >   functions into the same module.  Does this depend on one writing a .i
> >   file to connect them together?  How?

> What I usually do is:
> - somefuncs.i contains the declarations for some functions

OK,

Quote:
> - someotherfuncs.i contains the declarations for some other functions
> - I write a file mymodule.i containing:

> %module MyModule
> %include somefuncs.i
> %include someotherfuncs.i

> - by running SWIG on the latter file, all functions from the two original
> ones will be exported in MyModule.

Will it produce one "%s_wrap.c" or two?

Quote:

> >* Can you make swig roll the C functions into methods of a class, or
> >   must one do this via including a module.

> If the functions all refer to a struct, you can do it in the usual SWIG way
> (documented for other languages, so you can probably save yourself some
> work by referring to the relevant places):
        [...]
> After creating the extension you'll be able to use the struct as:

> s = MyModule::SomeStruct.new
> s.f()
> i = f.g()
> x = f.h(2)

OK, it always will come out as a module.  I was thinking it could look
like one had required a file just containing a class definition.

Quote:

> If the functions are unrelated, You can declare an empty struct and do the
> same thing, but you'll end up with instance methods, not static methods. If
> the latter is what you want, you might be better off doing it on the Ruby
> side. Finally, if you are not forced to use C only and can use C++, you can
> create a class whose methods proxy the C functions and wrap such class with
> SWIG.

I think I'll leave that can of worms for another day, but thank you.

Quote:

> >* Can extconf.rb be made to make other preliminary stuff, that is not
> >   part of the external interface of the class?  Eg, can one make it run
> >   swig if necessary?

> The roughest way would be to make a system call before creating the
> makefile, i.e., write extconf.rb as:

> require 'mkmf'
> system('swig -ruby MyModule.i')
> create_makefile('MyModule')

That would build it, but it would build it always -- I wanted to get

target: dependencies
        action

stuff in there.  I'll take that as a No for now.

Quote:

> If you want to get fancy, you can compare the modification times of
> MyModule.i and mymodule_wrap.cpp and run swig only when needed, as in:

> require 'mkmf'
> require 'ftools'
> system('swig -ruby MyModule.i') unless File.exists? "mymodule_wrap.cpp" and
> File.mtime("mymodule_wrap.cpp") > File.mtime("MyModule.i")
> create_makefile('MyModule')

> I didn't actually run the above code, so it might need corrections.
> However, you get the drift.

Yes, that sort of thing would work, but since extconf is supposed to be
used to write a makefile, it seems a bit much to re-implement make
inside it! :-)
Quote:

> Hope this helped,
>                          Luigi



Sun, 04 Jul 2004 19:15:31 GMT  
 Swig Ruby documentation mods.

Quote:

> > >* if you don't use -module in the swig -ruby command then swig
> > >   complains.  This is probably always correct, but needs mentioning.

> > Are you sure you are including the %module directive in your .i file?
> > I'm not using -module and SWIG doesn't complain.

>I'm not using a .i file -- I'm using the fact that swig -ruby example.c
>will generally work, as described elsewhere in the docs.

Oh, I see. Then I agree: SWIG is correct in complaining, but this needs
mentioning.

Quote:
> > >* It is not clear what should be done about integrating several C
> > >   functions into the same module.  Does this depend on one writing a .i
> > >   file to connect them together?  How?

> > - I write a file mymodule.i containing:

> > %module MyModule
> > %include somefuncs.i
> > %include someotherfuncs.i

> > - by running SWIG on the latter file, all functions from the two original
> > ones will be exported in MyModule.

>Will it produce one "%s_wrap.c" or two?

Just one.

Quote:
> > If you want to get fancy, you can compare the modification times of
> > MyModule.i and mymodule_wrap.cpp and run swig only when needed

>Yes, that sort of thing would work, but since extconf is supposed to be
>used to write a makefile, it seems a bit much to re-implement make
>inside it! :-)

I see your point.
Ok, how about:

require 'mkmf'
require 'ftools'

# create the wrapper file if it doesn't exist yet (i.e., first extconf run)
# since it is needed in order to create a correct makefile
system('swig -ruby MyModule.i') unless File.exists? "mymodule_wrap.cpp"

# create the makefile
create_makefile('MyModule')

# add the target to the makefile
makefile = File.new("Makefile","a")  # open in append mode
makefile << "\n"
makefile << "mymodule_wrap.cpp: MyModule.i\n"
makefile << "\tswig -ruby MyModule.i\n"
makefile.close
# Done. Now the makefile will do the job as expected.

Again, I didn't test the above.

Bye,
         Luigi



Sun, 04 Jul 2004 19:57:55 GMT  
 Swig Ruby documentation mods.

Quote:


> >I'm not using a .i file -- I'm using the fact that swig -ruby example.c
> >will generally work, as described elsewhere in the docs.

> Oh, I see. Then I agree: SWIG is correct in complaining, but this needs
> mentioning.

OK, will do.

Quote:

> > > >* It is not clear what should be done about integrating several C
        [...]
> > > - I write a file mymodule.i containing:

> > > %module MyModule
> > > %include somefuncs.i
> > > %include someotherfuncs.i

> > > - by running SWIG on the latter file, all functions from the two original
> > > ones will be exported in MyModule.

> >Will it produce one "%s_wrap.c" or two?

> Just one.

OK, that's worth knowing.

Quote:

> > > If you want to get fancy, you can compare the modification times of
> > > MyModule.i and mymodule_wrap.cpp and run swig only when needed

> >Yes, that sort of thing would work, but since extconf is supposed to be
> >used to write a makefile, it seems a bit much to re-implement make
> >inside it! :-)

> I see your point.
> Ok, how about:

> require 'mkmf'
> require 'ftools'
        [...]
> # add the target to the makefile
> makefile = File.new("Makefile","a")  # open in append mode

I'm not sure that putting it at the end would be right.

Quote:
> makefile << "\n"
> makefile << "mymodule_wrap.cpp: MyModule.i\n"
> makefile << "\tswig -ruby MyModule.i\n"
> makefile.close
> # Done. Now the makefile will do the job as expected.

I think mkmf needs some method for adding target, dependencies, actions
to the makefile.  I'm not sure who to contact about this, and I'm
not sure how to integrate it into the makefile creation process, so that
your new targets end up as dependencies in rules mkmf already writes.

I think such a method would be part of a good refactoring for this package.

Quote:

> Again, I didn't test the above.

> Bye,
>          Luigi

        Hugh


Sun, 04 Jul 2004 20:35:17 GMT  
 Swig Ruby documentation mods.

Quote:
>I think mkmf needs some method for adding target, dependencies, actions
>to the makefile.  I'm not sure who to contact about this, and I'm
>not sure how to integrate it into the makefile creation process, so that
>your new targets end up as dependencies in rules mkmf already writes.

Were we on the python mailing list, someone would now show up and say:
"Use the source, Luke" :)

Quote:
>I think such a method would be part of a good refactoring for this package.

I agree. Might be a good topic for starting a new thread.

Bye,
         Luigi



Sun, 04 Jul 2004 21:23:08 GMT  
 Swig Ruby documentation mods.

Quote:


> >I think mkmf needs some method for adding target, dependencies, actions
> >to the makefile.  I'm not sure who to contact about this, and I'm
> >not sure how to integrate it into the makefile creation process, so that
> >your new targets end up as dependencies in rules mkmf already writes.

> Were we on the Python mailing list, someone would now show up and say:
> "Use the source, Luke" :)

Yes, I have done, but "I'm not sure how to" means: I don't have enough
depth of understanding of the code to do this yet.  Given the usual time
constraints I raise it in case those who do deeply understand the code
consider this useful and trivial for them.

Quote:

> >I think such a method would be part of a good refactoring for this package.

> I agree. Might be a good topic for starting a new thread.

Hence the addition to the subject line...
Quote:

> Bye,
>          Luigi

        Hugh


Sun, 04 Jul 2004 22:11:47 GMT  
 
 [ 11 post ] 

 Relevant Pages 

1. Linking Ruby to C++ via SWIG in Cygwin: missing ruby.o

2. Linking Ruby to C++ via SWIG in Cygwin: missing ruby.a

3. Ruby-SWIG question: wrapping function objects with blocks

4. Limited Support for Multiple Inheritance in SWIG/Ruby

5. Ruby-swig for C struct plus methods

6. Ruby Xlib wrapper updated to use latest SWIG

7. SWIG: returning a bool reference or pointer to Ruby

8. SWIG & the New Allocation Framework (Ruby 1.7)

9. Ruby SWIG problems

10. bug in ruby SWIG-1.3.11

11. Updated Swig/Ruby interface document

 

 
Powered by phpBB® Forum Software