script.pas 14 KB

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