SCons
SCons 는 오픈 소스 소프트웨어 빌드 도구이다. SCons는 autoconf/automake의 기능과 ccache와 같은 컴파일러 캐시를 통합한, 고전적인 Make 유틸리티의 대체품이다. 이전의 도구들과 비교하여, SCons는 더 쓰기 쉽고, 더 신뢰할 수 있고, 더 빠른 것을 목표로 한다. SCons는 Python기반의 빌드툴이다.
Category
Command-line buind options
커맨드 라인에 scons debug=1
와 같이 입력하였을 때 GCC에 디버깅 옵션을 전달하는 방법은 아래와 같다.
env = Environment()
debug = ARGUMENTS.get('debug', 0)
if int(debug):
env.Append(CCFLAGS = '-g')
env.Program('prog.c')
Custom builder
Example
Cleaning Up After a Build
생성된 파일을 Clean하고 싶을 경우 명령줄 옵션에 -c
옵션을 추가하면 된다.
Print Dependencies Tree
아래와 같이 사용하면 종속성 트리를 출력할 수 있다.
아래와 같이 사용하면 준비 과정을 확인할 수 있다.
Include path setting
포함경로(INCLUDE PATH)는 CPPPATH
변수에 추가해야 한다.
참고로 Environment(CCFLAGS='-Iinc')
와 같이 플래그를 직접 입력하는 것은 추천하지 않는다.
use the Object
Object별 컴파일 옵션이 다를 경우 아래와 같이 사용하면 된다.
import os
env = Environment(ENV=os.environ, tools=['mingw'])
obj1 = env.Object('fun.o', 'fun.c', CC='gcc', CPPDEFINES='TEST2', CCFLAGS=['-l.', '-Winline'])
obj2 = env.Object('fun2.o', 'fun2.cpp')
obj3 = env.Object('main.o', 'main.cpp', CXX='g++', CPPDEFINES='TEST', CCFLAGS='-Winline', CXXFLAGS='-O2 -l.')
env.Program('result', source = [obj1, obj2, obj3])
Define setting
Define을 추가하고 싶다면 아래와 같이 추가하면 된다.
env = Environment()
env.Append(CPPDEFINES=['MAX(x,y)=(x>y ? x:y)'])
env.Program(target = 'main', source = 'main.cc')
위의 CPPPATH
와 마찬가지로 Environment(CCFLAGS='-Iinc')
와 같이 플래그를 직접 입력하는 것은 추천하지 않다. 그리고 오직 한 개만 추가하고 싶다면 AppendUnique
를 사용하면 된다.
참고로 Define들의 앞에 추가하고 싶다면 Prepend
또는 PrependUnique
를 사용하면 된다.
check for environment variables
# ---- check for environment variables
if 'CXX' in os.environ:
conf.env.Replace(CXX = os.environ['CXX'])
print(">> Using compiler " + os.environ['CXX'])
if 'CXXFLAGS' in os.environ:
conf.env.Append(CCFLAGS = os.environ['CXXFLAGS'])
print(">> Appending custom build flags : " + os.environ['CXXFLAGS'])
if 'LDFLAGS' in os.environ:
conf.env.Append(LINKFLAGS = os.environ['LDFLAGS'])
print(">> Appending custom link flags : " + os.environ['LDFLAGS'])
Explicit Dependencies
종속성을 명시할 경우 아래와 같이 적용한다.
또는
와 같이 적용한다.
Use PkgConfig
pkg-config를 적용할 경우 아래와 같이 사용하면 된다.
env = Environment()
# add support for GTK
env.ParseConfig('pkg-config --cflags --libs gtk+-2.0')
env.Program('toaster-test.c')
User define command
사용자 정의 명령어의 예제는 다음과 같다.
NVCC_CMD='nvcc -o $TARGET -c $SOURCE'
env = Environment(ENV=os.environ)
env.Command('gpu.o', 'gpu.cu', NVCC_CMD)
env.Program('result', source = ['gpu.o'])
Change suffix name
Suffix명(확장자)를 변경하고 싶을 경우 Replace
를 사용하면 된다. 이와 같이, soname을 변경하고 싶다면 아래와 같이 적용하면 된다.
Troubleshooting
SCons 사용중 발생할 수 있는 문제점에 대하여 정리한다.
Two environments with different actions were specified for the same target
아래와 같은 에러 메시지가 출력될 수 있다.
To avoid this problem, we must explicitly specify that each environment compile foo.c to a separately-named object file using the Object builder, like so:
opt = Environment(CCFLAGS = '-O2')
dbg = Environment(CCFLAGS = '-g')
o = opt.Object('foo-opt', 'foo.c')
opt.Program(o)
d = dbg.Object('foo-dbg', 'foo.c')
dbg.Program(d)
ImportError SCons.Script
scons를 실행했을 때 아래와 같은 에러가 발생할 경우가 있다.
Traceback (most recent call last):
File "/usr/local/bin/scons", line 188, in <module>
import SCons.Script
ImportError: No module named SCons.Script
이는 Python의 import가 정상적으로 경로를 못찾아 발생하는 문제이다.
만약 MAC OSX에서 위와같은 현상을 발견하면 아래의 경로를 추가하면 된다.
Is not compatible with shared target
빌드 도중 아래와 같은 메시지가 출력된다.
scons: *** Source file: mylib/libMine.a is static and is not compatible with shared target: libstuff.so
이 경우 SConstruct
파일의 SCons:Environment객체에 아래와 같이 설정하면 된다.
See also
Favorite site
Documentation
- SCons man page
- SCons user guide 1
- Scons Vs Other Build Tools
- Scons Tutorial 1
- Platform Specific Notes
- SCons User Guide 0.97 - Building and Linking with Libraries
Guide
- Build tool Scons (ko)
- How to get executable in debug mode using scons
- Integrating scons into IDEs
- Here are some tips for using SCons with Mac OSX
- SConstruct 사용법
- SCons / InstallTargets
- [추천] SCons / SconsProcessOverview
- [추천] SCons Design and Implementation
References
-
Scons.org-SCons_2.2.0_User_Guide.zip ↩