SCons - 为CUDA,CORBA等集成自定义构建器

时间:2015-11-28 18:18:05

标签: python c++ cuda scons corba

对于异构(C,C ++ 14,Fortran95,python3)项目,我目前正在评估CMake SCons将为CUDA(7.5)的平台编译器和编译器包装器的集成提供哪些优势? ,CORBA(omniorb-4.2.1),MPI2(MPICH)等。

首先,我创建了一个项目根目录(&#39; data / projects / snippets&#39;在附加的SConstruct脚本中),其中包含子目录&#39; model&#39;,in which the 'echo.idl' of the omniorb4 Documentation < / p>

interface Echo { 
    string echoString(in string mesg); 
};

驻留(echo_i.ccint main()生成omniidl将与-Wbexample开关一起生成,而子目录树包含/ cuda_samples_inc&#39; ,这是&#39; common / inc&#39;的副本。 CUDA工具包样本的分支,并复制了CUDA工具包样本

2_Graphics/simpleGL/simpleGL.cu 

进入项目根目录。

我自定义的SConstruct

import os

CORBA_PASS = False
CUDA_PASS = True

CUDA_FAIL = not CUDA_PASS
CORBA_FAIL = not CORBA_PASS

EXE_SUFFIX = None
if os.name != 'posix':
    raise NotImplementedError('Only on POSIX platforms yet')


PROJECT_ROOT = '/data/projects/snippets'  # your mileage will likely vary

INCLUDE_DIR = os.path.join(PROJECT_ROOT, 'include')
LIB_DIR = os.path.join(PROJECT_ROOT, 'lib')

SYS_LIBS = ['dl', 'pthread']
GL_LIBS = ['glut', 'GLEW', 'GL', 'GLU', 'X11']
CORBA_LIBS = ['omniORB4', 'omnithread']


class CUDAEnvironment(Environment):
    CUDA_ROOT = '/usr/local/cuda-7.5'
    CUDA_BIN_DIR = os.path.join(CUDA_ROOT, 'bin')
    CUDA_INCLUDE_DIRS = os.path.join(INCLUDE_DIR, 'cuda_samples_inc')
    GENC = "-gencode arch=compute_%d,code=sm_%d "
    GEN_RANGE = [20, 30, 35, 37, 50, 52]
    GENT = "-gencode arch=compute_52,code=compute_52"
    GENS = " ".join([GENC % (n, n) for n in GEN_RANGE]) + GENT

    COMPILE = """\
        %s/nvcc -ccbin g++ -I %s -m64 %s -o $TARGET -c $SOURCE \
    """ % (CUDA_BIN_DIR, CUDA_INCLUDE_DIRS, GENS)

    LINK = """\
        %s/nvcc -ccbin g++ -I %s -m64 %s -o $TARGET $SOURCE \
        -L%s, %s\
    """ % (CUDA_BIN_DIR, CUDA_INCLUDE_DIRS, GENS, LIB_DIR,
           ''.join([' -l%s' % (s,) for s in GL_LIBS]))

    def __init__(self):
        super(Environment, self).__init__()
        C = CUDAEnvironment  # class alias

        self.Append(PATH=":".join([os.environ['PATH'],
                                    C.CUDA_BIN_DIR]))

        cu2o = Builder(action=C.COMPILE, suffix='.o', src_suffix='.cu')
        o2exe = Builder(action=C.LINK, suffix=EXE_SUFFIX, src_suffix='.o')

        self.Append(BUILDERS={'CU2O': cu2o, 'O2EXE': o2exe})


if (CUDA_PASS):
    CUDA = CUDAEnvironment()
    CUDA.CU2O(['simpleGL.cu'])
    CUDA.O2EXE(['simpleGL.o'])

if (CUDA_FAIL):
    CUDA = CUDAEnvironment()
    o = CUDA.Object(['simpleGL.cu']) # line 63
    CUDA.Program(o)


class CORBAEnvironment(Environment):
    IDL2CC = """
        omniidl -bcxx -Wbexample $SOURCE
        cp `basename $SOURCE .idl`SK.cc `basename $SOURCE .idl`.cc
     """        
    COMPILE = """\
        g++ -std=c++14 -I. -Wall -pedantic %s $SOURCES -o $TARGET
    """ % (''.join([' -l%s' % (s,) for s in CORBA_LIBS + SYS_LIBS]))

    def __init__(self):
        super(Environment, self).__init__()
        C = CORBAEnvironment  # class alias

        idl2cc = Builder(
            action=C.IDL2CC, suffix='.cc', src_suffix='.idl'
        )

        cc2exe = Builder(
            action=C.COMPILE, suffix=EXE_SUFFIX, src_suffix='.cc'
        )

        self.Append(BUILDERS={'IDL2CC': idl2cc, 'CC2EXE': cc2exe})


if (CORBA_PASS):
    CORBA = CORBAEnvironment()
    CORBA.IDL2CC('echo.cc', os.path.join('model', 'echo.idl'))
    CORBA.CC2EXE('echo', ['echo.cc', 'echo_i.cc'])

if (CORBA_FAIL):
    CORBA = CORBAEnvironment()
    skel = CORBA.Object(os.path.join('model', 'echo.idl'))  # line 98
    impl = CORBA.Object('echo_i.cc')
    CORBA.Program([skel, impl])

适用于启用了CUDA_PASSCORBA_PASS的切换,但适用于

CUDA = CUDAEnvironment()
o = CUDA.Object(['simpleGL.cu']) # line 63
CUDA.Program(o)

RESP

CORBA = CORBAEnvironment()
skel = CORBA.Object(os.path.join('model', 'echo.idl'))  # line 98
impl = CORBA.Object('echo_i.cc')
CORBA.Program([skel, impl])
由于我害怕的那种错误,scons放弃了,

scons: *** While building `['simpleGL.o']' from `['simpleGL.cu']'
Don't know how to build from a source file with suffix `.cu'.  
Expected a suffix in this list: ['.c', '.m', '.cpp', '.cc', '.cxx', 
'.c++', '.C++', '.mm', '.C', '.f', '.for', '.ftn', '.fpp', '.FPP',
'.F', '.FOR', '.FTN', '.f77', '.F77', '.f90', '.F90', '.f95', '.F95',
'.f03', '.F03', '.s', '.asm', '.ASM', '.spp', '.SPP', '.sx', '.S'].
File "/data/projects/snippets/SConstruct", line 63, in <module>

RESP:

scons: *** While building `['model/echo.o']' from `['model/echo.idl']':
Don't know how to build from a source file with suffix `.idl'
[same as above]
File "/data/projects/snippets/SConstruct", line 98, in <module>.

可悲的是,

  

如何从带有后缀<suffix>

的源文件构建

正是我试图用我的自定义环境和构建器告诉scons。

所以我的问题是:

如何集成自定义环境和构建器,以便可以使用SCons Object()Program()构建器外观?

Scons Builder() doc,尤其是18.2. Attaching a Builder to a Construction Environment似乎只解释了我的PASS块中使用的语法以及传递Program()调用,但没有解释如何使用&#39;对象()&#39;了解自定义构建器。

毋庸置疑,python本身可以用来手动编写几乎所有类型的行为或附加组件,但我希望尽可能地保持在SCons工具集的范围内。

版本:

SCons 2.3.1(但是,如果对2.4.1的更新有帮助,那我就不太满意了,我会这样做)与python 2.7.9一起运行

1 个答案:

答案 0 :(得分:2)

SCons的概念是engine/SCons/Tool/qt.py。它是一个关键字(通常添加到Builder类的构造函数中),它通过首先运行其他构建器来通知系统如何创建实际的C / CPP源文件。 在ll中查看工具# register the builders env['BUILDERS']['Uic'] = uicBld env['BUILDERS']['Moc'] = mocBld static_obj, shared_obj = SCons.Tool.createObjBuilders(env) static_obj.add_src_builder('Uic') shared_obj.add_src_builder('Uic') 。 315它说:

add_src_builder

。通过调用*.uic,Uic构建器将分别向静态和共享对象构建器注册。然后,如果直接向Object发送*.uic文件,则SCons会检查其已定义的源构建器列表,将*.cc转换为其对应的^([a-zA-Z0-9._+-]+)(@[a-zA-Z0-9-.]+)(.[a-zA-Z]{2,4}){2,}$ 文件,然后通过后者在“对象”构建器上。