script.pas 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  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. BaseUnix,
  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. fn:=FixFileName(s);
  108. if cs_link_on_target in aktglobalswitches then
  109. fn:=AddExtension(fn,target_info.scriptext)
  110. else
  111. fn:=AddExtension(fn,source_info.scriptext);
  112. executable:=true;
  113. data:=TStringList.Create;
  114. end;
  115. destructor TScript.Destroy;
  116. begin
  117. data.Free;
  118. end;
  119. procedure TScript.AddStart(const s:string);
  120. begin
  121. data.Insert(s);
  122. end;
  123. procedure TScript.Add(const s:string);
  124. begin
  125. data.Concat(s);
  126. end;
  127. Function TScript.Empty:boolean;
  128. begin
  129. Empty:=Data.Empty;
  130. end;
  131. procedure TScript.WriteToDisk;
  132. var
  133. t : file;
  134. i : longint;
  135. s : string;
  136. begin
  137. Assign(t,fn);
  138. {$I-}
  139. Rewrite(t,1);
  140. if ioresult<>0 then
  141. exit;
  142. while not data.Empty do
  143. begin
  144. s:=data.GetFirst;
  145. if (cs_link_on_target in aktglobalswitches) then
  146. s:=s+target_info.newline
  147. else
  148. s:=s+source_info.newline;
  149. Blockwrite(t,s[1],length(s),i);
  150. end;
  151. Close(t);
  152. {$I+}
  153. i:=ioresult;
  154. {$ifdef hasUnix}
  155. if executable then
  156. {$ifdef VER1_0}ChMod{$else}fpchmod{$endif}(fn,493);
  157. {$endif}
  158. end;
  159. {****************************************************************************
  160. Asm Response
  161. ****************************************************************************}
  162. Constructor TAsmScript.Create (Const ScriptName : String);
  163. begin
  164. Inherited CreateExec(ScriptName);
  165. end;
  166. {****************************************************************************
  167. Asm Response
  168. ****************************************************************************}
  169. Constructor TAsmScriptDos.Create (Const ScriptName : String);
  170. begin
  171. Inherited Create(ScriptName);
  172. end;
  173. Procedure TAsmScriptDos.AddAsmCommand (Const Command, Options,FileName : String);
  174. begin
  175. if FileName<>'' then
  176. begin
  177. Add('SET THEFILE='+ScriptFixFileName(FileName));
  178. Add('echo Assembling %THEFILE%');
  179. end;
  180. Add(maybequoted(command)+' '+Options);
  181. Add('if errorlevel 1 goto asmend');
  182. end;
  183. Procedure TAsmScriptDos.AddLinkCommand (Const Command, Options, FileName : String);
  184. begin
  185. if FileName<>'' then
  186. begin
  187. Add('SET THEFILE='+ScriptFixFileName(FileName));
  188. Add('echo Linking %THEFILE%');
  189. end;
  190. Add(maybequoted(command)+' '+Options);
  191. Add('if errorlevel 1 goto linkend');
  192. end;
  193. Procedure TAsmScriptDos.AddDeleteCommand (Const FileName : String);
  194. begin
  195. Add('Del '+ScriptFixFileName(FileName));
  196. end;
  197. Procedure TAsmScriptDos.AddDeleteDirCommand (Const FileName : String);
  198. begin
  199. Add('Rmdir '+FileName);
  200. end;
  201. Procedure TAsmScriptDos.WriteToDisk;
  202. Begin
  203. AddStart('@echo off');
  204. Add('goto end');
  205. Add(':asmend');
  206. Add('echo An error occured while assembling %THEFILE%');
  207. Add('goto end');
  208. Add(':linkend');
  209. Add('echo An error occured while linking %THEFILE%');
  210. Add(':end');
  211. inherited WriteToDisk;
  212. end;
  213. {****************************************************************************
  214. Amiga Asm Response
  215. ****************************************************************************}
  216. Constructor TAsmScriptAmiga.Create (Const ScriptName : String);
  217. begin
  218. Inherited Create(ScriptName);
  219. end;
  220. Procedure TAsmScriptAmiga.AddAsmCommand (Const Command, Options,FileName : String);
  221. begin
  222. if FileName<>'' then
  223. begin
  224. Add('SET THEFILE '+ScriptFixFileName(FileName));
  225. Add('echo Assembling $THEFILE');
  226. end;
  227. Add(maybequoted(command)+' '+Options);
  228. { There is a problem here,
  229. as allways return with a non zero error value PM }
  230. Add('if error');
  231. Add('why');
  232. Add('skip asmend');
  233. Add('endif');
  234. end;
  235. Procedure TAsmScriptAmiga.AddLinkCommand (Const Command, Options, FileName : String);
  236. begin
  237. if FileName<>'' then
  238. begin
  239. Add('SET THEFILE '+ScriptFixFileName(FileName));
  240. Add('echo Linking $THEFILE');
  241. end;
  242. Add(maybequoted(command)+' '+Options);
  243. Add('if error');
  244. Add('skip linkend');
  245. Add('endif');
  246. end;
  247. Procedure TAsmScriptAmiga.AddDeleteCommand (Const FileName : String);
  248. begin
  249. Add('Delete '+ScriptFixFileName(FileName));
  250. end;
  251. Procedure TAsmScriptAmiga.AddDeleteDirCommand (Const FileName : String);
  252. begin
  253. Add('Delete '+ScriptFixFileName(FileName));
  254. end;
  255. Procedure TAsmScriptAmiga.WriteToDisk;
  256. Begin
  257. Add('skip end');
  258. Add('lab asmend');
  259. Add('why');
  260. Add('echo An error occured while assembling $THEFILE');
  261. Add('skip end');
  262. Add('lab linkend');
  263. Add('why');
  264. Add('echo An error occured while linking $THEFILE');
  265. Add('lab end');
  266. inherited WriteToDisk;
  267. end;
  268. {****************************************************************************
  269. Unix Asm Response
  270. ****************************************************************************}
  271. Constructor TAsmScriptUnix.Create (Const ScriptName : String);
  272. begin
  273. Inherited Create(ScriptName);
  274. end;
  275. Procedure TAsmScriptUnix.AddAsmCommand (Const Command, Options,FileName : String);
  276. begin
  277. if FileName<>'' then
  278. Add('echo Assembling '+ScriptFixFileName(FileName));
  279. Add(maybequoted(command)+' '+Options);
  280. Add('if [ $? != 0 ]; then DoExitAsm '+ScriptFixFileName(FileName)+'; fi');
  281. end;
  282. Procedure TAsmScriptUnix.AddLinkCommand (Const Command, Options, FileName : String);
  283. begin
  284. if FileName<>'' then
  285. Add('echo Linking '+ScriptFixFileName(FileName));
  286. Add(maybequoted(command)+' '+Options);
  287. Add('if [ $? != 0 ]; then DoExitLink '+ScriptFixFileName(FileName)+'; fi');
  288. end;
  289. Procedure TAsmScriptUnix.AddDeleteCommand (Const FileName : String);
  290. begin
  291. Add('rm '+ScriptFixFileName(FileName));
  292. end;
  293. Procedure TAsmScriptUnix.AddDeleteDirCommand (Const FileName : String);
  294. begin
  295. Add('rmdir '+ScriptFixFileName(FileName));
  296. end;
  297. Procedure TAsmScriptUnix.WriteToDisk;
  298. Begin
  299. AddStart('{ echo "An error occurred while linking $1"; exit 1; }');
  300. AddStart('DoExitLink ()');
  301. AddStart('{ echo "An error occurred while assembling $1"; exit 1; }');
  302. AddStart('DoExitAsm ()');
  303. {$ifdef BEOS}
  304. AddStart('#!/boot/beos/bin/sh');
  305. {$else}
  306. AddStart('#!/bin/sh');
  307. {$endif}
  308. inherited WriteToDisk;
  309. end;
  310. Procedure GenerateAsmRes(const st : string);
  311. var
  312. scripttyp : tscripttype;
  313. begin
  314. if cs_link_on_target in aktglobalswitches then
  315. scripttyp := target_info.script
  316. else
  317. scripttyp := source_info.script;
  318. case scripttyp of
  319. script_unix :
  320. AsmRes:=TAsmScriptUnix.Create(st);
  321. script_dos :
  322. AsmRes:=TAsmScriptDos.Create(st);
  323. script_amiga :
  324. AsmRes:=TAsmScriptAmiga.Create(st);
  325. end;
  326. end;
  327. {****************************************************************************
  328. Link Response
  329. ****************************************************************************}
  330. procedure TLinkRes.Add(const s:string);
  331. begin
  332. if s<>'' then
  333. inherited Add(s);
  334. end;
  335. procedure TLinkRes.AddFileName(const s:string);
  336. begin
  337. if s<>'' then
  338. begin
  339. if not(s[1] in ['a'..'z','A'..'Z','/','\','.','"']) then
  340. begin
  341. if cs_link_on_target in aktglobalswitches then
  342. inherited Add('.'+target_info.DirSep+s)
  343. else
  344. inherited Add('.'+source_info.DirSep+s);
  345. end
  346. else
  347. inherited Add(s);
  348. end;
  349. end;
  350. end.
  351. {
  352. $Log$
  353. Revision 1.24 2003-09-30 19:54:23 peter
  354. * better link on target support
  355. Revision 1.23 2003/09/16 13:42:39 marco
  356. * Had a useless dependancy on unit unix in 1_1 mode
  357. Revision 1.22 2003/09/14 20:26:18 marco
  358. * Unix reform
  359. Revision 1.21 2003/04/22 14:33:38 peter
  360. * removed some notes/hints
  361. Revision 1.20 2003/02/07 21:21:39 marco
  362. * Some small fix
  363. Revision 1.19 2003/01/10 21:49:00 marco
  364. * more hasunix fixes
  365. Revision 1.18 2003/01/06 20:16:42 peter
  366. * don't prepend ./ to quoted filenames
  367. Revision 1.17 2002/11/15 01:58:54 peter
  368. * merged changes from 1.0.7 up to 04-11
  369. - -V option for generating bug report tracing
  370. - more tracing for option parsing
  371. - errors for cdecl and high()
  372. - win32 import stabs
  373. - win32 records<=8 are returned in eax:edx (turned off by default)
  374. - heaptrc update
  375. - more info for temp management in .s file with EXTDEBUG
  376. Revision 1.16 2002/05/18 13:34:18 peter
  377. * readded missing revisions
  378. Revision 1.15 2002/05/16 19:46:44 carl
  379. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  380. + try to fix temp allocation (still in ifdef)
  381. + generic constructor calls
  382. + start of tassembler / tmodulebase class cleanup
  383. }