SCons构建分层目录结构

时间:2014-06-15 07:57:39

标签: python build scons build-script

目录结构:

WorkSpace/root_dir
|_ SConstruct
|_ multipy
  |_ include
    |_ mul.h
  |_ src
    |_ mul.cpp
    |_ SConscript
|_ sum
  |_ include
    |_ sum.h
  |_ src
    |_ sum.cpp
    |_ SConscript
|_ src_main
  |_ main.cpp
  |_ SConscript

root_dir / SConstruct的内容:

# This Sconstruct file should build:
# Static and Shared libraries of sum and mul for both release(build/release) and debug mode(build/debug) and place it into the directory as mentioned in parenthesis
# Finally build executable for main.cpp by linking it with sum and mul libraries
# and place it into src_main/build/release and src_main/build/debug directories
import os

env = Environment()
print 'env = ', env

debug_env   = env.Clone(CCFLAGS = '-g')
release_env = env.Clone(CCFLAGS = '-O2')

SConscript('sum/src/SConscript', variant_dir='sum/build/debug/', exports ={'env' : debug_env}, duplicate=0)
SConscript('sum/src/SConscript', variant_dir='sum/build/release/', exports = {'env' : release_env}, duplicate=0)
SConscript('multiply/src/SConscript', variant_dir='multiply/build/debug/', exports ={'env' : debug_env}, duplicate=0)
SConscript('multiply/src/SConscript', variant_dir='multiply/build/release/', exports = {'env' : release_env}, duplicate=0)

sum / src / SConscript文件的内容:

# The main purpose of this SConscript file is to create static and shared library version
# for release and debug mode
# It should also creates objects and place it into build/release and build/debug 
# directories inside multiply directory
import os
Import('env')

libs = ['libsum.a', 'libsum.so']

# list of source files
src_list = Glob(os.path.join(Dir('#').abspath, 'sum', 'src', '*.cpp'))

# Path of included files ( - not sure whether it's the correct way to do in scons ?)
env['CPPPATH'] = ['#/sum/include']

env.StaticLibrary(target = 'sum', source = src_list)
env.SharedLibrary(target = 'sum', source = src_list)

Return('libs')

类似的SConscript文件存在于multiply目录中,其目的与 sum / src / SConscript 文件的注释中提到的相同。

对于src_main / SConscript:

import os
import ('env')

Program('main.cpp', LIBS=['sum', 'mul'], LIBPATHS=[])  # How should I mention LIBPATHS here ?

运行%scons -Q

g++ -o multiply/src/mul.o -c -g -Imultiply/include multiply/src/mul.cpp
ar rc multiply/build/debug/libmul.a multiply/src/mul.o
ranlib multiply/build/debug/libmul.a
g++ -o multiply/src/mul.os -c -g -fPIC -Imultiply/include multiply/src/mul.cpp
g++ -o multiply/build/debug/libmul.so -shared multiply/src/mul.os
ar rc multiply/build/release/libmul.a multiply/src/mul.o
ranlib multiply/build/release/libmul.a
g++ -o multiply/build/release/libmul.so -shared multiply/src/mul.os
g++ -o sum/src/sum.o -c -g -Imultiply/include sum/src/sum.cpp
sum/src/sum.cpp:1:17: fatal error: sum.h: No such file or directory
compilation terminated.
scons: *** [sum/src/sum.o] Error 1

我无法理解为什么在创建sum.o时使用了-Imultiply / include,虽然对于sum SCOnscript文件我使用了 env ['CPPPATH'] = ['#/ sum / include'] - >它被视为我在multiply / src / SConscript文件中使用的类似语句(即 env ['CPPPATH'] = ['#/ multiply / include']

我是scons构建工具的新用户,我无法做这种简单的例子,这是一个非常普遍的实际场景。

请为我提供必要的详细信息以修复我的错误,并在我的方法中添加优化以满足要求/目的。

2 个答案:

答案 0 :(得分:0)

您需要提供SConscript文件的路径

SConscript('multiply/src/SConscript ', variant_dir='build/debug/', export={'env':debug_env}, duplicate=0)
SConscript('multiply/src/SConscript ', variant_dir='build/release/', export={'env':release_env}, duplicate=0)

并且您还需要在要包含的任何其他SConscript文件上调用SConscript()

答案 1 :(得分:0)

创建sum.o时使用了

-Imultiply / include,因为在包含的SConscript中有以下内容

env['CPPPATH'] = ['#/sum/include']