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): |
| 6 | """Tool - A concrete implementation of a phase.""" |
| 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, |
| 25 | output, outputType, args, |
| 26 | extraArgs): |
| 27 | assert len(inputs) == 1 |
| 28 | |
| 29 | input = inputs[0] |
| 30 | |
| 31 | cmd_args = args + extraArgs |
| 32 | if arch: |
| 33 | # FIXME: Clean this up. |
| 34 | if isinstance(arch, Arguments.DerivedArg): |
| 35 | cmd_args.extend([Arguments.DerivedArg('-arch'), |
| 36 | arch]) |
| 37 | else: |
| 38 | cmd_args.append(arch) |
| 39 | if isinstance(output, Jobs.PipedJob): |
| 40 | cmd_args.extend([Arguments.DerivedArg('-o'), Arguments.DerivedArg('-')]) |
| 41 | elif output is None: |
| 42 | cmd_args.append(Arguments.DerivedArg('-fsyntax-only')) |
| 43 | else: |
| 44 | # FIXME: Ditch this hack. |
| 45 | if isinstance(output, Arguments.DerivedArg): |
| 46 | cmd_args.extend([Arguments.DerivedArg('-o'), output]) |
| 47 | else: |
| 48 | cmd_args.append(output) |
| 49 | |
| 50 | cmd_args.extend([Arguments.DerivedArg('-x'), |
| 51 | Arguments.DerivedArg(input.type.name)]) |
| 52 | if isinstance(input.source, Jobs.PipedJob): |
| 53 | cmd_args.append(Arguments.DerivedArg('-')) |
| 54 | else: |
| 55 | cmd_args.append(input.source) |
| 56 | |
| 57 | jobs.addJob(Jobs.Command('gcc', cmd_args)) |
| 58 | |
| 59 | class GCC_PreprocessTool(GCC_Common_Tool): |
| 60 | def __init__(self): |
| 61 | super(GCC_PreprocessTool, self).__init__('gcc', |
| 62 | (Tool.eFlagsPipedInput | |
| 63 | Tool.eFlagsPipedOutput)) |
| 64 | |
| 65 | def constructJob(self, phase, arch, jobs, inputs, |
| 66 | output, outputType, args): |
| 67 | return super(GCC_PreprocessTool, self).constructJob(phase, arch, jobs, inputs, |
| 68 | output, outputType, args, |
| 69 | [Arguments.DerivedArg('-E')]) |
| 70 | |
| 71 | class GCC_CompileTool(GCC_Common_Tool): |
| 72 | def __init__(self): |
| 73 | super(GCC_CompileTool, self).__init__('gcc', |
| 74 | (Tool.eFlagsPipedInput | |
| 75 | Tool.eFlagsPipedOutput | |
| 76 | Tool.eFlagsIntegratedCPP)) |
| 77 | |
| 78 | def constructJob(self, phase, arch, jobs, inputs, |
| 79 | output, outputType, args): |
| 80 | return super(GCC_CompileTool, self).constructJob(phase, arch, jobs, inputs, |
| 81 | output, outputType, args, |
| 82 | [Arguments.DerivedArg('-S')]) |
| 83 | |
| 84 | class GCC_PrecompileTool(GCC_Common_Tool): |
| 85 | def __init__(self): |
| 86 | super(GCC_PrecompileTool, self).__init__('gcc', |
| 87 | (Tool.eFlagsPipedInput | |
| 88 | Tool.eFlagsIntegratedCPP)) |
| 89 | |
| 90 | def constructJob(self, phase, arch, jobs, inputs, |
| 91 | output, outputType, args): |
| 92 | return super(GCC_PrecompileTool, self).constructJob(phase, arch, jobs, inputs, |
| 93 | output, outputType, args, |
| 94 | []) |
| 95 | |
| 96 | class DarwinAssemblerTool(Tool): |
| 97 | def __init__(self): |
| 98 | super(DarwinAssemblerTool, self).__init__('as', |
| 99 | Tool.eFlagsPipedInput) |
| 100 | |
| 101 | def constructJob(self, phase, arch, jobs, inputs, |
| 102 | output, outputType, args): |
| 103 | assert len(inputs) == 1 |
| 104 | assert outputType is Types.ObjectType |
| 105 | |
| 106 | input = inputs[0] |
| 107 | |
| 108 | cmd_args = [] |
| 109 | if arch: |
| 110 | # FIXME: Clean this up. |
| 111 | if isinstance(arch, Arguments.DerivedArg): |
| 112 | cmd_args.extend([Arguments.DerivedArg('-arch'), |
| 113 | arch]) |
| 114 | else: |
| 115 | cmd_args.append(arch) |
| 116 | cmd_args.append(Arguments.DerivedArg('-force_cpusubtype_ALL')) |
| 117 | if isinstance(output, Arguments.DerivedArg): |
| 118 | cmd_args.extend([Arguments.DerivedArg('-o'), output]) |
| 119 | else: |
| 120 | cmd_args.append(output) |
| 121 | if isinstance(input.source, Jobs.PipedJob): |
| 122 | cmd_args.append(Arguments.DerivedArg('-')) |
| 123 | else: |
| 124 | cmd_args.append(input.source) |
| 125 | jobs.addJob(Jobs.Command('as', cmd_args)) |
| 126 | |
| 127 | class Collect2Tool(Tool): |
| 128 | kCollect2Path = '/usr/libexec/gcc/i686-apple-darwin10/4.2.1/collect2' |
| 129 | def __init__(self): |
| 130 | super(Collect2Tool, self).__init__('collect2') |
| 131 | |
| 132 | def constructJob(self, phase, arch, jobs, inputs, |
| 133 | output, outputType, args): |
| 134 | assert outputType is Types.ImageType |
| 135 | |
| 136 | cmd_args = [] |
| 137 | for arg in args: |
| 138 | if arg.opt: |
| 139 | if arg.opt.name in ('-framework',): |
| 140 | cmd_args.append(arg) |
| 141 | for input in inputs: |
| 142 | cmd_args.append(input.source) |
| 143 | if isinstance(output, Arguments.DerivedArg): |
| 144 | cmd_args.extend([Arguments.DerivedArg('-o'), output]) |
| 145 | else: |
| 146 | cmd_args.append(output) |
| 147 | cmd_args.extend([Arguments.DerivedArg('-L/usr/lib/gcc/i686-apple-darwin10/4.2.1'), |
| 148 | Arguments.DerivedArg('-lcrt1.10.5.o'), |
| 149 | Arguments.DerivedArg('-lgcc_s.10.5'), |
| 150 | Arguments.DerivedArg('-lgcc'), |
| 151 | Arguments.DerivedArg('-lSystem')]) |
| 152 | jobs.addJob(Jobs.Command(self.kCollect2Path, cmd_args)) |
| 153 | |
| 154 | class LipoTool(Tool): |
| 155 | def __init__(self): |
| 156 | super(LipoTool, self).__init__('lipo') |
| 157 | |
| 158 | def constructJob(self, phase, arch, jobs, inputs, |
| 159 | output, outputType, args): |
| 160 | assert outputType is Types.ImageType |
| 161 | |
| 162 | cmd_args = [Arguments.DerivedArg('-create')] |
| 163 | if isinstance(output, Arguments.DerivedArg): |
| 164 | cmd_args.extend([Arguments.DerivedArg('-o'), output]) |
| 165 | else: |
| 166 | cmd_args.append(output) |
| 167 | for input in inputs: |
| 168 | cmd_args.append(input.source) |
| 169 | jobs.addJob(Jobs.Command('lipo', cmd_args)) |