scons实用万能编译脚本


#coding=utf-8
import os

#list all files, first files ,then dirs
def list_src_suffix(path,suffix):
    srcs=[]
    abspath = os.path.abspath(path)
    file_list = os.listdir(abspath)
    for src_file in file_list:
        src_file = os.path.join(abspath,src_file)
        if os.path.isfile(src_file):
#           if src_file.find(".c") != -1:
            if src_file.endswith("."+suffix) != 0:
                srcs.append(src_file)
    for src_path in file_list:
        src_path = os.path.join(abspath,src_path)
        if os.path.isdir(src_path):
            srcs.extend(list_src_suffix(src_path, suffix))
    return srcs

#in    
#def list_contains(list=[], target=""):
#    if len(list) == 1:
#        return list[0] == target
#    for item in list:
#        if item == target:
#            return True
#    return False

def list_src_except(path,suffix,excepts=[]):
    srcs=[]
    abspath = os.path.abspath(path)
    file_list = os.listdir(abspath)
    for src_file in file_list:
        if src_file in excepts:
            continue
        src_file = os.path.join(abspath,src_file)
        if os.path.isfile(src_file):
#           if src_file.find(".c") != -1:
            if src_file.endswith("."+suffix) != 0:
                srcs.append(src_file)
    for src_path in file_list:
        if src_path in excepts:
            continue
        src_path = os.path.join(abspath,src_path)
        if os.path.isdir(src_path):
            srcs.extend(list_src_except(src_path, suffix, excepts))
    return srcs

#list all include dir, first files ,then dirs
def list_inc_path(path,inc):
    incs=[]
    abspath = os.path.abspath(path)
    file_list = os.listdir(abspath)
    for src_file in file_list:
        src_file = os.path.join(abspath,src_file)
        if os.path.isfile(src_file):
            if src_file.endswith("."+inc) != 0:
                incs.append(abspath)
                break
    for src_path in file_list:
        src_path = os.path.join(abspath,src_path)
        if os.path.isdir(src_path):
            incs.extend(list_inc_path(src_path, inc))
    return incs

def list_inc_expect(path,inc,expects=[]):
    incs=[]
    abspath = os.path.abspath(path)
    file_list = os.listdir(abspath)
    for src_file in file_list:
        src_file = os.path.join(abspath,src_file)
        if os.path.isfile(src_file):
            if src_file.endswith("."+inc) != 0:
                incs.append(abspath)
                break
    for src_path in file_list:
        if src_path in expects:
            continue
        src_path = os.path.join(abspath,src_path)
        if os.path.isdir(src_path):
            incs.extend(list_inc_expect(src_path, inc, expects))
    return incs

def build_run(name,path):
    Program(target=name, CPPPATH=list_inc_path(path,"h"), source=list_src_suffix(path,"c"), CCFLAGS='-w')

def build_static(name,path):
    StaticLibrary(target=name, CPPPATH=list_inc_path(path,"h"), source=list_src_suffix(path,"c"), CCFLAGS='-w')

def build_shared(name,path):
    SharedLibrary(target=name, CPPPATH=list_inc_path(path,"h"), source=list_src_suffix(path,"c"), CCFLAGS='-w')

def build_select(type, name, path, excepts=[],ccflags=[]):
    if type == "program":
        print list_src_except(path,"c",excepts)
        Program(target=name, CPPPATH=list_inc_path(path,"h"), source=list_src_except(path,"c",excepts), CCFLAGS=ccflags)
    elif type == "static":
        StaticLibrary(target=name, CPPPATH=list_inc_path(path,"h"), source=list_src_except(path,"c",excepts), CCFLAGS=ccflags)
    elif type == "shared":
        SharedLibrary(target=name, CPPPATH=list_inc_path(path,"h"), source=list_src_except(path,"c",excepts), CCFLAGS=ccflags)
    else:
        print "invalid type !"

#build_static("CUnit","./")
build_select("program", "test", "./",[],["-w", "-DMEMTRACE", "-DCUNIT_BUILD_TESTS"])
#build_select("static", "cunit", "./",["test_cunit.c"],["-w", "-DMEMTRACE", "-DCUNIT_BUILD_TESTS"])