Copyright (C) 2022 - 2023 Advanced Micro Devices, Inc. All rights reserved.
SPDX-License-Identifier: BSD-3-Clause

To build petalinux with jupyter notebook and RAFT, follow the below steps.
Use this document only if jupyter notebook is also required.
The final file system size will be higher in comparison.

Note: The raft recipe is added by default in system controller bsps. The below steps are not needed in those bsps.
      When the machine boots raft will come up through init scripts in system controller bsps.

For usage without jupyter notebook, use <RAFT>/docs/build_petalinux_with_raft.txt

BSPs are available at https://www.xilinx.com/support/download/index.html/content/xilinx/en/downloadNav/embedded-design-tools.html
For the tools, see the corresponding version of the PetaLinux Tools Documentation: PetaLinux Command Line Reference (UG1157).

Step 1: petalinux-create
petalinux-create -t project -s <bsp_name> -n <proj_name>

Step 2: petalinux-config
cd <proj_name>
petalinux-config --silentconfig

Step 3: Create raft recipe
petalinux-create -t apps --template c --name raft --enable
cd project-spec/meta-user/recipes-apps/raft
rm -f *
Create an empty raft_<version_name>.bb file. Eg: raft_2024.1.bb
Copy the below contents the file
Note: BRANCH, SRCREV should be updated correctly
      If the requried board is not in the PACKAGECONFIG:append:<board name>, it need to be added.
      The below script with zcu208, zcu216 and system controller boards.
	  Example:
	  BRANCH = "main"
	  SRCREV = "8f5d98b259bd43473b364f3fe839ca7fcb0744ec"
============================================================================
SUMMARY = "RAFT python application"
LICENSE = "MIT & BSD-3-Clause"
LIC_FILES_CHKSUM = " \
    file://${WORKDIR}/git/LICENSE;md5=cc21c526211d34984839aa67dd16f172 \
    file://${WORKDIR}/git/docs/LICENSE;md5=d8f0ffdbc8d019bc821a5a07bdca1406 \

BRANCH = "<Branch Name>"
SRC_URI = "git://github.com/Xilinx/RAFT;protocol=https;branch=${BRANCH}"
SRCREV = "<commit ID>"

inherit update-rc.d systemd

S = "${WORKDIR}/git"
COMPATIBLE_MACHINE = "^$"
COMPATIBLE_MACHINE:zynqmp = "${MACHINE}"

INITSCRIPT_NAME = "raft-startup"
INITSCRIPT_PARAMS = "start 99 S ."

SYSTEMD_PACKAGES = "${PN}"
SYSTEMD_SERVICE:${PN} = "raft-startup.service"
SYSTEMD_AUTO_ENABLE:${PN}="enable"

DEPENDS += "libmetal"

RDEPENDS:${PN} += "python3 \
        python3-pyro4 \
	python3-cffi \
        python3-async \
        python3-serpent \
        bash \
"

PACKAGECONFIG[raftnotebooks] = "enabled,disabled,,packagegroup-petalinux-jupyter"
PACKAGECONFIG[raftstartup] = "enabled,disabled,,rfdc rfclk libmetal"
PACKAGECONFIG[raftstartupsc] = "enabled,disabled,,python3-psutil python3-periphery"

do_install() {
    if ${@bb.utils.contains('DISTRO_FEATURES','sysvinit','true','false',d)}; then
        SYSCONFDIR=${D}${sysconfdir}/init.d/
    else
        SYSCONFDIR=''
    fi
    oe_runmake install DESTDIR=${D}\
    NOTEBOOKS=${@bb.utils.contains('PACKAGECONFIG','raftnotebooks','enabled','', d)}\
    STARTUPSC=${@bb.utils.contains('PACKAGECONFIG','raftstartupsc','enabled','',d)}\
    STARTUP=${@bb.utils.contains('PACKAGECONFIG','raftstartup','enabled','',d)}\
    BINDIR=${D}${bindir}\
    SYSTEM_UNIT_DIR=${D}${systemd_system_unitdir}\
    SYSCONF_DIR=${SYSCONFDIR}
}

PACKAGECONFIG:append:zcu208-zynqmp = "raftnotebooks raftstartup"
PACKAGECONFIG:append:zcu216-zynqmp = "raftnotebooks raftstartup"
PACKAGECONFIG:append:system-controller = "raftstartupsc"

FILES:${PN} += " \
    ${datadir}/raft/* \
    ${libdir}/libaximemmap.so.* \
    ${libdir}/libxhelper.so.* \
    ${libdir}/libXI2c.so.* \
    ${libdir}/libXSysmon.so.* \
    ${datadir}/notebooks \
    ${@bb.utils.contains('DISTRO_FEATURES','sysvinit','${sysconfdir}/*', '', d)} \
    "
============================================================================

Step 4: Config rootfs
petalinux-config -c rootfs --silentconfig

Step 5: Build
petalinux-build

For boot options please refer UG1444.
