From 9d7601de4c13a62042103dffa1c72da9d229641b Mon Sep 17 00:00:00 2001 From: ROCm CI Service Account <66695075+rocm-ci@users.noreply.github.com> Date: Fri, 5 Aug 2022 15:13:19 +0530 Subject: [PATCH] SWDEV-334508 - CMake create static lib & linker commands for windows. (#2841) Change-Id: I544837a65e47d425a946c666d0b2348aa7a33b15 [ROCm/hip-tests commit: 908988924aaed6441fdcec8b35e6721ac52db3cc] --- .../2_Cookbook/15_static_library/README.md | 3 +++ .../device_functions/CMakeLists.txt | 22 +++++++++++++++++-- .../host_functions/CMakeLists.txt | 22 ++++++++++++++----- 3 files changed, 40 insertions(+), 7 deletions(-) diff --git a/projects/hip-tests/samples/2_Cookbook/15_static_library/README.md b/projects/hip-tests/samples/2_Cookbook/15_static_library/README.md index bf0123573d..a38dc93451 100644 --- a/projects/hip-tests/samples/2_Cookbook/15_static_library/README.md +++ b/projects/hip-tests/samples/2_Cookbook/15_static_library/README.md @@ -172,5 +172,8 @@ cmake .. make ./test_*.out ``` +It is recommended to use Visual Studio's command prompt for this sample due to requirement of MS Librarian tool - LIB.exe on windows platform. +Override CMAKE_C_COMPILER and CMAKE_CXX_COMPILER to hipcc as Visual Studio's compiler would use cl.exe as default compiler. +i.e. cmake.exe -GNinja -DCMAKE_CXX_COMPILER_ID=ROCMClang -DCMAKE_C_COMPILER_ID=ROCMClang -DCMAKE_PREFIX_PATH=%HIP_PATH% -DCMAKE_C_COMPILER=%HIP_PATH%/bin/hipcc.bat -DCMAKE_CXX_COMPILER=%HIP_PATH%/bin/hipcc.bat .. ## For More Infomation, please refer to the HIP FAQ. diff --git a/projects/hip-tests/samples/2_Cookbook/15_static_library/device_functions/CMakeLists.txt b/projects/hip-tests/samples/2_Cookbook/15_static_library/device_functions/CMakeLists.txt index 30a5928389..317b7a819e 100644 --- a/projects/hip-tests/samples/2_Cookbook/15_static_library/device_functions/CMakeLists.txt +++ b/projects/hip-tests/samples/2_Cookbook/15_static_library/device_functions/CMakeLists.txt @@ -12,6 +12,12 @@ list(APPEND CMAKE_PREFIX_PATH ${ROCM_PATH}/hip ${ROCM_PATH}) # Find hip find_package(hip REQUIRED) +# For windows, AR is MS Librarian and that is picked by Visual Studio's command prompt. +if (WIN32) + find_program(libpath NAMES lib.exe) + set (CMAKE_AR ${libpath}) +endif() + # Set compiler and linker set(CMAKE_CXX_COMPILER ${HIP_HIPCC_EXECUTABLE}) set(CMAKE_CXX_LINKER ${HIP_HIPCC_EXECUTABLE}) @@ -22,6 +28,11 @@ option(BUILD_SHARED_LIBS "Build as a shared library" OFF) set(CPP_SOURCES hipDevice.cpp) +# For windows, We need to tell cmake how to create static library. +if (WIN32) + set (CMAKE_CXX_CREATE_STATIC_LIBRARY " /out: ") +endif() + # Generate static lib libHipDevice.a add_library(HipDevice STATIC ${CPP_SOURCES}) @@ -35,6 +46,13 @@ set(TEST_SOURCES ${CMAKE_SOURCE_DIR}/hipMain2.cpp) add_executable(test_device_static ${TEST_SOURCES}) add_dependencies(test_device_static HipDevice) target_compile_options(test_device_static PRIVATE -fgpu-rdc) -target_link_libraries(test_device_static PRIVATE HipDevice) -target_link_libraries(test_device_static PRIVATE -fgpu-rdc hip::host) + +# For windows, Change in a way to pass lib details +if (WIN32) + target_link_libraries(test_device_static PRIVATE -lHipDevice -L${CMAKE_BINARY_DIR}) +else() + target_link_libraries(test_device_static PRIVATE HipDevice) +endif() + +target_link_libraries(test_device_static PRIVATE -fgpu-rdc amdhip64 amd_comgr) diff --git a/projects/hip-tests/samples/2_Cookbook/15_static_library/host_functions/CMakeLists.txt b/projects/hip-tests/samples/2_Cookbook/15_static_library/host_functions/CMakeLists.txt index 347171ed9d..3c7c306866 100644 --- a/projects/hip-tests/samples/2_Cookbook/15_static_library/host_functions/CMakeLists.txt +++ b/projects/hip-tests/samples/2_Cookbook/15_static_library/host_functions/CMakeLists.txt @@ -12,10 +12,15 @@ list(APPEND CMAKE_PREFIX_PATH ${ROCM_PATH}/hip ${ROCM_PATH}) # Find hip find_package(hip REQUIRED) +# For windows, AR is MS Librarian and that is pickedby Visual Studio's command prompt. +if (WIN32) + find_program(libpath NAMES lib.exe) + set (CMAKE_AR ${libpath}) +endif() + # Set compiler and linker set(CMAKE_CXX_COMPILER ${HIP_HIPCC_EXECUTABLE}) set(CMAKE_CXX_LINKER ${HIP_HIPCC_EXECUTABLE}) -set(CMAKE_AR ${HIP_HIPCC_EXECUTABLE}) set(CMAKE_BUILD_TYPE Release) # Turn static library generation ON @@ -23,15 +28,17 @@ option(BUILD_SHARED_LIBS "Build as a shared library" OFF) set(CPP_SOURCES hipOptLibrary.cpp) +# For windows, We need to tell cmake how to create static library. +if (WIN32) + set (CMAKE_CXX_CREATE_STATIC_LIBRARY " /out: ") +endif() + # Generate static lib libHipOptLibrary.a. add_library(HipOptLibrary STATIC ${CPP_SOURCES}) # Set-up the correct flags to generate the static library. target_link_libraries(HipOptLibrary PRIVATE --emit-static-lib) target_include_directories(HipOptLibrary PRIVATE /opt/rocm/hsa/include) -get_property(link_libraries TARGET HipOptLibrary PROPERTY LINK_LIBRARIES) -string (REPLACE ";" " " LINK_PROPS "${link_libraries}") -set(CMAKE_CXX_ARCHIVE_CREATE " -o ${LINK_PROPS} ") # Create test executable that uses libHipOptLibrary.a set(TEST_SOURCES ${CMAKE_SOURCE_DIR}/hipMain1.cpp) @@ -39,5 +46,10 @@ set(TEST_SOURCES ${CMAKE_SOURCE_DIR}/hipMain1.cpp) add_executable(test_opt_static ${TEST_SOURCES}) add_dependencies(test_opt_static HipOptLibrary) target_link_libraries(test_opt_static PRIVATE -lHipOptLibrary -L${CMAKE_BINARY_DIR}) -target_link_libraries(test_opt_static PRIVATE amdhip64 amd_comgr hsa-runtime64::hsa-runtime64) + +if (WIN32) + target_link_libraries(test_opt_static PRIVATE amdhip64 amd_comgr) +else() + target_link_libraries(test_opt_static PRIVATE amdhip64 amd_comgr hsa-runtime64::hsa-runtime64) +endif()