vppcx64.pp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. {
  2. Copyright (c) 2000-2002 by Pierre Muller
  3. This program allows to run the Makefiles
  4. with the compiler running inside valgrind
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************}
  17. {$mode objfpc}
  18. { Use ansitrings for long PATH variables }
  19. {$H+}
  20. program fpc_with_valgrind;
  21. {
  22. This program uses:
  23. -- 'valgrind.fpc' is an optional file in starting directory
  24. that can contain optional commmand line parameters for valgrind.
  25. -- 'VALGRIND_FPC' is an optional environment variable
  26. that can contain optional commmand line parameters for valgrind.
  27. Note that valgrind also parses:
  28. -- '~/.valgrindrc' user file.
  29. -- 'VALGRIND_OPTS' environment variable.
  30. -- './.valgrindrc' local file.
  31. Use EXTDEBUG conditional to get debug information.
  32. }
  33. uses
  34. sysutils,
  35. dos;
  36. const
  37. {$ifdef Unix}
  38. ValgrindPasExeName : String = 'valgrindpas';
  39. ValgrindDefaultExeName = 'valgrind';
  40. DefaultCompilerName = 'ppcx64';
  41. PathSep=':';
  42. DirSep = '/';
  43. {$else}
  44. ValgrindPasExeName : String = 'valgrindpas.exe';
  45. ValgrindDefaultExeName = 'valgrind.exe';
  46. DefaultCompilerName = 'ppcx64.exe';
  47. PathSep=';';
  48. DirSep = '\';
  49. {$endif not linux}
  50. { If you add a valgrind.fpc file in a given directory }
  51. { This executable will read it; this allows you to add }
  52. { specific command line options to valgrind call. PM }
  53. FpcValgrindIniName : string = 'valgrind.fpc';
  54. { Dos/Windows Valgrind still need forward slashes }
  55. procedure AdaptToValgrind(var filename : string);
  56. var
  57. i : longint;
  58. begin
  59. for i:=1 to length(filename) do
  60. if filename[i]='\' then
  61. filename[i]:='/';
  62. end;
  63. var
  64. valgrind_args,
  65. env_value,
  66. all_args : String;
  67. ValGrindExeName : String;
  68. CompilerName : String;
  69. FullCompilerName : String;
  70. {$ifdef linux}
  71. argv0 : pchar;
  72. {$endif}
  73. Dir,Name,Ext,Param : ShortString;
  74. ValgrindExitCode,i : longint;
  75. line : string;
  76. f : text;
  77. begin
  78. all_args:='';
  79. valgrind_args:='';
  80. if FileExists('.'+DirSep+FpcValgrindIniName) then
  81. begin
  82. Assign(F,'.'+DirSep+FpcValgrindIniName);
  83. Reset(F);
  84. while not eof(F) do
  85. begin
  86. readln(f,line);
  87. valgrind_args:=valgrind_args+' '+line;
  88. end;
  89. Close(F);
  90. end;
  91. env_value:=GetEnvironmentVariable('VALGRIND_FPC');
  92. if env_value<>'' then
  93. valgrind_args:=valgrind_args+' '+env_value;
  94. fsplit(paramstr(0),Dir,Name,Ext);
  95. {$ifdef linux}
  96. argv0:=argv[0];
  97. if (argv0 <> '') then
  98. fsplit(argv0,Dir,Name,Ext);
  99. {$endif}
  100. if (length(Name)>3) and (UpCase(Name[1])='V') then
  101. CompilerName:=Copy(Name,2,255)+Ext
  102. else
  103. begin
  104. if (Name+ext = DefaultCompilerName) then
  105. begin
  106. writeln(stderr,'Avoiding infinite recursion with ',Name+Ext,' binary');
  107. halt(1);
  108. end;
  109. CompilerName:=DefaultCompilerName;
  110. end;
  111. if FileExists(Dir+CompilerName) then
  112. FullCompilerName:=Dir+CompilerName
  113. else
  114. FullCompilerName:=filesearch(CompilerName,Dir+PathSep+GetEnvironmentVariable('PATH'));
  115. if FullCompilerName='' then
  116. begin
  117. writeln(stderr,'Unable to find ',CompilerName,' binary');
  118. halt(2);
  119. end;
  120. { support for info functions directly : used in makefiles }
  121. if (paramcount=1) and (pos('-i',Paramstr(1))=1) then
  122. begin
  123. Exec(FullCompilerName,Paramstr(1));
  124. exit;
  125. end;
  126. {$ifdef EXTDEBUG}
  127. writeln(stderr,'Using compiler "',FullCompilerName,'"');
  128. flush(stderr);
  129. {$endif}
  130. { this will not work correctly if there are " or '' inside the command line :( }
  131. for i:=1 to Paramcount do
  132. begin
  133. Param:=Paramstr(i);
  134. if pos(' ',Param)>0 then
  135. all_args:=all_args+' "'+Param+'"'
  136. else
  137. all_args:=all_args+' '+Param;
  138. end;
  139. ValgrindExeName:=filesearch(ValgrindPasExeName,Dir+PathSep+GetEnvironmentVariable('PATH'));
  140. if ValgrindExeName='' then
  141. ValgrindExeName:=filesearch(ValgrindDefaultExeName,Dir+PathSep+GetEnvironmentVariable('PATH'));
  142. if ValgrindExeName='' then
  143. begin
  144. writeln('Unable to find ',ValgrindDefaultExeName,' and ',ValgrindPasExeName);
  145. halt(3);
  146. end;
  147. AdaptToValgrind(FullCompilerName);
  148. {$ifdef EXTDEBUG}
  149. Writeln(stderr,'Starting ',ValgrindExeName+' '+valgrind_args+' '+FullCompilerName+all_args);
  150. flush(stderr);
  151. {$endif}
  152. ValgrindExitCode:=ExecuteProcess(ValgrindExeName,valgrind_args+' '+FullCompilerName+all_args);
  153. if (ValgrindExitCode<>0) then
  154. begin
  155. Writeln('Error running Valgrind');
  156. Writeln('ExecuteProcess return value = ',ValgrindExitCode);
  157. RunError(ValgrindExitCode);
  158. end;
  159. end.