pkgglobals.pp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. {$mode objfpc}
  2. {$h+}
  3. unit pkgglobals;
  4. interface
  5. uses
  6. SysUtils,
  7. Classes;
  8. Const
  9. {$ifdef unix}
  10. ExeExt = '';
  11. AllFiles='*';
  12. {$else unix}
  13. ExeExt = '.exe';
  14. AllFiles='*.*';
  15. {$endif unix}
  16. Type
  17. TFPMKUnitDep=record
  18. package : string[12];
  19. reqver : string[8];
  20. undef : string[16];
  21. end;
  22. Const
  23. // Dependencies for compiling the fpmkunit unit
  24. FPMKUnitDepCount=4;
  25. FPMKUnitDeps : array[1..4] of TFPMKUnitDep = (
  26. (package: 'hash';
  27. reqver : '2.0.0';
  28. undef : 'NO_UNIT_ZIPPER'),
  29. (package: 'paszlib';
  30. reqver : '2.2.0';
  31. undef : 'NO_UNIT_ZIPPER'),
  32. (package: 'fcl-process';
  33. reqver : '2.0.0';
  34. undef : 'NO_UNIT_PROCESS'),
  35. (package: 'fpmkunit';
  36. reqver : '2.2.0';
  37. undef : '')
  38. );
  39. Type
  40. TVerbosity = (vError,vWarning,vInfo,vCommands,vDebug);
  41. TVerbosities = Set of TVerbosity;
  42. EPackagerError = class(Exception);
  43. // Logging
  44. Function StringToVerbosity (S : String) : TVerbosity;
  45. Function VerbosityToString (V : TVerbosity): String;
  46. Procedure Log(Level: TVerbosity;Msg : String);
  47. Procedure Log(Level: TVerbosity;Fmt : String; const Args : array of const);
  48. Procedure Error(Msg : String);
  49. Procedure Error(Fmt : String; const Args : array of const);
  50. // Utils
  51. function maybequoted(const s:string):string;
  52. Function FixPath(const S : String) : string;
  53. Function DirectoryExistsLog(const ADir:string):Boolean;
  54. Function FileExistsLog(const AFileName:string):Boolean;
  55. procedure BackupFile(const AFileName: String);
  56. Procedure DeleteDir(const ADir:string);
  57. Procedure SearchFiles(SL:TStringList;const APattern:string);
  58. Function GetCompilerInfo(const ACompiler,AOptions:string):string;
  59. function IsSuperUser:boolean;
  60. var
  61. Verbosity : TVerbosities;
  62. FPMKUnitDepAvailable : array[1..FPMKUnitDepCount] of boolean;
  63. Implementation
  64. // define use_shell to use sysutils.executeprocess
  65. // as alternate to using 'process' in getcompilerinfo
  66. {$IF defined(GO32v2) or defined(WATCOM) or defined(OS2)}
  67. {$DEFINE USE_SHELL}
  68. {$ENDIF GO32v2 or WATCOM or OS2}
  69. uses
  70. typinfo,
  71. {$ifdef unix}
  72. baseunix,
  73. {$endif}
  74. {$IFNDEF USE_SHELL}
  75. process,
  76. {$ENDIF USE_SHELL}
  77. contnrs,
  78. uriparser,
  79. pkgmessages;
  80. function StringToVerbosity(S: String): TVerbosity;
  81. Var
  82. I : integer;
  83. begin
  84. I:=GetEnumValue(TypeInfo(TVerbosity),'v'+S);
  85. If (I<>-1) then
  86. Result:=TVerbosity(I)
  87. else
  88. Raise EPackagerError.CreateFmt(SErrInvalidVerbosity,[S]);
  89. end;
  90. Function VerbosityToString (V : TVerbosity): String;
  91. begin
  92. Result:=GetEnumName(TypeInfo(TVerbosity),Integer(V));
  93. Delete(Result,1,1);// Delete 'v'
  94. end;
  95. procedure Log(Level:TVerbosity;Msg: String);
  96. var
  97. Prefix : string;
  98. begin
  99. if not(Level in Verbosity) then
  100. exit;
  101. Prefix:='';
  102. case Level of
  103. vWarning :
  104. Prefix:=SWarning;
  105. vError :
  106. Prefix:=SError;
  107. { vInfo :
  108. Prefix:='I: ';
  109. vCommands :
  110. Prefix:='C: ';
  111. vDebug :
  112. Prefix:='D: '; }
  113. end;
  114. Writeln(stdErr,Prefix,Msg);
  115. end;
  116. Procedure Log(Level:TVerbosity; Fmt:String; const Args:array of const);
  117. begin
  118. Log(Level,Format(Fmt,Args));
  119. end;
  120. procedure Error(Msg: String);
  121. begin
  122. Raise EPackagerError.Create(Msg);
  123. end;
  124. procedure Error(Fmt: String; const Args: array of const);
  125. begin
  126. Raise EPackagerError.CreateFmt(Fmt,Args);
  127. end;
  128. function maybequoted(const s:string):string;
  129. const
  130. {$IFDEF MSWINDOWS}
  131. FORBIDDEN_CHARS = ['!', '@', '#', '$', '%', '^', '&', '*', '(', ')',
  132. '{', '}', '''', '`', '~'];
  133. {$ELSE}
  134. FORBIDDEN_CHARS = ['!', '@', '#', '$', '%', '^', '&', '*', '(', ')',
  135. '{', '}', '''', ':', '\', '`', '~'];
  136. {$ENDIF}
  137. var
  138. s1 : string;
  139. i : integer;
  140. quoted : boolean;
  141. begin
  142. quoted:=false;
  143. s1:='"';
  144. for i:=1 to length(s) do
  145. begin
  146. case s[i] of
  147. '"' :
  148. begin
  149. quoted:=true;
  150. s1:=s1+'\"';
  151. end;
  152. ' ',
  153. #128..#255 :
  154. begin
  155. quoted:=true;
  156. s1:=s1+s[i];
  157. end;
  158. else begin
  159. if s[i] in FORBIDDEN_CHARS then
  160. quoted:=True;
  161. s1:=s1+s[i];
  162. end;
  163. end;
  164. end;
  165. if quoted then
  166. maybequoted:=s1+'"'
  167. else
  168. maybequoted:=s;
  169. end;
  170. Function FixPath(const S : String) : string;
  171. begin
  172. If (S<>'') then
  173. Result:=IncludeTrailingPathDelimiter(S)
  174. else
  175. Result:='';
  176. end;
  177. Function DirectoryExistsLog(const ADir:string):Boolean;
  178. begin
  179. result:=SysUtils.DirectoryExists(ADir);
  180. if result then
  181. Log(vDebug,SDbgDirectoryExists,[ADir,SDbgFound])
  182. else
  183. Log(vDebug,SDbgDirectoryExists,[ADir,SDbgNotFound]);
  184. end;
  185. Function FileExistsLog(const AFileName:string):Boolean;
  186. begin
  187. result:=SysUtils.FileExists(AFileName);
  188. if result then
  189. Log(vDebug,SDbgFileExists,[AFileName,SDbgFound])
  190. else
  191. Log(vDebug,SDbgFileExists,[AFileName,SDbgNotFound]);
  192. end;
  193. procedure BackupFile(const AFileName: String);
  194. Var
  195. BFN : String;
  196. begin
  197. BFN:=AFileName+'.bak';
  198. Log(vDebug,SDbgBackupFile,[BFN]);
  199. If not RenameFile(AFileName,BFN) then
  200. Error(SErrBackupFailed,[AFileName,BFN]);
  201. end;
  202. Procedure DeleteDir(const ADir:string);
  203. var
  204. Info : TSearchRec;
  205. begin
  206. // Prevent accidently deleting all files in current or root dir
  207. if (ADir='') or (ADir=PathDelim) then
  208. exit;
  209. if FindFirst(ADir+PathDelim+AllFiles,faAnyFile, Info)=0 then
  210. try
  211. repeat
  212. if (Info.Attr and faDirectory)=faDirectory then
  213. begin
  214. if (Info.Name<>'.') and (Info.Name<>'..') then
  215. DeleteDir(ADir+PathDelim+Info.Name)
  216. end
  217. else
  218. DeleteFile(ADir+PathDelim+Info.Name);
  219. until FindNext(Info)<>0;
  220. finally
  221. FindClose(Info);
  222. end;
  223. RemoveDir(Adir);
  224. end;
  225. Procedure SearchFiles(SL:TStringList;const APattern:string);
  226. var
  227. Info : TSearchRec;
  228. ADir : string;
  229. begin
  230. ADir:=ExtractFilePath(APattern);
  231. if FindFirst(APattern,faAnyFile, Info)=0 then
  232. try
  233. repeat
  234. if (Info.Attr and faDirectory)=faDirectory then
  235. begin
  236. if (Info.Name<>'.') and (Info.Name<>'..') then
  237. SearchFiles(SL,ADir+Info.Name+PathDelim+ExtractFileName(APattern))
  238. end;
  239. SL.Add(ADir+Info.Name);
  240. until FindNext(Info)<>0;
  241. finally
  242. FindClose(Info);
  243. end;
  244. end;
  245. //
  246. // if use_shell defined uses sysutils.executeprocess else uses 'process'
  247. //
  248. function GetCompilerInfo(const ACompiler,AOptions:string):string;
  249. const
  250. BufSize = 1024;
  251. var
  252. {$IFDEF USE_SHELL}
  253. TmpFileName, ProcIDStr: shortstring;
  254. TmpFile: file;
  255. CmdLine2: string;
  256. {$ELSE USE_SHELL}
  257. S: TProcess;
  258. {$ENDIF USE_SHELL}
  259. Buf: array [0..BufSize - 1] of char;
  260. Count: longint;
  261. begin
  262. {$IFDEF USE_SHELL}
  263. Str (GetProcessID, ProcIDStr);
  264. TmpFileName := GetEnvironmentVariable ('TEMP');
  265. if TmpFileName <> '' then
  266. TmpFileName := TmpFileName + DirectorySeparator + 'fppkgout.' + ProcIDStr
  267. else
  268. TmpfileName := 'fppkgout.' + ProcIDStr;
  269. CmdLine2 := '/C ' + ACompiler + ' ' + AOptions + ' > ' + TmpFileName;
  270. SysUtils.ExecuteProcess (GetEnvironmentVariable ('COMSPEC'), CmdLine2);
  271. Assign (TmpFile, TmpFileName);
  272. Reset (TmpFile, 1);
  273. BlockRead (TmpFile, Buf, BufSize, Count);
  274. Close (TmpFile);
  275. {$ELSE USE_SHELL}
  276. S:=TProcess.Create(Nil);
  277. S.Commandline:=ACompiler+' '+AOptions;
  278. S.Options:=[poUsePipes];
  279. S.execute;
  280. Count:=s.output.read(buf,BufSize);
  281. S.Free;
  282. {$ENDIF USE_SHELL}
  283. SetLength(Result,Count);
  284. Move(Buf,Result[1],Count);
  285. end;
  286. function IsSuperUser:boolean;
  287. begin
  288. {$ifdef unix}
  289. result:=(fpGetUID=0);
  290. {$else unix}
  291. result:=true;
  292. {$endif unix}
  293. end;
  294. end.