target.pas 7.3 KB

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