2
0

script.pas 14 KB

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