Skip to content

Commit aa33609

Browse files
committed
Clean up linux includes setup.
Includes setup was moved to ParserOptions.cs and is used just like `SetupMSVC()` and `SetupXcode()`. Native compiler is queried for it's type and version. Correct include paths are set up in cases where system has multiple compilers installed as well.
1 parent 4e735b5 commit aa33609

File tree

3 files changed

+59
-56
lines changed

3 files changed

+59
-56
lines changed

src/CLI/Generator.cs

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44
using CppSharp.Passes;
55
using System;
66
using System.Collections.Generic;
7+
using System.Diagnostics;
78
using System.IO;
89
using System.Linq;
910
using System.Text;
11+
using System.Text.RegularExpressions;
1012
using CppAbi = CppSharp.Parser.AST.CppAbi;
1113

1214
namespace CppSharp
@@ -193,34 +195,7 @@ public void Setup(Driver driver)
193195

194196
private void SetupLinuxOptions(ParserOptions parserOptions)
195197
{
196-
parserOptions.MicrosoftMode = false;
197-
parserOptions.NoBuiltinIncludes = true;
198-
199-
var headersPath = string.Empty;
200-
201-
// Search for the available GCC versions on the provided headers.
202-
var versions = Directory.EnumerateDirectories(Path.Combine(headersPath, "usr/include/c++"));
203-
204-
if (versions.Count() == 0)
205-
throw new Exception("No valid GCC version found on system include paths");
206-
207-
string gccVersionPath = versions.First();
208-
string gccVersion = gccVersionPath.Substring(gccVersionPath.LastIndexOf(Path.DirectorySeparatorChar) + 1);
209-
210-
string[] systemIncludeDirs = {
211-
Path.Combine("usr", "include", "c++", gccVersion),
212-
Path.Combine("usr", "include", "x86_64-linux-gnu", "c++", gccVersion),
213-
Path.Combine("usr", "include", "c++", gccVersion, "backward"),
214-
Path.Combine("usr", "lib", "gcc", "x86_64-linux-gnu", gccVersion, "include"),
215-
Path.Combine("usr", "lib", "gcc", "x86_64-pc-linux-gnu", gccVersion, "include"),
216-
Path.Combine("usr", "include", "x86_64-linux-gnu"),
217-
Path.Combine("usr", "include", "x86_64-pc-linux-gnu"),
218-
Path.Combine("usr", "include")
219-
};
220-
221-
foreach (var dir in systemIncludeDirs)
222-
parserOptions.AddSystemIncludeDirs(Path.Combine(headersPath, dir));
223-
198+
parserOptions.SetupLinux();
224199
parserOptions.AddDefines("_GLIBCXX_USE_CXX11_ABI=" + (options.Cpp11ABI ? "1" : "0"));
225200
}
226201

src/CppParser/ParserGen/ParserGen.cs

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -98,32 +98,7 @@ private void SetupLinuxOptions(ParserOptions options)
9898

9999
var headersPath = Platform.IsLinux ? string.Empty :
100100
Path.Combine(GetSourceDirectory("build"), "headers", "x86_64-linux-gnu");
101-
102-
// Search for the available GCC versions on the provided headers.
103-
var versions = Directory.EnumerateDirectories(Path.Combine(headersPath,
104-
"usr/include/c++"));
105-
106-
if (versions.Count() == 0)
107-
throw new Exception("No valid GCC version found on system include paths");
108-
109-
string gccVersionPath = versions.First();
110-
string gccVersion = gccVersionPath.Substring(
111-
gccVersionPath.LastIndexOf(Path.DirectorySeparatorChar) + 1);
112-
113-
string[] systemIncludeDirs = {
114-
Path.Combine("usr", "include", "c++", gccVersion),
115-
Path.Combine("usr", "include", "x86_64-linux-gnu", "c++", gccVersion),
116-
Path.Combine("usr", "include", "c++", gccVersion, "backward"),
117-
Path.Combine("usr", "lib", "gcc", "x86_64-linux-gnu", gccVersion, "include"),
118-
Path.Combine("usr", "lib", "gcc", "x86_64-pc-linux-gnu", gccVersion, "include"),
119-
Path.Combine("usr", "include", "x86_64-linux-gnu"),
120-
Path.Combine("usr", "include", "x86_64-pc-linux-gnu"),
121-
Path.Combine("usr", "include")
122-
};
123-
124-
foreach (var dir in systemIncludeDirs)
125-
options.AddSystemIncludeDirs(Path.Combine(headersPath, dir));
126-
101+
options.SetupLinux(headersPath);
127102
options.AddDefines("_GLIBCXX_USE_CXX11_ABI=" + (IsGnuCpp11Abi ? "1" : "0"));
128103
}
129104

@@ -237,4 +212,4 @@ public static void Main(string[] args)
237212
}
238213
}
239214
}
240-
}
215+
}

src/Parser/ParserOptions.cs

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
using CppSharp.Parser.AST;
1+
using System;
2+
using CppSharp.Parser.AST;
23
using System.Reflection;
34
using LanguageVersion = CppSharp.Parser.LanguageVersion;
45
using System.Globalization;
56
using System.Collections.Generic;
7+
using System.Diagnostics;
68
using System.Linq;
9+
using System.Text.RegularExpressions;
710

811
namespace CppSharp.Parser
912
{
@@ -218,6 +221,53 @@ public void SetupXcode()
218221
AddArguments("-stdlib=libc++");
219222
}
220223

224+
private void GetUnixCompilerInfo(out string compiler, out string version)
225+
{
226+
var info = new ProcessStartInfo(Environment.GetEnvironmentVariable("CXX") ?? "gcc", "-v");
227+
info.RedirectStandardError = true;
228+
info.CreateNoWindow = true;
229+
info.UseShellExecute = false;
230+
var process = Process.Start(info);
231+
if (process == null)
232+
throw new SystemException("GCC compiler was not found.");
233+
process.WaitForExit();
234+
235+
var output = process.StandardError.ReadToEnd();
236+
var match = Regex.Match(output, "(gcc|clang) version ([0-9\\.]+)");
237+
if (!match.Success)
238+
throw new SystemException("GCC compiler was not found.");
239+
240+
compiler = match.Groups[1].ToString();
241+
version = match.Groups[2].ToString();
242+
}
243+
244+
public void SetupLinux(string headersPath="")
245+
{
246+
MicrosoftMode = false;
247+
NoBuiltinIncludes = true;
248+
NoStandardIncludes = true;
249+
Abi = CppAbi.Itanium;
250+
251+
string compiler, version;
252+
GetUnixCompilerInfo(out compiler, out version);
253+
Console.WriteLine($"Compiler version: {compiler}/{version}");
254+
AddSystemIncludeDirs($"{headersPath}/usr/include");
255+
if (compiler == "gcc")
256+
{
257+
AddSystemIncludeDirs($"{headersPath}/usr/include/c++/{version}");
258+
AddSystemIncludeDirs($"{headersPath}/usr/include/x86_64-linux-gnu/c++/{version}");
259+
AddSystemIncludeDirs($"{headersPath}/usr/include/c++/{version}/backward");
260+
}
261+
string[] tripples = {"x86_64-linux-gnu", "x86_64-pc-linux-gnu"};
262+
foreach (var tripple in tripples)
263+
{
264+
AddSystemIncludeDirs($"{headersPath}/usr/lib/{compiler}/{tripple}/{version}/include");
265+
AddSystemIncludeDirs($"{headersPath}/usr/lib/{compiler}/{tripple}/{version}/include/c++");
266+
AddSystemIncludeDirs($"{headersPath}/usr/lib/{compiler}/{tripple}/{version}/include/c++/{tripple}");
267+
AddSystemIncludeDirs($"{headersPath}/usr/include/{tripple}");
268+
}
269+
}
270+
221271
public void Setup()
222272
{
223273
SetupArguments();
@@ -291,6 +341,9 @@ private void SetupIncludes()
291341
case TargetPlatform.MacOS:
292342
SetupXcode();
293343
break;
344+
case TargetPlatform.Linux:
345+
SetupLinux();
346+
break;
294347
}
295348
}
296349
}

0 commit comments

Comments
 (0)