# -*- coding: utf-8; mode: tcl; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- vim:fenc=utf-8:ft=tcl:et:sw=4:ts=4:sts=4

PortSystem          1.0
PortGroup           github 1.0
PortGroup           makefile 1.0

github.setup        renode renode 1.16.1 v
github.tarball_from releases

revision            0
categories          emulators devel
license             MIT
maintainers         {pguyot @pguyot} openmaintainer

description         Virtual development framework for embedded systems

long_description    Renode is an open source virtual development framework \
                    for complex embedded systems. It simulates SoCs, sensors, \
                    and other hardware, letting you run unmodified firmware \
                    binaries on your workstation. Supports ARM, RISC-V, \
                    SPARC, x86, Xtensa, POWER and more.

homepage            https://renode.io

distname            renode_${version}_source
use_xz              yes

checksums           rmd160  1162c3be17e823c83a1460a3963b8703f76b683e \
                    sha256  fefe9a495cb54af09f13a93a863b6cad027f7ddc8854921efa6e42d9fbcd9888 \
                    size    240071476

platforms           {darwin >= 21}
supported_archs     x86_64 arm64

depends_build-append \
                    port:dotnet-sdk-8 \
                    path:bin/cmake:cmake \
                    port:coreutils

depends_run-append  port:dotnet-cli \
                    port:dotnet-runtime-8

makefile.override   {}
makefile.has_destdir no

build.env-append    "PATH=${prefix}/libexec/gnubin:$env(PATH)" \
                    DOTNET_CLI_TELEMETRY_OPTOUT=1 \
                    PLATFORM=

build.cmd           ./build.sh
build.target
build.args          --host-arch ${configure.build_arch}

destroot {
    set renode_dir ${prefix}/libexec/${name}

    # Create directory structure
    xinstall -d ${destroot}${renode_dir}
    xinstall -d ${destroot}${renode_dir}/output

    # Install build output
    copy ${worksrcpath}/output/bin ${destroot}${renode_dir}/output/bin

    # Install tools (needed by the launcher scripts)
    copy ${worksrcpath}/tools ${destroot}${renode_dir}/tools

    # Install scripts, platforms, tests
    copy ${worksrcpath}/scripts ${destroot}${renode_dir}/scripts
    copy ${worksrcpath}/platforms ${destroot}${renode_dir}/platforms
    copy ${worksrcpath}/tests ${destroot}${renode_dir}/tests

    # Install lib resources (e.g. robot.css used by renode-test)
    copy ${worksrcpath}/lib ${destroot}${renode_dir}/lib

    # Install the root marker file used by TryGetRootDirectory to locate built-in platforms
    xinstall -m 0644 ${worksrcpath}/.renode-root ${destroot}${renode_dir}/.renode-root

    # Install the original launcher scripts
    xinstall -m 0755 ${worksrcpath}/renode \
        ${destroot}${renode_dir}/renode
    xinstall -m 0755 ${worksrcpath}/renode-test \
        ${destroot}${renode_dir}/renode-test

    # The upstream renode-test hardcodes the test results directory below
    # ${renode_dir}/output, which is read-only once installed. Default to /tmp
    reinplace "s|TESTS_RESULTS=\"\$ROOT_PATH/output/tests\"|TESTS_RESULTS=\"/tmp/renode/output/tests\"|" \
        ${destroot}${renode_dir}/renode-test

    # Create CLI wrapper for renode
    xinstall -m 0755 ${filespath}/renode.sh \
        ${destroot}${prefix}/bin/renode
    reinplace "s|@RENODE_DIR@|${renode_dir}|g" \
        ${destroot}${prefix}/bin/renode

    # Create CLI wrapper for renode-test
    xinstall -m 0755 ${filespath}/renode-test.sh \
        ${destroot}${prefix}/bin/renode-test
    reinplace "s|@RENODE_DIR@|${renode_dir}|g" \
        ${destroot}${prefix}/bin/renode-test
}

notes "
Renode has been installed as a CLI tool.

Usage:
    renode                  # launch with Monitor in the terminal
    renode --disable-gui    # headless mode
    renode script.resc      # run a Renode script
    renode-test test.robot  # run Robot Framework tests
"
