ppumove.pp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677
  1. {
  2. Copyright (c) 1999-2002 by the FPC Development Team
  3. Add multiple FPC units into a static/shared library
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************}
  16. {$ifndef TP}
  17. {$H+}
  18. {$endif}
  19. Program ppumove;
  20. uses
  21. {$IFDEF MACOS}
  22. {$DEFINE USE_FAKE_SYSUTILS}
  23. {$ENDIF MACOS}
  24. {$IFNDEF USE_FAKE_SYSUTILS}
  25. sysutils,
  26. {$ELSE}
  27. fksysutl,
  28. {$ENDIF}
  29. {$ifdef unix}
  30. Baseunix,Unix, Dos,
  31. {$else unix}
  32. dos,
  33. {$endif unix}
  34. cutils,ppu,entfile,systems,
  35. getopts;
  36. const
  37. Version = 'Version 2.1.1';
  38. Title = 'PPU-Mover';
  39. Copyright = 'Copyright (c) 1998-2007 by the Free Pascal Development Team';
  40. ShortOpts = 'o:e:d:i:qhsvb';
  41. BufSize = 4096;
  42. PPUExt = 'ppu';
  43. ObjExt = 'o';
  44. StaticLibExt ='a';
  45. {$ifdef unix}
  46. SharedLibExt ='so';
  47. BatchExt ='.sh';
  48. {$else}
  49. SharedLibExt ='dll';
  50. BatchExt ='.bat';
  51. {$endif unix}
  52. { link options }
  53. link_none = $0;
  54. link_always = $1;
  55. link_static = $2;
  56. link_smart = $4;
  57. link_shared = $8;
  58. link_lto = $10;
  59. Type
  60. PLinkOEnt = ^TLinkOEnt;
  61. TLinkOEnt = record
  62. Name : string;
  63. Next : PLinkOEnt;
  64. end;
  65. Var
  66. ArBin,LDBin,StripBin,
  67. OutputFileForPPU,
  68. OutputFile,
  69. OutputFileForLink, { the name of the output file needed when linking }
  70. InputPath,
  71. DestPath,
  72. PPLExt,
  73. LibExt : string;
  74. DoStrip,
  75. Batch,
  76. Quiet,
  77. MakeStatic : boolean;
  78. Buffer : Pointer;
  79. ObjFiles : PLinkOEnt;
  80. BatchFile : Text;
  81. Libs : ansistring;
  82. {*****************************************************************************
  83. Helpers
  84. *****************************************************************************}
  85. Procedure Error(const s:string;stop:boolean);
  86. {
  87. Write an error message to stderr
  88. }
  89. begin
  90. writeln(stderr,s);
  91. if stop then
  92. halt(1);
  93. end;
  94. function Shell(const s:string):longint;
  95. {
  96. Run a shell commnad and return the exitcode
  97. }
  98. begin
  99. if Batch then
  100. begin
  101. Writeln(BatchFile,s);
  102. Shell:=0;
  103. exit;
  104. end;
  105. {$ifdef unix}
  106. Shell:=unix.fpsystem(s);
  107. {$else}
  108. exec(getenv('COMSPEC'),'/C '+s);
  109. Shell:=DosExitCode;
  110. {$endif}
  111. end;
  112. Function FileExists (Const F : String) : Boolean;
  113. {
  114. Returns True if the file exists, False if not.
  115. }
  116. Var
  117. {$ifdef unix}
  118. info : Stat;
  119. {$else}
  120. info : searchrec;
  121. {$endif}
  122. begin
  123. {$ifdef unix}
  124. FileExists:=FpStat(F,Info)=0;
  125. {$else}
  126. FindFirst (F,anyfile,Info);
  127. FileExists:=DosError=0;
  128. {$endif}
  129. end;
  130. Function ChangeFileExt(Const HStr,ext:String):String;
  131. {
  132. Return a filename which will have extension ext added if no
  133. extension is found
  134. }
  135. var
  136. j : longint;
  137. begin
  138. j:=length(Hstr);
  139. while (j>0) and (Hstr[j]<>'.') do
  140. dec(j);
  141. if j=0 then
  142. ChangeFileExt:=Hstr+'.'+Ext
  143. else
  144. ChangeFileExt:=HStr;
  145. end;
  146. Function ForceExtension(Const HStr,ext:String):String;
  147. {
  148. Return a filename which certainly has the extension ext
  149. }
  150. var
  151. j : longint;
  152. begin
  153. j:=length(Hstr);
  154. while (j>0) and (Hstr[j]<>'.') do
  155. dec(j);
  156. if j=0 then
  157. j:=255;
  158. ForceExtension:=Copy(Hstr,1,j-1)+'.'+Ext;
  159. end;
  160. Procedure AddToLinkFiles(const S : String);
  161. {
  162. Adds a filename to a list of object files to link to.
  163. No duplicates allowed.
  164. }
  165. Var
  166. P : PLinKOEnt;
  167. begin
  168. P:=ObjFiles;
  169. { Don't add files twice }
  170. While (P<>nil) and (p^.name<>s) do
  171. p:=p^.next;
  172. if p=nil then
  173. begin
  174. new(p);
  175. p^.next:=ObjFiles;
  176. p^.name:=s;
  177. ObjFiles:=P;
  178. end;
  179. end;
  180. Function ExtractLib(const libfn:string):string;
  181. {
  182. Extract a static library libfn and return the files with a
  183. wildcard
  184. }
  185. var
  186. n : namestr;
  187. d : dirstr;
  188. e : extstr;
  189. begin
  190. { create the temp dir first }
  191. fsplit(libfn,d,n,e);
  192. {$push}{$I-}
  193. mkdir(n+'.sl');
  194. {$pop}
  195. if ioresult<>0 then;
  196. { Extract }
  197. if Shell(arbin+' x '+libfn)<>0 then
  198. Error('Fatal: Error running '+arbin,true);
  199. { Remove the lib file, it's extracted so it can be created with ease }
  200. if PPLExt=PPUExt then
  201. Shell('rm '+libfn);
  202. {$ifdef unix}
  203. ExtractLib:=n+'.sl/*';
  204. {$else}
  205. ExtractLib:=n+'.sl\*';
  206. {$endif}
  207. end;
  208. Function DoPPU(const PPUFn,PPLFn:String):Boolean;
  209. {
  210. Convert one file (in Filename) to library format.
  211. Return true if successful, false otherwise.
  212. }
  213. Var
  214. inppu,
  215. outppu : tppufile;
  216. b,
  217. untilb : byte;
  218. l,m : longint;
  219. f : file;
  220. ext,
  221. s : string;
  222. ppuversion,
  223. ppulongversion: dword;
  224. gotltofiles: boolean;
  225. begin
  226. DoPPU:=false;
  227. gotltofiles:=false;
  228. If Not Quiet then
  229. Write ('Processing ',PPUFn,'...');
  230. inppu:=tppufile.create(PPUFn);
  231. if not inppu.openfile then
  232. begin
  233. inppu.free;
  234. Error('Error: Could not open : '+PPUFn,false);
  235. Exit;
  236. end;
  237. { Check the ppufile }
  238. if not inppu.CheckPPUId then
  239. begin
  240. inppu.free;
  241. Error('Error: Not a PPU File : '+PPUFn,false);
  242. Exit;
  243. end;
  244. ppuversion:=inppu.getversion;
  245. if ppuversion<CurrentPPUVersion then
  246. begin
  247. inppu.free;
  248. Error('Error: Wrong PPU Version '+tostr(ppuversion)+' in '+PPUFn,false);
  249. Exit;
  250. end;
  251. { No .o file generated for this ppu, just skip }
  252. if (inppu.header.common.flags and uf_no_link)<>0 then
  253. begin
  254. inppu.free;
  255. If Not Quiet then
  256. Writeln (' No files.');
  257. DoPPU:=true;
  258. Exit;
  259. end;
  260. { Already a lib? }
  261. if (inppu.header.common.flags and uf_in_library)<>0 then
  262. begin
  263. inppu.free;
  264. Error('Error: PPU is already in a library : '+PPUFn,false);
  265. Exit;
  266. end;
  267. { We need a static linked unit }
  268. if (inppu.header.common.flags and uf_static_linked)=0 then
  269. begin
  270. inppu.free;
  271. Error('Error: PPU is not static linked : '+PPUFn,false);
  272. Exit;
  273. end;
  274. { Check if shared is allowed }
  275. if tsystem(inppu.header.common.target) in [system_i386_go32v2] then
  276. begin
  277. Writeln('Warning: shared library not supported for ppu target, switching to static library');
  278. MakeStatic:=true;
  279. end;
  280. { Create the new ppu }
  281. if PPUFn=PPLFn then
  282. outppu:=tppufile.create('ppumove.$$$')
  283. else
  284. outppu:=tppufile.create(PPLFn);
  285. outppu.createfile;
  286. { Create new header, with the new flags }
  287. outppu.header:=inppu.header;
  288. outppu.header.common.flags:=outppu.header.common.flags or uf_in_library;
  289. if MakeStatic then
  290. outppu.header.common.flags:=outppu.header.common.flags or uf_static_linked
  291. else
  292. outppu.header.common.flags:=outppu.header.common.flags or uf_shared_linked;
  293. { read until the object files are found }
  294. untilb:=iblinkunitofiles;
  295. repeat
  296. b:=inppu.readentry;
  297. if b in [ibendinterface,ibend] then
  298. begin
  299. inppu.free;
  300. outppu.free;
  301. Error('Error: No files to be linked found : '+PPUFn,false);
  302. Exit;
  303. end;
  304. if b<>untilb then
  305. begin
  306. if b=ibextraheader then
  307. begin
  308. ppulongversion:=cardinal(inppu.getlongint);
  309. if ppulongversion<>CurrentPPULongVersion then
  310. begin
  311. inppu.free;
  312. outppu.free;
  313. Error('Error: Wrong PPU Long Version '+tostr(ppulongversion)+' in '+PPUFn,false);
  314. Exit;
  315. end;
  316. outppu.putlongint(longint(ppulongversion));
  317. end;
  318. repeat
  319. inppu.getdatabuf(buffer^,bufsize,l);
  320. outppu.putdata(buffer^,l);
  321. until l<bufsize;
  322. outppu.writeentry(b);
  323. end;
  324. until (b=untilb);
  325. { we have now reached the section for the files which need to be added,
  326. now add them to the list }
  327. case b of
  328. iblinkunitofiles :
  329. begin
  330. { add all o files, and save the entry when not creating a static
  331. library to keep staticlinking possible (also keep possibility
  332. to link lto) }
  333. while not inppu.endofentry do
  334. begin
  335. s:=inppu.getstring;
  336. m:=inppu.getlongint;
  337. if (not MakeStatic) or
  338. ((m and link_lto)<>0) then
  339. begin
  340. gotltofiles:=gotltofiles or ((m and link_lto)<>0);
  341. outppu.putstring(s);
  342. outppu.putlongint(m);
  343. end;
  344. AddToLinkFiles(s);
  345. end;
  346. if not MakeStatic or
  347. gotltofiles then
  348. outppu.writeentry(b);
  349. end;
  350. { iblinkunitstaticlibs :
  351. begin
  352. AddToLinkFiles(ExtractLib(inppu.getstring));
  353. if not inppu.endofentry then
  354. begin
  355. repeat
  356. inppu.getdatabuf(buffer^,bufsize,l);
  357. outppu.putdata(buffer^,l);
  358. until l<bufsize;
  359. outppu.writeentry(b);
  360. end;
  361. end; }
  362. end;
  363. { just add a new entry with the new lib }
  364. if MakeStatic then
  365. begin
  366. outppu.putstring(OutputfileForPPU);
  367. if not gotltofiles then
  368. outppu.putlongint(link_static)
  369. else
  370. outppu.putlongint(link_static or link_lto);
  371. outppu.writeentry(iblinkunitstaticlibs)
  372. end
  373. else
  374. begin
  375. outppu.putstring(OutputfileForPPU);
  376. if not gotltofiles then
  377. outppu.putlongint(link_shared)
  378. else
  379. outppu.putlongint(link_shared or link_lto);
  380. outppu.writeentry(iblinkunitsharedlibs);
  381. end;
  382. { read all entries until the end and write them also to the new ppu }
  383. repeat
  384. b:=inppu.readentry;
  385. { don't write ibend, that's written automatically }
  386. if b<>ibend then
  387. begin
  388. if b=iblinkothersharedlibs then
  389. begin
  390. while not inppu.endofentry do
  391. begin
  392. s:=inppu.getstring;
  393. m:=inppu.getlongint;
  394. outppu.putstring(s);
  395. { strip lib prefix }
  396. if copy(s,1,3)='lib' then
  397. delete(s,1,3);
  398. { strip lib prefix }
  399. if copy(s,1,3)='lib' then
  400. delete(s,1,3);
  401. ext:=ExtractFileExt(s);
  402. if ext<>'' then
  403. delete(s,length(s)-length(ext)+1,length(ext));
  404. libs:=libs+' -l'+s;
  405. outppu.putlongint(m);
  406. end;
  407. end
  408. else
  409. repeat
  410. inppu.getdatabuf(buffer^,bufsize,l);
  411. outppu.putdata(buffer^,l);
  412. until l<bufsize;
  413. outppu.writeentry(b);
  414. end;
  415. until b=ibend;
  416. { write the last stuff and close }
  417. outppu.flush;
  418. outppu.writeheader;
  419. outppu.free;
  420. inppu.free;
  421. { rename }
  422. if PPUFn=PPLFn then
  423. begin
  424. {$push}{$I-}
  425. assign(f,PPUFn);
  426. erase(f);
  427. assign(f,'ppumove.$$$');
  428. rename(f,PPUFn);
  429. {$pop}
  430. if ioresult<>0 then;
  431. end;
  432. { the end }
  433. If Not Quiet then
  434. Writeln (' Done.');
  435. DoPPU:=True;
  436. end;
  437. Function DoFile(const FileName:String):Boolean;
  438. {
  439. Process a file, mainly here for wildcard support under Dos
  440. }
  441. {$ifndef unix}
  442. var
  443. dir : searchrec;
  444. {$endif}
  445. begin
  446. {$ifdef unix}
  447. DoFile:=DoPPU(InputPath+FileName,InputPath+ForceExtension(FileName,PPLExt));
  448. {$else}
  449. DoFile:=false;
  450. findfirst(filename,$20,dir);
  451. while doserror=0 do
  452. begin
  453. if not DoPPU(InputPath+Dir.Name,InputPath+ForceExtension(Dir.Name,PPLExt)) then
  454. exit;
  455. findnext(dir);
  456. end;
  457. findclose(dir);
  458. DoFile:=true;
  459. {$endif}
  460. end;
  461. Procedure DoLink;
  462. {
  463. Link the object files together to form a (shared) library
  464. }
  465. Var
  466. Names : ansistring;
  467. f : file;
  468. Err : boolean;
  469. P : PLinkOEnt;
  470. begin
  471. if not Quiet then
  472. Write ('Linking ');
  473. P:=ObjFiles;
  474. names:='';
  475. While p<>nil do
  476. begin
  477. if Names<>'' then
  478. Names:=Names+' '+InputPath+P^.name
  479. else
  480. Names:=InputPath+p^.Name;
  481. p:=p^.next;
  482. end;
  483. if Names='' then
  484. begin
  485. If not Quiet then
  486. Writeln('Error: no files found to be linked');
  487. exit;
  488. end;
  489. If not Quiet then
  490. WriteLn(names+Libs);
  491. { Run ar or ld to create the lib }
  492. If MakeStatic then
  493. Err:=Shell(arbin+' rs '+outputfile+' '+names)<>0
  494. else
  495. begin
  496. Err:=Shell(ldbin+' -shared -E -o '+OutputFile+' '+names+' '+libs)<>0;
  497. if (not Err) and dostrip then
  498. Shell(stripbin+' --strip-unneeded '+OutputFile);
  499. end;
  500. If Err then
  501. Error('Fatal: Library building stage failed.',true);
  502. { fix permission to 644, so it's not 755 }
  503. {$ifdef unix}
  504. FPChmod(OutputFile,420);
  505. {$endif}
  506. { Rename to the destpath }
  507. if DestPath<>'' then
  508. begin
  509. Assign(F, OutputFile);
  510. Rename(F,DestPath+DirectorySeparator+OutputFile);
  511. end;
  512. end;
  513. Procedure usage;
  514. {
  515. Print usage and exit.
  516. }
  517. begin
  518. Writeln(paramstr(0),': [-qhvbsS] [-e ext] [-o name] [-d path] file [file ...]');
  519. Halt(0);
  520. end;
  521. Procedure processopts;
  522. {
  523. Process command line opions, and checks if command line options OK.
  524. }
  525. var
  526. C : char;
  527. begin
  528. if paramcount=0 then
  529. usage;
  530. { Reset }
  531. ObjFiles:=Nil;
  532. Quiet:=False;
  533. Batch:=False;
  534. DoStrip:=False;
  535. OutputFile:='';
  536. PPLExt:='ppu';
  537. ArBin:='ar';
  538. LdBin:='ld';
  539. StripBin:='strip';
  540. repeat
  541. c:=Getopt (ShortOpts);
  542. Case C of
  543. EndOfOptions : break;
  544. 'S' : MakeStatic:=True;
  545. 'o' : OutputFile:=OptArg;
  546. 'd' : DestPath:=OptArg;
  547. 'i' : begin
  548. InputPath:=OptArg;
  549. if InputPath[length(InputPath)]<>DirectorySeparator then
  550. InputPath:=InputPath+DirectorySeparator;
  551. end;
  552. 'e' : PPLext:=OptArg;
  553. 'q' : Quiet:=True;
  554. 'b' : Batch:=true;
  555. 's' : DoStrip:=true;
  556. '?' : Usage;
  557. 'h' : Usage;
  558. end;
  559. until false;
  560. { Test filenames on the commandline }
  561. if (OptInd>Paramcount) then
  562. Error('Error: no input files',true);
  563. if (OptInd<ParamCount) and (OutputFile='') then
  564. Error('Error: when moving multiple units, specify an output name.',true);
  565. { alloc a buffer }
  566. GetMem (Buffer,Bufsize);
  567. If Buffer=Nil then
  568. Error('Error: could not allocate memory for buffer.',true);
  569. end;
  570. var
  571. i : longint;
  572. begin
  573. Libs:='';
  574. ProcessOpts;
  575. { Write Header }
  576. if not Quiet then
  577. begin
  578. Writeln(Title+' '+Version);
  579. Writeln(Copyright);
  580. Writeln;
  581. end;
  582. { fix the libext and outputfilename }
  583. if Makestatic then
  584. LibExt:=StaticLibExt
  585. else
  586. LibExt:=SharedLibExt;
  587. if OutputFile='' then
  588. OutputFile:=Paramstr(OptInd);
  589. OutputFileForPPU:=OutputFile;
  590. { fix filename }
  591. {$ifdef unix}
  592. if Copy(OutputFile,1,3)<>'lib' then
  593. OutputFile:='lib'+OutputFile;
  594. { For unix skip replacing the extension if a full .so.X.X if specified }
  595. i:=pos('.so.',Outputfile);
  596. if i<>0 then
  597. OutputFileForLink:=Copy(Outputfile,4,i-4)
  598. else
  599. begin
  600. OutputFile:=ForceExtension(OutputFile,LibExt);
  601. OutputFileForLink:=Copy(Outputfile,4,length(Outputfile)-length(LibExt)-4);
  602. end;
  603. {$else}
  604. OutputFile:=ForceExtension(OutputFile,LibExt);
  605. OutputFileForLink:=OutputFile;
  606. {$endif}
  607. { Open BatchFile }
  608. if Batch then
  609. begin
  610. Assign(BatchFile,'pmove'+BatchExt);
  611. Rewrite(BatchFile);
  612. end;
  613. { Process Files }
  614. i:=OptInd;
  615. While (i<=ParamCount) and Dofile(ChangeFileExt(Paramstr(i),PPUExt)) do
  616. Inc(i);
  617. { Do Linking stage }
  618. DoLink;
  619. { Close BatchFile }
  620. if Batch then
  621. begin
  622. if Not Quiet then
  623. Writeln('Writing pmove'+BatchExt);
  624. Close(BatchFile);
  625. {$ifdef unix}
  626. FPChmod('pmove'+BatchExt,493);
  627. {$endif}
  628. end;
  629. { The End }
  630. if Not Quiet then
  631. Writeln('Done.');
  632. end.