tcrecompile.pas 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. unit tcrecompile;
  2. {$mode ObjFPC}{$H+}
  3. interface
  4. uses
  5. Classes, SysUtils, fpcunit, testregistry, tstppuutils;
  6. type
  7. { TTestRecompile }
  8. TTestRecompile = class(TTestCase)
  9. private
  10. FCompiled: TStringList;
  11. FMainSrc: string;
  12. FOutDir: string;
  13. FPP: string;
  14. FStep: string;
  15. FUnitPath: string;
  16. protected
  17. procedure SetUp; override;
  18. procedure TearDown; override;
  19. procedure CleanOutputDir; overload;
  20. procedure CleanOutputDir(Dir: string); overload;
  21. procedure Compile;
  22. procedure CheckCompiled(const Expected: TStringArray);
  23. procedure MakeDateDiffer(const File1, File2: string);
  24. property PP: string read FPP write FPP;
  25. property UnitPath: string read FUnitPath write FUnitPath;
  26. property OutDir: string read FOutDir write FOutDir;
  27. property MainSrc: string read FMainSrc write FMainSrc;
  28. property Compiled: TStringList read FCompiled write FCompiled;
  29. property Step: string read FStep write FStep;
  30. public
  31. constructor Create; override;
  32. procedure GetCompiler;
  33. procedure CheckCompiler;
  34. published
  35. procedure TestTwoUnits; // 2 units
  36. procedure TestChangeLeaf1; // prog+2 units, change leaf
  37. procedure TestChangeInner1; // prog+2 units, change inner unit, keep leaf
  38. procedure TestChangeInlineBodyBug; // Bug: prog+1 unit plus a package of 2 units, change of inline body should change crc, but does not
  39. procedure TestBug41457; // two cycles of size 2 and 3
  40. // inline modifier in implementation (not in interface)
  41. procedure TestImplInline1; // 2 units, cycle, impl inline
  42. procedure TestImplInline2; // program + 2 units cycle, impl inline
  43. procedure TestImplInline_Bug41291; // program plus 3 cycles
  44. procedure TestImplInline3; // program + 2 units cycle, impl inline, implementation changed
  45. end;
  46. implementation
  47. { TTestRecompile }
  48. procedure TTestRecompile.SetUp;
  49. begin
  50. inherited SetUp;
  51. UnitPath:='';
  52. OutDir:='';
  53. MainSrc:='';
  54. Compiled:=TStringList.Create;
  55. end;
  56. procedure TTestRecompile.TearDown;
  57. begin
  58. FreeAndNil(FCompiled);
  59. inherited TearDown;
  60. end;
  61. procedure TTestRecompile.CleanOutputDir;
  62. begin
  63. CleanOutputDir(OutDir);
  64. end;
  65. procedure TTestRecompile.CleanOutputDir(Dir: string);
  66. var
  67. Info: TRawByteSearchRec;
  68. Filename: String;
  69. r: LongInt;
  70. begin
  71. if Dir='' then
  72. Fail('TTestRecompile.CleanOutputDir: missing Dir');
  73. if Dir[length(Dir)]=PathDelim then
  74. Delete(Dir,length(Dir),1);
  75. if not DirectoryExists(Dir) then
  76. if not CreateDir(Dir) then
  77. Fail('unable to create output directory "'+Dir+'"');
  78. writeln('CleanOutputDir ',Dir);
  79. r:=FindFirst(Dir+PathDelim+AllFilesMask,faAnyFile,Info);
  80. try
  81. if r<>0 then exit;
  82. repeat
  83. case Info.Name of
  84. '','.','..': continue;
  85. end;
  86. if faDirectory and Info.Attr>0 then
  87. continue; // keep directories
  88. if Info.Name[1]='.' then
  89. continue; // keep hidden files
  90. case lowercase(ExtractFileExt(Info.Name)) of
  91. '.txt': continue; // keep txt files
  92. end;
  93. Filename:=Dir+PathDelim+Info.Name;
  94. if not DeleteFile(Filename) then
  95. Fail('unable to delete "'+Filename+'"');
  96. until FindNext(Info)<>0;
  97. finally
  98. FindClose(Info);
  99. end;
  100. end;
  101. procedure TTestRecompile.Compile;
  102. var
  103. Params, Lines: TStringList;
  104. i: Integer;
  105. Line, Filename: String;
  106. begin
  107. Compiled.Clear;
  108. if UnitPath='' then
  109. Fail('missing UnitPath, Step='+Step);
  110. if OutDir='' then
  111. Fail('missing OutDir, Step='+Step);
  112. if not DirectoryExists(OutDir) then
  113. Fail('OutDir not found "'+OutDir+'", Step='+Step);
  114. if MainSrc='' then
  115. Fail('missing MainSrc, Step='+Step);
  116. if not FileExists(MainSrc) then
  117. Fail('main src file not found "'+MainSrc+'", Step='+Step);
  118. Lines:=nil;
  119. Params:=TStringList.Create;
  120. try
  121. Params.Add('-Fu'+UnitPath);
  122. Params.Add('-FE'+OutDir);
  123. Params.Add(MainSrc);
  124. if not RunTool(PP,Params,'',false,true,Lines) then
  125. Fail('compile failed, Step='+Step);
  126. for i:=0 to Lines.Count-1 do
  127. begin
  128. Line:=Lines[i];
  129. if LeftStr(Line,length('Compiling '))='Compiling ' then
  130. begin
  131. Filename:=copy(Line,length('Compiling ')+1,length(Line));
  132. writeln('Compiling ',Filename);
  133. Filename:=ExtractFileName(Filename);
  134. if Compiled.IndexOf(Filename)<0 then
  135. Compiled.Add(Filename);
  136. end;
  137. end;
  138. finally
  139. Lines.Free;
  140. Params.Free;
  141. end;
  142. end;
  143. procedure TTestRecompile.CheckCompiled(const Expected: TStringArray);
  144. var
  145. i, j: Integer;
  146. begin
  147. for i:=0 to length(Expected)-1 do
  148. if (Compiled=nil) or (Compiled.IndexOf(Expected[i])<0) then
  149. Fail('missing compiling "'+Expected[i]+'", Step='+Step);
  150. for i:=0 to Compiled.Count-1 do
  151. begin
  152. j:=length(Expected)-1;
  153. while (j>=0) and (Expected[j]<>Compiled[i]) do dec(j);
  154. if j<0 then
  155. Fail('unexpected compiling "'+Compiled[i]+'", Step='+Step);
  156. end;
  157. end;
  158. procedure TTestRecompile.MakeDateDiffer(const File1, File2: string);
  159. var
  160. Age1, Age2: Int64;
  161. begin
  162. Age1:=FileAge(File1);
  163. if Age1<0 then
  164. Fail('file not found "'+File1+'"');
  165. Age2:=FileAge(File2);
  166. if Age2<0 then
  167. Fail('file not found "'+File2+'"');
  168. if Age1<>Age2 then exit;
  169. FileSetDate(File2,Age2-2);
  170. end;
  171. constructor TTestRecompile.Create;
  172. begin
  173. inherited Create;
  174. GetCompiler;
  175. end;
  176. procedure TTestRecompile.GetCompiler;
  177. begin
  178. PP:=GetEnvironmentVariable(String('PP'));
  179. if PP>'' then
  180. begin
  181. CheckCompiler;
  182. exit;
  183. end;
  184. raise Exception.Create('I need environment var "PP"');
  185. end;
  186. procedure TTestRecompile.CheckCompiler;
  187. procedure E(Msg: string);
  188. begin
  189. writeln('TTestRecompile.CheckCompiler: '+Msg);
  190. raise Exception.Create('TTestRecompile.CheckCompiler: '+Msg);
  191. end;
  192. begin
  193. if PP='' then
  194. E('missing compiler');
  195. if not FileIsExecutable(PP) then
  196. E('compiler not executable: "'+PP+'"');
  197. end;
  198. procedure TTestRecompile.TestTwoUnits;
  199. begin
  200. UnitPath:='twounits';
  201. OutDir:='twounits'+PathDelim+'ppus';
  202. MainSrc:='twounits'+PathDelim+'tppu_twounits_ant.pas';
  203. Step:='First compile';
  204. CleanOutputDir;
  205. Compile;
  206. CheckCompiled(['tppu_twounits_ant.pas','tppu_twounits_bird.pas']);
  207. Step:='Second compile';
  208. Compile;
  209. // the bird ppu does not depend on ant, so it is kept
  210. CheckCompiled(['tppu_twounits_ant.pas']);
  211. end;
  212. procedure TTestRecompile.TestChangeLeaf1;
  213. var
  214. Dir: String;
  215. begin
  216. Dir:='changeleaf1';
  217. UnitPath:=Dir+';'+Dir+PathDelim+'src1';
  218. OutDir:=Dir+PathDelim+'ppus';
  219. MainSrc:=Dir+PathDelim+'changeleaf1_prg.pas';
  220. MakeDateDiffer(
  221. Dir+PathDelim+'src1'+PathDelim+'changeleaf1_bird.pas',
  222. Dir+PathDelim+'src2'+PathDelim+'changeleaf1_bird.pas');
  223. Step:='First compile';
  224. CleanOutputDir;
  225. Compile;
  226. CheckCompiled(['changeleaf1_prg.pas','changeleaf1_ant.pas','changeleaf1_bird.pas']);
  227. Step:='Second compile';
  228. UnitPath:=Dir+';'+Dir+PathDelim+'src2';
  229. Compile;
  230. // the main src is always compiled, bird changed, so ant must be recompiled as well
  231. CheckCompiled(['changeleaf1_prg.pas','changeleaf1_ant.pas','changeleaf1_bird.pas']);
  232. end;
  233. procedure TTestRecompile.TestChangeInner1;
  234. var
  235. Dir: String;
  236. begin
  237. Dir:='changeinner1';
  238. UnitPath:=Dir+';'+Dir+PathDelim+'src1';
  239. OutDir:=Dir+PathDelim+'ppus';
  240. MainSrc:=Dir+PathDelim+'changeinner1_prg.pas';
  241. MakeDateDiffer(
  242. Dir+PathDelim+'src1'+PathDelim+'changeinner1_ant.pas',
  243. Dir+PathDelim+'src2'+PathDelim+'changeinner1_ant.pas');
  244. Step:='First compile';
  245. CleanOutputDir;
  246. Compile;
  247. CheckCompiled(['changeinner1_prg.pas','changeinner1_ant.pas','changeinner1_bird.pas']);
  248. Step:='Second compile';
  249. UnitPath:=Dir+';'+Dir+PathDelim+'src2';
  250. Compile;
  251. // the main src is always compiled, ant changed, bird is kept
  252. CheckCompiled(['changeinner1_prg.pas','changeinner1_ant.pas']);
  253. end;
  254. procedure TTestRecompile.TestChangeInlineBodyBug;
  255. var
  256. ProgDir, PkgDir, PkgOutDir: String;
  257. begin
  258. // unit testcib_elk uses an inline function of unit testcib_bird
  259. // elk belongs to the program, bird to the package, so they are compiled separately
  260. // when the inline body of bird changes, the elk.ppu must be rebuilt too
  261. ProgDir:='changeinlinebody'+PathDelim;
  262. PkgDir:=ProgDir+'pkg';
  263. PkgOutDir:=PkgDir+PathDelim+'lib';
  264. MakeDateDiffer(
  265. ProgDir+'original'+PathDelim+'testcib_bird.pas',
  266. ProgDir+'changed'+PathDelim+'testcib_bird.pas');
  267. // compile package containing testcib_ant.pas and testcib_bird.pas
  268. Step:='Compile original package';
  269. UnitPath:=PkgDir+';'+ProgDir+'original';
  270. OutDir:=PkgOutDir;
  271. MainSrc:=PkgDir+PathDelim+'testcib_ant.pas';
  272. CleanOutputDir;
  273. Compile;
  274. CheckCompiled(['testcib_ant.pas','testcib_bird.pas']);
  275. // compile program
  276. Step:='Compile program with original package ppus';
  277. UnitPath:=ProgDir+';'+PkgOutDir;
  278. OutDir:=ProgDir+'lib';
  279. MainSrc:=ProgDir+'testcib_prog.pas';
  280. CleanOutputDir;
  281. Compile;
  282. CheckCompiled(['testcib_prog.pas','testcib_elk.pas']);
  283. // recompile package with changed testcib_bird.pas
  284. Step:='Compile changed package';
  285. UnitPath:=PkgDir+';'+ProgDir+'changed';
  286. OutDir:=PkgOutDir;
  287. MainSrc:=PkgDir+PathDelim+'testcib_ant.pas';
  288. Compile;
  289. CheckCompiled(['testcib_ant.pas','testcib_bird.pas']);
  290. // recompile program
  291. Step:='Compile program with changed package ppus';
  292. UnitPath:=ProgDir+';'+PkgOutDir;
  293. OutDir:=ProgDir+'lib';
  294. MainSrc:=ProgDir+'testcib_prog.pas';
  295. Compile;
  296. // fpc should compile elk:
  297. //CheckCompiled(['testcib_prog.pas','testcib_elk.pas']);
  298. // But it does not:
  299. CheckCompiled(['testcib_prog.pas']);
  300. end;
  301. procedure TTestRecompile.TestBug41457;
  302. begin
  303. UnitPath:='bug41457';
  304. OutDir:=UnitPath+PathDelim+'ppus';
  305. MainSrc:=UnitPath+PathDelim+'bug41457_bird.pas';
  306. Step:='First compile';
  307. CleanOutputDir;
  308. Compile;
  309. CheckCompiled(['bug41457_ant.pas',
  310. 'bug41457_bird.pas',
  311. 'bug41457_eagle.pas',
  312. 'bug41457_hawk.pas',
  313. 'bug41457_seagull.pas']);
  314. Step:='Second compile';
  315. // the two deepest nodes of the two cycles are eagle and hawk, which are not recompiled
  316. Compile;
  317. CheckCompiled(['bug41457_ant.pas',
  318. 'bug41457_bird.pas',
  319. 'bug41457_seagull.pas']);
  320. end;
  321. procedure TTestRecompile.TestImplInline1;
  322. // unit ant uses bird
  323. // unit bird impl uses ant and has a function with inline modifier in implementation
  324. begin
  325. UnitPath:='implinline1';
  326. OutDir:='implinline1'+PathDelim+'ppus';
  327. MainSrc:='implinline1'+PathDelim+'implinline1_ant.pas';
  328. Step:='First compile';
  329. CleanOutputDir;
  330. Compile;
  331. CheckCompiled(['implinline1_ant.pas','implinline1_bird.pas']);
  332. Step:='Second compile';
  333. Compile;
  334. // the main src is always compiled, and since bird ppu depends on ant, it is always compiled as well
  335. CheckCompiled(['implinline1_ant.pas','implinline1_bird.pas']);
  336. end;
  337. procedure TTestRecompile.TestImplInline2;
  338. // prg uses ant
  339. // unit ant uses bird
  340. // unit bird impl uses ant and has a function with inline modifier in implementation
  341. begin
  342. UnitPath:='implinline2';
  343. OutDir:='implinline2'+PathDelim+'ppus';
  344. MainSrc:='implinline2'+PathDelim+'implinline2_prg.pas';
  345. Step:='First compile';
  346. CleanOutputDir;
  347. Compile;
  348. CheckCompiled(['implinline2_prg.pas','implinline2_ant.pas','implinline2_bird.pas']);
  349. Step:='Second compile';
  350. Compile;
  351. // the main src is always compiled, the two ppus of ant and bird are kept
  352. CheckCompiled(['implinline2_prg.pas']);
  353. end;
  354. procedure TTestRecompile.TestImplInline_Bug41291;
  355. begin
  356. UnitPath:='bug41291';
  357. OutDir:='bug41291'+PathDelim+'ppus';
  358. MainSrc:='bug41291'+PathDelim+'bug41291_app.pas';
  359. Step:='First compile';
  360. CleanOutputDir;
  361. Compile;
  362. CheckCompiled(['bug41291_app.pas','bug41291_mclasses.pas','bug41291_mseapplication.pas',
  363. 'bug41291_mseclasses.pas','bug41291_mseeditglob.pas','bug41291_mseifiglob.pas']);
  364. Step:='Second compile';
  365. Compile;
  366. // the main src is always compiled, the other ppus are kept
  367. CheckCompiled(['bug41291_app.pas']);
  368. end;
  369. procedure TTestRecompile.TestImplInline3;
  370. var
  371. Dir: String;
  372. begin
  373. Dir:='implinline3';
  374. UnitPath:=Dir+';'+Dir+PathDelim+'src1';
  375. OutDir:=Dir+PathDelim+'ppus';
  376. MainSrc:=Dir+PathDelim+'implinline3_prg.pas';
  377. MakeDateDiffer(
  378. Dir+PathDelim+'src1'+PathDelim+'implinline3_ant.pas',
  379. Dir+PathDelim+'src2'+PathDelim+'implinline3_ant.pas');
  380. MakeDateDiffer(
  381. Dir+PathDelim+'src1'+PathDelim+'implinline3_bird.pas',
  382. Dir+PathDelim+'src2'+PathDelim+'implinline3_bird.pas');
  383. Step:='First compile';
  384. CleanOutputDir;
  385. Compile;
  386. CheckCompiled(['implinline3_prg.pas','implinline3_ant.pas','implinline3_bird.pas']);
  387. Step:='Second compile';
  388. UnitPath:=Dir+';'+Dir+PathDelim+'src2';
  389. Compile;
  390. // the main src is always compiled, and the ant impl changed, so bird is also compiled
  391. CheckCompiled(['implinline3_prg.pas','implinline3_ant.pas','implinline3_bird.pas']);
  392. end;
  393. initialization
  394. RegisterTests([TTestRecompile]);
  395. end.