target.pas 6.5 KB

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