Skip to content

Commit

Permalink
Add py interface windows (#48)
Browse files Browse the repository at this point in the history
* feat: add py interface

* not working

* Add cmake-build folder and *.zip files to .gitignore

* Change Python directory, Set Enable BOOST macro and adjust the Boost/Python version in py_icl CMakeLists.txt

* Change general_config to build all libraries (common_tools_lib, pugixml)  static

* Set Boost root dir and include folder in CMakeLists.txt

* Statically link libc and libstdcc to python wrapper

* Add additional constructor without logging to Keithley SMU class, implement error code enums

* fix python version hardcode

* fix python version hardcode

* Add command to recursively checkout boost when folder does not exist

* Update boost to master branch to allow checkout on .githubactions

* Add include path to boost/stage/lib/python/include

* Move boost python outside the WIN32 brakets

* Add ls command in boost folder for test purposes

* uncomment boost directory check to display the folder content for test purposes

* Display Boost_ROOT for test purposes

* Display Boost_ROOT/stage/lib/python for test purposes

* Change include folder of python.hpp

* Adjust the CMakeLists file additionally

* Temporary disable pyhton interface to satisfy CI

Co-authored-by: wuhao <[email protected]>
  • Loading branch information
FlorianFrank and liuwuhaoo authored Aug 1, 2022
1 parent fa16dcf commit f46ac51
Show file tree
Hide file tree
Showing 11 changed files with 206 additions and 2 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,7 @@ cmake-build-debug
cmake-build-release
cmake-build-relwithdebinfo
.idea
.vscode
.DS_Store
.vscode
*.zip
cmake-build*
4 changes: 4 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,7 @@
[submodule "pugixml"]
path = pugixml
url = [email protected]:zeux/pugixml.git
[submodule "boost"]
path = boost
url = https://github.com/boostorg/boost.git
ignore = all
7 changes: 7 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,17 @@ endif()
# Build third party libraries and executables
add_subdirectory("${CMAKE_CURRENT_SOURCE_DIR}/pugixml")
add_subdirectory(instrument_cmd_tool)
set(BoostRoot "${CMAKE_CURRENT_SOURCE_DIR}/boost")
include_directories("${CMAKE_CURRENT_SOURCE_DIR}/boost/include")
#add_subdirectory(memristor_tester)
add_subdirectory(common_tools_lib)
add_subdirectory(playground)

# TODO temporary disable python interface to satisfy CI
if(ENABLE_PYTHON)
add_subdirectory(py_icl)
endif() # ENABLE_PYTHON

if(BUILD_SHARED_LIBS)
set(PUGIXML pugixml-shared)
else()
Expand Down
1 change: 1 addition & 0 deletions boost
Submodule boost added at f72d65
2 changes: 1 addition & 1 deletion common_tools_lib
1 change: 1 addition & 0 deletions include/devices/KEI2600.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class KEI2600 : public SMU
{
public:
explicit KEI2600(const char *ip, int timeoutInMs, PIL::Logging *logger);
explicit KEI2600(const char *ip, int timeoutInMs);

PIL_ERROR_CODE measure(UNIT unit, SMU_CHANNEL channel, double* value) override;

Expand Down
109 changes: 109 additions & 0 deletions py_icl/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
cmake_minimum_required(VERSION 3.4)

set(PROJECT_NAME "py_icl")
set(LIBRARY_NAME "lib${PROJECT_NAME}")

set(PYTHON_FOLDER "C:/Users/Administrator/AppData/Local/Programs/Python/Python310")

find_package (Python REQUIRED)
find_package(PythonLibs)

add_definitions(/DBOOST_PYTHON_STATIC_LIB)
set(PYTHON_VERSION ${Python_VERSION_MAJOR}.${Python_VERSION_MINOR})
if (${Python_VERSION_MAJOR})
message(STATUS "Using python version: ${PYTHON_VERSION}")
else()
message(FATAL_ERROR "ERROR. Python not found.")
endif()


set(BOOST_BOOTSTRAP_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../boost/tools/build)

message(${BOOST_BOOTSTRAP_DIR})
if (NOT IS_DIRECTORY ${BOOST_BOOTSTRAP_DIR})
message(FATAL_ERROR "Boost build directory not found. Run `git submodule update --init --recursive` First")
set(BOOST_CHECKOUT powershell 'git submodule update --init --recursive')
execute_process(
COMMAND ${BOOST_CHECKOUT}
WORKING_DIRECTORY ${BOOST_ROOT}
)
endif()

# TODO: relocate
set(BOOST_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../boost)
set(BOOST_LIB_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../boost/build)

if(WIN32)
set(BOOST_BUILD_BINARY "${BOOST_BOOTSTRAP_DIR}/b2.exe")
set(BOOST_BOOTSTRAP_COMMAND cmd /C ".\\bootstrap.bat gcc --with-python-version=${PYTHON_VERSION}")
set(BOOST_BUILD_COMMAND cmd /C ".\\tools\\build\\b2.exe toolset=gcc --with-python --prefix=build variant=release")
else()
# TODO: BOOST_BUILD_BINARY
set(BOOST_BOOTSTRAP_COMMAND "./bootstrap.sh --with-python-version=${PYTHON_VERSION}")
endif()

if (NOT EXISTS ${BOOST_BUILD_BINARY})
message(STATUS "Start to build boost build binary(b2) in: ${BOOST_BOOTSTRAP_DIR}")
message(STATUS ${BOOST_BOOTSTRAP_COMMAND})
execute_process(
COMMAND ${BOOST_BOOTSTRAP_COMMAND}
WORKING_DIRECTORY ${BOOST_BOOTSTRAP_DIR}
)
endif()
if (NOT IS_DIRECTORY ${BOOST_LIB_DIR} )
message(STATUS "Start to build BOOST.PYTHON")
message(STATUS ${BOOST_BUILD_COMMAND})
execute_process(
COMMAND ${BOOST_BUILD_COMMAND}
WORKING_DIRECTORY ${BOOST_ROOT}
)
endif()


if (NOT IS_DIRECTORY ${BOOST_LIB_DIR})
# message("Start to build Boost library")
# message(${BOOST_BOOTSTRAP_COMMAND})
# execute_process(
# COMMAND ${BOOST_BOOTSTRAP_COMMAND}
# WORKING_DIRECTORY ${BOOST_ROOT}
# OUTPUT_FILE bootstrap_boost.txt
# )
#
# message(${BOOST_BUILD_COMMAND})
# execute_process(
# COMMAND ${BOOST_BUILD_COMMAND}
# WORKING_DIRECTORY ${BOOST_ROOT}
# OUTPUT_FILE build_boost.txt
# )
else()
message("Boost library built.")
endif()

link_directories(${BOOST_LIB_DIR}/lib "${PYTHON_FOLDER}"
"${BOOST_ROOT}/stage/lib")
add_library(${PROJECT_NAME} SHARED main.cpp)

target_include_directories(${PROJECT_NAME} PRIVATE
"${BOOST_ROOT}"
"${BOOST_LIB_DIR}/include/boost-1_80"
"${PYTHON_INCLUDE_PATH}"
"/usr/include/python${PYTHON_VERSION}"
"${CMAKE_CURRENT_SOURCE_DIR}/../include"
"${CMAKE_CURRENT_SOURCE_DIR}/../boost"
"${CMAKE_CURRENT_SOURCE_DIR}/../common_tools_lib/ErrorHandling/include"
)


set(BOOST_PYTHON_LIB "boost_python${Python_VERSION_MAJOR}${Python_VERSION_MINOR}")
target_link_directories(${PROJECT_NAME} PUBLIC "${BOOST_ROOT}\\stage\\lib")

set(BOOST_PYTHON_LIB "boost_python${Python_VERSION_MAJOR}${Python_VERSION_MINOR}-mgw11-mt-x64-1_80")
message("BOOST PYTHON LIB ${BOOST_PYTHON_LIB}")
target_link_directories(${PROJECT_NAME} PUBLIC "${BOOST_ROOT}/stage/lib/"
"${BOOST_ROOT}/libs/python/include")

target_link_libraries(${PROJECT_NAME} PUBLIC instrument_control_lib ${BOOST_PYTHON_LIB} ${PYTHON_LIBRARY})
set_target_properties(${PROJECT_NAME} PROPERTIES SUFFIX ".pyd")
target_link_options(${PROJECT_NAME} PUBLIC -static -static-libgcc -static-libstdc++)

configure_file(py_icl_test.py py_icl_test.py COPYONLY)
11 changes: 11 additions & 0 deletions py_icl/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Build python interface

Run the following commands to build python interfaces lib

In root directory
```
instrument_control_lib$ git submodule update --init --recursive
instrument_control_lib$ cd boost
instrument_control_lib/boost$ ./bootstrap.sh --with-python-version=3.8
instrument_control_lib/boost$ .b2 --with-python
```
44 changes: 44 additions & 0 deletions py_icl/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include "boost/python.hpp"
#include "Device.h"
#include "devices/types/DCPowerSupply.h"
#include "devices/SPD1305.h"
#include "devices/KEI2600.h"
#include "ctlib/Logging.hpp"

using namespace boost::python;

BOOST_PYTHON_MODULE(libpy_icl)
{
enum_<DCPowerSupply::DC_CHANNEL>("DC_CHANNEL")
.value("CHANNEL_1", DCPowerSupply::DC_CHANNEL::CHANNEL_1)
.value("CHANNEL_2", DCPowerSupply::DC_CHANNEL::CHANNEL_2);

enum_<PIL_ERROR_CODE>("ERROR_CODE")
.value("NO_ERROR", PIL_ERROR_CODE::PIL_NO_ERROR)
.value("PIL_INVALID_ARGUMENTS", PIL_ERROR_CODE::PIL_INVALID_ARGUMENTS)
.value("ERRNO", PIL_ERROR_CODE::PIL_ERRNO)
.value("INTERFACE_CLOSED", PIL_ERROR_CODE::PIL_INTERFACE_CLOSED)
.value("INVALID_BAUDRATE", PIL_ERROR_CODE::PIL_INVALID_BAUDRATE)
.value("INSUFFICIENT_RESOURCES", PIL_ERROR_CODE::PIL_INSUFFICIENT_RESOURCES)
.value("INSUFFICIENT_PERMISSIONS", PIL_ERROR_CODE::PIL_INSUFFICIENT_PERMISSIONS)
.value("DEADLOCK_DETECTED", PIL_ERROR_CODE::PIL_DEADLOCK_DETECTED)
.value("THREAD_NOT_JOINABLE", PIL_ERROR_CODE::PIL_THREAD_NOT_JOINABLE)
.value("THREAD_NOT_FOUND", PIL_ERROR_CODE::PIL_THREAD_NOT_FOUND)
.value("ONLY_PARTIALLY_READ_WRITTEN", PIL_ERROR_CODE::PIL_ONLY_PARTIALLY_READ_WRITTEN)
.value("NO_SUCH_FILE", PIL_ERROR_CODE::PIL_NO_SUCH_FILE)
.value("UNKNOWN_ERROR", PIL_ERROR_CODE::PIL_UNKNOWN_ERROR)
.value("XML_PARSING_ERROR", PIL_ERROR_CODE::PIL_XML_PARSING_ERROR);



class_<SPD1305>("SPD1305", init<char *, int>())
.def("Connect", &SPD1305::Connect)
.def("setCurrent", &SPD1305::setCurrent)
.def("getCurrent", &SPD1305::getCurrent)
.def("Disconnect", &SPD1305::Disconnect);


class_<KEI2600>("KEI2600", init<char *, int>())
.def("Connect", &KEI2600::Connect)
.def("setCurrent", &KEI2600::Disconnect);
}
21 changes: 21 additions & 0 deletions py_icl/py_icl_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

from libpy_icl import *
import os
import sys

os.add_dll_directory(os.getcwd())


def spd_test():
spd = SPD1305("132.231.14.176", 0)
spd.Connect()
spd.setCurrent(DC_CHANNEL.CHANNEL_1, 0.1)
current = spd.getCurrent(DC_CHANNEL.CHANNEL_1)
current == 0.1
spd.Disconnect()


if __name__ == "__main__":
import doctest
doctest.testmod()

4 changes: 4 additions & 0 deletions src/devices/KEI2600.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ KEI2600::KEI2600(const char *ip, int timeoutInMS, PIL::Logging *logger) : SMU(ip
this->m_DeviceName = DEVICE_NAME;
}

KEI2600::KEI2600(const char *ip, int timeoutInMS) : SMU(ip, timeoutInMS, nullptr) {
this->m_DeviceName = DEVICE_NAME;
}


PIL_ERROR_CODE KEI2600::measure(UNIT unit, SMU_CHANNEL channel, double* value)
{
Expand Down

0 comments on commit f46ac51

Please sign in to comment.