From 405b60af73120976cbc00554cead3f14daaebb54 Mon Sep 17 00:00:00 2001 From: Chris Freehill Date: Thu, 18 Jul 2024 09:49:37 -0500 Subject: [PATCH] Use static drm and drm_amdgpu for static builds This commit was ported from old repo. Original author: Ranjith Ramakrishnan For static builds use drm and drm_amdgpu static libraries for linking. Created a separate cmake target file and static library for static use case The config file will include the respective target file based on BUILD_SHARED_LIBS. Default target file will be the one where drm and libdrm_amdgpu shaed libraries are linked. Applications using statically linked cmake targets of hsakmt should install the required static libraries before building. Change-Id: Idf4e1a2b5f18b344f5a9927803756d50c2b33702 [ROCm/ROCR-Runtime commit: 9e8477e1c96aa6470d4b85e7bef3faf58e50beeb] --- .../rocr-runtime/libhsakmt/CMakeLists.txt | 43 ++++++++++++++++++- .../libhsakmt/hsakmt-config.cmake.in | 10 ++++- 2 files changed, 50 insertions(+), 3 deletions(-) diff --git a/projects/rocr-runtime/libhsakmt/CMakeLists.txt b/projects/rocr-runtime/libhsakmt/CMakeLists.txt index 4db94f1875..7d71d514b8 100644 --- a/projects/rocr-runtime/libhsakmt/CMakeLists.txt +++ b/projects/rocr-runtime/libhsakmt/CMakeLists.txt @@ -29,6 +29,7 @@ set ( HSAKMT "hsakmt" ) set ( HSAKMT_PACKAGE "hsakmt-roct" ) set ( HSAKMT_COMPONENT "lib${HSAKMT}" ) set ( HSAKMT_TARGET "${HSAKMT}" ) +set(HSAKMT_STATIC_DRM_TARGET "${HSAKMT_TARGET}-staticdrm") project ( ${HSAKMT_TARGET} VERSION 1.9.0) @@ -170,8 +171,8 @@ list (PREPEND CMAKE_PREFIX_PATH "${DRM_DIR}") # The module name passed to pkg_check_modules() is determined by the # name of file *.pc -pkg_check_modules(DRM REQUIRED libdrm) -pkg_check_modules(DRM_AMDGPU REQUIRED libdrm_amdgpu) +pkg_check_modules(DRM REQUIRED IMPORTED_TARGET libdrm) +pkg_check_modules(DRM_AMDGPU REQUIRED IMPORTED_TARGET libdrm_amdgpu) include_directories(${DRM_AMDGPU_INCLUDE_DIRS}) include_directories(${DRM_INCLUDE_DIRS}) @@ -277,6 +278,44 @@ configure_file ( libhsakmt.pc.in libhsakmt.pc @ONLY ) install ( FILES ${CMAKE_CURRENT_BINARY_DIR}/libhsakmt.pc DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig COMPONENT dev) +## Create separate target file for static builds +## In static builds, libdrm and libdrm_amdgpu need to be linked statically +add_library ( ${HSAKMT_STATIC_DRM_TARGET} STATIC ${HSAKMT_SRC}) + +target_include_directories( ${HSAKMT_STATIC_DRM_TARGET} + PUBLIC + $ + $ + PRIVATE + ${CMAKE_CURRENT_SOURCE_DIR}/src ) + +## Set the VERSION and SOVERSION values +set_property(TARGET ${HSAKMT_STATIC_DRM_TARGET} PROPERTY LINK_FLAGS ${HSAKMT_LINK_FLAGS} + PROPERTY VERSION "${LIB_VERSION_STRING}" + PROPERTY SOVERSION "${LIB_VERSION_MAJOR}" ) + +#Additional search path for static libraries +if(${DISTRO_ID} MATCHES "ubuntu") + set(AMDGPU_STATIC_LIB_PATHS "-L/opt/amdgpu/lib/x86_64-linux-gnu") +else() + set(AMDGPU_STATIC_LIB_PATHS "-L/opt/amdgpu/lib64" "-L/opt/amdgpu/lib") +endif() +# Link drm_amdgpu and drm library statically +target_link_libraries ( ${HSAKMT_STATIC_DRM_TARGET} + PRIVATE pthread rt c numa ${CMAKE_DL_LIBS} + INTERFACE -Wl,-Bstatic ${AMDGPU_STATIC_LIB_PATHS} ${DRM_AMDGPU_LDFLAGS} ${DRM_LDFLAGS} -Wl,-Bdynamic +) +target_compile_options(${HSAKMT_STATIC_DRM_TARGET} PRIVATE ${DRM_CFLAGS} ${HSAKMT_C_FLAGS}) + +install ( TARGETS ${HSAKMT_STATIC_DRM_TARGET} EXPORT ${HSAKMT_STATIC_DRM_TARGET}Targets + ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT devel + LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT devel) +install ( EXPORT ${HSAKMT_STATIC_DRM_TARGET}Targets + FILE ${HSAKMT_STATIC_DRM_TARGET}Targets.cmake + NAMESPACE ${HSAKMT_STATIC_DRM_TARGET}:: + DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${HSAKMT_TARGET} + COMPONENT devel) + ########################### # Packaging directives ########################### diff --git a/projects/rocr-runtime/libhsakmt/hsakmt-config.cmake.in b/projects/rocr-runtime/libhsakmt/hsakmt-config.cmake.in index 9b162dae71..da7aaac912 100644 --- a/projects/rocr-runtime/libhsakmt/hsakmt-config.cmake.in +++ b/projects/rocr-runtime/libhsakmt/hsakmt-config.cmake.in @@ -10,4 +10,12 @@ include( CMakeFindDependencyMacro ) # find_dependencies as shown below. #find_dependency(Bar, 2.0) -include( "${CMAKE_CURRENT_LIST_DIR}/@HSAKMT_TARGET@Targets.cmake" ) +# If the option is ON link other dependent libraries dynamically +# If the option is OFF, then link libdrm and libdrm_amdgpu statically +option(BUILD_SHARED_LIBS "Build using shared libraries" ON) + +if(BUILD_SHARED_LIBS) + include( "${CMAKE_CURRENT_LIST_DIR}/@HSAKMT_TARGET@Targets.cmake" ) +else() + include( "${CMAKE_CURRENT_LIST_DIR}/@HSAKMT_STATIC_DRM_TARGET@Targets.cmake" ) +endif() \ No newline at end of file