cscript.pas 15 KB

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