target.pas 8.6 KB

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