Add a simple example for using Clang as a cross compiler for MSVC

This commit is contained in:
Dragoon 2019-05-09 23:18:51 +02:00
parent 6154a8de60
commit 1581e221b4
4 changed files with 62 additions and 0 deletions

View File

@ -0,0 +1,14 @@
To cross compile for Windows in MSVC mode from Linux, you will require the following:
* A partition with Windows installed (NTFS).
* Visual Studio (Tested with 2017).
* The Windows SDK.
* lowntfs-3g file system driver.
Make sure the Windows partition is mounted with "-t lowntfs-3g -o defaults,ignore_case,windows_names".
This will allow Clang to find all headers and libraries referenced by scripts and headers, otherwise you will run into case sensitivity errors.
Clang uses the following environment variables to detect the Visual Studio install: VCINSTALLDIR, VCToolsInstallDir, INCLUDE, LIB, LIBPATH
I just copied these from the output of the "set" command in an MSVC command prompt on Windows and translated the paths to Linux paths.
Notice how the semicolon is still used as a path separator.
See "example_environment_linux.sh" for how my setup looks like.
It expects the Windows partition to be mounted on /mnt/windows, with VS2017 installed and Windows 10 SDK 10.0.17763.0.

View File

@ -0,0 +1,5 @@
export VCINSTALLDIR="/mnt/windows/program files (x86)/microsoft visual studio/2017/community/vc/"
export VCToolsInstallDir="/mnt/windows/program files (x86)/microsoft visual studio/2017/community/vc/tools/msvc/14.16.27023/"
export INCLUDE="/mnt/windows/program files (x86)/microsoft visual studio/2017/community/vc/tools/msvc/14.16.27023/atlmfc/include;/mnt/windows/program files (x86)/microsoft visual studio/2017/community/vc/tools/msvc/14.16.27023/include;/mnt/windows/program files (x86)/windows kits/10/include/10.0.17763.0/ucrt;/mnt/windows/program files (x86)/windows kits/10/include/10.0.17763.0/shared;/mnt/windows/program files (x86)/windows kits/10/include/10.0.17763.0/um;/mnt/windows/program files (x86)/windows kits/10/include/10.0.17763.0/winrt;/mnt/windows/program files (x86)/windows kits/10/include/10.0.17763.0/cppwinrt"
export LIB="/mnt/windows/program files (x86)/microsoft visual studio/2017/community/vc/tools/msvc/14.16.27023/atlmfc/lib/x64;/mnt/windows/program files (x86)/microsoft visual studio/2017/community/vc/tools/msvc/14.16.27023/lib/x64;/mnt/windows/program files (x86)/windows kits/10/lib/10.0.17763.0/ucrt/x64;/mnt/windows/program files (x86)/windows kits/10/lib/10.0.17763.0/um/x64"
export LIBPATH="/mnt/windows/program files (x86)/microsoft visual studio/2017/community/vc/tools/msvc/14.16.27023/atlmfc/lib/x64;/mnt/windows/program files (x86)/microsoft visual studio/2017/community/vc/tools/msvc/14.16.27023/lib/x64;/mnt/windows/program files (x86)/microsoft visual studio/2017/community/vc/tools/msvc/14.16.27023/lib/x86/store/references;/mnt/windows/program files (x86)/windows kits/10/unionmetadata/10.0.17763.0;/mnt/windows/program files (x86)/windows kits/10/references/10.0.17763.0"

View File

@ -0,0 +1,11 @@
#include <Windows.h>
int main(int argc, char* argv[])
{
(void)argc;
(void)argv;
WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE), "Hello world!\n", 13, NULL, NULL);
return 0;
}

View File

@ -0,0 +1,32 @@
#! /usr/bin/env python
# encoding: utf-8
# DragoonX6 2019
# the following two variables are used by the target "waf dist"
VERSION='0.0.1'
APPNAME='hello_msvc'
top = '.'
from waflib.Configure import conf, ConfigurationContext
from waflib.Options import OptionsContext
def options(opt):
opt.load('clang_cross')
def configure(conf):
conf.load('clang_cross')
if not conf.env.implib_PATTERN == '%s.lib':
conf.fatal('''clang is not configured to compile in msvc mode.
Use flag '--clang-target-triple=x86_64-windows-msvc' to configure.
On Windows you're likely to require running from an MSVC command prompt.
On Linux you will need to have access to a Windows partition with VS installed, and the environment set up properly.
See the ReadMe for more information.''')
conf.env.append_value('CFLAGS', conf.env.CFLAGS_CRT_MULTITHREADED_DLL)
def build(bld):
bld.program(
source = 'hello.c',
target = 'hello_msvc')