Skip to content

SCons:Environment

DUMP

환경변수 정보는 Dump('ENV')를 사용하여 확인할 수 있다.

env = Environment()
print env.Dump('ENV')

또는 아래와 같은 방법으로 확인 가능하다.

env = Environment()
print "CC is:", env['CC']

Clone env

아래와 같은 방법으로 클론을 만들 수 있다.

env = Environment(CC = 'gcc')
opt = env.Clone(CCFLAGS = '-O2')
dbg = env.Clone(CCFLAGS = '-g')

Alias

아래와 같이 Alias를 설정할 수 있다.

env = Environment()
p = env.Program('foo.c')
l = env.Library('bar.c')
env.Install('/usr/bin', p)
env.Install('/usr/lib', l)
ib = env.Alias('install-bin', '/usr/bin')
il = env.Alias('install-lib', '/usr/lib')
env.Alias('install', [ib, il])

Depends

종속성 설정은 아래와 같이 적용할 수 있다.

hello = Program('hello.c')
Depends(hello, 'other_file')

Adding PATH

env = Environment(ENV = os.environ)
env.PrependENVPath('PATH', '/usr/local/bin')
env.AppendENVPath('LIB', '/usr/local/lib')

Default Targets

env = Environment()
hello = env.Program('hello.c')
env.Program('goodbye.c')
Default(hello)

Ignoring Dependencies

hello = Program('hello.c', CPPPATH=['/usr/include'])
Ignore(hello, '/usr/include/stdio.h')

Command

env.Command('foo.out', 'foo.in',
            "$FOO_BUILD < $SOURCES > $TARGET")
## OR:
env.Command('bar.out', 'bar.in',
            ["rm -f $TARGET",
             "$BAR_BUILD < $SOURCES > $TARGET"],
            ENV = {'PATH' : '/usr/local/bin/'})
## OR:
def rename(env, target, source):
    import os
    os.rename('.tmp', str(target[0]))

env.Command('baz.out', 'baz.in',
            ["$BAZ_BUILD < $SOURCES > .tmp",
         rename ])

Custom Builders

bld = Builder(action = 'foobuild < $SOURCE > $TARGET')
env = Environment(BUILDERS = {'Foo' : bld})
env.Foo('file.foo', 'file.input')

실행결과는 아래와 같다.

$ scons -Q
foobuild < file.input > file.foo

Example

#!/usr/bin/python
env = Environment()
hello = env.Program('hello', 'hello.c')
env.Default(hello)
env.Append(BUILDERS={'CreateLog':
    Builder(action='$SOURCE.abspath > $TARGET', suffix='.log')})
log = env.CreateLog('hello', hello)
zipped_log = env.Zip('logs.zip', log)
env.Alias('cleanup', zipped_log)

Doxygen command

def setupDoxygenBuilder():
    env  = createEnvironment()
    name = 'doxygen'
    prefix  = 'src'
    config  = 'Doxyfile'
    source  = findWithSuffix(prefix, CPP_SUFFIX_LIST)
    target  = 'doxygen.log'
    output  = 'html'
    command = 'doxygen $SOURCE > $TARGET'

    env.Command(target, config, [command])
    env.Depends(target, source)
    env.Clean(target, [output])
    return env.Alias(name, target)

ctags command

def setupCtagsBuilder():
    env  = createEnvironment()
    name = 'ctags'
    target  = 'tags'
    prefix  = 'src'
    source  = findWithSuffix(prefix, CPP_SUFFIX_LIST)
    flags   = ' --c++-kinds=+p'
    flags  += ' --fields=+iaS'
    flags  += ' --extra=+q'
    flags  += ' --tag-relative=no'
    flags  += ' -f $TARGET'
    flags  += ' $SOURCES'
    command = 'ctags ' + flags

    env.Command(target, source, [command])
    return env.Alias(name, target)

cscope command

    env  = createEnvironment()
    name = 'cscope'
    files   = 'cscope.files'
    target  = 'cscope.out'
    prefix  = 'src'
    source  = findWithSuffix(prefix, CPP_SUFFIX_LIST)
    flags   = ' -b'
    flags  += ' -i $SOURCE'
    flags  += ' -f $TARGET'
    command = 'cscope ' + flags

    def create_files(env, target, source):
        result = str(target[0])
        if os.path.isfile(result):
            os.remove(result)
        with open(result, 'wb') as f:
            index = 0
            while index < len(source):
                f.write(str(source[index]) + '\n')
                index += 1

    env.Command(files, source, [create_files])
    env.Command(target, files, [command])
    return env.Alias(name, target)

See also

Favorite site