Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Easy native wrappers with SWIG

794 views

Published on

Learn how to use SWIG to create Perl modules that interface to libraries written in C or C++.

Published in: Software
  • Be the first to comment

  • Be the first to like this

Easy native wrappers with SWIG

  1. 1. EASY NATIVE WRAPPERS WITH SWIG 2014 Barcelona Perl Workshop
  2. 2. Text Every time you open a pipe to a program to do something that Perl already knows how to do, God kills a kitten. - @pplu_io ! Check CPAN to save kittens.
  3. 3. Going Native Obscure/Propietary libraries Legacy code Code Optimization* Test harness Glue
  4. 4. XS
  5. 5. XS is an interface description file format used to create an extension interface between Perl and C code (or a C library) which one wishes to use with Perl. -perlxs 1:1
  6. 6. “Hooking Perl to C using XS requires you to write a shell.pm module to bootstrap an object file that has been compiled from C code, which was in turn generated by xsubpp from a.xs source file containing pseudo-C annotated with an XS interface description.” –Brian D. Foy, “Perl Best Practices”
  7. 7. “Hooking Perl to C using XS requires you to write a shell.pm module to bootstrap an object file that has been compiled from C code, which was in turn generated by xsubpp from a.xs source file containing pseudo-C annotated with an XS interface description.” –Brian D. Foy, “Perl Best Practices”
  8. 8. “If that sounds horribly complicated, then you have achieved an accurate understanding of the use of xsubpp.” –Brian D. Foy, “Perl Best Practices”
  9. 9. Inline::C It’s C, but Inline!
  10. 10. Inline::C #!/usr/bin/env perl use strict; use Inline C => << 'END_C'; void hello() { printf("Hello, world!"); } END_C ! hello();
  11. 11. How does it work?
  12. 12. Live demo
  13. 13. SWIG Simplified Wrapper and Interface Generator
  14. 14. “SWIG is an interface compiler that connects programs written in C and C++ with scripting languages such as Perl, Python, Ruby, and Tcl.” – http://www.swig.org/exec.html
  15. 15. Purpose Prototyping & Debugging Systems Integration Build extension modules
  16. 16. SWIG Open Source (GPL) Created in 1995 by Dave Beazley Support for a couple dozen languages by 2014 http://www.swig.org/
  17. 17. Supported Languages Tcl Python Perl Java Ruby PHP Ocaml Pike C# Scheme Modula-3 Lua Common Lisp R Octave Go D Javascript http://www.swig.org/compat.html#SupportedLanguages
  18. 18. Interface definition /* File : example.i */ ! %module example ! %inline %{ extern int gcd(int x, int y); extern double Foo; %}
  19. 19. C file /* File : example.c */ ! /* A global variable */ double Foo = 3.0; ! /* Compute the greatest common divisor of positive integers */ int gcd(int x, int y) { int g; g = y; while (x > 0) { g = x; x = y % x; y = g; } return g; }
  20. 20. Build Wrapper swig -perl example.i
  21. 21. Compile & Link (Ideal) cc -c example_wrap.c ! cc -c example.c ! ldd example_wrap.o example.o -o example.so
  22. 22. Compile & Link (Real World) cc -c -fno-common -DPERL_DARWIN -fno-strict-aliasing - pipe -fstack-protector -I/usr/local/include -I/opt/ local/include -I/Users/arturo/perl5/perlbrew/perls/ perl-5.10.1/lib/5.10.1//darwin-2level/CORE/ example_wrap.c ! cc -c -fno-common -DPERL_DARWIN -fno-strict-aliasing - pipe -fstack-protector -I/usr/local/include -I/opt/ local/include -I/Users/arturo/perl5/perlbrew/perls/ perl-5.10.1/lib/5.10.1//darwin-2level/CORE/ example.c ! env MACOSX_DEPLOYMENT_TARGET=10.3 cc -bundle - undefined dynamic_lookup -L/usr/local/lib -L/opt/ local/lib -fstack-protector example_wrap.o example.o -o example.bundle
  23. 23. Hello, SWIG! perl runme.pl The gcd of 42 and 105 is 21 Foo = 3 Foo = 3.1415926
  24. 24. Getting There Simpler interface file Add SWIG to build chain
  25. 25. (#|%)include %module MyModule %{ #include "mymodule.h" %} ! %include "mymodule.h"
  26. 26. Makefile.PL use 5.008000; use Config; use ExtUtils::MakeMaker; use vars qw($version); $version='0.1'; my $cmd = qq{swig -outdir lib -o Fact_wrap.c -perl Fact.i}; print "Executing $cmdn"; system($cmd); if($?==-1) { die("failed to execute: $!"); } elsif($?>0) { die("Error creating perl wrapper: $?"); } WriteMakefile( NAME => 'Fact', VERSION => $version, PREREQ_PM => {}, DEFINE => '-DMACOSX '.$Config{ccflags}, INC => '', OBJECT => '$(O_FILES)', clean => { FILES => 'lib/Fact.pm Fact_wrap.c', }, PMLIBDIRS => [ 'lib', 'lib/Fact' ], );
  27. 27. Live demo!
  28. 28. structs struct Vector { double x,y; }; package Vect::Vector; use vars qw(@ISA %OWNER %ITERATORS %BLESSEDMEMBERS); @ISA = qw( Vect ); %OWNER = (); %ITERATORS = (); *swig_x_get = *Vectc::Vector_x_get; *swig_x_set = *Vectc::Vector_x_set; *swig_y_get = *Vectc::Vector_y_get; *swig_y_set = *Vectc::Vector_y_set; …
  29. 29. Sane Accessors my $v = Vect::Vector->new(); $v->{x}=3; $v->{y}=4;
  30. 30. Distribute on CPAN No need to force swig dependency Include the generated wrapper (*_wrap.c, *.pm) Include the interface definition as a courtesy
  31. 31. Tip of the Iceberg C++ Exceptions Typemaps %perlcode
  32. 32. ++$learn http://www.slideshare.net/daoswald/getting-started-with- perl-xs-and-inlinec Cozens, Simon. “Advanced Perl Programming” Orwant, Jon. “Computer Science & Perl Programming” http://www.swig.org/
  33. 33. Q&A
  34. 34. Thank you! @codehead javier@rodriguez.org.mx scribd.com/javierrgz

×