Compiling NumPy in Solaris 10

Trying to install NumPy 1.10.1 in my Solaris 10 machines produces the following compiling error:

[...]
/usr/local/bin/gfortran -Wall -g -Wall -g -shared -Wl,-gc-sections -Wl,-s -mimpure-text build/temp.solaris-2.10-i86pc.32bit-3.5/numpy/linalg/lapack_litemodule.o build/temp.solaris-2.10-i86pc.32bit-3.5/numpy/linalg/lapack_lite/python_xerbla.o -L/usr/local/lib -L/usr/local/lib/gcc/i386-pc-solaris2.10/5.3.0 -L/usr/local/lib -Lbuild/temp.solaris-2.10-i86pc.32bit-3.5 -llapack -lblas -lpython3.5m -lgfortran -o build/lib.solaris-2.10-i86pc.32bit-3.5/numpy/linalg/lapack_lite.so
    ld: fatal: unrecognized option '-g'
    ld: fatal: use the -z help option for usage information
    collect2: error: ld returned 1 exit status
    ld: fatal: unrecognized option '-g'
    ld: fatal: use the -z help option for usage information
    collect2: error: ld returned 1 exit status
    error: Command "/usr/local/bin/gfortran -Wall -g -Wall -g -shared -Wl,-gc-sections -Wl,-s -mimpure-text build/temp.solaris-2.10-i86pc.32bit-3.5/numpy/linalg/lapack_litemodule.o build/temp.solaris-2.10-i86pc.32bit-3.5/numpy/linalg/lapack_lite/python_xerbla.o -L/usr/local/lib -L/usr/local/lib/gcc/i386-pc-solaris2.10/5.3.0 -L/usr/local/lib -Lbuild/temp.solaris-2.10-i86pc.32bit-3.5 -llapack -lblas -lpython3.5m -lgfortran -o build/lib.solaris-2.10-i86pc.32bit-3.5/numpy/linalg/lapack_lite.so" failed with exit status 1

    ----------------------------------------
  Rolling back uninstall of numpy
Command "/usr/local/bin/python3 -u -c "import setuptools, tokenize;__file__='/tmp/pip-build-ottl5a7u/numpy/setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record /tmp/pip-24agajji-record/install-record.txt --single-version-externally-managed --compile" failed with error code 1 in /tmp/pip-build-ottl5a7u/numpy/

The problem is this directive to the fortran compiler:

-Wl,-gc-sections

This directive instructs the fortran compiler to run the linker with a -gc-sections directive.

Under Solaris 10 I am using the GCC compiler chain, but I keep the native Solaris 10 linker and that option is not recognized.

Studing the manual for Linux linker, I see this:

--gc-sections
--no-gc-sections
    Enable garbage collection of unused input sections.  It is ignored
    on targets that do not support this option.  The default behaviour
    (of not performing this garbage collection) can be restored by
    specifying --no-gc-sections on the command line.

Looks like we could safely just discard that directive.

We do the following steps:

  1. Download the NumPy package and unpack it:

    $ cd /tmp
    $ python3 -m pip download numpy
    Collecting numpy
    Using cached numpy-1.10.1.zip
    Saved ./numpy-1.10.1.zip
    $ unzip -x numpy-1.10.1.zip
    [...]
    $ cd numpy-1.10.1
    
  2. Apply this patch on file numpy/distutils/fcompiler/gnu.py:

    --- ./numpy/distutils/fcompiler/gnu.py2 2016-05-01 21:53:10.452514012 +0200
    +++ ./numpy/distutils/fcompiler/gnu.py  2016-05-01 21:54:04.773135377 +0200
    @@ -138,7 +138,7 @@
    
                 opt.extend(['-undefined', 'dynamic_lookup', '-bundle'])
             else:
    -            opt.append("-shared -Wl,-gc-sections -Wl,-s")
    +            opt.append("-shared -Wl,-s")
             if sys.platform.startswith('sunos'):
                 # SunOS often has dynamically loaded symbols defined in the
                 # static library libg2c.a  The linker doesn't like this.  To
    @@ -147,6 +147,8 @@
                 # ".. Instead of using -mimpure-text, you should compile all
                 #  source code with -fpic or -fPIC."
                 opt.append('-mimpure-text')
    +        else:
    +            opt.append('-Wl,-gc-sections')
             return opt
    
         def get_libgcc_dir(self):
    
  3. Compile and install numpy package:

    $ sudo python3 -m pip install .
    Processing /tmp/z/numpy-1.10.1
    Installing collected packages: numpy
      Found existing installation: numpy 1.10.0
        Uninstalling numpy-1.10.0:
          Successfully uninstalled numpy-1.10.0
      Running setup.py install for numpy ... /
    Successfully installed numpy-1.10.1
    
  4. Profit!

Update 20160501: NumPy version 1.11.0 compiles cleanly on my Solaris 10 machines. NumPy developers just dropped the directive.