forked from ROCm/amdsmi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CMakeLists.txt
333 lines (284 loc) · 13.1 KB
/
CMakeLists.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
#
# Minimum version of cmake required
#
cmake_minimum_required(VERSION 3.20)
set(AMD_SMI "amd_smi")
set(AMD_SMI_COMPONENT "lib${AMD_SMI}")
set(AMD_SMI_TARGET "${AMD_SMI}")
set(AMD_SMI_LIB_NAME "lib${AMD_SMI_TARGET}")
set(AMD_SMI_LIBS_TARGET "amd_smi_libraries")
set(BUILD_SHARED_LIBS
ON
CACHE BOOL "Build shared library (.so) or not.")
set(CMAKE_MODULE_PATH
"${CMAKE_CURRENT_SOURCE_DIR}/cmake_modules/"
CACHE INTERNAL "Default module path.")
## Include common cmake modules
include(utils)
include(help_package)
find_package(PkgConfig)
generic_add_rocm()
# provide git to utilities
find_program(GIT NAMES git)
## Setup the package version based on git tags.
set(PKG_VERSION_GIT_TAG_PREFIX "amdsmi_pkg_ver")
get_package_version_number("24.7.0" ${PKG_VERSION_GIT_TAG_PREFIX} GIT)
message("Package version: ${PKG_VERSION_STR}")
set(${AMD_SMI_LIBS_TARGET}_VERSION_MAJOR "${CPACK_PACKAGE_VERSION_MAJOR}")
set(${AMD_SMI_LIBS_TARGET}_VERSION_MINOR "${CPACK_PACKAGE_VERSION_MINOR}")
set(${AMD_SMI_LIBS_TARGET}_VERSION_PATCH "${CPACK_PACKAGE_VERSION_PATCH}")
set(${AMD_SMI_LIBS_TARGET}_VERSION_BUILD "0")
set(${AMD_SMI_LIBS_TARGET}_VERSION_HASH "${PKG_VERSION_HASH}")
set(${AMD_SMI_LIBS_TARGET}_VERSION_STRING "${${AMD_SMI_LIBS_TARGET}_VERSION_MAJOR}.${${AMD_SMI_LIBS_TARGET}_VERSION_MINOR}.${${AMD_SMI_LIBS_TARGET}_VERSION_PATCH}+${${AMD_SMI_LIBS_TARGET}_VERSION_HASH}")
# Make proper version for appending
# Default Value is 99999
set(ROCM_VERSION_FOR_PACKAGE "99999")
if(DEFINED ENV{ROCM_LIBPATCH_VERSION})
set(ROCM_VERSION_FOR_PACKAGE $ENV{ROCM_LIBPATCH_VERSION})
endif()
#Prepare final version for the CPACK use
set(CPACK_PACKAGE_VERSION
"${CPACK_PACKAGE_VERSION_MAJOR}.${CPACK_PACKAGE_VERSION_MINOR}.${CPACK_PACKAGE_VERSION_PATCH}.${ROCM_VERSION_FOR_PACKAGE}")
# The following default version values should be updated as appropriate for
# ABI breaks (update MAJOR and MINOR), and ABI/API additions (update MINOR).
# Until ABI stabilizes VERSION_MAJOR will be 0. This should be over-ridden
# by git tags (through "git describe") when they are present.
set(PKG_VERSION_MAJOR "${CPACK_PACKAGE_VERSION_MAJOR}")
set(PKG_VERSION_MINOR "${CPACK_PACKAGE_VERSION_MINOR}")
set(PKG_VERSION_PATCH "${CPACK_PACKAGE_VERSION_PATCH}")
set(PKG_VERSION_NUM_COMMIT 0)
set(AMD_SMI_PACKAGE
amd-smi-lib
CACHE STRING "")
project(${AMD_SMI_LIBS_TARGET})
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
include(GNUInstallDirs)
option(BUILD_TESTS "Build test suite" OFF)
option(ENABLE_ASAN_PACKAGING "" OFF)
option(ENABLE_ESMI_LIB "Build ESMI Library" ON)
include(CMakeDependentOption)
# these options don't work without BUILD_SHARED_LIBS
cmake_dependent_option(BUILD_WRAPPER "Rebuild AMDSMI-wrapper" OFF "BUILD_SHARED_LIBS" OFF)
cmake_dependent_option(BUILD_CLI "Build AMDSMI-CLI and install" ON "BUILD_SHARED_LIBS" OFF)
cmake_dependent_option(ENABLE_LDCONFIG "Set library links and caches using ldconfig." ON "BUILD_SHARED_LIBS" OFF)
# Set share path here because project name != amd_smi
set(SHARE_INSTALL_PREFIX "share/${AMD_SMI}" CACHE STRING "Tests and Example install directory")
# Packaging directives
set(CPACK_PACKAGE_NAME
${AMD_SMI_PACKAGE}
CACHE STRING "")
set(CPACK_PACKAGE_CONTACT
"AMD-SMILib Support <amd-smi.support@amd.com>"
CACHE STRING "")
set(CPACK_PACKAGE_DESCRIPTION_SUMMARY
"AMD System Management libraries"
CACHE STRING "")
generic_package()
## Compiler flags
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -fno-rtti")
if (${CMAKE_HOST_SYSTEM_PROCESSOR} STREQUAL "x86_64")
set(CMAKE_CXX_FLAGS
"${CMAKE_CXX_FLAGS} -m64 -msse -msse2")
endif()
# Security options
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wconversion -Wcast-align")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wformat=2 -fno-common -Wstrict-overflow")
# Intentionally leave out -Wsign-promo. It causes spurious warnings.
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Woverloaded-virtual -Wreorder")
set(COMMON_SRC_DIR "${PROJECT_SOURCE_DIR}/src")
set(ROCM_SRC_DIR "${PROJECT_SOURCE_DIR}/rocm_smi/src")
set(AMDSMI_SRC_DIR "${PROJECT_SOURCE_DIR}/src/amd_smi")
set(COMMON_INC_DIR "${PROJECT_SOURCE_DIR}/include/amd_smi")
set(AMDSMI_INC_DIR "${PROJECT_SOURCE_DIR}/include/amd_smi")
set(ROCM_INC_DIR "${PROJECT_SOURCE_DIR}/rocm_smi/include/rocm_smi")
set(SHR_MUTEX_DIR "${PROJECT_SOURCE_DIR}/third_party/shared_mutex")
if(ENABLE_ESMI_LIB)
if(NOT EXISTS ${PROJECT_SOURCE_DIR}/esmi_ib_library/src)
# TODO: use ExternalProject_Add instead or a submodule
# as of 2023.10.16 CI builds are broken with an updated submodule
execute_process(COMMAND git clone --depth=1 -b esmi_pkg_ver-3.0.3 https://github.com/amd/esmi_ib_library.git ${PROJECT_SOURCE_DIR}/esmi_ib_library)
endif()
if(NOT EXISTS ${PROJECT_SOURCE_DIR}/esmi_ib_library/include/asm/amd_hsmp.h)
file(DOWNLOAD
https://git.kernel.org/pub/scm/linux/kernel/git/pdx86/platform-drivers-x86.git/plain/arch/x86/include/uapi/asm/amd_hsmp.h?h=review-ilpo
${PROJECT_SOURCE_DIR}/esmi_ib_library/include/asm/amd_hsmp.h)
endif()
add_definitions("-DENABLE_ESMI_LIB=1")
set(ESMI_INC_DIR "${PROJECT_SOURCE_DIR}/esmi_ib_library/include")
set(ESMI_SRC_DIR "${PROJECT_SOURCE_DIR}/esmi_ib_library/src")
# esmi has a lot of write-strings warnings - silence them
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-write-strings")
endif()
pkg_check_modules(DRM REQUIRED libdrm)
pkg_check_modules(AMDGPU_DRM REQUIRED libdrm_amdgpu)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include ${CMAKE_CURRENT_SOURCE_DIR}/third_party/shared_mutex
${CMAKE_CURRENT_SOURCE_DIR}/include/amd_smi ${ESMI_INC_DIR} ${DRM_INCLUDE_DIRS})
set(CMN_SRC_LIST
"${ROCM_SRC_DIR}/rocm_smi.cc"
"${ROCM_SRC_DIR}/rocm_smi_counters.cc"
"${ROCM_SRC_DIR}/rocm_smi_device.cc"
"${ROCM_SRC_DIR}/rocm_smi_gpu_metrics.cc"
"${ROCM_SRC_DIR}/rocm_smi_binary_parser.cc"
"${ROCM_SRC_DIR}/rocm_smi_io_link.cc"
"${ROCM_SRC_DIR}/rocm_smi_kfd.cc"
"${ROCM_SRC_DIR}/rocm_smi_main.cc"
"${ROCM_SRC_DIR}/rocm_smi_monitor.cc"
"${ROCM_SRC_DIR}/rocm_smi_power_mon.cc"
"${ROCM_SRC_DIR}/rocm_smi_utils.cc"
"${ROCM_SRC_DIR}/rocm_smi_logger.cc"
"${SHR_MUTEX_DIR}/shared_mutex.cc")
if(ENABLE_ESMI_LIB)
list(APPEND CMN_SRC_LIST ${ESMI_SRC_DIR}/e_smi.c)
list(APPEND CMN_SRC_LIST ${ESMI_SRC_DIR}/e_smi_monitor.c)
list(APPEND CMN_SRC_LIST ${ESMI_SRC_DIR}/e_smi_plat.c)
list(APPEND CMN_SRC_LIST ${ESMI_SRC_DIR}/e_smi_utils.c)
endif()
set(CMN_INC_LIST
"${ROCM_INC_DIR}/rocm_smi.h"
"${ROCM_INC_DIR}/rocm_smi_common.h"
"${ROCM_INC_DIR}/rocm_smi_counters.h"
"${ROCM_INC_DIR}/rocm_smi_device.h"
"${ROCM_INC_DIR}/rocm_smi_gpu_metrics.h"
"${ROCM_INC_DIR}/rocm_smi_binary_parser.h"
"${ROCM_INC_DIR}/rocm_smi_exception.h"
"${ROCM_INC_DIR}/rocm_smi_io_link.h"
"${ROCM_INC_DIR}/rocm_smi_kfd.h"
"${ROCM_INC_DIR}/rocm_smi_main.h"
"${ROCM_INC_DIR}/rocm_smi_monitor.h"
"${ROCM_INC_DIR}/rocm_smi_power_mon.h"
"${ROCM_INC_DIR}/rocm_smi_utils.h"
"${ROCM_INC_DIR}/rocm_smi_logger.h"
"${SHR_MUTEX_DIR}/shared_mutex.h")
add_subdirectory("rocm_smi")
add_subdirectory("src")
add_subdirectory("example")
if(BUILD_TESTS)
set(TESTS_COMPONENT "tests")
#add_subdirectory("tests/rocm_smi_test")
add_subdirectory("tests/amd_smi_test")
add_subdirectory("tests/python_unittest")
endif()
# python interface, CLI, and py-test depend on shared libraries
if(BUILD_SHARED_LIBS)
add_subdirectory("py-interface")
if(BUILD_CLI)
add_subdirectory("amdsmi_cli")
endif()
endif()
include(CMakePackageConfigHelpers)
configure_package_config_file(
amd_smi-config.cmake.in ${CMAKE_CURRENT_BINARY_DIR}/amd_smi-config.cmake
INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${AMD_SMI}
PATH_VARS CMAKE_INSTALL_LIBDIR CMAKE_INSTALL_INCLUDEDIR CMAKE_INSTALL_BINDIR)
write_basic_package_version_file(
${CMAKE_CURRENT_BINARY_DIR}/amd_smi-config-version.cmake
VERSION
"${CPACK_PACKAGE_VERSION}"
COMPATIBILITY SameMajorVersion)
install(
FILES ${CMAKE_CURRENT_BINARY_DIR}/amd_smi-config.cmake ${CMAKE_CURRENT_BINARY_DIR}/amd_smi-config-version.cmake
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${AMD_SMI}
COMPONENT dev)
# Create cmake target
# Add all targets to the build-tree export set
export(TARGETS ${AMD_SMI_TARGET} FILE "${PROJECT_BINARY_DIR}/amd_smi_target.cmake")
# Export the package for use from the build-tree
# (this registers the build-tree with a global CMake-registry)
export(PACKAGE amd_smi)
# Create the amd_smiConfig.cmake and amd_smiConfigVersion files
# ... for the build tree
install(
EXPORT amd_smiTargets
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${AMD_SMI}
COMPONENT dev)
#License file
set(CPACK_RPM_PACKAGE_LICENSE "MIT")
# install license file in share/doc/amd_smi-asan folder
install(
FILES ${CPACK_RESOURCE_FILE_LICENSE}
DESTINATION share/doc/${AMD_SMI}-asan
RENAME LICENSE.txt
COMPONENT asan)
# docs are installed into different share directory from tests and examples
install(
FILES ${CPACK_RESOURCE_FILE_LICENSE}
DESTINATION share/doc/${AMD_SMI}
RENAME LICENSE.txt
COMPONENT dev)
#Readme file
install(
FILES ${CMAKE_CURRENT_SOURCE_DIR}/README.md
DESTINATION share/doc/${AMD_SMI}
COMPONENT dev)
# Make for goamdsmi_shim library
add_subdirectory(goamdsmi_shim)
#Debian package specific variables
set(CPACK_DEBIAN_PACKAGE_RECOMMENDS "python3-argcomplete, libdrm-dev, python3-PyYAML")
set(CPACK_DEBIAN_ASAN_PACKAGE_RECOMMENDS ${CPACK_DEBIAN_PACKAGE_RECOMMENDS})
set(CPACK_DEBIAN_DEV_PACKAGE_RECOMMENDS ${CPACK_DEBIAN_PACKAGE_RECOMMENDS})
set(CPACK_DEBIAN_PACKAGE_DEPENDS "sudo, python3 (>= 3.6.8), python3-pip")
set(CPACK_DEBIAN_ASAN_PACKAGE_DEPENDS ${CPACK_DEBIAN_PACKAGE_DEPENDS})
set(CPACK_DEBIAN_DEV_PACKAGE_DEPENDS ${CPACK_DEBIAN_PACKAGE_DEPENDS})
## Process the Debian install/remove scripts to update the CPACK variables
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/DEBIAN/postinst.in DEBIAN/postinst @ONLY)
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/DEBIAN/prerm.in DEBIAN/prerm @ONLY)
set(CPACK_DEBIAN_PACKAGE_CONTROL_EXTRA "DEBIAN/postinst;DEBIAN/prerm")
# RPM package specific variables
set(CPACK_RPM_EXCLUDE_FROM_AUTO_FILELIST_ADDITION
"${CPACK_PACKAGING_INSTALL_PREFIX} ${CPACK_PACKAGING_INSTALL_PREFIX}/${CMAKE_INSTALL_BINDIR}")
#Set rpm distro
if(CPACK_RPM_PACKAGE_RELEASE)
set(CPACK_RPM_PACKAGE_RELEASE_DIST ON)
endif()
# NOTE: RPM SUGGESTS DO NOT WORK! https://bugzilla.redhat.com/show_bug.cgi?id=1811358
set(CPACK_RPM_PACKAGE_SUGGESTS "python3-argcomplete")
set(CPACK_RPM_DEV_PACKAGE_SUGGESTS ${CPACK_RPM_PACKAGE_SUGGESTS})
set(CPACK_RPM_ASAN_PACKAGE_SUGGESTS ${CPACK_RPM_PACKAGE_SUGGESTS})
# python version gated by rhel8 :(
set(CPACK_RPM_PACKAGE_REQUIRES "sudo, python3 >= 3.6.8, python3-pip, python3-PyYAML")
set(CPACK_RPM_DEV_PACKAGE_REQUIRES ${CPACK_RPM_PACKAGE_REQUIRES})
set(CPACK_RPM_ASAN_PACKAGE_REQUIRES ${CPACK_RPM_PACKAGE_REQUIRES})
# don't terminate if bytecompile of python files fails
set(CPACK_RPM_SPEC_MORE_DEFINE "%define _python_bytecompile_errors_terminate_build 0")
# Cpack converts !/usr/bin/env python3 to /usr/libexec/platform-python in RHEL8.
# prevent the BRP(buildroot policy) script from checking and modifying interpreter directives
string( APPEND CPACK_RPM_SPEC_MORE_DEFINE "\n%undefine __brp_mangle_shebangs")
# Add rocm-core dependency if -DROCM_DEP_ROCMCORE=ON is passed
if(ROCM_DEP_ROCMCORE)
string(APPEND CPACK_DEBIAN_ASAN_PACKAGE_DEPENDS ", rocm-core-asan")
string(APPEND CPACK_RPM_ASAN_PACKAGE_REQUIRES ", rocm-core-asan")
string(APPEND CPACK_DEBIAN_DEV_PACKAGE_DEPENDS ", rocm-core")
string(APPEND CPACK_RPM_DEV_PACKAGE_REQUIRES ", rocm-core")
string(APPEND CPACK_DEBIAN_PACKAGE_DEPENDS ", rocm-core")
string(APPEND CPACK_RPM_PACKAGE_REQUIRES ", rocm-core")
endif()
## Enable Component Mode and set component specific flags
set(CPACK_DEB_COMPONENT_INSTALL ON)
set(CPACK_DEBIAN_DEV_PACKAGE_NAME "${AMD_SMI_PACKAGE}")
set(CPACK_DEBIAN_ASAN_PACKAGE_NAME "${AMD_SMI_PACKAGE}-asan")
set(CPACK_RPM_COMPONENT_INSTALL ON)
set(CPACK_RPM_DEV_PACKAGE_NAME "${AMD_SMI_PACKAGE}")
set(CPACK_RPM_ASAN_PACKAGE_NAME "${AMD_SMI_PACKAGE}-asan")
if(ENABLE_ASAN_PACKAGING)
# ASAN Package requires only asan component with libraries and license file
set(CPACK_COMPONENTS_ALL asan)
else()
set(CPACK_COMPONENTS_ALL dev tests )
endif()
# The line below doesn't currently work; it may be this issue:
# https://bugzilla.redhat.com/show_bug.cgi?id=1811358
# set(CPACK_RPM_PACKAGE_SUGGESTS "sudo, libdrm-dev")
## Process the Rpm install/remove scripts to update the CPACK variables
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/RPM/post.in" RPM/post @ONLY)
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/RPM/preun.in" RPM/preun @ONLY)
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/RPM/postun.in" RPM/postun @ONLY)
set(CPACK_RPM_POST_INSTALL_SCRIPT_FILE "${CMAKE_CURRENT_BINARY_DIR}/RPM/post")
set(CPACK_RPM_PRE_UNINSTALL_SCRIPT_FILE "${CMAKE_CURRENT_BINARY_DIR}/RPM/preun")
set(CPACK_RPM_POST_UNINSTALL_SCRIPT_FILE "${CMAKE_CURRENT_BINARY_DIR}/RPM/postun")
#Set the names now using CPACK utility
set(CPACK_DEBIAN_FILE_NAME "DEB-DEFAULT")
set(CPACK_RPM_FILE_NAME "RPM-DEFAULT")
include(CPack)
generic_package_post()