fpmake.pp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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,GdbVerIncFile: string;
  14. GdbLibFound: boolean;
  15. GdbintTarget, GdbconTarget, 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. GdbconTarget:=TTarget(p.Targets.ItemByName(Prefix+'gdbcon'));
  63. GdbVerIncFile:=IncludeTrailingPathDelimiter(P.GetUnitsOutputDir(Defaults.CompileTarget))+'gdbver.inc';
  64. if GdbLibFound then
  65. Installer.BuildEngine.Log(vlCommand,'File libgdb.a found ('+GdbLibFile+')')
  66. else
  67. Installer.BuildEngine.Log(vlCommand,'File libgdb.a not found');
  68. if GdbLibFound then
  69. begin
  70. // Detect if gdblib.inc is available
  71. if FileExists(GDBLibDir+PathDelim+'gdblib.inc') then
  72. begin
  73. P.Options.Add('-dUSE_GDBLIBINC');
  74. P.Options.Add('-Fi'+GdbLibDir);
  75. P.Options.Add('-Fl'+GdbLibDir);
  76. // No need to use gdbver in this case
  77. p.Targets.Delete(GdbVerTarget.Index);
  78. Installer.BuildEngine.Log(vlCommand,'Using gdblib.inc include file')
  79. end
  80. // When we're cross-compiling, running the gdbver executable to detect the
  81. // gdb-version is not possible, unless a i386-win32 to
  82. // i386-go32v2 compilation is performed.
  83. else if (not Defaults.IsBuildDifferentFromTarget
  84. or ((Defaults.CPU=i386) and (Defaults.OS=go32v2) and
  85. (Defaults.BuildOS=win32) and (Defaults.BuildCPU=i386))
  86. ) then
  87. begin
  88. P.Options.Add('-Fl'+GdbLibDir);
  89. Installer.BuildEngine.CreateOutputDir(p);
  90. Installer.BuildEngine.Log(vlCommand,'GDB-lib found, compiling and running gdbver to obtain GDB-version');
  91. Installer.BuildEngine.Compile(P,GdbVerTarget);
  92. Cmd:=Installer.BuildEngine.AddPathPrefix(p,
  93. p.GetBinOutputDir(Defaults.CompileTarget ))+
  94. PathDelim+
  95. AddProgramExtension('gdbver',Defaults.BuildOS);
  96. Opts:=TStringList.Create;
  97. try
  98. Opts.Add('-o');
  99. Opts.Add(Installer.BuildEngine.AddPathPrefix(p,GdbVerIncFile));
  100. Installer.BuildEngine.ExecuteCommand(Cmd,Opts);
  101. finally
  102. opts.Free;
  103. end;
  104. with GdbintTarget.Dependencies do
  105. AddInclude('gdbver.inc');
  106. GdbintTarget.IncludePath.Add(P.GetUnitsOutputDir(Defaults.CompileTarget));
  107. with GdbconTarget.Dependencies do
  108. AddInclude('gdbver.inc');
  109. GdbconTarget.IncludePath.Add(P.GetUnitsOutputDir(Defaults.CompileTarget));
  110. // Pass -dUSE_MINGW_GDB to the compiler when a MinGW gdb is used
  111. if FileExists(GdbLibDir+PathDelim+MinGWGdbLibName) then
  112. begin
  113. P.Options.Add('-dUSE_MINGW_GDB');
  114. Installer.BuildEngine.Log(vlCommand,'Using GDB (MinGW)')
  115. end
  116. else
  117. begin
  118. Installer.BuildEngine.Log(vlCommand,'Using GDB')
  119. end;
  120. end
  121. end
  122. else
  123. begin
  124. // No suitable gdb found
  125. // No need to compile gdbver.
  126. p.Targets.Delete(GdbVerTarget.Index);
  127. // use gdb_nogdb.inc
  128. L := TStringList.Create;
  129. try
  130. if P.Directory<>'' then
  131. L.values['src'+DirectorySeparator+'gdbver_nogdb.inc'] := IncludeTrailingPathDelimiter(P.Directory) +GdbVerIncFile
  132. else
  133. L.values['src'+DirectorySeparator+'gdbver_nogdb.inc'] := GdbVerIncFile;
  134. Installer.BuildEngine.cmdcopyfiles(L, Installer.BuildEngine.StartDir, nil);
  135. with GdbintTarget.Dependencies do
  136. AddInclude('gdbver.inc');
  137. GdbintTarget.IncludePath.Add(P.GetUnitsOutputDir(Defaults.CompileTarget));
  138. with GdbconTarget.Dependencies do
  139. AddInclude('gdbver.inc');
  140. GdbconTarget.IncludePath.Add(P.GetUnitsOutputDir(Defaults.CompileTarget));
  141. finally
  142. L.Free;
  143. end;
  144. end;
  145. end;
  146. procedure AfterClean_gdbint(Sender: TObject);
  147. var
  148. L : TStrings;
  149. P : TPackage;
  150. GdbVerIncFile: string;
  151. begin
  152. // Remove the generated gdbver.inc
  153. L := TStringList.Create;
  154. P := Sender as TPackage;
  155. try
  156. // This procedure is called at gdbint directory level
  157. GdbVerIncFile:=IncludeTrailingPathDelimiter(P.GetUnitsOutputDir(Defaults.CompileTarget))+'gdbver.inc';
  158. L.add(GdbVerIncFile);
  159. Installer.BuildEngine.CmdDeleteFiles(L);
  160. finally
  161. L.Free;
  162. end;
  163. end;
  164. procedure add_gdbint(const ADirectory: string);
  165. Var
  166. P : TPackage;
  167. T : TTarget;
  168. begin
  169. With Installer do
  170. begin
  171. P:=AddPackage('gdbint');
  172. P.ShortName:='gdb';
  173. P.Directory:=ADirectory;
  174. P.Version:='3.3.1';
  175. P.Author := 'Library : Cygnus, header: Peter Vreman';
  176. P.License := 'Library: GPL2 or later, header: LGPL with modification, ';
  177. P.HomepageURL := 'www.freepascal.org';
  178. P.Email := '';
  179. P.Description := 'Interface to libgdb, the GDB debugger in library format';
  180. P.NeedLibC:= true; // true for headers that indirectly link to libc?
  181. // In case of using a buildunit, it is not possible to compile a single
  182. // file within the BeforeCompile event.
  183. P.SupportBuildModes:= [bmOneByOne];
  184. P.OSes:=[aix,beos,haiku,freebsd,netbsd,openbsd,linux,win32,win64,go32v2,dragonfly,solaris];
  185. P.SourcePath.Add('src');
  186. P.IncludePath.Add('src');
  187. P.BeforeCompileProc:=@BeforeCompile_gdbint;
  188. P.AfterCleanProc:=@AfterClean_gdbint;
  189. T := p.Targets.AddProgram('src'+PathDelim+'gdbver.pp');
  190. T.Install := false;
  191. //
  192. // NOTE: the gdbver.inc dependencies gives warnings because the makefile.fpc
  193. // does a "cp src/gdbver_nogdb.inc src/gdbver.inc" to create it
  194. T:=P.Targets.AddUnit('gdbcon.pp');
  195. with T.Dependencies do
  196. begin
  197. AddUnit('gdbint');
  198. end;
  199. T:=P.Targets.AddUnit('gdbint.pp');
  200. P.ExamplePath.add('examples');
  201. P.Targets.AddExampleProgram('testgdb.pp');
  202. P.Targets.AddExampleProgram('symify.pp');
  203. P.Targets.AddExampleUnit('mingw.pas');
  204. P.Sources.AddSrc('src/gdbver_nogdb.inc');
  205. P.NamespaceMap:='namespaces.lst';
  206. end;
  207. end;
  208. {$ifndef ALLPACKAGES}
  209. begin
  210. add_gdbint('');
  211. Installer.Run;
  212. end.
  213. {$endif ALLPACKAGES}