Browse Source

Add -vs <number> command line argument, add vs probing, hello world compilation with system headers.

pull/1/head
Tarmo Pikaro 13 years ago committed by triton
parent
commit
bc317f8984
  1. 3
      examples/Hello/Hello.h
  2. 2
      examples/Hello/premake4.lua
  3. 33
      src/Generator/CodeGenerator.cs
  4. 121
      src/Parser/Parser.cpp
  5. 3
      src/Parser/Parser.h

3
examples/Hello/Hello.h

@ -1,3 +1,6 @@ @@ -1,3 +1,6 @@
// Just try out that system include headers can be included - no need for them in this code.
#include <iostream>
#include <fstream>
class Hello
{

2
examples/Hello/premake4.lua

@ -36,7 +36,7 @@ project "Hello" @@ -36,7 +36,7 @@ project "Hello"
buildrule {
description = "Compiling $(InputFile)...",
commands = {
'..\\..\\bin\\generator.exe -ns=CppCsBind -outdir=CppCsBind -I. hello.h',
'..\\..\\bin\\generator.exe -vs=10 -ns=CppCsBind -outdir=CppCsBind -I. hello.h',
},
outputs = { "CppCsBind\\hello_wrapper.cpp" }
}

33
src/Generator/CodeGenerator.cs

@ -44,8 +44,9 @@ namespace Cxxi @@ -44,8 +44,9 @@ namespace Cxxi
{
Library = library,
Verbose = false,
IncludeDirs = options.IncludeDirs,
FileName = file
IncludeDirs = options.IncludeDirs
FileName = file,
toolSetToUse = options.toolset2use
};
if (!ClangParser.Parse(parserOptions))
@ -125,6 +126,7 @@ namespace Cxxi @@ -125,6 +126,7 @@ namespace Cxxi
public List<string> Headers;
public string Template;
public string Assembly;
public int toolset2use;
}
public class Program
@ -137,7 +139,31 @@ namespace Cxxi @@ -137,7 +139,31 @@ namespace Cxxi
Console.WriteLine("Generates .NET bindings from C/C++ header files.");
Console.WriteLine();
Console.WriteLine("Options:");
options.WriteOptionDescriptions(Console.Out);
Console.WriteLine(@"
--vs, --visualstudio=VALUE - Visual studio version to use for
system include folders.
Valid values:
0 - autoprobe. (Default)
11 - 2012, 10 - 2010, 9 - 2008, 8 - 2005
Number can be find out from VSx0COMNTOOLS environment variable.
-D, --defines=VALUE - Specify define for preprocessor.
(E.g. WIN32, DEBUG)
-I, --include=VALUE - Add include path
--ns, --namespace=VALUE - Namespace where C# wrappers will reside
-o, --outdir=VALUE - Output folder
--debug
--lib, --library=VALUE
-t, --template=VALUE
-a, --assembly=VALUE
-v, --verbose
-h, -?, --help
");
}
static bool ParseCommandLineOptions(String[] args, Options options)
@ -155,6 +181,7 @@ namespace Cxxi @@ -155,6 +181,7 @@ namespace Cxxi
{ "t|template=", v => options.Template = v },
{ "a|assembly=", v => options.Assembly = v },
// Misc. options
{ "vs|visualstudio=", v => { Int32.TryParse(v, out options.toolset2use); } },
{ "v|verbose", v => { options.Verbose = true; } },
{ "h|?|help", v => options.ShowHelpText = v != null },
};

121
src/Parser/Parser.cpp

@ -60,6 +60,111 @@ static std::string GetClangBuiltinIncludeDir() @@ -60,6 +60,111 @@ static std::string GetClangBuiltinIncludeDir()
}
//-----------------------------------//
#ifdef _WIN32
#include <string>
#include <vector>
#include <stdio.h>
static std::string exec(const char* cmd)
{
FILE* pipe = _popen(cmd, "r");
if (!pipe) return "ERROR";
char buffer[128];
std::string result = "";
while(!feof(pipe)) {
if(fgets(buffer, 128, pipe) != NULL)
result += buffer;
}
_pclose(pipe);
return result;
}
#pragma comment(lib, "shlwapi.lib") //_popen, _pclose
#include <shlwapi.h>
//
// Returns true if got settings, false if not.
//
bool GetVisualStudioEnv(
const char* envVar, // [in] "INCLUDE" / "LIBPATH"
std::vector<std::string>& vecPathes,
int iProbeFrom = 0, int iProbeTo = 0 // 2005 - 8, 2008 - 9, 2010 - 10
)
{
std::string s;
if( iProbeFrom == 0 && iProbeTo == 0 )
{
// Environment variable specifies which VS to use.
if( getenv("VSENV") != NULL )
{
int iVer = atoi( getenv("VSENV") );
if( iVer == 0 )
return false;
return GetVisualStudioEnv(envVar, vecPathes, iVer, iVer );
}
iProbeFrom = 20;
iProbeTo = 9;
} //if
for( int iProbeVer = iProbeFrom; iProbeVer >= iProbeTo; iProbeVer-- )
{
char envvar[64];
sprintf(envvar, "VS%d0COMNTOOLS", iProbeVer);
// Not defined
if( getenv(envvar) == NULL )
continue;
std::string cmd = getenv(envvar);
cmd += "\\..\\..\\vc\\vcvarsall.bat";
if( !PathFileExistsA( cmd.c_str() ) )
continue;
//printf("Located: %s", envvar);
cmd = "cmd /C \"" + cmd + "\" ^>nul ^& set";
s = exec(cmd.c_str());
break;
}
if( s.length() == 0 )
return false;
char* envline;
vecPathes.clear();
for (envline = strtok( &s[0], "\n" ); envline; envline = strtok( NULL, "\n" ))
{
char* varend = strchr( envline, '=');
if( !varend )
continue;
*varend = 0;
if( strcmp(envline,envVar) == 0 )
{
char* val;
for (val = strtok( varend + 1, ";" ); val; val = strtok( NULL, ";" ))
{
vecPathes.push_back(val);
}
}
*varend = '=';
} //for
return true;
} //GetVisualStudioEnv
#endif //_WIN32
void Parser::Setup(ParserOptions^ Opts)
{
@ -121,6 +226,20 @@ void Parser::Setup(ParserOptions^ Opts) @@ -121,6 +226,20 @@ void Parser::Setup(ParserOptions^ Opts)
}
#endif
#ifdef _WIN32
std::vector<std::string> SystemDirs;
if( GetVisualStudioEnv("INCLUDE", SystemDirs, Opts->toolSetToUse, Opts->toolSetToUse ) )
{
clang::HeaderSearchOptions& HSOpts = C->getHeaderSearchOpts();
for(size_t i = 0; i < SystemDirs.size(); ++i)
{
HSOpts.AddPath(SystemDirs[i], frontend::System, false, false, true);
}
} //if
#endif
C->createPreprocessor();
C->createASTContext();
@ -796,7 +915,7 @@ Cxxi::Type^ Parser::WalkType(clang::QualType QualType, clang::TypeLoc* TL, @@ -796,7 +915,7 @@ Cxxi::Type^ Parser::WalkType(clang::QualType QualType, clang::TypeLoc* TL,
}
case Type::InjectedClassName:
{
auto IN = Type->getAs<InjectedClassNameType>();
auto MYIN = Type->getAs<InjectedClassNameType>();
return nullptr;
}
case Type::DependentName:

3
src/Parser/Parser.h

@ -47,6 +47,9 @@ public ref struct ParserOptions @@ -47,6 +47,9 @@ public ref struct ParserOptions
Cxxi::Library^ Library;
// Toolset version - 2005 - 8, 2008 - 9, 2010 - 10, 0 - autoprobe for any.
int toolSetToUse;
bool Verbose;
};

Loading…
Cancel
Save