Skip to content

Commit 46ad93c

Browse files
committed
In MSVC builds embed a VERSIONINFO resource in our exe and DLL files.
This reinstates my commits r238740/r238741 which I reverted due to a failure in the clang-cl selfhost tests on Windows. I've now fixed the issue in clang-cl that caused the failure so hopefully all should be well now. llvm-svn: 239612
1 parent 12677ab commit 46ad93c

File tree

2 files changed

+171
-0
lines changed

2 files changed

+171
-0
lines changed

cmake/modules/AddLLVM.cmake

+82
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,74 @@ function(set_output_directory target bindir libdir)
228228
endif()
229229
endfunction()
230230

231+
# If on Windows and building with MSVC, add the resource script containing the
232+
# VERSIONINFO data to the project. This embeds version resource information
233+
# into the output .exe or .dll.
234+
# TODO: Enable for MinGW Windows builds too.
235+
#
236+
function(add_windows_version_resource_file OUT_VAR)
237+
set(sources ${ARGN})
238+
if (MSVC)
239+
set(resource_file ${LLVM_SOURCE_DIR}/resources/windows_version_resource.rc)
240+
set(sources ${sources} ${resource_file})
241+
source_group("Resource Files" ${resource_file})
242+
set(windows_resource_file ${resource_file} PARENT_SCOPE)
243+
endif(MSVC)
244+
245+
set(${OUT_VAR} ${sources} PARENT_SCOPE)
246+
endfunction(add_windows_version_resource_file)
247+
248+
# set_windows_version_resource_properties(name resource_file...
249+
# VERSION_MAJOR int
250+
# Optional major version number (defaults to LLVM_VERSION_MAJOR)
251+
# VERSION_MINOR int
252+
# Optional minor version number (defaults to LLVM_VERSION_MINOR)
253+
# VERSION_PATCHLEVEL int
254+
# Optional patchlevel version number (defaults to LLVM_VERSION_PATCH)
255+
# VERSION_STRING
256+
# Optional version string (defaults to PACKAGE_VERSION)
257+
# PRODUCT_NAME
258+
# Optional product name string (defaults to "LLVM")
259+
# )
260+
function(set_windows_version_resource_properties name resource_file)
261+
cmake_parse_arguments(ARG
262+
""
263+
"VERSION_MAJOR;VERSION_MINOR;VERSION_PATCHLEVEL;VERSION_STRING;PRODUCT_NAME"
264+
""
265+
${ARGN})
266+
267+
if (NOT DEFINED ARG_VERSION_MAJOR)
268+
set(ARG_VERSION_MAJOR ${LLVM_VERSION_MAJOR})
269+
endif()
270+
271+
if (NOT DEFINED ARG_VERSION_MINOR)
272+
set(ARG_VERSION_MINOR ${LLVM_VERSION_MINOR})
273+
endif()
274+
275+
if (NOT DEFINED ARG_VERSION_PATCHLEVEL)
276+
set(ARG_VERSION_PATCHLEVEL ${LLVM_VERSION_PATCH})
277+
endif()
278+
279+
if (NOT DEFINED ARG_VERSION_STRING)
280+
set(ARG_VERSION_STRING ${PACKAGE_VERSION})
281+
endif()
282+
283+
if (NOT DEFINED ARG_PRODUCT_NAME)
284+
set(ARG_PRODUCT_NAME "LLVM")
285+
endif()
286+
287+
set_property(SOURCE ${resource_file}
288+
PROPERTY COMPILE_DEFINITIONS
289+
"RC_VERSION_FIELD_1=${ARG_VERSION_MAJOR}"
290+
"RC_VERSION_FIELD_2=${ARG_VERSION_MINOR}"
291+
"RC_VERSION_FIELD_3=${ARG_VERSION_PATCHLEVEL}"
292+
"RC_VERSION_FIELD_4=0"
293+
"RC_FILE_VERSION=\"${ARG_VERSION_STRING}\""
294+
"RC_INTERNAL_NAME=\"${name}\""
295+
"RC_PRODUCT_NAME=\"${ARG_PRODUCT_NAME}\""
296+
"RC_PRODUCT_VERSION=\"${ARG_VERSION_STRING}\"")
297+
endfunction(set_windows_version_resource_properties)
298+
231299
# llvm_add_library(name sources...
232300
# SHARED;STATIC
233301
# STATIC by default w/o BUILD_SHARED_LIBS.
@@ -316,10 +384,17 @@ function(llvm_add_library name)
316384
if(ARG_MODULE)
317385
add_library(${name} MODULE ${ALL_FILES})
318386
elseif(ARG_SHARED)
387+
add_windows_version_resource_file(ALL_FILES ${ALL_FILES})
319388
add_library(${name} SHARED ${ALL_FILES})
320389
else()
321390
add_library(${name} STATIC ${ALL_FILES})
322391
endif()
392+
393+
if(DEFINED windows_resource_file)
394+
set_windows_version_resource_properties(${name} ${windows_resource_file})
395+
set(windows_resource_file ${windows_resource_file} PARENT_SCOPE)
396+
endif()
397+
323398
set_output_directory(${name} ${LLVM_RUNTIME_OUTPUT_INTDIR} ${LLVM_LIBRARY_OUTPUT_INTDIR})
324399
llvm_update_compile_flags(${name})
325400
add_link_opts( ${name} )
@@ -482,11 +557,18 @@ endmacro(add_llvm_loadable_module name)
482557

483558
macro(add_llvm_executable name)
484559
llvm_process_sources( ALL_FILES ${ARGN} )
560+
add_windows_version_resource_file(ALL_FILES ${ALL_FILES})
561+
485562
if( EXCLUDE_FROM_ALL )
486563
add_executable(${name} EXCLUDE_FROM_ALL ${ALL_FILES})
487564
else()
488565
add_executable(${name} ${ALL_FILES})
489566
endif()
567+
568+
if(DEFINED windows_resource_file)
569+
set_windows_version_resource_properties(${name} ${windows_resource_file})
570+
endif()
571+
490572
llvm_update_compile_flags(${name})
491573
add_link_opts( ${name} )
492574

resources/windows_version_resource.rc

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
// Microsoft Visual C++ resource script for embedding version information.
2+
// The format is described at:
3+
// http://msdn.microsoft.com/en-gb/library/windows/desktop/aa380599(v=vs.85).aspx
4+
// The VERSIONINFO resource is described at:
5+
// https://msdn.microsoft.com/en-gb/library/windows/desktop/aa381058(v=vs.85).aspx
6+
7+
8+
// Default values for required fields.
9+
10+
#ifndef RC_VERSION_FIELD_1
11+
#define RC_VERSION_FIELD_1 0
12+
#endif
13+
14+
#ifndef RC_VERSION_FIELD_2
15+
#define RC_VERSION_FIELD_2 0
16+
#endif
17+
18+
#ifndef RC_VERSION_FIELD_3
19+
#define RC_VERSION_FIELD_3 0
20+
#endif
21+
22+
#ifndef RC_VERSION_FIELD_4
23+
#define RC_VERSION_FIELD_4 0
24+
#endif
25+
26+
#ifndef RC_COMPANY_NAME
27+
#define RC_COMPANY_NAME ""
28+
#endif
29+
30+
#ifndef RC_FILE_DESCRIPTION
31+
#define RC_FILE_DESCRIPTION ""
32+
#endif
33+
34+
#ifndef RC_FILE_VERSION
35+
#define RC_FILE_VERSION ""
36+
#endif
37+
38+
#ifndef RC_INTERNAL_NAME
39+
#define RC_INTERNAL_NAME ""
40+
#endif
41+
42+
#ifndef RC_ORIGINAL_FILENAME
43+
#define RC_ORIGINAL_FILENAME ""
44+
#endif
45+
46+
#ifndef RC_PRODUCT_NAME
47+
#define RC_PRODUCT_NAME ""
48+
#endif
49+
50+
#ifndef RC_PRODUCT_VERSION
51+
#define RC_PRODUCT_VERSION ""
52+
#endif
53+
54+
55+
1 VERSIONINFO
56+
FILEVERSION RC_VERSION_FIELD_1,RC_VERSION_FIELD_2,RC_VERSION_FIELD_3,RC_VERSION_FIELD_4
57+
BEGIN
58+
BLOCK "StringFileInfo"
59+
BEGIN
60+
BLOCK "040904B0"
61+
BEGIN
62+
// Required strings
63+
VALUE "CompanyName", RC_COMPANY_NAME
64+
VALUE "FileDescription", RC_FILE_DESCRIPTION
65+
VALUE "FileVersion", RC_FILE_VERSION
66+
VALUE "InternalName", RC_INTERNAL_NAME
67+
VALUE "OriginalFilename", RC_ORIGINAL_FILENAME
68+
VALUE "ProductName", RC_PRODUCT_NAME
69+
VALUE "ProductVersion", RC_PRODUCT_VERSION
70+
71+
// Optional strings
72+
#ifdef RC_COMMENTS
73+
VALUE "Comments", RC_COMMENTS
74+
#endif
75+
76+
#ifdef RC_COPYRIGHT
77+
VALUE "LegalCopyright", RC_COPYRIGHT
78+
#endif
79+
END
80+
END
81+
82+
BLOCK "VarFileInfo"
83+
BEGIN
84+
// The translation must correspond to the above BLOCK inside StringFileInfo
85+
// langID 0x0409 U.S. English
86+
// charsetID 0x04B0 Unicode
87+
VALUE "Translation", 0x0409, 0x04B0
88+
END
89+
END

0 commit comments

Comments
 (0)