cscript.pas 15 KB

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