2
0

target.pas 7.6 KB

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