2
0

script.pas 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  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. BaseUnix,
  86. {$endif}
  87. SysUtils,
  88. cutils,cfileutils,
  89. globtype,globals,systems,verbose;
  90. {****************************************************************************
  91. Helpers
  92. ****************************************************************************}
  93. Function ScriptFixFileName(const s:string):string;
  94. begin
  95. if cs_link_on_target in current_settings.globalswitches then
  96. ScriptFixFileName:=TargetFixFileName(s)
  97. else
  98. ScriptFixFileName:=FixFileName(s);
  99. end;
  100. {****************************************************************************
  101. TScript
  102. ****************************************************************************}
  103. constructor TScript.Create(const s:string);
  104. begin
  105. fn:=FixFileName(s);
  106. executable:=false;
  107. data:=TStringList.Create;
  108. end;
  109. constructor TScript.CreateExec(const s:string);
  110. begin
  111. fn:=FixFileName(s);
  112. if cs_link_on_target in current_settings.globalswitches then
  113. fn:=ChangeFileExt(fn,target_info.scriptext)
  114. else
  115. fn:=ChangeFileExt(fn,source_info.scriptext);
  116. executable:=true;
  117. data:=TStringList.Create;
  118. end;
  119. destructor TScript.Destroy;
  120. begin
  121. data.Free;
  122. end;
  123. procedure TScript.AddStart(const s:string);
  124. begin
  125. data.Insert(s);
  126. end;
  127. procedure TScript.Add(const s:string);
  128. begin
  129. data.Concat(s);
  130. end;
  131. Function TScript.Empty:boolean;
  132. begin
  133. Empty:=Data.Empty;
  134. end;
  135. procedure TScript.WriteToDisk;
  136. var
  137. t : file;
  138. i : longint;
  139. s : string;
  140. le: string[2];
  141. begin
  142. Assign(t,fn);
  143. if cs_link_on_target in current_settings.globalswitches then
  144. le:= target_info.newline
  145. else
  146. le:= source_info.newline;
  147. {$I-}
  148. Rewrite(t,1);
  149. if ioresult<>0 then
  150. exit;
  151. while not data.Empty do
  152. begin
  153. s:=data.GetFirst;
  154. Blockwrite(t,s[1],length(s),i);
  155. Blockwrite(t,le[1],length(le),i);
  156. end;
  157. Close(t);
  158. {$I+}
  159. i:=ioresult;
  160. {$ifdef hasUnix}
  161. if executable then
  162. fpchmod(fn,493);
  163. {$endif}
  164. end;
  165. {****************************************************************************
  166. Asm Response
  167. ****************************************************************************}
  168. Constructor TAsmScript.Create (Const ScriptName : String);
  169. begin
  170. Inherited CreateExec(ScriptName);
  171. end;
  172. {****************************************************************************
  173. DOS Asm Response
  174. ****************************************************************************}
  175. Constructor TAsmScriptDos.Create (Const ScriptName : String);
  176. begin
  177. Inherited Create(ScriptName);
  178. end;
  179. Procedure TAsmScriptDos.AddAsmCommand (Const Command, Options,FileName : String);
  180. begin
  181. if FileName<>'' then
  182. begin
  183. Add('SET THEFILE='+ScriptFixFileName(FileName));
  184. Add('echo Assembling %THEFILE%');
  185. end;
  186. Add(maybequoted(command)+' '+Options);
  187. Add('if errorlevel 1 goto asmend');
  188. end;
  189. Procedure TAsmScriptDos.AddLinkCommand (Const Command, Options, FileName : String);
  190. begin
  191. if FileName<>'' then
  192. begin
  193. Add('SET THEFILE='+ScriptFixFileName(FileName));
  194. Add('echo Linking %THEFILE%');
  195. end;
  196. Add(maybequoted(command)+' '+Options);
  197. Add('if errorlevel 1 goto linkend');
  198. end;
  199. Procedure TAsmScriptDos.AddDeleteCommand (Const FileName : String);
  200. begin
  201. Add('Del ' + MaybeQuoted (ScriptFixFileName (FileName)));
  202. end;
  203. Procedure TAsmScriptDos.AddDeleteDirCommand (Const FileName : String);
  204. begin
  205. Add('Rmdir ' + MaybeQuoted (ScriptFixFileName (FileName)));
  206. end;
  207. Procedure TAsmScriptDos.WriteToDisk;
  208. Begin
  209. AddStart('@echo off');
  210. Add('goto end');
  211. Add(':asmend');
  212. Add('echo An error occured while assembling %THEFILE%');
  213. Add('goto end');
  214. Add(':linkend');
  215. Add('echo An error occured while linking %THEFILE%');
  216. Add(':end');
  217. inherited WriteToDisk;
  218. end;
  219. {****************************************************************************
  220. Amiga Asm Response
  221. ****************************************************************************}
  222. { * PathConv is required, since Amiga commands can't handle Unix-style
  223. relative paths used by the compiler (KB) * }
  224. {$IF DEFINED(MORPHOS) OR DEFINED(AMIGA)}
  225. { * PathConv is implemented in the system unit! * }
  226. function PathConv(path: string): string; external name 'PATHCONV';
  227. {$ELSE}
  228. function PathConv(path: string): string;
  229. begin
  230. PathConv:=path;
  231. end;
  232. {$ENDIF}
  233. Constructor TAsmScriptAmiga.Create (Const ScriptName : String);
  234. begin
  235. Inherited Create(ScriptName);
  236. end;
  237. Procedure TAsmScriptAmiga.AddAsmCommand (Const Command, Options,FileName : String);
  238. begin
  239. if FileName<>'' then
  240. begin
  241. Add('SET THEFILE '+ScriptFixFileName(FileName));
  242. Add('echo Assembling $THEFILE');
  243. end;
  244. Add(maybequoted(command)+' '+Options);
  245. { There is a problem here,
  246. as always return with a non zero error value PM }
  247. Add('if error');
  248. Add('why');
  249. Add('skip asmend');
  250. Add('endif');
  251. end;
  252. Procedure TAsmScriptAmiga.AddLinkCommand (Const Command, Options, FileName : String);
  253. begin
  254. if FileName<>'' then
  255. begin
  256. Add('SET THEFILE '+ScriptFixFileName(FileName));
  257. Add('echo Linking $THEFILE');
  258. end;
  259. Add(maybequoted(command)+' '+Options);
  260. Add('if error');
  261. Add('skip linkend');
  262. Add('endif');
  263. end;
  264. Procedure TAsmScriptAmiga.AddDeleteCommand (Const FileName : String);
  265. begin
  266. Add('Delete ' + PathConv(MaybeQuoted(ScriptFixFileName(FileName))) + ' Quiet');
  267. end;
  268. Procedure TAsmScriptAmiga.AddDeleteDirCommand (Const FileName : String);
  269. begin
  270. Add('Delete ' + PathConv(MaybeQuoted(ScriptFixFileName(FileName))) + ' All Quiet');
  271. end;
  272. Procedure TAsmScriptAmiga.WriteToDisk;
  273. Begin
  274. Add('skip end');
  275. Add('lab asmend');
  276. Add('why');
  277. Add('echo An error occured while assembling $THEFILE');
  278. Add('skip end');
  279. Add('lab linkend');
  280. Add('why');
  281. Add('echo An error occured while linking $THEFILE');
  282. Add('lab end');
  283. inherited WriteToDisk;
  284. end;
  285. {****************************************************************************
  286. Unix Asm Response
  287. ****************************************************************************}
  288. Constructor TAsmScriptUnix.Create (Const ScriptName : String);
  289. begin
  290. Inherited Create(ScriptName);
  291. end;
  292. Procedure TAsmScriptUnix.AddAsmCommand (Const Command, Options,FileName : String);
  293. begin
  294. if FileName<>'' then
  295. Add('echo Assembling '+ScriptFixFileName(FileName));
  296. Add(maybequoted(command)+' '+Options);
  297. Add('if [ $? != 0 ]; then DoExitAsm '+ScriptFixFileName(FileName)+'; fi');
  298. end;
  299. Procedure TAsmScriptUnix.AddLinkCommand (Const Command, Options, FileName : String);
  300. begin
  301. if FileName<>'' then
  302. Add('echo Linking '+ScriptFixFileName(FileName));
  303. Add(maybequoted(command)+' '+Options);
  304. Add('if [ $? != 0 ]; then DoExitLink '+ScriptFixFileName(FileName)+'; fi');
  305. end;
  306. Procedure TAsmScriptUnix.AddDeleteCommand (Const FileName : String);
  307. begin
  308. Add('rm ' + MaybeQuoted (ScriptFixFileName(FileName)));
  309. end;
  310. Procedure TAsmScriptUnix.AddDeleteDirCommand (Const FileName : String);
  311. begin
  312. Add('rmdir ' + MaybeQuoted (ScriptFixFileName(FileName)));
  313. end;
  314. Procedure TAsmScriptUnix.WriteToDisk;
  315. Begin
  316. AddStart('{ echo "An error occurred while linking $1"; exit 1; }');
  317. AddStart('DoExitLink ()');
  318. AddStart('{ echo "An error occurred while assembling $1"; exit 1; }');
  319. AddStart('DoExitAsm ()');
  320. {$ifdef BEOS}
  321. AddStart('#!/boot/beos/bin/sh');
  322. {$else}
  323. AddStart('#!/bin/sh');
  324. {$endif}
  325. inherited WriteToDisk;
  326. end;
  327. {****************************************************************************
  328. MPW (MacOS) Asm Response
  329. ****************************************************************************}
  330. Constructor TAsmScriptMPW.Create (Const ScriptName : String);
  331. begin
  332. Inherited Create(ScriptName);
  333. end;
  334. Procedure TAsmScriptMPW.AddAsmCommand (Const Command, Options,FileName : String);
  335. begin
  336. if FileName<>'' then
  337. Add('Echo Assembling '+ScriptFixFileName(FileName));
  338. Add(maybequoted(command)+' '+Options);
  339. Add('Exit If "{Status}" != 0');
  340. end;
  341. Procedure TAsmScriptMPW.AddLinkCommand (Const Command, Options, FileName : String);
  342. begin
  343. if FileName<>'' then
  344. Add('Echo Linking '+ScriptFixFileName(FileName));
  345. Add(maybequoted(command)+' '+Options);
  346. Add('Exit If "{Status}" != 0');
  347. {Add resources}
  348. if apptype = app_cui then {If SIOW}
  349. begin
  350. Add('Rez -append "{RIncludes}"SIOW.r -o '+ ScriptFixFileName(FileName));
  351. Add('Exit If "{Status}" != 0');
  352. end;
  353. end;
  354. Procedure TAsmScriptMPW.AddDeleteCommand (Const FileName : String);
  355. begin
  356. Add('Delete ' + MaybeQuoted (ScriptFixFileName(FileName)));
  357. end;
  358. Procedure TAsmScriptMPW.AddDeleteDirCommand (Const FileName : String);
  359. begin
  360. Add('Delete ' + MaybeQuoted (ScriptFixFileName (FileName)));
  361. end;
  362. Procedure TAsmScriptMPW.WriteToDisk;
  363. Begin
  364. AddStart('# Script for assembling and linking a FreePascal program on MPW (MacOS)');
  365. Add('Echo Done');
  366. inherited WriteToDisk;
  367. end;
  368. Procedure GenerateAsmRes(const st : string);
  369. var
  370. scripttyp : tscripttype;
  371. begin
  372. if cs_link_on_target in current_settings.globalswitches then
  373. scripttyp := target_info.script
  374. else
  375. scripttyp := source_info.script;
  376. case scripttyp of
  377. script_unix :
  378. AsmRes:=TAsmScriptUnix.Create(st);
  379. script_dos :
  380. AsmRes:=TAsmScriptDos.Create(st);
  381. script_amiga :
  382. AsmRes:=TAsmScriptAmiga.Create(st);
  383. script_mpw :
  384. AsmRes:=TAsmScriptMPW.Create(st);
  385. end;
  386. end;
  387. {****************************************************************************
  388. Link Response
  389. ****************************************************************************}
  390. procedure TLinkRes.Add(const s:string);
  391. begin
  392. if s<>'' then
  393. inherited Add(s);
  394. end;
  395. procedure TLinkRes.AddFileName(const s:string);
  396. begin
  397. if s<>'' then
  398. begin
  399. if not(s[1] in ['a'..'z','A'..'Z','/','\','.','"']) then
  400. begin
  401. if cs_link_on_target in current_settings.globalswitches then
  402. inherited Add('.'+target_info.DirSep+s)
  403. else
  404. inherited Add('.'+source_info.DirSep+s);
  405. end
  406. else
  407. inherited Add(s);
  408. end;
  409. end;
  410. end.