in reply to Updating broken and possibly incomplete SWIG Perl module building tutorial on SWIG web site

So the new commands I proposed for SWIG are:

Both are wrong ;)

When building a module, you're not building perl (like perlembed), so use a Makefile.PL

See perlxstut, h2xs, module-starter, ExtUtils::MakeMaker/Module::Build

Any thoughts? Could it be done better or differently?

If you insist invoking gcc yourself, don't assume everyone hash bash, just write a perl program to invoke gcc

system 'gcc', $ccflags, $in, $out ...;
  • Comment on Re: Updating broken and possibly incomplete SWIG Perl module building tutorial on SWIG web site
  • Download Code

Replies are listed 'Best First'.
Re^2: Updating broken and possibly incomplete SWIG Perl module building tutorial on SWIG web site
by educated_foo (Vicar) on Mar 26, 2011 at 18:44 UTC
    ... use a Makefile.PL ...
    This is really the best option, and it's not hard once you figure out which of ExtUtils::MakeMaker's many options to tweak:
    use ExtUtils::MakeMaker; WriteMakefile( NAME => 'YourModule', OBJECT => 'YourModule_wrap.o', # Add extra include paths here, if needed # INC => '-I/path/to/include', # Add shared and static libraries here, if needed # LDFROM => 'YourModule_wrap.o -L/path/to/lib -lyourmodule', );
    Basically, you add Foo_wrap.o to OBJECT, and possibly modify INC and LDFROM if you're compiling against a C library. SWIG could/should even generate a basic Makefile.PL when it generates the wrapper code.
      Thanks guys for the insightful input, I agree a more robust and elegant end solution would be to have SWIG auto-generate the appropriate Makefile.PL (or Build.PL) that will then be used to do the compilation and linking properly for one's environment.

      I will work on this and present it to the SWIG people. For the time being they have at least updated the Perl quick tutorial which was broken to what I proposed and this has been tested. I know its only for systems with gcc but that is what their original commands had anyway so I guess no harm done.

Re^2: Updating broken and possibly incomplete SWIG Perl module building tutorial on SWIG web site
by hermida (Scribe) on Mar 26, 2011 at 16:44 UTC
    I explained that this is for compiling a SWIG auto-generated XS and .pm binding code for specified C code, so it makes no sense to have to go through the additional hassle of creating and using a Makefile.PL when all you need are the gcc compile and gcc link commands that should be used for building a Perl module. ExtUtils::MakeMaker and Module::Build get the same gcc compile and gcc link flags from Perl core Config.pm no? They just put it into a Makefile as constants and during make they through them into the gcc compile and gcc link commands. I am doing the same but just shortcutting the parts I don't need.

    The commands I provided produce the same gcc compile and link commands that you see when you install a CPAN module with C/C++ w/ XS code so how can this be wrong? Please check for yourself. The only flags that are excluded are not relevant to a SWIG Perl module build. CPAN modules with C/C++ w/ XS code are compiled like Perl itself was and should be, I don't know here if there is a misunderstanding here.

      Backticks won't work on Windows, but then, I don't know whether SWIG will work on (non-Cygwin) Windows at all either.

      And not all the world uses gcc.

      Personally, I would go the route of least resistance and change the SWIG "build" process to produce a Makefile through ExtUtils::MakeMaker (see WriteMakefile() in ExtUtils::MakeMaker - this is a tried and true mechanism that will know about the appropriate C compiler to use with This Perl and will also know about the options needed.

        You are totally right about doing something more elegant with ExtUtils modules, we talked about it on chatterbox, but as a first go I was updating the quick getting started tutorial on the SWIG web site and wanted the commands to be similar to what they already had, but correct and working this time.

        For Windows the tutorial wasn't covering that so just wanted to update what they already had which was for Unix/Linux

        True not all people use gcc, but this is what they had in the tutorial so stuck with that and I believe they assumed if you don't have gcc you see what is going on and can easily map it to your particular cc and ld program.

        How would you use all the goodies of ExtUtils::MakeMaker without starting with a manually written Makefile.PL? I just think having to write a Makefile.PL might be too much unnecessary hassle for the SWIG Perl build process.

        Or would this Makefile.PL be the same for every SWIG Perl build and I could send it to them for everyone to use with an updated SWIG Perl build process tutorial?

Re^2: Updating broken and possibly incomplete SWIG Perl module building tutorial on SWIG web site
by hermida (Scribe) on Mar 26, 2011 at 17:02 UTC
    Here is just one example of the commands when installing a CPAN module and using ExtUtils::MakeMaker:
    /home/hermida/soft/perl/5.12.3/bin/perl /home/hermida/soft/perl/5.12.3 +/lib/5.12.3/ExtUtils/xsubpp -noprototypes -typemap /home/hermida/soft +/perl/5.12.3/lib/5.12.3/ExtUtils/typemap -typemap typemap Expat.xs > + Expat.xsc && mv Expat.xsc Expat.c gcc -c -D_REENTRANT -D_GNU_SOURCE -fno-strict-aliasing -pipe -fstack-p +rotector -I/usr/local/include -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS +=64 -O2 -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-prot +ector --param=ssp-buffer-size=4 -m64 -mtune=native -march=native -g - +DVERSION=\"2.40\" -DXS_VERSION=\"2.40\" -fPIC "-I/home/hermida/soft/p +erl/5.12.3/lib/5.12.3/x86_64-linux-thread-multi/CORE" Expat.c gcc -shared -O2 -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fsta +ck-protector --param=ssp-buffer-size=4 -m64 -mtune=native -march=nati +ve -g Expat.o -o ../blib/arch/auto/XML/Parser/Expat/Expat.so

    This is how all CPAN Perl modules with C + XS code compile and link (yes some have some extra flags that are just relevant to that particular module, but the all have these as the basis). The first command is not necessary for SWIG as you already have the C code and as I showed before swig -perl example.i autogenerates XS and .pm and other wrapping C code automatically. Let's breakdown the above parts that ExtUtils::MakeMaker created for this command:

    gcc -c [Config-ccflags] [Config-optimize] [Config-cccdlflags] "-I[Conf +ig-archlib]/CORE" module.c gcc [Config-lddlflags] module.o -o module.so

    This creates everything you need to create the Perl module and has been tested successfully by me and by SWIG people just yesterday. We ran Perl code using the modules and they work perfectly.

    Please tell me why this is wrong in detail.