target.pas 8.1 KB

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