blob: 75f7d46dd2f87596802d115189374ba7a5a3d37e [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 Gotchas: None.
19
20 Copyright: None.
21 --------------------------------------------------------------------------
22
Deepak Panickal9b35cf52014-07-01 17:57:19 +000023"""
24
25# Python modules:
Ed Masteae9d8412015-03-20 19:53:49 +000026import sys # Provide argument parsing
27import os # Provide directory and file handling
Deepak Panickal9b35cf52014-07-01 17:57:19 +000028
29# Third party modules:
30
31# In-house modules:
Ed Masteae9d8412015-03-20 19:53:49 +000032import utilsArgsParse # Parse and validate this script's input arguments
33import utilsOsType # Determine the OS type this script is running on
34import utilsDebug # Debug Python scripts
Deepak Panickal9b35cf52014-07-01 17:57:19 +000035
36# Instantiations:
Ed Masteae9d8412015-03-20 19:53:49 +000037gbDbgVerbose = False; # True = Turn on script function tracing, False = off.
38gbDbgFlag = False; # Global debug mode flag, set by input parameter
39 # --dbgFlag. True = operate in debug mode.
40gbMakeFileFlag = False; # True = yes called from makefile system, False = not.
Deepak Panickal9b35cf52014-07-01 17:57:19 +000041
42# User facing text:
43strMsgErrorNoMain = "Program called by another Python script not allowed";
44strExitMsgSuccess = "Program successful";
45strExitMsgError = "Program error: ";
46strParameter = "Parameter: ";
47strMsgErrorOsTypeUnknown = "Unable to determine OS type"
48strScriptDirNotFound = "Unable to locate the script directory \'/script\'";
49strScriptLangsFound = "Found the following script languages:";
50strPostProcessError = "Executing \'%s\' post process script failed: ";
51strScriptNotFound = "Unable to locate the post process script file \'%s\' in \'%s\'";
52strScriptLangFound = "Found \'%s\' build script.";
53strScriptLangsFound = "Found the following script languages:";
54strExecuteMsg = "Executing \'%s\' build script...";
55strExecuteError = "Executing \'%s\' build script failed: ";
56strHelpInfo = "\
57Python script(s) to finish off the SWIG Python C++ Script \n\
58Bridge wrapper code on the Windows/LINUX/OSX platform. The Python \n\
59scripts are equivalent to the shell script (.sh) files \n\
60run on others platforms.\n\
Ed Masteae9d8412015-03-20 19:53:49 +000061Args: -h (optional) Print help information on this program.\n\
62 -d (optional) Determines whether or not this script\n\
63 outputs additional information when running.\n\
64 -m (optional) Specify called from Makefile system.\n\
65 --srcRoot= The root of the lldb source tree.\n\
66 --targetDir= Where the lldb framework/shared library gets put.\n\
67 --cfgBldDir= (optional) Where the build-swig-Python-LLDB.py program \n\
68 will put the lldb.py file it generated from running\n\
69 SWIG.\n\
70 --prefix= (optional) Is the root directory used to determine where\n\
71 third-party modules for scripting languages should\n\
72 be installed. Where non-Darwin systems want to put\n\
73 the .py and .so files so that Python can find them\n\
74 automatically. Python install directory.\n\
75 --cmakeBuildConfiguration= (optional) Is the build configuration(Debug, Release, RelWithDebugInfo)\n\
76 used to determine where the bin and lib directories are \n\
77 created for a Windows build.\n\
78 --argsFile= The args are read from a file instead of the\n\
79 command line. Other command line args are ignored.\n\
Deepak Panickal9b35cf52014-07-01 17:57:19 +000080\n\
81Usage:\n\
Ed Masteae9d8412015-03-20 19:53:49 +000082 finishSwigWrapperClasses.py --srcRoot=ADirPath --targetDir=ADirPath\n\
83 --cfgBldDir=ADirPath --prefix=ADirPath -m -d\n\
Deepak Panickal9b35cf52014-07-01 17:57:19 +000084\n\
85"; #TAG_PROGRAM_HELP_INFO
86
87#++---------------------------------------------------------------------------
Ed Masteae9d8412015-03-20 19:53:49 +000088# Details: Exit the program on success. Called on program successfully done
89# its work. Returns a status result to the caller.
90# Args: vnResult - (R) 0 or greater indicating success.
91# vMsg - (R) Success message if any to show success to user.
92# Returns: None.
93# Throws: None.
Deepak Panickal9b35cf52014-07-01 17:57:19 +000094#--
95def program_exit_success( vnResult, vMsg ):
Ed Masteae9d8412015-03-20 19:53:49 +000096 strMsg = "";
97
Ed Masteb933fb22015-03-20 19:59:35 +000098 if vMsg.__len__() != 0:
Ed Masteae9d8412015-03-20 19:53:49 +000099 strMsg = "%s: %s (%d)" % (strExitMsgSuccess, vMsg, vnResult);
Ed Masteb933fb22015-03-20 19:59:35 +0000100 print strMsg;
Ed Masteae9d8412015-03-20 19:53:49 +0000101
102 sys.exit( vnResult );
Deepak Panickal9b35cf52014-07-01 17:57:19 +0000103
104#++---------------------------------------------------------------------------
Ed Masteae9d8412015-03-20 19:53:49 +0000105# Details: Exit the program with error. Called on exit program failed its
106# task. Returns a status result to the caller.
107# Args: vnResult - (R) A negative number indicating error condition.
108# vMsg - (R) Error message to show to user.
109# Returns: None.
110# Throws: None.
Deepak Panickal9b35cf52014-07-01 17:57:19 +0000111#--
112def program_exit_on_failure( vnResult, vMsg ):
Ed Masteae9d8412015-03-20 19:53:49 +0000113 print "%s%s (%d)" % (strExitMsgError, vMsg, vnResult);
114 sys.exit( vnResult );
Deepak Panickal9b35cf52014-07-01 17:57:19 +0000115
116#++---------------------------------------------------------------------------
Ed Masteae9d8412015-03-20 19:53:49 +0000117# Details: Exit the program return a exit result number and print a message.
118# Positive numbers and zero are returned for success other error
119# occurred.
120# Args: vnResult - (R) A -ve (an error), 0 or +ve number (ok or status).
121# vMsg - (R) Error message to show to user.
122# Returns: None.
123# Throws: None.
Deepak Panickal9b35cf52014-07-01 17:57:19 +0000124#--
125def program_exit( vnResult, vMsg ):
Ed Masteae9d8412015-03-20 19:53:49 +0000126 if vnResult >= 0:
127 program_exit_success( vnResult, vMsg );
128 else:
129 program_exit_on_failure( vnResult, vMsg );
130
Deepak Panickal9b35cf52014-07-01 17:57:19 +0000131#++---------------------------------------------------------------------------
Ed Masteae9d8412015-03-20 19:53:49 +0000132# Details: Dump input parameters.
133# Args: vDictArgs - (R) Map of input args to value.
134# Returns: None.
135# Throws: None.
Deepak Panickal9b35cf52014-07-01 17:57:19 +0000136#--
137def print_out_input_parameters( vDictArgs ):
Ed Masteae9d8412015-03-20 19:53:49 +0000138 for arg, val in vDictArgs.iteritems():
139 strEqs = "";
140 strQ = "";
141 if val.__len__() != 0:
142 strEqs = " =";
143 strQ = "\"";
144 print "%s%s%s %s%s%s\n" % (strParameter, arg, strEqs, strQ, val, strQ);
Deepak Panickal9b35cf52014-07-01 17:57:19 +0000145
146#++---------------------------------------------------------------------------
Ed Masteae9d8412015-03-20 19:53:49 +0000147# Details: Validate the arguments passed to the program. This function exits
148# the program should error with the arguments be found.
149# Args: vArgv - (R) List of arguments and values.
150# Returns: Int - 0 = success, -ve = some failure.
151# Dict - Map of arguments names to argument values
152# Throws: None.
Deepak Panickal9b35cf52014-07-01 17:57:19 +0000153#--
154def validate_arguments( vArgv ):
Ed Masteae9d8412015-03-20 19:53:49 +0000155 dbg = utilsDebug.CDebugFnVerbose( "validate_arguments()" );
156 strMsg = "";
157 dictArgs = {};
158 nResult = 0;
159 strListArgs = "hdm"; # Format "hiox:" = -h -i -o -x <arg>
160 listLongArgs = ["srcRoot=", "targetDir=", "cfgBldDir=", "prefix=", "cmakeBuildConfiguration=",
161 "argsFile", "buildConfig="];
162 dictArgReq = { "-h": "o", # o = optional, m = mandatory
163 "-d": "o",
164 "-m": "o",
165 "--srcRoot": "m",
166 "--targetDir": "m",
167 "--cfgBldDir": "o",
168 "--buildConfig": "m",
169 "--prefix": "o",
170 "--cmakeBuildConfiguration": "o",
171 "--argsFile": "o" };
172
173 # Check for mandatory parameters
174 nResult, dictArgs, strMsg = utilsArgsParse.parse( vArgv, strListArgs,
175 listLongArgs,
176 dictArgReq,
177 strHelpInfo );
178 if nResult < 0:
179 program_exit_on_failure( nResult, strMsg );
180
181 # User input -h for help
182 if nResult == 1:
183 program_exit_success( 0, strMsg );
184
185 return (nResult, dictArgs);
Deepak Panickal9b35cf52014-07-01 17:57:19 +0000186
187#++---------------------------------------------------------------------------
Ed Masteae9d8412015-03-20 19:53:49 +0000188# Details: Locate post process script language directory and the script within
189# and execute.
190# Args: vStrScriptLang - (R) Name of the script language to build.
191# vstrFinishFileName - (R) Prefix file name to build full name.
192# vDictArgs - (R) Program input parameters.
193# Returns: Int - 0 = Success, < 0 some error condition.
194# Str - Error message.
195# Throws: None.
Deepak Panickal9b35cf52014-07-01 17:57:19 +0000196#--
197def run_post_process( vStrScriptLang, vstrFinishFileName, vDictArgs ):
Ed Masteae9d8412015-03-20 19:53:49 +0000198 dbg = utilsDebug.CDebugFnVerbose( "run_post_process()" );
199 nResult = 0;
200 strStatusMsg = "";
201 strScriptFile = vstrFinishFileName % vStrScriptLang;
202 strScriptFileDir = "%s%s/%s" % (vDictArgs[ "--srcRoot" ], "/scripts",
203 vStrScriptLang);
204 strScriptFilePath = "%s/%s" % (strScriptFileDir, strScriptFile);
205
206 # Check for the existence of the script file
207 strPath = os.path.normcase( strScriptFilePath );
208 bOk = os.path.exists( strPath );
209 if bOk == False:
210 strDir = os.path.normcase( strScriptFileDir );
211 strStatusMsg = strScriptNotFound % (strScriptFile, strDir);
212 return (-9, strStatusMsg);
213
214 if gbDbgFlag:
215 print strScriptLangFound % vStrScriptLang;
216 print strExecuteMsg % vStrScriptLang;
217
218 # Change where Python looks for our modules
219 strDir = os.path.normcase( strScriptFileDir );
220 sys.path.append( strDir );
221
222 # Execute the specific language script
223 dictArgs = vDictArgs; # Remove any args not required before passing on
224 strModuleName = strScriptFile[ : strScriptFile.__len__() - 3 ];
225 module = __import__( strModuleName );
226 nResult, strStatusMsg = module.main( dictArgs );
227
228 # Revert sys path
229 sys.path.remove( strDir );
230
231 return (nResult, strStatusMsg);
Deepak Panickal9b35cf52014-07-01 17:57:19 +0000232
233#++---------------------------------------------------------------------------
Ed Masteae9d8412015-03-20 19:53:49 +0000234# Details: Step through each script language sub directory supported
235# and execute post processing script for each scripting language,
236# make sure the build script for that language exists.
237# For now the only language we support is Python, but we expect this
238# to change.
239# Args: vDictArgs - (R) Program input parameters.
240# Returns: Int - 0 = Success, < 0 some error condition.
241# Str - Error message.
242# Throws: None.
Deepak Panickal9b35cf52014-07-01 17:57:19 +0000243#--
244def run_post_process_for_each_script_supported( vDictArgs ):
Ed Masteae9d8412015-03-20 19:53:49 +0000245 dbg = utilsDebug.CDebugFnVerbose( "run_post_process_for_each_script_supported()" );
246 nResult = 0;
247 strStatusMsg = "";
248 strScriptDir = vDictArgs[ "--srcRoot" ] + "/scripts";
249 strFinishFileName = "finishSwig%sLLDB.py";
250
251 # Check for the existence of the scripts folder
252 strScriptsDir = os.path.normcase( strScriptDir );
253 bOk = os.path.exists( strScriptsDir );
254 if bOk == False:
255 return (-8, strScriptDirNotFound);
256
257 # Look for any script language directories to build for
258 listDirs = [];
259 nDepth = 1;
260 for strPath, listDirs, listFiles in os.walk( strScriptDir ):
261 nDepth = nDepth - 1;
262 if nDepth == 0:
263 break;
264
265 if gbDbgFlag:
266 print strScriptLangsFound,
267 for dir in listDirs:
268 print dir,
269 print "\n";
270
271 # Iterate script directory find any script language directories
272 for scriptLang in listDirs:
273 dbg.dump_text( "Executing language script for \'%s\'" % scriptLang );
274 nResult, strStatusMsg = run_post_process( scriptLang, strFinishFileName,
275 vDictArgs );
276 if nResult < 0:
277 break;
278
279 if nResult < 0:
280 strTmp = strPostProcessError % scriptLang;
281 strTmp += strStatusMsg;
282 strStatusMsg = strTmp;
283
284 return (nResult, strStatusMsg);
Deepak Panickal9b35cf52014-07-01 17:57:19 +0000285
286#++---------------------------------------------------------------------------
Ed Masteae9d8412015-03-20 19:53:49 +0000287# Details: Program's main() with arguments passed in from the command line.
288# Program either exits normally or with error from this function -
289# top most level function.
290# Args: vArgv - (R) List of arguments and values.
291# Returns: None
292# Throws: None.
Deepak Panickal9b35cf52014-07-01 17:57:19 +0000293#--
294def main( vArgv ):
Ed Masteae9d8412015-03-20 19:53:49 +0000295 dbg = utilsDebug.CDebugFnVerbose( "main()" );
296 bOk = False;
297 dictArgs = {};
298 nResult = 0;
299 strMsg = "";
Deepak Panickal9b35cf52014-07-01 17:57:19 +0000300
Ed Masteae9d8412015-03-20 19:53:49 +0000301 # The validate arguments fn will exit the program if tests fail
302 nResult, dictArgs = validate_arguments( vArgv );
Deepak Panickal9b35cf52014-07-01 17:57:19 +0000303
Ed Masteae9d8412015-03-20 19:53:49 +0000304 eOSType = utilsOsType.determine_os_type();
305 if eOSType == utilsOsType.EnumOsType.Unknown:
306 program_exit( -4, strMsgErrorOsTypeUnknown );
307
308 global gbDbgFlag;
309 gbDbgFlag = dictArgs.has_key( "-d" );
310 if gbDbgFlag:
311 print_out_input_parameters( dictArgs );
312
313 # Check to see if we were called from the Makefile system. If we were, check
314 # if the caller wants SWIG to generate a dependency file.
315 # Not used in this program, but passed through to the language script file
316 # called by this program
317 global gbMakeFileFlag;
318 gbMakeFileFlag = dictArgs.has_key( "-m" );
319
320 nResult, strMsg = run_post_process_for_each_script_supported( dictArgs );
321
322 program_exit( nResult, strMsg );
Deepak Panickal9b35cf52014-07-01 17:57:19 +0000323
324#-----------------------------------------------------------------------------
325#-----------------------------------------------------------------------------
326#-----------------------------------------------------------------------------
327
328#TAG_PROGRAM_HELP_INFO
Ed Masteae9d8412015-03-20 19:53:49 +0000329""" Details: Program main entry point.
Deepak Panickal9b35cf52014-07-01 17:57:19 +0000330
Ed Masteae9d8412015-03-20 19:53:49 +0000331 --------------------------------------------------------------------------
332 Args: -h (optional) Print help information on this program.
333 -d (optional) Determines whether or not this script
334 outputs additional information when running.
335 -m (optional) Specify called from Makefile system. If given locate
336 the LLDBWrapPython.cpp in --srcRoot/source folder
337 else in the --targetDir folder.
338 --srcRoot= The root of the lldb source tree.
339 --targetDir= Where the lldb framework/shared library gets put.
340 --cfgBldDir= Where the buildSwigPythonLLDB.py program will
341 (optional) put the lldb.py file it generated from running
342 SWIG.
343 --prefix= Is the root directory used to determine where
344 (optional) third-party modules for scripting languages should
345 be installed. Where non-Darwin systems want to put
346 the .py and .so files so that Python can find them
347 automatically. Python install directory.
348 --cmakeBuildConfiguration= (optional) Is the build configuration(Debug, Release, RelWithDebugInfo)\n\
349 used to determine where the bin and lib directories are \n\
350 created for a Windows build.\n\
351 --argsFile= The args are read from a file instead of the
352 command line. Other command line args are ignored.
353 Usage:
354 finishSwigWrapperClasses.py --srcRoot=ADirPath --targetDir=ADirPath
355 --cfgBldDir=ADirPath --prefix=ADirPath -m -d
356
357 Results: 0 Success
358 -1 Error - invalid parameters passed.
359 -2 Error - incorrect number of mandatory parameters passed.
360
361 -4 Error - unable to determine OS type.
362 -5 Error - program not run with name of "__main__".
363 -8 Error - unable to locate the scripts folder.
364 -9 Error - unable to locate the post process language script
365 file.
366
367 -100+ - Error messages from the child language script file.
368
369 --------------------------------------------------------------------------
370
Deepak Panickal9b35cf52014-07-01 17:57:19 +0000371"""
372
373# Called using "__main__" when not imported i.e. from the command line
374if __name__ == "__main__":
Ed Masteae9d8412015-03-20 19:53:49 +0000375 utilsDebug.CDebugFnVerbose.bVerboseOn = gbDbgVerbose;
376 dbg = utilsDebug.CDebugFnVerbose( "__main__" );
377 main( sys.argv[ 1: ] );
Deepak Panickal9b35cf52014-07-01 17:57:19 +0000378else:
Ed Masteae9d8412015-03-20 19:53:49 +0000379 program_exit( -5, strMsgErrorNoMain );
380