fpmake.pp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. {$ifndef ALLPACKAGES}
  2. {$mode objfpc}{$H+}
  3. program fpmake;
  4. uses {$ifdef unix}cthreads,{$endif} fpmkunit, classes, sysutils;
  5. {$endif ALLPACKAGES}
  6. const
  7. GdbLibName = 'libgdb.a';
  8. MinGWGdbLibName = 'libmingw32.a';
  9. procedure BeforeCompile_gdbint(Sender: TObject);
  10. var
  11. L : TStrings;
  12. P : TPackage;
  13. Cmd,GdbLibDir, GdbLibFile: string;
  14. GdbLibFound: boolean;
  15. GdbintTarget, GdbVerTarget: TTarget;
  16. Opts : TStrings;
  17. Prefix : String;
  18. begin
  19. P := Sender as TPackage;
  20. Prefix:='';
  21. if Defaults.Namespaces then
  22. Prefix:='Api.';
  23. // Search for a libgdb file.
  24. GdbLibFound:=false;
  25. // First try the environment setting GDBLIBDIR
  26. GdbLibDir := GetEnvironmentVariable('GDBLIBDIR');
  27. if (GdbLibDir<>'') then
  28. begin
  29. if DirectoryExists(GdbLibDir) then
  30. begin
  31. GdbLibFile:=IncludeTrailingPathDelimiter(GdbLibDir)+GdbLibName;
  32. if not FileExists(GdbLibFile) then
  33. Installer.BuildEngine.Log(vlCommand,
  34. 'GDBLIBDIR environment variable set, but libgdb not found. ('+GdbLibFile+')')
  35. else
  36. GdbLibFound:=true;
  37. end
  38. else
  39. Installer.BuildEngine.Log(vlCommand,
  40. 'GDBLIBDIR environment variable set, but directory does not exist. ('+GdbLibDir+')');
  41. end;
  42. // Try the default locations
  43. if not GdbLibFound then
  44. begin
  45. GdbLibDir:=Installer.BuildEngine.AddPathPrefix(p,'..'+PathDelim+'..'+PathDelim+'libgdb');
  46. if DirectoryExists(GdbLibDir) then
  47. begin
  48. GdbLibDir:=GdbLibDir+PathDelim+OSToString(Defaults.OS);
  49. GdbLibFile:=GdbLibDir+PathDelim+GdbLibName;
  50. if FileExists(GdbLibFile) then
  51. GdbLibFound:=true
  52. else
  53. begin
  54. GdbLibDir:=GdbLibDir+PathDelim+CPUToString(Defaults.CPU);
  55. GdbLibFile:=GdbLibDir+PathDelim+GdbLibName;
  56. GdbLibFound:=FileExists(GdbLibFile);
  57. end;
  58. end;
  59. end;
  60. GdbVerTarget:=TTarget(p.Targets.ItemByName('gdbver'));
  61. GdbintTarget:=TTarget(p.Targets.ItemByName(Prefix+'gdbint'));
  62. if GdbLibFound then
  63. Installer.BuildEngine.Log(vlCommand,'File libgdb.a found ('+GdbLibFile+')')
  64. else
  65. Installer.BuildEngine.Log(vlCommand,'File libgdb.a not found');
  66. if GdbLibFound then
  67. begin
  68. // Detect if gdblib.inc is available
  69. if FileExists(GDBLibDir+PathDelim+'gdblib.inc') then
  70. begin
  71. P.Options.Add('-dUSE_GDBLIBINC');
  72. P.Options.Add('-Fi'+GdbLibDir);
  73. P.Options.Add('-Fl'+GdbLibDir);
  74. // No need to use gdbver in this case
  75. p.Targets.Delete(GdbVerTarget.Index);
  76. Installer.BuildEngine.Log(vlCommand,'Using gdblib.inc include file')
  77. end
  78. // When we're cross-compiling, running the gdbver executable to detect the
  79. // gdb-version is not possible, unless a i386-win32 to
  80. // i386-go32v2 compilation is performed.
  81. else if (not Defaults.IsBuildDifferentFromTarget
  82. or ((Defaults.CPU=i386) and (Defaults.OS=go32v2) and
  83. (Defaults.BuildOS=win32) and (Defaults.BuildCPU=i386))
  84. ) then
  85. begin
  86. P.Options.Add('-Fl'+GdbLibDir);
  87. Installer.BuildEngine.CreateOutputDir(p);
  88. Installer.BuildEngine.Log(vlCommand,'GDB-lib found, compiling and running gdbver to obtain GDB-version');
  89. Installer.BuildEngine.Compile(P,GdbVerTarget);
  90. Cmd:=Installer.BuildEngine.AddPathPrefix(p,
  91. p.GetBinOutputDir(Defaults.CompileTarget ))+
  92. PathDelim+
  93. AddProgramExtension('gdbver',Defaults.BuildOS);
  94. Opts:=TStringList.Create;
  95. try
  96. Opts.Add('-o');
  97. Opts.Add(Installer.BuildEngine.AddPathPrefix(p,'src'+PathDelim+'gdbver.inc'));
  98. Installer.BuildEngine.ExecuteCommand(Cmd,Opts);
  99. finally
  100. opts.Free;
  101. end;
  102. with GdbintTarget.Dependencies do
  103. AddInclude('gdbver.inc');
  104. // Pass -dUSE_MINGW_GDB to the compiler when a MinGW gdb is used
  105. if FileExists(GdbLibDir+PathDelim+MinGWGdbLibName) then
  106. begin
  107. P.Options.Add('-dUSE_MINGW_GDB');
  108. Installer.BuildEngine.Log(vlCommand,'Using GDB (MinGW)')
  109. end
  110. else
  111. begin
  112. Installer.BuildEngine.Log(vlCommand,'Using GDB')
  113. end;
  114. end
  115. end
  116. else
  117. begin
  118. // No suitable gdb found
  119. // No need to compile gdbver.
  120. p.Targets.Delete(GdbVerTarget.Index);
  121. // use gdb_nogdb.inc
  122. L := TStringList.Create;
  123. try
  124. if P.Directory<>'' then
  125. L.values['src'+DirectorySeparator+'gdbver_nogdb.inc'] := IncludeTrailingPathDelimiter(P.Directory) +'src'+DirectorySeparator+'gdbver.inc'
  126. else
  127. L.values['src'+DirectorySeparator+'gdbver_nogdb.inc'] := 'src'+DirectorySeparator+'gdbver.inc';
  128. Installer.BuildEngine.cmdcopyfiles(L, Installer.BuildEngine.StartDir, nil);
  129. with GdbintTarget.Dependencies do
  130. AddInclude('gdbver.inc');
  131. finally
  132. L.Free;
  133. end;
  134. end;
  135. end;
  136. procedure AfterCompile_gdbint(Sender: TObject);
  137. var
  138. L : TStrings;
  139. P : TPackage;
  140. begin
  141. // Remove the generated gdbver.inc
  142. L := TStringList.Create;
  143. P := Sender as TPackage;
  144. try
  145. if P.Directory<>'' then
  146. L.add(IncludeTrailingPathDelimiter(P.Directory)+'src'+DirectorySeparator+'gdbver.inc')
  147. else
  148. L.add(IncludeTrailingPathDelimiter(Installer.BuildEngine.StartDir)+'src'+DirectorySeparator+'gdbver.inc');
  149. Installer.BuildEngine.CmdDeleteFiles(L);
  150. finally
  151. L.Free;
  152. end;
  153. end;
  154. procedure add_gdbint(const ADirectory: string);
  155. Var
  156. P : TPackage;
  157. T : TTarget;
  158. begin
  159. With Installer do
  160. begin
  161. P:=AddPackage('gdbint');
  162. P.ShortName:='gdb';
  163. P.Directory:=ADirectory;
  164. P.Version:='3.3.1';
  165. P.Author := 'Library : Cygnus, header: Peter Vreman';
  166. P.License := 'Library: GPL2 or later, header: LGPL with modification, ';
  167. P.HomepageURL := 'www.freepascal.org';
  168. P.Email := '';
  169. P.Description := 'Interface to libgdb, the GDB debugger in library format';
  170. P.NeedLibC:= true; // true for headers that indirectly link to libc?
  171. // In case of using a buildunit, it is not possible to compile a single
  172. // file within the BeforeCompile event.
  173. P.SupportBuildModes:= [bmOneByOne];
  174. P.OSes:=[aix,beos,haiku,freebsd,netbsd,openbsd,linux,win32,win64,go32v2,dragonfly,solaris];
  175. P.SourcePath.Add('src');
  176. P.IncludePath.Add('src');
  177. P.BeforeCompileProc:=@BeforeCompile_gdbint;
  178. P.AfterCompileProc:=@AfterCompile_gdbint;
  179. T := p.Targets.AddProgram('src'+PathDelim+'gdbver.pp');
  180. T.Install := false;
  181. //
  182. // NOTE: the gdbver.inc dependencies gives warnings because the makefile.fpc
  183. // does a "cp src/gdbver_nogdb.inc src/gdbver.inc" to create it
  184. T:=P.Targets.AddUnit('gdbcon.pp');
  185. with T.Dependencies do
  186. begin
  187. AddUnit('gdbint');
  188. end;
  189. T:=P.Targets.AddUnit('gdbint.pp');
  190. P.ExamplePath.add('examples');
  191. P.Targets.AddExampleProgram('testgdb.pp');
  192. P.Targets.AddExampleProgram('symify.pp');
  193. P.Targets.AddExampleUnit('mingw.pas');
  194. P.Sources.AddSrc('src/gdbver_nogdb.inc');
  195. P.NamespaceMap:='namespaces.lst';
  196. end;
  197. end;
  198. {$ifndef ALLPACKAGES}
  199. begin
  200. add_gdbint('');
  201. Installer.Run;
  202. end.
  203. {$endif ALLPACKAGES}