blob: 67cc95a4c448ca90a9bb8fc21d10be892e1ab50d [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
Ed Masteb933fb22015-03-20 19:59:35 +0000103 if vMsg.__len__() != 0:
Ed Masteae9d8412015-03-20 19:53:49 +0000104 strMsg = "%s: %s (%d)" % (strExitMsgSuccess, vMsg, vnResult);
Ed Masteb933fb22015-03-20 19:59:35 +0000105 print strMsg;
Ed Masteae9d8412015-03-20 19:53:49 +0000106
107 sys.exit( vnResult );
Deepak Panickal9b35cf52014-07-01 17:57:19 +0000108
109#++---------------------------------------------------------------------------
Ed Masteae9d8412015-03-20 19:53:49 +0000110# Details: Exit the program with error. Called on exit program failed its
111# task. Returns a status result to the caller.
112# Args: vnResult - (R) A negative number indicating error condition.
113# vMsg - (R) Error message to show to user.
114# Returns: None.
115# Throws: None.
Deepak Panickal9b35cf52014-07-01 17:57:19 +0000116#--
117def program_exit_on_failure( vnResult, vMsg ):
Ed Masteae9d8412015-03-20 19:53:49 +0000118 print "%s%s (%d)" % (strExitMsgError, vMsg, vnResult);
119 sys.exit( vnResult );
Deepak Panickal9b35cf52014-07-01 17:57:19 +0000120
121#++---------------------------------------------------------------------------
Ed Masteae9d8412015-03-20 19:53:49 +0000122# Details: Exit the program return a exit result number and print a message.
123# Positive numbers and zero are returned for success other error
124# occurred.
125# Args: vnResult - (R) A -ve (an error), 0 or +ve number (ok or status).
126# vMsg - (R) Error message to show to user.
127# Returns: None.
128# Throws: None.
Deepak Panickal9b35cf52014-07-01 17:57:19 +0000129#--
130def program_exit( vnResult, vMsg ):
Ed Masteae9d8412015-03-20 19:53:49 +0000131 if vnResult >= 0:
132 program_exit_success( vnResult, vMsg );
133 else:
134 program_exit_on_failure( vnResult, vMsg );
135
Deepak Panickal9b35cf52014-07-01 17:57:19 +0000136#++---------------------------------------------------------------------------
Ed Masteae9d8412015-03-20 19:53:49 +0000137# Details: Dump input parameters.
138# Args: vDictArgs - (R) Map of input args to value.
139# Returns: None.
140# Throws: None.
Deepak Panickal9b35cf52014-07-01 17:57:19 +0000141#--
142def print_out_input_parameters( vDictArgs ):
Ed Masteae9d8412015-03-20 19:53:49 +0000143 for arg, val in vDictArgs.iteritems():
144 strEqs = "";
145 strQ = "";
146 if val.__len__() != 0:
147 strEqs = " =";
148 strQ = "\"";
149 print "%s%s%s %s%s%s\n" % (strParameter, arg, strEqs, strQ, val, strQ);
Deepak Panickal9b35cf52014-07-01 17:57:19 +0000150
151#++---------------------------------------------------------------------------
Ed Masteae9d8412015-03-20 19:53:49 +0000152# Details: Validate the arguments passed to the program. This function exits
153# the program should error with the arguments be found.
154# Args: vArgv - (R) List of arguments and values.
155# Returns: Int - 0 = success, -ve = some failure.
156# Dict - Map of arguments names to argument values
157# Throws: None.
Deepak Panickal9b35cf52014-07-01 17:57:19 +0000158#--
159def validate_arguments( vArgv ):
Ed Masteae9d8412015-03-20 19:53:49 +0000160 dbg = utilsDebug.CDebugFnVerbose( "validate_arguments()" );
161 strMsg = "";
162 dictArgs = {};
163 nResult = 0;
164 strListArgs = "hdm"; # Format "hiox:" = -h -i -o -x <arg>
165 listLongArgs = ["srcRoot=", "targetDir=", "cfgBldDir=", "prefix=", "cmakeBuildConfiguration=",
166 "argsFile", "buildConfig="];
167 dictArgReq = { "-h": "o", # o = optional, m = mandatory
168 "-d": "o",
169 "-m": "o",
170 "--srcRoot": "m",
171 "--targetDir": "m",
172 "--cfgBldDir": "o",
173 "--buildConfig": "m",
174 "--prefix": "o",
175 "--cmakeBuildConfiguration": "o",
176 "--argsFile": "o" };
177
178 # Check for mandatory parameters
179 nResult, dictArgs, strMsg = utilsArgsParse.parse( vArgv, strListArgs,
180 listLongArgs,
181 dictArgReq,
182 strHelpInfo );
183 if nResult < 0:
184 program_exit_on_failure( nResult, strMsg );
185
186 # User input -h for help
187 if nResult == 1:
188 program_exit_success( 0, strMsg );
189
190 return (nResult, dictArgs);
Deepak Panickal9b35cf52014-07-01 17:57:19 +0000191
192#++---------------------------------------------------------------------------
Ed Masteae9d8412015-03-20 19:53:49 +0000193# Details: Locate post process script language directory and the script within
194# and execute.
195# Args: vStrScriptLang - (R) Name of the script language to build.
196# vstrFinishFileName - (R) Prefix file name to build full name.
197# vDictArgs - (R) Program input parameters.
198# Returns: Int - 0 = Success, < 0 some error condition.
199# Str - Error message.
200# Throws: None.
Deepak Panickal9b35cf52014-07-01 17:57:19 +0000201#--
202def run_post_process( vStrScriptLang, vstrFinishFileName, vDictArgs ):
Ed Masteae9d8412015-03-20 19:53:49 +0000203 dbg = utilsDebug.CDebugFnVerbose( "run_post_process()" );
204 nResult = 0;
205 strStatusMsg = "";
206 strScriptFile = vstrFinishFileName % vStrScriptLang;
207 strScriptFileDir = "%s%s/%s" % (vDictArgs[ "--srcRoot" ], "/scripts",
208 vStrScriptLang);
209 strScriptFilePath = "%s/%s" % (strScriptFileDir, strScriptFile);
210
211 # Check for the existence of the script file
212 strPath = os.path.normcase( strScriptFilePath );
213 bOk = os.path.exists( strPath );
214 if bOk == False:
215 strDir = os.path.normcase( strScriptFileDir );
216 strStatusMsg = strScriptNotFound % (strScriptFile, strDir);
217 return (-9, strStatusMsg);
218
219 if gbDbgFlag:
220 print strScriptLangFound % vStrScriptLang;
221 print strExecuteMsg % vStrScriptLang;
222
223 # Change where Python looks for our modules
224 strDir = os.path.normcase( strScriptFileDir );
225 sys.path.append( strDir );
226
227 # Execute the specific language script
228 dictArgs = vDictArgs; # Remove any args not required before passing on
229 strModuleName = strScriptFile[ : strScriptFile.__len__() - 3 ];
230 module = __import__( strModuleName );
231 nResult, strStatusMsg = module.main( dictArgs );
232
233 # Revert sys path
234 sys.path.remove( strDir );
235
236 return (nResult, strStatusMsg);
Deepak Panickal9b35cf52014-07-01 17:57:19 +0000237
238#++---------------------------------------------------------------------------
Ed Masteae9d8412015-03-20 19:53:49 +0000239# Details: Step through each script language sub directory supported
240# and execute post processing script for each scripting language,
241# make sure the build script for that language exists.
242# For now the only language we support is Python, but we expect this
243# to change.
244# Args: vDictArgs - (R) Program input parameters.
245# Returns: Int - 0 = Success, < 0 some error condition.
246# Str - Error message.
247# Throws: None.
Deepak Panickal9b35cf52014-07-01 17:57:19 +0000248#--
249def run_post_process_for_each_script_supported( vDictArgs ):
Ed Masteae9d8412015-03-20 19:53:49 +0000250 dbg = utilsDebug.CDebugFnVerbose( "run_post_process_for_each_script_supported()" );
251 nResult = 0;
252 strStatusMsg = "";
253 strScriptDir = vDictArgs[ "--srcRoot" ] + "/scripts";
254 strFinishFileName = "finishSwig%sLLDB.py";
255
256 # Check for the existence of the scripts folder
257 strScriptsDir = os.path.normcase( strScriptDir );
258 bOk = os.path.exists( strScriptsDir );
259 if bOk == False:
260 return (-8, strScriptDirNotFound);
261
262 # Look for any script language directories to build for
263 listDirs = [];
264 nDepth = 1;
265 for strPath, listDirs, listFiles in os.walk( strScriptDir ):
266 nDepth = nDepth - 1;
267 if nDepth == 0:
268 break;
269
270 if gbDbgFlag:
271 print strScriptLangsFound,
272 for dir in listDirs:
273 print dir,
274 print "\n";
275
276 # Iterate script directory find any script language directories
277 for scriptLang in listDirs:
278 dbg.dump_text( "Executing language script for \'%s\'" % scriptLang );
279 nResult, strStatusMsg = run_post_process( scriptLang, strFinishFileName,
280 vDictArgs );
281 if nResult < 0:
282 break;
283
284 if nResult < 0:
285 strTmp = strPostProcessError % scriptLang;
286 strTmp += strStatusMsg;
287 strStatusMsg = strTmp;
288
289 return (nResult, strStatusMsg);
Deepak Panickal9b35cf52014-07-01 17:57:19 +0000290
291#++---------------------------------------------------------------------------
Ed Masteae9d8412015-03-20 19:53:49 +0000292# Details: Program's main() with arguments passed in from the command line.
293# Program either exits normally or with error from this function -
294# top most level function.
295# Args: vArgv - (R) List of arguments and values.
296# Returns: None
297# Throws: None.
Deepak Panickal9b35cf52014-07-01 17:57:19 +0000298#--
299def main( vArgv ):
Ed Masteae9d8412015-03-20 19:53:49 +0000300 dbg = utilsDebug.CDebugFnVerbose( "main()" );
301 bOk = False;
302 dictArgs = {};
303 nResult = 0;
304 strMsg = "";
Deepak Panickal9b35cf52014-07-01 17:57:19 +0000305
Ed Masteae9d8412015-03-20 19:53:49 +0000306 # The validate arguments fn will exit the program if tests fail
307 nResult, dictArgs = validate_arguments( vArgv );
Deepak Panickal9b35cf52014-07-01 17:57:19 +0000308
Ed Masteae9d8412015-03-20 19:53:49 +0000309 eOSType = utilsOsType.determine_os_type();
310 if eOSType == utilsOsType.EnumOsType.Unknown:
311 program_exit( -4, strMsgErrorOsTypeUnknown );
312
313 global gbDbgFlag;
314 gbDbgFlag = dictArgs.has_key( "-d" );
315 if gbDbgFlag:
316 print_out_input_parameters( dictArgs );
317
318 # Check to see if we were called from the Makefile system. If we were, check
319 # if the caller wants SWIG to generate a dependency file.
320 # Not used in this program, but passed through to the language script file
321 # called by this program
322 global gbMakeFileFlag;
323 gbMakeFileFlag = dictArgs.has_key( "-m" );
324
325 nResult, strMsg = run_post_process_for_each_script_supported( dictArgs );
326
327 program_exit( nResult, strMsg );
Deepak Panickal9b35cf52014-07-01 17:57:19 +0000328
329#-----------------------------------------------------------------------------
330#-----------------------------------------------------------------------------
331#-----------------------------------------------------------------------------
332
333#TAG_PROGRAM_HELP_INFO
Ed Masteae9d8412015-03-20 19:53:49 +0000334""" Details: Program main entry point.
Deepak Panickal9b35cf52014-07-01 17:57:19 +0000335
Ed Masteae9d8412015-03-20 19:53:49 +0000336 --------------------------------------------------------------------------
337 Args: -h (optional) Print help information on this program.
338 -d (optional) Determines whether or not this script
339 outputs additional information when running.
340 -m (optional) Specify called from Makefile system. If given locate
341 the LLDBWrapPython.cpp in --srcRoot/source folder
342 else in the --targetDir folder.
343 --srcRoot= The root of the lldb source tree.
344 --targetDir= Where the lldb framework/shared library gets put.
345 --cfgBldDir= Where the buildSwigPythonLLDB.py program will
346 (optional) put the lldb.py file it generated from running
347 SWIG.
348 --prefix= Is the root directory used to determine where
349 (optional) third-party modules for scripting languages should
350 be installed. Where non-Darwin systems want to put
351 the .py and .so files so that Python can find them
352 automatically. Python install directory.
353 --cmakeBuildConfiguration= (optional) Is the build configuration(Debug, Release, RelWithDebugInfo)\n\
354 used to determine where the bin and lib directories are \n\
355 created for a Windows build.\n\
356 --argsFile= The args are read from a file instead of the
357 command line. Other command line args are ignored.
358 Usage:
359 finishSwigWrapperClasses.py --srcRoot=ADirPath --targetDir=ADirPath
360 --cfgBldDir=ADirPath --prefix=ADirPath -m -d
361
362 Results: 0 Success
363 -1 Error - invalid parameters passed.
364 -2 Error - incorrect number of mandatory parameters passed.
365
366 -4 Error - unable to determine OS type.
367 -5 Error - program not run with name of "__main__".
368 -8 Error - unable to locate the scripts folder.
369 -9 Error - unable to locate the post process language script
370 file.
371
372 -100+ - Error messages from the child language script file.
373
374 --------------------------------------------------------------------------
375
Deepak Panickal9b35cf52014-07-01 17:57:19 +0000376"""
377
378# Called using "__main__" when not imported i.e. from the command line
379if __name__ == "__main__":
Ed Masteae9d8412015-03-20 19:53:49 +0000380 utilsDebug.CDebugFnVerbose.bVerboseOn = gbDbgVerbose;
381 dbg = utilsDebug.CDebugFnVerbose( "__main__" );
382 main( sys.argv[ 1: ] );
Deepak Panickal9b35cf52014-07-01 17:57:19 +0000383else:
Ed Masteae9d8412015-03-20 19:53:49 +0000384 program_exit( -5, strMsgErrorNoMain );
385