Skip to content

Commit

Permalink
Initial pybind11 Controller Wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
krypton36 committed Jul 15, 2021
1 parent b2a1856 commit b5c952c
Show file tree
Hide file tree
Showing 13 changed files with 693 additions and 15 deletions.
12 changes: 12 additions & 0 deletions .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,18 @@ RUN useradd -s /bin/bash -u $USER_UID -g $USER_GID -G docker -m $USERNAME
RUN echo $USERNAME ALL=\(root\) NOPASSWD:ALL > /etc/sudoers.d/$USERNAME
RUN chmod 0440 /etc/sudoers.d/$USERNAME

# Python 3.9 and PIP
RUN set -x \
&& sudo apt-get install -y libgirepository1.0-dev \
&& DEBIAN_FRONTEND=noninteractive apt-get install -y software-properties-common \
&& sudo add-apt-repository universe \
&& DEBIAN_FRONTEND=noninteractive apt-get install -y python3.9 python3.9-venv python3.9-dev\
&& curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py \
&& python3.9 get-pip.py \
&& sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.9 1 \
&& rm -rf /var/lib/apt/lists/ \
&& : # last line

RUN mkdir -p /var/downloads
RUN cd /var/downloads
RUN curl -JL https://github.com/microsoft/vscode-cpptools/releases/download/0.27.0/cpptools-linux.vsix > extension.zip
Expand Down
4 changes: 4 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,7 @@
path = third_party/mbed-os-posix-socket/repo
url = https://github.com/ARMmbed/mbed-os-posix-socket.git
branch = main
[submodule "third_party/pybind11/repo"]
path = third_party/pybind11/repo
url = ../../pybind/pybind11
branch = stable
2 changes: 2 additions & 0 deletions BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ if (current_toolchain != "${dir_pw_toolchain}/default:default") {
deps += [ "${chip_root}/src/tools/chip-cert" ]
}
if (chip_enable_python_modules) {
deps += [ "${chip_root}/src/pybindings/pycontroller" ]
deps += [ "${chip_root}/src/controller/python" ]
}
}
Expand All @@ -129,6 +130,7 @@ if (current_toolchain != "${dir_pw_toolchain}/default:default") {
data_deps = [ "${chip_root}/examples/chip-tool" ]

if (chip_enable_python_modules) {
data_deps += [ "${chip_root}/src/pybindings/pycontroller" ]
data_deps += [ "${chip_root}/src/controller/python" ]
}

Expand Down
11 changes: 10 additions & 1 deletion build/config/compiler/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,11 @@ config("sanitize_default") {
config("fuzzing_default") {
}

declare_args() {
# Enable Runtime Type Information (RTTI)
enable_rtti = false
}

config("no_rtti") {
cflags_cc = [ "-fno-rtti" ]
}
Expand All @@ -365,7 +370,11 @@ config("rtti") {
}

config("rtti_default") {
configs = [ ":no_rtti" ]
if (enable_rtti) {
configs = [ ":rtti" ]
} else {
configs = [ ":no_rtti" ]
}
}

config("no_exceptions") {
Expand Down
21 changes: 12 additions & 9 deletions integrations/docker/images/chip-build/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@ RUN set -x \
ninja-build \
openjdk-8-jdk \
pkg-config \
python3 \
python3-dev \
python3-pip \
python3-venv \
python3.9 \
python3.9-dev \
python3.9-venv \
rsync \
shellcheck \
strace \
sudo \
systemd \
udev \
unzip \
Expand All @@ -74,14 +74,17 @@ RUN set -x \
&& exec bash \
&& : # last line

# Python 2 and PIP
RUN useradd -m docker && echo "docker:docker" | chpasswd && adduser docker sudo
# Python 3.9 and PIP
RUN set -x \
&& apt-get update \
&& sudo apt-get update \
&& sudo apt-get install -y libgirepository1.0-dev \
&& DEBIAN_FRONTEND=noninteractive apt-get install -y software-properties-common \
&& add-apt-repository universe \
&& DEBIAN_FRONTEND=noninteractive apt-get install -y python python2 \
&& curl https://bootstrap.pypa.io/pip/2.7/get-pip.py --output get-pip.py \
&& python2 get-pip.py \
&& DEBIAN_FRONTEND=noninteractive apt-get install -y python3.9 python3.9-venv python3.9-dev\
&& curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py \
&& python3.9 get-pip.py \
&& update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.9 1 \
&& rm -rf /var/lib/apt/lists/ \
&& : # last line

Expand Down
34 changes: 29 additions & 5 deletions scripts/build_python.sh
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,13 @@ OUTPUT_ROOT="$CHIP_ROOT/out/python_lib"
ENVIRONMENT_ROOT="$CHIP_ROOT/out/python_env"

declare chip_detail_logging=false
declare enable_pybindinds=false
declare chip_mdns
declare clusters=true

help() {

echo "Usage: $file_name [ options ... ] [ -chip_detail_logging ChipDetailLoggingValue ] [ -chip_mdns ChipMDNSValue ]"
echo "Usage: $file_name [ options ... ] [ -chip_detail_logging ChipDetailLoggingValue ] [ -chip_mdns ChipMDNSValue ] [-enable_pybindinds EnableValue]"

echo "General Options:
-h, --help Display this information.
Expand All @@ -55,6 +56,7 @@ Input Options:
By default it is minimal.
-c, --clusters_for_ip_commissioning true/false Specify whether to use clusters for IP commissioning.
By default it is true.
-p, --enable_pybindinds EnableValue Specify whether to enable pybindings as python controller.
"
}

Expand All @@ -78,6 +80,10 @@ while (($#)); do
clusters=$2
shift
;;
--enable_pybindinds | -p)
enable_pybindinds=$2
shift
;;
-*)
help
echo "Unknown Option \"$1\""
Expand All @@ -88,25 +94,43 @@ while (($#)); do
done

# Print input values
echo "Input values: chip_detail_logging = $chip_detail_logging , chip_mdns = \"$chip_mdns\""
echo "Input values: chip_detail_logging = $chip_detail_logging , chip_mdns = \"$chip_mdns\", enable_pybindinds = $enable_pybindinds"

# Ensure we have a compilation environment
source "$CHIP_ROOT/scripts/activate.sh"

# Generates ninja files
[[ -n "$chip_mdns" ]] && chip_mdns_arg="chip_mdns=\"$chip_mdns\"" || chip_mdns_arg=""
gn --root="$CHIP_ROOT" gen "$OUTPUT_ROOT" --args="chip_detail_logging=$chip_detail_logging chip_use_clusters_for_ip_commissioning=$clusters $chip_mdns_arg"

gn --root="$CHIP_ROOT" gen "$OUTPUT_ROOT" --args="chip_detail_logging=$chip_detail_logging enable_rtti=$enable_pybindinds chip_use_clusters_for_ip_commissioning=$clusters $chip_mdns_arg"

# Compiles python files
ninja -C "$OUTPUT_ROOT" python
# Check pybindings was requested
if [ $enable_pybindinds == true ]
then
ninja -v -C "$OUTPUT_ROOT" pycontroller
else
ninja -v -C "$OUTPUT_ROOT" python
fi



# Create a virtual environment that has access to the built python tools
virtualenv --clear "$ENVIRONMENT_ROOT"

# Activate the new enviroment to register the python WHL

if [ $enable_pybindinds == true ]
then
WHEEL="$OUTPUT_ROOT"/pybindings/pycontroller/pybindings-*.whl
else
WHEEL="$OUTPUT_ROOT"/controller/python/chip-*.whl
fi

source "$ENVIRONMENT_ROOT"/bin/activate
"$ENVIRONMENT_ROOT"/bin/python -m pip install --upgrade pip
"$ENVIRONMENT_ROOT"/bin/pip install --upgrade --force-reinstall --no-cache-dir "$OUTPUT_ROOT"/controller/python/chip-*.whl
"$ENVIRONMENT_ROOT"/bin/pip install --upgrade --force-reinstall --no-cache-dir $WHEEL


echo ""
echo_green "Compilation completed and WHL package installed in: "
Expand Down
1 change: 1 addition & 0 deletions scripts/tools/zap_regen_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def getSpecificTemplatesTargets():
targets.append(['src/controller/data_model/controller-clusters.zap', '-t', 'src/app/common/templates/templates.json'])
targets.append(['src/controller/data_model/controller-clusters.zap', '-t', 'examples/chip-tool/templates/templates.json'])
targets.append(['src/controller/data_model/controller-clusters.zap', '-t', 'src/controller/python/templates/templates.json'])
targets.append(['src/controller/data_model/controller-clusters.zap', '-t', 'src/pybindings/pycontroller/templates/templates.json'])
targets.append(['src/controller/data_model/controller-clusters.zap', '-t', 'src/darwin/Framework/CHIP/templates/templates.json'])
targets.append(['src/controller/data_model/controller-clusters.zap', '-t', 'src/controller/java/templates/templates.json'])
return targets
Expand Down
68 changes: 68 additions & 0 deletions src/pybindings/Bnder_Code_Gen.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# Generating PyBind with Binder

## Install Binder

- Installation instructions can be found
[here](https://cppbinder.readthedocs.io/en/latest/install.html).

## Binder Command

binder --config `./CHIPDeviceController.config` --suppress-errors --root-module
PyChip --prefix `connectedhomeip/src/pybindings/pycontroller/GeneratedFiles`
--flat `all_header.hpp` -- -DCHIP_HAVE_CONFIG_H=1
-DCHIP_CONFIG_IM_ENABLE_SCHEMA_CHECK=1 -target `x86_64-apple-macos10.15` -x c++
-std=c++11
`-I/Library/Developer/CommandLineTools/SDKs/MacOSX11.3.sdk/usr/include/`
-Iconnectedhomeip/src -Iconnectedhomeip/src/lib
-Iconnectedhomeip/out/python_lib/gen/include -Iconnectedhomeip/config/standalone
-Iconnectedhomeip/third_party/pigweed/repo/pw_minimal_cpp_stdlib/public
-Iconnectedhomeip/third_party/pigweed/repo/pw_polyfill/standard_library_public
-Iconnectedhomeip/src/include/

- Include paths may vary and `highlighted` text may also vary.

## Binder Output

- Binder will output several files with prefix PyChip and suffix \_#
- The output will be based on the config and hpp file passed in.
- Since binder is new to this project, manual conversion is necessary.
- Example of the output below:

![alt text](https://raw.githubusercontent.com/krypton36/images/master/ss3.png)

## Binder Conversions

- If the file contains everythin you want then the file can be copied over and
added.
- If there is just a class and it can be added to the existing, the class
binding can be added.

Example of conversion of Logging:

- Search for the module:

<img src="https://raw.githubusercontent.com/krypton36/images/master/ss16.png " alt="alt text" width="250" height="400">

- Combined these two to a single Logging file

<img src="https://raw.githubusercontent.com/krypton36/images/master/ss17.png " alt="alt text" width="353" height="147">
<font size="+3">-></font>
<img src="https://raw.githubusercontent.com/krypton36/images/master/ss27.png " alt="alt text" width="150" height="100">

- Rename function to bind_PyChip_Logging:

<img src="https://raw.githubusercontent.com/krypton36/images/master/ss19.png " alt="alt text" width="665" height="25">

- Add the logging binding to Main

<img src="https://raw.githubusercontent.com/krypton36/images/master/ss20.png " alt="alt text" width="265" height="20">
<img src="https://raw.githubusercontent.com/krypton36/images/master/ss23.png " alt="alt text" width="265" height="100"><font size="+3">-></font> <img src="https://raw.githubusercontent.com/krypton36/images/master/ss24.png " alt="alt text" width="165" height="100"><font size="+3">-></font> <img src="https://raw.githubusercontent.com/krypton36/images/master/ss18.png " alt="alt text" width="100" height="180"><font size="+3">-></font><img src="https://raw.githubusercontent.com/krypton36/images/master/ss26.png " alt="alt text" width="300" height="50">

- Add to BUILD GN file:

<img src="https://raw.githubusercontent.com/krypton36/images/master/ss25.png " alt="alt text" width="500" height="200">

- Lastly Test:

<img src="https://raw.githubusercontent.com/krypton36/images/master/TestGif.gif" alt="alt text" width="500" height="200">
Loading

0 comments on commit b5c952c

Please sign in to comment.