blob: 6cfc255d9e1d052d5739263ae0cb9897e5024b55 [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=",
Zachary Turner0405d682015-04-22 22:53:18 +0000161 "argsFile"];
Ed Masteae9d8412015-03-20 19:53:49 +0000162 dictArgReq = { "-h": "o", # o = optional, m = mandatory
163 "-d": "o",
164 "-m": "o",
165 "--srcRoot": "m",
166 "--targetDir": "m",
167 "--cfgBldDir": "o",
Ed Masteae9d8412015-03-20 19:53:49 +0000168 "--prefix": "o",
169 "--cmakeBuildConfiguration": "o",
170 "--argsFile": "o" };
171
172 # Check for mandatory parameters
173 nResult, dictArgs, strMsg = utilsArgsParse.parse( vArgv, strListArgs,
174 listLongArgs,
175 dictArgReq,
176 strHelpInfo );
177 if nResult < 0:
178 program_exit_on_failure( nResult, strMsg );
179
180 # User input -h for help
181 if nResult == 1:
182 program_exit_success( 0, strMsg );
183
184 return (nResult, dictArgs);
Deepak Panickal9b35cf52014-07-01 17:57:19 +0000185
186#++---------------------------------------------------------------------------
Ed Masteae9d8412015-03-20 19:53:49 +0000187# Details: Locate post process script language directory and the script within
188# and execute.
189# Args: vStrScriptLang - (R) Name of the script language to build.
190# vstrFinishFileName - (R) Prefix file name to build full name.
191# vDictArgs - (R) Program input parameters.
192# Returns: Int - 0 = Success, < 0 some error condition.
193# Str - Error message.
194# Throws: None.
Deepak Panickal9b35cf52014-07-01 17:57:19 +0000195#--
196def run_post_process( vStrScriptLang, vstrFinishFileName, vDictArgs ):
Ed Masteae9d8412015-03-20 19:53:49 +0000197 dbg = utilsDebug.CDebugFnVerbose( "run_post_process()" );
198 nResult = 0;
199 strStatusMsg = "";
200 strScriptFile = vstrFinishFileName % vStrScriptLang;
Zachary Turnereaa92662015-04-03 17:19:43 +0000201 strScriptFileDir = os.path.normpath(os.path.join(vDictArgs["--srcRoot"], "scripts", vStrScriptLang));
202 strScriptFilePath = os.path.join(strScriptFileDir, strScriptFile);
Ed Masteae9d8412015-03-20 19:53:49 +0000203
204 # Check for the existence of the script file
205 strPath = os.path.normcase( strScriptFilePath );
206 bOk = os.path.exists( strPath );
207 if bOk == False:
208 strDir = os.path.normcase( strScriptFileDir );
209 strStatusMsg = strScriptNotFound % (strScriptFile, strDir);
210 return (-9, strStatusMsg);
211
212 if gbDbgFlag:
213 print strScriptLangFound % vStrScriptLang;
214 print strExecuteMsg % vStrScriptLang;
215
216 # Change where Python looks for our modules
217 strDir = os.path.normcase( strScriptFileDir );
218 sys.path.append( strDir );
219
220 # Execute the specific language script
221 dictArgs = vDictArgs; # Remove any args not required before passing on
222 strModuleName = strScriptFile[ : strScriptFile.__len__() - 3 ];
223 module = __import__( strModuleName );
224 nResult, strStatusMsg = module.main( dictArgs );
225
226 # Revert sys path
227 sys.path.remove( strDir );
228
229 return (nResult, strStatusMsg);
Deepak Panickal9b35cf52014-07-01 17:57:19 +0000230
231#++---------------------------------------------------------------------------
Ed Masteae9d8412015-03-20 19:53:49 +0000232# Details: Step through each script language sub directory supported
233# and execute post processing script for each scripting language,
234# make sure the build script for that language exists.
235# For now the only language we support is Python, but we expect this
236# to change.
237# Args: vDictArgs - (R) Program input parameters.
238# Returns: Int - 0 = Success, < 0 some error condition.
239# Str - Error message.
240# Throws: None.
Deepak Panickal9b35cf52014-07-01 17:57:19 +0000241#--
242def run_post_process_for_each_script_supported( vDictArgs ):
Ed Masteae9d8412015-03-20 19:53:49 +0000243 dbg = utilsDebug.CDebugFnVerbose( "run_post_process_for_each_script_supported()" );
244 nResult = 0;
245 strStatusMsg = "";
Zachary Turnereaa92662015-04-03 17:19:43 +0000246 strScriptDir = os.path.normpath(os.path.join(vDictArgs["--srcRoot"], "scripts"));
Ed Masteae9d8412015-03-20 19:53:49 +0000247 strFinishFileName = "finishSwig%sLLDB.py";
248
249 # Check for the existence of the scripts folder
250 strScriptsDir = os.path.normcase( strScriptDir );
251 bOk = os.path.exists( strScriptsDir );
252 if bOk == False:
253 return (-8, strScriptDirNotFound);
254
255 # Look for any script language directories to build for
256 listDirs = [];
257 nDepth = 1;
258 for strPath, listDirs, listFiles in os.walk( strScriptDir ):
259 nDepth = nDepth - 1;
260 if nDepth == 0:
261 break;
262
263 if gbDbgFlag:
264 print strScriptLangsFound,
265 for dir in listDirs:
266 print dir,
267 print "\n";
268
269 # Iterate script directory find any script language directories
270 for scriptLang in listDirs:
271 dbg.dump_text( "Executing language script for \'%s\'" % scriptLang );
272 nResult, strStatusMsg = run_post_process( scriptLang, strFinishFileName,
273 vDictArgs );
274 if nResult < 0:
275 break;
276
277 if nResult < 0:
278 strTmp = strPostProcessError % scriptLang;
279 strTmp += strStatusMsg;
280 strStatusMsg = strTmp;
281
282 return (nResult, strStatusMsg);
Deepak Panickal9b35cf52014-07-01 17:57:19 +0000283
284#++---------------------------------------------------------------------------
Ed Masteae9d8412015-03-20 19:53:49 +0000285# Details: Program's main() with arguments passed in from the command line.
286# Program either exits normally or with error from this function -
287# top most level function.
288# Args: vArgv - (R) List of arguments and values.
289# Returns: None
290# Throws: None.
Deepak Panickal9b35cf52014-07-01 17:57:19 +0000291#--
292def main( vArgv ):
Ed Masteae9d8412015-03-20 19:53:49 +0000293 dbg = utilsDebug.CDebugFnVerbose( "main()" );
294 bOk = False;
295 dictArgs = {};
296 nResult = 0;
297 strMsg = "";
Deepak Panickal9b35cf52014-07-01 17:57:19 +0000298
Ed Masteae9d8412015-03-20 19:53:49 +0000299 # The validate arguments fn will exit the program if tests fail
300 nResult, dictArgs = validate_arguments( vArgv );
Deepak Panickal9b35cf52014-07-01 17:57:19 +0000301
Ed Masteae9d8412015-03-20 19:53:49 +0000302 eOSType = utilsOsType.determine_os_type();
303 if eOSType == utilsOsType.EnumOsType.Unknown:
304 program_exit( -4, strMsgErrorOsTypeUnknown );
305
306 global gbDbgFlag;
307 gbDbgFlag = dictArgs.has_key( "-d" );
308 if gbDbgFlag:
309 print_out_input_parameters( dictArgs );
310
311 # Check to see if we were called from the Makefile system. If we were, check
312 # if the caller wants SWIG to generate a dependency file.
313 # Not used in this program, but passed through to the language script file
314 # called by this program
315 global gbMakeFileFlag;
316 gbMakeFileFlag = dictArgs.has_key( "-m" );
317
318 nResult, strMsg = run_post_process_for_each_script_supported( dictArgs );
319
320 program_exit( nResult, strMsg );
Deepak Panickal9b35cf52014-07-01 17:57:19 +0000321
322#-----------------------------------------------------------------------------
323#-----------------------------------------------------------------------------
324#-----------------------------------------------------------------------------
325
326#TAG_PROGRAM_HELP_INFO
Ed Masteae9d8412015-03-20 19:53:49 +0000327""" Details: Program main entry point.
Deepak Panickal9b35cf52014-07-01 17:57:19 +0000328
Ed Masteae9d8412015-03-20 19:53:49 +0000329 --------------------------------------------------------------------------
330 Args: -h (optional) Print help information on this program.
331 -d (optional) Determines whether or not this script
332 outputs additional information when running.
333 -m (optional) Specify called from Makefile system. If given locate
334 the LLDBWrapPython.cpp in --srcRoot/source folder
335 else in the --targetDir folder.
336 --srcRoot= The root of the lldb source tree.
337 --targetDir= Where the lldb framework/shared library gets put.
338 --cfgBldDir= Where the buildSwigPythonLLDB.py program will
339 (optional) put the lldb.py file it generated from running
340 SWIG.
341 --prefix= Is the root directory used to determine where
342 (optional) third-party modules for scripting languages should
343 be installed. Where non-Darwin systems want to put
344 the .py and .so files so that Python can find them
345 automatically. Python install directory.
346 --cmakeBuildConfiguration= (optional) Is the build configuration(Debug, Release, RelWithDebugInfo)\n\
347 used to determine where the bin and lib directories are \n\
348 created for a Windows build.\n\
349 --argsFile= The args are read from a file instead of the
350 command line. Other command line args are ignored.
351 Usage:
352 finishSwigWrapperClasses.py --srcRoot=ADirPath --targetDir=ADirPath
353 --cfgBldDir=ADirPath --prefix=ADirPath -m -d
354
355 Results: 0 Success
356 -1 Error - invalid parameters passed.
357 -2 Error - incorrect number of mandatory parameters passed.
358
359 -4 Error - unable to determine OS type.
360 -5 Error - program not run with name of "__main__".
361 -8 Error - unable to locate the scripts folder.
362 -9 Error - unable to locate the post process language script
363 file.
364
365 -100+ - Error messages from the child language script file.
366
367 --------------------------------------------------------------------------
368
Deepak Panickal9b35cf52014-07-01 17:57:19 +0000369"""
370
371# Called using "__main__" when not imported i.e. from the command line
372if __name__ == "__main__":
Ed Masteae9d8412015-03-20 19:53:49 +0000373 utilsDebug.CDebugFnVerbose.bVerboseOn = gbDbgVerbose;
374 dbg = utilsDebug.CDebugFnVerbose( "__main__" );
375 main( sys.argv[ 1: ] );
Deepak Panickal9b35cf52014-07-01 17:57:19 +0000376else:
Ed Masteae9d8412015-03-20 19:53:49 +0000377 program_exit( -5, strMsgErrorNoMain );
378