makemakefile.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #
  2. # Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
  3. # All rights reserved.
  4. # This component and the accompanying materials are made available
  5. # under the terms of the License "Eclipse Public License v1.0"
  6. # which accompanies this distribution, and is available
  7. # at the URL "http://www.eclipse.org/legal/epl-v10.html".
  8. #
  9. # Initial Contributors:
  10. # Nokia Corporation - initial contribution.
  11. #
  12. # Contributors:
  13. #
  14. # Description:
  15. #
  16. #! python
  17. import optparse
  18. import sys
  19. def writeline(line, filehandle):
  20. """Assumes filehandle has a write method and that line is a string;
  21. Calls the filehandle's write method with line as an argument. Note:
  22. filehandle can be STDOUT or STDERR or a real file. The client code
  23. should take care of the creation of the filehandle object"""
  24. print >> filehandle, line,
  25. def info(arg, filehandle):
  26. writeline("# INFO: " + str(arg), filehandle)
  27. def warning(arg, filehandle):
  28. writeline("# WARNING: " + str(arg), filehandle)
  29. def error(arg, filehandle):
  30. writeline("# ERROR: " + str(arg), filehandle)
  31. # Get the command line options
  32. parser = optparse.OptionParser()
  33. parser.add_option("-t", "--targets", type="int", dest="targets",
  34. help="Number of main (or \"level1\") targets to generate - these are the targets " + \
  35. "that actually perform some simulated actions.")
  36. parser.add_option("-d", "--divisions", type="int", dest="divisions",
  37. help="The number of \"level2\" targets. Each level2 target will depend on " + \
  38. "t/d level1 targets. This makes makefile generation more logical.")
  39. parser.add_option("-m", "--makefile", dest="makefile",
  40. help="Name of makefile to generate. If blank, makefile is printed to STDOUT.")
  41. parser.add_option("-c", "--case", dest="case",
  42. help="Type of commands to use in each rule. Default is \"all\"; other options are " + \
  43. "\"env\", \"echo\", \"cp\" and \"sed\"")
  44. (options, args) = parser.parse_args()
  45. makefile = options.makefile
  46. makefilefh = None
  47. # Open the makefile if possible. Note, all info, warnings and errors
  48. # will appear in the makefile as comments.
  49. if makefile != None:
  50. try:
  51. makefilefh = open(makefile, "w")
  52. except:
  53. makefile = None
  54. makefilefh = None
  55. error("Failed to open " + makefile + ". STDOUT will be used instead.", makefilefh)
  56. info("Auto-generated makefile for stress-testing.\n\n", makefilefh)
  57. if options.targets == None:
  58. error("Missing option \"targets\". Please ensure you have specified the number of targets required.\n\n", makefilefh)
  59. sys.exit(2)
  60. if options.divisions == None:
  61. info("Missing option \"divisions\". Defaulting to 1.\n\n", makefilefh)
  62. options.divisions = 1
  63. # Commands to use in the main "worker" rules
  64. command_env = "echo Echoing PATH from $@ in a subshell; (echo PATH=$$PATH;)"
  65. command_echo_1 = "echo This is rule $@; echo PATH=$$PATH && echo TMP=$$TMP;"
  66. command_echo_2 = "echo Echoing PATH from $@ in a subshell; (echo PATH=$$PATH;)"
  67. command_echo_3 = "echo Echoing PATH from $@ in a subshell; (echo Another subshell; (echo PATH=$$PATH;))"
  68. command_cp_1 = "cp -f junk_file junk_file_copy_1_$@"
  69. command_cp_2 = "cp -f junk_file junk_file_copy_2_$@"
  70. command_cp_3 = "cp -f junk_file junk_file_copy_3_$@"
  71. command_cp_4 = "cp -f junk_file junk_file_copy_4_$@"
  72. command_sed_1 = "echo asdfsdf-----asdfasdfasdf-.txt | sed 's!.*-----!!g';"
  73. command_sed_2 = "echo 'ssss:33 x' | sed 's!.*:[0-9][0-9] *!!g'"
  74. # Default command list
  75. command_list = []
  76. if options.case == "env":
  77. command_list = [command_env]
  78. elif options.case == "echo":
  79. command_list = [command_echo_1, command_echo_2, command_echo_3]
  80. elif options.case == "cp":
  81. command_list = [command_cp_1, command_cp_2, command_cp_3, command_cp_4]
  82. elif options.case == "sed":
  83. command_list = [command_sed_1, command_sed_2]
  84. elif options.case in ["all", None]:
  85. command_list = [command_env,
  86. command_echo_1, command_echo_2, command_echo_3,
  87. command_cp_1, command_cp_2, command_cp_3, command_cp_4,
  88. command_sed_1, command_sed_2]
  89. else:
  90. error("Unknown option for \"case\" option: %s. Reverting to defaults..." % (options.case), makefilefh)
  91. command_list = [command_env,
  92. command_echo_1, command_echo_2, command_echo_3,
  93. command_cp_1, command_cp_2, command_cp_3, command_cp_4,
  94. command_sed_1, command_sed_2]
  95. # Clean command to delete all the junk copy files
  96. clean = "rm -f junk_file_copy_*"
  97. total_targets = options.targets
  98. divisions = options.divisions
  99. quotient = total_targets/divisions
  100. remainder = total_targets - quotient*divisions
  101. writeline("main:", makefilefh)
  102. for i in range(divisions):
  103. writeline("level_2_rule_%09d " % (i), makefilefh)
  104. writeline("\n\n", makefilefh)
  105. for i in range(divisions):
  106. writeline("level_2_rule_%09d: " % (i), makefilefh)
  107. for j in range(quotient):
  108. writeline("level_1_rule_%09d " % (j + i*quotient), makefilefh)
  109. writeline("\n\n", makefilefh)
  110. # Generate extra rule for the "remainder" targets
  111. if remainder > 0:
  112. writeline("main:", makefilefh)
  113. writeline("level_2_rule_%09d " % (divisions), makefilefh)
  114. writeline("\n\n", makefilefh)
  115. writeline("level_2_rule_%09d: " % (divisions), makefilefh)
  116. for j in range(total_targets - remainder,total_targets):
  117. writeline("level_1_rule_%09d " % (j), makefilefh)
  118. writeline("\n\n", makefilefh)
  119. # Generate the level_1_rules - these are the ones that actually
  120. # execute commands.
  121. for i in range(total_targets):
  122. writeline("level_1_rule_%09d: \n" % (i), makefilefh)
  123. for command in command_list:
  124. writeline("\t" + command + "\n", makefilefh)
  125. writeline("\n\n", makefilefh)
  126. writeline("clean:\n", makefilefh)
  127. writeline("\t" + clean + "\n", makefilefh)
  128. writeline("\n", makefilefh)
  129. if makefile != None:
  130. makefilefh.close()