2
0

script.pas 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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 Unix}
  78. {$ifdef ver1_0}
  79. Linux,
  80. {$else}
  81. Unix,
  82. {$endif}
  83. {$endif}
  84. globtype,globals,systems;
  85. {****************************************************************************
  86. Helpers
  87. ****************************************************************************}
  88. Function ScriptFixFileName(const s:string):string;
  89. begin
  90. if cs_link_on_target in aktglobalswitches then
  91. ScriptFixFileName:=TargetFixFileName(s)
  92. else
  93. ScriptFixFileName:=FixFileName(s);
  94. end;
  95. {****************************************************************************
  96. TScript
  97. ****************************************************************************}
  98. constructor TScript.Create(const s:string);
  99. begin
  100. fn:=FixFileName(s);
  101. executable:=false;
  102. data:=TStringList.Create;
  103. end;
  104. constructor TScript.CreateExec(const s:string);
  105. begin
  106. if cs_link_on_target in aktglobalswitches then
  107. fn:=FixFileName(s)+target_info.scriptext
  108. else
  109. fn:=FixFileName(s)+source_info.scriptext;
  110. executable:=true;
  111. data:=TStringList.Create;
  112. end;
  113. destructor TScript.Destroy;
  114. begin
  115. data.Free;
  116. end;
  117. procedure TScript.AddStart(const s:string);
  118. begin
  119. data.Insert(s);
  120. end;
  121. procedure TScript.Add(const s:string);
  122. begin
  123. data.Concat(s);
  124. end;
  125. Function TScript.Empty:boolean;
  126. begin
  127. Empty:=Data.Empty;
  128. end;
  129. procedure TScript.WriteToDisk;
  130. var
  131. t : Text;
  132. begin
  133. Assign(t,fn);
  134. Rewrite(t);
  135. while not data.Empty do
  136. Writeln(t,data.GetFirst);
  137. Close(t);
  138. {$ifdef Unix}
  139. if executable then
  140. ChMod(fn,493);
  141. {$endif}
  142. end;
  143. {****************************************************************************
  144. Asm Response
  145. ****************************************************************************}
  146. Constructor TAsmScript.Create (Const ScriptName : String);
  147. begin
  148. Inherited CreateExec(ScriptName);
  149. end;
  150. {****************************************************************************
  151. Asm Response
  152. ****************************************************************************}
  153. Constructor TAsmScriptDos.Create (Const ScriptName : String);
  154. begin
  155. Inherited Create(ScriptName);
  156. end;
  157. Procedure TAsmScriptDos.AddAsmCommand (Const Command, Options,FileName : String);
  158. begin
  159. if FileName<>'' then
  160. begin
  161. Add('SET THEFILE='+FileName);
  162. Add('echo Assembling %THEFILE%');
  163. end;
  164. Add(command+' '+Options);
  165. Add('if errorlevel 1 goto asmend');
  166. end;
  167. Procedure TAsmScriptDos.AddLinkCommand (Const Command, Options, FileName : String);
  168. begin
  169. if FileName<>'' then
  170. begin
  171. Add('SET THEFILE='+FileName);
  172. Add('echo Linking %THEFILE%');
  173. end;
  174. Add (Command+' '+Options);
  175. Add('if errorlevel 1 goto linkend');
  176. end;
  177. Procedure TAsmScriptDos.AddDeleteCommand (Const FileName : String);
  178. begin
  179. Add('Del '+FileName);
  180. end;
  181. Procedure TAsmScriptDos.AddDeleteDirCommand (Const FileName : String);
  182. begin
  183. Add('Rmdir '+FileName);
  184. end;
  185. Procedure TAsmScriptDos.WriteToDisk;
  186. Begin
  187. AddStart('@echo off');
  188. Add('goto end');
  189. Add(':asmend');
  190. Add('echo An error occured while assembling %THEFILE%');
  191. Add('goto end');
  192. Add(':linkend');
  193. Add('echo An error occured while linking %THEFILE%');
  194. Add(':end');
  195. inherited WriteToDisk;
  196. end;
  197. {****************************************************************************
  198. Amiga Asm Response
  199. ****************************************************************************}
  200. Constructor TAsmScriptAmiga.Create (Const ScriptName : String);
  201. begin
  202. Inherited Create(ScriptName);
  203. end;
  204. Procedure TAsmScriptAmiga.AddAsmCommand (Const Command, Options,FileName : String);
  205. begin
  206. if FileName<>'' then
  207. begin
  208. Add('SET THEFILE '+FileName);
  209. Add('echo Assembling $THEFILE');
  210. end;
  211. Add(command+' '+Options);
  212. { There is a problem here,
  213. as allways return with a non zero error value PM }
  214. Add('if error');
  215. Add('why');
  216. Add('skip asmend');
  217. Add('endif');
  218. end;
  219. Procedure TAsmScriptAmiga.AddLinkCommand (Const Command, Options, FileName : String);
  220. begin
  221. if FileName<>'' then
  222. begin
  223. Add('SET THEFILE '+FileName);
  224. Add('echo Linking $THEFILE');
  225. end;
  226. Add (Command+' '+Options);
  227. Add('if error');
  228. Add('skip linkend');
  229. Add('endif');
  230. end;
  231. Procedure TAsmScriptAmiga.AddDeleteCommand (Const FileName : String);
  232. begin
  233. Add('Delete '+FileName);
  234. end;
  235. Procedure TAsmScriptAmiga.AddDeleteDirCommand (Const FileName : String);
  236. begin
  237. Add('Delete '+FileName);
  238. end;
  239. Procedure TAsmScriptAmiga.WriteToDisk;
  240. Begin
  241. Add('skip end');
  242. Add('lab asmend');
  243. Add('why');
  244. Add('echo An error occured while assembling $THEFILE');
  245. Add('skip end');
  246. Add('lab linkend');
  247. Add('why');
  248. Add('echo An error occured while linking $THEFILE');
  249. Add('lab end');
  250. inherited WriteToDisk;
  251. end;
  252. {****************************************************************************
  253. Unix Asm Response
  254. ****************************************************************************}
  255. Constructor TAsmScriptUnix.Create (Const ScriptName : String);
  256. begin
  257. Inherited Create(ScriptName);
  258. end;
  259. Procedure TAsmScriptUnix.AddAsmCommand (Const Command, Options,FileName : String);
  260. begin
  261. if FileName<>'' then
  262. Add('echo Assembling '+FileName);
  263. Add (Command+' '+Options);
  264. Add('if [ $? != 0 ]; then DoExitAsm '+FileName+'; fi');
  265. end;
  266. Procedure TAsmScriptUnix.AddLinkCommand (Const Command, Options, FileName : String);
  267. begin
  268. if FileName<>'' then
  269. Add('echo Linking '+FileName);
  270. Add (Command+' '+Options);
  271. Add('if [ $? != 0 ]; then DoExitLink '+FileName+'; fi');
  272. end;
  273. Procedure TAsmScriptUnix.AddDeleteCommand (Const FileName : String);
  274. begin
  275. Add('rm '+FileName);
  276. end;
  277. Procedure TAsmScriptUnix.AddDeleteDirCommand (Const FileName : String);
  278. begin
  279. Add('rmdir '+FileName);
  280. end;
  281. Procedure TAsmScriptUnix.WriteToDisk;
  282. Begin
  283. AddStart('{ echo "An error occurred while linking $1"; exit 1; }');
  284. AddStart('DoExitLink ()');
  285. AddStart('{ echo "An error occurred while assembling $1"; exit 1; }');
  286. AddStart('DoExitAsm ()');
  287. AddStart('#!/bin/sh');
  288. inherited WriteToDisk;
  289. end;
  290. Procedure GenerateAsmRes(const st : string);
  291. var
  292. scripttyp : tscripttype;
  293. begin
  294. if cs_link_on_target in aktglobalswitches then
  295. scripttyp := target_info.script
  296. else
  297. scripttyp := source_info.script;
  298. case scripttyp of
  299. script_unix :
  300. AsmRes:=TAsmScriptUnix.Create(st);
  301. script_dos :
  302. AsmRes:=TAsmScriptDos.Create(st);
  303. script_amiga :
  304. AsmRes:=TAsmScriptAmiga.Create(st);
  305. end;
  306. end;
  307. {****************************************************************************
  308. Link Response
  309. ****************************************************************************}
  310. procedure TLinkRes.Add(const s:string);
  311. begin
  312. if s<>'' then
  313. inherited Add(s);
  314. end;
  315. procedure TLinkRes.AddFileName(const s:string);
  316. begin
  317. if s<>'' then
  318. begin
  319. if not(s[1] in ['a'..'z','A'..'Z','/','\','.']) then
  320. begin
  321. if cs_link_on_target in aktglobalswitches then
  322. inherited Add('.'+target_info.DirSep+s)
  323. else
  324. inherited Add('.'+source_info.DirSep+s);
  325. end
  326. else
  327. inherited Add(s);
  328. end;
  329. end;
  330. end.
  331. {
  332. $Log$
  333. Revision 1.16 2002-05-18 13:34:18 peter
  334. * readded missing revisions
  335. Revision 1.15 2002/05/16 19:46:44 carl
  336. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  337. + try to fix temp allocation (still in ifdef)
  338. + generic constructor calls
  339. + start of tassembler / tmodulebase class cleanup
  340. }