Skip to content

Commit f7e6db2

Browse files
authored
Merge pull request #728 from zillemarco/master
Adding CLI tools for CppSharp
2 parents 60f5da9 + 4e5778d commit f7e6db2

File tree

3 files changed

+512
-0
lines changed

3 files changed

+512
-0
lines changed

src/CLI/CLI.cs

Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Security.Policy;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
8+
namespace CppSharp
9+
{
10+
class CLI
11+
{
12+
private static Options _options = new Options();
13+
private static List<string> _assemblies;
14+
15+
static void AddIncludeDirs(String dir)
16+
{
17+
_options.IncludeDirs.Add(dir);
18+
}
19+
20+
static void ParseCommandLineArgs(string[] args)
21+
{
22+
var showHelp = args.Length == 0;
23+
24+
var optionSet = new Mono.Options.OptionSet()
25+
{
26+
{ "h|header=", "the path to an header file to generate source from", h => _options.HeaderFiles.Add(h) },
27+
{ "pa|path=", "the path of a folder whose files will generate code (can append a filter at the end like '<path>/*.hpp'", pa => { GetFilesFromPath(pa); } },
28+
{ "inc|includedir=", "the path of a folder to search for include files", i => { AddIncludeDirs(i); } },
29+
{ "l|library=", "the path of a library that includes the definitions for the generated source code", l => _options.Libraries.Add(l) },
30+
{ "ld|librarydir=", "the path of a folder to search for additional libraries", l => _options.LibraryDirs.Add(l) },
31+
{ "d|define=", "a define to add for the parse of the given header files", d => _options.Defines.Add(d) },
32+
{ "od|outputdir=", "the path for the destination folder that will contain the generated code", od => _options.OutputDir = od },
33+
{ "on|outputnamespace=", "the namespace that will be used for the generated code", on => _options.OutputNamespace = on },
34+
{ "iln|inputlibraryname=", "the name of the shared library that contains the actual definitions (without extension)", iln => _options.InputLibraryName = iln },
35+
{ "isln|inputsharedlibraryname=", "the full name of the shared library that contains the actual definitions (with extension)", isln => _options.InputSharedLibraryName = isln },
36+
{ "gen|generator=", "the type of generated code: 'chsarp' or 'cli' ('cli' supported only for Windows)", g => { GetGeneratorKind(g); } },
37+
{ "p|platform=", "the platform that the generated code will target: 'win', 'osx', 'linux'", p => { GetDestinationPlatform(p); } },
38+
{ "a|arch=", "the architecture that the generated code will target: 'x86', 'x64'", a => { GetDestinationArchitecture(a); } },
39+
{ "c++11", "enables GCC C++ 11 compilation (valid only for Linux platform)", cpp11 => { _options.Cpp11ABI = (cpp11 != null); } },
40+
{ "cs|checksymbols", "enable the symbol check for the generated code", cs => { _options.CheckSymbols = (cs != null); } },
41+
{ "ub|unitybuild", "enable unity build", ub => { _options.UnityBuild = (ub != null); } },
42+
{ "help", "shows the help", hl => { showHelp = (hl != null); } }
43+
};
44+
45+
try
46+
{
47+
_assemblies = optionSet.Parse(args);
48+
}
49+
catch (Mono.Options.OptionException e)
50+
{
51+
Console.WriteLine(e.Message);
52+
Environment.Exit(0);
53+
}
54+
55+
if (showHelp)
56+
{
57+
// Print usage and exit.
58+
Console.WriteLine("{0} [options]+", AppDomain.CurrentDomain.FriendlyName);
59+
Console.WriteLine("Generates target language bindings for interop with unmanaged code.");
60+
Console.WriteLine();
61+
optionSet.WriteOptionDescriptions(Console.Out);
62+
Environment.Exit(0);
63+
}
64+
65+
if (_assemblies == null)
66+
{
67+
Console.WriteLine("Invalid arguments.");
68+
Environment.Exit(0);
69+
}
70+
}
71+
72+
static void GetFilesFromPath(String path)
73+
{
74+
path = path.Replace(System.IO.Path.DirectorySeparatorChar, System.IO.Path.AltDirectorySeparatorChar);
75+
76+
String searchPattern = String.Empty;
77+
int lastSeparatorPosition = path.LastIndexOf(System.IO.Path.AltDirectorySeparatorChar);
78+
79+
if (lastSeparatorPosition >= 0)
80+
{
81+
if (path.IndexOf('*', lastSeparatorPosition) >= lastSeparatorPosition || path.IndexOf('?', lastSeparatorPosition) >= lastSeparatorPosition)
82+
{
83+
searchPattern = path.Substring(lastSeparatorPosition + 1);
84+
path = path.Substring(0, lastSeparatorPosition);
85+
}
86+
}
87+
88+
try
89+
{
90+
if (searchPattern != String.Empty)
91+
{
92+
String[] files = System.IO.Directory.GetFiles(path, searchPattern);
93+
94+
foreach (String s in files)
95+
_options.HeaderFiles.Add(s);
96+
}
97+
else
98+
{
99+
String[] files = System.IO.Directory.GetFiles(path);
100+
101+
foreach (String s in files)
102+
_options.HeaderFiles.Add(s);
103+
}
104+
}
105+
catch (Exception ex)
106+
{
107+
Console.WriteLine("Source files path error: " + ex.Message);
108+
Environment.Exit(0);
109+
}
110+
}
111+
112+
static void GetGeneratorKind(String generator)
113+
{
114+
switch (generator.ToLower())
115+
{
116+
case "csharp":
117+
_options.Kind = CppSharp.Generators.GeneratorKind.CSharp;
118+
break;
119+
case "cli":
120+
_options.Kind = CppSharp.Generators.GeneratorKind.CLI;
121+
break;
122+
}
123+
124+
throw new NotSupportedException("Unknown generator kind: " + generator);
125+
}
126+
127+
static void GetDestinationPlatform(String platform)
128+
{
129+
switch (platform.ToLower())
130+
{
131+
case "win":
132+
_options.Platform = TargetPlatform.Windows;
133+
break;
134+
case "osx":
135+
_options.Platform = TargetPlatform.MacOS;
136+
break;
137+
case "linux":
138+
_options.Platform = TargetPlatform.Linux;
139+
break;
140+
}
141+
142+
throw new NotSupportedException("Unknown target platform: " + platform);
143+
}
144+
145+
static void GetDestinationArchitecture(String architecture)
146+
{
147+
switch (architecture.ToLower())
148+
{
149+
case "x86":
150+
_options.Architecture = TargetArchitecture.x86;
151+
break;
152+
case "x64":
153+
_options.Architecture = TargetArchitecture.x64;
154+
break;
155+
}
156+
157+
throw new NotSupportedException("Unknown target architecture: " + architecture);
158+
}
159+
160+
static void Main(string[] args)
161+
{
162+
ParseCommandLineArgs(args);
163+
164+
Generator gen = new Generator(_options);
165+
166+
try
167+
{
168+
gen.Run();
169+
}
170+
catch (Exception ex)
171+
{
172+
Console.WriteLine("Error: " + ex.ToString());
173+
}
174+
}
175+
}
176+
}

0 commit comments

Comments
 (0)