使用scons运行perl脚本(自动生成一系列文件),然后编译这些文件并创建静态库

0 投票
1 回答
612 浏览
提问于 2025-04-16 15:11

我正在把我们的项目从make迁移到scons,但遇到了一些问题。

我们有一些perl脚本,通过make运行这些脚本,生成一系列C++源文件。这些文件随后会被编译成一个静态库。

目前,我可以通过scons运行perl脚本,并用一些额外的python脚本编译这些文件,但我觉得应该有更简单的方法来做到这一点。

另外,我发现scons脚本的执行顺序似乎不是线性的,有些部分的执行顺序是错乱的。

这是我的scons脚本:

// SConscript file

import platform
import os
import glob
import time
Import('directEnv')
cohEnv = directEnv.Clone()

includePath = Split("""
#Direct/include
#Direct/libsrc/liblog
#Direct/libsrc/libtime
#tools/include
#Direct/include
#Direct/engine
""")

if platform.machine() == 'i686':
    includePath = includePath + ['#tools/coh-cpp-v3.6-linux-x32/coherence-cpp/include']

else:
    includePath = includePath + ['#tools/cohe-cpp-v3.5.3b465-linux-x64/coherence cpp/include']

cohFiles = Split("""
#Direct/include/IntApi.h 
#Direct/include/MessagingApiRisk.h 
#Direct/include/MessagingApiCommon.h
""")

cohEnv.Append(CPPPATH = includePath)
cohEnv.Append(CCFLAGS = '-D_FILE_OFFSET_BITS=64 -DUTPBRIDGE -Wno-unused-variable')
cohEnv.Append(LIBS = Split('nsl m rt'))

#
# Run Perl script - this generates approx 30 c++ source files
#

Clean('.', '#Direct/coh/cpp/CohMsgObj_0.cc')
temp1 = cohEnv.RunPerl('#Direct/coh/cpp/CohMsgObj_0.cc', '#Direct/coh/BuildCohObjs.pl')
Depends(temp1, '#Direct/coh/BuildCoherenceObjs.pl')
Depends(temp1, '#Direct/include/IntApi.h')

#
# Run Perl script - this generates 3 c++ source files
#

Clean('.', '#Direct/coh/cpp/Print_BinV4.cc')
temp2 = cohEnv.RunPerl('#Direct/coh/cpp/Print_BinV4.cc', '#Direct/coh/BuildCohObjsRisk.pl')
Depends(temp2, '#Direct/coh/BuildCohObjsRisk.pl')

#
# Build the object and library
#

print os.getcwd()
os.chdir('../../cpp')
path = os.getcwd()
print path

# 
# get all the c++ source files that we need to compile
#

List = []
for infile in glob.glob(os.path.join(path, '*.cc')):
    List.append(infile)

count = 0    
suffix = ".cc"
ObjectList = []

#
# create objects for each source file
# I'm trying to create variables dynamically - incase the files which generates the 
# source files change - I don;t want to manually list everything that needs to be compiled

for item in List:
    locals()['obj%s' % count] = coherenceEnv.Object(item[:-len(suffix)] + '.o' , item)
    print "obj%s" % count
    ObjectList.append(coherenceEnv.Object(item[:-len(suffix)] + '.o' , item))
    count = count + 1

#
# create a static library using the newly created objects
#

cohLib = cohEnv.StaticLibrary(target = 'riskpo', source = [cohFiles, ObjectList])
cohEnv.Install('#/lib', [cohLib])

现在这个方法可以工作,但远不是理想的状态。有没有更简单直接的方法来使用基本的scons命令?还有,我该如何在scons中强制执行执行顺序?谢谢!

1 个回答

1

在从Make转到SCons的过程中,人们通常会遇到一个问题,那就是在SCons中你不需要指定执行的顺序。你只需要告诉SCons哪些东西是由什么生成的,然后SCons会(通常)自动搞定顺序。

对于你的perl脚本,它们生成了很多.c文件,问题在于SCons并不知道这些生成的文件,因此找不到它们。这就意味着SCons不知道在使用这些输出之前要先运行perl脚本。

你应该看看用户指南,特别是关于发射器(emitters)这一部分,可能会对解决这个问题有帮助:http://scons.org/doc/production/HTML/scons-user/x3689.html

维基也是一个很好的例子来源。你可以看看这两个链接:http://scons.org/wiki/DynamicSourceGeneratorhttp://scons.org/wiki/ToolsForFools

这些可能会对你有帮助。

生成的C文件列表是静态的吗?或者它是否可以通过另一个文件的内容来确定?

撰写回答