Skip to content

Commit 7cf0b17

Browse files
committed
configure: Start adding MSVC support
This commit starts to add MSVC support to the ./configure script to enable the build system to detect and build an MSVC target with the cl.exe compiler and toolchain. The primary change here is a large sanity check when an MSVC target is requested (and currently only `x86_64-pc-windows-msvc` is recognized). When building an MSVC target, the configure script either requires the `--msvc-root` argument or for `cl.exe` to be in `PATH`. It also requires that if in the path `cl.exe` is the 64-bit version of the compiler. Once detected the configure script will run the `vcvarsall.bat` script provided by Visual Studio to learn about the `INCLUDE` and `LIB` variables needed by the `cl.exe` compiler to run (the default include/lib paths for the compiler/linker). These variables are then reexported when running `make` to ensure that our own compiles are running the same toolchain. The purpose of this detection and environment variable scraping is to avoid requiring the build itself to be run inside of a `cmd.exe` shell but rather allow it to run in the currently expected MinGW/MSYS shell.
1 parent ee258c5 commit 7cf0b17

File tree

2 files changed

+83
-11
lines changed

2 files changed

+83
-11
lines changed

configure

Lines changed: 60 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1084,6 +1084,65 @@ do
10841084
err "musl libc $CFG_MUSL_ROOT/lib/libc.a not found"
10851085
fi
10861086
;;
1087+
1088+
x86_64-*-msvc)
1089+
# Currently the build system is not configured to build jemalloc
1090+
# with MSVC, so we omit this optional dependency.
1091+
step_msg "targeting MSVC, disabling jemalloc"
1092+
CFG_DISABLE_JEMALLOC=1
1093+
putvar CFG_DISABLE_JEMALLOC
1094+
1095+
# There are some MSYS python builds which will auto-translate
1096+
# windows-style paths to MSYS-style paths in Python itself.
1097+
# Unfortunately this breaks LLVM's build system as somewhere along
1098+
# the line LLVM prints a path into a file from Python and then CMake
1099+
# later tries to interpret that path. If Python prints a MSYS path
1100+
# and CMake tries to use it as a Windows path, you're gonna have a
1101+
# Bad Time.
1102+
#
1103+
# Consequently here we try to detect when that happens and print an
1104+
# error if it does.
1105+
if $CFG_PYTHON -c 'import sys; print sys.argv[1]' `pwd` | grep '^/'
1106+
then
1107+
err "python is silently translating windows paths to MSYS paths \
1108+
and the build will fail if this python is used.\n\n \
1109+
Either an official python install must be used or an \
1110+
alternative python package in MinGW must be used."
1111+
fi
1112+
1113+
# MSVC requires cmake because that's how we're going to build LLVM
1114+
probe_need CFG_CMAKE cmake
1115+
1116+
# Use the REG program to figure out where VS is installed
1117+
# We need to figure out where cl.exe and link.exe are, so we do some
1118+
# munging and some probing here. We also look for the default
1119+
# INCLUDE and LIB variables for MSVC so we can set those in the
1120+
# build system as well.
1121+
install=$(reg QUERY \
1122+
'HKLM\SOFTWARE\Wow6432Node\Microsoft\VisualStudio\12.0' \
1123+
-v InstallDir)
1124+
need_ok "couldn't find visual studio install root"
1125+
CFG_MSVC_ROOT=$(echo "$install" | grep InstallDir | sed 's/.*REG_SZ[ ]*//')
1126+
CFG_MSVC_ROOT=$(dirname "$CFG_MSVC_ROOT")
1127+
CFG_MSVC_ROOT=$(dirname "$CFG_MSVC_ROOT")
1128+
CFG_MSVC_CL="${CFG_MSVC_ROOT}/VC/bin/amd64/cl.exe"
1129+
CFG_MSVC_LIB="${CFG_MSVC_ROOT}/VC/bin/amd64/lib.exe"
1130+
CFG_MSVC_LINK="${CFG_MSVC_ROOT}/VC/bin/amd64/link.exe"
1131+
1132+
vcvarsall="${CFG_MSVC_ROOT}/VC/vcvarsall.bat"
1133+
CFG_MSVC_INCLUDE_PATH=$(cmd /c "\"$vcvarsall\" amd64 && cmd /c echo %INCLUDE%")
1134+
need_ok "failed to learn about MSVC's INCLUDE"
1135+
CFG_MSVC_LIB_PATH=$(cmd /c "\"$vcvarsall\" amd64 && cmd /c echo %LIB%")
1136+
need_ok "failed to learn about MSVC's LIB"
1137+
1138+
putvar CFG_MSVC_ROOT
1139+
putvar CFG_MSVC_CL
1140+
putvar CFG_MSVC_LIB
1141+
putvar CFG_MSVC_LINK
1142+
putvar CFG_MSVC_INCLUDE_PATH
1143+
putvar CFG_MSVC_LIB_PATH
1144+
;;
1145+
10871146
*)
10881147
;;
10891148
esac
@@ -1125,6 +1184,7 @@ do
11251184
do
11261185
make_dir $t/rt/stage$s
11271186
make_dir $t/rt/jemalloc
1187+
make_dir $t/rt/compiler-rt
11281188
for i in \
11291189
isaac sync test \
11301190
arch/i386 arch/x86_64 arch/arm arch/aarch64 arch/mips arch/powerpc
@@ -1496,11 +1556,6 @@ do
14961556
putvar $CFG_LLVM_INST_DIR
14971557
done
14981558

1499-
# Munge any paths that appear in config.mk back to posix-y
1500-
cp config.tmp config.tmp.bak
1501-
sed -e 's@ \([a-zA-Z]\):[/\\]@ /\1/@g;' <config.tmp.bak >config.tmp
1502-
rm -f config.tmp.bak
1503-
15041559
msg
15051560
copy_if_changed ${CFG_SRC_DIR}Makefile.in ./Makefile
15061561
move_if_changed config.tmp config.mk

mk/cfg/x86_64-pc-windows-msvc.mk

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# x86_64-pc-windows-msvc configuration
2-
CC_x86_64-pc-windows-msvc=cl
3-
LINK_x86_64-pc-windows-msvc=link
4-
CXX_x86_64-pc-windows-msvc=cl
5-
CPP_x86_64-pc-windows-msvc=cl
6-
AR_x86_64-pc-windows-msvc=llvm-ar
2+
CC_x86_64-pc-windows-msvc="$(CFG_MSVC_CL)" -nologo
3+
LINK_x86_64-pc-windows-msvc="$(CFG_MSVC_LINK)" -nologo
4+
CXX_x86_64-pc-windows-msvc="$(CFG_MSVC_CL)" -nologo
5+
CPP_x86_64-pc-windows-msvc="$(CFG_MSVC_CL)" -nologo
6+
AR_x86_64-pc-windows-msvc="$(CFG_MSVC_LIB)" -nologo
77
CFG_LIB_NAME_x86_64-pc-windows-msvc=$(1).dll
88
CFG_STATIC_LIB_NAME_x86_64-pc-windows-msvc=$(1).lib
99
CFG_LIB_GLOB_x86_64-pc-windows-msvc=$(1)-*.dll
@@ -21,4 +21,21 @@ CFG_UNIXY_x86_64-pc-windows-msvc :=
2121
CFG_LDPATH_x86_64-pc-windows-msvc :=
2222
CFG_RUN_x86_64-pc-windows-msvc=$(2)
2323
CFG_RUN_TARG_x86_64-pc-windows-msvc=$(call CFG_RUN_x86_64-pc-windows-msvc,,$(2))
24-
CFG_GNU_TRIPLE_x86_64-pc-windows-msvc := x86_64-pc-windows-msvc
24+
CFG_GNU_TRIPLE_x86_64-pc-windows-msvc := x86_64-pc-win32
25+
26+
# These two environment variables are scraped by the `./configure` script and
27+
# are necessary for `cl.exe` to find standard headers (the INCLUDE variable) and
28+
# for `link.exe` to find standard libraries (the LIB variable).
29+
ifdef CFG_MSVC_INCLUDE_PATH
30+
export INCLUDE := $(CFG_MSVC_INCLUDE_PATH)
31+
endif
32+
ifdef CFG_MSVC_LIB_PATH
33+
export LIB := $(CFG_MSVC_LIB_PATH)
34+
endif
35+
36+
# Unfortunately `link.exe` is also a program in `/usr/bin` on MinGW installs,
37+
# but it's not the one that we want. As a result we make sure that our detected
38+
# `link.exe` shows up in PATH first.
39+
ifdef CFG_MSVC_LINK
40+
export PATH := $(CFG_MSVC_ROOT)/VC/bin/amd64:$(PATH)
41+
endif

0 commit comments

Comments
 (0)