cscript.pas 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  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. data := nil;
  132. end;
  133. procedure TScript.AddStart(const s:TCmdStr);
  134. begin
  135. data.Insert(s);
  136. end;
  137. procedure TScript.Add(const s:TCmdStr);
  138. begin
  139. data.Concat(s);
  140. end;
  141. Function TScript.Empty:boolean;
  142. begin
  143. Empty:=Data.Empty;
  144. end;
  145. procedure TScript.WriteToDisk;
  146. var
  147. t : file;
  148. i : longint;
  149. s : TCmdStr;
  150. le: string[2];
  151. begin
  152. Assign(t,fn);
  153. if cs_link_on_target in current_settings.globalswitches then
  154. le:= target_info.newline
  155. else
  156. le:= source_info.newline;
  157. {$push}{$I-}
  158. Rewrite(t,1);
  159. if ioresult<>0 then
  160. exit;
  161. while not data.Empty do
  162. begin
  163. s:=data.GetFirst;
  164. Blockwrite(t,s[1],length(s),i);
  165. Blockwrite(t,le[1],length(le),i);
  166. end;
  167. Close(t);
  168. {$pop}
  169. i:=ioresult;
  170. {$ifdef hasUnix}
  171. if executable then
  172. fpchmod(fn,493);
  173. {$endif}
  174. end;
  175. {****************************************************************************
  176. Asm Response
  177. ****************************************************************************}
  178. Constructor TAsmScript.Create (Const ScriptName : TCmdStr);
  179. begin
  180. Inherited CreateExec(ScriptName);
  181. end;
  182. {****************************************************************************
  183. DOS Asm Response
  184. ****************************************************************************}
  185. Constructor TAsmScriptDos.Create (Const ScriptName : TCmdStr);
  186. begin
  187. Inherited Create(ScriptName);
  188. end;
  189. Procedure TAsmScriptDos.AddAsmCommand (Const Command, Options,FileName : TCmdStr);
  190. begin
  191. if FileName<>'' then
  192. begin
  193. Add('SET THEFILE='+ScriptFixFileName(FileName));
  194. Add('echo Assembling %THEFILE%');
  195. end;
  196. Add(maybequoted(command)+' '+Options);
  197. Add('if errorlevel 1 goto asmend');
  198. end;
  199. Procedure TAsmScriptDos.AddLinkCommand (Const Command, Options, FileName : TCmdStr);
  200. begin
  201. if FileName<>'' then
  202. begin
  203. Add('SET THEFILE='+ScriptFixFileName(FileName));
  204. Add('echo Linking %THEFILE%');
  205. end;
  206. Add(maybequoted(command)+' '+Options);
  207. Add('if errorlevel 1 goto linkend');
  208. end;
  209. Procedure TAsmScriptDos.AddDeleteCommand (Const FileName : TCmdStr);
  210. begin
  211. Add('Del ' + MaybeQuoted (ScriptFixFileName (FileName)));
  212. end;
  213. Procedure TAsmScriptDos.AddDeleteDirCommand (Const FileName : TCmdStr);
  214. begin
  215. Add('Rmdir ' + MaybeQuoted (ScriptFixFileName (FileName)));
  216. end;
  217. Procedure TAsmScriptDos.WriteToDisk;
  218. Begin
  219. AddStart('@echo off');
  220. Add('goto end');
  221. Add(':asmend');
  222. Add('echo An error occurred while assembling %THEFILE%');
  223. Add('goto end');
  224. Add(':linkend');
  225. Add('echo An error occurred while linking %THEFILE%');
  226. Add(':end');
  227. inherited WriteToDisk;
  228. end;
  229. {****************************************************************************
  230. Amiga Asm Response
  231. ****************************************************************************}
  232. Constructor TAsmScriptAmiga.Create (Const ScriptName : TCmdStr);
  233. begin
  234. Inherited Create(ScriptName);
  235. end;
  236. Procedure TAsmScriptAmiga.AddAsmCommand (Const Command, Options,FileName : TCmdStr);
  237. begin
  238. if FileName<>'' then
  239. begin
  240. Add('SET THEFILE '+ScriptFixFileName(FileName));
  241. Add('echo Assembling $THEFILE');
  242. end;
  243. Add(maybequoted(command)+' '+Options);
  244. { There is a problem here,
  245. as always return with a non zero error value PM }
  246. Add('if error');
  247. Add('why');
  248. Add('skip asmend');
  249. Add('endif');
  250. end;
  251. Procedure TAsmScriptAmiga.AddLinkCommand (Const Command, Options, FileName : TCmdStr);
  252. begin
  253. if FileName<>'' then
  254. begin
  255. Add('SET THEFILE '+ScriptFixFileName(FileName));
  256. Add('echo Linking $THEFILE');
  257. end;
  258. Add(maybequoted(command)+' '+Options);
  259. Add('if error');
  260. Add('skip linkend');
  261. Add('endif');
  262. end;
  263. Procedure TAsmScriptAmiga.AddDeleteCommand (Const FileName : TCmdStr);
  264. begin
  265. Add('Delete ' + Unix2AmigaPath(MaybeQuoted(ScriptFixFileName(FileName))) + ' Quiet');
  266. end;
  267. Procedure TAsmScriptAmiga.AddDeleteDirCommand (Const FileName : TCmdStr);
  268. begin
  269. Add('Delete ' + Unix2AmigaPath(MaybeQuoted(ScriptFixFileName(FileName))) + ' All Quiet');
  270. end;
  271. Procedure TAsmScriptAmiga.WriteToDisk;
  272. Begin
  273. Add('skip end');
  274. Add('lab asmend');
  275. Add('why');
  276. Add('echo An error occurred while assembling $THEFILE');
  277. Add('skip end');
  278. Add('lab linkend');
  279. Add('why');
  280. Add('echo An error occurred while linking $THEFILE');
  281. Add('lab end');
  282. inherited WriteToDisk;
  283. end;
  284. {****************************************************************************
  285. Unix Asm Response
  286. ****************************************************************************}
  287. Constructor TAsmScriptUnix.Create (Const ScriptName : TCmdStr);
  288. begin
  289. Inherited Create(ScriptName);
  290. end;
  291. Procedure TAsmScriptUnix.AddAsmCommand (Const Command, Options,FileName : TCmdStr);
  292. begin
  293. if FileName<>'' then
  294. Add('echo Assembling '+maybequoted(ScriptFixFileName(FileName)));
  295. Add(maybequoted(command)+' '+Options);
  296. Add('if [ $? != 0 ]; then DoExitAsm '+maybequoted(ScriptFixFileName(FileName))+'; fi');
  297. end;
  298. Procedure TAsmScriptUnix.AddLinkCommand (Const Command, Options, FileName : TCmdStr);
  299. begin
  300. if FileName<>'' then
  301. Add('echo Linking '+ScriptFixFileName(FileName));
  302. Add('OFS=$IFS');
  303. Add('IFS="');
  304. Add('"');
  305. Add(maybequoted(command)+' '+Options);
  306. Add('if [ $? != 0 ]; then DoExitLink '+ScriptFixFileName(FileName)+'; fi');
  307. Add('IFS=$OFS');
  308. end;
  309. Procedure TAsmScriptUnix.AddDeleteCommand (Const FileName : TCmdStr);
  310. begin
  311. Add('rm ' + MaybeQuoted (ScriptFixFileName(FileName)));
  312. end;
  313. Procedure TAsmScriptUnix.AddDeleteDirCommand (Const FileName : TCmdStr);
  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 : TCmdStr);
  334. begin
  335. Inherited Create(ScriptName);
  336. end;
  337. Procedure TAsmScriptMPW.AddAsmCommand (Const Command, Options,FileName : TCmdStr);
  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 : TCmdStr);
  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 : TCmdStr);
  358. begin
  359. Add('Delete ' + MaybeQuoted (ScriptFixFileName(FileName)));
  360. end;
  361. Procedure TAsmScriptMPW.AddDeleteDirCommand (Const FileName : TCmdStr);
  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 : TCmdStr);
  372. begin
  373. AsmRes:=GenerateScript(st);
  374. end;
  375. function GenerateScript(const st: TCmdStr): TAsmScript;
  376. var
  377. scripttyp : tscripttype;
  378. begin
  379. if cs_link_on_target in current_settings.globalswitches then
  380. scripttyp := target_info.script
  381. else
  382. scripttyp := source_info.script;
  383. case scripttyp of
  384. script_unix :
  385. Result:=TAsmScriptUnix.Create(st);
  386. script_dos :
  387. Result:=TAsmScriptDos.Create(st);
  388. script_amiga :
  389. Result:=TAsmScriptAmiga.Create(st);
  390. script_mpw :
  391. Result:=TAsmScriptMPW.Create(st);
  392. else
  393. internalerror(2013112805);
  394. end;
  395. end;
  396. {****************************************************************************
  397. Link Response
  398. ****************************************************************************}
  399. constructor TLinkRes.Create(const ScriptName: TCmdStr; RealResponseFile: Boolean);
  400. begin
  401. inherited Create(ScriptName);
  402. fRealResponseFile:=RealResponseFile;
  403. fForceUseForwardSlash:=false;
  404. end;
  405. procedure TLinkRes.Add(const s:TCmdStr);
  406. begin
  407. if s<>'' then
  408. inherited Add(s);
  409. end;
  410. procedure TLinkRes.AddFileName(const s:TCmdStr);
  411. var
  412. ls: TCmdStr;
  413. i: longint;
  414. begin
  415. if section<>'' then
  416. begin
  417. inherited Add(section);
  418. section:='';
  419. end;
  420. if s<>'' then
  421. begin
  422. ls:=s;
  423. if fForceUseForwardSlash then
  424. { Fix separator }
  425. for i:=1 to length(ls) do
  426. if (ls[i]=source_info.dirsep) then
  427. ls[i]:='/';
  428. { GNU ld only supports double quotes in the response file. }
  429. if fRealResponseFile and
  430. (ls[1]='''') and
  431. (((cs_link_on_target in current_settings.globalswitches) and
  432. (target_info.script=script_unix)) or
  433. (not(cs_link_on_target in current_settings.globalswitches) and
  434. (source_info.script=script_unix))) then
  435. inherited add(UnixRequoteWithDoubleQuotes(s))
  436. else if not(ls[1] in ['a'..'z','A'..'Z','/','\','.','"']) then
  437. begin
  438. if fForceUseForwardSlash then
  439. inherited Add('./'+ls)
  440. else if (cs_link_on_target in current_settings.globalswitches) then
  441. inherited Add('.'+target_info.DirSep+ls)
  442. else
  443. inherited Add('.'+source_info.DirSep+ls);
  444. end
  445. else
  446. inherited Add(ls);
  447. end;
  448. end;
  449. procedure TLinkRes.EndSection(const s:TCmdStr);
  450. begin
  451. { only terminate if we started the section }
  452. if section='' then
  453. inherited Add(s);
  454. section:='';
  455. end;
  456. procedure TLinkRes.StartSection(const s:TCmdStr);
  457. begin
  458. section:=s;
  459. end;
  460. end.