Skip to content

Commit

Permalink
Fix Python pathing and initialization in the shell.
Browse files Browse the repository at this point in the history
Signed-off-by: fruffy <[email protected]>
  • Loading branch information
fruffy committed Jan 27, 2025
1 parent 0369a33 commit ceb6e72
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 19 deletions.
6 changes: 6 additions & 0 deletions cmake/PythonDependencies.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion p4studio/dependencies/dependencies.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions pkgsrc/bf-drivers/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions pkgsrc/bf-drivers/src/bf_rt/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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_OBJECTS:bfshell_plugin_bf_rt_o>)
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()
Expand Down Expand Up @@ -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
Expand Down
37 changes: 25 additions & 12 deletions pkgsrc/bf-drivers/src/bf_rt/cli/bf_rt_cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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");
Expand Down
2 changes: 1 addition & 1 deletion pkgsrc/bf-drivers/src/lld/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ add_library(lld SHARED EXCLUDE_FROM_ALL $<TARGET_OBJECTS:lld_o>)
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_OBJECTS:bfshell_plugin_debug_o>)
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()
Expand Down
2 changes: 1 addition & 1 deletion pkgsrc/bf-drivers/src/perf/python_cli/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit ceb6e72

Please sign in to comment.