mono / CppSharp

Tools and libraries to glue C/C++ APIs to high-level languages
MIT License
3.12k stars 513 forks source link

Compile fails with latest LLVM (they switched to std::atomic) #212

Closed oysteinkrog closed 10 years ago

oysteinkrog commented 10 years ago

It seems LLVM has switched from using their own atomics to using std::atomic.

When I try to compile the parser now I get: 3>C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\atomic(9): fatal error C1189: #error : is not supported when compiling with /clr or /clr:pure.

This seems like a pretty bad thing. I'm not sure if it's possible to work around it?

I guess switching to the new parser would be an option, but I'm still not sure how that is supposed to work... from what I understand there is no way to bootstrap?

tritao commented 10 years ago

I had already noticed the same problem, and have been discussing some potential solutions with @ddobrev.

Switching to the new parser seems the most likely solution... we could try lobbying the LLVM devs to revert this but I'm don't think it will make any difference, we'd be negatively impacting the entire LLVM project for a configuration that frankly no one cares much about (CLR).

Viv-Rajkumar commented 10 years ago

Is there any workarounds to this currently to get the Example SDL project built? Maybe a recommended older version of Clang?

oysteinkrog commented 10 years ago

Yes, you can find the latest supported LLVM/Clang SVN revision in the Getting Started document: https://github.com/mono/CppSharp/blob/master/docs/GettingStarted.md

Right now it is r202563 (I think LLVM and Clang share the same SVN repo, afaik r202563 is the LLVM revision and you just pick the closest Clang revision). We could perhaps make this easier by using git submodules to the github repo's (and thus referencing specific versions).

Viv-Rajkumar commented 10 years ago

We could perhaps make this easier by using git submodules to the github repo's (and thus referencing specific versions).

+1 That would be great.

Have never used svn much at all. I got LLVM and clang following the getting started with git-mirror

Will try the svn route now to pick the "r202563" rev.

oysteinkrog commented 10 years ago

You can use the git mirrors also, I do, you just have to find the commits for the svn revision and checkout those instead;P

Viv-Rajkumar commented 10 years ago

@oysteinkrog cheers for that hint. Managed to just get it via svn.

I've got r202562 for clang with llvm at r202563 based on http://llvm.org/viewvc/llvm-project/?pathrev=202563

Quick question: in the getting started when you mention "compile solution in RelWithDebInfo mode" do you mean the AllBuild target with tests and everything or is there any particular project target to build which might be quicker than building everything?

oysteinkrog commented 10 years ago

I'm not sure myself...I did not write those:)

Here are the commands I've got in my notes, they work but I'm not sure if they are 100% correct. I build both RelWithDebInfo and Debug, but Debug is really only useful if you are e.g. debugging the Parser code.

"C:\Program Files (x86)\Microsoft Visual Studio 12.0\vc\vcvarsall.bat" x86

"C:\Program Files (x86)\CMake 2.8\bin\cmake.exe" -DCMAKE_BUILD_TYPE:STRING=RelWithDebInfo -DLLVM_DEFAULT_TARGET_TRIPLE:STRING=x86_64-pc-win64 -DLLVM_HOST_TRIPLE:STRING=x86_64-pc-win64 -DLLVM_TARGETS_TO_BUILD:STRING=X86 -DLLVM_ENABLE_CXX11:BOOL=true -DLLVM_ENABLE_LIBCXX:BOOL=true -DLLVM_BUILD_32_BITS:BOOL=false -DLLVM_INCLUDE_TESTS=OFF ../

msbuild ALL_BUILD.vcxproj /p:Configuration=Debug msbuild ALL_BUILD.vcxproj /p:Configuration=RelWithDebInfo

Viv-Rajkumar commented 10 years ago

Ah cheers. Just started building All_Build now. hopefully it builds fine :)

Viv-Rajkumar commented 10 years ago

@oysteinkrog LLVM built fine.

Got the CppSharp project to build fine as well.

The SDL example does not seem to generate anything as it's missing a SDL.h file according to the output

Parsing libraries...
Indexing library symbols...
Parsing code...
File 'SDL.h' was not found
Processing code...
Generating code...

Is this intended? Am I meant to create a SDL.h file myself to test the example and if so which folder do I create it in?

oysteinkrog commented 10 years ago

I don't know, I think @tritao is the one to ask. I would guess you need to provide it yourself yes, I just downloaded SDL and placed it in the folder. I can't remember what the correct place is ATM, but you can look at the code and see where it looks for it.

tritao commented 10 years ago

Glad you got it building already.

You can continue using the Git mirror, in each commit message there should be an SVN revision comment so just reset the repository to the known-working revision before the atomics change.

About the target, you should just need the Clang target which builds the needed Clang libraries for CppSharp. It should be much faster than building the entire project. There's also some nice speedups if you set some custom options in CMake. IIRC, in LLVM there's LLVM_TARGETS_TO_BUILD which you can set to just X86, and in Clang you can disable ARC, the static analyzer and the AST matchers libraries. That should give you a much faster build. If you want even more speed you can try building via the Ninja target instead of MSBuild.

About the SDL sample, I haven't touched it for a couple months, but you should just need to get the source for SDL and extract it to maybe the examples SDL folder or to your build folder.

Let me know if you have any further questions.

Viv-Rajkumar commented 10 years ago

@tritao Hey cheers for the detailed guide. That certainly helps and I'll remember it for the next build.

About the SDL sample, I haven't touched it for a couple months, but you should just need to get the source for SDL and extract it to maybe the examples SDL folder or to your build folder.

Yeh I didn't realize I needed to get some other project as well. I ended up trying to create a sample project myself following the instructions. Hit a few issues though.

First with the csharp option I got the output file but it still needed the C++ project as a dll I guess. the c++ project I have is just a static lib. So instead I tried using the C++/CLI option.

With that I got the output header and .cc file. Then I had to create a new C++/CLI project add these files in add the include dir path, path to static lib and compile it to a dll.(Used Win32 than x64 to build the wrapper dll)

However this dll was working fine via a general .Net Console/WPF app. However as soon as I tried adding a Xamarin.iOS app for Xamarin, The reference was added fine and the project built fine. However at runtime when the object was being created(the wrapper object) it threw an exception abt (Module exception) with not enough info for me to identify the cause. So I pretty much left it at that stage and not sure what could be my next option to try and get this Xamarin friendly for iOS and Droid.

Was pretty excited when the iOS project built, as in the sample project I had boost added too on the Native side and was thinking that was the end of the tunnel, but it managed to screw me over at the very end :P

oysteinkrog commented 10 years ago

Hmm, we use the Xamarin toolchain for our app development, perhaps I can help.

What exactly is your use-case here? From what I understand you want to generate C# bindings for a native (C++) library that is compiled for iOS? I think this should work as the mono/Xamarin toolchain does support pinvoke for C libraries.

However, if the native library is objective-c you will have to use the iOS/Xamarin custom introp stuff, which is not supported at all in CppSharp. http://docs.xamarin.com/guides/ios/advanced_topics/native_interop/

Viv-Rajkumar commented 10 years ago

What exactly is your use-case here? From what I understand you want to generate C# bindings for a native (C++) library that is compiled for iOS?

Our use case is to be able to use a c++ library with Xamarin on mobile platforms yes. The c++ library isnt specifically compiled for iOS in any way(That's prolly because I thought it wasnt required to be. Could you maybe elaborate on what that entails as in compiling the c++ library specifically for iOS. My understanding was just that certain features might not be available however as for the compiler itself, did not know about such stuff.)

However, if the native library is objective-c you will have to use the iOS/Xamarin custom introp stuff, which is not supported at all in CppSharp.

It's not a obj-c proj. I do use some obj-C projects but yes I create bindings for them using Objective Sharpie and then tweaking them out. Not with CppSharp. CppSharp was just going to be used for the C++ libs itself.

oysteinkrog commented 10 years ago

Unfortunately I don't have too much experience with native libraries on Xamarin, but my understanding is that you need to compile a static library and then specify some special build options in the C# project that links the library into the final application.

I would also guess that you need to compile the library with the correct compiler and link it against the correct runtime... The link I posted above probably does a much better job at explaining this than I do (I really don't know too much about this;P)

I've seen this technique used by e.g. the TestFlight SDK Xamarin bindings (opensource and available on github last I checked).

Please let us know if you get this working, it would be good to know if/if not CppSharp works on iOS :)

tritao commented 10 years ago

I'm not too sure about how to include native libraries in iOS Xamarin projects, but CppSharp genearted bindings code should work fine on iOS. Be sure to select the Itanium ABI in your options though, not the MS one.

There's only one bug that I am aware of for Itanium-based ABI libraries. It happens with instance method calls returning objects with constructors/destructors by value. I'm working on a fix for that so it should be fixed soon.

Viv-Rajkumar commented 10 years ago

@oysteinkrog No problem. Yes the C++ library was built statically and then the CLi project exposes it as a C# class to the .Net world. It works fine for a consoleApp. it's just mobile that had some weird exception. It prolly is some compiler specific info that's required. Just a matter of finding out what info :)

Please let us know if you get this working, it would be good to know if/if not CppSharp works on iOS :)

Will definitely do. If I can get this sample working on iOS & Droid. I'll post the entire process with the code to git. So can serve as an example for people with the same requirement.

@tritao

Be sure to select the Itanium ABI in your options though, not the MS one.

Where do I do this? Is this in the CLI project settings / native C++ project settings somewhere or something to specify to CPPSharp to get the binding generated with.

The following is all I have in my implementation for ILibrary currently. rest are just empty functions.

public void Setup(Driver driver) {
  var options = driver.Options;
  options.GeneratorKind = GeneratorKind.CLI;
  options.LibraryName = "NativeProject";
  options.Headers.Add("Calculator.h");
  options.Libraries.Add("NativeProject.lib");
  options.IncludeDirs.Add(@"...Path for native project includes...");
  options.LibraryDirs.Add(@"...Path to native lib...");
  options.OutputDir = @"...Path for output of cli files...";
}
Viv-Rajkumar commented 10 years ago

@tritao oh maybe you meant to specify options.Abi = CppAbi.Itanium; in the Setup() function? Just tried that and checked the diff output for the .h and cpp files to what I had before and they are identical. Am I applying the option in the wrong place?

tritao commented 10 years ago

I think the problem might be that you are trying to use the CLI backend. That only works on Windows since Xamarin has no C++/CLI compiler available. You can use the C# P/Invoke generator to get working code for Xamarin platforms.

Viv-Rajkumar commented 10 years ago

@tritao Ok I'll give that a go. Was just not sure about the DllImport and where to actually place the native dll for say iOS. I'll try it in the root of the app bundle and see if it works or try some alternatives

Am guessing I should still mention options.Abi = CppAbi.Itanium; in the Setup right with options.GeneratorKind = GeneratorKind.CSharp; ??

tritao commented 10 years ago

Yes, you need to set it in options.Abi.

tomspilman commented 10 years ago

FYI.

llvm r202563 is git mirror revision c9bf74fdc543c2da90f334e0bf8e34b128c8a615. clang r202562 is git mirror revision 30577e6c4f5ba66b8dc832ed9955b1a7475f788a.

Both on 2014-02-28.

tritao commented 10 years ago

Thanks Tom, I added the revisions to the getting started guide.

tritao commented 10 years ago

We now use an updated version of LLVM and no longer have this problem.

oysteinkrog commented 10 years ago

Ah, good to hear.