OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/python2.4 |
| 2 # Copyright 2009, Google Inc. |
| 3 # All rights reserved. |
| 4 # |
| 5 # Redistribution and use in source and binary forms, with or without |
| 6 # modification, are permitted provided that the following conditions are |
| 7 # met: |
| 8 # |
| 9 # * Redistributions of source code must retain the above copyright |
| 10 # notice, this list of conditions and the following disclaimer. |
| 11 # * Redistributions in binary form must reproduce the above |
| 12 # copyright notice, this list of conditions and the following disclaimer |
| 13 # in the documentation and/or other materials provided with the |
| 14 # distribution. |
| 15 # * Neither the name of Google Inc. nor the names of its |
| 16 # contributors may be used to endorse or promote products derived from |
| 17 # this software without specific prior written permission. |
| 18 # |
| 19 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 22 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 23 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 25 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 26 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 27 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 29 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 |
| 31 |
| 32 """Docbuilder for O3D and o3djs.""" |
| 33 |
| 34 |
| 35 import os |
| 36 import os.path |
| 37 import sys |
| 38 import imp |
| 39 import types |
| 40 import glob |
| 41 import subprocess |
| 42 |
| 43 _java_exe = '' |
| 44 _script_path = os.path.dirname(os.path.realpath(__file__)) |
| 45 |
| 46 GlobalsDict = { } |
| 47 |
| 48 |
| 49 def MakePath(file_path): |
| 50 """Makes a path absolute given a path relativel to this script.""" |
| 51 return os.path.join(_script_path, file_path) |
| 52 |
| 53 |
| 54 def UpdateGlobals(dict): |
| 55 """Copies pairs from dict into GlobalDict.""" |
| 56 for i, v in dict.items(): |
| 57 GlobalsDict.__setitem__(i, v) |
| 58 |
| 59 |
| 60 def GetCallingNamespaces(): |
| 61 """Return the locals and globals for the function that called |
| 62 into this module in the current call stack.""" |
| 63 try: 1/0 |
| 64 except ZeroDivisionError: |
| 65 # Don't start iterating with the current stack-frame to |
| 66 # prevent creating reference cycles (f_back is safe). |
| 67 frame = sys.exc_info()[2].tb_frame.f_back |
| 68 |
| 69 # Find the first frame that *isn't* from this file |
| 70 while frame.f_globals.get("__name__") == __name__: |
| 71 frame = frame.f_back |
| 72 |
| 73 return frame.f_locals, frame.f_globals |
| 74 |
| 75 |
| 76 def ComputeExports(exports): |
| 77 """Compute a dictionary of exports given one of the parameters |
| 78 to the Export() function or the exports argument to SConscript().""" |
| 79 |
| 80 loc, glob = GetCallingNamespaces() |
| 81 |
| 82 retval = {} |
| 83 try: |
| 84 for export in exports: |
| 85 if isinstance(export, types.DictType): |
| 86 retval.update(export) |
| 87 else: |
| 88 try: |
| 89 retval[export] = loc[export] |
| 90 except KeyError: |
| 91 retval[export] = glob[export] |
| 92 except KeyError, x: |
| 93 raise Error, "Export of non-existent variable '%s'"%x |
| 94 |
| 95 return retval |
| 96 |
| 97 |
| 98 def Export(*vars): |
| 99 """Copies the named variables to GlobalDict.""" |
| 100 for var in vars: |
| 101 UpdateGlobals(ComputeExports(vars)) |
| 102 |
| 103 |
| 104 def Import(filename): |
| 105 """Imports a python file in a scope with 'Export' defined.""" |
| 106 scope = {'__builtins__': globals()['__builtins__'], |
| 107 'Export': Export} |
| 108 file = open(filename, 'r') |
| 109 exec file in scope |
| 110 file.close() |
| 111 |
| 112 |
| 113 def Execute(args): |
| 114 """Executes an external program.""" |
| 115 # Comment the next line in for debugging. |
| 116 # print "Execute: ", ' '.join(args) |
| 117 if subprocess.call(args) > 0: |
| 118 raise RuntimeError('FAILED: ' + ' '.join(args)) |
| 119 |
| 120 |
| 121 def AppendBasePath(folder, filenames): |
| 122 """Appends a base path to a ist of files""" |
| 123 return [os.path.join(folder, filename) for filename in filenames] |
| 124 |
| 125 |
| 126 def RunNixysa(idl_files, generate, output_dir, nixysa_options): |
| 127 """Executes Nixysa.""" |
| 128 python_exe = 'python' |
| 129 Execute([ |
| 130 python_exe, |
| 131 MakePath('../third_party/nixysa/files/codegen.py'), |
| 132 '--binding-module=o3d:%s' % MakePath('../plugin/o3d_binding.py'), |
| 133 '--generate=' + generate, |
| 134 '--force', |
| 135 '--output-dir=' + output_dir] + |
| 136 nixysa_options + |
| 137 idl_files) |
| 138 |
| 139 |
| 140 def RunJSDocToolkit(js_files, output_dir, prefix): |
| 141 """Executes the JSDocToolkit.""" |
| 142 list_filename = MakePath('../scons-out/docs/obj/doclist.conf') |
| 143 f = open(list_filename, 'w') |
| 144 f.write('{\n_: [\n') |
| 145 for filename in js_files: |
| 146 f.write('"%s",\n' % filename.replace('\\', '/')) |
| 147 f.write(']\n}\n') |
| 148 f.close() |
| 149 |
| 150 Execute([ |
| 151 _java_exe, |
| 152 '-Djsdoc.dir=%s' % MakePath('../third_party/jsdoctoolkit/files'), |
| 153 '-jar', |
| 154 MakePath('../third_party/jsdoctoolkit/files/jsrun.jar'), |
| 155 MakePath('../third_party/jsdoctoolkit/files/app/run.js'), |
| 156 '-D="prefix:%s"' % prefix, |
| 157 '-v', |
| 158 '-t=%s' % MakePath('./jsdoc-toolkit-templates//'), |
| 159 '-d=' + output_dir, |
| 160 '-c=' + list_filename]) |
| 161 |
| 162 |
| 163 def BuildJavaScriptForDocsFromIDLs(idl_files, output_dir): |
| 164 RunNixysa(idl_files, 'jsheader', output_dir, []) |
| 165 |
| 166 |
| 167 def BuildJavaScriptForExternsFromIDLs(idl_files, output_dir): |
| 168 if (os.path.exists(output_dir)): |
| 169 for filename in glob.glob(os.path.join(output_dir, '*.js')): |
| 170 os.unlink(filename) |
| 171 RunNixysa(idl_files, 'jsheader', output_dir, ['--no-return-docs']) |
| 172 |
| 173 |
| 174 def BuildO3DDocsFromJavaScript(js_files, output_dir): |
| 175 RunJSDocToolkit(js_files, output_dir, 'classo3d_1_1_') |
| 176 |
| 177 |
| 178 def BuildO3DExternsFile(js_files_dir, extra_externs_file, externs_file): |
| 179 outfile = open(externs_file, 'w') |
| 180 filenames = (glob.glob(os.path.join(js_files_dir, '*.js')) + |
| 181 [extra_externs_file]) |
| 182 for filename in filenames: |
| 183 infile = open(filename, 'r') |
| 184 outfile.write(infile.read()) |
| 185 infile.close() |
| 186 outfile.close() |
| 187 |
| 188 |
| 189 def BuildCompiledO3DJS(o3djs_files, |
| 190 externs_path, |
| 191 o3d_externs_js_path, |
| 192 compiled_o3djs_outpath): |
| 193 Execute([ |
| 194 _java_exe, |
| 195 '-jar', |
| 196 MakePath('JSCompiler_deploy.jar'), |
| 197 '--externs=%s' % externs_path, |
| 198 ('--externs=%s' % o3d_externs_js_path), |
| 199 ('--js_output_file=%s' % compiled_o3djs_outpath)] + |
| 200 ['-js=%s' % (x, ) for x in o3djs_files]); |
| 201 |
| 202 |
| 203 def main(): |
| 204 """Builds the O3D API docs and externs and the o3djs docs.""" |
| 205 global _java_exe |
| 206 _java_exe = sys.argv[1] |
| 207 |
| 208 js_list_filename = MakePath('../samples/o3djs/js_list.scons') |
| 209 idl_list_filename = MakePath('../plugin/idl_list.scons') |
| 210 js_list_basepath = os.path.dirname(js_list_filename) |
| 211 idl_list_basepath = os.path.dirname(idl_list_filename) |
| 212 |
| 213 docs_js_outpath = MakePath('../scons-out/docs/obj/documentation/apijs') |
| 214 externs_js_outpath = MakePath('../scons-out/docs/obj/externs') |
| 215 o3d_docs_html_outpath = MakePath('../scons-out/docs/obj/documentation/html') |
| 216 o3d_externs_path = MakePath('../scons-out/docs/obj/o3d-externs.js') |
| 217 compiled_o3djs_outpath = MakePath( |
| 218 '../scons-out/docs/obj/documentation/base.js') |
| 219 externs_path = MakePath('externs/externs.js') |
| 220 o3d_extra_externs_path = MakePath('externs/o3d-extra-externs.js') |
| 221 |
| 222 Import(js_list_filename) |
| 223 Import(idl_list_filename) |
| 224 |
| 225 idl_files = AppendBasePath(idl_list_basepath, GlobalsDict['O3D_IDL_SOURCES']) |
| 226 o3djs_files = AppendBasePath(js_list_basepath, GlobalsDict['O3D_JS_SOURCES']) |
| 227 |
| 228 # we need to put base.js first? |
| 229 o3djs_files = ( |
| 230 filter(lambda x: x.endswith('base.js'), o3djs_files) + |
| 231 filter(lambda x: not x.endswith('base.js'), o3djs_files)) |
| 232 |
| 233 docs_js_files = [os.path.join( |
| 234 docs_js_outpath, |
| 235 os.path.splitext(os.path.basename(f))[0] + '.js') |
| 236 for f in GlobalsDict['O3D_IDL_SOURCES']] |
| 237 |
| 238 BuildJavaScriptForDocsFromIDLs(idl_files, docs_js_outpath) |
| 239 BuildO3DDocsFromJavaScript([o3d_extra_externs_path] + docs_js_files, |
| 240 o3d_docs_html_outpath) |
| 241 BuildJavaScriptForExternsFromIDLs(idl_files, externs_js_outpath) |
| 242 BuildO3DExternsFile(externs_js_outpath, |
| 243 o3d_extra_externs_path, |
| 244 o3d_externs_path) |
| 245 BuildCompiledO3DJS(o3djs_files, |
| 246 externs_path, |
| 247 o3d_externs_path, |
| 248 compiled_o3djs_outpath) |
| 249 |
| 250 |
| 251 if __name__ == '__main__': |
| 252 main() |
| 253 |
OLD | NEW |