target.pas 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. {
  2. FPCRes - Free Pascal Resource Converter
  3. Part of the Free Pascal distribution
  4. Copyright (C) 2008 by Giulio Bernardi
  5. Target selection and definitions
  6. See the file COPYING, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. }
  12. unit target;
  13. {$MODE OBJFPC}
  14. interface
  15. type
  16. TMachineType = (mtnone, mti386,mtx86_64,mtppc,mtppc64,mtarm,mtarmeb,mtm68k,
  17. mtsparc,mtalpha,mtia64,mtmips,mtmipsel,
  18. mtBigEndian,mtLittleEndian);
  19. TMachineTypes = set of TMachineType;
  20. TSubMachineTypeArm = (smtarm_all,smtarm_v4t,smtarm_v6,smtarm_v5tej,smtarm_xscale,smtarm_v7);
  21. TSubMachineTypeGeneric = (smtgen_all);
  22. TSubMachineType = record
  23. case TMachineType of
  24. mtarm,mtarmeb:
  25. (subarm: TSubMachineTypeArm);
  26. mtnone, mti386,mtx86_64,mtppc,mtppc64,mtm68k,
  27. mtsparc,mtalpha,mtia64,mtmips,mtmipsel,
  28. mtBigEndian,mtLittleEndian:
  29. (subgen: TSubMachineTypeGeneric);
  30. end;
  31. TObjFormat = (ofNone, ofRes, ofElf, ofCoff, ofXCoff, ofMachO, ofExt);
  32. TObjFormats = set of TObjFormat;
  33. TMachineInfo = record
  34. name : string;
  35. formats : TObjFormats;
  36. end;
  37. TFormatInfo = record
  38. name : string;
  39. ext : string;
  40. machines : TMachineTypes;
  41. end;
  42. TResTarget = record
  43. machine : TMachineType;
  44. submachine : TSubMachineType;
  45. objformat : TObjFormat;
  46. end;
  47. function GetDefaultMachineForFormat(aFormat : TObjFormat) : TMachineType;
  48. function GetDefaultSubMachineForMachine(aMachine: TMachineType) : TSubMachineType;
  49. function TargetToStr(const aTarget : TResTarget) : string;
  50. function MachineToStr(const aMachine : TMachineType) : string;
  51. function ObjFormatToStr(const aFormat : TObjFormat) : string;
  52. var
  53. Machines : array[TMachineType] of TMachineInfo =
  54. (
  55. (name : ''; formats : [ofRes]), //mtnone
  56. (name : 'i386'; formats : [ofElf, ofCoff, ofMachO]), //mti386
  57. (name : 'x86_64'; formats : [ofElf, ofCoff, ofMachO]), //mtx86_64
  58. (name : 'powerpc'; formats : [ofElf, ofXCoff, ofMachO]), //mtppc
  59. (name : 'powerpc64'; formats : [ofElf, ofMachO]), //mtppc64
  60. (name : 'arm'; formats : [ofElf, ofCoff, ofMachO]), //mtarm
  61. (name : 'armeb'; formats : [ofElf]), //mtarmeb
  62. (name : 'm68k'; formats : [ofElf]), //mtm68k
  63. (name : 'sparc'; formats : [ofElf]), //mtsparc
  64. (name : 'alpha'; formats : [ofElf]), //mtalpha
  65. (name : 'ia64'; formats : [ofElf]), //mtia64
  66. (name : 'mips'; formats : [ofElf]), //mtmips
  67. (name : 'mipsel'; formats : [ofElf]), //mtmipsel
  68. (name : 'bigendian'; formats : [ofExt]), //mtBigEndian
  69. (name : 'littleendian'; formats : [ofExt]) //mtLittleEndian
  70. );
  71. SubMachinesArm: array[TSubMachineTypeArm] of string[8] =
  72. ('all','armv4','armv6','armv5tej','xscale','armv7');
  73. SubMachinesGen: array[TSubMachineTypeGeneric] of string[3] =
  74. ('all');
  75. ObjFormats : array[TObjFormat] of TFormatInfo =
  76. (
  77. (name : ''; ext : ''; machines : []),
  78. (name : 'res'; ext : '.res'; machines : [mtnone]),
  79. (name : 'elf'; ext : '.or'; machines : [mti386,mtx86_64,mtppc,
  80. mtppc64,mtarm,mtarmeb,
  81. mtm68k,mtsparc,mtalpha,
  82. mtia64,mtmips,mtmipsel]),
  83. (name : 'coff'; ext : '.o'; machines : [mti386,mtx86_64,mtarm,
  84. mtppc,mtppc64]),
  85. (name : 'xcoff'; ext : '.o'; machines : [mtppc{,mtppc64}]),
  86. (name : 'mach-o'; ext : '.or'; machines : [mti386,mtx86_64,mtppc,
  87. mtppc64,mtarm]),
  88. (name : 'external'; ext : '.fpcres'; machines : [mtBigEndian,mtLittleEndian])
  89. );
  90. CurrentTarget : TResTarget =
  91. (
  92. {$IFDEF CPUI386}
  93. machine : mti386;
  94. submachine : (subgen: smtgen_all);
  95. {$ELSE}
  96. {$IFDEF CPUX86_64}
  97. machine : mtx86_64;
  98. submachine : (subgen: smtgen_all);
  99. {$ELSE}
  100. {$IFDEF CPUPOWERPC32}
  101. machine : mtppc;
  102. submachine : (subgen: smtgen_all);
  103. {$ELSE}
  104. {$IFDEF CPUPOWERPC64}
  105. machine : mtppc64;
  106. submachine : (subgen: smtgen_all);
  107. {$ELSE}
  108. {$IFDEF CPUARM}
  109. {$IFDEF ENDIAN_LITTLE}
  110. machine : mtarm;
  111. submachine : (subarm: smtarm_all);
  112. {$ELSE}
  113. machine : mtarmeb;
  114. submachine : (subarm: smtarm_all);
  115. {$ENDIF}
  116. {$ELSE}
  117. {$IFDEF CPU68K}
  118. machine : mtm68k;
  119. submachine : (subgen: smtgen_all);
  120. {$ELSE}
  121. {$IFDEF CPUSPARC}
  122. machine : mtsparc;
  123. submachine : (subgen: smtgen_all);
  124. {$ELSE}
  125. {$IFDEF CPUALPHA}
  126. machine : mtalpha;
  127. submachine : (subgen: smtgen_all);
  128. {$ELSE}
  129. {$IFDEF CPUIA64}
  130. machine : mtia64;
  131. submachine : (subgen: smtgen_all);
  132. {$ELSE}
  133. {$IFDEF CPUMIPSEL}
  134. machine : mtmipsel;
  135. submachine : (subgen: smtgen_all);
  136. {$ELSE}
  137. {$IFDEF CPUMIPS}
  138. machine : mtmips;
  139. submachine : (subgen: smtgen_all);
  140. {$ELSE}
  141. machine : mti386; //default i386
  142. submachine : (subgen: smtgen_all);
  143. {$ENDIF}
  144. {$ENDIF}
  145. {$ENDIF}
  146. {$ENDIF}
  147. {$ENDIF}
  148. {$ENDIF}
  149. {$ENDIF}
  150. {$ENDIF}
  151. {$ENDIF}
  152. {$ENDIF}
  153. {$ENDIF}
  154. {$IFDEF WINDOWS}
  155. objformat : ofCoff;
  156. {$ELSE}
  157. {$IF defined(DARWIN)}
  158. objformat : ofMachO;
  159. {$ELSEIF defined(AIX)}
  160. objformat : ofXCoff;
  161. {$ELSE}
  162. objformat : ofElf;
  163. {$ENDIF}
  164. {$ENDIF}
  165. );
  166. implementation
  167. function GetDefaultMachineForFormat(aFormat : TObjFormat) : TMachineType;
  168. begin
  169. case aFormat of
  170. ofNone : Result:=mtnone;
  171. ofRes : Result:=mtnone;
  172. ofElf : Result:=mti386;
  173. ofCoff : Result:=mti386;
  174. ofXCoff: Result:=mtppc;
  175. ofMachO: Result:=mti386;
  176. {$IFDEF ENDIAN_BIG}
  177. ofExt : Result:=mtBigEndian;
  178. {$ELSE}
  179. ofExt : Result:=mtLittleEndian;
  180. {$ENDIF}
  181. end;
  182. end;
  183. function MachineToStr(const aMachine : TMachineType) : string;
  184. begin
  185. Result:=Machines[aMachine].name;
  186. end;
  187. function SubMachineToStr(const aMachine : TMachineType; const aSubMachine : TSubMachineType) : string;
  188. begin
  189. case aMachine of
  190. mtarm,mtarmeb:
  191. result:=SubMachinesArm[aSubMachine.subarm];
  192. else
  193. // no need to confuse people with the "all" suffix, it doesn't do
  194. // anything anyway
  195. result:='';
  196. end;
  197. end;
  198. function ObjFormatToStr(const aFormat : TObjFormat) : string;
  199. begin
  200. Result:=ObjFormats[aFormat].name;
  201. end;
  202. function GetDefaultSubMachineForMachine(aMachine: TMachineType): TSubMachineType;
  203. begin
  204. case aMachine of
  205. mtarm,mtarmeb:
  206. result.subarm:=smtarm_all;
  207. else
  208. result.subgen:=smtgen_all;
  209. end;
  210. end;
  211. function TargetToStr(const aTarget : TResTarget) : string;
  212. var s1, s2, s3 : string;
  213. begin
  214. s1:=MachineToStr(aTarget.machine);
  215. s2:=ObjFormatToStr(aTarget.objformat);
  216. s3:=SubMachineToStr(aTarget.Machine,aTarget.submachine);
  217. if (s1='') or (s2='') then Result:=s1+s2
  218. else Result:=s1+' - '+s2;
  219. if s3<>'' then
  220. Result:=Result+'-'+s3;
  221. end;
  222. end.