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