mrb's blog

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

Keywords: hack perl solaris sysadmin

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:

# 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:
       elif sys.argv[i] == '-KPIC':
       elif sys.argv[i] == '-xO3':
       elif sys.argv[i] == '-xspace':
       elif sys.argv[i] == '-xildoff':
       i += 1
os.execv(path, args)

Niles Mills wrote: Nice fix. Works great. Rewrote it in bash for a box that didn't have python. Thanks! 07 Nov 2010 22:06 UTC

Larry Siden wrote: This is fantastic! Thank you! 04 Apr 2011 16:49 UTC

Dmitry Zayats wrote: 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


I redid this python script to perl as follows



foreach $arg (@ARGV)
$arg =~ s/-KPIC/-fPIC/;
$arg =~ s/-xO3/-O3/;
if(($arg =~ /-xspace/) || ($arg =~ /-xildoff/) || ($arg =~ /arch=v8/))

printf "Executing gcc with following arguments\n";

foreach (@args)
print "$_ \n";

21 Jul 2011 01:54 UTC

mrb wrote: Thanks Dmitry. I wish my blog software would not mess up the formatting of code in comments... 22 Jul 2011 03:38 UTC

Andreas wrote: 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).
08 Feb 2013 19:29 UTC

Tony Dixon wrote: Genius! Many thanks. 22 Aug 2013 16:18 UTC