Skip to content
This repository was archived by the owner on Jan 12, 2024. It is now read-only.

Commit e024636

Browse files
authored
Use explicit static VC runtime on Windows (#851)
* Use explicit static VC runtime on Windows This change updates the QIR runtime to use an explicit path to the Spectre-mitigated, static VC runtime on Windows. Using a full path here overrides the selection logic and ensures the chosen libcmt[d] and vcruntime[d] are loaded. * Add "-sort" flag * Include version number in find pattern * Include libcpmt library as well * Use spectre component name
1 parent 3efb72b commit e024636

File tree

5 files changed

+45
-0
lines changed

5 files changed

+45
-0
lines changed

src/Qir/Runtime/CMakeLists.txt

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,47 @@ if (NOT APPLE)
1717
endif()
1818

1919
if (WIN32)
20+
# Enforce use of static runtime (avoids target machine needing msvcrt installed).
21+
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
22+
23+
# Locate the vswhere application, which will provide paths to any installed Visual Studio instances.
24+
# By invoking it with "-find **/lib/spectre/x64" we will find any Spectre mitigated libaries that
25+
# have been installed.
26+
find_program(_vswhere_tool
27+
NAMES vswhere
28+
PATHS "$ENV{ProgramFiles\(x86\)}/Microsoft Visual Studio/Installer")
29+
if (NOT ${vswhere})
30+
message(FATAL_ERROR "Could not locate vswhere.exe - unable to source vc redistributable")
31+
endif()
32+
execute_process(
33+
COMMAND "${_vswhere_tool}" -latest -products * -requires Microsoft.VisualStudio.Component.VC.Tools.x86.x64 -find **/14.29.*/**/lib/spectre/x64 -sort
34+
OUTPUT_VARIABLE _vs_install_loc_out
35+
RESULT_VARIABLE _vs_where_exitcode
36+
OUTPUT_STRIP_TRAILING_WHITESPACE)
37+
file(TO_CMAKE_PATH "${_vs_install_loc_out}" SPECTRE_LIB_PATH_OUT)
38+
string(REGEX REPLACE "[\r\n]+" ";" SPECTRE_LIB_PATH ${SPECTRE_LIB_PATH_OUT})
39+
message(INFO "*** install loc: ${SPECTRE_LIB_PATH}")
40+
41+
# Locate the spectre mitigated runtime libraries and fail if they can't be found. Targets in this
42+
# cmake project can use the variables to explicitly link these libraries rather than using the
43+
# non-mitigated libraries that are found by default.
44+
find_library(LIBCMT_SPECTRE_REL libcmt PATHS ${SPECTRE_LIB_PATH} REQUIRED)
45+
find_library(LIBCMT_SPECTRE_DEB libcmtd PATHS ${SPECTRE_LIB_PATH} REQUIRED)
46+
set(LIBCMT_SPECTRE debug ${LIBCMT_SPECTRE_DEB} optimized ${LIBCMT_SPECTRE_REL})
47+
message(INFO "*** using spectre lib: ${LIBCMT_SPECTRE}")
48+
find_library(LIBCPMT_SPECTRE_REL libcpmt PATHS ${SPECTRE_LIB_PATH} REQUIRED)
49+
find_library(LIBCPMT_SPECTRE_DEB libcpmtd PATHS ${SPECTRE_LIB_PATH} REQUIRED)
50+
set(LIBCPMT_SPECTRE debug ${LIBCPMT_SPECTRE_DEB} optimized ${LIBCPMT_SPECTRE_REL})
51+
message(INFO "*** using spectre lib: ${LIBCPMT_SPECTRE}")
52+
find_library(LIBVCRUNTIME_SPECTRE_REL libvcruntime PATHS ${SPECTRE_LIB_PATH} REQUIRED)
53+
find_library(LIBVCRUNTIME_SPECTRE_DEB libvcruntimed PATHS ${SPECTRE_LIB_PATH} REQUIRED)
54+
set(LIBVCRUNTIME_SPECTRE debug ${LIBVCRUNTIME_SPECTRE_DEB} optimized ${LIBVCRUNTIME_SPECTRE_REL})
55+
message(INFO "*** using spectre lib: ${LIBVCRUNTIME_SPECTRE}")
56+
set(SPECTRE_LIBS
57+
${LIBCMT_SPECTRE}
58+
${LIBCPMT_SPECTRE}
59+
${LIBVCRUNTIME_SPECTRE})
60+
2061
add_link_options("LINKER:/guard:cf")
2162
endif()
2263

src/Qir/Runtime/lib/QIR/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ add_library(Microsoft.Quantum.Qir.Runtime SHARED)
4343
target_link_libraries(Microsoft.Quantum.Qir.Runtime
4444
${CMAKE_DL_LIBS}
4545
qir-rt-support-obj
46+
${SPECTRE_LIBS}
4647
)
4748

4849
target_include_directories(Microsoft.Quantum.Qir.Runtime PUBLIC ${public_includes})

src/Qir/Runtime/lib/QSharpCore/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ target_link_libraries(Microsoft.Quantum.Qir.QSharp.Core
2727
simulators-obj
2828
"-L${CMAKE_BINARY_DIR}/lib/QIR"
2929
-lMicrosoft.Quantum.Qir.Runtime
30+
${SPECTRE_LIBS}
3031
)
3132
add_dependencies(Microsoft.Quantum.Qir.QSharp.Core Microsoft.Quantum.Qir.Runtime)
3233

src/Qir/Runtime/lib/QSharpFoundation/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ target_link_libraries(Microsoft.Quantum.Qir.QSharp.Foundation
3030
qsharp-foundation-qis-support-obj
3131
"-L${CMAKE_BINARY_DIR}/lib/QIR"
3232
-lMicrosoft.Quantum.Qir.Runtime
33+
${SPECTRE_LIBS}
3334
)
3435
add_dependencies(Microsoft.Quantum.Qir.QSharp.Foundation Microsoft.Quantum.Qir.Runtime)
3536

src/Qir/Runtime/lib/Tracer/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ target_link_libraries(Microsoft.Quantum.Qir.Tracer
2525
tracer-obj
2626
"-L${CMAKE_BINARY_DIR}/lib/QIR"
2727
-lMicrosoft.Quantum.Qir.Runtime
28+
${SPECTRE_LIBS}
2829
)
2930
add_dependencies(Microsoft.Quantum.Qir.QSharp.Foundation Microsoft.Quantum.Qir.Runtime)
3031

0 commit comments

Comments
 (0)