target.pas 8.9 KB

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