Skip to content

Commit 33541cd

Browse files
authored
Add versioning for Windows (#285)
1 parent c01b4f1 commit 33541cd

7 files changed

Lines changed: 128 additions & 1 deletion

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
build/**/*
33
docs/_build/
44
docs/sphinx/_toc.yml
5+
.idea/

docs/src/reference/ROCMSetupVersion.rst

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,17 @@ Get the version of directory using git tags if possible.
3434
3535
Setup the version for the project. This will try to use git tag to set the version if possible unless ``NO_GIT_TAG_VERSION`` is passed. The ``PARENT`` argument can be used to set the commit to start the count of number of commits to the current revision.
3636

37+
.. cmake:command:: rocm_add_resource_version
38+
39+
.. code-block:: cmake
40+
41+
rocm_add_version_resource(<target> <product_name> <product_description>)
42+
43+
Add a Windows PE version resource to a shared library, module library (DLL) or executable (EXE) target.
44+
45+
Example
46+
-------
47+
48+
.. code-block: cmake
49+
50+
rocm_add_version_resource(mylib "MyLib" "My ROCm Library")

share/rocmcmakebuildtools/cmake/ROCMSetupVersion.cmake

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
# ######################################################################################################################
2-
# Copyright (C) 2017 Advanced Micro Devices, Inc.
2+
# Copyright (C) 2026 Advanced Micro Devices, Inc.
33
# ######################################################################################################################
44

5+
include_guard(GLOBAL)
6+
57
macro(rocm_set_parent VAR)
68
set(${VAR}
79
${ARGN}
@@ -170,3 +172,70 @@ function(rocm_set_soversion LIBRARY_TARGET SOVERSION)
170172
set_target_properties(${LIBRARY_TARGET} PROPERTIES VERSION ${LIB_VERSION_STRING})
171173
endif()
172174
endfunction()
175+
176+
function(rocm_add_version_resource TARGET NAME DESCRIPTION)
177+
if(WIN32)
178+
get_target_property(TARGET_TYPE ${TARGET} TYPE)
179+
if (NOT TARGET_TYPE STREQUAL "EXECUTABLE" AND
180+
NOT TARGET_TYPE STREQUAL "SHARED_LIBRARY" AND
181+
NOT TARGET_TYPE STREQUAL "MODULE_LIBRARY")
182+
message(FATAL_ERROR "${CMAKE_CURRENT_FUNCTION}: only EXECUTABLE, SHARED_LIBRARY, "
183+
"and MODULE_LIBRARY target types are supported")
184+
endif()
185+
get_target_property(FILENAME ${TARGET} OUTPUT_NAME)
186+
if(NOT FILENAME)
187+
if(TARGET_TYPE STREQUAL "EXECUTABLE")
188+
set(FILENAME ${TARGET}${CMAKE_EXECUTABLE_SUFFIX})
189+
elseif(TARGET_TYPE STREQUAL "SHARED_LIBRARY")
190+
set(FILENAME ${TARGET}${CMAKE_SHARED_LIBRARY_SUFFIX})
191+
elseif(TARGET_TYPE STREQUAL "MODULE_LIBRARY")
192+
set(FILENAME ${TARGET}${CMAKE_SHARED_MODULE_SUFFIX})
193+
endif()
194+
endif()
195+
string(TIMESTAMP YEAR "%Y")
196+
set(RC_OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${TARGET}_version.rc")
197+
file(WRITE "${RC_OUTPUT}" "#include <winver.h>
198+
VS_VERSION_INFO VERSIONINFO
199+
FILEVERSION ${PROJECT_VERSION_MAJOR},${PROJECT_VERSION_MINOR},${PROJECT_VERSION_PATCH},0
200+
PRODUCTVERSION ${PROJECT_VERSION_MAJOR},${PROJECT_VERSION_MINOR},${PROJECT_VERSION_PATCH},0
201+
FILEFLAGSMASK VS_FFI_FILEFLAGSMASK
202+
#ifdef _DEBUG
203+
FILEFLAGS VS_FF_DEBUG
204+
#else
205+
FILEFLAGS 0
206+
#endif
207+
FILEOS VOS_NT_WINDOWS32
208+
#ifdef DLL_BUILD
209+
FILETYPE VFT_DLL
210+
#else
211+
FILETYPE VFT_APP
212+
#endif
213+
FILESUBTYPE VFT2_UNKNOWN
214+
BEGIN
215+
BLOCK \"StringFileInfo\"
216+
BEGIN
217+
BLOCK \"040904B0\"
218+
BEGIN
219+
VALUE \"CompanyName\", \"Advanced Micro Devices, Inc.\\0\"
220+
VALUE \"FileDescription\", \"${DESCRIPTION}\\0\"
221+
VALUE \"FileVersion\", \"${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}.0\\0\"
222+
VALUE \"InternalName\", \"${NAME}\\0\"
223+
VALUE \"LegalCopyright\", \"Copyright (c) ${YEAR} Advanced Micro Devices, Inc. All rights reserved.\\0\"
224+
VALUE \"OriginalFilename\", \"${FILENAME}\\0\"
225+
VALUE \"ProductName\", \"${NAME} ${FILENAME}\\0\"
226+
VALUE \"ProductVersion\", \"${PROJECT_VERSION}\\0\"
227+
END
228+
END
229+
BLOCK \"VarFileInfo\"
230+
BEGIN
231+
VALUE \"Translation\", 0x409, 1200
232+
END
233+
END
234+
")
235+
target_sources(${TARGET} PRIVATE ${RC_OUTPUT})
236+
if(TARGET_TYPE STREQUAL "SHARED_LIBRARY" OR TARGET_TYPE STREQUAL "MODULE_LIBRARY")
237+
set_source_files_properties(${RC_OUTPUT} PROPERTIES COMPILE_FLAGS -DDLL_BUILD)
238+
endif()
239+
message(STATUS "Added version resource to ${TARGET}: ${DESCRIPTION}")
240+
endif()
241+
endfunction()

test/pass/version-resource.cmake

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# ######################################################################################################################
2+
# Copyright (C) 2026 Advanced Micro Devices, Inc.
3+
# ######################################################################################################################
4+
5+
if(WIN32)
6+
configure_dir(${TEST_DIR}/versionresource TARGETS all BUILD_DIR_VAR BUILD_DIR)
7+
test_expect_file(${BUILD_DIR}/mylib_version.rc)
8+
test_expect_file(${BUILD_DIR}/myapp_version.rc)
9+
endif()
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# ######################################################################################################################
2+
# Copyright (C) 2026 Advanced Micro Devices, Inc.
3+
# ######################################################################################################################
4+
5+
cmake_minimum_required(VERSION 3.5...3.31.0)
6+
project(versionresource VERSION 1.2.3 LANGUAGES CXX)
7+
8+
find_package(ROCmCMakeBuildTools)
9+
10+
include(ROCMSetupVersion)
11+
12+
add_library(mylib SHARED mylib.cpp)
13+
rocm_add_version_resource(mylib "MyLib" "Test version resource library")
14+
15+
add_executable(myapp myapp.cpp)
16+
target_link_libraries(myapp mylib)
17+
rocm_add_version_resource(myapp "MyApp" "Test version resource application")

test/versionresource/myapp.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Copyright (C) 2026 Advanced Micro Devices, Inc.
2+
3+
#ifdef _WIN32
4+
__declspec(dllimport)
5+
#endif
6+
void mylib_func();
7+
8+
int main() {
9+
mylib_func();
10+
return 0;
11+
}

test/versionresource/mylib.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// Copyright (C) 2026 Advanced Micro Devices, Inc.
2+
3+
#ifdef _WIN32
4+
__declspec(dllexport)
5+
#endif
6+
void mylib_func() {}

0 commit comments

Comments
 (0)