Unable to compile llvm pass on Windows using Visual studio - visual-studio

Im learning how to use the llvm toolchain on Windows.
I compiled and installed llvm using the following cmake command
cmake -S llvm -B build -DLLVM_ENABLE_PROJECTS="clang;lld" -DLLVM_USE_LINKER=lld -DLLVM_TARGETS_TO_BUILD=X86 -DCMAKE_INSTALL_PREFIX="C:\llvm" -Thost=x64
Running the INSTALL project in the solution created in the build folder installed llvm in c:\llvm.
To test my llvm installation i created a new Visual Studio project and placed a Directory.build.props file in the project root with the following contents:
<Project>
<PropertyGroup>
<LLVMInstallDir>C:\llvm</LLVMInstallDir>
<LLVMToolsVersion>14.0.0</LLVMToolsVersion>
</PropertyGroup>
</Project>
I set the project toolchain to LLVM (clang-cl) in project settings and successfully compiled a hello world program.
Now i wanna use llvm for it's intended use and play around with some passes. So i downloaded https://github.com/tsarpaul/llvm-string-obfuscator.
Running cmake -Bbuild -DLLVM_DIR=C:\llvm\lib\cmake\llvm\ generated a Visual Studio solution in .\build.
So far so good i tought. This is similar to the steps required to compile llvm itself.
But running the BUILD_ALL project in the generated solution gave me 105 linker errors.
I have pasted the entire error output here: https://pastebin.com/TAizbAEi.
I have a feeling the required libraries in C:\llvm\lib are not visible to Visual Studio. But i have no idea how to proceed. Could anyone point me in the right direction on how to use llvm with visual studio correctly?

Solved by adding to the cmake file:
llvm_map_components_to_libnames(llvm_libs core)
target_link_libraries(LLVMStringObfuscator ${llvm_libs})
Now the dll build.
I still have problems using opt.exe
PS C:\Users\Administrator\source\repos> C:\llvm\bin\opt.exe -load-pass-plugin=.\11philip22\llvm-string-obfuscator\build\StringObfuscator\Release\LLVMStringObfuscator.dll -passes="string-obfuscator-pass"
C:\llvm\bin\opt.exe: .\11philip22\llvm-string-obfuscator\build\StringObfuscator\Release\LLVMStringObfuscator.dll:1:1: error: expected top-level entity
MZÉ    ╕ # ║ ┤ ═!╕L═!This program cannot be run in DOS mode.
But that's a new problem for another thread :)

Related

Visual Studio 2017 - How to create a project from the source using CMake?

I have a pretty large software library using CMake to be compiled. We use Linux mostly, but now a new colleague wants to use Visual Studio.
Is there any way to create a new VS 2017 project from the existing source codes with CMake structure?
I know, it's possible to do it with CLion, but I have no idea about VS, as I have a very little experience with it.
Other questions seem to focus on creating an empty project, which will use CMake, but not on creating a project from already existing source files.
Creating a cmake project with visual studio
Creating a project with visual studio 2017
I'm not sure why you asked for details but...
Assuming you are using cmake 3.13 then you can do the following in a command shell:
cmake -G "Visual Studio 15 2017" -S path_to_source -B path_to_build
This will then create a solution file. Actually it creates a solution file for every project() command that is issued in CMakeLists.txt.
You can then open the solution file in Visual Studio, and build the project as usual.
You don't even need to do this in the Visual Studio GUI. After creating the initial project you can also issue the command:
cmake --build path_to_build
Which will kick off the build at the command line.
Now if your CMakeLists.txt in path_to_source is using Linux specific libraries or gcc specific compiler settings then the CMakeLists.txt will have to get updated to the Windows equivalent.
The alternative is to start Visual Studio and then use File->Open->CMake and open the CMakeLists.txt file in path_to_source. It'll then start to generate the project. But I prefer using the command line method.

Cmake | UWP :: Using Cmake to build Universal Windows App

How to use CMake for building a Universal Windows Application. I have already searched a lot and I came across the following:
CMake -G "Visual Studio 14 2015" -DCMAKE_SYSTEM_NAME=WindowsStore -DCMAKE_SYSTEM_VERSION=10
This is how I am supposed to invoke CMake.
(How can i use CMake to generate Windows 10 universal project)
How should the corresponding CMakeLists.txt files look like. I know about the command for executables and library like add_executable etc. But I do not know about the command to be used for building a UWP app. Can anyone please provide a sample CMakeLists.txt file along with some sample code?
Also how to use CMake for a WinRT component?
CMake is a cross-platform build tool, Microsoft has an fork branch from the official cmake repository, to bring in support for UWP and WinRT. So the offical cmake has support for UWP since some 3.4.0 version.
The CMakeLists.txt file differs from no general cmake file, you need to specify the cmake version, source file etc...
When targeting UWP project, you need to specify the build command as you pointed out,
CMake -G "Visual Studio 14 2015" -DCMAKE_SYSTEM_NAME=WindowsStore -DCMAKE_SYSTEM_VERSION=10
You can also use the CMake GUI tool, just declar the MAKE_SYSTEM_NAME and CMAKE_SYSTEM_VERSION macros.
Worth mentioning here is there's a open source fips project, which is a wrapper module around cmake. You can see how easy it is to generate UWP project, following the tutorial here,
fips build uwp-vstudio-debug

Cmake not finding Visual Studios 2012 in Generators list

I'm trying to run through the Firebreath tutorial, but I cannot get Cmake to work. Cmake does not register any version of Visual Studios under the Generators list. This is the list when running cmake --help:
Generators
The following generators are available on this platform:
Unix Makefiles = Generates standard UNIX makefiles.
Ninja = Generates build.ninja files (experimental).
CodeBlocks - Ninja = Generates CodeBlocks project files.
CodeBlocks - Unix Makefiles = Generates CodeBlocks project files.
Eclipse CDT4 - Ninja = Generates Eclipse CDT 4.0 project files.
Eclipse CDT4 - Unix Makefiles
= Generates Eclipse CDT 4.0 project files.
KDevelop3 = Generates KDevelop 3 project files.
KDevelop3 - Unix Makefiles = Generates KDevelop 3 project files.
Sublime Text 2 - Ninja = Generates Sublime Text 2 project files.
Sublime Text 2 - Unix Makefiles
= Generates Sublime Text 2 project files.
I have re-installed CMake, installing Visual Studios 2010 and 2012 (Express and Professional editions), and even uninstalled and re-installed .NET 4.0 to 4.5 (from here). It works fine on my personal computer, as I got it running on the first go. However, my work laptop is just not registering Visual Studios.
I'm running it both on Windows 7 Enterprise SP1. Any help is appreciated.
Here is the exact error I am getting:
C:\code\tutorial2\TutorialTestPlugin>firebreath\prep2012.cmd . build
A subdirectory or file build already exists.
Using projects in: "C:\code\tutorial2\TutorialTestPlugin"
Generating build files in: "C:\code\tutorial2\TutorialTestPlugin\build"
NOTE: The build files in "C:\code\tutorial2\TutorialTestPlugin\build" should *NE
VER* be modified directly.
When needed, make project changes in cmake files and re-run this script.
Project-specific cmake files are found in [plugin dir]\CMakeLists.txt and
[plugin dir]\Win\projectDef.cmake.
Note that parameters for cmake should be enclosed in double quotes, e.g. "-DVERB
OSE=1"
CMAKE parameters:
C:\code\tutorial2\TutorialTestPlugin\build>cmake -G "Visual Studio 11" -DFB_PROJ
ECTS_DIR="C:\code\tutorial2\TutorialTestPlugin" "C:\code\tutorial2\TutorialTest
Plugin\firebreath"
CMake Error: Could not create named generator Visual Studio 11
cygwin warning:
MS-DOS style path detected: C:\code\tutorial2\TutorialTestPlugin\firebreath
Preferred POSIX equivalent is: /cygdrive/c/code/tutorial2/TutorialTestPlugin/f
irebreath
CYGWIN environment variable option "nodosfilewarning" turns off this warning.
Consult the user's guide for more details about POSIX paths:
http://cygwin.com/cygwin-ug-net/using.html#using-pathnames
The problem is you are using the cmake that is included in cygwin instead of the windows version of cmake. It appears that the current cygwin version is compiled without the generators for Visual Studio while the windows version has these.
In your case a simple way to get windows to use the windows version of cmake is to uninstall the cygwin cmake since it does not appear that you need that anyways.
An alternate method would have been adjusting your system path so that c:\cygwin\bin is after C:\Program Files (x86)\CMake 2.8\bin.

Where is llvm-config in Windows?

I am open to either a Visual Studio answer or a MinGW answer. I just finished building LLVM 3.2 using CMake and Visual Studio 2010. Everything went smoothly, but I have no llvm-config. Do I need it? Every example I see on the intertubes makes use of that tool. If I don't need it, how do I configure my project to make use of LLVM?
To be clear, I am not trying to use LLVM tools/compilers (like clang and whatnot). I am trying to write C++ code that uses the LLVM libraries to produce LLVM IR and even compile that stuff. I setup my include and lib folders. I ran llvm-config in Linux and saw a long list of macros and libraries.
I have a wonderful folder full of goodies. It just has no llvm-config in there: C:\Program Files (x86)\LLVM\
llvm-config does not exist in windows prebuilt binaries. You need to compile from the source code to get it.
Grab CMAKE > 3.5 , install it and make sure you add it to PATH.
Download Visual Studio 2019
Donwload the source code (9.0.1 is the latest as I'm writing this)
Extract the source code
Cd into the root of the llvm source-code
In cmd, type cmake . this will generate Visual Studio 2019 sln.
open sln file(LLVM.sln), change the build type to Rlease, build the whole project
navigate to your Rlease\bin, and there you have your llvm-config.exe
If you have built the LLVM in debug version all the executables (including llvm-config) have been placed in your build directory (containing Visual Studio project and solution files) in bin/Debug/ subdirectory. In case of release build replace Debug with Release.
If you are interested in using LLVM on Windows more than building it, check out Windows snapshot builds.

Compiling FFTW source in Visual studio

Recently I am trying to compile the source code of FFTW in Visual studio 2010. I followed the instruction from the FFTW website. I downloaded the source code fftw-3.3.2.zip and corresponding vs 2010 package fftw-3.3-libs-visual-studio-2010.zip.
I got four projects from the solution, bench, benchf, libfftw-3.3, libfftwf-3.3
But on compiling I got following errors on each of the project:
C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\Platforms\x64\Microsoft.Cpp.x64.Targets(514,5): error MSB8008: Specified platform toolset (Windows7.1SDK) is not installed or invalid. Please make sure that a supported PlatformToolset value is selected.
which points to the following content
I have tried the debug/release build for both x64 and win32, none of them worked.
Could anyone please help me on this?
It seems that it can be solved without installing the Windows 7.1 SDK
Right click on the 'libfftw-3.3' project and selected properties
Go to Configuration Properties -> General
Switch 'Platfrom Toolset' from 'Windows7.1SDK' to 'v100'
Recompile
Works for the projects 'libfftwf-3.3' and 'libfftw-3.3'
The project 'bench' and 'benchf' are failing to build:
fatal error C1083: Cannot open source file: '....\libbench2\aligned-main.c': No such file or directory
Can be solved by removing the aligned-main.c from both projects.
You can also use CMake - I created this CMake file for fftw-3.3.2 which I tested with Visual Studio 2010 x64:
https://bitbucket.org/Vertexwahn/cmakedemos/src/670f189321d89dbd61ddc8c446c91578305f9da2/fftw-3.3.2/CMakeLists.txt?at=default
You also need this config.h file:
https://bitbucket.org/Vertexwahn/cmakedemos/src/670f189321d89dbd61ddc8c446c91578305f9da2/fftw-3.3.2/config.h?at=default
It looks like you need to install the Windows 7.1 SDK, if you have already installed it, try reinstalling it incase it is corrupted.
You can download the 7.1 SDK from http://www.microsoft.com/en-us/download/details.aspx?id=8279

Resources