diff --git a/README.md b/README.md index 2b5f924e98..c4568921b9 100755 --- a/README.md +++ b/README.md @@ -34,6 +34,8 @@ installed to query firmware information and hardware IPs. ``` bash apt install amd-smi-lib +# if installed with rocm ignore the export +export PATH="$PATH:/opt/rocm/bin" amd-smi --help ``` @@ -64,7 +66,6 @@ Then install Python library from your target rocm instance: ``` bash apt install amd-smi-lib -amd-smi --help cd /opt/rocm/share/amd_smi python3 -m pip install --upgrade pip python3 -m pip install --user . diff --git a/py-interface/CMakeLists.txt b/py-interface/CMakeLists.txt index 0d2160d1b1..d55e9b6fca 100644 --- a/py-interface/CMakeLists.txt +++ b/py-interface/CMakeLists.txt @@ -36,15 +36,9 @@ if(NOT GOOD_CLANG_FOUND) OUTPUT amdsmi_wrapper.py ${PY_PACKAGE_DIR}/amdsmi_wrapper.py DEPENDS ${AMD_SMI} - ${CMAKE_CURRENT_SOURCE_DIR}/amdsmi_wrapper.py.in - # remove .in suffix when copying - COMMAND cp -f ${CMAKE_CURRENT_SOURCE_DIR}/amdsmi_wrapper.py.in ${CMAKE_CURRENT_BINARY_DIR}/amdsmi_wrapper.py - # hacky alternative to configure_file that will run at MAKE compile instead of CMake configure - COMMAND sed -i - s:"@CPACK_PACKAGING_INSTALL_PREFIX@/@CMAKE_INSTALL_LIBDIR@":"${CPACK_PACKAGING_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}":g - ${CMAKE_CURRENT_BINARY_DIR}/amdsmi_wrapper.py + ${CMAKE_CURRENT_SOURCE_DIR}/amdsmi_wrapper.py COMMAND mkdir -p ${PY_PACKAGE_DIR} - COMMAND ln -Pf ${CMAKE_CURRENT_BINARY_DIR}/amdsmi_wrapper.py ${PY_PACKAGE_DIR}/) + COMMAND ln -Pf ${CMAKE_CURRENT_SOURCE_DIR}/amdsmi_wrapper.py ${PY_PACKAGE_DIR}/) else() find_package(Python3 3.6 COMPONENTS Interpreter Development REQUIRED) # --break-system-packages is needed for python 3.11 @@ -59,7 +53,7 @@ else() configure_file(${PROJECT_SOURCE_DIR}/tools/generator.py generator.py @ONLY COPYONLY) add_custom_command( OUTPUT amdsmi.h - ${CMAKE_CURRENT_SOURCE_DIR}/amdsmi_wrapper.py.in + ${CMAKE_CURRENT_SOURCE_DIR}/amdsmi_wrapper.py amdsmi_wrapper.py ${PY_PACKAGE_DIR}/amdsmi_wrapper.py DEPENDS ${AMD_SMI} @@ -67,15 +61,9 @@ else() generator.py ${PROJECT_SOURCE_DIR}/include/amd_smi/amdsmi.h COMMAND cp ${PROJECT_SOURCE_DIR}/include/amd_smi/amdsmi.h ./ - COMMAND ${Python3_EXECUTABLE} generator.py "$<$:-e -DENABLE_ESMI_LIB>" -i amdsmi.h -l ${PROJECT_BINARY_DIR}/src/libamd_smi.so -o ${CMAKE_CURRENT_SOURCE_DIR}/amdsmi_wrapper.py.in - # remove .in suffix when copying - COMMAND cp -f ${CMAKE_CURRENT_SOURCE_DIR}/amdsmi_wrapper.py.in ${CMAKE_CURRENT_BINARY_DIR}/amdsmi_wrapper.py - # hacky alternative to configure_file that will run at MAKE compile instead of CMake configure - COMMAND sed -i - s:"@CPACK_PACKAGING_INSTALL_PREFIX@/@CMAKE_INSTALL_LIBDIR@":"${CPACK_PACKAGING_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}":g - ${CMAKE_CURRENT_BINARY_DIR}/amdsmi_wrapper.py + COMMAND ${Python3_EXECUTABLE} generator.py "$<$:-e -DENABLE_ESMI_LIB>" -i amdsmi.h -l ${PROJECT_BINARY_DIR}/src/libamd_smi.so -o ${CMAKE_CURRENT_SOURCE_DIR}/amdsmi_wrapper.py COMMAND mkdir -p ${PY_PACKAGE_DIR} - COMMAND ln -Pf ${CMAKE_CURRENT_BINARY_DIR}/amdsmi_wrapper.py ${PY_PACKAGE_DIR}/) + COMMAND ln -Pf ${CMAKE_CURRENT_SOURCE_DIR}/amdsmi_wrapper.py ${PY_PACKAGE_DIR}/) endif() # populate version string @@ -98,7 +86,17 @@ add_custom_command( COMMAND ln -Pf ${CMAKE_CURRENT_SOURCE_DIR}/amdsmi_exception.py ${PY_PACKAGE_DIR}/ COMMAND ln -Pf ${CMAKE_CURRENT_SOURCE_DIR}/amdsmi_interface.py ${PY_PACKAGE_DIR}/ COMMAND ln -Pf ${CMAKE_CURRENT_SOURCE_DIR}/README.md ${PY_PACKAGE_DIR}/ - COMMAND ln -Pf ${PROJECT_SOURCE_DIR}/LICENSE ${PY_PACKAGE_DIR}/) + COMMAND ln -Pf ${PROJECT_SOURCE_DIR}/LICENSE ${PY_PACKAGE_DIR}/ + ) + +# copy libamd_smi.so to allow for a self-contained python package +add_custom_command( + OUTPUT ${PY_PACKAGE_DIR}/libamd_smi.so + DEPENDS ${PROJECT_BINARY_DIR}/src/libamd_smi.so + COMMAND cp + "${PROJECT_BINARY_DIR}/src/libamd_smi.so" + ${PY_PACKAGE_DIR}/ + ) add_custom_target( python_package ALL @@ -108,7 +106,9 @@ add_custom_target( ${PY_PACKAGE_DIR}/amdsmi_exception.py ${PY_PACKAGE_DIR}/amdsmi_interface.py ${PY_PACKAGE_DIR}/README.md - ${PY_PACKAGE_DIR}/LICENSE) + ${PY_PACKAGE_DIR}/LICENSE + ${PY_PACKAGE_DIR}/libamd_smi.so + ) install( FILES diff --git a/py-interface/amdsmi_wrapper.py.in b/py-interface/amdsmi_wrapper.py similarity index 99% rename from py-interface/amdsmi_wrapper.py.in rename to py-interface/amdsmi_wrapper.py index 27b62a7235..fafa936ecf 100644 --- a/py-interface/amdsmi_wrapper.py.in +++ b/py-interface/amdsmi_wrapper.py @@ -168,20 +168,11 @@ def char_pointer_cast(string, encoding='utf-8'): _libraries = {} from pathlib import Path -### TODO: Add lib build folder cmake variable or dynamic -libamd_smi_cpack = Path("@CPACK_PACKAGING_INSTALL_PREFIX@/@CMAKE_INSTALL_LIBDIR@/libamd_smi.so") -libamd_smi_optrocm = Path("/opt/rocm/lib/libamd_smi.so") libamd_smi_parent_dir = Path(__file__).resolve().parent / "libamd_smi.so" libamd_smi_cwd = Path.cwd() / "libamd_smi.so" try: - if libamd_smi_cpack.is_file(): - # try to find library in install directory provided by CMake - _libraries['libamd_smi.so'] = ctypes.CDLL(libamd_smi_cpack) - elif libamd_smi_optrocm.is_file(): - # try /opt/rocm/lib as a fallback - _libraries['libamd_smi.so'] = ctypes.CDLL(libamd_smi_optrocm) - elif libamd_smi_parent_dir.is_file(): + if libamd_smi_parent_dir.is_file(): # try to fall back to parent directory _libraries['libamd_smi.so'] = ctypes.CDLL(libamd_smi_parent_dir) else: diff --git a/py-interface/pyproject.toml.in b/py-interface/pyproject.toml.in index 34368333c8..45b18e4d11 100644 --- a/py-interface/pyproject.toml.in +++ b/py-interface/pyproject.toml.in @@ -25,3 +25,7 @@ dependencies = [ [tool.setuptools] packages = ["amdsmi"] + +# install libamd_smi.so +[tool.setuptools.package-data] +amdsmi = ["*.so"] diff --git a/py-interface/setup.cfg.in b/py-interface/setup.cfg.in index d266f8c231..3647feae6b 100644 --- a/py-interface/setup.cfg.in +++ b/py-interface/setup.cfg.in @@ -4,3 +4,6 @@ [metadata] name = amdsmi version = @amd_smi_libraries_VERSION_STRING@ + +[options.package_data] +amdsmi = *.so diff --git a/tools/generator.py b/tools/generator.py index 18383ee118..cc3db2f92b 100644 --- a/tools/generator.py +++ b/tools/generator.py @@ -114,19 +114,11 @@ def main(): library_path = os.path.join(os.path.dirname(__file__), library) line_to_replace = "_libraries['{}'] = ctypes.CDLL('{}')".format(library_name, library_path) new_line = f"""from pathlib import Path -libamd_smi_cpack = Path("@CPACK_PACKAGING_INSTALL_PREFIX@/@CMAKE_INSTALL_LIBDIR@/{library_name}") -libamd_smi_optrocm = Path("/opt/rocm/lib/{library_name}") libamd_smi_parent_dir = Path(__file__).resolve().parent / "{library_name}" libamd_smi_cwd = Path.cwd() / "{library_name}" try: - if libamd_smi_cpack.is_file(): - # try to find library in install directory provided by CMake - _libraries['{library_name}'] = ctypes.CDLL(libamd_smi_cpack) - elif libamd_smi_optrocm.is_file(): - # try /opt/rocm/lib as a fallback - _libraries['{library_name}'] = ctypes.CDLL(libamd_smi_optrocm) - elif libamd_smi_parent_dir.is_file(): + if libamd_smi_parent_dir.is_file(): # try to fall back to parent directory _libraries['{library_name}'] = ctypes.CDLL(libamd_smi_parent_dir) else: diff --git a/update_wrapper.sh b/update_wrapper.sh index fdbb2985c9..d6557d92c0 100755 --- a/update_wrapper.sh +++ b/update_wrapper.sh @@ -1,6 +1,6 @@ #!/usr/bin/env bash -# this program generates py-interface/amdsmi_wrapper.py.in +# this program generates py-interface/amdsmi_wrapper.py set -eu @@ -44,7 +44,8 @@ cp -r /src /tmp/src \ && rm -rf build .cache \ && cmake -B build -DBUILD_WRAPPER=ON $ENABLE_ESMI_LIB \ && make -C build -j $(nproc) \ - && cp /tmp/src/py-interface/amdsmi_wrapper.py.in /src/py-interface/amdsmi_wrapper.py.in" + && cp /tmp/src/py-interface/amdsmi_wrapper.py /src/py-interface/amdsmi_wrapper.py \ + && chown --reference /src/py-interface/CMakeLists.txt /src/py-interface/amdsmi_wrapper.py" echo -e "Generated new wrapper! -[$DIR/py-interface/amdsmi_wrapper.py.in]" +[$DIR/py-interface/amdsmi_wrapper.py]"