vppcx64.pp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 several files :
  23. -- 'valgrind.fpc' is an optional file that can contain optional
  24. commmand line parameters for valgrind.
  25. Use EXTDEBUG conditional to get debug information.
  26. }
  27. uses
  28. sysutils,
  29. dos;
  30. const
  31. {$ifdef Unix}
  32. ValgrindPasExeName : String = 'valgrindpas';
  33. ValgrindDefaultExeName = 'valgrind';
  34. DefaultCompilerName = 'ppcx64';
  35. PathSep=':';
  36. DirSep = '/';
  37. {$else}
  38. ValgrindPasExeName : String = 'valgrindpas.exe';
  39. ValgrindDefaultExeName = 'valgrind.exe';
  40. DefaultCompilerName = 'ppcx64.exe';
  41. PathSep=';';
  42. DirSep = '\';
  43. {$endif not linux}
  44. { If you add a valgrind.fpc file in a given directory }
  45. { This executable will read it; this allows you to add }
  46. { specific command line options to valgrind call. PM }
  47. FpcValgrindIniName : string = 'valgrind.fpc';
  48. { Dos/Windows Valgrind still need forward slashes }
  49. procedure AdaptToValgrind(var filename : string);
  50. var
  51. i : longint;
  52. begin
  53. for i:=1 to length(filename) do
  54. if filename[i]='\' then
  55. filename[i]:='/';
  56. end;
  57. var
  58. valgrind_args,
  59. all_args : String;
  60. ValGrindExeName : String;
  61. CompilerName : String;
  62. FullCompilerName : String;
  63. {$ifdef linux}
  64. argv0 : pchar;
  65. {$endif}
  66. Dir,Name,Ext,Param : ShortString;
  67. ValgrindExitCode,i : longint;
  68. line : string;
  69. f : text;
  70. begin
  71. all_args:='';
  72. valgrind_args:='';
  73. if FileExists('.'+DirSep+FpcValgrindIniName) then
  74. begin
  75. Assign(F,'.'+DirSep+FpcValgrindIniName);
  76. Reset(F);
  77. while not eof(F) do
  78. begin
  79. readln(f,line);
  80. valgrind_args:=valgrind_args+' '+line;
  81. end;
  82. Close(F);
  83. end;
  84. fsplit(paramstr(0),Dir,Name,Ext);
  85. {$ifdef linux}
  86. argv0:=argv[0];
  87. if (argv0 <> '') then
  88. fsplit(argv0,Dir,Name,Ext);
  89. {$endif}
  90. if (length(Name)>3) and (UpCase(Name[1])='V') then
  91. CompilerName:=Copy(Name,2,255)+Ext
  92. else
  93. begin
  94. if (Name+ext = DefaultCompilerName) then
  95. begin
  96. writeln(stderr,'Avoiding infinite recursion with ',Name+Ext,' binary');
  97. halt(1);
  98. end;
  99. CompilerName:=DefaultCompilerName;
  100. end;
  101. FullCompilerName:=filesearch(CompilerName,Dir+PathSep+GetEnvironmentVariable('PATH'));
  102. if FullCompilerName='' then
  103. begin
  104. writeln(stderr,'Unable to find ',CompilerName,' binary');
  105. halt(2);
  106. end;
  107. { support for info functions directly : used in makefiles }
  108. if (paramcount=1) and (pos('-i',Paramstr(1))=1) then
  109. begin
  110. Exec(FullCompilerName,Paramstr(1));
  111. exit;
  112. end;
  113. {$ifdef EXTDEBUG}
  114. writeln(stderr,'Using compiler "',FullCompilerName,'"');
  115. flush(stderr);
  116. {$endif}
  117. { this will not work correctly if there are " or '' inside the command line :( }
  118. for i:=1 to Paramcount do
  119. begin
  120. Param:=Paramstr(i);
  121. if pos(' ',Param)>0 then
  122. all_args:=all_args+' "'+Param+'"'
  123. else
  124. all_args:=all_args+' '+Param;
  125. end;
  126. ValgrindExeName:=filesearch(ValgrindPasExeName,Dir+PathSep+GetEnvironmentVariable('PATH'));
  127. if ValgrindExeName='' then
  128. ValgrindExeName:=filesearch(ValgrindDefaultExeName,Dir+PathSep+GetEnvironmentVariable('PATH'));
  129. if ValgrindExeName='' then
  130. begin
  131. writeln('Unable to find ',ValgrindDefaultExeName,' and ',ValgrindPasExeName);
  132. halt(3);
  133. end;
  134. AdaptToValgrind(FullCompilerName);
  135. {$ifdef EXTDEBUG}
  136. Writeln(stderr,'Starting ',ValgrindExeName+' '+valgrind_args+' '+FullCompilerName+all_args);
  137. flush(stderr);
  138. {$endif}
  139. ValgrindExitCode:=ExecuteProcess(ValgrindExeName,valgrind_args+' '+FullCompilerName+all_args);
  140. if (ValgrindExitCode<>0) then
  141. begin
  142. Writeln('Error running Valgrind');
  143. Writeln('ExecuteProcess return value = ',ValgrindExitCode);
  144. RunError(ValgrindExitCode);
  145. end;
  146. end.