script.pas 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  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[100];
  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. TAsmScriptMPW = class (TAsmScript)
  68. Constructor Create (Const ScriptName : String); override;
  69. Procedure AddAsmCommand (Const Command, Options,FileName : String);override;
  70. Procedure AddLinkCommand (Const Command, Options, FileName : String);override;
  71. Procedure AddDeleteCommand (Const FileName : String);override;
  72. Procedure AddDeleteDirCommand (Const FileName : String);override;
  73. Procedure WriteToDisk;override;
  74. end;
  75. TLinkRes = Class (TScript)
  76. procedure Add(const s:string);
  77. procedure AddFileName(const s:string);
  78. end;
  79. var
  80. AsmRes : TAsmScript;
  81. Function ScriptFixFileName(const s:string):string;
  82. Procedure GenerateAsmRes(const st : string);
  83. implementation
  84. uses
  85. {$ifdef hasUnix}
  86. {$ifdef havelinuxrtl10}
  87. Linux,
  88. {$else}
  89. BaseUnix,
  90. {$endif}
  91. {$endif}
  92. cutils,
  93. globtype,globals,systems,verbose;
  94. {****************************************************************************
  95. Helpers
  96. ****************************************************************************}
  97. Function ScriptFixFileName(const s:string):string;
  98. begin
  99. if cs_link_on_target in aktglobalswitches then
  100. ScriptFixFileName:=TargetFixFileName(s)
  101. else
  102. ScriptFixFileName:=FixFileName(s);
  103. end;
  104. {****************************************************************************
  105. TScript
  106. ****************************************************************************}
  107. constructor TScript.Create(const s:string);
  108. begin
  109. fn:=FixFileName(s);
  110. executable:=false;
  111. data:=TStringList.Create;
  112. end;
  113. constructor TScript.CreateExec(const s:string);
  114. begin
  115. fn:=FixFileName(s);
  116. if cs_link_on_target in aktglobalswitches then
  117. fn:=AddExtension(fn,target_info.scriptext)
  118. else
  119. fn:=AddExtension(fn,source_info.scriptext);
  120. executable:=true;
  121. data:=TStringList.Create;
  122. end;
  123. destructor TScript.Destroy;
  124. begin
  125. data.Free;
  126. end;
  127. procedure TScript.AddStart(const s:string);
  128. begin
  129. data.Insert(s);
  130. end;
  131. procedure TScript.Add(const s:string);
  132. begin
  133. data.Concat(s);
  134. end;
  135. Function TScript.Empty:boolean;
  136. begin
  137. Empty:=Data.Empty;
  138. end;
  139. procedure TScript.WriteToDisk;
  140. var
  141. t : file;
  142. i : longint;
  143. s : string;
  144. le: string[2];
  145. begin
  146. Assign(t,fn);
  147. if cs_link_on_target in aktglobalswitches then
  148. le:= target_info.newline
  149. else
  150. le:= source_info.newline;
  151. {$I-}
  152. Rewrite(t,1);
  153. if ioresult<>0 then
  154. exit;
  155. while not data.Empty do
  156. begin
  157. s:=data.GetFirst;
  158. Blockwrite(t,s[1],length(s),i);
  159. Blockwrite(t,le[1],length(le),i);
  160. end;
  161. Close(t);
  162. {$I+}
  163. i:=ioresult;
  164. {$ifdef hasUnix}
  165. if executable then
  166. {$ifdef havelinuxrtl10}ChMod{$else}fpchmod{$endif}(fn,493);
  167. {$endif}
  168. end;
  169. {****************************************************************************
  170. Asm Response
  171. ****************************************************************************}
  172. Constructor TAsmScript.Create (Const ScriptName : String);
  173. begin
  174. Inherited CreateExec(ScriptName);
  175. end;
  176. {****************************************************************************
  177. DOS Asm Response
  178. ****************************************************************************}
  179. Constructor TAsmScriptDos.Create (Const ScriptName : String);
  180. begin
  181. Inherited Create(ScriptName);
  182. end;
  183. Procedure TAsmScriptDos.AddAsmCommand (Const Command, Options,FileName : String);
  184. begin
  185. if FileName<>'' then
  186. begin
  187. Add('SET THEFILE='+ScriptFixFileName(FileName));
  188. Add('echo Assembling %THEFILE%');
  189. end;
  190. Add(maybequoted(command)+' '+Options);
  191. Add('if errorlevel 1 goto asmend');
  192. end;
  193. Procedure TAsmScriptDos.AddLinkCommand (Const Command, Options, FileName : String);
  194. begin
  195. if FileName<>'' then
  196. begin
  197. Add('SET THEFILE='+ScriptFixFileName(FileName));
  198. Add('echo Linking %THEFILE%');
  199. end;
  200. Add(maybequoted(command)+' '+Options);
  201. Add('if errorlevel 1 goto linkend');
  202. end;
  203. Procedure TAsmScriptDos.AddDeleteCommand (Const FileName : String);
  204. begin
  205. Add('Del ' + MaybeQuoted (ScriptFixFileName (FileName)));
  206. end;
  207. Procedure TAsmScriptDos.AddDeleteDirCommand (Const FileName : String);
  208. begin
  209. Add('Rmdir ' + MaybeQuoted (ScriptFixFileName (FileName)));
  210. end;
  211. Procedure TAsmScriptDos.WriteToDisk;
  212. Begin
  213. AddStart('@echo off');
  214. Add('goto end');
  215. Add(':asmend');
  216. Add('echo An error occured while assembling %THEFILE%');
  217. Add('goto end');
  218. Add(':linkend');
  219. Add('echo An error occured while linking %THEFILE%');
  220. Add(':end');
  221. inherited WriteToDisk;
  222. end;
  223. {****************************************************************************
  224. Amiga Asm Response
  225. ****************************************************************************}
  226. Constructor TAsmScriptAmiga.Create (Const ScriptName : String);
  227. begin
  228. Inherited Create(ScriptName);
  229. end;
  230. Procedure TAsmScriptAmiga.AddAsmCommand (Const Command, Options,FileName : String);
  231. begin
  232. if FileName<>'' then
  233. begin
  234. Add('SET THEFILE '+ScriptFixFileName(FileName));
  235. Add('echo Assembling $THEFILE');
  236. end;
  237. Add(maybequoted(command)+' '+Options);
  238. { There is a problem here,
  239. as allways return with a non zero error value PM }
  240. Add('if error');
  241. Add('why');
  242. Add('skip asmend');
  243. Add('endif');
  244. end;
  245. Procedure TAsmScriptAmiga.AddLinkCommand (Const Command, Options, FileName : String);
  246. begin
  247. if FileName<>'' then
  248. begin
  249. Add('SET THEFILE '+ScriptFixFileName(FileName));
  250. Add('echo Linking $THEFILE');
  251. end;
  252. Add(maybequoted(command)+' '+Options);
  253. Add('if error');
  254. Add('skip linkend');
  255. Add('endif');
  256. end;
  257. Procedure TAsmScriptAmiga.AddDeleteCommand (Const FileName : String);
  258. begin
  259. Add('Delete ' + MaybeQuoted (ScriptFixFileName(FileName)));
  260. end;
  261. Procedure TAsmScriptAmiga.AddDeleteDirCommand (Const FileName : String);
  262. begin
  263. Add('Delete ' + MaybeQuoted (ScriptFixFileName(FileName)));
  264. end;
  265. Procedure TAsmScriptAmiga.WriteToDisk;
  266. Begin
  267. Add('skip end');
  268. Add('lab asmend');
  269. Add('why');
  270. Add('echo An error occured while assembling $THEFILE');
  271. Add('skip end');
  272. Add('lab linkend');
  273. Add('why');
  274. Add('echo An error occured while linking $THEFILE');
  275. Add('lab end');
  276. inherited WriteToDisk;
  277. end;
  278. {****************************************************************************
  279. Unix Asm Response
  280. ****************************************************************************}
  281. Constructor TAsmScriptUnix.Create (Const ScriptName : String);
  282. begin
  283. Inherited Create(ScriptName);
  284. end;
  285. Procedure TAsmScriptUnix.AddAsmCommand (Const Command, Options,FileName : String);
  286. begin
  287. if FileName<>'' then
  288. Add('echo Assembling '+ScriptFixFileName(FileName));
  289. Add(maybequoted(command)+' '+Options);
  290. Add('if [ $? != 0 ]; then DoExitAsm '+ScriptFixFileName(FileName)+'; fi');
  291. end;
  292. Procedure TAsmScriptUnix.AddLinkCommand (Const Command, Options, FileName : String);
  293. begin
  294. if FileName<>'' then
  295. Add('echo Linking '+ScriptFixFileName(FileName));
  296. Add(maybequoted(command)+' '+Options);
  297. Add('if [ $? != 0 ]; then DoExitLink '+ScriptFixFileName(FileName)+'; fi');
  298. end;
  299. Procedure TAsmScriptUnix.AddDeleteCommand (Const FileName : String);
  300. begin
  301. Add('rm ' + MaybeQuoted (ScriptFixFileName(FileName)));
  302. end;
  303. Procedure TAsmScriptUnix.AddDeleteDirCommand (Const FileName : String);
  304. begin
  305. Add('rmdir ' + MaybeQuoted (ScriptFixFileName(FileName)));
  306. end;
  307. Procedure TAsmScriptUnix.WriteToDisk;
  308. Begin
  309. AddStart('{ echo "An error occurred while linking $1"; exit 1; }');
  310. AddStart('DoExitLink ()');
  311. AddStart('{ echo "An error occurred while assembling $1"; exit 1; }');
  312. AddStart('DoExitAsm ()');
  313. {$ifdef BEOS}
  314. AddStart('#!/boot/beos/bin/sh');
  315. {$else}
  316. AddStart('#!/bin/sh');
  317. {$endif}
  318. inherited WriteToDisk;
  319. end;
  320. {****************************************************************************
  321. MPW (MacOS) Asm Response
  322. ****************************************************************************}
  323. Constructor TAsmScriptMPW.Create (Const ScriptName : String);
  324. begin
  325. Inherited Create(ScriptName);
  326. end;
  327. Procedure TAsmScriptMPW.AddAsmCommand (Const Command, Options,FileName : String);
  328. begin
  329. if FileName<>'' then
  330. Add('Echo Assembling '+ScriptFixFileName(FileName));
  331. Add(maybequoted(command)+' '+Options);
  332. Add('Exit If "{Status}" != 0');
  333. end;
  334. Procedure TAsmScriptMPW.AddLinkCommand (Const Command, Options, FileName : String);
  335. begin
  336. if FileName<>'' then
  337. Add('Echo Linking '+ScriptFixFileName(FileName));
  338. Add(maybequoted(command)+' '+Options);
  339. Add('Exit If "{Status}" != 0');
  340. {Add resources}
  341. if apptype = app_cui then {If SIOW}
  342. begin
  343. Add('Rez -append "{RIncludes}"SIOW.r -o '+ ScriptFixFileName(FileName));
  344. Add('Exit If "{Status}" != 0');
  345. end;
  346. end;
  347. Procedure TAsmScriptMPW.AddDeleteCommand (Const FileName : String);
  348. begin
  349. Add('Delete ' + MaybeQuoted (ScriptFixFileName(FileName)));
  350. end;
  351. Procedure TAsmScriptMPW.AddDeleteDirCommand (Const FileName : String);
  352. begin
  353. Add('Delete ' + MaybeQuoted (ScriptFixFileName (FileName)));
  354. end;
  355. Procedure TAsmScriptMPW.WriteToDisk;
  356. Begin
  357. AddStart('# Script for assembling and linking a FreePascal program on MPW (MacOS)');
  358. Add('Echo Done');
  359. inherited WriteToDisk;
  360. end;
  361. Procedure GenerateAsmRes(const st : string);
  362. var
  363. scripttyp : tscripttype;
  364. begin
  365. if cs_link_on_target in aktglobalswitches then
  366. scripttyp := target_info.script
  367. else
  368. scripttyp := source_info.script;
  369. case scripttyp of
  370. script_unix :
  371. AsmRes:=TAsmScriptUnix.Create(st);
  372. script_dos :
  373. AsmRes:=TAsmScriptDos.Create(st);
  374. script_amiga :
  375. AsmRes:=TAsmScriptAmiga.Create(st);
  376. script_mpw :
  377. AsmRes:=TAsmScriptMPW.Create(st);
  378. end;
  379. end;
  380. {****************************************************************************
  381. Link Response
  382. ****************************************************************************}
  383. procedure TLinkRes.Add(const s:string);
  384. begin
  385. if s<>'' then
  386. inherited Add(s);
  387. end;
  388. procedure TLinkRes.AddFileName(const s:string);
  389. begin
  390. if s<>'' then
  391. begin
  392. if not(s[1] in ['a'..'z','A'..'Z','/','\','.','"']) then
  393. begin
  394. if cs_link_on_target in aktglobalswitches then
  395. inherited Add('.'+target_info.DirSep+s)
  396. else
  397. inherited Add('.'+source_info.DirSep+s);
  398. end
  399. else
  400. inherited Add(s);
  401. end;
  402. end;
  403. end.
  404. {
  405. $Log$
  406. Revision 1.31 2004-12-27 19:48:58 hajny
  407. * missing maybequoted() added for deletion of generated .s files during external assembly
  408. Revision 1.30 2004/08/20 10:23:35 olle
  409. * minor fix for macos
  410. Revision 1.29 2004/06/20 08:55:30 florian
  411. * logs truncated
  412. Revision 1.28 2004/04/06 22:44:16 olle
  413. + Status checks in scripts
  414. + Scripts support apptype tool
  415. + Added some ScriptFixFileName
  416. Revision 1.27 2004/02/24 00:53:48 olle
  417. * increased maxsize of link.res file name
  418. * fixed a 255-limit in TScript.WriteToDisk
  419. Revision 1.26 2004/02/19 20:40:15 olle
  420. + Support for Link on target especially for MacOS
  421. + TLinkerMPW
  422. + TAsmScriptMPW
  423. }