Cmake | UWP :: Using Cmake to build Universal Windows App - visual-studio

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

Related

Unable to compile llvm pass on Windows using 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 :)

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.

Glslang VS Project Setup

I am following this quick tutorial and I am not sure how to do the project setup part for my pre-existing VS 2017 project that uses the LunarG Vulkan SDK which includes the glslang and SPIRV folders. I tried using the table found here to convert the cmake commands in the tutorial; I was able to include the glslang directory but I cannot figure out what I should be adding to properties->linker->input->additional dependencies for linking with glslang and SPIRV. Do I have to somehow compile these first then link to them? In which case how do I do that?
Do I have to somehow compile these first then link to them? In which case how do I do that?
Yes. If you want to use the glslang tools at runtime instead of at build time, you need to first build them. The VulkanSDK contains the glslang source code, but not binaries. Since the interface to glslang is C++, not C like Vulkan, producing a binary that would work for everyone is basically a non-starter.
The tutorial you link to includes this:
add_subdirectory(${LIB_DIRECTORY}/glslang)
Which basically means "include this other CMake based project in my own", which would in turn mean that it would have the same build settings as your own CMake based project. In order to bypass CMake you would need to construct a Visual Studio project for glslang from scratch.
My advice to you is to stop trying to fit the tutorial into your pre-existing Visual Studio project and just use CMake to generate your VS project so that you can follow the tutorial directly. It's extremely unlikely that whatever settings you have in your pre-existing VS project can't be replicated in a CMake based VS project very quickly.
Alternatively, you can learn just enough CMake to create a project for glslang and build it, at which point you'll be able to use the binaries it creates as your linker inputs, BUT you'll need to make sure that the project settings in glslang generated project are sufficiently close to your own, in particular the Runtime Library and the 32/64 bit-edness and make sure that you don't mix and match release and debug builds across the dependency.
Here is a simple recipe for building glslang, assuming you have CMake installed and the VulkanSDK correctly installed:
mkdir %VULKAN_SDK%\..\glslang
cd %VULKAN_SDK%\glslang
mkdir build
cd build
cmake .. -G "Visual Studio 15 Win64" -DCMAKE_INSTALL_PREFIX=%VULKAN_SDK%\..\glslang
cmake --build . --config Release
cmake --build . --config Debug
cmake --build . --config Release --target INSTALL
cmake --build . --config Debug --target INSTALL

What is the -D define to tell CMake where to find nmake?

I have Visual C++ Build Tools 2015 | Standalone compiler, libraries and scripts installed on a low-end netbook. It's necessary, because the machine has a small eMMC soldered to the board with no real space available.
nmake is installed at %PROGRAM FILES%\Microsoft Visual Studio 14.0\VC\bin. However, CMake cannot find it when attempting to generate the Makefile. I'd like to use a -D to tell CMake what the makefile program is, but I am having trouble locating the list of -D defines for CMake.
For completeness, I'm trying to avoid other Microsoft tools. I have LLVM build tools at C:\LLVM\bin, so I'm setting CMAKE_C_COMPILER and CMAKE_CXX_COMPILER. But I needed nmake, because I can't find a stand-alone Make program for Windows already built.
What is the -D define to specify nmake for CMake?
The variable you are looking for is CMAKE_MAKE_PROGRAM.
If you try to set this variable plus CMAKE_C_COMPILER, CMAKE_LINKER_EXE, etc., this will still fail, because cl.exe and link.exe need some environment variables to be set. Those can be set by using a "Visual Studio * Command Prompt" (this uses vcvars.bat from the Visual Studio install directory).
To use Clang you can install a Clang toolset from http://llvm.org/builds/. Then you can specify CMAKE_GENERATOR_TOOLSET in a toolchain file.
Let me know how this works out for you.
CMAKE_MAKE_PROGRAM. See the documentation:

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.

Resources