SWDEV-334508 - CMake create static lib & linker commands for windows. (#2841)

Change-Id: I544837a65e47d425a946c666d0b2348aa7a33b15
This commit is contained in:
ROCm CI Service Account
2022-08-05 15:13:19 +05:30
committed by GitHub
parent 99e3b14ea4
commit 908988924a
3 changed files with 40 additions and 7 deletions
@@ -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 "<CMAKE_AR> /out:<TARGET> <LINK_FLAGS> <OBJECTS>")
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 "<CMAKE_AR> -o <TARGET> ${LINK_PROPS} <LINK_FLAGS> <OBJECTS>")
# 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()