script.pas 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Peter Vreman
  4. This unit handles the writing of script files
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit Script;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. cclasses;
  23. type
  24. TScript=class
  25. fn : string[80];
  26. data : TStringList;
  27. executable : boolean;
  28. constructor Create(const s:string);
  29. constructor CreateExec(const s:string);
  30. destructor Destroy;override;
  31. procedure AddStart(const s:string);
  32. procedure Add(const s:string);
  33. Function Empty:boolean;
  34. procedure WriteToDisk;virtual;
  35. end;
  36. TAsmScript = class (TScript)
  37. Constructor Create(Const ScriptName : String); virtual;
  38. Procedure AddAsmCommand (Const Command, Options,FileName : String);virtual;abstract;
  39. Procedure AddLinkCommand (Const Command, Options, FileName : String);virtual;abstract;
  40. Procedure AddDeleteCommand (Const FileName : String);virtual;abstract;
  41. Procedure AddDeleteDirCommand (Const FileName : String);virtual;abstract;
  42. end;
  43. TAsmScriptDos = class (TAsmScript)
  44. Constructor Create (Const ScriptName : String); override;
  45. Procedure AddAsmCommand (Const Command, Options,FileName : String);override;
  46. Procedure AddLinkCommand (Const Command, Options, FileName : String);override;
  47. Procedure AddDeleteCommand (Const FileName : String);override;
  48. Procedure AddDeleteDirCommand (Const FileName : String);override;
  49. Procedure WriteToDisk;override;
  50. end;
  51. TAsmScriptAmiga = class (TAsmScript)
  52. Constructor Create (Const ScriptName : String); override;
  53. Procedure AddAsmCommand (Const Command, Options,FileName : String);override;
  54. Procedure AddLinkCommand (Const Command, Options, FileName : String);override;
  55. Procedure AddDeleteCommand (Const FileName : String);override;
  56. Procedure AddDeleteDirCommand (Const FileName : String);override;
  57. Procedure WriteToDisk;override;
  58. end;
  59. TAsmScriptUnix = class (TAsmScript)
  60. Constructor Create (Const ScriptName : String);override;
  61. Procedure AddAsmCommand (Const Command, Options,FileName : String);override;
  62. Procedure AddLinkCommand (Const Command, Options, FileName : String);override;
  63. Procedure AddDeleteCommand (Const FileName : String);override;
  64. Procedure AddDeleteDirCommand (Const FileName : String);override;
  65. Procedure WriteToDisk;override;
  66. end;
  67. TLinkRes = Class (TScript)
  68. procedure Add(const s:string);
  69. procedure AddFileName(const s:string);
  70. end;
  71. var
  72. AsmRes : TAsmScript;
  73. Function ScriptFixFileName(const s:string):string;
  74. Procedure GenerateAsmRes(const st : string);
  75. implementation
  76. uses
  77. {$ifdef hasUnix}
  78. {$ifdef ver1_0}
  79. Linux,
  80. {$else}
  81. Unix,
  82. {$endif}
  83. {$endif}
  84. cutils,
  85. globtype,globals,systems;
  86. {****************************************************************************
  87. Helpers
  88. ****************************************************************************}
  89. Function ScriptFixFileName(const s:string):string;
  90. begin
  91. if cs_link_on_target in aktglobalswitches then
  92. ScriptFixFileName:=TargetFixFileName(s)
  93. else
  94. ScriptFixFileName:=FixFileName(s);
  95. end;
  96. {****************************************************************************
  97. TScript
  98. ****************************************************************************}
  99. constructor TScript.Create(const s:string);
  100. begin
  101. fn:=FixFileName(s);
  102. executable:=false;
  103. data:=TStringList.Create;
  104. end;
  105. constructor TScript.CreateExec(const s:string);
  106. begin
  107. if cs_link_on_target in aktglobalswitches then
  108. fn:=TargetFixFileName(s)+target_info.scriptext
  109. else
  110. fn:=FixFileName(s)+source_info.scriptext;
  111. executable:=true;
  112. data:=TStringList.Create;
  113. end;
  114. destructor TScript.Destroy;
  115. begin
  116. data.Free;
  117. end;
  118. procedure TScript.AddStart(const s:string);
  119. begin
  120. data.Insert(s);
  121. end;
  122. procedure TScript.Add(const s:string);
  123. begin
  124. data.Concat(s);
  125. end;
  126. Function TScript.Empty:boolean;
  127. begin
  128. Empty:=Data.Empty;
  129. end;
  130. procedure TScript.WriteToDisk;
  131. var
  132. t : Text;
  133. begin
  134. Assign(t,fn);
  135. Rewrite(t);
  136. while not data.Empty do
  137. Writeln(t,data.GetFirst);
  138. Close(t);
  139. {$ifdef hasUnix}
  140. if executable then
  141. ChMod(fn,493);
  142. {$endif}
  143. end;
  144. {****************************************************************************
  145. Asm Response
  146. ****************************************************************************}
  147. Constructor TAsmScript.Create (Const ScriptName : String);
  148. begin
  149. Inherited CreateExec(ScriptName);
  150. end;
  151. {****************************************************************************
  152. Asm Response
  153. ****************************************************************************}
  154. Constructor TAsmScriptDos.Create (Const ScriptName : String);
  155. begin
  156. Inherited Create(ScriptName);
  157. end;
  158. Procedure TAsmScriptDos.AddAsmCommand (Const Command, Options,FileName : String);
  159. begin
  160. if FileName<>'' then
  161. begin
  162. Add('SET THEFILE='+ScriptFixFileName(FileName));
  163. Add('echo Assembling %THEFILE%');
  164. end;
  165. Add(maybequoted(command)+' '+Options);
  166. Add('if errorlevel 1 goto asmend');
  167. end;
  168. Procedure TAsmScriptDos.AddLinkCommand (Const Command, Options, FileName : String);
  169. begin
  170. if FileName<>'' then
  171. begin
  172. Add('SET THEFILE='+ScriptFixFileName(FileName));
  173. Add('echo Linking %THEFILE%');
  174. end;
  175. Add(maybequoted(command)+' '+Options);
  176. Add('if errorlevel 1 goto linkend');
  177. end;
  178. Procedure TAsmScriptDos.AddDeleteCommand (Const FileName : String);
  179. begin
  180. Add('Del '+ScriptFixFileName(FileName));
  181. end;
  182. Procedure TAsmScriptDos.AddDeleteDirCommand (Const FileName : String);
  183. begin
  184. Add('Rmdir '+FileName);
  185. end;
  186. Procedure TAsmScriptDos.WriteToDisk;
  187. Begin
  188. AddStart('@echo off');
  189. Add('goto end');
  190. Add(':asmend');
  191. Add('echo An error occured while assembling %THEFILE%');
  192. Add('goto end');
  193. Add(':linkend');
  194. Add('echo An error occured while linking %THEFILE%');
  195. Add(':end');
  196. inherited WriteToDisk;
  197. end;
  198. {****************************************************************************
  199. Amiga Asm Response
  200. ****************************************************************************}
  201. Constructor TAsmScriptAmiga.Create (Const ScriptName : String);
  202. begin
  203. Inherited Create(ScriptName);
  204. end;
  205. Procedure TAsmScriptAmiga.AddAsmCommand (Const Command, Options,FileName : String);
  206. begin
  207. if FileName<>'' then
  208. begin
  209. Add('SET THEFILE '+ScriptFixFileName(FileName));
  210. Add('echo Assembling $THEFILE');
  211. end;
  212. Add(maybequoted(command)+' '+Options);
  213. { There is a problem here,
  214. as allways return with a non zero error value PM }
  215. Add('if error');
  216. Add('why');
  217. Add('skip asmend');
  218. Add('endif');
  219. end;
  220. Procedure TAsmScriptAmiga.AddLinkCommand (Const Command, Options, FileName : String);
  221. begin
  222. if FileName<>'' then
  223. begin
  224. Add('SET THEFILE '+ScriptFixFileName(FileName));
  225. Add('echo Linking $THEFILE');
  226. end;
  227. Add(maybequoted(command)+' '+Options);
  228. Add('if error');
  229. Add('skip linkend');
  230. Add('endif');
  231. end;
  232. Procedure TAsmScriptAmiga.AddDeleteCommand (Const FileName : String);
  233. begin
  234. Add('Delete '+ScriptFixFileName(FileName));
  235. end;
  236. Procedure TAsmScriptAmiga.AddDeleteDirCommand (Const FileName : String);
  237. begin
  238. Add('Delete '+ScriptFixFileName(FileName));
  239. end;
  240. Procedure TAsmScriptAmiga.WriteToDisk;
  241. Begin
  242. Add('skip end');
  243. Add('lab asmend');
  244. Add('why');
  245. Add('echo An error occured while assembling $THEFILE');
  246. Add('skip end');
  247. Add('lab linkend');
  248. Add('why');
  249. Add('echo An error occured while linking $THEFILE');
  250. Add('lab end');
  251. inherited WriteToDisk;
  252. end;
  253. {****************************************************************************
  254. Unix Asm Response
  255. ****************************************************************************}
  256. Constructor TAsmScriptUnix.Create (Const ScriptName : String);
  257. begin
  258. Inherited Create(ScriptName);
  259. end;
  260. Procedure TAsmScriptUnix.AddAsmCommand (Const Command, Options,FileName : String);
  261. begin
  262. if FileName<>'' then
  263. Add('echo Assembling '+ScriptFixFileName(FileName));
  264. Add(maybequoted(command)+' '+Options);
  265. Add('if [ $? != 0 ]; then DoExitAsm '+ScriptFixFileName(FileName)+'; fi');
  266. end;
  267. Procedure TAsmScriptUnix.AddLinkCommand (Const Command, Options, FileName : String);
  268. begin
  269. if FileName<>'' then
  270. Add('echo Linking '+ScriptFixFileName(FileName));
  271. Add(maybequoted(command)+' '+Options);
  272. Add('if [ $? != 0 ]; then DoExitLink '+ScriptFixFileName(FileName)+'; fi');
  273. end;
  274. Procedure TAsmScriptUnix.AddDeleteCommand (Const FileName : String);
  275. begin
  276. Add('rm '+ScriptFixFileName(FileName));
  277. end;
  278. Procedure TAsmScriptUnix.AddDeleteDirCommand (Const FileName : String);
  279. begin
  280. Add('rmdir '+ScriptFixFileName(FileName));
  281. end;
  282. Procedure TAsmScriptUnix.WriteToDisk;
  283. Begin
  284. AddStart('{ echo "An error occurred while linking $1"; exit 1; }');
  285. AddStart('DoExitLink ()');
  286. AddStart('{ echo "An error occurred while assembling $1"; exit 1; }');
  287. AddStart('DoExitAsm ()');
  288. AddStart('#!/bin/sh');
  289. inherited WriteToDisk;
  290. end;
  291. Procedure GenerateAsmRes(const st : string);
  292. var
  293. scripttyp : tscripttype;
  294. begin
  295. if cs_link_on_target in aktglobalswitches then
  296. scripttyp := target_info.script
  297. else
  298. scripttyp := source_info.script;
  299. case scripttyp of
  300. script_unix :
  301. AsmRes:=TAsmScriptUnix.Create(st);
  302. script_dos :
  303. AsmRes:=TAsmScriptDos.Create(st);
  304. script_amiga :
  305. AsmRes:=TAsmScriptAmiga.Create(st);
  306. end;
  307. end;
  308. {****************************************************************************
  309. Link Response
  310. ****************************************************************************}
  311. procedure TLinkRes.Add(const s:string);
  312. begin
  313. if s<>'' then
  314. inherited Add(s);
  315. end;
  316. procedure TLinkRes.AddFileName(const s:string);
  317. begin
  318. if s<>'' then
  319. begin
  320. if not(s[1] in ['a'..'z','A'..'Z','/','\','.','"']) then
  321. begin
  322. if cs_link_on_target in aktglobalswitches then
  323. inherited Add('.'+target_info.DirSep+s)
  324. else
  325. inherited Add('.'+source_info.DirSep+s);
  326. end
  327. else
  328. inherited Add(s);
  329. end;
  330. end;
  331. end.
  332. {
  333. $Log$
  334. Revision 1.19 2003-01-10 21:49:00 marco
  335. * more hasunix fixes
  336. Revision 1.18 2003/01/06 20:16:42 peter
  337. * don't prepend ./ to quoted filenames
  338. Revision 1.17 2002/11/15 01:58:54 peter
  339. * merged changes from 1.0.7 up to 04-11
  340. - -V option for generating bug report tracing
  341. - more tracing for option parsing
  342. - errors for cdecl and high()
  343. - win32 import stabs
  344. - win32 records<=8 are returned in eax:edx (turned off by default)
  345. - heaptrc update
  346. - more info for temp management in .s file with EXTDEBUG
  347. Revision 1.16 2002/05/18 13:34:18 peter
  348. * readded missing revisions
  349. Revision 1.15 2002/05/16 19:46:44 carl
  350. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  351. + try to fix temp allocation (still in ifdef)
  352. + generic constructor calls
  353. + start of tassembler / tmodulebase class cleanup
  354. }