Utilization of Clang CFE Toolchains from C++ - clang

I'm trying to use the Clang Compiler FrontEnd to compile C++ for OpenCL to SPIR-V.
I assume that this is done by creating a clang::driver::toolchains::SPIRVToolChain which is defined in header SPIRV.h. However, this header is located under LLVM_ROOT_DIR/clang/lib/ToolChains/, which only exists in the LLVM project folder. It is not installed with the rest of Clangs headers.
Am I supposed to only interface abstract class clang::driver::ToolChain in order to perform the compilation, or do I have to modify the LLVM/Clang-installation?

Related

Compiling objective-c++ on Windows with clang-cl

I have a Qt project that was developed on MacOS. I've been given the job to make it compile on Windows.
My current problem is that compiler (Microsofts LLVM clang-cl) seems to be ignoring objective-c++ files, and the QtCreator is freaking out on #import statement (#import of type library is an unspported Microsoft feature). It also freaks out about a lot of stuff after that, but I'm guessing it's due to the import not being handled properly.
I was led to believe that win32-clang-mvsc was the only QtCreator compiler that supports compilation of objective-c++ files on windows, however, it doesn't seem to support the #import statement.
I've looked around and it seems that I would need GNUstep to be able to compile that on windows, and I'm not entirely sure it would work.
Is it possible to use the whichever compiler GNUstep provides with QtCreator? Or is there some way to use the compiler I'm already using to compile the files with #import statement?
The error I'm getting on the build is that ***.obj file doesn't exists, where *** is the name of the file with the #import statement. I'm guessing the compiler skips it and doesn't generate the .obj file, so something can't find it. I'd guess it was the linker, but I'd expect linker errors then, not a generic file not found error.
You can use the GNUstep Windows MSVC Toolchain to build Objective-C code using Clang on Windows. We’re also using this in a Qt project. You’ll just need to tell your build system to use the necessary flags for Objective-C files (e.g. using QMAKE_EXTRA_COMPILERS if you’re using QMake). This Qt example project for using Objective-C in a Qt project for Android should get you started.
The error you’re seeing about #import is likely because your project is using some clang-cl flags like /TC//TP that force the compiler to treat the file as C/C++ irrespective of its extension. Removing these flags should make this error disappear.
Please open issues in the GitHub project linked above if you need further help with the setup.

Swift classes marked with #objc not being added to "-Swift.h" header in mixed Objective-C/Swift framework

I am attempting to convert an old statically linked library to a framework. With mixed swift and objective c in the static library, all headers are generated correctly. However, switching to a framework target and adding swift files, marked with the #objc header, the class is not added to the -Swift.h header. I can import the header, but swift classes are not found. This is in Xcode 10.2 and attempted using both Swift 4.2 and 5.
Are there any particular settings in XCode that will affect the generation of the *-Swift.h header in a mixed Objective C/Swift framework target?
I had a similar issue. In my case it was a known issue in Xcode 10.2:
https://developer.apple.com/documentation/xcode_release_notes/xcode_10_2_release_notes
If you’re building a framework containing Swift code and using lipo to create a binary that supports both device and simulator platforms, you must also combine the generated Framework-Swift.h headers for each platform to create a header that supports both device and simulator platforms. (48635615)
...
In my case all I had to do was to update the Carthage to the newest version 0.33.0
The problem appears to be a combination of Apple's new build system, the expectations they set when compiling and the number of inter-dependencies in the project setup.
The new build system runs the Swift compilations in parallel. When having multiple library/framework dependencies that are mixed Objective C and Swift, the compiler appears to not generate the -Swift.h files on time. In static libraries, the -Swift.h files appear to be generated at the end of the Swift Compilation process, meaning they are not generated quickly enough to be used by the Objective C files when the Objective C compilation occurs. When generating a framework, it appears that the Compiler generates the header at the beginning of the compilation process and the Swift files are not fully compiled and the -Swift.h file does not generate appropriately with the Objective C class interfaces and protocols.
What this means ends up meaning is that we can not rely on the "target dependencies" to build the dependent projects correctly.
So how can we build our .framework of mixed Objective C and -Swift.h without a ton of manual scripting.
Here are the tricks I discovered that will work.
Use the old build system. When using the new build system there is an error when it attempts to merge the module map for the static library file saying that it can not find the *-Swift.h file whether or not it exists.
Create your framework by making it a wrapper around the static library by:
Giving them both the same product name and module name.
Add a single .Swift file to the framework build so that it has something to compile and will link the swift libraries.
link to the static library in the framework.
Add all necessary headers to the public headers of the framework.
Add all public headers to the umbrella header.
Use a Run script phase to copy the *-Swift.h file from the static library build to the framework product post compile.
For any public headers that include the *-Swift.h, you may need to post process the header and replace the *-Swift.h import with the appropriate framework import ie . This would not be recommended due to possible cyclical imports in the umbrella header.
When running a clean, build the framework target first manually, before building the application target.
Below is an example script for copying the *-Swift.h file post build.
header_file="${TARGET_TEMP_DIR}/../${PRODUCT_MODULE_NAME}.build/DerivedSources/${PRODUCT_MODULE_NAME}-Swift.h"
header_dir="${BUILT_PRODUCTS_DIR}/${PUBLIC_HEADERS_FOLDER_PATH}"
mkdir -p "$DIR"
echo "copying $header_file $header_dir"
cp -f "$FILE" "$DIR"
UPDATED
Marking all modules Swift compilation mode to "Whole Module" appears to have a positive affect on this issue, but I have not fully tested it.

Is it possible to compile files which are changed due to compiler preprocessor macros in Xcode?

I have a project which I need to compile with different preprocessor macros. To reduce the compile time, I would like to compile only the files which re changed due to the macros and then somehow put them in the app binary. Is it possible to do using Xcode or command line tools for iOS?
If the macros are defined in a header file, it might only recompile files that #import or #include that header. Make sure to import the header only in source files where the macros are used to limit the recompiles.
If the macros are defined in the project file, imported or defined in a prefix file or added from the command-line, then it will likely recompile the whole project as it can't tell who depends on them. (Unless some newer version of Xcode can analyze and adapt for that.)

CMake override libraries from find_package

I'm running find_package(OpenCV, REQUIRED) to locate a library. It's installed on the build system and the target system, however the target is slightly different in that one part of the library is not available.
So when building, I get back linker flags like -lfoo -lbar. However, bar isn't available on the target machine and I'm not using it anywhere in the application. Of cource, since it was linked in, the runtime linker complains that it cannot be found.
Is there anyway to override the libraries linked in with find_package? I'll probably just do a string replace type of solution, though I'd be thankful for any help there too since I'm a CMake novice.
Thanks
I suppose you can remove undesired libraries from OpenCV_LIBRARIES variable:
list(REMOVE_ITEM OpenCV_LIBRARIES bar)
Alternatively, you can read FindOpenCV.cmake source to check if it supports COMPONENTS keyword.

Is there a way to load llvm bitcode into Mono (F#)

LLVM has libraries that allow easy reading and writing of bitcode. This enables e.g. writing code generators that output llvm bitcode (which one can compile to native code using llvm), or loading bitcode files and performing optimizations or analyses on them.
One can even do so from OCaml.
Is there a library to load LLVM bitcode into Mono (F#), and browse it as e.g. an object tree?
(Note: I do not want to execute LLVM bitcode in Mono, just load the code and browse the structures.)
llvm-fs is a set of F# bindings for the LLVM project.
Bonus reading:
Building a simple compiler with F# and LLVM on windows.

Resources