Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagelib
Path: blob/master/c_lib/SConstruct
4024 views
##
## c_lib SConstruct file
##
## Author: Joel Mohler (original file)
##         Craig Citro (minor edits)
##
##
## 2007 Sep 01:
## I added a fix for trac ticket 555, which is to add the "install_lib"
## and "install_includes" below. 
## This is not the most elegant approach, I think. The problem is this:
## I (= Craig Citro) don't understand how to tell scons that it should
## look at /sage/local/libcsage.dylib to see if *it* is up to date. So
## what happens is that you change libcsage in branch foo, do a sage -b bar,
## and since you haven't modified sage-bar/c_lib/libcsage.dylib, scons
## doesn't think it needs to reinstall libcsage.dylib into
## $SAGE_ROOT/local/lib. (If it's not an OSX machine, then the target
## would be libcsage.so, but the point is the same.) This is obviously 
## problematic; the best solution I could come up with is just always 
## copying all the libraries and headers into $SAGE_ROOT where they belong.
## Actually, having discussed this with William, we came up with a better
## plan: we have two targets, install and branch_switch. With install, it's
## what you'd get with a sage -b. With branch_switch, it rebuilds the
## libraries, and then forces the copy regardless to exactly deal with
## the problem above.
##
## I also edited a bunch of lines below so that they didn't go over 80 chars,
## because I don't like seeing wrapped lines.
##
## 2007 Oct 01:
## After discussion with Gonzalo Tornaria and William Stein, we 
## decided that there's a much better solution to the above 
## problem: make $SAGE_ROOT/local/lib/libcsage.dylib. Also, we're
## reorganizing the directory tree to have src/ and include/
## subdirectories, and $SAGE_ROOT/local/include has a link into
## this directory.
##
import os, platform

# Note that SCons's strong point is not the './configure' step of 
# autotools.  However, for this build we know that we are in a 
# SAGE local filesystem.  Once we have SAGE_LOCAL imported, we 
# know where everything is.

# Native tools are supposedly used by default on SCons, 
# Though this was only happening with the C++ compiler, not the 
# C compiler on Solaris. But we MUST ensure
# that gcc and g++ are used if environment variables CC and CXX 
# are set to GNU tools. 

# This was a particular issue on Solaris
# http://trac.sagemath.org/sage_trac/ticket/6595
# but will no doubt affect other proprietary Unix systems
# This forces the GNU tools on AIX, HP-UX, Tru64 and IRIX.
# The Sun tools on Solaris or Linux if the variable CXX points 
# to a Sun compiler 'CC' 

# See this email: http://markmail.org/message/4atmrof2gc2dgavj?q=scons+cxx
# Tested on Solaris, AIX and HP-UX. Untested on Tru64 and IRIX.
# Linux with Sun Studio is also untested. 

CXX = os.environ.get('CXX', 'g++')
if os.uname()[0] == 'AIX' and os.environ['CXX'].endswith('g++'):
   env = Environment(tools=['gcc','g++','gnulink'], ENV = os.environ)
elif os.uname()[0] == 'HP-UX' and os.environ['CXX'].endswith('g++'):
   env = Environment(tools=['gcc','g++','gnulink'], ENV = os.environ)
elif os.uname()[0] == 'IRIX' and os.environ['CXX'].endswith('g++'):
   env = Environment(tools=['gcc','g++','gnulink'], ENV = os.environ)
elif os.uname()[0] == 'IRIX64' and os.environ['CXX'].endswith('g++'):
   env = Environment(tools=['gcc','g++','gnulink'], ENV = os.environ)
elif os.uname()[0] == 'SunOS' and os.environ['CXX'].endswith('g++'):
   env = Environment(tools=['gcc','g++','gnulink'], ENV = os.environ)
elif os.uname()[0] == 'Tru64' and os.environ['CXX'].endswith('g++'):
   env = Environment(tools=['gcc','g++','gnulink'], ENV = os.environ)
# Non-GNU compilers follow. Linux with Sun Studio has not been tested. 
# It is not likely to work unless Sun Studio was installed by root
# (There are two ways to install Sun Studio - either as a tar file, 
# or as package. SCons uses information from the package database
# to know where to find Sun Studio. 
elif os.uname()[0] == 'SunOS' and os.environ['CXX'].endswith('CC'):
   env = Environment(tools=['suncc','sunc++','sunlink'], ENV = os.environ)
elif os.uname()[0] == 'Linux' and os.environ['CXX'].endswith('CC'):
   env = Environment(tools=['suncc','sunc++','sunlink'], ENV = os.environ)
else:
   env = Environment(ENV = os.environ)

# By default, SCons hashes the source file contents to determine 
# if rebuilds are necessary. If you like the old way better, 
# uncomment this to use timestamps.
#env.SourceSignatures('timestamp')


# Since the sage build of python is only a static library,
# we just suppress the undefined python symbols. I don't 
# really understand the other options (-single_module and 
# -flat_namespace).  I can't find the documentation.
##
## These extra link flags make OS X play nice with building a dynamic
## library. -undefined dynamic_lookup is being used instead of
## -undefined suppress -- they should do basically the same thing (
## tell the linker it's okay to ignore undefined symbols at link time),
## but there's a possibility that -undefined suppress will throw an
## error, whereas -undefined dynamic_lookup will definitely try to find
## missing symbols at runtime.
## The other two options control the way the linker creates a namespace
## for the dynamic library; check the man page for ld on a mac to see
## the details.
if os.environ['SAGE64']=="yes":
    # We want the debug and optimization flags, since debug symbols are so useful, etc.
    print "64 bit mode"
    env.Append( CFLAGS="-O2 -g -m64" )
    env.Append( CXXFLAGS="-O2 -g -m64" )
    env.Append( LINKFLAGS="-m64" )

if env['PLATFORM']=="darwin":
    env.Append( LINKFLAGS=" -single_module -flat_namespace -undefined dynamic_lookup" )

# SCons doesn't automatically pull in system environment variables
# However, we only need SAGE_LOCAL, so that's easy.
env['SAGE_LOCAL'] = os.environ['SAGE_LOCAL']

# Get the python version number. We want this to be a bit more resistant to major 
# change of version. 
env['PYV']=platform.python_version().rsplit('.', 1)[0]

# The SCons convenience function Split is the only strange thing 
# to python programmers. It just makes a list by splitting on 
# whitespace without the syntax clutter of lists of strings.
includes = ['$SAGE_LOCAL/include/', '$SAGE_LOCAL/include/python$PYV/',
            '$SAGE_LOCAL/include/NTL/', 'include']
cFiles = Split( "convert.c  interrupt.c  memory.c  mpn_pylong.c  mpz_pylong.c") + \
         Split( "mpz_longlong.c stdsage.c  gmp_globals.c" )
cppFiles = Split( "ZZ_pylong.cpp  ntl_wrap.cpp" )
srcFiles = cFiles + cppFiles

lib = env.SharedLibrary( "csage", [ "src/" + x for x in srcFiles ],
                         LIBS=['ntl', 'pari', 'gmp', 'python$PYV'], 
                         LIBPATH=['$SAGE_LOCAL/lib','$SAGE_LOCAL/lib/python$PYV/config/'],
                         CPPPATH=includes )
env.Install("$SAGE_LOCAL/lib", lib)

#Here we only copy the files over if we are on Cygwin.  Otherwise, the
#library will be handled by the symlinks created in
#$SAGE_ROOT/devel/sage/spkg-install
if os.environ['UNAME'] == 'CYGWIN':
    env.Alias("install", "$SAGE_LOCAL/lib")
else:
    env.Alias("install", [lib])