ppumove.pp 14 KB

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