Daniel Dunbar | a567751 | 2009-01-05 19:53:30 +0000 | [diff] [blame] | 1 | import Arguments |
| 2 | import Jobs |
| 3 | import Types |
| 4 | |
| 5 | class Tool(object): |
Daniel Dunbar | ba6e323 | 2009-01-06 06:12:13 +0000 | [diff] [blame] | 6 | """Tool - A concrete implementation of an action.""" |
Daniel Dunbar | a567751 | 2009-01-05 19:53:30 +0000 | [diff] [blame] | 7 | |
| 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 | |
| 23 | class GCC_Common_Tool(Tool): |
| 24 | def constructJob(self, phase, arch, jobs, inputs, |
Daniel Dunbar | db43990 | 2009-01-07 18:40:45 +0000 | [diff] [blame] | 25 | output, outputType, args, arglist, |
Daniel Dunbar | a567751 | 2009-01-05 19:53:30 +0000 | [diff] [blame] | 26 | extraArgs): |
Daniel Dunbar | db43990 | 2009-01-07 18:40:45 +0000 | [diff] [blame] | 27 | cmd_args = sum(map(arglist.render, args),[]) + extraArgs |
Daniel Dunbar | a567751 | 2009-01-05 19:53:30 +0000 | [diff] [blame] | 28 | if arch: |
Daniel Dunbar | 39cbfaa | 2009-01-07 18:54:26 +0000 | [diff] [blame] | 29 | cmd_args.extend(arglist.render(arch)) |
Daniel Dunbar | a567751 | 2009-01-05 19:53:30 +0000 | [diff] [blame] | 30 | if isinstance(output, Jobs.PipedJob): |
Daniel Dunbar | db43990 | 2009-01-07 18:40:45 +0000 | [diff] [blame] | 31 | cmd_args.extend(['-o', '-']) |
Daniel Dunbar | a567751 | 2009-01-05 19:53:30 +0000 | [diff] [blame] | 32 | elif output is None: |
Daniel Dunbar | db43990 | 2009-01-07 18:40:45 +0000 | [diff] [blame] | 33 | cmd_args.append('-fsyntax-only') |
Daniel Dunbar | a567751 | 2009-01-05 19:53:30 +0000 | [diff] [blame] | 34 | else: |
Daniel Dunbar | 39cbfaa | 2009-01-07 18:54:26 +0000 | [diff] [blame] | 35 | cmd_args.extend(arglist.render(output)) |
Daniel Dunbar | a567751 | 2009-01-05 19:53:30 +0000 | [diff] [blame] | 36 | |
Daniel Dunbar | a9ad2bc | 2009-01-10 02:00:04 +0000 | [diff] [blame] | 37 | # 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 Dunbar | a567751 | 2009-01-05 19:53:30 +0000 | [diff] [blame] | 52 | |
| 53 | jobs.addJob(Jobs.Command('gcc', cmd_args)) |
| 54 | |
| 55 | class GCC_PreprocessTool(GCC_Common_Tool): |
| 56 | def __init__(self): |
Daniel Dunbar | a9ad2bc | 2009-01-10 02:00:04 +0000 | [diff] [blame] | 57 | super(GCC_PreprocessTool, self).__init__('gcc (cpp)', |
Daniel Dunbar | a567751 | 2009-01-05 19:53:30 +0000 | [diff] [blame] | 58 | (Tool.eFlagsPipedInput | |
| 59 | Tool.eFlagsPipedOutput)) |
| 60 | |
| 61 | def constructJob(self, phase, arch, jobs, inputs, |
Daniel Dunbar | db43990 | 2009-01-07 18:40:45 +0000 | [diff] [blame] | 62 | output, outputType, args, arglist): |
Daniel Dunbar | a567751 | 2009-01-05 19:53:30 +0000 | [diff] [blame] | 63 | return super(GCC_PreprocessTool, self).constructJob(phase, arch, jobs, inputs, |
Daniel Dunbar | db43990 | 2009-01-07 18:40:45 +0000 | [diff] [blame] | 64 | output, outputType, args, arglist, |
| 65 | ['-E']) |
Daniel Dunbar | a567751 | 2009-01-05 19:53:30 +0000 | [diff] [blame] | 66 | |
| 67 | class GCC_CompileTool(GCC_Common_Tool): |
| 68 | def __init__(self): |
Daniel Dunbar | a9ad2bc | 2009-01-10 02:00:04 +0000 | [diff] [blame] | 69 | super(GCC_CompileTool, self).__init__('gcc (cc1)', |
Daniel Dunbar | a567751 | 2009-01-05 19:53:30 +0000 | [diff] [blame] | 70 | (Tool.eFlagsPipedInput | |
| 71 | Tool.eFlagsPipedOutput | |
| 72 | Tool.eFlagsIntegratedCPP)) |
| 73 | |
| 74 | def constructJob(self, phase, arch, jobs, inputs, |
Daniel Dunbar | db43990 | 2009-01-07 18:40:45 +0000 | [diff] [blame] | 75 | output, outputType, args, arglist): |
Daniel Dunbar | a567751 | 2009-01-05 19:53:30 +0000 | [diff] [blame] | 76 | return super(GCC_CompileTool, self).constructJob(phase, arch, jobs, inputs, |
Daniel Dunbar | db43990 | 2009-01-07 18:40:45 +0000 | [diff] [blame] | 77 | output, outputType, args, arglist, |
| 78 | ['-S']) |
Daniel Dunbar | a567751 | 2009-01-05 19:53:30 +0000 | [diff] [blame] | 79 | |
| 80 | class GCC_PrecompileTool(GCC_Common_Tool): |
| 81 | def __init__(self): |
Daniel Dunbar | a9ad2bc | 2009-01-10 02:00:04 +0000 | [diff] [blame] | 82 | super(GCC_PrecompileTool, self).__init__('gcc (pch)', |
Daniel Dunbar | a567751 | 2009-01-05 19:53:30 +0000 | [diff] [blame] | 83 | (Tool.eFlagsPipedInput | |
| 84 | Tool.eFlagsIntegratedCPP)) |
| 85 | |
| 86 | def constructJob(self, phase, arch, jobs, inputs, |
Daniel Dunbar | db43990 | 2009-01-07 18:40:45 +0000 | [diff] [blame] | 87 | output, outputType, args, arglist): |
Daniel Dunbar | a567751 | 2009-01-05 19:53:30 +0000 | [diff] [blame] | 88 | return super(GCC_PrecompileTool, self).constructJob(phase, arch, jobs, inputs, |
Daniel Dunbar | db43990 | 2009-01-07 18:40:45 +0000 | [diff] [blame] | 89 | output, outputType, args, arglist, |
Daniel Dunbar | a567751 | 2009-01-05 19:53:30 +0000 | [diff] [blame] | 90 | []) |
| 91 | |
Daniel Dunbar | a9ad2bc | 2009-01-10 02:00:04 +0000 | [diff] [blame] | 92 | class DarwinAssembleTool(Tool): |
Daniel Dunbar | a567751 | 2009-01-05 19:53:30 +0000 | [diff] [blame] | 93 | def __init__(self): |
Daniel Dunbar | a9ad2bc | 2009-01-10 02:00:04 +0000 | [diff] [blame] | 94 | super(DarwinAssembleTool, self).__init__('as', |
| 95 | Tool.eFlagsPipedInput) |
Daniel Dunbar | a567751 | 2009-01-05 19:53:30 +0000 | [diff] [blame] | 96 | |
| 97 | def constructJob(self, phase, arch, jobs, inputs, |
Daniel Dunbar | db43990 | 2009-01-07 18:40:45 +0000 | [diff] [blame] | 98 | output, outputType, args, arglist): |
Daniel Dunbar | a567751 | 2009-01-05 19:53:30 +0000 | [diff] [blame] | 99 | assert len(inputs) == 1 |
| 100 | assert outputType is Types.ObjectType |
| 101 | |
| 102 | input = inputs[0] |
| 103 | |
| 104 | cmd_args = [] |
| 105 | if arch: |
Daniel Dunbar | 39cbfaa | 2009-01-07 18:54:26 +0000 | [diff] [blame] | 106 | cmd_args.extend(arglist.render(arch)) |
Daniel Dunbar | db43990 | 2009-01-07 18:40:45 +0000 | [diff] [blame] | 107 | cmd_args.append('-force_cpusubtype_ALL') |
Daniel Dunbar | 39cbfaa | 2009-01-07 18:54:26 +0000 | [diff] [blame] | 108 | cmd_args.extend(arglist.render(output)) |
Daniel Dunbar | a567751 | 2009-01-05 19:53:30 +0000 | [diff] [blame] | 109 | if isinstance(input.source, Jobs.PipedJob): |
Daniel Dunbar | db43990 | 2009-01-07 18:40:45 +0000 | [diff] [blame] | 110 | cmd_args.append('-') |
Daniel Dunbar | a567751 | 2009-01-05 19:53:30 +0000 | [diff] [blame] | 111 | else: |
Daniel Dunbar | db43990 | 2009-01-07 18:40:45 +0000 | [diff] [blame] | 112 | cmd_args.append(arglist.getValue(input.source)) |
Daniel Dunbar | a567751 | 2009-01-05 19:53:30 +0000 | [diff] [blame] | 113 | jobs.addJob(Jobs.Command('as', cmd_args)) |
| 114 | |
Daniel Dunbar | a9ad2bc | 2009-01-10 02:00:04 +0000 | [diff] [blame] | 115 | class 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 | |
| 127 | class 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 Dunbar | a567751 | 2009-01-05 19:53:30 +0000 | [diff] [blame] | 137 | class 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 Dunbar | db43990 | 2009-01-07 18:40:45 +0000 | [diff] [blame] | 143 | output, outputType, args, arglist): |
Daniel Dunbar | a567751 | 2009-01-05 19:53:30 +0000 | [diff] [blame] | 144 | assert outputType is Types.ImageType |
| 145 | |
| 146 | cmd_args = [] |
| 147 | for arg in args: |
Daniel Dunbar | a75ea3d | 2009-01-09 22:21:24 +0000 | [diff] [blame] | 148 | if arg.opt.name in ('-framework',): |
| 149 | cmd_args.extend(arglist.render(arg)) |
Daniel Dunbar | a567751 | 2009-01-05 19:53:30 +0000 | [diff] [blame] | 150 | for input in inputs: |
Daniel Dunbar | db43990 | 2009-01-07 18:40:45 +0000 | [diff] [blame] | 151 | cmd_args.append(arglist.getValue(input.source)) |
Daniel Dunbar | 39cbfaa | 2009-01-07 18:54:26 +0000 | [diff] [blame] | 152 | cmd_args.extend(arglist.render(output)) |
Daniel Dunbar | db43990 | 2009-01-07 18:40:45 +0000 | [diff] [blame] | 153 | 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 Dunbar | a567751 | 2009-01-05 19:53:30 +0000 | [diff] [blame] | 158 | jobs.addJob(Jobs.Command(self.kCollect2Path, cmd_args)) |
| 159 | |
| 160 | class LipoTool(Tool): |
| 161 | def __init__(self): |
| 162 | super(LipoTool, self).__init__('lipo') |
| 163 | |
| 164 | def constructJob(self, phase, arch, jobs, inputs, |
Daniel Dunbar | db43990 | 2009-01-07 18:40:45 +0000 | [diff] [blame] | 165 | output, outputType, args, arglist): |
Daniel Dunbar | a567751 | 2009-01-05 19:53:30 +0000 | [diff] [blame] | 166 | assert outputType is Types.ImageType |
| 167 | |
Daniel Dunbar | db43990 | 2009-01-07 18:40:45 +0000 | [diff] [blame] | 168 | cmd_args = ['-create'] |
Daniel Dunbar | 39cbfaa | 2009-01-07 18:54:26 +0000 | [diff] [blame] | 169 | cmd_args.extend(arglist.render(output)) |
Daniel Dunbar | a567751 | 2009-01-05 19:53:30 +0000 | [diff] [blame] | 170 | for input in inputs: |
Daniel Dunbar | db43990 | 2009-01-07 18:40:45 +0000 | [diff] [blame] | 171 | cmd_args.append(arglist.getValue(input.source)) |
Daniel Dunbar | a567751 | 2009-01-05 19:53:30 +0000 | [diff] [blame] | 172 | jobs.addJob(Jobs.Command('lipo', cmd_args)) |