Merge remote branch 'origin/master' into pipe-video
[mesa.git] / scons / custom.py
1 """custom
2
3 Custom builders and methods.
4
5 """
6
7 #
8 # Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
9 # All Rights Reserved.
10 #
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:
18 #
19 # The above copyright notice and this permission notice (including the
20 # next paragraph) shall be included in all copies or substantial portions
21 # of the Software.
22 #
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.
30 #
31
32
33 import os
34 import os.path
35 import re
36
37 import SCons.Action
38 import SCons.Builder
39 import SCons.Scanner
40
41 import fixes
42
43
44 def quietCommandLines(env):
45 # Quiet command lines
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['CODEGENCOMSTR'] = " Generating $TARGET ..."
60
61
62 def createConvenienceLibBuilder(env):
63 """This is a utility function that creates the ConvenienceLibrary
64 Builder in an Environment if it is not there already.
65
66 If it is already there, we return the existing one.
67
68 Based on the stock StaticLibrary and SharedLibrary builders.
69 """
70
71 try:
72 convenience_lib = env['BUILDERS']['ConvenienceLibrary']
73 except KeyError:
74 action_list = [ SCons.Action.Action("$ARCOM", "$ARCOMSTR") ]
75 if env.Detect('ranlib'):
76 ranlib_action = SCons.Action.Action("$RANLIBCOM", "$RANLIBCOMSTR")
77 action_list.append(ranlib_action)
78
79 convenience_lib = SCons.Builder.Builder(action = action_list,
80 emitter = '$LIBEMITTER',
81 prefix = '$LIBPREFIX',
82 suffix = '$LIBSUFFIX',
83 src_suffix = '$SHOBJSUFFIX',
84 src_builder = 'SharedObject')
85 env['BUILDERS']['ConvenienceLibrary'] = convenience_lib
86
87 return convenience_lib
88
89
90 # TODO: handle import statements with multiple modules
91 # TODO: handle from import statements
92 import_re = re.compile(r'^import\s+(\S+)$', re.M)
93
94 def python_scan(node, env, path):
95 # http://www.scons.org/doc/0.98.5/HTML/scons-user/c2781.html#AEN2789
96 contents = node.get_contents()
97 source_dir = node.get_dir()
98 imports = import_re.findall(contents)
99 results = []
100 for imp in imports:
101 for dir in path:
102 file = os.path.join(str(dir), imp.replace('.', os.sep) + '.py')
103 if os.path.exists(file):
104 results.append(env.File(file))
105 break
106 file = os.path.join(str(dir), imp.replace('.', os.sep), '__init__.py')
107 if os.path.exists(file):
108 results.append(env.File(file))
109 break
110 return results
111
112 python_scanner = SCons.Scanner.Scanner(function = python_scan, skeys = ['.py'])
113
114
115 def code_generate(env, script, target, source, command):
116 """Method to simplify code generation via python scripts.
117
118 http://www.scons.org/wiki/UsingCodeGenerators
119 http://www.scons.org/doc/0.98.5/HTML/scons-user/c2768.html
120 """
121
122 # We're generating code using Python scripts, so we have to be
123 # careful with our scons elements. This entry represents
124 # the generator file *in the source directory*.
125 script_src = env.File(script).srcnode()
126
127 # This command creates generated code *in the build directory*.
128 command = command.replace('$SCRIPT', script_src.path)
129 action = SCons.Action.Action(command, "$CODEGENCOMSTR")
130 code = env.Command(target, source, action)
131
132 # Explicitly mark that the generated code depends on the generator,
133 # and on implicitly imported python modules
134 path = (script_src.get_dir(),)
135 deps = [script_src]
136 deps += script_src.get_implicit_deps(env, python_scanner, path)
137 env.Depends(code, deps)
138
139 # Running the Python script causes .pyc files to be generated in the
140 # source directory. When we clean up, they should go too. So add side
141 # effects for .pyc files
142 for dep in deps:
143 pyc = env.File(str(dep) + 'c')
144 env.SideEffect(pyc, code)
145
146 return code
147
148
149 def createCodeGenerateMethod(env):
150 env.Append(SCANNERS = python_scanner)
151 env.AddMethod(code_generate, 'CodeGenerate')
152
153
154 def generate(env):
155 """Common environment generation code"""
156
157 if env.get('quiet', True):
158 quietCommandLines(env)
159
160 # Custom builders and methods
161 createConvenienceLibBuilder(env)
162 createCodeGenerateMethod(env)
163
164 # for debugging
165 #print env.Dump()
166
167
168 def exists(env):
169 return 1