script.pas 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 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 defines.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. Procedure GenerateAsmRes(const st : string);
  74. implementation
  75. uses
  76. {$ifdef Unix}
  77. {$ifdef ver1_0}
  78. Linux,
  79. {$else}
  80. Unix,
  81. {$endif}
  82. {$endif}
  83. globals,systems;
  84. {****************************************************************************
  85. TScript
  86. ****************************************************************************}
  87. constructor TScript.Create(const s:string);
  88. begin
  89. fn:=FixFileName(s);
  90. executable:=false;
  91. data:=TStringList.Create;
  92. end;
  93. constructor TScript.CreateExec(const s:string);
  94. begin
  95. fn:=FixFileName(s)+target_info.scriptext;
  96. executable:=true;
  97. data:=TStringList.Create;
  98. end;
  99. destructor TScript.Destroy;
  100. begin
  101. data.Free;
  102. end;
  103. procedure TScript.AddStart(const s:string);
  104. begin
  105. data.Insert(s);
  106. end;
  107. procedure TScript.Add(const s:string);
  108. begin
  109. data.Concat(s);
  110. end;
  111. Function TScript.Empty:boolean;
  112. begin
  113. Empty:=Data.Empty;
  114. end;
  115. procedure TScript.WriteToDisk;
  116. var
  117. t : Text;
  118. begin
  119. Assign(t,fn);
  120. Rewrite(t);
  121. while not data.Empty do
  122. Writeln(t,data.GetFirst);
  123. Close(t);
  124. {$ifdef Unix}
  125. if executable then
  126. ChMod(fn,493);
  127. {$endif}
  128. end;
  129. {****************************************************************************
  130. Asm Response
  131. ****************************************************************************}
  132. Constructor TAsmScript.Create (Const ScriptName : String);
  133. begin
  134. Inherited CreateExec(ScriptName);
  135. end;
  136. {****************************************************************************
  137. Asm Response
  138. ****************************************************************************}
  139. Constructor TAsmScriptDos.Create (Const ScriptName : String);
  140. begin
  141. Inherited Create(ScriptName);
  142. end;
  143. Procedure TAsmScriptDos.AddAsmCommand (Const Command, Options,FileName : String);
  144. begin
  145. if FileName<>'' then
  146. begin
  147. Add('SET THEFILE='+FileName);
  148. Add('echo Assembling %THEFILE%');
  149. end;
  150. Add(command+' '+Options);
  151. Add('if errorlevel 1 goto asmend');
  152. end;
  153. Procedure TAsmScriptDos.AddLinkCommand (Const Command, Options, FileName : String);
  154. begin
  155. if FileName<>'' then
  156. begin
  157. Add('SET THEFILE='+FileName);
  158. Add('echo Linking %THEFILE%');
  159. end;
  160. Add (Command+' '+Options);
  161. Add('if errorlevel 1 goto linkend');
  162. end;
  163. Procedure TAsmScriptDos.AddDeleteCommand (Const FileName : String);
  164. begin
  165. Add('Del '+FileName);
  166. end;
  167. Procedure TAsmScriptDos.AddDeleteDirCommand (Const FileName : String);
  168. begin
  169. Add('Rmdir '+FileName);
  170. end;
  171. Procedure TAsmScriptDos.WriteToDisk;
  172. Begin
  173. AddStart('@echo off');
  174. Add('goto end');
  175. Add(':asmend');
  176. Add('echo An error occured while assembling %THEFILE%');
  177. Add('goto end');
  178. Add(':linkend');
  179. Add('echo An error occured while linking %THEFILE%');
  180. Add(':end');
  181. inherited WriteToDisk;
  182. end;
  183. {****************************************************************************
  184. Amiga Asm Response
  185. ****************************************************************************}
  186. Constructor TAsmScriptAmiga.Create (Const ScriptName : String);
  187. begin
  188. Inherited Create(ScriptName);
  189. end;
  190. Procedure TAsmScriptAmiga.AddAsmCommand (Const Command, Options,FileName : String);
  191. begin
  192. if FileName<>'' then
  193. begin
  194. Add('SET THEFILE '+FileName);
  195. Add('echo Assembling $THEFILE');
  196. end;
  197. Add(command+' '+Options);
  198. Add('if error');
  199. Add('skip asmend');
  200. Add('endif');
  201. end;
  202. Procedure TAsmScriptAmiga.AddLinkCommand (Const Command, Options, FileName : String);
  203. begin
  204. if FileName<>'' then
  205. begin
  206. Add('SET THEFILE '+FileName);
  207. Add('echo Linking $THEFILE');
  208. end;
  209. Add (Command+' '+Options);
  210. Add('if error');
  211. Add('skip linkend');
  212. Add('endif');
  213. end;
  214. Procedure TAsmScriptAmiga.AddDeleteCommand (Const FileName : String);
  215. begin
  216. Add('Delete '+FileName);
  217. end;
  218. Procedure TAsmScriptAmiga.AddDeleteDirCommand (Const FileName : String);
  219. begin
  220. Add('Delete '+FileName);
  221. end;
  222. Procedure TAsmScriptAmiga.WriteToDisk;
  223. Begin
  224. Add('skip end');
  225. Add('lab asmend');
  226. Add('echo An error occured while assembling $THEFILE');
  227. Add('skip end');
  228. Add('lab linkend');
  229. Add('echo An error occured while linking $THEFILE');
  230. Add('lab end');
  231. inherited WriteToDisk;
  232. end;
  233. {****************************************************************************
  234. Unix Asm Response
  235. ****************************************************************************}
  236. Constructor TAsmScriptUnix.Create (Const ScriptName : String);
  237. begin
  238. Inherited Create(ScriptName);
  239. end;
  240. Procedure TAsmScriptUnix.AddAsmCommand (Const Command, Options,FileName : String);
  241. begin
  242. if FileName<>'' then
  243. Add('echo Assembling '+FileName);
  244. Add (Command+' '+Options);
  245. Add('if [ $? != 0 ]; then DoExitAsm '+FileName+'; fi');
  246. end;
  247. Procedure TAsmScriptUnix.AddLinkCommand (Const Command, Options, FileName : String);
  248. begin
  249. if FileName<>'' then
  250. Add('echo Linking '+FileName);
  251. Add (Command+' '+Options);
  252. Add('if [ $? != 0 ]; then DoExitLink '+FileName+'; fi');
  253. end;
  254. Procedure TAsmScriptUnix.AddDeleteCommand (Const FileName : String);
  255. begin
  256. Add('rm '+FileName);
  257. end;
  258. Procedure TAsmScriptUnix.AddDeleteDirCommand (Const FileName : String);
  259. begin
  260. Add('rmdir '+FileName);
  261. end;
  262. Procedure TAsmScriptUnix.WriteToDisk;
  263. Begin
  264. AddStart('{ echo "An error occurred while linking $1"; exit 1; }');
  265. AddStart('DoExitLink ()');
  266. AddStart('{ echo "An error occurred while assembling $1"; exit 1; }');
  267. AddStart('DoExitAsm ()');
  268. AddStart('#!/bin/sh');
  269. inherited WriteToDisk;
  270. end;
  271. Procedure GenerateAsmRes(const st : string);
  272. begin
  273. case target_info.script of
  274. script_unix :
  275. AsmRes:=TAsmScriptUnix.Create(st);
  276. script_dos :
  277. AsmRes:=TAsmScriptDos.Create(st);
  278. script_amiga :
  279. AsmRes:=TAsmScriptAmiga.Create(st);
  280. end;
  281. end;
  282. {****************************************************************************
  283. Link Response
  284. ****************************************************************************}
  285. procedure TLinkRes.Add(const s:string);
  286. begin
  287. if s<>'' then
  288. inherited Add(s);
  289. end;
  290. procedure TLinkRes.AddFileName(const s:string);
  291. begin
  292. if s<>'' then
  293. begin
  294. if not(s[1] in ['a'..'z','A'..'Z','/','\','.']) then
  295. inherited Add('.'+DirSep+s)
  296. else
  297. inherited Add(s);
  298. end;
  299. end;
  300. end.
  301. {
  302. $Log$
  303. Revision 1.12 2001-08-07 18:44:09 peter
  304. * made script target indepedent
  305. Revision 1.11 2001/07/30 20:59:27 peter
  306. * m68k updates from v10 merged
  307. Revision 1.10 2001/07/10 21:01:35 peter
  308. * fixed crash with writing of the linker script
  309. Revision 1.9 2001/04/18 22:01:58 peter
  310. * registration of targets and assemblers
  311. Revision 1.8 2001/04/13 01:22:14 peter
  312. * symtable change to classes
  313. * range check generation and errors fixed, make cycle DEBUG=1 works
  314. * memory leaks fixed
  315. Revision 1.7 2001/02/05 20:47:00 peter
  316. * support linux unit for ver1_0 compilers
  317. Revision 1.6 2001/01/21 20:32:45 marco
  318. * Renamefest. Compiler part. Not that hard.
  319. Revision 1.5 2000/12/25 00:07:29 peter
  320. + new tlinkedlist class (merge of old tstringqueue,tcontainer and
  321. tlinkedlist objects)
  322. Revision 1.4 2000/11/13 15:43:07 marco
  323. * Renamefest
  324. Revision 1.3 2000/09/24 15:06:28 peter
  325. * use defines.inc
  326. Revision 1.2 2000/07/13 11:32:49 michael
  327. + removed logs
  328. }