target.pas 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. alias : string;
  37. end;
  38. TFormatInfo = record
  39. name : string;
  40. ext : string;
  41. machines : TMachineTypes;
  42. end;
  43. TResTarget = record
  44. machine : TMachineType;
  45. submachine : TSubMachineType;
  46. objformat : TObjFormat;
  47. end;
  48. function GetDefaultMachineForFormat(aFormat : TObjFormat) : TMachineType;
  49. function GetDefaultSubMachineForMachine(aMachine: TMachineType) : TSubMachineType;
  50. function TargetToStr(const aTarget : TResTarget) : string;
  51. function MachineToStr(const aMachine : TMachineType) : string;
  52. function ObjFormatToStr(const aFormat : TObjFormat) : string;
  53. var
  54. Machines : array[TMachineType] of TMachineInfo =
  55. (
  56. (name : ''; formats : [ofRes]), //mtnone
  57. (name : 'i386'; formats : [ofElf, ofCoff, ofMachO]), //mti386
  58. (name : 'x86_64'; formats : [ofElf, ofCoff, ofMachO]), //mtx86_64
  59. (name : 'powerpc'; formats : [ofElf, ofXCoff, ofMachO]), //mtppc
  60. (name : 'powerpc64'; formats : [ofElf, {ofXCoff,} ofMachO]), //mtppc64
  61. (name : 'arm'; formats : [ofElf, ofCoff, ofMachO]), //mtarm
  62. (name : 'armeb'; formats : [ofElf]), //mtarmeb
  63. (name : 'm68k'; formats : [ofElf]), //mtm68k
  64. (name : 'sparc'; formats : [ofElf]), //mtsparc
  65. (name : 'alpha'; formats : [ofElf]), //mtalpha
  66. (name : 'ia64'; formats : [ofElf]), //mtia64
  67. (name : 'mips'; formats : [ofElf]; alias : 'mipseb'), //mtmips
  68. (name : 'mipsel'; formats : [ofElf]), //mtmipsel
  69. (name : 'bigendian'; formats : [ofExt]), //mtBigEndian
  70. (name : 'littleendian'; formats : [ofExt]) //mtLittleEndian
  71. );
  72. SubMachinesArm: array[TSubMachineTypeArm] of string[8] =
  73. ('all','armv4','armv6','armv5tej','xscale','armv7');
  74. SubMachinesGen: array[TSubMachineTypeGeneric] of string[3] =
  75. ('all');
  76. ObjFormats : array[TObjFormat] of TFormatInfo =
  77. (
  78. (name : ''; ext : ''; machines : []),
  79. (name : 'res'; ext : '.res'; machines : [mtnone]),
  80. (name : 'elf'; ext : '.or'; machines : [mti386,mtx86_64,mtppc,
  81. mtppc64,mtarm,mtarmeb,
  82. mtm68k,mtsparc,mtalpha,
  83. mtia64,mtmips,mtmipsel]),
  84. (name : 'coff'; ext : '.o'; machines : [mti386,mtx86_64,mtarm,
  85. mtppc,mtppc64]),
  86. (name : 'xcoff'; ext : '.o'; machines : [mtppc{,mtppc64}]),
  87. (name : 'mach-o'; ext : '.or'; machines : [mti386,mtx86_64,mtppc,
  88. mtppc64,mtarm]),
  89. (name : 'external'; ext : '.fpcres'; machines : [mtBigEndian,mtLittleEndian])
  90. );
  91. CurrentTarget : TResTarget =
  92. (
  93. {$IFDEF CPUI386}
  94. machine : mti386;
  95. submachine : (subgen: smtgen_all);
  96. {$ELSE}
  97. {$IFDEF CPUX86_64}
  98. machine : mtx86_64;
  99. submachine : (subgen: smtgen_all);
  100. {$ELSE}
  101. {$IFDEF CPUPOWERPC32}
  102. machine : mtppc;
  103. submachine : (subgen: smtgen_all);
  104. {$ELSE}
  105. {$IFDEF CPUPOWERPC64}
  106. machine : mtppc64;
  107. submachine : (subgen: smtgen_all);
  108. {$ELSE}
  109. {$IFDEF CPUARM}
  110. {$IFDEF ENDIAN_LITTLE}
  111. machine : mtarm;
  112. submachine : (subarm: smtarm_all);
  113. {$ELSE}
  114. machine : mtarmeb;
  115. submachine : (subarm: smtarm_all);
  116. {$ENDIF}
  117. {$ELSE}
  118. {$IFDEF CPU68K}
  119. machine : mtm68k;
  120. submachine : (subgen: smtgen_all);
  121. {$ELSE}
  122. {$IFDEF CPUSPARC}
  123. machine : mtsparc;
  124. submachine : (subgen: smtgen_all);
  125. {$ELSE}
  126. {$IFDEF CPUALPHA}
  127. machine : mtalpha;
  128. submachine : (subgen: smtgen_all);
  129. {$ELSE}
  130. {$IFDEF CPUIA64}
  131. machine : mtia64;
  132. submachine : (subgen: smtgen_all);
  133. {$ELSE}
  134. {$IFDEF CPUMIPSEL}
  135. machine : mtmipsel;
  136. submachine : (subgen: smtgen_all);
  137. {$ELSE}
  138. {$IFDEF CPUMIPS}
  139. machine : mtmips;
  140. submachine : (subgen: smtgen_all);
  141. {$ELSE}
  142. machine : mti386; //default i386
  143. submachine : (subgen: smtgen_all);
  144. {$ENDIF}
  145. {$ENDIF}
  146. {$ENDIF}
  147. {$ENDIF}
  148. {$ENDIF}
  149. {$ENDIF}
  150. {$ENDIF}
  151. {$ENDIF}
  152. {$ENDIF}
  153. {$ENDIF}
  154. {$ENDIF}
  155. {$IFDEF WINDOWS}
  156. objformat : ofCoff;
  157. {$ELSE}
  158. {$IF defined(DARWIN)}
  159. objformat : ofMachO;
  160. {$ELSEIF defined(AIX)}
  161. objformat : ofXCoff;
  162. {$ELSE}
  163. objformat : ofElf;
  164. {$ENDIF}
  165. {$ENDIF}
  166. );
  167. implementation
  168. function GetDefaultMachineForFormat(aFormat : TObjFormat) : TMachineType;
  169. begin
  170. case aFormat of
  171. ofNone : Result:=mtnone;
  172. ofRes : Result:=mtnone;
  173. ofElf : Result:=mti386;
  174. ofCoff : Result:=mti386;
  175. ofXCoff: Result:=mtppc;
  176. ofMachO: Result:=mti386;
  177. {$IFDEF ENDIAN_BIG}
  178. ofExt : Result:=mtBigEndian;
  179. {$ELSE}
  180. ofExt : Result:=mtLittleEndian;
  181. {$ENDIF}
  182. end;
  183. end;
  184. function MachineToStr(const aMachine : TMachineType) : string;
  185. begin
  186. Result:=Machines[aMachine].name;
  187. end;
  188. function SubMachineToStr(const aMachine : TMachineType; const aSubMachine : TSubMachineType) : string;
  189. begin
  190. case aMachine of
  191. mtarm,mtarmeb:
  192. result:=SubMachinesArm[aSubMachine.subarm];
  193. else
  194. // no need to confuse people with the "all" suffix, it doesn't do
  195. // anything anyway
  196. result:='';
  197. end;
  198. end;
  199. function ObjFormatToStr(const aFormat : TObjFormat) : string;
  200. begin
  201. Result:=ObjFormats[aFormat].name;
  202. end;
  203. function GetDefaultSubMachineForMachine(aMachine: TMachineType): TSubMachineType;
  204. begin
  205. case aMachine of
  206. mtarm,mtarmeb:
  207. result.subarm:=smtarm_all;
  208. else
  209. result.subgen:=smtgen_all;
  210. end;
  211. end;
  212. function TargetToStr(const aTarget : TResTarget) : string;
  213. var s1, s2, s3 : string;
  214. begin
  215. s1:=MachineToStr(aTarget.machine);
  216. s2:=ObjFormatToStr(aTarget.objformat);
  217. s3:=SubMachineToStr(aTarget.Machine,aTarget.submachine);
  218. if (s1='') or (s2='') then Result:=s1+s2
  219. else Result:=s1+' - '+s2;
  220. if s3<>'' then
  221. Result:=Result+'-'+s3;
  222. end;
  223. end.