Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

How to know if a CPAN module uses XS

by bcarroll (Pilgrim)
on Jun 19, 2012 at 23:51 UTC ( [id://977171]=perlquestion: print w/replies, xml ) Need Help??

bcarroll has asked for the wisdom of the Perl Monks concerning the following question:

Is there a magical method to determine if a module on CPAN is pure perl code or if it contains XS code?

Basically, I am trying to determine which modules actually need compiled.

Replies are listed 'Best First'.
Re: How to know if a CPAN module uses XS
by syphilis (Archbishop) on Jun 20, 2012 at 00:09 UTC
    I just scan the MANIFEST (in the source distro) for a '.xs' file. Presence of one means that compiling is reqired; absence means no compilation is required.

    However, whilst this is a fairly reliable method, it's not foolproof. For example, PDL::NetCDF has no xs file yet requires compilation. (The 'perl Makefile.PL' step generates the xs file.) And I think there are some modules that, while providing an xs file, also cater for a pure-perl build.

    Perhaps some of the other monks know of a smarter approach.

    Cheers,
    Rob

      Another heuristic is to look for use DynaLoader or use XSLoader in any of the modules.

      XS code also often comes with a file called typemap which is used by the XS build system to map between Perl data structures and C data structures.

      And ppport.h is often bundled. This is a C header file which, as I understand it, smooths over the differences between versions of the Perl API.

      perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
      (The 'perl Makefile.PL' step generates the xs file.)
      It does not. The Makefile thereby generated has a rule that turns the supplied .pd file into .pm and .xs files, and for distributions that have "opted in" and with a PDL 2.058+, a .c file per operation (allowing parallel builds).

      Probably the best heuristic to spot a PDL distro that generates XS rather than being a pure-Perl user of other PDL modules is the use of PDL::Core::Dev, and/or calls to pdlpp_stdargs.

Re: How to know if a CPAN module uses XS
by toolic (Bishop) on Jun 20, 2012 at 00:35 UTC
    A Super Search yielded the somewhat related Modules in CPAN that don't need to be compiled, but it does not give any more helpful advice than syphilis'.

    http://search.cpan.org uses DLSIP notation. The "L" indicates whether compile is needed. However, it requires CPAN authors to "Register" a newly uploaded module, which is voluntary, and in my experience, unreliable. IIRC, the DSLIP designation is manually entered by the author, which means it is subject to typos.

      Out of interest how can you see that module metadata? I know where you change it on pause but I can only see metadata for modules I own. There are some modules I maintain but I'm not the owner and I'd like to check them.

      DSLIP designation is manually entered by the author, which means it is subject to typos.

      AFAIK (its been 10 years) its a checkbox and/or dropdown menu list with descriptions, so typos not possible :)

        While not as easy to type in incorrect information, it is still possible to check the wrong box (I do it probably once a week here at the Monastery as I'm voting) or select the wrong dropdown item, without noticing. The point is that it is voluntary information which means it is error-prone.
Re: How to know if a CPAN module uses XS
by bulk88 (Priest) on Jun 20, 2012 at 01:44 UTC
    WriteMakefile returns a MM object which has has_link_code and needs_linking. Somehow calling these are your safest bet. If I had to try and do it, I would copy EUMM's WriteMakefile into my script, switch packages, define a new WriteMakefile based off EUMM's but without the flush() at the end, switch packages back to main, , run the makefile.pl with a do(), intercept the returning MM obj in my WriteMakefile, and call needs_linking(). The most complex use of EUMM I've ever seen was in Perl TK, and I did something similar to what I described to you to intercept Tk::MMutil::TkExtMakefile. Note this is not a "static" way of determining whether it needs to be compile, so security issues might be a problem for you.

    Another solution would be to generate the makefile, then do backticks on your make tool running in "list commands and do not execute" mode, then parse that looking for your local C compiler.
      I think the first difficulty (not necessarily a show-stopper) I see with that approach is that it relies on WriteMakefile() being run.
      However, some Makefile.PL's may abort prior to that happening - eg because $] is too low, or because $^O is inappropriate, or because some pre-requisite could not be found.

      Handle that, and you're probably on a winner.
      It may even be that if $] is too low or $^O is inappropriate, then you don't even need to know whether the module needs to be compiled ... and if the Makefile.PL aborts because a pre-requisite C library can't be found, then it should be safe to assume it's a module that needs compiling. But if the Makefile.PL aborts because a pre-requisite perl module could not be found, then all bets are off. (I've a notion that there are some modules that do that, though I can't provide an example.)

      then do backticks on your make tool running in "list commands and do not execute" mode

      And then there's Inline/Inline::C ... run make in "list commands and do not execute" mode when building Inline/Inline::C and you'll see no mention of any compiler. (But perhaps that's the right result anyway ... not sure about that one.)

      And, of course, not every distro has a Makefile.PL - and there's presumably a need to handle them, too. (That's the beauty of having a choice of 2 build systems - instead of solving a problem once, you get the added joy of having to solve it twice :-)

      Cheers,
      Rob

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://977171]
Approved by stevieb
Front-paged by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (2)
As of 2024-04-25 03:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found