script.pas 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. {
  2. Copyright (c) 1998-2002 by Peter Vreman
  3. This unit handles the writing of script files
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit script;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. cclasses;
  22. type
  23. TScript=class
  24. fn : string[100];
  25. data : TStringList;
  26. executable : boolean;
  27. constructor Create(const s:string);
  28. constructor CreateExec(const s:string);
  29. destructor Destroy;override;
  30. procedure AddStart(const s:string);
  31. procedure Add(const s:string);
  32. Function Empty:boolean;
  33. procedure WriteToDisk;virtual;
  34. end;
  35. TAsmScript = class (TScript)
  36. Constructor Create(Const ScriptName : String); virtual;
  37. Procedure AddAsmCommand (Const Command, Options,FileName : String);virtual;abstract;
  38. Procedure AddLinkCommand (Const Command, Options, FileName : String);virtual;abstract;
  39. Procedure AddDeleteCommand (Const FileName : String);virtual;abstract;
  40. Procedure AddDeleteDirCommand (Const FileName : String);virtual;abstract;
  41. end;
  42. TAsmScriptDos = class (TAsmScript)
  43. Constructor Create (Const ScriptName : String); override;
  44. Procedure AddAsmCommand (Const Command, Options,FileName : String);override;
  45. Procedure AddLinkCommand (Const Command, Options, FileName : String);override;
  46. Procedure AddDeleteCommand (Const FileName : String);override;
  47. Procedure AddDeleteDirCommand (Const FileName : String);override;
  48. Procedure WriteToDisk;override;
  49. end;
  50. TAsmScriptAmiga = class (TAsmScript)
  51. Constructor Create (Const ScriptName : String); override;
  52. Procedure AddAsmCommand (Const Command, Options,FileName : String);override;
  53. Procedure AddLinkCommand (Const Command, Options, FileName : String);override;
  54. Procedure AddDeleteCommand (Const FileName : String);override;
  55. Procedure AddDeleteDirCommand (Const FileName : String);override;
  56. Procedure WriteToDisk;override;
  57. end;
  58. TAsmScriptUnix = class (TAsmScript)
  59. Constructor Create (Const ScriptName : String);override;
  60. Procedure AddAsmCommand (Const Command, Options,FileName : String);override;
  61. Procedure AddLinkCommand (Const Command, Options, FileName : String);override;
  62. Procedure AddDeleteCommand (Const FileName : String);override;
  63. Procedure AddDeleteDirCommand (Const FileName : String);override;
  64. Procedure WriteToDisk;override;
  65. end;
  66. TAsmScriptMPW = class (TAsmScript)
  67. Constructor Create (Const ScriptName : String); override;
  68. Procedure AddAsmCommand (Const Command, Options,FileName : String);override;
  69. Procedure AddLinkCommand (Const Command, Options, FileName : String);override;
  70. Procedure AddDeleteCommand (Const FileName : String);override;
  71. Procedure AddDeleteDirCommand (Const FileName : String);override;
  72. Procedure WriteToDisk;override;
  73. end;
  74. TLinkRes = Class (TScript)
  75. procedure Add(const s:string);
  76. procedure AddFileName(const s:string);
  77. end;
  78. var
  79. AsmRes : TAsmScript;
  80. Function ScriptFixFileName(const s:string):string;
  81. Procedure GenerateAsmRes(const st : string);
  82. implementation
  83. uses
  84. {$ifdef hasUnix}
  85. {$ifdef havelinuxrtl10}
  86. Linux,
  87. {$else}
  88. BaseUnix,
  89. {$endif}
  90. {$endif}
  91. cutils,
  92. globtype,globals,systems,verbose;
  93. {****************************************************************************
  94. Helpers
  95. ****************************************************************************}
  96. Function ScriptFixFileName(const s:string):string;
  97. begin
  98. if cs_link_on_target in aktglobalswitches then
  99. ScriptFixFileName:=TargetFixFileName(s)
  100. else
  101. ScriptFixFileName:=FixFileName(s);
  102. end;
  103. {****************************************************************************
  104. TScript
  105. ****************************************************************************}
  106. constructor TScript.Create(const s:string);
  107. begin
  108. fn:=FixFileName(s);
  109. executable:=false;
  110. data:=TStringList.Create;
  111. end;
  112. constructor TScript.CreateExec(const s:string);
  113. begin
  114. fn:=FixFileName(s);
  115. if cs_link_on_target in aktglobalswitches then
  116. fn:=AddExtension(fn,target_info.scriptext)
  117. else
  118. fn:=AddExtension(fn,source_info.scriptext);
  119. executable:=true;
  120. data:=TStringList.Create;
  121. end;
  122. destructor TScript.Destroy;
  123. begin
  124. data.Free;
  125. end;
  126. procedure TScript.AddStart(const s:string);
  127. begin
  128. data.Insert(s);
  129. end;
  130. procedure TScript.Add(const s:string);
  131. begin
  132. data.Concat(s);
  133. end;
  134. Function TScript.Empty:boolean;
  135. begin
  136. Empty:=Data.Empty;
  137. end;
  138. procedure TScript.WriteToDisk;
  139. var
  140. t : file;
  141. i : longint;
  142. s : string;
  143. le: string[2];
  144. begin
  145. Assign(t,fn);
  146. if cs_link_on_target in aktglobalswitches then
  147. le:= target_info.newline
  148. else
  149. le:= source_info.newline;
  150. {$I-}
  151. Rewrite(t,1);
  152. if ioresult<>0 then
  153. exit;
  154. while not data.Empty do
  155. begin
  156. s:=data.GetFirst;
  157. Blockwrite(t,s[1],length(s),i);
  158. Blockwrite(t,le[1],length(le),i);
  159. end;
  160. Close(t);
  161. {$I+}
  162. i:=ioresult;
  163. {$ifdef hasUnix}
  164. if executable then
  165. {$ifdef havelinuxrtl10}ChMod{$else}fpchmod{$endif}(fn,493);
  166. {$endif}
  167. end;
  168. {****************************************************************************
  169. Asm Response
  170. ****************************************************************************}
  171. Constructor TAsmScript.Create (Const ScriptName : String);
  172. begin
  173. Inherited CreateExec(ScriptName);
  174. end;
  175. {****************************************************************************
  176. DOS Asm Response
  177. ****************************************************************************}
  178. Constructor TAsmScriptDos.Create (Const ScriptName : String);
  179. begin
  180. Inherited Create(ScriptName);
  181. end;
  182. Procedure TAsmScriptDos.AddAsmCommand (Const Command, Options,FileName : String);
  183. begin
  184. if FileName<>'' then
  185. begin
  186. Add('SET THEFILE='+ScriptFixFileName(FileName));
  187. Add('echo Assembling %THEFILE%');
  188. end;
  189. Add(maybequoted(command)+' '+Options);
  190. Add('if errorlevel 1 goto asmend');
  191. end;
  192. Procedure TAsmScriptDos.AddLinkCommand (Const Command, Options, FileName : String);
  193. begin
  194. if FileName<>'' then
  195. begin
  196. Add('SET THEFILE='+ScriptFixFileName(FileName));
  197. Add('echo Linking %THEFILE%');
  198. end;
  199. Add(maybequoted(command)+' '+Options);
  200. Add('if errorlevel 1 goto linkend');
  201. end;
  202. Procedure TAsmScriptDos.AddDeleteCommand (Const FileName : String);
  203. begin
  204. Add('Del ' + MaybeQuoted (ScriptFixFileName (FileName)));
  205. end;
  206. Procedure TAsmScriptDos.AddDeleteDirCommand (Const FileName : String);
  207. begin
  208. Add('Rmdir ' + MaybeQuoted (ScriptFixFileName (FileName)));
  209. end;
  210. Procedure TAsmScriptDos.WriteToDisk;
  211. Begin
  212. AddStart('@echo off');
  213. Add('goto end');
  214. Add(':asmend');
  215. Add('echo An error occured while assembling %THEFILE%');
  216. Add('goto end');
  217. Add(':linkend');
  218. Add('echo An error occured while linking %THEFILE%');
  219. Add(':end');
  220. inherited WriteToDisk;
  221. end;
  222. {****************************************************************************
  223. Amiga Asm Response
  224. ****************************************************************************}
  225. Constructor TAsmScriptAmiga.Create (Const ScriptName : String);
  226. begin
  227. Inherited Create(ScriptName);
  228. end;
  229. Procedure TAsmScriptAmiga.AddAsmCommand (Const Command, Options,FileName : String);
  230. begin
  231. if FileName<>'' then
  232. begin
  233. Add('SET THEFILE '+ScriptFixFileName(FileName));
  234. Add('echo Assembling $THEFILE');
  235. end;
  236. Add(maybequoted(command)+' '+Options);
  237. { There is a problem here,
  238. as allways return with a non zero error value PM }
  239. Add('if error');
  240. Add('why');
  241. Add('skip asmend');
  242. Add('endif');
  243. end;
  244. Procedure TAsmScriptAmiga.AddLinkCommand (Const Command, Options, FileName : String);
  245. begin
  246. if FileName<>'' then
  247. begin
  248. Add('SET THEFILE '+ScriptFixFileName(FileName));
  249. Add('echo Linking $THEFILE');
  250. end;
  251. Add(maybequoted(command)+' '+Options);
  252. Add('if error');
  253. Add('skip linkend');
  254. Add('endif');
  255. end;
  256. Procedure TAsmScriptAmiga.AddDeleteCommand (Const FileName : String);
  257. begin
  258. Add('Delete ' + MaybeQuoted (ScriptFixFileName(FileName)));
  259. end;
  260. Procedure TAsmScriptAmiga.AddDeleteDirCommand (Const FileName : String);
  261. begin
  262. Add('Delete ' + MaybeQuoted (ScriptFixFileName(FileName)));
  263. end;
  264. Procedure TAsmScriptAmiga.WriteToDisk;
  265. Begin
  266. Add('skip end');
  267. Add('lab asmend');
  268. Add('why');
  269. Add('echo An error occured while assembling $THEFILE');
  270. Add('skip end');
  271. Add('lab linkend');
  272. Add('why');
  273. Add('echo An error occured while linking $THEFILE');
  274. Add('lab end');
  275. inherited WriteToDisk;
  276. end;
  277. {****************************************************************************
  278. Unix Asm Response
  279. ****************************************************************************}
  280. Constructor TAsmScriptUnix.Create (Const ScriptName : String);
  281. begin
  282. Inherited Create(ScriptName);
  283. end;
  284. Procedure TAsmScriptUnix.AddAsmCommand (Const Command, Options,FileName : String);
  285. begin
  286. if FileName<>'' then
  287. Add('echo Assembling '+ScriptFixFileName(FileName));
  288. Add(maybequoted(command)+' '+Options);
  289. Add('if [ $? != 0 ]; then DoExitAsm '+ScriptFixFileName(FileName)+'; fi');
  290. end;
  291. Procedure TAsmScriptUnix.AddLinkCommand (Const Command, Options, FileName : String);
  292. begin
  293. if FileName<>'' then
  294. Add('echo Linking '+ScriptFixFileName(FileName));
  295. Add(maybequoted(command)+' '+Options);
  296. Add('if [ $? != 0 ]; then DoExitLink '+ScriptFixFileName(FileName)+'; fi');
  297. end;
  298. Procedure TAsmScriptUnix.AddDeleteCommand (Const FileName : String);
  299. begin
  300. Add('rm ' + MaybeQuoted (ScriptFixFileName(FileName)));
  301. end;
  302. Procedure TAsmScriptUnix.AddDeleteDirCommand (Const FileName : String);
  303. begin
  304. Add('rmdir ' + MaybeQuoted (ScriptFixFileName(FileName)));
  305. end;
  306. Procedure TAsmScriptUnix.WriteToDisk;
  307. Begin
  308. AddStart('{ echo "An error occurred while linking $1"; exit 1; }');
  309. AddStart('DoExitLink ()');
  310. AddStart('{ echo "An error occurred while assembling $1"; exit 1; }');
  311. AddStart('DoExitAsm ()');
  312. {$ifdef BEOS}
  313. AddStart('#!/boot/beos/bin/sh');
  314. {$else}
  315. AddStart('#!/bin/sh');
  316. {$endif}
  317. inherited WriteToDisk;
  318. end;
  319. {****************************************************************************
  320. MPW (MacOS) Asm Response
  321. ****************************************************************************}
  322. Constructor TAsmScriptMPW.Create (Const ScriptName : String);
  323. begin
  324. Inherited Create(ScriptName);
  325. end;
  326. Procedure TAsmScriptMPW.AddAsmCommand (Const Command, Options,FileName : String);
  327. begin
  328. if FileName<>'' then
  329. Add('Echo Assembling '+ScriptFixFileName(FileName));
  330. Add(maybequoted(command)+' '+Options);
  331. Add('Exit If "{Status}" != 0');
  332. end;
  333. Procedure TAsmScriptMPW.AddLinkCommand (Const Command, Options, FileName : String);
  334. begin
  335. if FileName<>'' then
  336. Add('Echo Linking '+ScriptFixFileName(FileName));
  337. Add(maybequoted(command)+' '+Options);
  338. Add('Exit If "{Status}" != 0');
  339. {Add resources}
  340. if apptype = app_cui then {If SIOW}
  341. begin
  342. Add('Rez -append "{RIncludes}"SIOW.r -o '+ ScriptFixFileName(FileName));
  343. Add('Exit If "{Status}" != 0');
  344. end;
  345. end;
  346. Procedure TAsmScriptMPW.AddDeleteCommand (Const FileName : String);
  347. begin
  348. Add('Delete ' + MaybeQuoted (ScriptFixFileName(FileName)));
  349. end;
  350. Procedure TAsmScriptMPW.AddDeleteDirCommand (Const FileName : String);
  351. begin
  352. Add('Delete ' + MaybeQuoted (ScriptFixFileName (FileName)));
  353. end;
  354. Procedure TAsmScriptMPW.WriteToDisk;
  355. Begin
  356. AddStart('# Script for assembling and linking a FreePascal program on MPW (MacOS)');
  357. Add('Echo Done');
  358. inherited WriteToDisk;
  359. end;
  360. Procedure GenerateAsmRes(const st : string);
  361. var
  362. scripttyp : tscripttype;
  363. begin
  364. if cs_link_on_target in aktglobalswitches then
  365. scripttyp := target_info.script
  366. else
  367. scripttyp := source_info.script;
  368. case scripttyp of
  369. script_unix :
  370. AsmRes:=TAsmScriptUnix.Create(st);
  371. script_dos :
  372. AsmRes:=TAsmScriptDos.Create(st);
  373. script_amiga :
  374. AsmRes:=TAsmScriptAmiga.Create(st);
  375. script_mpw :
  376. AsmRes:=TAsmScriptMPW.Create(st);
  377. end;
  378. end;
  379. {****************************************************************************
  380. Link Response
  381. ****************************************************************************}
  382. procedure TLinkRes.Add(const s:string);
  383. begin
  384. if s<>'' then
  385. inherited Add(s);
  386. end;
  387. procedure TLinkRes.AddFileName(const s:string);
  388. begin
  389. if s<>'' then
  390. begin
  391. if not(s[1] in ['a'..'z','A'..'Z','/','\','.','"']) then
  392. begin
  393. if cs_link_on_target in aktglobalswitches then
  394. inherited Add('.'+target_info.DirSep+s)
  395. else
  396. inherited Add('.'+source_info.DirSep+s);
  397. end
  398. else
  399. inherited Add(s);
  400. end;
  401. end;
  402. end.