blob: 4aa2e50d95a09165557eadea677e7ff35bb4e50c [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
Bruce Mitchener17d27302015-04-24 00:38:53 +0000263 # Skip the directory that contains the interface files.
264 listDirs.remove('interface')
Pavel Labath9c2f7162015-06-29 13:51:49 +0000265 # and the svn directory.
266 if '.svn' in listDirs:
267 listDirs.remove('.svn')
Bruce Mitchener17d27302015-04-24 00:38:53 +0000268
Ed Masteae9d8412015-03-20 19:53:49 +0000269 if gbDbgFlag:
270 print strScriptLangsFound,
271 for dir in listDirs:
272 print dir,
273 print "\n";
274
275 # Iterate script directory find any script language directories
276 for scriptLang in listDirs:
277 dbg.dump_text( "Executing language script for \'%s\'" % scriptLang );
278 nResult, strStatusMsg = run_post_process( scriptLang, strFinishFileName,
279 vDictArgs );
280 if nResult < 0:
281 break;
282
283 if nResult < 0:
284 strTmp = strPostProcessError % scriptLang;
285 strTmp += strStatusMsg;
286 strStatusMsg = strTmp;
287
288 return (nResult, strStatusMsg);
Deepak Panickal9b35cf52014-07-01 17:57:19 +0000289
290#++---------------------------------------------------------------------------
Ed Masteae9d8412015-03-20 19:53:49 +0000291# Details: Program's main() with arguments passed in from the command line.
292# Program either exits normally or with error from this function -
293# top most level function.
294# Args: vArgv - (R) List of arguments and values.
295# Returns: None
296# Throws: None.
Deepak Panickal9b35cf52014-07-01 17:57:19 +0000297#--
298def main( vArgv ):
Ed Masteae9d8412015-03-20 19:53:49 +0000299 dbg = utilsDebug.CDebugFnVerbose( "main()" );
300 bOk = False;
301 dictArgs = {};
302 nResult = 0;
303 strMsg = "";
Deepak Panickal9b35cf52014-07-01 17:57:19 +0000304
Ed Masteae9d8412015-03-20 19:53:49 +0000305 # The validate arguments fn will exit the program if tests fail
306 nResult, dictArgs = validate_arguments( vArgv );
Deepak Panickal9b35cf52014-07-01 17:57:19 +0000307
Ed Masteae9d8412015-03-20 19:53:49 +0000308 eOSType = utilsOsType.determine_os_type();
309 if eOSType == utilsOsType.EnumOsType.Unknown:
310 program_exit( -4, strMsgErrorOsTypeUnknown );
311
312 global gbDbgFlag;
313 gbDbgFlag = dictArgs.has_key( "-d" );
314 if gbDbgFlag:
315 print_out_input_parameters( dictArgs );
316
317 # Check to see if we were called from the Makefile system. If we were, check
318 # if the caller wants SWIG to generate a dependency file.
319 # Not used in this program, but passed through to the language script file
320 # called by this program
321 global gbMakeFileFlag;
322 gbMakeFileFlag = dictArgs.has_key( "-m" );
323
324 nResult, strMsg = run_post_process_for_each_script_supported( dictArgs );
325
326 program_exit( nResult, strMsg );
Deepak Panickal9b35cf52014-07-01 17:57:19 +0000327
328#-----------------------------------------------------------------------------
329#-----------------------------------------------------------------------------
330#-----------------------------------------------------------------------------
331
332#TAG_PROGRAM_HELP_INFO
Ed Masteae9d8412015-03-20 19:53:49 +0000333""" Details: Program main entry point.
Deepak Panickal9b35cf52014-07-01 17:57:19 +0000334
Ed Masteae9d8412015-03-20 19:53:49 +0000335 --------------------------------------------------------------------------
336 Args: -h (optional) Print help information on this program.
337 -d (optional) Determines whether or not this script
338 outputs additional information when running.
339 -m (optional) Specify called from Makefile system. If given locate
340 the LLDBWrapPython.cpp in --srcRoot/source folder
341 else in the --targetDir folder.
342 --srcRoot= The root of the lldb source tree.
343 --targetDir= Where the lldb framework/shared library gets put.
344 --cfgBldDir= Where the buildSwigPythonLLDB.py program will
345 (optional) put the lldb.py file it generated from running
346 SWIG.
347 --prefix= Is the root directory used to determine where
348 (optional) third-party modules for scripting languages should
349 be installed. Where non-Darwin systems want to put
350 the .py and .so files so that Python can find them
351 automatically. Python install directory.
352 --cmakeBuildConfiguration= (optional) Is the build configuration(Debug, Release, RelWithDebugInfo)\n\
353 used to determine where the bin and lib directories are \n\
354 created for a Windows build.\n\
355 --argsFile= The args are read from a file instead of the
356 command line. Other command line args are ignored.
357 Usage:
358 finishSwigWrapperClasses.py --srcRoot=ADirPath --targetDir=ADirPath
359 --cfgBldDir=ADirPath --prefix=ADirPath -m -d
360
361 Results: 0 Success
362 -1 Error - invalid parameters passed.
363 -2 Error - incorrect number of mandatory parameters passed.
364
365 -4 Error - unable to determine OS type.
366 -5 Error - program not run with name of "__main__".
367 -8 Error - unable to locate the scripts folder.
368 -9 Error - unable to locate the post process language script
369 file.
370
371 -100+ - Error messages from the child language script file.
372
373 --------------------------------------------------------------------------
374
Deepak Panickal9b35cf52014-07-01 17:57:19 +0000375"""
376
377# Called using "__main__" when not imported i.e. from the command line
378if __name__ == "__main__":
Ed Masteae9d8412015-03-20 19:53:49 +0000379 utilsDebug.CDebugFnVerbose.bVerboseOn = gbDbgVerbose;
380 dbg = utilsDebug.CDebugFnVerbose( "__main__" );
381 main( sys.argv[ 1: ] );
Deepak Panickal9b35cf52014-07-01 17:57:19 +0000382else:
Ed Masteae9d8412015-03-20 19:53:49 +0000383 program_exit( -5, strMsgErrorNoMain );
384