script.pas 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Peter Vreman
  4. This unit handles the writing of script files
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit script;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. cclasses;
  23. type
  24. TScript=class
  25. fn : string[80];
  26. data : TStringList;
  27. executable : boolean;
  28. constructor Create(const s:string);
  29. constructor CreateExec(const s:string);
  30. destructor Destroy;override;
  31. procedure AddStart(const s:string);
  32. procedure Add(const s:string);
  33. Function Empty:boolean;
  34. procedure WriteToDisk;virtual;
  35. end;
  36. TAsmScript = class (TScript)
  37. Constructor Create(Const ScriptName : String); virtual;
  38. Procedure AddAsmCommand (Const Command, Options,FileName : String);virtual;abstract;
  39. Procedure AddLinkCommand (Const Command, Options, FileName : String);virtual;abstract;
  40. Procedure AddDeleteCommand (Const FileName : String);virtual;abstract;
  41. Procedure AddDeleteDirCommand (Const FileName : String);virtual;abstract;
  42. end;
  43. TAsmScriptDos = class (TAsmScript)
  44. Constructor Create (Const ScriptName : String); override;
  45. Procedure AddAsmCommand (Const Command, Options,FileName : String);override;
  46. Procedure AddLinkCommand (Const Command, Options, FileName : String);override;
  47. Procedure AddDeleteCommand (Const FileName : String);override;
  48. Procedure AddDeleteDirCommand (Const FileName : String);override;
  49. Procedure WriteToDisk;override;
  50. end;
  51. TAsmScriptAmiga = class (TAsmScript)
  52. Constructor Create (Const ScriptName : String); override;
  53. Procedure AddAsmCommand (Const Command, Options,FileName : String);override;
  54. Procedure AddLinkCommand (Const Command, Options, FileName : String);override;
  55. Procedure AddDeleteCommand (Const FileName : String);override;
  56. Procedure AddDeleteDirCommand (Const FileName : String);override;
  57. Procedure WriteToDisk;override;
  58. end;
  59. TAsmScriptUnix = class (TAsmScript)
  60. Constructor Create (Const ScriptName : String);override;
  61. Procedure AddAsmCommand (Const Command, Options,FileName : String);override;
  62. Procedure AddLinkCommand (Const Command, Options, FileName : String);override;
  63. Procedure AddDeleteCommand (Const FileName : String);override;
  64. Procedure AddDeleteDirCommand (Const FileName : String);override;
  65. Procedure WriteToDisk;override;
  66. end;
  67. TAsmScriptMPW = class (TAsmScript)
  68. Constructor Create (Const ScriptName : String); override;
  69. Procedure AddAsmCommand (Const Command, Options,FileName : String);override;
  70. Procedure AddLinkCommand (Const Command, Options, FileName : String);override;
  71. Procedure AddDeleteCommand (Const FileName : String);override;
  72. Procedure AddDeleteDirCommand (Const FileName : String);override;
  73. Procedure WriteToDisk;override;
  74. end;
  75. TLinkRes = Class (TScript)
  76. procedure Add(const s:string);
  77. procedure AddFileName(const s:string);
  78. end;
  79. var
  80. AsmRes : TAsmScript;
  81. Function ScriptFixFileName(const s:string):string;
  82. Procedure GenerateAsmRes(const st : string);
  83. implementation
  84. uses
  85. {$ifdef hasUnix}
  86. {$ifdef havelinuxrtl10}
  87. Linux,
  88. {$else}
  89. BaseUnix,
  90. {$endif}
  91. {$endif}
  92. cutils,
  93. globtype,globals,systems,verbose;
  94. {****************************************************************************
  95. Helpers
  96. ****************************************************************************}
  97. Function ScriptFixFileName(const s:string):string;
  98. begin
  99. if cs_link_on_target in aktglobalswitches then
  100. ScriptFixFileName:=TargetFixFileName(s)
  101. else
  102. ScriptFixFileName:=FixFileName(s);
  103. end;
  104. {****************************************************************************
  105. TScript
  106. ****************************************************************************}
  107. constructor TScript.Create(const s:string);
  108. begin
  109. fn:=FixFileName(s);
  110. executable:=false;
  111. data:=TStringList.Create;
  112. end;
  113. constructor TScript.CreateExec(const s:string);
  114. begin
  115. fn:=FixFileName(s);
  116. if cs_link_on_target in aktglobalswitches then
  117. fn:=AddExtension(fn,target_info.scriptext)
  118. else
  119. fn:=AddExtension(fn,source_info.scriptext);
  120. executable:=true;
  121. data:=TStringList.Create;
  122. end;
  123. destructor TScript.Destroy;
  124. begin
  125. data.Free;
  126. end;
  127. procedure TScript.AddStart(const s:string);
  128. begin
  129. data.Insert(s);
  130. end;
  131. procedure TScript.Add(const s:string);
  132. begin
  133. data.Concat(s);
  134. end;
  135. Function TScript.Empty:boolean;
  136. begin
  137. Empty:=Data.Empty;
  138. end;
  139. (*
  140. procedure TScript.WriteToDisk;
  141. var
  142. t : file;
  143. s : string;
  144. le: string[2];
  145. begin
  146. if cs_link_on_target in aktglobalswitches then
  147. le:= target_info.newline
  148. else
  149. le:= source_info.newline;
  150. Assign(t,fn);
  151. Rewrite(t,1);
  152. while not data.Empty do
  153. begin
  154. s:= data.GetFirst;
  155. BlockWrite(t, s[1] ,Length(s));
  156. BlockWrite(t, le[1], Length(le));
  157. end;
  158. Close(t);
  159. {$ifdef hasUnix}
  160. if executable then
  161. {$ifdef VER1_0}ChMod{$else}fpchmod{$endif}(fn,493);
  162. {$endif}
  163. end;
  164. *)
  165. procedure TScript.WriteToDisk;
  166. var
  167. t : file;
  168. i : longint;
  169. s : string;
  170. begin
  171. Assign(t,fn);
  172. {$I-}
  173. Rewrite(t,1);
  174. if ioresult<>0 then
  175. exit;
  176. while not data.Empty do
  177. begin
  178. s:=data.GetFirst;
  179. if (cs_link_on_target in aktglobalswitches) then
  180. s:=s+target_info.newline
  181. else
  182. s:=s+source_info.newline;
  183. Blockwrite(t,s[1],length(s),i);
  184. end;
  185. Close(t);
  186. {$I+}
  187. i:=ioresult;
  188. {$ifdef hasUnix}
  189. if executable then
  190. {$ifdef havelinuxrtl10}ChMod{$else}fpchmod{$endif}(fn,493);
  191. {$endif}
  192. end;
  193. {****************************************************************************
  194. Asm Response
  195. ****************************************************************************}
  196. Constructor TAsmScript.Create (Const ScriptName : String);
  197. begin
  198. Inherited CreateExec(ScriptName);
  199. end;
  200. {****************************************************************************
  201. DOS Asm Response
  202. ****************************************************************************}
  203. Constructor TAsmScriptDos.Create (Const ScriptName : String);
  204. begin
  205. Inherited Create(ScriptName);
  206. end;
  207. Procedure TAsmScriptDos.AddAsmCommand (Const Command, Options,FileName : String);
  208. begin
  209. if FileName<>'' then
  210. begin
  211. Add('SET THEFILE='+ScriptFixFileName(FileName));
  212. Add('echo Assembling %THEFILE%');
  213. end;
  214. Add(maybequoted(command)+' '+Options);
  215. Add('if errorlevel 1 goto asmend');
  216. end;
  217. Procedure TAsmScriptDos.AddLinkCommand (Const Command, Options, FileName : String);
  218. begin
  219. if FileName<>'' then
  220. begin
  221. Add('SET THEFILE='+ScriptFixFileName(FileName));
  222. Add('echo Linking %THEFILE%');
  223. end;
  224. Add(maybequoted(command)+' '+Options);
  225. Add('if errorlevel 1 goto linkend');
  226. end;
  227. Procedure TAsmScriptDos.AddDeleteCommand (Const FileName : String);
  228. begin
  229. Add('Del '+ScriptFixFileName(FileName));
  230. end;
  231. Procedure TAsmScriptDos.AddDeleteDirCommand (Const FileName : String);
  232. begin
  233. Add('Rmdir '+FileName);
  234. end;
  235. Procedure TAsmScriptDos.WriteToDisk;
  236. Begin
  237. AddStart('@echo off');
  238. Add('goto end');
  239. Add(':asmend');
  240. Add('echo An error occured while assembling %THEFILE%');
  241. Add('goto end');
  242. Add(':linkend');
  243. Add('echo An error occured while linking %THEFILE%');
  244. Add(':end');
  245. inherited WriteToDisk;
  246. end;
  247. {****************************************************************************
  248. Amiga Asm Response
  249. ****************************************************************************}
  250. Constructor TAsmScriptAmiga.Create (Const ScriptName : String);
  251. begin
  252. Inherited Create(ScriptName);
  253. end;
  254. Procedure TAsmScriptAmiga.AddAsmCommand (Const Command, Options,FileName : String);
  255. begin
  256. if FileName<>'' then
  257. begin
  258. Add('SET THEFILE '+ScriptFixFileName(FileName));
  259. Add('echo Assembling $THEFILE');
  260. end;
  261. Add(maybequoted(command)+' '+Options);
  262. { There is a problem here,
  263. as allways return with a non zero error value PM }
  264. Add('if error');
  265. Add('why');
  266. Add('skip asmend');
  267. Add('endif');
  268. end;
  269. Procedure TAsmScriptAmiga.AddLinkCommand (Const Command, Options, FileName : String);
  270. begin
  271. if FileName<>'' then
  272. begin
  273. Add('SET THEFILE '+ScriptFixFileName(FileName));
  274. Add('echo Linking $THEFILE');
  275. end;
  276. Add(maybequoted(command)+' '+Options);
  277. Add('if error');
  278. Add('skip linkend');
  279. Add('endif');
  280. end;
  281. Procedure TAsmScriptAmiga.AddDeleteCommand (Const FileName : String);
  282. begin
  283. Add('Delete '+ScriptFixFileName(FileName));
  284. end;
  285. Procedure TAsmScriptAmiga.AddDeleteDirCommand (Const FileName : String);
  286. begin
  287. Add('Delete '+ScriptFixFileName(FileName));
  288. end;
  289. Procedure TAsmScriptAmiga.WriteToDisk;
  290. Begin
  291. Add('skip end');
  292. Add('lab asmend');
  293. Add('why');
  294. Add('echo An error occured while assembling $THEFILE');
  295. Add('skip end');
  296. Add('lab linkend');
  297. Add('why');
  298. Add('echo An error occured while linking $THEFILE');
  299. Add('lab end');
  300. inherited WriteToDisk;
  301. end;
  302. {****************************************************************************
  303. Unix Asm Response
  304. ****************************************************************************}
  305. Constructor TAsmScriptUnix.Create (Const ScriptName : String);
  306. begin
  307. Inherited Create(ScriptName);
  308. end;
  309. Procedure TAsmScriptUnix.AddAsmCommand (Const Command, Options,FileName : String);
  310. begin
  311. if FileName<>'' then
  312. Add('echo Assembling '+ScriptFixFileName(FileName));
  313. Add(maybequoted(command)+' '+Options);
  314. Add('if [ $? != 0 ]; then DoExitAsm '+ScriptFixFileName(FileName)+'; fi');
  315. end;
  316. Procedure TAsmScriptUnix.AddLinkCommand (Const Command, Options, FileName : String);
  317. begin
  318. if FileName<>'' then
  319. Add('echo Linking '+ScriptFixFileName(FileName));
  320. Add(maybequoted(command)+' '+Options);
  321. Add('if [ $? != 0 ]; then DoExitLink '+ScriptFixFileName(FileName)+'; fi');
  322. end;
  323. Procedure TAsmScriptUnix.AddDeleteCommand (Const FileName : String);
  324. begin
  325. Add('rm '+ScriptFixFileName(FileName));
  326. end;
  327. Procedure TAsmScriptUnix.AddDeleteDirCommand (Const FileName : String);
  328. begin
  329. Add('rmdir '+ScriptFixFileName(FileName));
  330. end;
  331. Procedure TAsmScriptUnix.WriteToDisk;
  332. Begin
  333. AddStart('{ echo "An error occurred while linking $1"; exit 1; }');
  334. AddStart('DoExitLink ()');
  335. AddStart('{ echo "An error occurred while assembling $1"; exit 1; }');
  336. AddStart('DoExitAsm ()');
  337. {$ifdef BEOS}
  338. AddStart('#!/boot/beos/bin/sh');
  339. {$else}
  340. AddStart('#!/bin/sh');
  341. {$endif}
  342. inherited WriteToDisk;
  343. end;
  344. {****************************************************************************
  345. MPW (MacOS) Asm Response
  346. ****************************************************************************}
  347. Constructor TAsmScriptMPW.Create (Const ScriptName : String);
  348. begin
  349. Inherited Create(ScriptName);
  350. end;
  351. Procedure TAsmScriptMPW.AddAsmCommand (Const Command, Options,FileName : String);
  352. begin
  353. if FileName<>'' then
  354. Add('Echo Assembling '+ScriptFixFileName(FileName));
  355. Add(maybequoted(command)+' '+Options);
  356. end;
  357. Procedure TAsmScriptMPW.AddLinkCommand (Const Command, Options, FileName : String);
  358. begin
  359. if FileName<>'' then
  360. Add('Echo Linking '+ScriptFixFileName(FileName));
  361. Add(maybequoted(command)+' '+Options);
  362. {Add resources}
  363. if true then {If SIOW}
  364. Add('Rez -append "{RIncludes}"SIOW.r -o '+ ScriptFixFileName(FileName));
  365. end;
  366. Procedure TAsmScriptMPW.AddDeleteCommand (Const FileName : String);
  367. begin
  368. Add('Delete '+ScriptFixFileName(FileName));
  369. end;
  370. Procedure TAsmScriptMPW.AddDeleteDirCommand (Const FileName : String);
  371. begin
  372. Add('Delete '+FileName);
  373. end;
  374. Procedure TAsmScriptMPW.WriteToDisk;
  375. Begin
  376. AddStart('# Script for assembling and linking a FreePascal program on MPW (MacOS)');
  377. Add('Echo Done');
  378. inherited WriteToDisk;
  379. end;
  380. Procedure GenerateAsmRes(const st : string);
  381. var
  382. scripttyp : tscripttype;
  383. begin
  384. if cs_link_on_target in aktglobalswitches then
  385. scripttyp := target_info.script
  386. else
  387. scripttyp := source_info.script;
  388. case scripttyp of
  389. script_unix :
  390. AsmRes:=TAsmScriptUnix.Create(st);
  391. script_dos :
  392. AsmRes:=TAsmScriptDos.Create(st);
  393. script_amiga :
  394. AsmRes:=TAsmScriptAmiga.Create(st);
  395. script_mpw :
  396. AsmRes:=TAsmScriptMPW.Create(st);
  397. end;
  398. end;
  399. {****************************************************************************
  400. Link Response
  401. ****************************************************************************}
  402. procedure TLinkRes.Add(const s:string);
  403. begin
  404. if s<>'' then
  405. inherited Add(s);
  406. end;
  407. procedure TLinkRes.AddFileName(const s:string);
  408. begin
  409. if s<>'' then
  410. begin
  411. if not(s[1] in ['a'..'z','A'..'Z','/','\','.','"']) then
  412. begin
  413. if cs_link_on_target in aktglobalswitches then
  414. inherited Add('.'+target_info.DirSep+s)
  415. else
  416. inherited Add('.'+source_info.DirSep+s);
  417. end
  418. else
  419. inherited Add(s);
  420. end;
  421. end;
  422. end.
  423. {
  424. $Log$
  425. Revision 1.26 2004-02-19 20:40:15 olle
  426. + Support for Link on target especially for MacOS
  427. + TLinkerMPW
  428. + TAsmScriptMPW
  429. Revision 1.25 2003/11/10 17:22:28 marco
  430. * havelinuxrtl10 fixes
  431. Revision 1.24 2003/09/30 19:54:23 peter
  432. * better link on target support
  433. Revision 1.23 2003/09/16 13:42:39 marco
  434. * Had a useless dependancy on unit unix in 1_1 mode
  435. Revision 1.22 2003/09/14 20:26:18 marco
  436. * Unix reform
  437. Revision 1.21 2003/04/22 14:33:38 peter
  438. * removed some notes/hints
  439. Revision 1.20 2003/02/07 21:21:39 marco
  440. * Some small fix
  441. Revision 1.19 2003/01/10 21:49:00 marco
  442. * more hasunix fixes
  443. Revision 1.18 2003/01/06 20:16:42 peter
  444. * don't prepend ./ to quoted filenames
  445. Revision 1.17 2002/11/15 01:58:54 peter
  446. * merged changes from 1.0.7 up to 04-11
  447. - -V option for generating bug report tracing
  448. - more tracing for option parsing
  449. - errors for cdecl and high()
  450. - win32 import stabs
  451. - win32 records<=8 are returned in eax:edx (turned off by default)
  452. - heaptrc update
  453. - more info for temp management in .s file with EXTDEBUG
  454. Revision 1.16 2002/05/18 13:34:18 peter
  455. * readded missing revisions
  456. Revision 1.15 2002/05/16 19:46:44 carl
  457. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  458. + try to fix temp allocation (still in ifdef)
  459. + generic constructor calls
  460. + start of tassembler / tmodulebase class cleanup
  461. }