script.pas 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  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. section: string[30];
  76. procedure Add(const s:string);
  77. procedure AddFileName(const s:string);
  78. procedure EndSection(const s:string);
  79. procedure StartSection(const s:string);
  80. end;
  81. var
  82. AsmRes : TAsmScript;
  83. Function ScriptFixFileName(const s:string):string;
  84. Procedure GenerateAsmRes(const st : string);
  85. implementation
  86. uses
  87. {$ifdef hasUnix}
  88. BaseUnix,
  89. {$endif}
  90. SysUtils,
  91. cutils,cfileutils,
  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 current_settings.globalswitches 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 current_settings.globalswitches then
  116. fn:=ChangeFileExt(fn,target_info.scriptext)
  117. else
  118. fn:=ChangeFileExt(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 current_settings.globalswitches 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. fpchmod(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. { * PathConv is required, since Amiga commands can't handle Unix-style
  226. relative paths used by the compiler (KB) * }
  227. {$IF DEFINED(MORPHOS) OR DEFINED(AMIGA)}
  228. { * PathConv is implemented in the system unit! * }
  229. function PathConv(path: string): string; external name 'PATHCONV';
  230. {$ELSE}
  231. function PathConv(path: string): string;
  232. begin
  233. PathConv:=path;
  234. end;
  235. {$ENDIF}
  236. Constructor TAsmScriptAmiga.Create (Const ScriptName : String);
  237. begin
  238. Inherited Create(ScriptName);
  239. end;
  240. Procedure TAsmScriptAmiga.AddAsmCommand (Const Command, Options,FileName : String);
  241. begin
  242. if FileName<>'' then
  243. begin
  244. Add('SET THEFILE '+ScriptFixFileName(FileName));
  245. Add('echo Assembling $THEFILE');
  246. end;
  247. Add(maybequoted(command)+' '+Options);
  248. { There is a problem here,
  249. as always return with a non zero error value PM }
  250. Add('if error');
  251. Add('why');
  252. Add('skip asmend');
  253. Add('endif');
  254. end;
  255. Procedure TAsmScriptAmiga.AddLinkCommand (Const Command, Options, FileName : String);
  256. begin
  257. if FileName<>'' then
  258. begin
  259. Add('SET THEFILE '+ScriptFixFileName(FileName));
  260. Add('echo Linking $THEFILE');
  261. end;
  262. Add(maybequoted(command)+' '+Options);
  263. Add('if error');
  264. Add('skip linkend');
  265. Add('endif');
  266. end;
  267. Procedure TAsmScriptAmiga.AddDeleteCommand (Const FileName : String);
  268. begin
  269. Add('Delete ' + PathConv(MaybeQuoted(ScriptFixFileName(FileName))) + ' Quiet');
  270. end;
  271. Procedure TAsmScriptAmiga.AddDeleteDirCommand (Const FileName : String);
  272. begin
  273. Add('Delete ' + PathConv(MaybeQuoted(ScriptFixFileName(FileName))) + ' All Quiet');
  274. end;
  275. Procedure TAsmScriptAmiga.WriteToDisk;
  276. Begin
  277. Add('skip end');
  278. Add('lab asmend');
  279. Add('why');
  280. Add('echo An error occured while assembling $THEFILE');
  281. Add('skip end');
  282. Add('lab linkend');
  283. Add('why');
  284. Add('echo An error occured while linking $THEFILE');
  285. Add('lab end');
  286. inherited WriteToDisk;
  287. end;
  288. {****************************************************************************
  289. Unix Asm Response
  290. ****************************************************************************}
  291. Constructor TAsmScriptUnix.Create (Const ScriptName : String);
  292. begin
  293. Inherited Create(ScriptName);
  294. end;
  295. Procedure TAsmScriptUnix.AddAsmCommand (Const Command, Options,FileName : String);
  296. begin
  297. if FileName<>'' then
  298. Add('echo Assembling '+ScriptFixFileName(FileName));
  299. Add(maybequoted(command)+' '+Options);
  300. Add('if [ $? != 0 ]; then DoExitAsm '+ScriptFixFileName(FileName)+'; fi');
  301. end;
  302. Procedure TAsmScriptUnix.AddLinkCommand (Const Command, Options, FileName : String);
  303. begin
  304. if FileName<>'' then
  305. Add('echo Linking '+ScriptFixFileName(FileName));
  306. Add(maybequoted(command)+' '+Options);
  307. Add('if [ $? != 0 ]; then DoExitLink '+ScriptFixFileName(FileName)+'; fi');
  308. end;
  309. Procedure TAsmScriptUnix.AddDeleteCommand (Const FileName : String);
  310. begin
  311. Add('rm ' + MaybeQuoted (ScriptFixFileName(FileName)));
  312. end;
  313. Procedure TAsmScriptUnix.AddDeleteDirCommand (Const FileName : String);
  314. begin
  315. Add('rmdir ' + MaybeQuoted (ScriptFixFileName(FileName)));
  316. end;
  317. Procedure TAsmScriptUnix.WriteToDisk;
  318. Begin
  319. AddStart('{ echo "An error occurred while linking $1"; exit 1; }');
  320. AddStart('DoExitLink ()');
  321. AddStart('{ echo "An error occurred while assembling $1"; exit 1; }');
  322. AddStart('DoExitAsm ()');
  323. {$ifdef BEOS}
  324. AddStart('#!/boot/beos/bin/sh');
  325. {$else}
  326. AddStart('#!/bin/sh');
  327. {$endif}
  328. inherited WriteToDisk;
  329. end;
  330. {****************************************************************************
  331. MPW (MacOS) Asm Response
  332. ****************************************************************************}
  333. Constructor TAsmScriptMPW.Create (Const ScriptName : String);
  334. begin
  335. Inherited Create(ScriptName);
  336. end;
  337. Procedure TAsmScriptMPW.AddAsmCommand (Const Command, Options,FileName : String);
  338. begin
  339. if FileName<>'' then
  340. Add('Echo Assembling '+ScriptFixFileName(FileName));
  341. Add(maybequoted(command)+' '+Options);
  342. Add('Exit If "{Status}" != 0');
  343. end;
  344. Procedure TAsmScriptMPW.AddLinkCommand (Const Command, Options, FileName : String);
  345. begin
  346. if FileName<>'' then
  347. Add('Echo Linking '+ScriptFixFileName(FileName));
  348. Add(maybequoted(command)+' '+Options);
  349. Add('Exit If "{Status}" != 0');
  350. {Add resources}
  351. if apptype = app_cui then {If SIOW}
  352. begin
  353. Add('Rez -append "{RIncludes}"SIOW.r -o '+ ScriptFixFileName(FileName));
  354. Add('Exit If "{Status}" != 0');
  355. end;
  356. end;
  357. Procedure TAsmScriptMPW.AddDeleteCommand (Const FileName : String);
  358. begin
  359. Add('Delete ' + MaybeQuoted (ScriptFixFileName(FileName)));
  360. end;
  361. Procedure TAsmScriptMPW.AddDeleteDirCommand (Const FileName : String);
  362. begin
  363. Add('Delete ' + MaybeQuoted (ScriptFixFileName (FileName)));
  364. end;
  365. Procedure TAsmScriptMPW.WriteToDisk;
  366. Begin
  367. AddStart('# Script for assembling and linking a FreePascal program on MPW (MacOS)');
  368. Add('Echo Done');
  369. inherited WriteToDisk;
  370. end;
  371. Procedure GenerateAsmRes(const st : string);
  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. AsmRes:=TAsmScriptUnix.Create(st);
  382. script_dos :
  383. AsmRes:=TAsmScriptDos.Create(st);
  384. script_amiga :
  385. AsmRes:=TAsmScriptAmiga.Create(st);
  386. script_mpw :
  387. AsmRes:=TAsmScriptMPW.Create(st);
  388. end;
  389. end;
  390. {****************************************************************************
  391. Link Response
  392. ****************************************************************************}
  393. procedure TLinkRes.Add(const s:string);
  394. begin
  395. if s<>'' then
  396. inherited Add(s);
  397. end;
  398. procedure TLinkRes.AddFileName(const s:string);
  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:string);
  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:string);
  426. begin
  427. section:=s;
  428. end;
  429. end.