3 Custom builders and methods.
8 # Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
11 # Permission is hereby granted, free of charge, to any person obtaining a
12 # copy of this software and associated documentation files (the
13 # "Software"), to deal in the Software without restriction, including
14 # without limitation the rights to use, copy, modify, merge, publish,
15 # distribute, sub license, and/or sell copies of the Software, and to
16 # permit persons to whom the Software is furnished to do so, subject to
17 # the following conditions:
19 # The above copyright notice and this permission notice (including the
20 # next paragraph) shall be included in all copies or substantial portions
23 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
24 # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
26 # IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
27 # ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
28 # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
29 # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
44 def quietCommandLines(env
):
46 # See also http://www.scons.org/wiki/HidingCommandLinesInOutput
47 env
['ASCOMSTR'] = " Assembling $SOURCE ..."
48 env
['ASPPCOMSTR'] = " Assembling $SOURCE ..."
49 env
['CCCOMSTR'] = " Compiling $SOURCE ..."
50 env
['SHCCCOMSTR'] = " Compiling $SOURCE ..."
51 env
['CXXCOMSTR'] = " Compiling $SOURCE ..."
52 env
['SHCXXCOMSTR'] = " Compiling $SOURCE ..."
53 env
['ARCOMSTR'] = " Archiving $TARGET ..."
54 env
['RANLIBCOMSTR'] = " Indexing $TARGET ..."
55 env
['LINKCOMSTR'] = " Linking $TARGET ..."
56 env
['SHLINKCOMSTR'] = " Linking $TARGET ..."
57 env
['LDMODULECOMSTR'] = " Linking $TARGET ..."
58 env
['SWIGCOMSTR'] = " Generating $TARGET ..."
59 env
['LEXCOMSTR'] = " Generating $TARGET ..."
60 env
['YACCCOMSTR'] = " Generating $TARGET ..."
61 env
['CODEGENCOMSTR'] = " Generating $TARGET ..."
62 env
['INSTALLSTR'] = " Installing $TARGET ..."
65 def createConvenienceLibBuilder(env
):
66 """This is a utility function that creates the ConvenienceLibrary
67 Builder in an Environment if it is not there already.
69 If it is already there, we return the existing one.
71 Based on the stock StaticLibrary and SharedLibrary builders.
75 convenience_lib
= env
['BUILDERS']['ConvenienceLibrary']
77 action_list
= [ SCons
.Action
.Action("$ARCOM", "$ARCOMSTR") ]
78 if env
.Detect('ranlib'):
79 ranlib_action
= SCons
.Action
.Action("$RANLIBCOM", "$RANLIBCOMSTR")
80 action_list
.append(ranlib_action
)
82 convenience_lib
= SCons
.Builder
.Builder(action
= action_list
,
83 emitter
= '$LIBEMITTER',
84 prefix
= '$LIBPREFIX',
85 suffix
= '$LIBSUFFIX',
86 src_suffix
= '$SHOBJSUFFIX',
87 src_builder
= 'SharedObject')
88 env
['BUILDERS']['ConvenienceLibrary'] = convenience_lib
90 return convenience_lib
93 # TODO: handle import statements with multiple modules
94 # TODO: handle from import statements
95 import_re
= re
.compile(r
'^import\s+(\S+)$', re
.M
)
97 def python_scan(node
, env
, path
):
98 # http://www.scons.org/doc/0.98.5/HTML/scons-user/c2781.html#AEN2789
99 contents
= node
.get_contents()
100 source_dir
= node
.get_dir()
101 imports
= import_re
.findall(contents
)
105 file = os
.path
.join(str(dir), imp
.replace('.', os
.sep
) + '.py')
106 if os
.path
.exists(file):
107 results
.append(env
.File(file))
109 file = os
.path
.join(str(dir), imp
.replace('.', os
.sep
), '__init__.py')
110 if os
.path
.exists(file):
111 results
.append(env
.File(file))
115 python_scanner
= SCons
.Scanner
.Scanner(function
= python_scan
, skeys
= ['.py'])
118 def code_generate(env
, script
, target
, source
, command
):
119 """Method to simplify code generation via python scripts.
121 http://www.scons.org/wiki/UsingCodeGenerators
122 http://www.scons.org/doc/0.98.5/HTML/scons-user/c2768.html
125 # We're generating code using Python scripts, so we have to be
126 # careful with our scons elements. This entry represents
127 # the generator file *in the source directory*.
128 script_src
= env
.File(script
).srcnode()
130 # This command creates generated code *in the build directory*.
131 command
= command
.replace('$SCRIPT', script_src
.path
)
132 action
= SCons
.Action
.Action(command
, "$CODEGENCOMSTR")
133 code
= env
.Command(target
, source
, action
)
135 # Explicitly mark that the generated code depends on the generator,
136 # and on implicitly imported python modules
137 path
= (script_src
.get_dir(),)
139 deps
+= script_src
.get_implicit_deps(env
, python_scanner
, path
)
140 env
.Depends(code
, deps
)
142 # Running the Python script causes .pyc files to be generated in the
143 # source directory. When we clean up, they should go too. So add side
144 # effects for .pyc files
146 pyc
= env
.File(str(dep
) + 'c')
147 env
.SideEffect(pyc
, code
)
152 def createCodeGenerateMethod(env
):
153 env
.Append(SCANNERS
= python_scanner
)
154 env
.AddMethod(code_generate
, 'CodeGenerate')
158 """Common environment generation code"""
160 if env
.get('quiet', True):
161 quietCommandLines(env
)
163 # Custom builders and methods
164 createConvenienceLibBuilder(env
)
165 createCodeGenerateMethod(env
)