ppumove.pp 14 KB

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