script.pas 14 KB

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