1{$ifndef ALLPACKAGES}
2{$mode objfpc}{$H+}
3program fpmake;
4
5uses fpmkunit, classes, sysutils;
6
7{$endif ALLPACKAGES}
8
9const
10  GdbLibName = 'libgdb.a';
11  MinGWGdbLibName = 'libmingw32.a';
12
13procedure BeforeCompile_gdbint(Sender: TObject);
14var
15  L : TStrings;
16  P : TPackage;
17  GdbLibDir, GdbLibFile: string;
18  GdbLibFound: boolean;
19  GdbintTarget, GdbVerTarget: TTarget;
20begin
21  P := Sender as TPackage;
22  // Search for a libgdb file.
23  GdbLibFound:=false;
24
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
43  // Try the default locations
44  if not GdbLibFound then
45    begin
46      GdbLibDir:=Installer.BuildEngine.AddPathPrefix(p,'..'+PathDelim+'..'+PathDelim+'libgdb');
47      if DirectoryExists(GdbLibDir) then
48        begin
49          GdbLibDir:=GdbLibDir+PathDelim+OSToString(Defaults.OS);
50          GdbLibFile:=GdbLibDir+PathDelim+GdbLibName;
51          if FileExists(GdbLibFile) then
52            GdbLibFound:=true
53          else
54            begin
55              GdbLibDir:=GdbLibDir+PathDelim+CPUToString(Defaults.CPU);
56              GdbLibFile:=GdbLibDir+PathDelim+GdbLibName;
57              GdbLibFound:=FileExists(GdbLibFile);
58            end;
59        end;
60    end;
61
62  GdbVerTarget:=TTarget(p.Targets.ItemByName('gdbver'));
63  GdbintTarget:=TTarget(p.Targets.ItemByName('gdbint'));
64
65  if GdbLibFound then
66    Installer.BuildEngine.Log(vlCommand,'File libgdb.a found ('+GdbLibFile+')')
67  else
68    Installer.BuildEngine.Log(vlCommand,'File libgdb.a not found');
69  if GdbLibFound then
70    begin
71      // Detect if gdblib.inc is available
72      if FileExists(GDBLibDir+PathDelim+'gdblib.inc') then
73        begin
74          P.Options.Add('-dUSE_GDBLIBINC');
75          P.Options.Add('-Fi'+GdbLibDir);
76          P.Options.Add('-Fl'+GdbLibDir);
77          // No need to use gdbver in this case
78          p.Targets.Delete(GdbVerTarget.Index);
79          Installer.BuildEngine.Log(vlCommand,'Using gdblib.inc include file')
80        end
81     // When we're cross-compiling, running the gdbver executable to detect the
82     // gdb-version is not possible, unless a i386-win32 to
83     // i386-go32v2 compilation is performed.
84     else if (not Defaults.IsBuildDifferentFromTarget
85       or ((Defaults.CPU=i386) and (Defaults.OS=go32v2) and
86           (Defaults.BuildOS=win32) and (Defaults.BuildCPU=i386))
87     ) then
88    begin
89      P.Options.Add('-Fl'+GdbLibDir);
90      Installer.BuildEngine.CreateOutputDir(p);
91      Installer.BuildEngine.Log(vlCommand,'GDB-lib found, compiling and running gdbver to obtain GDB-version');
92      Installer.BuildEngine.Compile(P,GdbVerTarget);
93      Installer.BuildEngine.ExecuteCommand(Installer.BuildEngine.AddPathPrefix(p,p.
94        GetBinOutputDir(Defaults.CPU, Defaults.OS))+PathDelim+
95        AddProgramExtension('gdbver',Defaults.BuildOS),'-o ' +
96        Installer.BuildEngine.AddPathPrefix(p,'src'+PathDelim+'gdbver.inc'));
97
98      with GdbintTarget.Dependencies do
99        AddInclude('gdbver.inc');
100      // Pass -dUSE_MINGW_GDB to the compiler when a MinGW gdb is used
101      if FileExists(GdbLibDir+PathDelim+MinGWGdbLibName) then
102        begin
103          P.Options.Add('-dUSE_MINGW_GDB');
104          Installer.BuildEngine.Log(vlCommand,'Using GDB (MinGW)')
105        end
106      else
107        begin
108          Installer.BuildEngine.Log(vlCommand,'Using GDB')
109        end;
110    end
111    end
112  else
113    begin
114      // No suitable gdb found
115      // No need to compile gdbver.
116      p.Targets.Delete(GdbVerTarget.Index);
117      // use gdb_nogdb.inc
118      L := TStringList.Create;
119      try
120        if P.Directory<>'' then
121          L.values['src'+DirectorySeparator+'gdbver_nogdb.inc'] := IncludeTrailingPathDelimiter(P.Directory) +'src'+DirectorySeparator+'gdbver.inc'
122        else
123          L.values['src'+DirectorySeparator+'gdbver_nogdb.inc'] := 'src'+DirectorySeparator+'gdbver.inc';
124        Installer.BuildEngine.cmdcopyfiles(L, Installer.BuildEngine.StartDir, nil);
125        with GdbintTarget.Dependencies do
126          AddInclude('gdbver.inc');
127      finally
128        L.Free;
129      end;
130
131    end;
132end;
133
134procedure AfterCompile_gdbint(Sender: TObject);
135var
136  L : TStrings;
137  P : TPackage;
138begin
139  // Remove the generated gdbver.inc
140  L := TStringList.Create;
141  P := Sender as TPackage;
142  try
143    if P.Directory<>'' then
144      L.add(IncludeTrailingPathDelimiter(P.Directory)+'src'+DirectorySeparator+'gdbver.inc')
145    else
146      L.add(IncludeTrailingPathDelimiter(Installer.BuildEngine.StartDir)+'src'+DirectorySeparator+'gdbver.inc');
147    Installer.BuildEngine.CmdDeleteFiles(L);
148  finally
149    L.Free;
150  end;
151end;
152
153procedure add_gdbint(const ADirectory: string);
154
155Var
156  P : TPackage;
157  T : TTarget;
158begin
159  With Installer do
160    begin
161    P:=AddPackage('gdbint');
162    P.ShortName:='gdb';
163    P.Directory:=ADirectory;
164    P.Version:='3.2.2';
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
175    P.OSes:=[aix,beos,haiku,freebsd,netbsd,openbsd,linux,win32,win64,go32v2,dragonfly,solaris];
176
177    P.SourcePath.Add('src');
178    P.IncludePath.Add('src');
179
180    P.BeforeCompileProc:=@BeforeCompile_gdbint;
181    P.AfterCompileProc:=@AfterCompile_gdbint;
182
183    T := p.Targets.AddProgram('src'+PathDelim+'gdbver.pp');
184    T.Install := false;
185    //
186    // NOTE: the gdbver.inc dependencies gives warnings because the makefile.fpc
187    // does a "cp src/gdbver_nogdb.inc src/gdbver.inc" to create it
188
189    T:=P.Targets.AddUnit('gdbcon.pp');
190      with T.Dependencies do
191        begin
192          AddUnit('gdbint');
193        end;
194    T:=P.Targets.AddUnit('gdbint.pp');
195    P.ExamplePath.add('examples');
196    P.Targets.AddExampleProgram('testgdb.pp');
197    P.Targets.AddExampleProgram('symify.pp');
198    P.Targets.AddExampleUnit('mingw.pas');
199
200    P.Sources.AddSrc('src/gdbver_nogdb.inc');
201    end;
202end;
203
204{$ifndef ALLPACKAGES}
205begin
206  add_gdbint('');
207  Installer.Run;
208end.
209{$endif ALLPACKAGES}
210
211