fpcmmain.pp 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359
  1. {
  2. $Id$
  3. Copyright (c) 2001 by Peter Vreman
  4. FPCMake - Main module
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. {$ifdef fpc}{$mode objfpc}{$endif}
  12. {$H+}
  13. unit fpcmmain;
  14. interface
  15. uses
  16. dos,
  17. {$ifdef Unix}
  18. {$ifdef VER1_0}
  19. linux,
  20. {$else}
  21. unix,
  22. {$endif}
  23. {$endif}
  24. sysutils,classes,
  25. fpcmdic;
  26. const
  27. Version='1.1';
  28. Title='FPCMake Version '+Version;
  29. TitleDate=Title+' ['+{$ifdef fpc}{$i %DATE}{$else}'n/a'{$endif}+']';
  30. type
  31. TTarget=(
  32. t_linux,t_go32v2,t_win32,t_os2,t_freebsd,t_beos,
  33. t_amiga,t_atari
  34. );
  35. TTargetSet=set of TTarget;
  36. const
  37. TargetStr : array[TTarget] of string=(
  38. 'linux','go32v2','win32','os2','freebsd','beos',
  39. 'amiga','atari'
  40. );
  41. TargetSuffix : array[TTarget] of string=(
  42. '_linux','_go32v2','_win32','_os2','_freebsd','_beos',
  43. '_amiga','_atari'
  44. );
  45. type
  46. TKeyValueItem = class(TDictionaryItem)
  47. private
  48. FValue : string;
  49. public
  50. constructor Create(const k,v:string);
  51. property Value:string read FValue write FValue;
  52. end;
  53. TKeyValue = class(TDictionary)
  54. private
  55. function GetKey(const k:string):string;
  56. public
  57. procedure Add(const k,v:String);
  58. property Key[const s:string]:string read GetKey write Add;default;
  59. end;
  60. TFPCMakeSection = class(TDictionaryItem)
  61. private
  62. FList : TStringList;
  63. FDictionary : TKeyValue;
  64. procedure BuildIniDic(p:TDictionaryItem);
  65. procedure BuildMakefileDic(p:TDictionaryItem);
  66. function GetKey(const k:string):string;
  67. public
  68. constructor Create(const n:string);
  69. constructor CreateKeyValue(const n:string);
  70. destructor Destroy;override;
  71. procedure AddLine(const s:string);
  72. procedure AddKey(const k,v:string);
  73. procedure Clear;
  74. procedure ParseIni;
  75. procedure BuildIni;
  76. procedure BuildMakefile;
  77. property Key[const s:string]:string read GetKey;default;
  78. property List:TStringList read FList;
  79. property Dictionary:TKeyValue read FDictionary;
  80. end;
  81. TTargetRequireList = array[ttarget] of TStringList;
  82. TFPCMakeVerbose = (FPCMakeError, FPCMakeInfo, FPCMakeDebug);
  83. TFPCMake = class
  84. private
  85. FStream : TStream;
  86. FFileName : string;
  87. FCommentChars : TSysCharSet;
  88. FEmptyLines : boolean;
  89. FSections : TDictionary;
  90. FPackageSec,
  91. FExportSec : TFPCMakeSection;
  92. FIsPackage : boolean;
  93. FPackageName,
  94. FPackageVersion,
  95. FPackageTargets : string;
  96. FRequireList : TTargetRequireList;
  97. FVariables : TKeyValue;
  98. FIncludeTargets : TTargetSet;
  99. procedure Init;
  100. procedure ParseSec(p:TDictionaryItem);
  101. procedure PrintSec(p:TDictionaryItem);
  102. procedure PrintDic(p:TDictionaryItem);
  103. function GetSec(const AName:string):TDictionaryItem;
  104. procedure LoadRequiredPackage(t:TTarget;const ReqName,ReqVersion:string);
  105. procedure LoadRequiredDir(t:TTarget;const MainPack,currdir,subdir:string);
  106. procedure LoadRequires(t:Ttarget;FromFPCMake:TFPCMake);
  107. function CopySection(Sec:TFPCMakeSection;Secname:string):TFPCMakeSection;
  108. protected
  109. VerboseIdent : string;
  110. public
  111. constructor Create(const AFileName:string);
  112. constructor CreateFromStream(s:TStream;const AFileName:string);
  113. destructor Destroy;override;
  114. procedure Verbose(lvl:TFPCMakeVerbose;const s:string);virtual;
  115. procedure SetTargets(const s:string);
  116. procedure LoadSections;
  117. procedure LoadMakefileFPC;
  118. procedure LoadPackageSection;
  119. procedure LoadRequireSection;
  120. function GetTargetRequires(t:TTarget):TStringList;
  121. function CheckLibcRequire:boolean;
  122. procedure CreateExportSection;
  123. procedure AddDefaultVariables;
  124. function SubstVariables(const s:string):string;
  125. function GetVariable(const inivar:string;dosubst:boolean):string;
  126. function SetVariable(const inivar,value:string;add:boolean):string;
  127. procedure Print;
  128. property Section[const s:string]:TDictionaryItem read GetSec;default;
  129. property RequireList:TTargetRequireList read FRequireList;
  130. property Variables:TKeyValue read FVariables;
  131. property IsPackage:boolean read FIsPackage;
  132. property PackageName:string read FPackageName;
  133. property PackageVersion:string read FPackageVersion;
  134. property PackageSec:TFPCMakeSection read FPackageSec;
  135. property ExportSec:TFPCMakeSection read FExportSec;
  136. property CommentChars:TSysCharSet read FCommentChars write FCommentChars;
  137. property EmptyLines:Boolean read FEmptyLines write FEmptyLines;
  138. property IncludeTargets:TTargetSet read FIncludeTargets write FIncludeTargets;
  139. end;
  140. function posidx(const substr,s : string;idx:integer):integer;
  141. function GetToken(var s:string;sep:char):string;
  142. procedure AddToken(var s:string;const tok:string;sep:char);
  143. implementation
  144. resourcestring
  145. s_not_list_sec='Not a list section "%s"';
  146. s_not_key_value_sec='Not a key-value section "%s"';
  147. s_err_section_start='%s:%d: Wrong section start';
  148. s_err_not_key_value='Parse error key=value excepted: "%s"';
  149. s_err_no_section='%s:%d: Entries without section';
  150. s_no_package_name='No package name set';
  151. s_no_package_version='No package version set';
  152. s_err_require_format='Wrong require format "%s"';
  153. s_wrong_package_name='Package name "%s" expected, but "%s" found';
  154. s_wrong_package_version='Package version "%s" expected, but version "%s" found';
  155. s_directory_not_found='Directory "%s" not found';
  156. s_makefilefpc_not_found='No Makefile.fpc found in directory "%s"';
  157. s_package_not_found='Package "%s" not found';
  158. s_fpcmake_version_required='FPCMake version "%s" is required';
  159. s_no_targets_set='No targets set';
  160. s_targets_info='Targets: "%s"';
  161. s_globals='Globals:';
  162. {****************************************************************************
  163. Helpers
  164. ****************************************************************************}
  165. Function PathExists ( F : String) : Boolean;
  166. Var
  167. Info : TSearchRec;
  168. begin
  169. if F[Length(f)] in ['/','\'] then
  170. Delete(f,length(f),1);
  171. PathExists:=(findfirst(F,fareadonly+faarchive+fahidden+fadirectory,info)=0) and
  172. ((info.attr and fadirectory)=fadirectory);
  173. findclose(Info);
  174. end;
  175. Function PathOrFileExists ( F : String) : Boolean;
  176. Var
  177. Info : Dos.SearchRec;
  178. begin
  179. if F[Length(f)] in ['/','\'] then
  180. Delete(f,length(f),1);
  181. dos.findfirst(f,fareadonly+faarchive+fahidden+fadirectory,info);
  182. PathOrFileExists:=(Doserror=0);
  183. dos.findclose(Info);
  184. end;
  185. function posidx(const substr,s : string;idx:integer):integer;
  186. var
  187. i,j : integer;
  188. e : boolean;
  189. begin
  190. i:=idx;
  191. j:=0;
  192. e:=(length(SubStr)>0);
  193. while e and (i<=Length(s)-Length(SubStr)) do
  194. begin
  195. inc(i);
  196. if (SubStr[1]=s[i]) and (Substr=Copy(s,i,Length(SubStr))) then
  197. begin
  198. j:=i;
  199. e:=false;
  200. end;
  201. end;
  202. PosIdx:=j;
  203. end;
  204. function GetToken(var s:string;sep:char):string;
  205. var
  206. i : integer;
  207. begin
  208. s:=Trim(s);
  209. i:=pos(sep,s);
  210. if i=0 then
  211. begin
  212. Result:=s;
  213. s:='';
  214. end
  215. else
  216. begin
  217. Result:=Copy(s,1,i-1);
  218. Delete(s,1,i);
  219. end;
  220. end;
  221. procedure AddToken(var s:string;const tok:string;sep:char);
  222. begin
  223. if tok='' then
  224. exit;
  225. if s<>'' then
  226. s:=s+sep+tok
  227. else
  228. s:=tok;
  229. end;
  230. {****************************************************************************
  231. TKeyValueItem
  232. ****************************************************************************}
  233. constructor TKeyValueItem.Create(const k,v:string);
  234. begin
  235. inherited Create(k);
  236. value:=v;
  237. end;
  238. {****************************************************************************
  239. TKeyValue
  240. ****************************************************************************}
  241. function TKeyValue.GetKey(const k:string):string;
  242. var
  243. p : TKeyValueItem;
  244. begin
  245. p:=TKeyValueItem(Search(k));
  246. if p=nil then
  247. GetKey:=''
  248. else
  249. GetKey:=p.Value;
  250. end;
  251. procedure TKeyValue.Add(const k,v:string);
  252. var
  253. p : TKeyValueItem;
  254. begin
  255. p:=TKeyValueItem(Search(k));
  256. if p=nil then
  257. begin
  258. p:=TKeyValueItem.Create(k,v);
  259. Insert(p);
  260. end
  261. else
  262. p.Value:=v;
  263. end;
  264. {****************************************************************************
  265. TFPCMakeSection
  266. ****************************************************************************}
  267. constructor TFPCMakeSection.Create(const n:string);
  268. begin
  269. inherited Create(n);
  270. FList:=TStringList.Create;
  271. FDictionary:=nil;
  272. end;
  273. constructor TFPCMakeSection.CreateKeyValue(const n:string);
  274. begin
  275. inherited Create(n);
  276. FList:=nil;
  277. FDictionary:=TKeyValue.Create;
  278. end;
  279. destructor TFPCMakeSection.Destroy;
  280. begin
  281. inherited Destroy;
  282. FList.Free;
  283. FDictionary.Free;
  284. end;
  285. procedure TFPCMakeSection.Clear;
  286. begin
  287. FList.Free;
  288. FList:=TStringList.Create;
  289. FDictionary.Free;
  290. FDictionary:=nil;
  291. end;
  292. procedure TFPCMakeSection.AddLine(const s:string);
  293. begin
  294. if FList=nil then
  295. raise Exception.Create(Format(s_not_list_sec,[Name]));
  296. FList.Add(s);
  297. end;
  298. procedure TFPCMakeSection.AddKey(const k,v:string);
  299. begin
  300. if FDictionary=nil then
  301. raise Exception.Create(Format(s_not_key_value_sec,[Name]));
  302. { Don't add empty values }
  303. if v<>'' then
  304. FDictionary.Add(k,v);
  305. end;
  306. function TFPCMakeSection.GetKey(const k:string):string;
  307. begin
  308. if FDictionary=nil then
  309. raise Exception.Create(Format(s_not_key_value_sec,[Name]));
  310. GetKey:=FDictionary[k];
  311. end;
  312. procedure TFPCMakeSection.ParseIni;
  313. var
  314. p : TKeyValueItem;
  315. i,j,len,maxi : integer;
  316. s,newkey,value : string;
  317. begin
  318. { If already processed skip }
  319. if assigned(FDictionary) then
  320. exit;
  321. { Don't process rules section }
  322. if (Name='prerules') or (Name='rules') then
  323. exit;
  324. { Parse the section }
  325. FDictionary:=TKeyValue.Create;
  326. { Parse the list }
  327. maxi:=FList.Count;
  328. i:=0;
  329. while (i<maxi) do
  330. begin
  331. s:=Trim(FList[i]);
  332. len:=Length(s);
  333. { Concat lines ending with \ }
  334. while s[len]='\' do
  335. begin
  336. Delete(s,len,1);
  337. if i+1<maxi then
  338. begin
  339. inc(i);
  340. s:=s+Trim(FList[i]);
  341. len:=Length(s);
  342. end;
  343. end;
  344. { Parse key=value line }
  345. j:=0;
  346. while (j<len) and (s[j+1] in ['A'..'Z','a'..'z','0'..'9','_']) do
  347. inc(j);
  348. NewKey:=Copy(s,1,j);
  349. While (j<len) and (s[j+1] in [' ',#9]) do
  350. inc(j);
  351. inc(j);
  352. if s[j]<>'=' then
  353. Raise Exception.Create(Format(s_err_not_key_value,[s]));
  354. While (j<len) and (s[j+1] in [' ',#9]) do
  355. inc(j);
  356. Value:=Copy(s,j+1,len-j);
  357. p:=TKeyValueItem(FDictionary.Search(NewKey));
  358. { Concat values if key already exists }
  359. if assigned(p) then
  360. AddToken(p.FValue,Value,' ')
  361. else
  362. FDictionary.Add(NewKey,Value);
  363. inc(i);
  364. end;
  365. { List is not used anymore }
  366. FList.Free;
  367. FList:=nil;
  368. end;
  369. procedure TFPCMakeSection.BuildIniDic(p:TDictionaryItem);
  370. begin
  371. with TKeyValueItem(p) do
  372. begin
  373. FList.Add(Name+'='+Value);
  374. end;
  375. end;
  376. procedure TFPCMakeSection.BuildIni;
  377. begin
  378. if assigned(FList) then
  379. exit;
  380. FList:=TStringList.Create;
  381. FDictionary.Foreach(@BuildIniDic);
  382. FDictionary.Free;
  383. FDictionary:=nil;
  384. end;
  385. procedure TFPCMakeSection.BuildMakefileDic(p:TDictionaryItem);
  386. begin
  387. FList.Add(Uppercase(Name+'_'+TKeyValueItem(p).Name)+'='+TKeyValueItem(p).Value);
  388. end;
  389. procedure TFPCMakeSection.BuildMakefile;
  390. begin
  391. if assigned(FList) then
  392. exit;
  393. FList:=TStringList.Create;
  394. FDictionary.Foreach(@BuildMakefileDic);
  395. FDictionary.Free;
  396. FDictionary:=nil;
  397. end;
  398. {****************************************************************************
  399. TFPCMake
  400. ****************************************************************************}
  401. constructor TFPCMake.Create(const AFileName:string);
  402. begin
  403. FFileName:=AFileName;
  404. FStream:=nil;
  405. Init;
  406. end;
  407. constructor TFPCMake.CreateFromStream(s:TStream;const AFileName:string);
  408. begin
  409. FFileName:=AFileName;
  410. FStream:=s;
  411. Init;
  412. end;
  413. procedure TFPCMake.Init;
  414. var
  415. t : ttarget;
  416. begin
  417. FSections:=TDictionary.Create;
  418. for t:=low(ttarget) to high(ttarget) do
  419. FRequireList[t]:=TStringList.Create;
  420. FVariables:=TKeyValue.Create;
  421. FCommentChars:=[';','#'];
  422. FEmptyLines:=false;
  423. FIsPackage:=false;
  424. FPackageName:='';
  425. FPackageVersion:='';
  426. FPackageSec:=nil;
  427. FExportSec:=nil;
  428. FIncludeTargets:=[low(TTarget)..high(TTarget)];
  429. VerboseIdent:='';
  430. end;
  431. destructor TFPCMake.Destroy;
  432. var
  433. t : ttarget;
  434. begin
  435. FSections.Free;
  436. for t:=low(ttarget) to high(ttarget) do
  437. FRequireList[t].Free;
  438. FVariables.Free;
  439. end;
  440. procedure TFPCMake.LoadSections;
  441. var
  442. SLInput : TStringList;
  443. i,j,n : integer;
  444. s,
  445. SecName : string;
  446. CurrSec : TFPCMakeSection;
  447. begin
  448. try
  449. SLInput:=TStringList.Create;
  450. if assigned(FStream) then
  451. SLInput.LoadFromStream(FStream)
  452. else
  453. SLInput.LoadFromFile(FFileName);
  454. { Load Input into sections list }
  455. n:=SLInput.Count;
  456. i:=0;
  457. while (i<n) do
  458. begin
  459. s:=Trim(SLInput[i]);
  460. if (EmptyLines and (s='')) or
  461. ((s<>'') and not(s[1] in FCommentChars)) then
  462. begin
  463. { section start? }
  464. if (s<>'') and (s[1]='[') then
  465. begin
  466. j:=pos(']',s);
  467. if j=0 then
  468. raise Exception.Create(Format(s_err_section_start,[FFileName,i]));
  469. SecName:=Copy(s,2,j-2);
  470. CurrSec:=TFPCMakeSection(FSections[SecName]);
  471. if CurrSec=nil then
  472. CurrSec:=TFPCMakeSection(FSections.Insert(TFPCMakeSection.Create(SecName)));
  473. end
  474. else
  475. begin
  476. if CurrSec=nil then
  477. raise Exception.Create(Format(s_err_no_section,[FFileName,i]));
  478. { Insert string without spaces stripped }
  479. CurrSec.AddLine(SLInput[i]);
  480. end;
  481. end;
  482. inc(i);
  483. end;
  484. finally
  485. SLInput.Free;
  486. end;
  487. end;
  488. function TFPCMake.CopySection(Sec:TFPCMakeSection;Secname:string):TFPCMakeSection;
  489. begin
  490. Result:=TFPCMakeSection(FSections[SecName]);
  491. if Sec=Nil then
  492. exit;
  493. { Clear old section or if not existing create new }
  494. if assigned(Result) then
  495. Result.Clear
  496. else
  497. Result:=TFPCMakeSection(FSections.Insert(TFPCMakeSection.Create(SecName)));
  498. Sec.BuildIni;
  499. Result.List.AddStrings(Sec.List);
  500. Result.ParseIni;
  501. Sec.ParseIni;
  502. end;
  503. procedure TFPCMake.LoadMakefileFPC;
  504. begin
  505. LoadSections;
  506. { Parse all sections }
  507. FSections.Foreach(@ParseSec);
  508. { Add some default variables like FPCDIR, UNITSDIR }
  509. AddDefaultVariables;
  510. { Load package section }
  511. LoadPackageSection;
  512. LoadRequireSection;
  513. end;
  514. procedure TFPCMake.Verbose(lvl:TFPCMakeVerbose;const s:string);
  515. begin
  516. writeln(VerboseIdent,s);
  517. end;
  518. procedure TFPCMake.SetTargets(const s:string);
  519. var
  520. hslst : string;
  521. hs : string;
  522. t : TTarget;
  523. begin
  524. FIncludeTargets:=[];
  525. hslst:=s;
  526. repeat
  527. hs:=LowerCase(GetToken(hslst,','));
  528. if hs='' then
  529. break;
  530. for t:=low(TTarget) to high(TTarget) do
  531. if hs=TargetStr[t] then
  532. include(FIncludeTargets,t);
  533. until false;
  534. if FIncludeTargets=[] then
  535. raise Exception.Create(s_no_targets_set)
  536. else
  537. begin
  538. hs:='';
  539. for t:=low(TTarget) to high(TTarget) do
  540. if t in FIncludeTargets then
  541. AddToken(hs,TargetStr[t],' ');
  542. Verbose(FPCMakeDebug,Format(s_targets_info,[hs]));
  543. end;
  544. end;
  545. procedure TFPCMake.LoadPackageSection;
  546. var
  547. hs,s : string;
  548. t : TTarget;
  549. begin
  550. { Get package info from package section }
  551. FPackageSec:=TFPCMakeSection(FSections['package']);
  552. if FPackageSec=nil then
  553. exit;
  554. { Parse the section to key=value pairs }
  555. FPackageSec.ParseIni;
  556. { Are we a subpart of a package, then load that package }
  557. s:=FPackageSec['main'];
  558. if s<>'' then
  559. begin
  560. SetVariable('package_name',s,false);
  561. FPackageName:=s;
  562. end
  563. else
  564. begin
  565. { mandatory name }
  566. FPackageName:=FPackageSec['name'];
  567. if FPackageName='' then
  568. Raise Exception.Create(s_no_package_name);
  569. { mandatory version }
  570. FPackageVersion:=FPackageSec['version'];
  571. if FPackageVersion='' then
  572. Raise Exception.Create(s_no_package_version);
  573. FIsPackage:=true;
  574. { optional targets }
  575. FPackageTargets:='';
  576. s:=LowerCase(FPackageSec['targets']);
  577. repeat
  578. hs:=GetToken(s,' ');
  579. if hs='' then
  580. break;
  581. for t:=low(TTarget) to high(TTarget) do
  582. if hs=TargetStr[t] then
  583. begin
  584. AddToken(FPackageTargets,hs,' ');
  585. break;
  586. end;
  587. until false;
  588. { Set the ExportSec }
  589. FExportSec:=TFPCMakeSection(FSections[Lowercase(FPackageName)]);
  590. end;
  591. end;
  592. procedure TFPCMake.CreateExportSection;
  593. var
  594. t : TTarget;
  595. begin
  596. { Don't create a section twice }
  597. if FExportSec<>nil then
  598. exit;
  599. { Look if we've already an own section, else create a new
  600. key-value section }
  601. FExportSec:=TFPCMakeSection(FSections[LowerCase(FPackageName)]);
  602. if FExportSec=nil then
  603. FExportSec:=TFPCMakeSection(FSections.Insert(TFPCMakeSection.CreateKeyValue(LowerCase(FPackageName))));
  604. { Add default the values to the export section }
  605. FExportSec.AddKey('name',FPackageName);
  606. FExportSec.AddKey('version',FPackageVersion);
  607. { Add required packages }
  608. for t:=low(TTarget) to high(TTarget) do
  609. FExportSec.AddKey('require'+TargetSuffix[t],FPackageSec['require'+TargetSuffix[t]]);
  610. { Unit dir }
  611. {FExportSec.AddKey('unitdir','$(UNITSDIR)/'+Lowercase(PackageName));}
  612. end;
  613. procedure TFPCMake.LoadRequiredPackage(t:TTarget;const ReqName,ReqVersion:string);
  614. function TryFile(const fn:string):boolean;
  615. var
  616. ReqFPCMake : TFPCMake;
  617. begin
  618. TryFile:=false;
  619. if FileExists(fn) then
  620. begin
  621. VerboseIdent:=VerboseIdent+' ';
  622. Verbose(FPCMakeDebug,'Package '+ReqName+': '+fn);
  623. ReqFPCMake:=TFPCMake.Create(fn);
  624. ReqFPCMake.LoadSections;
  625. ReqFPCMake.LoadPackageSection;
  626. { Check package name and version }
  627. if LowerCase(ReqFPCMake.PackageName)<>ReqName then
  628. raise Exception.Create(Format(s_wrong_package_name,[ReqName,LowerCase(ReqFPCMake.PackageName)]));
  629. if (ReqVersion<>'') and (ReqFPCMake.PackageVersion<ReqVersion) then
  630. raise Exception.Create(Format(s_wrong_package_version,[ReqVersion,ReqFPCMake.PackageVersion]));
  631. { First load the requirements of this package }
  632. LoadRequires(t,ReqFPCMake);
  633. { Get a copy of the package section }
  634. CopySection(ReqFPCMake.PackageSec,ReqName+'_package');
  635. { Get a copy of the export section }
  636. CopySection(ReqFPCMake.ExportSec,ReqName);
  637. { Get a copy of the require section }
  638. CopySection(TFPCMakeSection(ReqFPCMake['require']),ReqName+'_require');
  639. { Free }
  640. ReqFPCMake.Free;
  641. Delete(VerboseIdent,1,2);
  642. TryFile:=true;
  643. end;
  644. end;
  645. var
  646. s : string;
  647. begin
  648. { Force the current target }
  649. SetVariable('TARGET',TargetStr[t],false);
  650. { Check for Makefile.fpc }
  651. s:=SubstVariables('$(wildcard $(addsuffix /'+ReqName+'/Makefile.fpc,$(FPCDIR)) $(addsuffix /'+ReqName+'/Makefile.fpc,$(PACKAGESDIR)) $(addsuffix /'+ReqName+'/Makefile.fpc,$(REQUIRE_PACKAGESDIR)))');
  652. if TryFile(s) then
  653. exit;
  654. { Check for Package.fpc }
  655. s:=SubstVariables('$(wildcard $(addsuffix /'+ReqName+'/Package.fpc,$(FPCDIR)) $(addsuffix /'+ReqName+'/Package.fpc,$(UNITSDIR)) $(addsuffix /'+ReqName+'/Package.fpc,$(REQUIRE_UNITSDIR)))');
  656. if TryFile(s) then
  657. exit;
  658. Raise Exception.Create(Format(s_package_not_found,[Reqname]));
  659. end;
  660. procedure TFPCMake.LoadRequiredDir(t:TTarget;const MainPack,currdir,subdir:string);
  661. var
  662. ReqFPCMake : TFPCMake;
  663. s : string;
  664. begin
  665. VerboseIdent:=VerboseIdent+' ';
  666. s:=currdir+subdir;
  667. Verbose(FPCMakeDebug,'Subdir: '+s+'/Makefile.fpc');
  668. if not FileExists(s+'/Makefile.fpc') then
  669. begin
  670. { give better error what is wrong }
  671. if not PathExists(s) then
  672. Raise Exception.Create(Format(s_directory_not_found,[s]))
  673. else
  674. Raise Exception.Create(Format(s_makefilefpc_not_found,[s]));
  675. end;
  676. { Process Makefile.fpc }
  677. ReqFPCMake:=TFPCMake.Create(currdir+subdir+'/Makefile.fpc');
  678. ReqFPCMake.LoadSections;
  679. ReqFPCMake.LoadPackageSection;
  680. { Are we a subpackage? }
  681. if (ReqFPCMake.GetVariable('package_name',false)<>MainPack) then
  682. begin
  683. ReqFPCMake.Free;
  684. Delete(VerboseIdent,1,2);
  685. exit;
  686. end;
  687. { Load the requirements of this package }
  688. LoadRequires(t,ReqFPCMake);
  689. { Add the current requirements to our parents requirements }
  690. s:=Trim(ReqFPCMake.GetVariable('require_packages',true)+' '+ReqFPCMake.GetVariable('require_packages'+targetsuffix[t],true));
  691. SetVariable('require_packages'+targetsuffix[t],s,true);
  692. if ReqFPCMake.GetVariable('require_libc',false)<>'' then
  693. SetVariable('require_libc','y',false);
  694. { Free }
  695. ReqFPCMake.Free;
  696. Delete(VerboseIdent,1,2);
  697. end;
  698. procedure TFPCMake.LoadRequires(t:Ttarget;FromFPCMake:TFPCMake);
  699. var
  700. s,
  701. ReqDir,
  702. ReqName,
  703. ReqVersion : string;
  704. i,j : integer;
  705. begin
  706. { packages }
  707. s:=Trim(FromFPCMake.GetVariable('require_packages',true)+' '+FromFPCMake.GetVariable('require_packages'+TargetSuffix[t],true));
  708. Verbose(FPCMakeDebug,'Required packages for '+TargetStr[t]+': '+s);
  709. repeat
  710. reqname:=GetToken(s,' ');
  711. if reqname='' then
  712. break;
  713. i:=Pos('(',ReqName);
  714. if i>0 then
  715. begin
  716. j:=Pos(')',ReqName);
  717. if (i=1) or (j=0) then
  718. Raise Exception.Create(Format(s_err_require_format,[ReqName]));
  719. ReqVersion:=Copy(ReqName,i+1,j-i-1);
  720. ReqName:=Copy(ReqName,1,i-1);
  721. end
  722. else
  723. ReqVersion:='';
  724. { We only use lowercase names }
  725. ReqName:=Lowercase(ReqName);
  726. { Already loaded ? }
  727. if (RequireList[t].IndexOf(ReqName)=-1) then
  728. begin
  729. LoadRequiredPackage(t,ReqName,ReqVersion);
  730. RequireList[t].Add(ReqName);
  731. end;
  732. until false;
  733. { sub dirs }
  734. s:=FromFPCMake.GetVariable('target_dirs',true)+' '+FromFPCMake.GetVariable('target_dirs'+TargetSuffix[t],true);
  735. Verbose(FPCMakeDebug,'Required dirs for '+TargetStr[t]+': '+s);
  736. repeat
  737. reqdir:=GetToken(s,' ');
  738. if reqdir='' then
  739. break;
  740. LoadRequiredDir(t,FromFPCMake.FPackageName,ExtractFilePath(FromFPCMake.FFileName),ReqDir)
  741. until false;
  742. end;
  743. procedure TFPCMake.LoadRequireSection;
  744. function CheckVar(const s:string):boolean;
  745. var
  746. t : ttarget;
  747. begin
  748. result:=false;
  749. if GetVariable(s,false)<>'' then
  750. begin
  751. result:=true;
  752. exit;
  753. end;
  754. for t:=low(ttarget) to high(ttarget) do
  755. if t in FIncludeTargets then
  756. begin
  757. if GetVariable(s+targetsuffix[t],false)<>'' then
  758. begin
  759. result:=true;
  760. exit;
  761. end;
  762. end;
  763. end;
  764. var
  765. s : string;
  766. t : ttarget;
  767. begin
  768. { Check FPCMake version }
  769. s:=GetVariable('require_fpcmake',false);
  770. if (s>version) then
  771. raise Exception.Create(Format(s_fpcmake_version_required,[s]));
  772. { Maybe add an implicit rtl dependency if there is something
  773. to compile }
  774. s:=GetVariable('require_packages',false);
  775. if (GetVariable('require_nortl',false)='') and
  776. (CheckVar('target_programs') or
  777. CheckVar('target_units') or
  778. CheckVar('target_examples')) and
  779. (Pos('rtl(',s)=0) then
  780. begin
  781. s:='rtl '+s;
  782. SetVariable('require_packages',s,false);
  783. end;
  784. { Load recursively all required packages starting with this Makefile.fpc }
  785. for t:=low(TTarget) to high(TTarget) do
  786. if t in FIncludeTargets then
  787. LoadRequires(t,self);
  788. end;
  789. function TFPCMake.GetTargetRequires(t:TTarget):TStringList;
  790. var
  791. ReqSec : TFPCMakeSection;
  792. ReqList : TStringList;
  793. procedure AddReqSec(t:TTarget;Sec:TFPCMakeSection);
  794. var
  795. s,
  796. ReqName : string;
  797. RSec : TFPCMakeSection;
  798. i : integer;
  799. begin
  800. s:=Sec['packages']+' '+Sec['packages'+TargetSuffix[t]];
  801. repeat
  802. ReqName:=GetToken(s,' ');
  803. if ReqName='' then
  804. break;
  805. i:=Pos('(',ReqName);
  806. if i>0 then
  807. ReqName:=Copy(ReqName,1,i-1);
  808. { We only use lowercase names }
  809. ReqName:=Lowercase(ReqName);
  810. { Already loaded ? }
  811. if (ReqList.IndexOf(ReqName)=-1) then
  812. begin
  813. RSec:=TFPCMakeSection(FSections[ReqName+'_require']);
  814. if assigned(RSec) then
  815. AddReqSec(t,RSec);
  816. ReqList.Add(ReqName);
  817. end;
  818. until false;
  819. end;
  820. begin
  821. ReqList:=TStringList.Create;
  822. ReqSec:=TFPCMakeSection(FSections['require']);
  823. if assigned(ReqSec) then
  824. AddReqSec(t,ReqSec);
  825. GetTargetRequires:=ReqList;
  826. end;
  827. function TFPCMake.CheckLibcRequire:boolean;
  828. var
  829. i : integer;
  830. RSec : TFPCMakeSection;
  831. t : ttarget;
  832. begin
  833. Result:=false;
  834. if GetVariable('require_libc',false)<>'' then
  835. begin
  836. Result:=true;
  837. exit;
  838. end;
  839. for t:=low(ttarget) to high(ttarget) do
  840. if t in FIncludeTargets then
  841. begin
  842. for i:=0 to RequireList[t].Count-1 do
  843. begin
  844. RSec:=TFPCMakeSection(FSections[RequireList[t][i]+'_require']);
  845. if assigned(RSec) then
  846. begin
  847. if RSec['libc']<>'' then
  848. begin
  849. Result:=true;
  850. exit;
  851. end;
  852. end;
  853. end;
  854. end;
  855. end;
  856. procedure TFPCMake.AddDefaultVariables;
  857. var
  858. hs,s : string;
  859. begin
  860. { Already set FPCDIR }
  861. hs:='';
  862. s:=GetVariable('FPCDIR',false);
  863. if s<>'' then
  864. hs:=SubstVariables('$(wildcard $(addprefix '+s+'/,rtl units))');
  865. { Load from environment }
  866. if hs='' then
  867. begin
  868. s:=GetEnv('FPCDIR');
  869. if s<>'' then
  870. hs:=SubstVariables('$(wildcard $(addprefix '+s+'/,rtl units))');
  871. end;
  872. { default_fpcdir }
  873. if hs='' then
  874. begin
  875. s:=GetVariable('default_fpcdir',true);
  876. { add the current subdir to relative paths }
  877. if s<>'' then
  878. begin
  879. {$ifdef UNIX}
  880. if (s[1]<>'/') then
  881. {$else}
  882. if (length(s)>2) and (s[2]<>':') then
  883. {$endif}
  884. s:=ExtractFilePath(FFileName)+s;
  885. hs:=SubstVariables('$(wildcard $(addprefix '+s+'/,rtl units))');
  886. end
  887. end;
  888. { OS defaults }
  889. if hs='' then
  890. begin
  891. {$ifdef UNIX}
  892. if FileExists('/usr/local/bin/ppc386') then
  893. begin
  894. s:=ExtractFilePath(ReadLink('/usr/local/bin/ppc386'));
  895. if s<>'' then
  896. begin
  897. if s[length(s)]='/' then
  898. delete(s,length(s),1);
  899. hs:=SubstVariables('$(wildcard $(addprefix '+s+'/,rtl units))');
  900. end;
  901. end;
  902. if hs='' then
  903. begin
  904. if FileExists('/usr/bin/ppc386') then
  905. begin
  906. s:=ExtractFilePath(ReadLink('/usr/bin/ppc386'));
  907. if s<>'' then
  908. begin
  909. if s[length(s)]='/' then
  910. delete(s,length(s),1);
  911. hs:=SubstVariables('$(wildcard $(addprefix '+s+'/,rtl units))');
  912. end;
  913. end;
  914. end;
  915. {$else UNIX}
  916. hs:=ExtractFilePath(FSearch('ppc386.exe',getenv('PATH')));
  917. if hs<>'' then
  918. begin
  919. s:=hs+'/..';
  920. hs:=SubstVariables('$(wildcard $(addprefix '+s+'/,rtl units))');
  921. if hs='' then
  922. begin
  923. s:=s+'/..';
  924. hs:=SubstVariables('$(wildcard $(addprefix '+s+'/,rtl units))');
  925. end;
  926. end;
  927. if hs='' then
  928. s:='c:/pp';
  929. {$endif UNIX}
  930. end;
  931. SetVariable('FPCDIR',s,false);
  932. { PACKAGESDIR }
  933. if GetVariable('PACKAGESDIR',false)='' then
  934. SetVariable('PACKAGESDIR','$(FPCDIR)/packages',false);
  935. { UNITSDIR }
  936. if GetVariable('UNITSDIR',false)='' then
  937. SetVariable('UNITSDIR','$(FPCDIR)/units/$(TARGET)',false);
  938. Verbose(FPCMakeDebug,s_globals);
  939. Variables.Foreach(@PrintDic);
  940. end;
  941. function TFPCMake.SubstVariables(const s:string):string;
  942. function Expect(var s:string;c:char):boolean;
  943. begin
  944. if (s<>'') and (s[1]=c) then
  945. begin
  946. Delete(s,1,1);
  947. Result:=true;
  948. end
  949. else
  950. begin
  951. Verbose(FPCMakeError,'Error "'+c+'" expected');
  952. Result:=false;
  953. end;
  954. end;
  955. function GetVar(var s:string;untilc:char):string;
  956. var
  957. i,j,k : integer;
  958. first : boolean;
  959. func,
  960. tok,s1,s2,s3 : string;
  961. Sec : TFPCMakeSection;
  962. begin
  963. Result:='';
  964. repeat
  965. j:=Pos(untilc,s);
  966. if j=0 then
  967. j:=Length(s)+1;
  968. i:=Pos('$(',s);
  969. if (j<i) or (i=0) then
  970. break;
  971. Result:=Result+Copy(s,1,i-1);
  972. Delete(s,1,i+1);
  973. { Maybe Function ? }
  974. j:=Pos(')',s);
  975. if j=0 then
  976. j:=Length(s)+1;
  977. i:=Pos(' ',s);
  978. if i=0 then
  979. i:=Length(s)+1;
  980. if i<j then
  981. begin
  982. { It's a function }
  983. Func:=Copy(s,1,i-1);
  984. //writeln('func: ',func);
  985. { $(wildcard(<list>) }
  986. if Func='wildcard' then
  987. begin
  988. Delete(s,1,9);
  989. s1:=GetVar(s,')');
  990. Expect(s,')');
  991. first:=true;
  992. repeat
  993. tok:=GetToken(s1,' ');
  994. if tok='' then
  995. break;
  996. if PathOrFileExists(tok) then
  997. begin
  998. if not first then
  999. Result:=Result+' '
  1000. else
  1001. first:=false;
  1002. Result:=Result+tok;
  1003. end;
  1004. until false;
  1005. end
  1006. { $(addprefix(<suffix>,<list>) }
  1007. else if Func='addprefix' then
  1008. begin
  1009. Delete(s,1,10);
  1010. s1:=GetVar(s,',');
  1011. if Expect(s,',') then
  1012. begin
  1013. s2:=GetVar(s,')');
  1014. Expect(s,')');
  1015. end;
  1016. first:=true;
  1017. repeat
  1018. tok:=GetToken(s2,' ');
  1019. if tok='' then
  1020. break;
  1021. if not first then
  1022. Result:=Result+' '
  1023. else
  1024. first:=false;
  1025. Result:=Result+s1+tok;
  1026. until false;
  1027. end
  1028. { $(addsuffix(<suffix>,<list>) }
  1029. else if Func='addsuffix' then
  1030. begin
  1031. Delete(s,1,10);
  1032. s1:=GetVar(s,',');
  1033. if Expect(s,',') then
  1034. begin
  1035. s2:=GetVar(s,')');
  1036. Expect(s,')');
  1037. end;
  1038. first:=true;
  1039. repeat
  1040. tok:=GetToken(s2,' ');
  1041. if tok='' then
  1042. break;
  1043. if not first then
  1044. Result:=Result+' '
  1045. else
  1046. first:=false;
  1047. Result:=Result+tok+s1;
  1048. until false;
  1049. end;
  1050. end
  1051. else
  1052. begin
  1053. s2:=Copy(s,1,j-1);
  1054. Delete(s,1,j);
  1055. k:=pos('_',s2);
  1056. if k>0 then
  1057. begin
  1058. s3:=LowerCase(Copy(s2,k+1,Length(s2)-k));
  1059. s2:=LowerCase(Copy(s2,1,k-1));
  1060. Sec:=TFPCMakeSection(Section[s2]);
  1061. if assigned(Sec) then
  1062. s2:=Sec[s3]
  1063. else
  1064. s2:='';
  1065. end
  1066. else
  1067. s2:=Variables[s2];
  1068. Insert(s2,s,1);
  1069. end;
  1070. until false;
  1071. Result:=Result+Copy(s,1,j-1);
  1072. Delete(s,1,j-1);
  1073. end;
  1074. var
  1075. s1 : string;
  1076. begin
  1077. //writeln('S: ',s);
  1078. s1:=s;
  1079. Result:=GetVar(s1,#0);
  1080. //writeln('R: ',result);
  1081. end;
  1082. function TFPCMake.GetVariable(const inivar:string;dosubst:boolean):string;
  1083. var
  1084. Sec : TFPCMakeSection;
  1085. Dic : TKeyValue;
  1086. i : integer;
  1087. begin
  1088. Result:='';
  1089. i:=Pos('_',inivar);
  1090. if i<>0 then
  1091. begin
  1092. Sec:=TFPCMakeSection(FSections[Copy(Inivar,1,i-1)]);
  1093. if assigned(Sec) then
  1094. begin
  1095. if not assigned(Sec.Dictionary) then
  1096. Sec.ParseIni;
  1097. Dic:=TKeyValue(Sec.Dictionary);
  1098. Result:=Dic[Copy(IniVar,i+1,Length(IniVar)-i)];
  1099. end
  1100. else
  1101. exit;
  1102. end
  1103. else
  1104. Result:=Variables[IniVar];
  1105. { Substition asked ? }
  1106. if dosubst then
  1107. Result:=SubstVariables(Result);
  1108. end;
  1109. function TFPCMake.SetVariable(const inivar,value:string;add:boolean):string;
  1110. var
  1111. Sec : TFPCMakeSection;
  1112. P : TKeyValueItem;
  1113. i : integer;
  1114. key : string;
  1115. begin
  1116. Result:='';
  1117. i:=Pos('_',inivar);
  1118. if i<>0 then
  1119. begin
  1120. Sec:=TFPCMakeSection(FSections[Copy(Inivar,1,i-1)]);
  1121. if Sec=nil then
  1122. Sec:=TFPCMakeSection(FSections.Insert(TFPCMakeSection.CreateKeyValue(Copy(Inivar,1,i-1))));
  1123. key:=Copy(IniVar,i+1,Length(IniVar)-i);
  1124. p:=TKeyValueItem(Sec.Dictionary.Search(Key));
  1125. if assigned(p) then
  1126. begin
  1127. if Add then
  1128. AddToken(p.FValue,Value,' ')
  1129. else
  1130. p.Value:=Value;
  1131. end
  1132. else
  1133. TKeyValue(Sec.Dictionary).Add(key,value);
  1134. end
  1135. else
  1136. Variables[IniVar]:=value;
  1137. end;
  1138. procedure TFPCMake.ParseSec(p:TDictionaryItem);
  1139. begin
  1140. TFPCMakeSection(p).ParseIni;
  1141. end;
  1142. procedure TFPCMake.PrintSec(p:TDictionaryItem);
  1143. var
  1144. i : integer;
  1145. begin
  1146. with TFPCMakeSection(p) do
  1147. begin
  1148. Verbose(FPCMakeDebug,'['+Name+']');
  1149. if assigned(FList) then
  1150. begin
  1151. Verbose(FPCMakeDebug,' List:');
  1152. for i:=0 to FList.Count-1 do
  1153. Verbose(FPCMakeDebug,' "'+FList[i]+'"');
  1154. if assigned(FDictionary) then
  1155. Verbose(FPCMakeDebug,'');
  1156. end;
  1157. if assigned(FDictionary) then
  1158. begin
  1159. Verbose(FPCMakeDebug,' Dictionary:');
  1160. FDictionary.Foreach(@PrintDic);
  1161. end;
  1162. end;
  1163. end;
  1164. procedure TFPCMake.PrintDic(p:TDictionaryItem);
  1165. begin
  1166. with TKeyValueItem(p) do
  1167. begin
  1168. Verbose(FPCMakeDebug,' '+name+' = "'+value+'"');
  1169. end;
  1170. end;
  1171. procedure TFPCMake.Print;
  1172. begin
  1173. { global variables }
  1174. Verbose(FPCMakeDebug,'[global variables]');
  1175. Verbose(FPCMakeDebug,' Dictionary:');
  1176. Variables.Foreach(@PrintDic);
  1177. { sections }
  1178. FSections.Foreach(@PrintSec);
  1179. end;
  1180. function TFPCMake.GetSec(const AName:string):TDictionaryItem;
  1181. begin
  1182. GetSec:=FSections.Search(AName);
  1183. end;
  1184. end.
  1185. {
  1186. $Log$
  1187. Revision 1.11 2001-08-02 20:50:29 peter
  1188. * -T<target> support
  1189. * better error reporting for not found dirs
  1190. * some cleanups and nicer strings
  1191. Revision 1.10 2001/07/31 22:02:32 peter
  1192. * install Package.fpc
  1193. Revision 1.9 2001/07/24 09:06:40 pierre
  1194. + added amiga and atari targets
  1195. Revision 1.8 2001/07/13 21:01:59 peter
  1196. * cygdrive support
  1197. * fixed cygwin detection
  1198. * fixed some duplicate and extraeous spaces
  1199. Revision 1.7 2001/06/04 21:42:57 peter
  1200. * Arguments added
  1201. * Start of Package.fpc creation
  1202. Revision 1.6 2001/06/02 19:20:24 peter
  1203. * beos target added
  1204. Revision 1.5 2001/02/22 21:11:24 peter
  1205. * fpcdir detection added
  1206. * fixed loading of variables in fpcmake itself
  1207. Revision 1.4 2001/02/05 20:44:56 peter
  1208. * variable substition like GNU Make. wildcard,addprefix,addsuffix
  1209. already implemented
  1210. Revision 1.3 2001/02/01 22:00:10 peter
  1211. * default.fpcdir is back
  1212. * subdir requirement checking works, but not very optimal yet as
  1213. it can load the same Makefile.fpc multiple times
  1214. Revision 1.2 2001/01/29 21:49:10 peter
  1215. * lot of updates
  1216. Revision 1.1 2001/01/24 21:59:36 peter
  1217. * first commit of new fpcmake
  1218. }