blob: 82edb376d357e248ee97bac24e97e4220a5a5082 [file] [log] [blame]
Deepak Panickal9b35cf52014-07-01 17:57:19 +00001""" Post process SWIG Bridge wrapper code Python script for Windows/LINUX/OSX platform
2
Ed Masteae9d8412015-03-20 19:53:49 +00003 --------------------------------------------------------------------------
4 File: finishSwigWrapperClasses.py
Deepak Panickal9b35cf52014-07-01 17:57:19 +00005
Ed Masteae9d8412015-03-20 19:53:49 +00006 Overview: Python script(s) to finish off the SWIG Python C++ Script
7 Bridge wrapper code on the Windows/LINUX/OSX platform.
8 The Python scripts are equivalent to the shell script (.sh)
9 files.
10 We use SWIG to create a C++ file containing the appropriate
11 wrapper classes and functions for each scripting language,
12 before liblldb is built (thus the C++ file can be compiled
13 into liblldb. In some cases, additional work may need to be
14 done after liblldb has been compiled, to make the scripting
15 language stuff fully functional. Any such post-processing
16 is handled through the Python scripts called here.
Deepak Panickal9b35cf52014-07-01 17:57:19 +000017
Ed Masteae9d8412015-03-20 19:53:49 +000018 Environment: OS: Windows Vista or newer,LINUX,OSX.
19 IDE: Visual Studio 2013 Plugin Python Tools (PTVS)
20 Script: Python 2.6/2.7.5 x64
21 Other: None.
Deepak Panickal9b35cf52014-07-01 17:57:19 +000022
Ed Masteae9d8412015-03-20 19:53:49 +000023 Gotchas: None.
24
25 Copyright: None.
26 --------------------------------------------------------------------------
27
Deepak Panickal9b35cf52014-07-01 17:57:19 +000028"""
29
30# Python modules:
Ed Masteae9d8412015-03-20 19:53:49 +000031import sys # Provide argument parsing
32import os # Provide directory and file handling
Deepak Panickal9b35cf52014-07-01 17:57:19 +000033
34# Third party modules:
35
36# In-house modules:
Ed Masteae9d8412015-03-20 19:53:49 +000037import utilsArgsParse # Parse and validate this script's input arguments
38import utilsOsType # Determine the OS type this script is running on
39import utilsDebug # Debug Python scripts
Deepak Panickal9b35cf52014-07-01 17:57:19 +000040
41# Instantiations:
Ed Masteae9d8412015-03-20 19:53:49 +000042gbDbgVerbose = False; # True = Turn on script function tracing, False = off.
43gbDbgFlag = False; # Global debug mode flag, set by input parameter
44 # --dbgFlag. True = operate in debug mode.
45gbMakeFileFlag = False; # True = yes called from makefile system, False = not.
Deepak Panickal9b35cf52014-07-01 17:57:19 +000046
47# User facing text:
48strMsgErrorNoMain = "Program called by another Python script not allowed";
49strExitMsgSuccess = "Program successful";
50strExitMsgError = "Program error: ";
51strParameter = "Parameter: ";
52strMsgErrorOsTypeUnknown = "Unable to determine OS type"
53strScriptDirNotFound = "Unable to locate the script directory \'/script\'";
54strScriptLangsFound = "Found the following script languages:";
55strPostProcessError = "Executing \'%s\' post process script failed: ";
56strScriptNotFound = "Unable to locate the post process script file \'%s\' in \'%s\'";
57strScriptLangFound = "Found \'%s\' build script.";
58strScriptLangsFound = "Found the following script languages:";
59strExecuteMsg = "Executing \'%s\' build script...";
60strExecuteError = "Executing \'%s\' build script failed: ";
61strHelpInfo = "\
62Python script(s) to finish off the SWIG Python C++ Script \n\
63Bridge wrapper code on the Windows/LINUX/OSX platform. The Python \n\
64scripts are equivalent to the shell script (.sh) files \n\
65run on others platforms.\n\
Ed Masteae9d8412015-03-20 19:53:49 +000066Args: -h (optional) Print help information on this program.\n\
67 -d (optional) Determines whether or not this script\n\
68 outputs additional information when running.\n\
69 -m (optional) Specify called from Makefile system.\n\
70 --srcRoot= The root of the lldb source tree.\n\
71 --targetDir= Where the lldb framework/shared library gets put.\n\
72 --cfgBldDir= (optional) Where the build-swig-Python-LLDB.py program \n\
73 will put the lldb.py file it generated from running\n\
74 SWIG.\n\
75 --prefix= (optional) Is the root directory used to determine where\n\
76 third-party modules for scripting languages should\n\
77 be installed. Where non-Darwin systems want to put\n\
78 the .py and .so files so that Python can find them\n\
79 automatically. Python install directory.\n\
80 --cmakeBuildConfiguration= (optional) Is the build configuration(Debug, Release, RelWithDebugInfo)\n\
81 used to determine where the bin and lib directories are \n\
82 created for a Windows build.\n\
83 --argsFile= The args are read from a file instead of the\n\
84 command line. Other command line args are ignored.\n\
Deepak Panickal9b35cf52014-07-01 17:57:19 +000085\n\
86Usage:\n\
Ed Masteae9d8412015-03-20 19:53:49 +000087 finishSwigWrapperClasses.py --srcRoot=ADirPath --targetDir=ADirPath\n\
88 --cfgBldDir=ADirPath --prefix=ADirPath -m -d\n\
Deepak Panickal9b35cf52014-07-01 17:57:19 +000089\n\
90"; #TAG_PROGRAM_HELP_INFO
91
92#++---------------------------------------------------------------------------
Ed Masteae9d8412015-03-20 19:53:49 +000093# Details: Exit the program on success. Called on program successfully done
94# its work. Returns a status result to the caller.
95# Args: vnResult - (R) 0 or greater indicating success.
96# vMsg - (R) Success message if any to show success to user.
97# Returns: None.
98# Throws: None.
Deepak Panickal9b35cf52014-07-01 17:57:19 +000099#--
100def program_exit_success( vnResult, vMsg ):
Ed Masteae9d8412015-03-20 19:53:49 +0000101 strMsg = "";
102
103 if vMsg.__len__() == 0:
104 strMsg = "%s (%d)" % (strExitMsgSuccess, vnResult);
105 else:
106 strMsg = "%s: %s (%d)" % (strExitMsgSuccess, vMsg, vnResult);
107 print strMsg;
108
109 sys.exit( vnResult );
Deepak Panickal9b35cf52014-07-01 17:57:19 +0000110
111#++---------------------------------------------------------------------------
Ed Masteae9d8412015-03-20 19:53:49 +0000112# Details: Exit the program with error. Called on exit program failed its
113# task. Returns a status result to the caller.
114# Args: vnResult - (R) A negative number indicating error condition.
115# vMsg - (R) Error message to show to user.
116# Returns: None.
117# Throws: None.
Deepak Panickal9b35cf52014-07-01 17:57:19 +0000118#--
119def program_exit_on_failure( vnResult, vMsg ):
Ed Masteae9d8412015-03-20 19:53:49 +0000120 print "%s%s (%d)" % (strExitMsgError, vMsg, vnResult);
121 sys.exit( vnResult );
Deepak Panickal9b35cf52014-07-01 17:57:19 +0000122
123#++---------------------------------------------------------------------------
Ed Masteae9d8412015-03-20 19:53:49 +0000124# Details: Exit the program return a exit result number and print a message.
125# Positive numbers and zero are returned for success other error
126# occurred.
127# Args: vnResult - (R) A -ve (an error), 0 or +ve number (ok or status).
128# vMsg - (R) Error message to show to user.
129# Returns: None.
130# Throws: None.
Deepak Panickal9b35cf52014-07-01 17:57:19 +0000131#--
132def program_exit( vnResult, vMsg ):
Ed Masteae9d8412015-03-20 19:53:49 +0000133 if vnResult >= 0:
134 program_exit_success( vnResult, vMsg );
135 else:
136 program_exit_on_failure( vnResult, vMsg );
137
Deepak Panickal9b35cf52014-07-01 17:57:19 +0000138#++---------------------------------------------------------------------------
Ed Masteae9d8412015-03-20 19:53:49 +0000139# Details: Dump input parameters.
140# Args: vDictArgs - (R) Map of input args to value.
141# Returns: None.
142# Throws: None.
Deepak Panickal9b35cf52014-07-01 17:57:19 +0000143#--
144def print_out_input_parameters( vDictArgs ):
Ed Masteae9d8412015-03-20 19:53:49 +0000145 for arg, val in vDictArgs.iteritems():
146 strEqs = "";
147 strQ = "";
148 if val.__len__() != 0:
149 strEqs = " =";
150 strQ = "\"";
151 print "%s%s%s %s%s%s\n" % (strParameter, arg, strEqs, strQ, val, strQ);
Deepak Panickal9b35cf52014-07-01 17:57:19 +0000152
153#++---------------------------------------------------------------------------
Ed Masteae9d8412015-03-20 19:53:49 +0000154# Details: Validate the arguments passed to the program. This function exits
155# the program should error with the arguments be found.
156# Args: vArgv - (R) List of arguments and values.
157# Returns: Int - 0 = success, -ve = some failure.
158# Dict - Map of arguments names to argument values
159# Throws: None.
Deepak Panickal9b35cf52014-07-01 17:57:19 +0000160#--
161def validate_arguments( vArgv ):
Ed Masteae9d8412015-03-20 19:53:49 +0000162 dbg = utilsDebug.CDebugFnVerbose( "validate_arguments()" );
163 strMsg = "";
164 dictArgs = {};
165 nResult = 0;
166 strListArgs = "hdm"; # Format "hiox:" = -h -i -o -x <arg>
167 listLongArgs = ["srcRoot=", "targetDir=", "cfgBldDir=", "prefix=", "cmakeBuildConfiguration=",
168 "argsFile", "buildConfig="];
169 dictArgReq = { "-h": "o", # o = optional, m = mandatory
170 "-d": "o",
171 "-m": "o",
172 "--srcRoot": "m",
173 "--targetDir": "m",
174 "--cfgBldDir": "o",
175 "--buildConfig": "m",
176 "--prefix": "o",
177 "--cmakeBuildConfiguration": "o",
178 "--argsFile": "o" };
179
180 # Check for mandatory parameters
181 nResult, dictArgs, strMsg = utilsArgsParse.parse( vArgv, strListArgs,
182 listLongArgs,
183 dictArgReq,
184 strHelpInfo );
185 if nResult < 0:
186 program_exit_on_failure( nResult, strMsg );
187
188 # User input -h for help
189 if nResult == 1:
190 program_exit_success( 0, strMsg );
191
192 return (nResult, dictArgs);
Deepak Panickal9b35cf52014-07-01 17:57:19 +0000193
194#++---------------------------------------------------------------------------
Ed Masteae9d8412015-03-20 19:53:49 +0000195# Details: Locate post process script language directory and the script within
196# and execute.
197# Args: vStrScriptLang - (R) Name of the script language to build.
198# vstrFinishFileName - (R) Prefix file name to build full name.
199# vDictArgs - (R) Program input parameters.
200# Returns: Int - 0 = Success, < 0 some error condition.
201# Str - Error message.
202# Throws: None.
Deepak Panickal9b35cf52014-07-01 17:57:19 +0000203#--
204def run_post_process( vStrScriptLang, vstrFinishFileName, vDictArgs ):
Ed Masteae9d8412015-03-20 19:53:49 +0000205 dbg = utilsDebug.CDebugFnVerbose( "run_post_process()" );
206 nResult = 0;
207 strStatusMsg = "";
208 strScriptFile = vstrFinishFileName % vStrScriptLang;
209 strScriptFileDir = "%s%s/%s" % (vDictArgs[ "--srcRoot" ], "/scripts",
210 vStrScriptLang);
211 strScriptFilePath = "%s/%s" % (strScriptFileDir, strScriptFile);
212
213 # Check for the existence of the script file
214 strPath = os.path.normcase( strScriptFilePath );
215 bOk = os.path.exists( strPath );
216 if bOk == False:
217 strDir = os.path.normcase( strScriptFileDir );
218 strStatusMsg = strScriptNotFound % (strScriptFile, strDir);
219 return (-9, strStatusMsg);
220
221 if gbDbgFlag:
222 print strScriptLangFound % vStrScriptLang;
223 print strExecuteMsg % vStrScriptLang;
224
225 # Change where Python looks for our modules
226 strDir = os.path.normcase( strScriptFileDir );
227 sys.path.append( strDir );
228
229 # Execute the specific language script
230 dictArgs = vDictArgs; # Remove any args not required before passing on
231 strModuleName = strScriptFile[ : strScriptFile.__len__() - 3 ];
232 module = __import__( strModuleName );
233 nResult, strStatusMsg = module.main( dictArgs );
234
235 # Revert sys path
236 sys.path.remove( strDir );
237
238 return (nResult, strStatusMsg);
Deepak Panickal9b35cf52014-07-01 17:57:19 +0000239
240#++---------------------------------------------------------------------------
Ed Masteae9d8412015-03-20 19:53:49 +0000241# Details: Step through each script language sub directory supported
242# and execute post processing script for each scripting language,
243# make sure the build script for that language exists.
244# For now the only language we support is Python, but we expect this
245# to change.
246# Args: vDictArgs - (R) Program input parameters.
247# Returns: Int - 0 = Success, < 0 some error condition.
248# Str - Error message.
249# Throws: None.
Deepak Panickal9b35cf52014-07-01 17:57:19 +0000250#--
251def run_post_process_for_each_script_supported( vDictArgs ):
Ed Masteae9d8412015-03-20 19:53:49 +0000252 dbg = utilsDebug.CDebugFnVerbose( "run_post_process_for_each_script_supported()" );
253 nResult = 0;
254 strStatusMsg = "";
255 strScriptDir = vDictArgs[ "--srcRoot" ] + "/scripts";
256 strFinishFileName = "finishSwig%sLLDB.py";
257
258 # Check for the existence of the scripts folder
259 strScriptsDir = os.path.normcase( strScriptDir );
260 bOk = os.path.exists( strScriptsDir );
261 if bOk == False:
262 return (-8, strScriptDirNotFound);
263
264 # Look for any script language directories to build for
265 listDirs = [];
266 nDepth = 1;
267 for strPath, listDirs, listFiles in os.walk( strScriptDir ):
268 nDepth = nDepth - 1;
269 if nDepth == 0:
270 break;
271
272 if gbDbgFlag:
273 print strScriptLangsFound,
274 for dir in listDirs:
275 print dir,
276 print "\n";
277
278 # Iterate script directory find any script language directories
279 for scriptLang in listDirs:
280 dbg.dump_text( "Executing language script for \'%s\'" % scriptLang );
281 nResult, strStatusMsg = run_post_process( scriptLang, strFinishFileName,
282 vDictArgs );
283 if nResult < 0:
284 break;
285
286 if nResult < 0:
287 strTmp = strPostProcessError % scriptLang;
288 strTmp += strStatusMsg;
289 strStatusMsg = strTmp;
290
291 return (nResult, strStatusMsg);
Deepak Panickal9b35cf52014-07-01 17:57:19 +0000292
293#++---------------------------------------------------------------------------
Ed Masteae9d8412015-03-20 19:53:49 +0000294# Details: Program's main() with arguments passed in from the command line.
295# Program either exits normally or with error from this function -
296# top most level function.
297# Args: vArgv - (R) List of arguments and values.
298# Returns: None
299# Throws: None.
Deepak Panickal9b35cf52014-07-01 17:57:19 +0000300#--
301def main( vArgv ):
Ed Masteae9d8412015-03-20 19:53:49 +0000302 dbg = utilsDebug.CDebugFnVerbose( "main()" );
303 bOk = False;
304 dictArgs = {};
305 nResult = 0;
306 strMsg = "";
Deepak Panickal9b35cf52014-07-01 17:57:19 +0000307
Ed Masteae9d8412015-03-20 19:53:49 +0000308 # The validate arguments fn will exit the program if tests fail
309 nResult, dictArgs = validate_arguments( vArgv );
Deepak Panickal9b35cf52014-07-01 17:57:19 +0000310
Ed Masteae9d8412015-03-20 19:53:49 +0000311 eOSType = utilsOsType.determine_os_type();
312 if eOSType == utilsOsType.EnumOsType.Unknown:
313 program_exit( -4, strMsgErrorOsTypeUnknown );
314
315 global gbDbgFlag;
316 gbDbgFlag = dictArgs.has_key( "-d" );
317 if gbDbgFlag:
318 print_out_input_parameters( dictArgs );
319
320 # Check to see if we were called from the Makefile system. If we were, check
321 # if the caller wants SWIG to generate a dependency file.
322 # Not used in this program, but passed through to the language script file
323 # called by this program
324 global gbMakeFileFlag;
325 gbMakeFileFlag = dictArgs.has_key( "-m" );
326
327 nResult, strMsg = run_post_process_for_each_script_supported( dictArgs );
328
329 program_exit( nResult, strMsg );
Deepak Panickal9b35cf52014-07-01 17:57:19 +0000330
331#-----------------------------------------------------------------------------
332#-----------------------------------------------------------------------------
333#-----------------------------------------------------------------------------
334
335#TAG_PROGRAM_HELP_INFO
Ed Masteae9d8412015-03-20 19:53:49 +0000336""" Details: Program main entry point.
Deepak Panickal9b35cf52014-07-01 17:57:19 +0000337
Ed Masteae9d8412015-03-20 19:53:49 +0000338 --------------------------------------------------------------------------
339 Args: -h (optional) Print help information on this program.
340 -d (optional) Determines whether or not this script
341 outputs additional information when running.
342 -m (optional) Specify called from Makefile system. If given locate
343 the LLDBWrapPython.cpp in --srcRoot/source folder
344 else in the --targetDir folder.
345 --srcRoot= The root of the lldb source tree.
346 --targetDir= Where the lldb framework/shared library gets put.
347 --cfgBldDir= Where the buildSwigPythonLLDB.py program will
348 (optional) put the lldb.py file it generated from running
349 SWIG.
350 --prefix= Is the root directory used to determine where
351 (optional) third-party modules for scripting languages should
352 be installed. Where non-Darwin systems want to put
353 the .py and .so files so that Python can find them
354 automatically. Python install directory.
355 --cmakeBuildConfiguration= (optional) Is the build configuration(Debug, Release, RelWithDebugInfo)\n\
356 used to determine where the bin and lib directories are \n\
357 created for a Windows build.\n\
358 --argsFile= The args are read from a file instead of the
359 command line. Other command line args are ignored.
360 Usage:
361 finishSwigWrapperClasses.py --srcRoot=ADirPath --targetDir=ADirPath
362 --cfgBldDir=ADirPath --prefix=ADirPath -m -d
363
364 Results: 0 Success
365 -1 Error - invalid parameters passed.
366 -2 Error - incorrect number of mandatory parameters passed.
367
368 -4 Error - unable to determine OS type.
369 -5 Error - program not run with name of "__main__".
370 -8 Error - unable to locate the scripts folder.
371 -9 Error - unable to locate the post process language script
372 file.
373
374 -100+ - Error messages from the child language script file.
375
376 --------------------------------------------------------------------------
377
Deepak Panickal9b35cf52014-07-01 17:57:19 +0000378"""
379
380# Called using "__main__" when not imported i.e. from the command line
381if __name__ == "__main__":
Ed Masteae9d8412015-03-20 19:53:49 +0000382 utilsDebug.CDebugFnVerbose.bVerboseOn = gbDbgVerbose;
383 dbg = utilsDebug.CDebugFnVerbose( "__main__" );
384 main( sys.argv[ 1: ] );
Deepak Panickal9b35cf52014-07-01 17:57:19 +0000385else:
Ed Masteae9d8412015-03-20 19:53:49 +0000386 program_exit( -5, strMsgErrorNoMain );
387