OpenSolaris: How to Build CPAN Perl Modules with GCC Instead of the Sun Compiler

When building Perl modules with CPAN, the system assumes that the same compiler arguments that were used to compile Perl (indicated in the output of "perl -V") should be used to compile modules. However on OpenSolaris, Perl was compiled with the Sun C compiler, whereas the OS distributes GCC by default. This translates to an annoying situation: out of the box, when attempting to build a CPAN module, GCC will fail when encountering arguments CPAN passed to it that it does not recognize (the most prevalent error is "unrecognized option `-KPIC'"). The right solution is of course to install the Sun C compiler ("pkg install ss-dev") but this is 200MB+ of packages with tons of dependencies. A quicker and hackish workaround is to write a cc(1) wrapper that translates or ignores the 4 arguments that GCC does not support (-KPIC -xO3 -xspace -xildoff). I wrote such a wrapper. Put it in a temporary PATH location (eg. /root/bin) and run CPAN like this:

$ env PATH="/root/bin:$PATH" /usr/perl5/5.8.4/bin/cpan Crypt::SSLeay

Here is the code:

#!/usr/bin/python
# cc(1) wrapper to build CPAN Perl modules with GCC on OpenSolaris. -mrb
import os, sys
path = '/usr/gnu/bin/cc'
args = []
i = 0
while i < len(sys.argv):
       if i == 0:
               args.append(path)
       elif sys.argv[i] == '-KPIC':
               args.append('-fPIC')
       elif sys.argv[i] == '-xO3':
               args.append('-O3')
       elif sys.argv[i] == '-xspace':
               pass
       elif sys.argv[i] == '-xildoff':
               pass
       else:
               args.append(sys.argv[i])
       i += 1
os.execv(path, args)
mrb Monday 13 September 2010 at 10:11 pm | | Default
Used tags: , , ,

six comments

Niles Mills

Nice fix. Works great. Rewrote it in bash for a box that didn’t have python. Thanks!

Niles Mills, (Email ) - 07-11-’10 14:06
Larry Siden

This is fantastic! Thank you!

Larry Siden, (URL) - 04-04-’11 09:49
Dmitry Zayats

very nice article and it’s also valid for Solaris 10 SPARC.

In my case there is one more option that gcc doesn’t understand

arch=v8

I redid this python script to perl as follows

#!/usr/bin/perl

push(@args,”/usr/sfw/bin/gcc”);

foreach $arg (ARGV) { $arg =~ s/-KPIC/-fPIC/; $arg =~ s/-xO3/-O3/; if(($arg =~ /-xspace/) || ($arg =~ /-xildoff/) || ($arg =~ /arch=v8/)) { next; } push(args,$arg); }

printf “Executing gcc with following arguments\n”; foreach (@args) { print “$_ \n”; }

system(@args);

Dmitry Zayats, - 20-07-’11 18:54
mrb

Thanks Dmitry. I wish my blog software would not mess up the formatting of code in comments…

mrb, - 21-07-’11 20:38
Andreas

And even more than 2 years after the original posting the idea got me moving and passing this hurdle of not having the original cc, thanks a lot.
(my version is in Bourne shell but that doesn’t matter, your idea is what counts).

Andreas, - 08-02-’13 11:29
Tony Dixon

Genius! Many thanks.

Tony Dixon, - 22-08-’13 09:18
(optional field)
(optional field)
Remember personal info?
Small print: All html tags except <b> and <i> will be removed from your comment. You can make links by just typing the url or mail-address.