GCC源码自动编译-python脚本

一、前言

    目前因机器OS GCC版本太老,导致无法编译一些新版本软件,所以写了一个自动编译GCC的python脚本,操作系统是比较老的suse 10, 很多系统自动软件版本都很低,所以此脚本一般可适用目前比较流行的OS,大家可多尝试一下

二、机器环境

OS: SUSE 10 

Bit: 64-bit

python: 2.6

 

三、依赖软件

gmp: 4.3.2

mpc: 0.8.1

mpfr: 2.4.2

gcc: 4.4.0

 

四、安装脚本

1 目录结构

InstallGcc

---bin

-----install_gcc.py

---conf

---tmp

---src

-----gcc

-------gmp-4.3.2.tar.gz

-------mpc-0.8.1.tar.gz

-------mpfr-2.4.2.tar.gz

-------gcc-4.4.0.tar.gz

 

 

2 脚本如下:

#!/usr/bin/env python
import sys,os,time,commands
import logging,logging.handlers

class InstallGcc(object):
    def __init__(self):
        self.base_dir=os.path.abspath(os.path.join(os.path.dirname(__file__),os.pardir))
        self.logger = self.get_logger()
        self.install_pardir='/usr/local'
        self.tmp_dir = '%s/tmp' % self.base_dir
        self.install_srcdir='%s/src/gcc' % self.base_dir
        self.dependList={'gmp':{'version':'4.3.2','srcfile':'gmp-4.3.2.tar.bz2'},'mpc':{'version':'0.8.1','srcfile':'mpc-0.8.1.tar.gz'},'mpfr':{'version':'2.4.2','srcfile':'mpfr-2.4.2.tar.bz2'},'gcc':{'version':'4.4.0','srcfile':'gcc-4.4.0.tar.gz'}}
        self.process_num = self.get_process_num()
    
    def get_logger(self):
        logname = '%s/log/install_gcc.log' % self.base_dir
        logger=logging.getLogger('install_mysql')
        logger.setLevel(logging.DEBUG)
        handler=logging.handlers.RotatingFileHandler(logname,maxBytes=4194304,backupCount=100)
        formatter = logging.Formatter('%(asctime)s %(filename)s:%(lineno)d [%(levelname)s]  %(message)s')
        handler.setFormatter(formatter)
        logger.addHandler(handler)
        return logger
    def get_process_num(self):
        cmd="cat /proc/cpuinfo |grep processor|wc -l"
        ret = self.get_exec_result(cmd)
        return ret[1] 
    def get_exec_result(self,cmd):
        ret = commands.getstatusoutput(cmd)
        if ret[0] != 0:
            self.logger.error("%s:%s",cmd,ret[1])
            sys.exit(1)
        return ret
    def decompress_soft(self,name):
        try:
            srcfile=self.dependList[name]['srcfile']
            if srcfile.endswith('bz2'):
                cmd = "tar -jxvf %s/%s -C %s" % (self.install_srcdir,srcfile,self.tmp_dir)
            elif srcfile.endswith('gz'):
                cmd = "tar -zxvf %s/%s -C %s" % (self.install_srcdir,srcfile,self.tmp_dir)
            ret = self.get_exec_result(cmd)
            untar_dir = "%s/%s-%s" % (self.tmp_dir,name,self.dependList[name]['version'])
            os.chdir(untar_dir)
        except Exception,e:
            self.logger.error("decompress error:%s",e)
            sys.exit(1)
    def install_gmp(self):
        try:
            install_dir='%s/gmp-%s' % (self.install_pardir,self.dependList['gmp']['version'])
            if os.path.isdir(install_dir):
                msg = "gmp has been existed,continuing next step"
                print msg
                self.logger.error(msg)
            else:
                self.decompress_soft('gmp')
                cmd = "./configure --prefix=%s" % install_dir
                ret = self.get_exec_result(cmd)
                cmd = "make -j %s && make install" % (self.process_num)
                ret = self.get_exec_result(cmd)
                msg = "gmp installed ok,continuing next step"
                print msg
                self.logger.debug(msg)    
        except Exception,e:
            self.logger.error("install gmp error:%s",e)
            sys.exit(1)
    def install_mpfr(self):
        try:
            install_dir = '%s/mpfr-%s' % (self.install_pardir,self.dependList['mpfr']['version'])
            if os.path.isdir(install_dir):
                msg = "mpfr has been existed,continuing next step"
                print msg
                self.logger.error(msg)
            else:
                self.decompress_soft('mpfr')
                cmd = "./configure --prefix=%s --with-gmp=%s/gmp-%s" % (install_dir,self.install_pardir,self.dependList['gmp']['version'])
                ret = self.get_exec_result(cmd)
                cmd = "make -j %s && make install" % self.process_num
                ret = self.get_exec_result(cmd)
                msg = "mpfr installed ok,continuing next step"
                print msg
                self.logger.debug(msg)
        except Exception,e:
            self.logger.error("install mpfr error:%s",e)
            sys.exit(1)
    def install_mpc(self):
        try:
            install_dir = '%s/mpc-%s' % (self.install_pardir,self.dependList['mpc']['version'])
            if os.path.isdir(install_dir):
                msg = "mpc has been existed,continuing next step"
                print msg
                self.logger.error(msg)
            else:
                self.decompress_soft('mpc')
                cmd = " ./configure --prefix=%s --with-gmp=%s/gmp-%s --with-mpfr=%s/mpfr-%s" % (install_dir,self.install_pardir,self.dependList['gmp']['version'],self.install_pardir,self.dependList['mpfr']['version'])
                ret = self.get_exec_result(cmd)
                cmd = "make -j %s && make install" % self.process_num
                ret = self.get_exec_result(cmd)
                msg = "mpc installed ok,continuing next step"
                print msg
                self.logger.debug(msg)
        except Exception,e:
            self.logger.error("install mpc error:%s",e)
            sys.exit(1)
    def install_gcc(self):
        try:
            install_dir = '%s/gcc-%s' % (self.install_pardir,self.dependList['gcc']['version'])    
            if os.path.isdir(install_dir):
                msg = "gcc has been existed,continuing next step"
                print msg
                self.logger.error(msg)
            else:
                self.decompress_soft('gcc')
                
                gmp_dir= "%s/gmp-%s" % (self.install_pardir,self.dependList['gmp']['version'])
                mpfr_dir = "%s/mpfr-%s" % (self.install_pardir,self.dependList['mpfr']['version'])
                mpc_dir = "%s/mpc-%s" % (self.install_pardir,self.dependList['mpc']['version'])
                enable_con = "--enable-shared --enable-threads=posix --enable-languages=c,c++,objc,obj-c++"
                cmd = "export LD_LIBRARY_PATH=%s/lib:%s/lib:%s/lib:$LD_LIBRARY_PATH" % (gmp_dir,mpfr_dir,mpc_dir)
                ret = self.get_exec_result(cmd)
                cmd = "./configure --prefix=%s --with-gmp=%s --with-mpfr=%s --with-mpc=%s %s" % (install_dir,gmp_dir,mpfr_dir,mpc_dir,enable_con)
                ret = self.get_exec_result(cmd)
                cmd = "make -j %s && make install" % self.process_num
                ret = self.get_exec_result(cmd)
                msg = "gcc installed ok,work done"
                print msg
                self.logger.debug(msg)
        except Exception,e:
            self.logger.error("install gcc error:%s",e)
            sys.exit(1)
    def print_start(self,obj):
        msg="%s install........[START]" % obj
        self.logger.debug(msg)
        print msg
    def print_end(self,obj):
        msg="%s install.......[DONE]" % obj
        self.logger.debug(msg)
        print msg
    def run_install(self):
        self.print_start('gmp')
        self.install_gmp()
        self.print_end('gmp')
        self.print_start('mpfr')
        self.install_mpfr()
        self.print_end('mpfr')
        self.print_start('mpc')
        self.install_mpc()
        self.print_end('mpc')
        self.print_start('gcc')
        self.install_gcc()
        self.print_end('gcc')
if __name__ == '__main__':
    obj = InstallGcc()
    obj.run_install()

 

posted @ 2016-09-06 15:58 ballwql 阅读(...) 评论(...) 编辑 收藏