From ceb6e729b263915b4b6caaec101fb02d02659ff3 Mon Sep 17 00:00:00 2001 From: fruffy Date: Sun, 26 Jan 2025 19:20:44 -0500 Subject: [PATCH] Fix Python pathing and initialization in the shell. Signed-off-by: fruffy --- cmake/PythonDependencies.cmake | 6 +++ p4studio/dependencies/dependencies.yaml | 2 +- pkgsrc/bf-drivers/CMakeLists.txt | 4 +- pkgsrc/bf-drivers/src/bf_rt/CMakeLists.txt | 4 +- pkgsrc/bf-drivers/src/bf_rt/cli/bf_rt_cli.c | 37 +++++++++++++------ pkgsrc/bf-drivers/src/lld/CMakeLists.txt | 2 +- .../src/perf/python_cli/CMakeLists.txt | 2 +- 7 files changed, 38 insertions(+), 19 deletions(-) diff --git a/cmake/PythonDependencies.cmake b/cmake/PythonDependencies.cmake index 10639ae36..c8c1eefb9 100644 --- a/cmake/PythonDependencies.cmake +++ b/cmake/PythonDependencies.cmake @@ -10,7 +10,13 @@ if(Python3_FOUND) message(STATUS "Python3 found: ${Python3_EXECUTABLE}") message(STATUS "Python3 version: ${Python3_VERSION}") message(STATUS "Python3 include dir: ${Python3_INCLUDE_DIRS}") + message(STATUS "Python3 libraries: ${Python3_LIBRARIES}") set(PYTHON_EXECUTABLE ${Python3_EXECUTABLE}) + # Split version into major, minor, and micro. + string(REPLACE "." ";" version_list ${Python3_VERSION}) + list(GET version_list 0 Python3_VERSION_MAJOR) + list(GET version_list 1 Python3_VERSION_MINOR) + list(GET version_list 2 Python3_VERSION_MICRO) else() message(FATAL_ERROR "Python3 not found. Please install Python3 development files.") endif() diff --git a/p4studio/dependencies/dependencies.yaml b/p4studio/dependencies/dependencies.yaml index 4427b8340..9c28cfbd7 100755 --- a/p4studio/dependencies/dependencies.yaml +++ b/p4studio/dependencies/dependencies.yaml @@ -318,7 +318,7 @@ packages: type: pip Pygments==2.12.0: type: pip - traitlets==5.2.2: + traitlets==5.14.3: type: pip wcwidth==0.2.5: type: pip diff --git a/pkgsrc/bf-drivers/CMakeLists.txt b/pkgsrc/bf-drivers/CMakeLists.txt index ed3cbcadf..790a86c47 100644 --- a/pkgsrc/bf-drivers/CMakeLists.txt +++ b/pkgsrc/bf-drivers/CMakeLists.txt @@ -127,8 +127,8 @@ if (THRIFT-DRIVER) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DTHRIFT_ENABLED") endif() -set(BF_PYTHON_VER 3.10) -set(TDI_PYTHON_VER 3.10) +set(BF_PYTHON_VER "${Python3_VERSION_MAJOR}.${Python3_VERSION_MINOR}") +set(TDI_PYTHON_VER ${BF_PYTHON_VER}) add_subdirectory(third-party) add_subdirectory(src) diff --git a/pkgsrc/bf-drivers/src/bf_rt/CMakeLists.txt b/pkgsrc/bf-drivers/src/bf_rt/CMakeLists.txt index 0e009e1c1..0dd74115a 100644 --- a/pkgsrc/bf-drivers/src/bf_rt/CMakeLists.txt +++ b/pkgsrc/bf-drivers/src/bf_rt/CMakeLists.txt @@ -79,8 +79,8 @@ c_frontend/bf_rt_info_c.cpp if(BF-PYTHON) add_library(bfshell_plugin_bf_rt_o OBJECT cli/bf_rt_cli.c) target_include_directories(bfshell_plugin_bf_rt_o PUBLIC ${Python3_INCLUDE_DIRS}) - target_link_libraries(bfshell_plugin_bf_rt_o ${Python3_LIBRARIES}) add_library(bfshell_plugin_bf_rt SHARED $) + target_link_libraries(bfshell_plugin_bf_rt ${Python3_LIBRARIES}) SET_TARGET_PROPERTIES(bfshell_plugin_bf_rt PROPERTIES PREFIX "") install(FILES xml/bf_rt.xml DESTINATION share/cli/xml) endif() @@ -236,7 +236,7 @@ else() endif() # Install bfrt_python files to python install directory -install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/bf_rt_python/ DESTINATION lib/python${BF_PYTHON_VER}) +install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/bf_rt_python/ DESTINATION lib/python${BF_PYTHON_VER}/site-packages) #Install bfruntime python client files install(FILES diff --git a/pkgsrc/bf-drivers/src/bf_rt/cli/bf_rt_cli.c b/pkgsrc/bf-drivers/src/bf_rt/cli/bf_rt_cli.c index 01a8e692d..c39d632db 100644 --- a/pkgsrc/bf-drivers/src/bf_rt/cli/bf_rt_cli.c +++ b/pkgsrc/bf-drivers/src/bf_rt/cli/bf_rt_cli.c @@ -49,12 +49,14 @@ static int bf_rt_start_cli(int in_fd, bf_dev_id_t *dev_id_list = NULL; int ret_val = 0; + // first run, initialize python interpreter. + // Use the system-default config and add site-packages modules. if (!Py_IsInitialized()) { PyConfig config; - wchar_t cfg_home_path[256]; PyConfig_InitPythonConfig(&config); - swprintf(cfg_home_path, 256, L"%s", install_dir); - config.home = cfg_home_path; + wchar_t py_path[256]; + swprintf(py_path, 256, L"%slib/python%d.%d/site-packages", install_dir, PY_MAJOR_VERSION, PY_MINOR_VERSION); + config.pythonpath_env = py_path; Py_InitializeFromConfig(&config); } @@ -64,22 +66,33 @@ static int bf_rt_start_cli(int in_fd, bf_rt_device_id_list_get(dev_id_list); } - // first run, initialize python interpreter if (bfrtpModule == NULL) { - PyObject *pName; - /* Load the bfrtcli python program. Py_Initialize loads its libraries from - the install dir we installed Python into. */ - pName = PyUnicode_DecodeFSDefault("bfrtcli"); - /* Error checking of pName left out */ - bfrtpModule = PyImport_Import(pName); - Py_DECREF(pName); + PyObject *pName; + pName = PyUnicode_DecodeFSDefault("bfrtcli"); + if (pName == NULL) { + fprintf(stderr, "Failed to decode module name 'bfrtcli' as a Python string.\n"); + ret_val = 1; + goto cleanup; + } + + bfrtpModule = PyImport_Import(pName); + Py_DECREF(pName); + if (bfrtpModule == NULL) { - printf("cannot import module in bfrt\n"); + fprintf(stderr, "Failed to import module 'bfrtcli'.\n"); + + if (PyErr_Occurred()) { + PyErr_Print(); + } else { + fprintf(stderr, "No additional error information from Python.\n"); + } + ret_val = 1; goto cleanup; } } + if (bfrtpModule != NULL) { // Create a call to the start_bfrt function in bfrtcli.py pFunc = PyObject_GetAttrString(bfrtpModule, "start_bfrt"); diff --git a/pkgsrc/bf-drivers/src/lld/CMakeLists.txt b/pkgsrc/bf-drivers/src/lld/CMakeLists.txt index c934a3471..0036b8d56 100644 --- a/pkgsrc/bf-drivers/src/lld/CMakeLists.txt +++ b/pkgsrc/bf-drivers/src/lld/CMakeLists.txt @@ -83,8 +83,8 @@ add_library(lld SHARED EXCLUDE_FROM_ALL $) if(BF-PYTHON) add_library(bfshell_plugin_debug_o OBJECT cli/debug_cli.c) target_include_directories(bfshell_plugin_debug_o PUBLIC ${Python3_INCLUDE_DIRS}) - target_link_libraries(bfshell_plugin_debug_o ${Python3_LIBRARIES}) add_library(bfshell_plugin_debug SHARED $) + target_link_libraries(bfshell_plugin_debug ${Python3_LIBRARIES}) SET_TARGET_PROPERTIES(bfshell_plugin_debug PROPERTIES PREFIX "") install(FILES cli/xml/debug.xml DESTINATION share/cli/xml) endif() diff --git a/pkgsrc/bf-drivers/src/perf/python_cli/CMakeLists.txt b/pkgsrc/bf-drivers/src/perf/python_cli/CMakeLists.txt index ba42de9a4..b0883be62 100644 --- a/pkgsrc/bf-drivers/src/perf/python_cli/CMakeLists.txt +++ b/pkgsrc/bf-drivers/src/perf/python_cli/CMakeLists.txt @@ -1 +1 @@ -install(FILES perfCli.py perfHelpers.py perfTest.py DESTINATION lib/python${BF_PYTHON_VER}) +install(FILES perfCli.py perfHelpers.py perfTest.py DESTINATION lib/python${BF_PYTHON_VER}/site-packages)