blob: c2138d5fe6ed2f8632c7d61ff2b536cab71bc699 [file] [log] [blame]
Daniel Dunbara5677512009-01-05 19:53:30 +00001import Arguments
2import Jobs
3import Types
4
5class Tool(object):
Daniel Dunbarba6e3232009-01-06 06:12:13 +00006 """Tool - A concrete implementation of an action."""
Daniel Dunbara5677512009-01-05 19:53:30 +00007
8 eFlagsPipedInput = 1 << 0
9 eFlagsPipedOutput = 1 << 1
10 eFlagsIntegratedCPP = 1 << 2
11
12 def __init__(self, name, flags = 0):
13 self.name = name
14 self.flags = flags
15
16 def acceptsPipedInput(self):
17 return not not (self.flags & Tool.eFlagsPipedInput)
18 def canPipeOutput(self):
19 return not not (self.flags & Tool.eFlagsPipedOutput)
20 def hasIntegratedCPP(self):
21 return not not (self.flags & Tool.eFlagsIntegratedCPP)
22
23class GCC_Common_Tool(Tool):
24 def constructJob(self, phase, arch, jobs, inputs,
Daniel Dunbardb439902009-01-07 18:40:45 +000025 output, outputType, args, arglist,
Daniel Dunbara5677512009-01-05 19:53:30 +000026 extraArgs):
Daniel Dunbardb439902009-01-07 18:40:45 +000027 cmd_args = sum(map(arglist.render, args),[]) + extraArgs
Daniel Dunbara5677512009-01-05 19:53:30 +000028 if arch:
Daniel Dunbar39cbfaa2009-01-07 18:54:26 +000029 cmd_args.extend(arglist.render(arch))
Daniel Dunbara5677512009-01-05 19:53:30 +000030 if isinstance(output, Jobs.PipedJob):
Daniel Dunbardb439902009-01-07 18:40:45 +000031 cmd_args.extend(['-o', '-'])
Daniel Dunbara5677512009-01-05 19:53:30 +000032 elif output is None:
Daniel Dunbardb439902009-01-07 18:40:45 +000033 cmd_args.append('-fsyntax-only')
Daniel Dunbara5677512009-01-05 19:53:30 +000034 else:
Daniel Dunbar39cbfaa2009-01-07 18:54:26 +000035 cmd_args.extend(arglist.render(output))
Daniel Dunbara5677512009-01-05 19:53:30 +000036
Daniel Dunbara9ad2bc2009-01-10 02:00:04 +000037 # Only pass -x if gcc will understand it; otherwise hope gcc
38 # understands the suffix correctly. The main use case this
39 # would go wrong in is for linker inputs, say if the user
40 # tried to make an executable named 'a.c'.
41 #
42 # FIXME: For the linker case specifically, can we safely
43 # convert inputs into '-Wl,' options?
44 for input in inputs:
45 if input.type.canBeUserSpecified:
46 cmd_args.extend(['-x', input.type.name])
47
48 if isinstance(input.source, Jobs.PipedJob):
49 cmd_args.append('-')
50 else:
51 cmd_args.append(arglist.getValue(input.source))
Daniel Dunbara5677512009-01-05 19:53:30 +000052
53 jobs.addJob(Jobs.Command('gcc', cmd_args))
54
55class GCC_PreprocessTool(GCC_Common_Tool):
56 def __init__(self):
Daniel Dunbara9ad2bc2009-01-10 02:00:04 +000057 super(GCC_PreprocessTool, self).__init__('gcc (cpp)',
Daniel Dunbara5677512009-01-05 19:53:30 +000058 (Tool.eFlagsPipedInput |
59 Tool.eFlagsPipedOutput))
60
61 def constructJob(self, phase, arch, jobs, inputs,
Daniel Dunbardb439902009-01-07 18:40:45 +000062 output, outputType, args, arglist):
Daniel Dunbara5677512009-01-05 19:53:30 +000063 return super(GCC_PreprocessTool, self).constructJob(phase, arch, jobs, inputs,
Daniel Dunbardb439902009-01-07 18:40:45 +000064 output, outputType, args, arglist,
65 ['-E'])
Daniel Dunbara5677512009-01-05 19:53:30 +000066
67class GCC_CompileTool(GCC_Common_Tool):
68 def __init__(self):
Daniel Dunbara9ad2bc2009-01-10 02:00:04 +000069 super(GCC_CompileTool, self).__init__('gcc (cc1)',
Daniel Dunbara5677512009-01-05 19:53:30 +000070 (Tool.eFlagsPipedInput |
71 Tool.eFlagsPipedOutput |
72 Tool.eFlagsIntegratedCPP))
73
74 def constructJob(self, phase, arch, jobs, inputs,
Daniel Dunbardb439902009-01-07 18:40:45 +000075 output, outputType, args, arglist):
Daniel Dunbara5677512009-01-05 19:53:30 +000076 return super(GCC_CompileTool, self).constructJob(phase, arch, jobs, inputs,
Daniel Dunbardb439902009-01-07 18:40:45 +000077 output, outputType, args, arglist,
78 ['-S'])
Daniel Dunbara5677512009-01-05 19:53:30 +000079
80class GCC_PrecompileTool(GCC_Common_Tool):
81 def __init__(self):
Daniel Dunbara9ad2bc2009-01-10 02:00:04 +000082 super(GCC_PrecompileTool, self).__init__('gcc (pch)',
Daniel Dunbara5677512009-01-05 19:53:30 +000083 (Tool.eFlagsPipedInput |
84 Tool.eFlagsIntegratedCPP))
85
86 def constructJob(self, phase, arch, jobs, inputs,
Daniel Dunbardb439902009-01-07 18:40:45 +000087 output, outputType, args, arglist):
Daniel Dunbara5677512009-01-05 19:53:30 +000088 return super(GCC_PrecompileTool, self).constructJob(phase, arch, jobs, inputs,
Daniel Dunbardb439902009-01-07 18:40:45 +000089 output, outputType, args, arglist,
Daniel Dunbara5677512009-01-05 19:53:30 +000090 [])
91
Daniel Dunbara9ad2bc2009-01-10 02:00:04 +000092class DarwinAssembleTool(Tool):
Daniel Dunbara5677512009-01-05 19:53:30 +000093 def __init__(self):
Daniel Dunbara9ad2bc2009-01-10 02:00:04 +000094 super(DarwinAssembleTool, self).__init__('as',
95 Tool.eFlagsPipedInput)
Daniel Dunbara5677512009-01-05 19:53:30 +000096
97 def constructJob(self, phase, arch, jobs, inputs,
Daniel Dunbardb439902009-01-07 18:40:45 +000098 output, outputType, args, arglist):
Daniel Dunbara5677512009-01-05 19:53:30 +000099 assert len(inputs) == 1
100 assert outputType is Types.ObjectType
101
102 input = inputs[0]
103
104 cmd_args = []
105 if arch:
Daniel Dunbar39cbfaa2009-01-07 18:54:26 +0000106 cmd_args.extend(arglist.render(arch))
Daniel Dunbardb439902009-01-07 18:40:45 +0000107 cmd_args.append('-force_cpusubtype_ALL')
Daniel Dunbar39cbfaa2009-01-07 18:54:26 +0000108 cmd_args.extend(arglist.render(output))
Daniel Dunbara5677512009-01-05 19:53:30 +0000109 if isinstance(input.source, Jobs.PipedJob):
Daniel Dunbardb439902009-01-07 18:40:45 +0000110 cmd_args.append('-')
Daniel Dunbara5677512009-01-05 19:53:30 +0000111 else:
Daniel Dunbardb439902009-01-07 18:40:45 +0000112 cmd_args.append(arglist.getValue(input.source))
Daniel Dunbara5677512009-01-05 19:53:30 +0000113 jobs.addJob(Jobs.Command('as', cmd_args))
114
Daniel Dunbara9ad2bc2009-01-10 02:00:04 +0000115class GCC_AssembleTool(GCC_Common_Tool):
116 def __init__(self):
117 # We can't generally assume the assembler can take or output
118 # on pipes.
119 super(GCC_AssembleTool, self).__init__('gcc (as)')
120
121 def constructJob(self, phase, arch, jobs, inputs,
122 output, outputType, args, arglist):
123 return super(GCC_AssembleTool, self).constructJob(phase, arch, jobs, inputs,
124 output, outputType, args, arglist,
125 ['-c'])
126
127class GCC_LinkTool(GCC_Common_Tool):
128 def __init__(self):
129 super(GCC_LinkTool, self).__init__('gcc (ld)')
130
131 def constructJob(self, phase, arch, jobs, inputs,
132 output, outputType, args, arglist):
133 return super(GCC_LinkTool, self).constructJob(phase, arch, jobs, inputs,
134 output, outputType, args, arglist,
135 [])
136
Daniel Dunbara5677512009-01-05 19:53:30 +0000137class Collect2Tool(Tool):
138 kCollect2Path = '/usr/libexec/gcc/i686-apple-darwin10/4.2.1/collect2'
139 def __init__(self):
140 super(Collect2Tool, self).__init__('collect2')
141
142 def constructJob(self, phase, arch, jobs, inputs,
Daniel Dunbardb439902009-01-07 18:40:45 +0000143 output, outputType, args, arglist):
Daniel Dunbara5677512009-01-05 19:53:30 +0000144 assert outputType is Types.ImageType
145
146 cmd_args = []
147 for arg in args:
Daniel Dunbara75ea3d2009-01-09 22:21:24 +0000148 if arg.opt.name in ('-framework',):
149 cmd_args.extend(arglist.render(arg))
Daniel Dunbara5677512009-01-05 19:53:30 +0000150 for input in inputs:
Daniel Dunbardb439902009-01-07 18:40:45 +0000151 cmd_args.append(arglist.getValue(input.source))
Daniel Dunbar39cbfaa2009-01-07 18:54:26 +0000152 cmd_args.extend(arglist.render(output))
Daniel Dunbardb439902009-01-07 18:40:45 +0000153 cmd_args.extend(['-L/usr/lib/gcc/i686-apple-darwin10/4.2.1',
154 '-lcrt1.10.5.o',
155 '-lgcc_s.10.5',
156 '-lgcc',
157 '-lSystem'])
Daniel Dunbara5677512009-01-05 19:53:30 +0000158 jobs.addJob(Jobs.Command(self.kCollect2Path, cmd_args))
159
160class LipoTool(Tool):
161 def __init__(self):
162 super(LipoTool, self).__init__('lipo')
163
164 def constructJob(self, phase, arch, jobs, inputs,
Daniel Dunbardb439902009-01-07 18:40:45 +0000165 output, outputType, args, arglist):
Daniel Dunbara5677512009-01-05 19:53:30 +0000166 assert outputType is Types.ImageType
167
Daniel Dunbardb439902009-01-07 18:40:45 +0000168 cmd_args = ['-create']
Daniel Dunbar39cbfaa2009-01-07 18:54:26 +0000169 cmd_args.extend(arglist.render(output))
Daniel Dunbara5677512009-01-05 19:53:30 +0000170 for input in inputs:
Daniel Dunbardb439902009-01-07 18:40:45 +0000171 cmd_args.append(arglist.getValue(input.source))
Daniel Dunbara5677512009-01-05 19:53:30 +0000172 jobs.addJob(Jobs.Command('lipo', cmd_args))