ppumove.pp 14 KB

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