SUMMARY = "This recipe will automatically install the OpenPLC Runtime Environment" HOMEPAGE = "https://autonomylogic.com/" LICENSE = "MIT" SRC_URI:append = "git://github.com/Autonomy-Logic/openplc-runtime.git;protocol=https;branch=main \ file://openplc-runtime.service \ file://fake_root.patch \ " SRCREV = "cd9825b703f17c0d5db2fc99c5454f3cf822f3b3" LIC_FILES_CHKSUM = "file://LICENSE;md5=1e9650a938047d709b897ea200cd0a71" #UNPACKDIR = "${D}/openplc" #WORKDIR = "${D}/openplc" OPENPLC_USER = "plc" #S = "${UNPACKDIR}/git" inherit systemd cmake pkgconfig setuptools3 #do_configure:prepend() { #rm -rf ${S}/* #mkdir -p ${D}/openplc #rm -rf ${D}/openplc/* #} #do_unpack:append() { #} do_install[network] = "1" INHIBIT_PACKAGE_STRIP = "1" PACKAGE_STRIP = "no" INHIBIT_PACKAGE_DEBUG_SPLIT = "1" HAS_SYSTEMD = "${@bb.utils.contains('DISTRO_FEATURES','systemd','1','',d)}" ####################################################### # Colors for output RED = '\033[0;31m' GREEN = '\033[0;32m' YELLOW = '\033[1;33m' BLUE = '\033[0;34m' NC = '\033[0m' # Helper functions #log_info() { # echo -e "${BLUE}[INFO]${NC} $1" #} #log_success() { # echo -e "${GREEN}[SUCCESS]${NC} $1" #} #log_warning() { # echo -e "${YELLOW}[WARNING]${NC} $1" #} #log_error() { # echo -e "${RED}[ERROR]${NC} $1" #} install_systemd_service() { service_file="$ROOT_DIR/etc/systemd/system/openplc-runtime.service" #log_info "Installing OpenPLC Runtime systemd service..." # Create the service file cat > "$service_file" < ${S}/setup.py <<-EOF from setuptools import setup setup( name="${PYPI_PACKAGE}", version="${PV}", license="${LICENSE}", ) EOF ############################################################################################################################################### #!/bin/bash set -e # Detect the project root directory # This works whether the script is called from project root, Docker, or anywhere else #SCRIPT_DIR=$(cd $(dirname ${BASH_SOURCE[0]}) && pwd) SCRIPT_DIR=${S} OPENPLC_DIR=${S} VENV_DIR=$OPENPLC_DIR/venvs/runtime SCRIPTS_DIR=$OPENPLC_DIR/scripts ROOT_DIR=${D} # Ensure we're in the project directory cd $OPENPLC_DIR echo "OpenPLC Runtime Installation" echo "Project directory: $OPENPLC_DIR" echo "Working directory: $(pwd)" echo "Simulated Root directory: $ROOT_DIR" # Setup runtime directory (needed for both Linux and Docker) mkdir -p $ROOT_DIR/var/run/runtime chmod 775 $ROOT_DIR/var/run/runtime 2>/dev/null || true # Ignore permission errors in Docker # Create persistent data directory for native Linux installs # This directory stores .env and database files that must survive reboot # In Docker, /var/run/runtime is mounted as a persistent volume instead if [ HAS_SYSTEMD ]; then mkdir -p $ROOT_DIR/var/lib/openplc-runtime chmod 755 $ROOT_DIR/var/lib/openplc-runtime #log_info "Created persistent data directory at /var/lib/openplc-runtime" fi # Make scripts executable chmod +x "$OPENPLC_DIR/install.sh" 2>/dev/null || true chmod +x "$OPENPLC_DIR/scripts/"* 2>/dev/null || true chmod +x "$OPENPLC_DIR/start_openplc.sh" 2>/dev/null || true #python3 -m venv "$VENV_DIR" #./cross-venv/bin/cross-python3 -m venv "$VENV_DIR" #"$VENV_DIR/bin/python3" -m pip install --upgrade pip setuptools wheel #"$VENV_DIR/bin/python3" -m pip install -r "$OPENPLC_DIR/requirements.txt" #"$VENV_DIR/bin/python3" -m pip install -e . #python3 -m pip install --upgrade pip setuptools wheel #python3 -m pip install -r "$OPENPLC_DIR/requirements.txt" #python3 -m pip install -e . #echo "Dependencies installed..." #echo "Virtual environment created at $VENV_DIR" } compile_plc() { echo "Preparing build directory..." # Always clean build directory for Docker environment or when CMake cache exists # This prevents cross-contamination between Linux and Docker builds if [ -d $OPENPLC_DIR/build ] && [ -f $OPENPLC_DIR/build/CMakeCache.txt ]; then echo "Cleaning existing build directory to ensure clean build..." rm -rf $OPENPLC_DIR/build fi # Create build directory if ! mkdir -p $OPENPLC_DIR/build; then echo "ERROR: Failed to create build directory" >&2 return 1 fi cd $OPENPLC_DIR/build echo "Running cmake configuration..." if ! cmake ..; then echo "ERROR: CMake configuration failed" >&2 cd $OPENPLC_DIR return 1 fi echo "Compiling with make (using $(nproc) cores)..." if ! make -j"$(nproc)"; then echo "ERROR: Compilation failed" >&2 cd $OPENPLC_DIR return 1 fi cd $OPENPLC_DIR echo "SUCCESS: OpenPLC compiled successfully!" } build_native_plugins() { native_plugins_dir=$OPENPLC_DIR/core/src/drivers/plugins/native plugins_output_dir=$OPENPLC_DIR/build/plugins #log_info "Scanning for native plugins to build..." # Check if native plugins directory exists if [ ! -d $native_plugins_dir ]; then #log_warning "Native plugins directory not found: $native_plugins_dir" return 0 fi # Create plugins output directory mkdir -p $plugins_output_dir # Find directories with CMakeLists.txt (indicates buildable plugin) plugins_found=0 plugins_built=0 plugins_failed=0 for plugin_dir in $native_plugins_dir/*/; do # Skip if not a directory [ -d $plugin_dir ] || continue plugin_name=$(basename $plugin_dir) cmake_file=$plugin_dir/CMakeLists.txt # Skip if no CMakeLists.txt if [ ! -f $cmake_file ]; then continue fi plugins_found=1 #$((plugins_found + 1)) #log_info "Found native plugin: $plugin_name" # Create build directory for this plugin plugin_build_dir=$plugin_dir/build # Clean existing build directory if [ -d $plugin_build_dir ]; then #log_info "Cleaning existing build directory for $plugin_name..." rm -rf $plugin_build_dir fi mkdir -p $plugin_build_dir # Build the plugin #log_info "Building $plugin_name..." cd $plugin_build_dir # Configure with cmake, passing OpenPLC root directory cmake -DOPENPLC_ROOT=$OPENPLC_DIR .. # Build with make make -j"$(nproc)" # Copy built plugin to central plugins directory built_lib=$(find $plugin_build_dir -name "*.so" -type f 2>/dev/null | head -1) if [ -n $built_lib ] && [ -f $built_lib ]; then cp $built_lib $plugins_output_dir #log_success "Built and installed: $plugin_name ($(basename "$built_lib"))" plugins_built=1 #$((plugins_built + 1)) else #log_warning "No .so file found after building $plugin_name" plugins_failed=1 #$((plugins_failed + 1)) fi done } do_configure:append() { #setup_plugin_venvs echo "Compiling OpenPLC..." if compile_plc; then echo "Build process completed successfully!" # Build native plugins after main compilation echo "Building native plugins..." build_native_plugins # Create installation marker (must be done before starting the service) touch "$OPENPLC_DIR/.installed" echo "Installation completed at $(date)" > "$OPENPLC_DIR/.installed" # Check if systemd is available and install the service SYSTEMD_SERVICE_INSTALLED=0 if [ HAS_SYSTEMD ]; then #log_info "Systemd detected. Installing OpenPLC Runtime service..." if install_systemd_service; then SYSTEMD_SERVICE_INSTALLED=1 #log_warning "Failed to install systemd service. You can start the runtime manually." fi #log_info "Systemd not available. Skipping service installation." fi fi } ################################################################################################################################################################################### do_install:append() { mkdir -p ${D}/openplc #TODO make this more granular. openplc depends on some files from the source to be present on the final system, but surely not all of them. export tmppwd=${PWD} # export D="${D}/openplc" #bash ${S}/install.sh cp ${S}/start_openplc.sh ${D}/openplc #cp -r ${S}/venvs ${D}/openplc #bitbake seems to have a problem with either stripping these files out or giving incorrect ownership #rm -rf ${D}/openplc/venvs/opcua #rm -rf "${D}/openplc/venvs/runtime/lib/python3.12/site-packages/greenlet" #rm -rf "${D}/openplc/venvs/runtime/lib/python3.12/site-packages/sqlalchemy" cp -r ${S}/scripts ${D}/openplc cp -r ${S}/webserver ${D}/openplc mkdir -p ${D}/openplc/build/ cp ${S}/build/plc_main ${D}/openplc/build cp -r ${S}/build/plugins ${D}/openplc/build cp -r ${S}/core ${D}/openplc cp ${S}/.installed ${D}/openplc cp ${S}/plugins.conf ${D}/openplc find ${D}/openplc/ -wholename "*CMake*" -delete find ${D}/openplc/ -name "Makefile" -delete grep -rlw ${D} -e "${S}" | xargs sed -i -e "s|${S}|/openplc|g" #grep -rlw ${D} -e "${D}" | xargs sed -i -e "s|${D}|/openplc|g" #grep -rlw ${D} -e ${TMPDIR} | xargs rm -f grep -rlw ${D} -e ${TMPDIR} | xargs sed -i -e "s|${TMPDIR}|/tmp_dir|g" rm -rf ${D}/var rm -rf ${D}/usr/ find ${D} -name *.pyc -delete #find ${D}/openplc/venvs -name python3 -delete #should hopefully only kil symlinks #grep -rlw ${D}/openplc/venvs -e "${D}" | xargs sed -i -e "s|${D}|/|g" #grep -rlw ${D}/openplc -e ${TMPDIR} | xargs sed -i -e "s|${TMPDIR}|/tmp|g" #this should maybe pipe to rm -f instead? install -d ${D}${systemd_system_unitdir} install -m 0755 ${UNPACKDIR}/openplc-runtime.service ${D}${systemd_system_unitdir}/openplc-runtime.service #bbwarn "attempting to install service" #${D}${BIN_FILE} service install #bbwarn "attempting to start service" #${D}${BIN_FILE} service start } INHERIT += "extrausers" EXTRA_USERS_PARAMS += " useradd ${OPENPLC_USER}; \ usermod -p '\$6\$TNPZD8XEuoDZWxLo\$WOIudv2UA5eePqcX0qgiS.o0feNZow1cAq9OnYLIZYsE6Jq2n.G/7792z73lx0h6EpBr/FUZpEqIDpWliK7Oy.' ${OPENPLC_USER}; \ " SYSTEMD_PACKAGES = "${@bb.utils.contains('DISTRO_FEATURES','systemd','${PN}','',d)}" SYSTEMD_SERVICE:${PN} = "openplc-runtime.service" SYSTEMD_AUTO_ENABLE:${PN} = "enable" DEPENDS = "\ python3 \ python3-pip \ python3-setuptools \ python3-wheel \ pkgconfig \ cmake \ pkgconfig-native \ " RDEPENDS:${PN} = "\ python3 \ python3-pip \ python3-venv \ python3-modules \ python3-pymodbus \ python3-greenlet \ python3-sqlalchemy \ python3-psutil \ python3-markupsafe \ python3-asyncua \ python3-pyserial \ python3-flask \ python3-cryptography \ python3-pytest \ python3-pytest-asyncio \ python3-pytest-mock \ python3-psutil \ python3-bcrypt \ python3-socketio \ python3-python-dotenv \ python3-pyjwt \ python3-flask-sqlalchemy \ python3-pre-commit \ python3-flask-jwt-extended \ python3-flask-login \ python3-aiomqtt \ python3-flask-socketio \ python3-aiofiles \ python3-aiosqlite \ python3-pyopenssl \ python3-dateutil \ python3-pytz \ python3-sortedcontainers \ bash \ " #python3-dev #python3-yaml #iproute #nc FILES:${PN} = "\ /var \ /run \ /run/runtime \ /openplc \ /openplc/* \ ${systemd_system_unitdir}/openplc-runtime.service \ "