From 1581e221b4d7c6f706770b7871e06f2bafaf9d22 Mon Sep 17 00:00:00 2001 From: Dragoon Date: Thu, 9 May 2019 23:18:51 +0200 Subject: [PATCH] Add a simple example for using Clang as a cross compiler for MSVC --- playground/clang_cross/msvc/ReadMe.txt | 14 ++++++++ .../msvc/example_environment_linux.sh | 5 +++ playground/clang_cross/msvc/hello.c | 11 +++++++ playground/clang_cross/msvc/wscript | 32 +++++++++++++++++++ 4 files changed, 62 insertions(+) create mode 100644 playground/clang_cross/msvc/ReadMe.txt create mode 100644 playground/clang_cross/msvc/example_environment_linux.sh create mode 100644 playground/clang_cross/msvc/hello.c create mode 100644 playground/clang_cross/msvc/wscript diff --git a/playground/clang_cross/msvc/ReadMe.txt b/playground/clang_cross/msvc/ReadMe.txt new file mode 100644 index 00000000..e82291a4 --- /dev/null +++ b/playground/clang_cross/msvc/ReadMe.txt @@ -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. diff --git a/playground/clang_cross/msvc/example_environment_linux.sh b/playground/clang_cross/msvc/example_environment_linux.sh new file mode 100644 index 00000000..1fd5a005 --- /dev/null +++ b/playground/clang_cross/msvc/example_environment_linux.sh @@ -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" diff --git a/playground/clang_cross/msvc/hello.c b/playground/clang_cross/msvc/hello.c new file mode 100644 index 00000000..3ae11826 --- /dev/null +++ b/playground/clang_cross/msvc/hello.c @@ -0,0 +1,11 @@ +#include + +int main(int argc, char* argv[]) +{ + (void)argc; + (void)argv; + + WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE), "Hello world!\n", 13, NULL, NULL); + + return 0; +} diff --git a/playground/clang_cross/msvc/wscript b/playground/clang_cross/msvc/wscript new file mode 100644 index 00000000..a1ed74a3 --- /dev/null +++ b/playground/clang_cross/msvc/wscript @@ -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')