fpcmmain.pp 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634
  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. {$ifdef linux}
  20. {$ifndef BSD}
  21. linux,
  22. {$endif}
  23. {$endif}
  24. {$ifdef BSD}
  25. Linux,
  26. {$endif}
  27. {$else}
  28. baseunix,
  29. unix,
  30. {$endif}
  31. {$endif}
  32. sysutils,classes,
  33. fpcmdic;
  34. {$ifdef BEOS}
  35. {$define NO_UNIX_UNIT}
  36. {$endif}
  37. {$ifdef SUNOS}
  38. {$define NO_UNIX_UNIT}
  39. {$endif}
  40. {$ifdef QNX}
  41. {$define NO_UNIX_UNIT}
  42. {$endif}
  43. const
  44. Version='1.1';
  45. Title='FPCMake Version '+Version;
  46. TitleDate=Title+' ['+{$ifdef fpc}{$i %DATE}{$else}'n/a'{$endif}+']';
  47. type
  48. TCpu=(
  49. c_i386,c_m68k,c_powerpc,c_sparc,c_x86_64,c_arm
  50. );
  51. TCpuSet=set of TCpu;
  52. TTarget=(
  53. t_linux,t_go32v2,t_win32,t_os2,t_freebsd,t_beos,t_netbsd,
  54. t_amiga,t_atari, t_sunos, t_qnx, t_netware, t_openbsd,t_wdosx,
  55. t_palmos,t_macos,t_darwin,t_emx,t_watcom,t_morphos
  56. );
  57. TTargetSet=set of TTarget;
  58. const
  59. CpuStr : array[TCpu] of string=(
  60. 'i386','m68k','powerpc','sparc','x86_64','arm'
  61. );
  62. CpuSuffix : array[TCpu] of string=(
  63. '_i386','_m68k','_powerpc','_sparc','_x86_64','_arm'
  64. );
  65. TargetStr : array[TTarget] of string=(
  66. 'linux','go32v2','win32','os2','freebsd','beos','netbsd',
  67. 'amiga','atari','sunos', 'qnx', 'netware','openbsd','wdosx',
  68. 'palmos','macos','darwin','emx','watcom','morphos'
  69. );
  70. TargetSuffix : array[TTarget] of string=(
  71. '_linux','_go32v2','_win32','_os2','_freebsd','_beos','_netbsd',
  72. '_amiga','_atari','_sunos', '_qnx', '_netware','_openbsd','_wdosx',
  73. '_palmos','_macos','_darwin','_emx','_watcom','_morphos'
  74. );
  75. TargetCpuPossible : array[TTarget,TCpu] of boolean = (
  76. { os i386 m68k ppc sparc x86_64 arm }
  77. { linux } ( true, true, true, true, true, true),
  78. { go32v2 } ( true, false, false, false, false, false),
  79. { win32 } ( true, false, false, false, false, false),
  80. { os2 } ( true, false, false, false, false, false),
  81. { freebsd } ( true, true, false, false, true, false),
  82. { beos } ( true, false, false, false, false, false),
  83. { netbsd } ( true, true, true, true, false, false),
  84. { amiga } ( false, true, false, false, false, false),
  85. { atari } ( false, true, false, false, false, false),
  86. { sunos } ( true, false, false, true, false, false),
  87. { qnx } ( true, false, false, false, false, false),
  88. { netware } ( true, false, false, false, false, false),
  89. { openbsd } ( true, true, false, false, false, false),
  90. { wdosx } ( true, false, false, false, false, false),
  91. { palmos } ( false, true, false, false, false, false),
  92. { macos } ( false, false, true, false, false, false),
  93. { darwin } ( false, false, true, false, false, false),
  94. { emx } ( true, false, false, false, false, false),
  95. { watcom } ( true, false, false, false ,false, false),
  96. { morphos } ( false, false, true, false ,false, false)
  97. );
  98. type
  99. TKeyValueItem = class(TDictionaryItem)
  100. private
  101. FValue : string;
  102. public
  103. constructor Create(const k,v:string);
  104. property Value:string read FValue write FValue;
  105. end;
  106. TKeyValue = class(TDictionary)
  107. private
  108. function GetKey(const k:string):string;
  109. public
  110. procedure Add(const k,v:String);
  111. property Key[const s:string]:string read GetKey write Add;default;
  112. end;
  113. TFPCMakeSection = class(TDictionaryItem)
  114. private
  115. FList : TStringList;
  116. FDictionary : TKeyValue;
  117. procedure BuildIniDic(p:TDictionaryItem);
  118. procedure BuildMakefileDic(p:TDictionaryItem);
  119. function GetKey(const k:string):string;
  120. public
  121. constructor Create(const n:string);
  122. constructor CreateKeyValue(const n:string);
  123. destructor Destroy;override;
  124. procedure AddLine(const s:string);
  125. procedure AddKey(const k,v:string);
  126. procedure Clear;
  127. procedure ParseIni;
  128. procedure BuildIni;
  129. procedure BuildMakefile;
  130. property Key[const s:string]:string read GetKey;default;
  131. property List:TStringList read FList;
  132. property Dictionary:TKeyValue read FDictionary;
  133. end;
  134. TTargetRequireList = array[ttarget,tcpu] of TStringList;
  135. TFPCMakeVerbose = (FPCMakeError, FPCMakeInfo, FPCMakeDebug);
  136. TFPCMake = class
  137. private
  138. FStream : TStream;
  139. FFileName : string;
  140. FCommentChars : TSysCharSet;
  141. FEmptyLines : boolean;
  142. FSections : TDictionary;
  143. FPackageSec,
  144. FExportSec : TFPCMakeSection;
  145. FUsesLCL,
  146. FIsPackage : boolean;
  147. FPackageName,
  148. FPackageVersion,
  149. FPackageTargets : string;
  150. FRequireList : TTargetRequireList;
  151. FVariables : TKeyValue;
  152. FIncludeTargets : TTargetSet;
  153. FIncludeCpus : TCpuSet;
  154. procedure Init;
  155. procedure ParseSec(p:TDictionaryItem);
  156. procedure PrintSec(p:TDictionaryItem);
  157. procedure PrintDic(p:TDictionaryItem);
  158. function GetSec(const AName:string):TDictionaryItem;
  159. procedure LoadRequiredPackage(t:TTarget;c:TCpu;const ReqName,ReqVersion:string);
  160. procedure LoadRequiredDir(t:TTarget;c:TCpu;const MainPack,currdir,subdir:string);
  161. procedure LoadRequires(t:Ttarget;c:TCpu;FromFPCMake:TFPCMake);
  162. function CopySection(Sec:TFPCMakeSection;Secname:string):TFPCMakeSection;
  163. protected
  164. VerboseIdent : string;
  165. public
  166. constructor Create(const AFileName:string);
  167. constructor CreateFromStream(s:TStream;const AFileName:string);
  168. destructor Destroy;override;
  169. procedure Verbose(lvl:TFPCMakeVerbose;const s:string);virtual;
  170. procedure SetTargets(const s:string);
  171. procedure LoadSections;
  172. procedure LoadMakefileFPC;
  173. procedure LoadPackageSection;
  174. procedure LoadRequireSection;
  175. function GetTargetRequires(t:TTarget;c:TCpu):TStringList;
  176. function CheckLibcRequire:boolean;
  177. procedure CreateExportSection;
  178. procedure AddFPCDefaultVariables;
  179. procedure AddLCLDefaultVariables;
  180. function SubstVariables(const s:string):string;
  181. function GetVariable(const inivar:string;dosubst:boolean):string;
  182. function SetVariable(const inivar,value:string;add:boolean):string;
  183. procedure Print;
  184. property Section[const s:string]:TDictionaryItem read GetSec;default;
  185. property RequireList:TTargetRequireList read FRequireList;
  186. property Variables:TKeyValue read FVariables;
  187. property UsesLCL:boolean read FUsesLCL;
  188. property IsPackage:boolean read FIsPackage;
  189. property PackageName:string read FPackageName;
  190. property PackageVersion:string read FPackageVersion;
  191. property PackageSec:TFPCMakeSection read FPackageSec;
  192. property ExportSec:TFPCMakeSection read FExportSec;
  193. property CommentChars:TSysCharSet read FCommentChars write FCommentChars;
  194. property EmptyLines:Boolean read FEmptyLines write FEmptyLines;
  195. property IncludeTargets:TTargetSet read FIncludeTargets write FIncludeTargets;
  196. property IncludeCpus:TCpuSet read FIncludeCpus write FIncludeCpus;
  197. end;
  198. function posidx(const substr,s : string;idx:integer):integer;
  199. function GetToken(var s:string;sep:char):string;
  200. procedure AddToken(var s:string;const tok:string;sep:char);
  201. procedure AddTokenNoDup(var s:string;const s2:string;sep:char);
  202. implementation
  203. resourcestring
  204. s_not_list_sec='Not a list section "%s"';
  205. s_not_key_value_sec='Not a key-value section "%s"';
  206. s_err_section_start='%s:%d: Wrong section start';
  207. s_err_not_key_value='Parse error key=value excepted: "%s"';
  208. s_err_no_section='%s:%d: Entries without section';
  209. s_no_package_name='No package name set';
  210. s_no_package_version='No package version set';
  211. s_err_require_format='Wrong require format "%s"';
  212. s_wrong_package_name='Package name "%s" expected, but "%s" found';
  213. s_wrong_package_version='Package version "%s" expected, but version "%s" found';
  214. s_directory_not_found='Directory "%s" not found';
  215. s_makefilefpc_not_found='No Makefile.fpc found in directory "%s"';
  216. s_package_not_found='Target "%s", package "%s" not found';
  217. s_fpcmake_version_required='FPCMake version "%s" is required';
  218. s_no_targets_set='No targets set';
  219. s_targets_info='Targets: "%s"';
  220. s_globals='Globals:';
  221. {****************************************************************************
  222. Helpers
  223. ****************************************************************************}
  224. Function PathExists ( F : String) : Boolean;
  225. Var
  226. Info : TSearchRec;
  227. begin
  228. if F[Length(f)] in ['/','\'] then
  229. Delete(f,length(f),1);
  230. PathExists:=(findfirst(F,faAnyFile,info)=0) and
  231. ((info.attr and fadirectory)=fadirectory);
  232. findclose(Info);
  233. end;
  234. Function PathOrFileExists ( F : String) : Boolean;
  235. Var
  236. Info : Dos.SearchRec;
  237. begin
  238. if F[Length(f)] in ['/','\'] then
  239. Delete(f,length(f),1);
  240. dos.findfirst(f,fareadonly+faarchive+fahidden+fadirectory,info);
  241. PathOrFileExists:=(Doserror=0);
  242. dos.findclose(Info);
  243. end;
  244. function posidx(const substr,s : string;idx:integer):integer;
  245. var
  246. i,j : integer;
  247. e : boolean;
  248. begin
  249. i:=idx;
  250. j:=0;
  251. e:=(length(SubStr)>0);
  252. while e and (i<=Length(s)-Length(SubStr)) do
  253. begin
  254. inc(i);
  255. if (SubStr[1]=s[i]) and (Substr=Copy(s,i,Length(SubStr))) then
  256. begin
  257. j:=i;
  258. e:=false;
  259. end;
  260. end;
  261. PosIdx:=j;
  262. end;
  263. function GetToken(var s:string;sep:char):string;
  264. var
  265. i : integer;
  266. begin
  267. s:=Trim(s);
  268. i:=pos(sep,s);
  269. if i=0 then
  270. begin
  271. Result:=s;
  272. s:='';
  273. end
  274. else
  275. begin
  276. Result:=Copy(s,1,i-1);
  277. Delete(s,1,i);
  278. end;
  279. end;
  280. procedure AddToken(var s:string;const tok:string;sep:char);
  281. begin
  282. if tok='' then
  283. exit;
  284. if s<>'' then
  285. s:=s+sep+tok
  286. else
  287. s:=tok;
  288. end;
  289. procedure AddTokenNoDup(var s:string;const s2:string;sep:char);
  290. var
  291. i,idx : integer;
  292. again,add : boolean;
  293. begin
  294. add:=false;
  295. idx:=0;
  296. repeat
  297. again:=false;
  298. i:=posidx(s2,s,idx);
  299. if (i=0) then
  300. add:=true
  301. else
  302. if (i=1) then
  303. begin
  304. if (length(s)>length(s2)) and
  305. (s[length(s2)+1]<>sep) then
  306. add:=true;
  307. end
  308. else
  309. if (i>1) and
  310. ((s[i-1]<>sep) or
  311. ((length(s)>=i+length(s2)) and (s[i+length(s2)]<>sep))) then
  312. begin
  313. idx:=i+length(s2);
  314. again:=true;
  315. end;
  316. until not again;
  317. if add then
  318. begin
  319. if s='' then
  320. s:=s2
  321. else
  322. s:=s+sep+s2;
  323. end;
  324. end;
  325. {****************************************************************************
  326. TKeyValueItem
  327. ****************************************************************************}
  328. constructor TKeyValueItem.Create(const k,v:string);
  329. begin
  330. inherited Create(k);
  331. value:=v;
  332. end;
  333. {****************************************************************************
  334. TKeyValue
  335. ****************************************************************************}
  336. function TKeyValue.GetKey(const k:string):string;
  337. var
  338. p : TKeyValueItem;
  339. begin
  340. p:=TKeyValueItem(Search(k));
  341. if p=nil then
  342. GetKey:=''
  343. else
  344. GetKey:=p.Value;
  345. end;
  346. procedure TKeyValue.Add(const k,v:string);
  347. var
  348. p : TKeyValueItem;
  349. begin
  350. p:=TKeyValueItem(Search(k));
  351. if p=nil then
  352. begin
  353. p:=TKeyValueItem.Create(k,v);
  354. Insert(p);
  355. end
  356. else
  357. p.Value:=v;
  358. end;
  359. {****************************************************************************
  360. TFPCMakeSection
  361. ****************************************************************************}
  362. constructor TFPCMakeSection.Create(const n:string);
  363. begin
  364. inherited Create(n);
  365. FList:=TStringList.Create;
  366. FDictionary:=nil;
  367. end;
  368. constructor TFPCMakeSection.CreateKeyValue(const n:string);
  369. begin
  370. inherited Create(n);
  371. FList:=nil;
  372. FDictionary:=TKeyValue.Create;
  373. end;
  374. destructor TFPCMakeSection.Destroy;
  375. begin
  376. inherited Destroy;
  377. FList.Free;
  378. FDictionary.Free;
  379. end;
  380. procedure TFPCMakeSection.Clear;
  381. begin
  382. FList.Free;
  383. FList:=TStringList.Create;
  384. FDictionary.Free;
  385. FDictionary:=nil;
  386. end;
  387. procedure TFPCMakeSection.AddLine(const s:string);
  388. begin
  389. if FList=nil then
  390. raise Exception.Create(Format(s_not_list_sec,[Name]));
  391. FList.Add(s);
  392. end;
  393. procedure TFPCMakeSection.AddKey(const k,v:string);
  394. begin
  395. if FDictionary=nil then
  396. raise Exception.Create(Format(s_not_key_value_sec,[Name]));
  397. { Don't add empty values }
  398. if v<>'' then
  399. FDictionary.Add(k,v);
  400. end;
  401. function TFPCMakeSection.GetKey(const k:string):string;
  402. begin
  403. if FDictionary=nil then
  404. raise Exception.Create(Format(s_not_key_value_sec,[Name]));
  405. GetKey:=FDictionary[k];
  406. end;
  407. procedure TFPCMakeSection.ParseIni;
  408. var
  409. p : TKeyValueItem;
  410. i,j,len,maxi : integer;
  411. s,newkey,value : string;
  412. begin
  413. { If already processed skip }
  414. if assigned(FDictionary) then
  415. exit;
  416. { Don't process rules section }
  417. if (Name='prerules') or (Name='rules') then
  418. exit;
  419. { Parse the section }
  420. FDictionary:=TKeyValue.Create;
  421. { Parse the list }
  422. maxi:=FList.Count;
  423. i:=0;
  424. while (i<maxi) do
  425. begin
  426. s:=Trim(FList[i]);
  427. len:=Length(s);
  428. { Concat lines ending with \ }
  429. while s[len]='\' do
  430. begin
  431. Delete(s,len,1);
  432. if i+1<maxi then
  433. begin
  434. inc(i);
  435. s:=s+Trim(FList[i]);
  436. len:=Length(s);
  437. end;
  438. end;
  439. { Parse key=value line }
  440. j:=0;
  441. while (j<len) and (s[j+1] in ['A'..'Z','a'..'z','0'..'9','_']) do
  442. inc(j);
  443. NewKey:=Copy(s,1,j);
  444. While (j<len) and (s[j+1] in [' ',#9]) do
  445. inc(j);
  446. inc(j);
  447. if s[j]<>'=' then
  448. Raise Exception.Create(Format(s_err_not_key_value,[s]));
  449. While (j<len) and (s[j+1] in [' ',#9]) do
  450. inc(j);
  451. Value:=Copy(s,j+1,len-j);
  452. p:=TKeyValueItem(FDictionary.Search(NewKey));
  453. { Concat values if key already exists }
  454. if assigned(p) then
  455. AddToken(p.FValue,Value,' ')
  456. else
  457. FDictionary.Add(NewKey,Value);
  458. inc(i);
  459. end;
  460. { List is not used anymore }
  461. FList.Free;
  462. FList:=nil;
  463. end;
  464. procedure TFPCMakeSection.BuildIniDic(p:TDictionaryItem);
  465. begin
  466. with TKeyValueItem(p) do
  467. begin
  468. FList.Add(Name+'='+Value);
  469. end;
  470. end;
  471. procedure TFPCMakeSection.BuildIni;
  472. begin
  473. if assigned(FList) then
  474. exit;
  475. FList:=TStringList.Create;
  476. FDictionary.Foreach(@BuildIniDic);
  477. FDictionary.Free;
  478. FDictionary:=nil;
  479. end;
  480. procedure TFPCMakeSection.BuildMakefileDic(p:TDictionaryItem);
  481. begin
  482. FList.Add(Uppercase(Name+'_'+TKeyValueItem(p).Name)+'='+TKeyValueItem(p).Value);
  483. end;
  484. procedure TFPCMakeSection.BuildMakefile;
  485. begin
  486. if assigned(FList) then
  487. exit;
  488. FList:=TStringList.Create;
  489. FDictionary.Foreach(@BuildMakefileDic);
  490. FDictionary.Free;
  491. FDictionary:=nil;
  492. end;
  493. {****************************************************************************
  494. TFPCMake
  495. ****************************************************************************}
  496. constructor TFPCMake.Create(const AFileName:string);
  497. begin
  498. FFileName:=AFileName;
  499. FStream:=nil;
  500. Init;
  501. end;
  502. constructor TFPCMake.CreateFromStream(s:TStream;const AFileName:string);
  503. begin
  504. FFileName:=AFileName;
  505. FStream:=s;
  506. Init;
  507. end;
  508. procedure TFPCMake.Init;
  509. var
  510. t : ttarget;
  511. c : tcpu;
  512. begin
  513. FSections:=TDictionary.Create;
  514. for t:=low(ttarget) to high(ttarget) do
  515. for c:=low(tcpu) to high(tcpu) do
  516. FRequireList[t,c]:=TStringList.Create;
  517. FVariables:=TKeyValue.Create;
  518. FCommentChars:=[';','#'];
  519. FEmptyLines:=false;
  520. FIsPackage:=false;
  521. FPackageName:='';
  522. FPackageVersion:='';
  523. FPackageSec:=nil;
  524. FExportSec:=nil;
  525. FIncludeTargets:=[low(TTarget)..high(TTarget)];
  526. FIncludeCpus:=[low(TCpu)..high(TCpu)];
  527. VerboseIdent:='';
  528. FUsesLCL:=false;
  529. end;
  530. destructor TFPCMake.Destroy;
  531. var
  532. t : ttarget;
  533. c : tcpu;
  534. begin
  535. FSections.Free;
  536. for t:=low(ttarget) to high(ttarget) do
  537. for c:=low(tcpu) to high(tcpu) do
  538. FRequireList[t,c].Free;
  539. FVariables.Free;
  540. end;
  541. procedure TFPCMake.LoadSections;
  542. var
  543. SLInput : TStringList;
  544. i,j,n : integer;
  545. s,
  546. SecName : string;
  547. CurrSec : TFPCMakeSection;
  548. begin
  549. try
  550. CurrSec:=nil;
  551. SLInput:=TStringList.Create;
  552. if assigned(FStream) then
  553. SLInput.LoadFromStream(FStream)
  554. else
  555. SLInput.LoadFromFile(FFileName);
  556. { Load Input into sections list }
  557. n:=SLInput.Count;
  558. i:=0;
  559. while (i<n) do
  560. begin
  561. s:=Trim(SLInput[i]);
  562. if (EmptyLines and (s='')) or
  563. ((s<>'') and not(s[1] in FCommentChars)) then
  564. begin
  565. { section start? }
  566. if (s<>'') and (s[1]='[') then
  567. begin
  568. j:=pos(']',s);
  569. if j=0 then
  570. raise Exception.Create(Format(s_err_section_start,[FFileName,i+1]));
  571. SecName:=Copy(s,2,j-2);
  572. CurrSec:=TFPCMakeSection(FSections[SecName]);
  573. if CurrSec=nil then
  574. CurrSec:=TFPCMakeSection(FSections.Insert(TFPCMakeSection.Create(SecName)));
  575. end
  576. else
  577. begin
  578. if CurrSec=nil then
  579. raise Exception.Create(Format(s_err_no_section,[FFileName,i+1]));
  580. { Insert string without spaces stripped }
  581. CurrSec.AddLine(SLInput[i]);
  582. end;
  583. end;
  584. inc(i);
  585. end;
  586. finally
  587. SLInput.Free;
  588. end;
  589. end;
  590. function TFPCMake.CopySection(Sec:TFPCMakeSection;Secname:string):TFPCMakeSection;
  591. begin
  592. Result:=TFPCMakeSection(FSections[SecName]);
  593. if Sec=Nil then
  594. exit;
  595. { Clear old section or if not existing create new }
  596. if assigned(Result) then
  597. Result.Clear
  598. else
  599. Result:=TFPCMakeSection(FSections.Insert(TFPCMakeSection.Create(SecName)));
  600. Sec.BuildIni;
  601. Result.List.AddStrings(Sec.List);
  602. Result.ParseIni;
  603. Sec.ParseIni;
  604. end;
  605. procedure TFPCMake.LoadMakefileFPC;
  606. var
  607. s : string;
  608. begin
  609. LoadSections;
  610. { Parse all sections }
  611. FSections.Foreach(@ParseSec);
  612. { Load package section }
  613. LoadPackageSection;
  614. { Add some default variables like FPCDIR, UNITSDIR }
  615. AddFPCDefaultVariables;
  616. { Load LCL code ? }
  617. s:=GetVariable('require_packages',true);
  618. if (pos('lcl',s)>0) or (PackageName='lcl') then
  619. begin
  620. FUsesLCL:=true;
  621. AddLCLDefaultVariables;
  622. end;
  623. { Show globals }
  624. Verbose(FPCMakeDebug,s_globals);
  625. Variables.Foreach(@PrintDic);
  626. { Load required packages }
  627. LoadRequireSection;
  628. end;
  629. procedure TFPCMake.Verbose(lvl:TFPCMakeVerbose;const s:string);
  630. begin
  631. writeln(VerboseIdent,s);
  632. end;
  633. procedure TFPCMake.SetTargets(const s:string);
  634. var
  635. hslst : string;
  636. hs : string;
  637. t : TTarget;
  638. begin
  639. FIncludeTargets:=[];
  640. hslst:=s;
  641. repeat
  642. hs:=LowerCase(GetToken(hslst,','));
  643. if hs='' then
  644. break;
  645. { target 'all' includes all targets }
  646. if hs='all' then
  647. begin
  648. for t:=low(TTarget) to high(TTarget) do
  649. include(FIncludeTargets,t);
  650. break;
  651. end;
  652. for t:=low(TTarget) to high(TTarget) do
  653. if hs=TargetStr[t] then
  654. include(FIncludeTargets,t);
  655. until false;
  656. if FIncludeTargets=[] then
  657. raise Exception.Create(s_no_targets_set)
  658. else
  659. begin
  660. hs:='';
  661. for t:=low(TTarget) to high(TTarget) do
  662. if t in FIncludeTargets then
  663. AddToken(hs,TargetStr[t],' ');
  664. Verbose(FPCMakeDebug,Format(s_targets_info,[hs]));
  665. end;
  666. end;
  667. procedure TFPCMake.LoadPackageSection;
  668. var
  669. hs,s : string;
  670. t : TTarget;
  671. begin
  672. { Get package info from package section }
  673. FPackageSec:=TFPCMakeSection(FSections['package']);
  674. if FPackageSec=nil then
  675. exit;
  676. { Parse the section to key=value pairs }
  677. FPackageSec.ParseIni;
  678. { Are we a subpart of a package, then load that package }
  679. s:=FPackageSec['main'];
  680. if s<>'' then
  681. begin
  682. SetVariable('package_name',s,false);
  683. FPackageName:=s;
  684. end
  685. else
  686. begin
  687. { mandatory name }
  688. FPackageName:=FPackageSec['name'];
  689. if FPackageName='' then
  690. Raise Exception.Create(s_no_package_name);
  691. { mandatory version }
  692. FPackageVersion:=FPackageSec['version'];
  693. if FPackageVersion='' then
  694. Raise Exception.Create(s_no_package_version);
  695. FIsPackage:=true;
  696. { optional targets }
  697. FPackageTargets:='';
  698. s:=LowerCase(FPackageSec['targets']);
  699. repeat
  700. hs:=GetToken(s,' ');
  701. if hs='' then
  702. break;
  703. for t:=low(TTarget) to high(TTarget) do
  704. if hs=TargetStr[t] then
  705. begin
  706. AddToken(FPackageTargets,hs,' ');
  707. break;
  708. end;
  709. until false;
  710. { Set the ExportSec }
  711. FExportSec:=TFPCMakeSection(FSections[Lowercase(FPackageName)]);
  712. end;
  713. end;
  714. procedure TFPCMake.CreateExportSection;
  715. var
  716. t : TTarget;
  717. c : TCpu;
  718. begin
  719. { Don't create a section twice }
  720. if FExportSec<>nil then
  721. exit;
  722. { Look if we've already an own section, else create a new
  723. key-value section }
  724. FExportSec:=TFPCMakeSection(FSections[LowerCase(FPackageName)]);
  725. if FExportSec=nil then
  726. FExportSec:=TFPCMakeSection(FSections.Insert(TFPCMakeSection.CreateKeyValue(LowerCase(FPackageName))));
  727. { Add default the values to the export section }
  728. FExportSec.AddKey('name',FPackageName);
  729. FExportSec.AddKey('version',FPackageVersion);
  730. { Add required packages }
  731. for t:=low(TTarget) to high(TTarget) do
  732. for c:=low(TCpu) to high(TCpu) do
  733. FExportSec.AddKey('require'+TargetSuffix[t]+CpuSuffix[c],FPackageSec['require'+TargetSuffix[t]+CpuSuffix[c]]);
  734. { Unit dir }
  735. {FExportSec.AddKey('unitdir','$(UNITSDIR)/'+Lowercase(PackageName));}
  736. end;
  737. procedure TFPCMake.LoadRequiredPackage(t:TTarget;c:TCpu;const ReqName,ReqVersion:string);
  738. function TryFile(const fn:string):boolean;
  739. var
  740. ReqFPCMake : TFPCMake;
  741. begin
  742. TryFile:=false;
  743. if FileExists(fn) then
  744. begin
  745. VerboseIdent:=VerboseIdent+' ';
  746. Verbose(FPCMakeDebug,'Package '+ReqName+': '+fn);
  747. ReqFPCMake:=TFPCMake.Create(fn);
  748. ReqFPCMake.LoadSections;
  749. ReqFPCMake.LoadPackageSection;
  750. { Check package name and version }
  751. if LowerCase(ReqFPCMake.PackageName)<>ExtractFileName(ReqName) then
  752. raise Exception.Create(Format(s_wrong_package_name,[ReqName,LowerCase(ReqFPCMake.PackageName)]));
  753. if (ReqVersion<>'') and (ReqFPCMake.PackageVersion<ReqVersion) then
  754. raise Exception.Create(Format(s_wrong_package_version,[ReqVersion,ReqFPCMake.PackageVersion]));
  755. { First load the requirements of this package }
  756. LoadRequires(t,c,ReqFPCMake);
  757. { Get a copy of the package section }
  758. CopySection(ReqFPCMake.PackageSec,ReqName+'_package');
  759. { Get a copy of the export section }
  760. CopySection(ReqFPCMake.ExportSec,ReqName);
  761. { Get a copy of the require section }
  762. CopySection(TFPCMakeSection(ReqFPCMake['require']),ReqName+'_require');
  763. { Free }
  764. ReqFPCMake.Free;
  765. Delete(VerboseIdent,1,2);
  766. TryFile:=true;
  767. end;
  768. end;
  769. var
  770. s : string;
  771. begin
  772. { Force the current target }
  773. SetVariable('TARGET',TargetStr[t],false);
  774. { Check for Makefile.fpc }
  775. s:=SubstVariables('$(addsuffix /'+ReqName+'/Makefile.fpc,$(FPCDIR)) $(addsuffix /'+ReqName+'/Makefile.fpc,$(PACKAGESDIR)) $(addsuffix /'+ReqName+'/Makefile.fpc,$(REQUIRE_PACKAGESDIR))');
  776. Verbose(FPCMakeDebug,'Package "'+ReqName+'": Looking for Makefile.fpc: "'+s+'"');
  777. s:=SubstVariables('$(firstword $(wildcard '+s+'))');
  778. if TryFile(s) then
  779. exit;
  780. { Check for Package.fpc }
  781. s:=SubstVariables('$(addsuffix /'+ReqName+'/Package.fpc,$(FPCDIR)) $(addsuffix /'+ReqName+'/Package.fpc,$(UNITSDIR)) $(addsuffix /'+ReqName+'/Package.fpc,$(REQUIRE_UNITSDIR))');
  782. Verbose(FPCMakeDebug,'Package "'+ReqName+'": Looking for Package.fpc: "'+s+'"');
  783. s:=SubstVariables('$(firstword $(wildcard '+s+'))');
  784. if TryFile(s) then
  785. exit;
  786. Raise Exception.Create(Format(s_package_not_found,[TargetStr[t],Reqname]));
  787. end;
  788. procedure TFPCMake.LoadRequiredDir(t:TTarget;c:TCpu;const MainPack,currdir,subdir:string);
  789. var
  790. ReqFPCMake : TFPCMake;
  791. s : string;
  792. begin
  793. VerboseIdent:=VerboseIdent+' ';
  794. s:=currdir+subdir;
  795. Verbose(FPCMakeDebug,'Subdir: '+s+'/Makefile.fpc');
  796. if not FileExists(s+'/Makefile.fpc') then
  797. begin
  798. { give better error what is wrong }
  799. if not PathExists(s) then
  800. Raise Exception.Create(Format(s_directory_not_found,[s]))
  801. else
  802. Raise Exception.Create(Format(s_makefilefpc_not_found,[s]));
  803. end;
  804. { Process Makefile.fpc }
  805. ReqFPCMake:=TFPCMake.Create(currdir+subdir+'/Makefile.fpc');
  806. ReqFPCMake.LoadSections;
  807. ReqFPCMake.LoadPackageSection;
  808. { Are we a subpackage? }
  809. if (ReqFPCMake.GetVariable('package_name',false)<>MainPack) then
  810. begin
  811. ReqFPCMake.Free;
  812. Delete(VerboseIdent,1,2);
  813. exit;
  814. end;
  815. { Load the requirements of this package }
  816. LoadRequires(t,c,ReqFPCMake);
  817. { Add the current requirements to our parents requirements }
  818. s:=Trim(ReqFPCMake.GetVariable('require_packages',true)+' '+
  819. ReqFPCMake.GetVariable('require_packages'+targetsuffix[t],true)+' '+
  820. ReqFPCMake.GetVariable('require_packages'+targetsuffix[t]+cpusuffix[c],true));
  821. SetVariable('require_packages'+targetsuffix[t]+cpusuffix[c],s,true);
  822. if ReqFPCMake.GetVariable('require_libc',false)<>'' then
  823. SetVariable('require_libc','y',false);
  824. { Free }
  825. ReqFPCMake.Free;
  826. Delete(VerboseIdent,1,2);
  827. end;
  828. procedure TFPCMake.LoadRequires(t:Ttarget;c:TCpu;FromFPCMake:TFPCMake);
  829. var
  830. s,
  831. ReqDir,
  832. ReqName,
  833. ReqVersion : string;
  834. i,j : integer;
  835. begin
  836. { packages }
  837. s:=Trim(FromFPCMake.GetVariable('require_packages',true)+' '+
  838. FromFPCMake.GetVariable('require_packages'+TargetSuffix[t],true)+' '+
  839. FromFPCMake.GetVariable('require_packages'+TargetSuffix[t]+CpuSuffix[c],true));
  840. Verbose(FPCMakeDebug,'Required packages for '+TargetStr[t]+'-'+CpuStr[c]+': '+s);
  841. repeat
  842. reqname:=GetToken(s,' ');
  843. if reqname='' then
  844. break;
  845. i:=Pos('(',ReqName);
  846. if i>0 then
  847. begin
  848. j:=Pos(')',ReqName);
  849. if (i=1) or (j=0) then
  850. Raise Exception.Create(Format(s_err_require_format,[ReqName]));
  851. ReqVersion:=Copy(ReqName,i+1,j-i-1);
  852. ReqName:=Copy(ReqName,1,i-1);
  853. end
  854. else
  855. ReqVersion:='';
  856. { We only use lowercase names }
  857. ReqName:=Lowercase(ReqName);
  858. { Already loaded ? }
  859. if (RequireList[t,c].IndexOf(ReqName)=-1) then
  860. begin
  861. LoadRequiredPackage(t,c,ReqName,ReqVersion);
  862. RequireList[t,c].Add(ReqName);
  863. end;
  864. until false;
  865. { sub dirs }
  866. s:=Trim(FromFPCMake.GetVariable('target_dirs',true)+' '+
  867. FromFPCMake.GetVariable('target_dirs'+TargetSuffix[t],true)+' '+
  868. FromFPCMake.GetVariable('target_dirs'+TargetSuffix[t]+CpuSuffix[c],true));
  869. Verbose(FPCMakeDebug,'Required dirs for '+TargetStr[t]+'-'+CpuStr[c]+': '+s);
  870. repeat
  871. reqdir:=GetToken(s,' ');
  872. if reqdir='' then
  873. break;
  874. LoadRequiredDir(t,c,FromFPCMake.FPackageName,ExtractFilePath(FromFPCMake.FFileName),ReqDir)
  875. until false;
  876. end;
  877. procedure TFPCMake.LoadRequireSection;
  878. function CheckVar(const s:string):boolean;
  879. var
  880. t : ttarget;
  881. c : tcpu;
  882. begin
  883. result:=false;
  884. if GetVariable(s,false)<>'' then
  885. begin
  886. result:=true;
  887. exit;
  888. end;
  889. for t:=low(ttarget) to high(ttarget) do
  890. if t in FIncludeTargets then
  891. begin
  892. if GetVariable(s+targetsuffix[t],false)<>'' then
  893. begin
  894. result:=true;
  895. exit;
  896. end;
  897. for c:=low(tcpu) to high(tcpu) do
  898. if (TargetCpuPossible[t,c]) and (c in FIncludeCpus) then
  899. begin
  900. result:=true;
  901. exit;
  902. end;
  903. end;
  904. end;
  905. var
  906. s : string;
  907. t : ttarget;
  908. c : tcpu;
  909. begin
  910. { Check FPCMake version }
  911. s:=GetVariable('require_fpcmake',false);
  912. if (s>version) then
  913. raise Exception.Create(Format(s_fpcmake_version_required,[s]));
  914. { Maybe add an implicit rtl dependency if there is something
  915. to compile }
  916. s:=GetVariable('require_packages',false);
  917. if (GetVariable('require_nortl',false)='') and
  918. (CheckVar('target_programs') or
  919. CheckVar('target_units') or
  920. CheckVar('target_examples')) and
  921. (Pos('rtl(',s)=0) then
  922. begin
  923. s:='rtl '+s;
  924. SetVariable('require_packages',s,false);
  925. end;
  926. { Load recursively all required packages starting with this Makefile.fpc }
  927. for t:=low(TTarget) to high(TTarget) do
  928. if t in FIncludeTargets then
  929. begin
  930. for c:=low(TCpu) to high(TCpu) do
  931. if (TargetCpuPossible[t,c]) and (c in FIncludeCpus) then
  932. LoadRequires(t,c,self);
  933. end;
  934. end;
  935. function TFPCMake.GetTargetRequires(t:TTarget;c:TCpu):TStringList;
  936. var
  937. ReqSec : TFPCMakeSection;
  938. ReqList : TStringList;
  939. procedure AddReqSec(t:TTarget;c:TCpu;Sec:TFPCMakeSection);
  940. var
  941. s,
  942. ReqName : string;
  943. RSec : TFPCMakeSection;
  944. i : integer;
  945. begin
  946. s:=Sec['packages']+' '+
  947. Sec['packages'+TargetSuffix[t]]+' '+
  948. Sec['packages'+TargetSuffix[t]+CpuSuffix[c]];
  949. repeat
  950. ReqName:=GetToken(s,' ');
  951. if ReqName='' then
  952. break;
  953. i:=Pos('(',ReqName);
  954. if i>0 then
  955. ReqName:=Copy(ReqName,1,i-1);
  956. { We only use lowercase names }
  957. ReqName:=Lowercase(ReqName);
  958. { Already loaded ? }
  959. if (ReqList.IndexOf(ReqName)=-1) then
  960. begin
  961. RSec:=TFPCMakeSection(FSections[ReqName+'_require']);
  962. if assigned(RSec) then
  963. AddReqSec(t,c,RSec);
  964. ReqList.Add(ReqName);
  965. end;
  966. until false;
  967. end;
  968. begin
  969. ReqList:=TStringList.Create;
  970. ReqSec:=TFPCMakeSection(FSections['require']);
  971. if assigned(ReqSec) then
  972. AddReqSec(t,c,ReqSec);
  973. GetTargetRequires:=ReqList;
  974. end;
  975. function TFPCMake.CheckLibcRequire:boolean;
  976. var
  977. i : integer;
  978. RSec : TFPCMakeSection;
  979. t : ttarget;
  980. c : tcpu;
  981. begin
  982. Result:=false;
  983. if GetVariable('require_libc',false)<>'' then
  984. begin
  985. Result:=true;
  986. exit;
  987. end;
  988. for t:=low(ttarget) to high(ttarget) do
  989. if t in FIncludeTargets then
  990. begin
  991. for c:=low(tcpu) to high(tcpu) do
  992. if (TargetCpuPossible[t,c]) and (c in FIncludeCpus) then
  993. begin
  994. for i:=0 to RequireList[t,c].Count-1 do
  995. begin
  996. RSec:=TFPCMakeSection(FSections[RequireList[t,c][i]+'_require']);
  997. if assigned(RSec) then
  998. begin
  999. if RSec['libc']<>'' then
  1000. begin
  1001. Result:=true;
  1002. exit;
  1003. end;
  1004. end;
  1005. end;
  1006. end;
  1007. end;
  1008. end;
  1009. procedure TFPCMake.AddFPCDefaultVariables;
  1010. var
  1011. hs,s : string;
  1012. begin
  1013. { Already set FPCDIR }
  1014. hs:='';
  1015. s:=GetVariable('FPCDIR',false);
  1016. if s<>'' then
  1017. hs:=SubstVariables('$(wildcard $(addprefix '+s+'/,rtl units))');
  1018. { Load from environment }
  1019. if hs='' then
  1020. begin
  1021. s:=GetEnv('FPCDIR');
  1022. if s<>'' then
  1023. hs:=SubstVariables('$(wildcard $(addprefix '+s+'/,rtl units))');
  1024. end;
  1025. { default_fpcdir }
  1026. if hs='' then
  1027. begin
  1028. s:=GetVariable('default_fpcdir',true);
  1029. { add the current subdir to relative paths }
  1030. if s<>'' then
  1031. begin
  1032. {$ifdef UNIX}
  1033. if (s[1]<>'/') then
  1034. {$else}
  1035. if ((length(s)>2) and (s[2]<>':')) or (length(s)<=2) then
  1036. {$endif}
  1037. s:=ExtractFilePath(FFileName)+s;
  1038. hs:=SubstVariables('$(wildcard $(addprefix '+s+'/,rtl units))');
  1039. end
  1040. end;
  1041. { OS defaults }
  1042. if hs='' then
  1043. begin
  1044. {$ifdef UNIX}
  1045. {$ifndef NO_UNIX_UNIT}
  1046. if FileExists('/usr/local/bin/ppc386') then
  1047. begin
  1048. s:=ExtractFilePath({$ifdef ver1_0}ReadLink{$else}fpReadlink{$endif}('/usr/local/bin/ppc386'));
  1049. if s<>'' then
  1050. begin
  1051. if s[length(s)]='/' then
  1052. delete(s,length(s),1);
  1053. hs:=SubstVariables('$(wildcard $(addprefix '+s+'/,rtl units))');
  1054. end;
  1055. end;
  1056. if hs='' then
  1057. begin
  1058. if FileExists('/usr/bin/ppc386') then
  1059. begin
  1060. s:=ExtractFilePath({$ifdef ver1_0}ReadLink{$else}fpReadLink{$endif}('/usr/bin/ppc386'));
  1061. if s<>'' then
  1062. begin
  1063. if s[length(s)]='/' then
  1064. delete(s,length(s),1);
  1065. hs:=SubstVariables('$(wildcard $(addprefix '+s+'/,rtl units))');
  1066. end;
  1067. end;
  1068. end;
  1069. {$endif}
  1070. {$else UNIX}
  1071. hs:=ExtractFilePath(FSearch('ppc386.exe',getenv('PATH')));
  1072. if hs<>'' then
  1073. begin
  1074. s:=hs+'/..';
  1075. hs:=SubstVariables('$(wildcard $(addprefix '+s+'/,rtl units))');
  1076. if hs='' then
  1077. begin
  1078. s:=s+'/..';
  1079. hs:=SubstVariables('$(wildcard $(addprefix '+s+'/,rtl units))');
  1080. end;
  1081. end;
  1082. if hs='' then
  1083. s:='c:/pp';
  1084. {$endif UNIX}
  1085. end;
  1086. SetVariable('FPCDIR',s,false);
  1087. { PACKAGESDIR }
  1088. if GetVariable('PACKAGESDIR',false)='' then
  1089. SetVariable('PACKAGESDIR','$(FPCDIR)/packages/base $(FPCDIR)/packages/extra',false);
  1090. { UNITSDIR }
  1091. if GetVariable('UNITSDIR',false)='' then
  1092. SetVariable('UNITSDIR','$(FPCDIR)/units/$(TARGET)',false);
  1093. { BASEDIR }
  1094. SetVariable('BASEDIR',GetCurrentDir,false);
  1095. end;
  1096. procedure TFPCMake.AddLCLDefaultVariables;
  1097. var
  1098. hs,s : string;
  1099. begin
  1100. { Already set LCLDIR }
  1101. hs:=SubstVariables('$(wildcard $(LCLDIR)/units)');
  1102. { Load from environment }
  1103. if hs='' then
  1104. begin
  1105. SetVariable('LCLDIR',GetEnv('LCLDIR'),false);
  1106. hs:=SubstVariables('$(wildcard $(LCLDIR)/units)');
  1107. end;
  1108. { default_lcldir }
  1109. if hs='' then
  1110. begin
  1111. s:=GetVariable('default_lcldir',true);
  1112. { add the current subdir to relative paths }
  1113. if s<>'' then
  1114. begin
  1115. {$ifdef UNIX}
  1116. if (s[1]<>'/') then
  1117. {$else}
  1118. if ((length(s)>2) and (s[2]<>':')) or (length(s)<=2) then
  1119. {$endif}
  1120. s:=ExtractFilePath(FFileName)+s;
  1121. SetVariable('LCLDIR',s,false);
  1122. hs:=SubstVariables('$(wildcard $(LCLDIR)/units)');
  1123. end
  1124. end;
  1125. { OS defaults }
  1126. if hs='' then
  1127. begin
  1128. hs:=SubstVariables('$(subst /units,,$(firstword $(wildcard $(addsuffix /units,$(BASEDIR)/lcl $(BASEDIR)))))');
  1129. if hs<>'' then
  1130. SetVariable('LCLDIR',hs,false);
  1131. end;
  1132. {$ifdef UNIX}
  1133. if hs='' then
  1134. begin
  1135. hs:=SubstVariables('$(subst /units,,$(firstword $(wildcard $(addsuffix /lib/lazarus/units,/usr/local /usr))))');
  1136. if hs<>'' then
  1137. SetVariable('LCLDIR',hs,false);
  1138. end;
  1139. {$endif UNIX}
  1140. { Add components to PACKAGESDIR }
  1141. SetVariable('PACKAGESDIR',SubstVariables('$(wildcard $(LCLDIR)/.. $(LCLDIR)/../components $(LCLDIR)/components)'),true);
  1142. end;
  1143. function TFPCMake.SubstVariables(const s:string):string;
  1144. function Expect(var s:string;c:char):boolean;
  1145. begin
  1146. if (s<>'') and (s[1]=c) then
  1147. begin
  1148. Delete(s,1,1);
  1149. Result:=true;
  1150. end
  1151. else
  1152. begin
  1153. Verbose(FPCMakeError,'Error "'+c+'" expected');
  1154. Result:=false;
  1155. end;
  1156. end;
  1157. function GetVar(var s:string;untilc:char):string;
  1158. var
  1159. i,j,k : integer;
  1160. first : boolean;
  1161. func,
  1162. tok,s1,s2,s3 : string;
  1163. Sec : TFPCMakeSection;
  1164. begin
  1165. Result:='';
  1166. repeat
  1167. j:=Pos(untilc,s);
  1168. if j=0 then
  1169. j:=Length(s)+1;
  1170. i:=Pos('$(',s);
  1171. if (j<i) or (i=0) then
  1172. break;
  1173. Result:=Result+Copy(s,1,i-1);
  1174. Delete(s,1,i+1);
  1175. { Maybe Function ? }
  1176. j:=Pos(')',s);
  1177. if j=0 then
  1178. j:=Length(s)+1;
  1179. i:=Pos(' ',s);
  1180. if i=0 then
  1181. i:=Length(s)+1;
  1182. if i<j then
  1183. begin
  1184. { It's a function }
  1185. Func:=Copy(s,1,i-1);
  1186. //writeln('func: ',func);
  1187. { $(wildcard <list>) }
  1188. if Func='wildcard' then
  1189. begin
  1190. Delete(s,1,9);
  1191. s1:=GetVar(s,')');
  1192. Expect(s,')');
  1193. first:=true;
  1194. repeat
  1195. tok:=GetToken(s1,' ');
  1196. if tok='' then
  1197. break;
  1198. if PathOrFileExists(tok) then
  1199. begin
  1200. if not first then
  1201. Result:=Result+' '
  1202. else
  1203. first:=false;
  1204. Result:=Result+tok;
  1205. end;
  1206. until false;
  1207. end
  1208. { $(addprefix <suffix>,<list>) }
  1209. else if Func='addprefix' then
  1210. begin
  1211. Delete(s,1,10);
  1212. s1:=GetVar(s,',');
  1213. if Expect(s,',') then
  1214. begin
  1215. s2:=GetVar(s,')');
  1216. Expect(s,')');
  1217. end;
  1218. first:=true;
  1219. repeat
  1220. tok:=GetToken(s2,' ');
  1221. if tok='' then
  1222. break;
  1223. if not first then
  1224. Result:=Result+' '
  1225. else
  1226. first:=false;
  1227. Result:=Result+s1+tok;
  1228. until false;
  1229. end
  1230. { $(addsuffix <suffix>,<list>) }
  1231. else if Func='addsuffix' then
  1232. begin
  1233. Delete(s,1,10);
  1234. s1:=GetVar(s,',');
  1235. if Expect(s,',') then
  1236. begin
  1237. s2:=GetVar(s,')');
  1238. Expect(s,')');
  1239. end;
  1240. first:=true;
  1241. repeat
  1242. tok:=GetToken(s2,' ');
  1243. if tok='' then
  1244. break;
  1245. if not first then
  1246. Result:=Result+' '
  1247. else
  1248. first:=false;
  1249. Result:=Result+tok+s1;
  1250. until false;
  1251. end
  1252. { $(firstword <list>) }
  1253. else if Func='firstword' then
  1254. begin
  1255. Delete(s,1,10);
  1256. s1:=GetVar(s,')');
  1257. Expect(s,')');
  1258. Result:=GetToken(s1,' ');
  1259. end
  1260. { $(subst <oldpat>,<newpat>,<string>) }
  1261. else if Func='subst' then
  1262. begin
  1263. Delete(s,1,6);
  1264. s1:=GetVar(s,',');
  1265. if Expect(s,',') then
  1266. begin
  1267. s2:=GetVar(s,',');
  1268. if Expect(s,',') then
  1269. begin
  1270. s3:=GetVar(s,')');
  1271. Expect(s,')');
  1272. end;
  1273. end;
  1274. Result:=StringReplace(s3,s1,s2,[rfReplaceAll]);
  1275. end;
  1276. end
  1277. else
  1278. begin
  1279. s2:=Copy(s,1,j-1);
  1280. Delete(s,1,j);
  1281. k:=pos('_',s2);
  1282. if k>0 then
  1283. begin
  1284. s3:=LowerCase(Copy(s2,k+1,Length(s2)-k));
  1285. s2:=LowerCase(Copy(s2,1,k-1));
  1286. Sec:=TFPCMakeSection(Section[s2]);
  1287. if assigned(Sec) then
  1288. s2:=Sec[s3]
  1289. else
  1290. s2:='';
  1291. end
  1292. else
  1293. s2:=Variables[s2];
  1294. Insert(s2,s,1);
  1295. end;
  1296. until false;
  1297. Result:=Result+Copy(s,1,j-1);
  1298. Delete(s,1,j-1);
  1299. end;
  1300. var
  1301. s1 : string;
  1302. begin
  1303. //writeln('S: ',s);
  1304. s1:=s;
  1305. Result:=GetVar(s1,#0);
  1306. //writeln('R: ',result);
  1307. end;
  1308. function TFPCMake.GetVariable(const inivar:string;dosubst:boolean):string;
  1309. var
  1310. Sec : TFPCMakeSection;
  1311. Dic : TKeyValue;
  1312. i : integer;
  1313. begin
  1314. Result:='';
  1315. i:=Pos('_',inivar);
  1316. if i<>0 then
  1317. begin
  1318. Sec:=TFPCMakeSection(FSections[Copy(Inivar,1,i-1)]);
  1319. if assigned(Sec) then
  1320. begin
  1321. if not assigned(Sec.Dictionary) then
  1322. Sec.ParseIni;
  1323. Dic:=TKeyValue(Sec.Dictionary);
  1324. Result:=Dic[Copy(IniVar,i+1,Length(IniVar)-i)];
  1325. end
  1326. else
  1327. exit;
  1328. end
  1329. else
  1330. Result:=Variables[IniVar];
  1331. { Substition asked ? }
  1332. if dosubst then
  1333. Result:=SubstVariables(Result);
  1334. end;
  1335. function TFPCMake.SetVariable(const inivar,value:string;add:boolean):string;
  1336. var
  1337. Sec : TFPCMakeSection;
  1338. P : TKeyValueItem;
  1339. i : integer;
  1340. tempval,key : string;
  1341. begin
  1342. Result:='';
  1343. i:=Pos('_',inivar);
  1344. if i<>0 then
  1345. begin
  1346. Sec:=TFPCMakeSection(FSections[Copy(Inivar,1,i-1)]);
  1347. if Sec=nil then
  1348. Sec:=TFPCMakeSection(FSections.Insert(TFPCMakeSection.CreateKeyValue(Copy(Inivar,1,i-1))));
  1349. key:=Copy(IniVar,i+1,Length(IniVar)-i);
  1350. p:=TKeyValueItem(Sec.Dictionary.Search(Key));
  1351. if assigned(p) then
  1352. begin
  1353. if Add then
  1354. AddToken(p.FValue,Value,' ')
  1355. else
  1356. p.Value:=Value;
  1357. end
  1358. else
  1359. TKeyValue(Sec.Dictionary).Add(key,value);
  1360. end
  1361. else
  1362. begin
  1363. if Add then
  1364. begin
  1365. tempval:=Variables[IniVar];
  1366. AddToken(tempval,Value,' ');
  1367. Variables[IniVar]:=tempval;
  1368. end
  1369. else
  1370. Variables[IniVar]:=Value;
  1371. end;
  1372. end;
  1373. procedure TFPCMake.ParseSec(p:TDictionaryItem);
  1374. begin
  1375. TFPCMakeSection(p).ParseIni;
  1376. end;
  1377. procedure TFPCMake.PrintSec(p:TDictionaryItem);
  1378. var
  1379. i : integer;
  1380. begin
  1381. with TFPCMakeSection(p) do
  1382. begin
  1383. Verbose(FPCMakeDebug,'['+Name+']');
  1384. if assigned(FList) then
  1385. begin
  1386. Verbose(FPCMakeDebug,' List:');
  1387. for i:=0 to FList.Count-1 do
  1388. Verbose(FPCMakeDebug,' "'+FList[i]+'"');
  1389. if assigned(FDictionary) then
  1390. Verbose(FPCMakeDebug,'');
  1391. end;
  1392. if assigned(FDictionary) then
  1393. begin
  1394. Verbose(FPCMakeDebug,' Dictionary:');
  1395. FDictionary.Foreach(@PrintDic);
  1396. end;
  1397. end;
  1398. end;
  1399. procedure TFPCMake.PrintDic(p:TDictionaryItem);
  1400. begin
  1401. with TKeyValueItem(p) do
  1402. begin
  1403. Verbose(FPCMakeDebug,' '+name+' = "'+value+'"');
  1404. end;
  1405. end;
  1406. procedure TFPCMake.Print;
  1407. begin
  1408. { global variables }
  1409. Verbose(FPCMakeDebug,'[global variables]');
  1410. Verbose(FPCMakeDebug,' Dictionary:');
  1411. Variables.Foreach(@PrintDic);
  1412. { sections }
  1413. FSections.Foreach(@PrintSec);
  1414. end;
  1415. function TFPCMake.GetSec(const AName:string):TDictionaryItem;
  1416. begin
  1417. GetSec:=FSections.Search(AName);
  1418. end;
  1419. end.
  1420. {
  1421. $Log$
  1422. Revision 1.40 2004-06-06 14:11:54 karoly
  1423. + added morphos target
  1424. Revision 1.39 2004/06/05 11:14:49 olle
  1425. * niceified
  1426. Revision 1.38 2004/05/20 12:02:48 marco
  1427. * freebsd/x86_64
  1428. Revision 1.37 2004/02/22 14:55:22 hajny
  1429. * small correction for checking of absolute paths
  1430. Revision 1.36 2004/02/07 00:22:24 florian
  1431. + arm-linux target
  1432. Revision 1.35 2004/01/05 17:45:02 marco
  1433. * netbsd patches
  1434. Revision 1.34 2003/09/30 09:10:28 marco
  1435. * watcom support
  1436. Revision 1.33 2003/09/27 13:00:30 peter
  1437. * fixed for unix
  1438. Revision 1.32 2003/05/20 23:54:45 florian
  1439. + darwin support added
  1440. Revision 1.31 2003/04/24 23:21:01 peter
  1441. * support different cpu target
  1442. Revision 1.30 2003/03/23 23:18:26 hajny
  1443. + platform extensions unified, emx target added
  1444. Revision 1.29 2003/01/13 15:09:16 florian
  1445. + macos and macosx target
  1446. * fixed target detection, first we should try the default target
  1447. Revision 1.28 2003/01/13 11:54:02 pierre
  1448. + palmos target added
  1449. Revision 1.27 2002/10/07 18:41:02 peter
  1450. * support mainpackage/subpackage. This allows to use things like
  1451. gnome1/gconf as required package
  1452. Revision 1.26 2002/09/07 15:40:31 peter
  1453. * old logs removed and tabs fixed
  1454. Revision 1.25 2002/07/30 13:18:42 marco
  1455. * OpenBSD fixes
  1456. Revision 1.24 2002/06/01 18:39:15 marco
  1457. * Renamefest
  1458. Revision 1.23 2002/03/15 11:37:46 armin
  1459. + Added netware target
  1460. Revision 1.22 2002/01/29 22:00:22 peter
  1461. * load package section first before setting globals
  1462. * fixed buildunit
  1463. Revision 1.21 2002/01/29 17:48:53 peter
  1464. * packages splitted to base and extra
  1465. Revision 1.20 2002/01/27 21:42:35 peter
  1466. * -r option to process target dirs also
  1467. * default changed to build only for current target
  1468. * removed auto building of required packages
  1469. * removed makefile target because it causes problems with
  1470. an internal rule of make
  1471. Revision 1.19 2002/01/06 21:50:04 peter
  1472. * lcl updates
  1473. * small optimizes for package check
  1474. }