# -*- 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           cmake 1.1
PortGroup           github 1.0
PortGroup           openssl 1.0

github.setup        avast retdec 5.0 v
# Change github.tarball_from to 'releases' or 'archive' next update
github.tarball_from tarball
revision            0
conflicts           ${name}-devel

categories          devel
license             MIT
maintainers         nomaintainer

description         RetDec is a retargetable machine-code decompiler based on LLVM
long_description    {*}${description}. The decompiler is not limited to any particular \
                    target architecture, operating system, or executable file format.

# NOTE: keep it in sync with cmake/deps.cmake
set capstone_version    5.0-rc2
set googletest_version  90a443f9c2437ca8a682a1ac625eba64e1d74a8a
set keystone_version    d7ba8e378e5284e6384fc9ecd660ed5f6532e922
set llvm_version        2a1f3d8a97241c6e91710be8f84cf3cf80c03390
set yara_version        4.2.0-rc1
set yaramod_version     a367d910ae79698e64e99d8414695281723cd34b
set support_pkg_version retdec-support_2019-03-08

# after update it, adjust checksums of file
array set deps_cmake_checksums { \
                    rmd160  5fda05a8823a9dc88c613e65039d67c3887e81ef \
                    sha256  70a7f3eeba53148265439ea7edbf043d4e8ae3d3495e26cb09583cca5c057eae \
                    size    2022 \
                    }

checksums           ${distname}${extract.suffix} \
                    rmd160  3f0d02db6e5a7c6adda86a268ce1a535ec8ae9c1 \
                    sha256  46db5ce52ce2928d07276f41ef3510fe4667f70477b95dacfc637617fb910a1a \
                    size    27197302 \
                    ${capstone_version}${extract.suffix} \
                    rmd160  b70ecbc6efb0a91dcf7159ac86d98255089f432d \
                    sha256  869d94813a887329bc11b4bf1f4410a7a2b7f270176439e90b158127d5a215dd \
                    size    5761725 \
                    ${googletest_version}${extract.suffix} \
                    rmd160  67078fab78962dec98bb1dd52b2ea710dd48f0bb \
                    sha256  b048f20cde9e31291b58b9890c65f6ad9f0a75c758bc842238490af4d4ed94b3 \
                    size    907391 \
                    ${keystone_version}${extract.suffix} \
                    rmd160  21d7335da9a68ee6eb5ad3db8683b007e46dfb32 \
                    sha256  24180af1a0e3e72766d7e7a23ecec1fad808764d25f015eff5c420a8016af28e \
                    size    4404853 \
                    ${llvm_version}${extract.suffix} \
                    rmd160  ead34a16d7090d48fbe1add17f5f45dbd8e6531d \
                    sha256  15606af6ceb140c7c83cdc657018bfc36ff23fa7ac6ed895563acf6304512d24 \
                    size    44827080 \
                    v${yara_version}${extract.suffix} \
                    rmd160  324ba13781c23a31cfb1f377ba6b678937b8eb31 \
                    sha256  5ddb8c82127ac1bb9ab990d0b34aecda4912a35ab06614ba34ac772692db8cd3 \
                    size    1283822 \
                    ${yaramod_version}${extract.suffix} \
                    rmd160  b5d37180ff230b6ca341041147b0a53e17e4b4e2 \
                    sha256  bbb703399974bf11861674c94a7235df5b6463e7f8efcef17d04264bfc45a2e9 \
                    size    951331 \
                    ${support_pkg_version}.tar.xz \
                    rmd160  f32521327c16ae2cc5d6ddff1b760a831707b385 \
                    sha256  629351609bca0f4b8edbd4e53789192305256aeb908e953f5546e121a911d54e \
                    size    43473100

post-patch {
    reinplace "s|\${CMAKE_INSTALL_PREFIX}|${destroot}\${CMAKE_INSTALL_PREFIX}|" ${worksrcpath}/CMakeLists.txt
    reinplace "s|\${CMAKE_INSTALL_PREFIX}|${destroot}\${CMAKE_INSTALL_PREFIX}|" ${worksrcpath}/support/CMakeLists.txt
    reinplace "s|DESTINATION \${SUPPORT_TARGET_DIR}/|DESTINATION \${RETDEC_INSTALL_SUPPORT_DIR}|" ${worksrcpath}/support/CMakeLists.txt
}

set python_branch   3.10
set python_version  [string map {. {}} ${python_branch}]

configure.python    ${prefix}/bin/python${python_branch}

# It can't be linked only against libc++
# this force is OK as node does not link against any other c++ libs
depends_lib-append   port:libcxx
configure.cxx_stdlib libc++

if {${name} eq ${subport}} {
    # 4.0's yara uses std::variant<>, which is broken on macOS <= 10.12
    if {${os.platform} eq "darwin" && ${os.major} <= 16} {
        depends_lib-append          port:macports-libcxx
        configure.cxxflags-append   -nostdinc++ -isystem${prefix}/include/libcxx/v1
        configure.ldflags-append    -L${prefix}/lib/libcxx
    }

    patchfiles      yaramod-python.diff
}

subport retdec-devel {
    conflicts       $name

    github.setup    avast retdec ce18766a2a98d0d33e67e269c1eadb3d656981ff
    # Change github.tarball_from to 'releases' or 'archive' next update
    github.tarball_from tarball
    version         20230920
    revision        0
    epoch           1

    set yaramod_version \
                    aa06dd408c492a8f4488774caf2ee105ccc23ab5

    array set deps_cmake_checksums { \
                    rmd160  5f5124b900777b3ab7cadbea8e096b0df9bc957c \
                    sha256  1db6cdb279b9f6b52fddbc7391c4548656a8e780a37ba59ba479c6f3b654d6d5 \
                    size    2022 \
                    }

    checksums-append \
                    ${distname}${extract.suffix} \
                    rmd160  40e79be606c932da2ab252237e9d02db98f1f531 \
                    sha256  36f66473e32958a86dc2e495efc312d96bc4a5106cdf9049cfe7a2ab09fe15e0 \
                    size    27198378 \
                    ${yaramod_version}${extract.suffix} \
                    rmd160  eab0a12e77f24be097e8f97f768fa02574a23c31 \
                    sha256  f9c75eb93c379ab4e1a80f28a678d54531b1bbc1de2725438b3f54be11332ee9 \
                    size    962598
}

master_sites-append https://github.com/capstone-engine/capstone/archive/refs/tags:capstone \
                    https://github.com/google/googletest/archive:googletest \
                    https://github.com/keystone-engine/keystone/archive:keystone \
                    https://github.com/avast/llvm/archive:llvm \
                    https://github.com/VirusTotal/yara/archive:yara \
                    https://github.com/avast/yaramod/archive:yaramod \
                    https://github.com/avast/retdec-support/releases/download/[lindex [split ${support_pkg_version} _] 1]:support_pkg

distfiles-append    ${capstone_version}${extract.suffix}:capstone \
                    ${googletest_version}${extract.suffix}:googletest \
                    ${keystone_version}${extract.suffix}:keystone \
                    ${llvm_version}${extract.suffix}:llvm \
                    v${yara_version}${extract.suffix}:yara \
                    ${yaramod_version}${extract.suffix}:yaramod \
                    ${support_pkg_version}.tar.xz:support_pkg

# extract.rename won't work with multiple files, emulate it
# See: https://trac.macports.org/ticket/66993
post-extract {
    move {*}[glob ${workpath}/${github.author}-${github.project}-*] ${worksrcpath}
}

# Verify that cmake.deps has expected checksums
post-extract {
    set fail no
    set deps_cmake ${worksrcpath}/cmake/deps.cmake

    foreach {type sum} [array get deps_cmake_checksums] {
        set calculated_sum [portchecksum::calc_$type $deps_cmake]
        ui_debug "[format [msgcat::mc "Calculated (%s) is %s"] $type $calculated_sum]"

        if {$sum eq $calculated_sum} {
            ui_debug "[format [msgcat::mc "Correct (%s) checksum for deps.cmake"] $type]"
        } else {
            ui_error "[format [msgcat::mc "Checksum (%s) mismatch for deps.cmake"] $type]"
            ui_info "[format [msgcat::mc "Portfile checksum: deps.cmake %s %s"] $type $sum]"
            ui_info "[format [msgcat::mc "Distfile checksum: deps.cmake %s %s"] $type $calculated_sum]"
            # Raise the failure flag
            set fail yes
        }
    }
    if {[tbool fail]} {
        return -code error "Portfile checksums for cmake/deps.cmake is wrong"
    }
}

# use MacPorts to unpack and keep all dependencies
post-extract {
    ln -s ${workpath}/capstone-${capstone_version} ${worksrcpath}/deps/capstone/src
    ln -s ${workpath}/googletest-${googletest_version} ${worksrcpath}/deps/googletest/src
    ln -s ${workpath}/keystone-${keystone_version} ${worksrcpath}/deps/keystone/src
    ln -s ${workpath}/llvm-${llvm_version} ${worksrcpath}/deps/llvm/src
    ln -s ${workpath}/yara-${yara_version} ${worksrcpath}/deps/yara/src
    ln -s ${workpath}/yaramod-${yaramod_version} ${worksrcpath}/deps/yaramod/src
}

configure.args-append \
                    -DCAPSTONE_LOCAL_DIR:PATH=${worksrcpath}/deps/capstone/src \
                    -DGOOGLETEST_LOCAL_DIR:PATH=${worksrcpath}/deps/googletest/src \
                    -DKEYSTONE_LOCAL_DIR:PATH=${worksrcpath}/deps/keystone/src \
                    -DLLVM_LOCAL_DIR:PATH=${worksrcpath}/deps/llvm/src \
                    -DYARA_LOCAL_DIR:PATH=${worksrcpath}/deps/yara/src \
                    -DYARAMOD_LOCAL_DIR:PATH=${worksrcpath}/deps/yaramod/src

# with one exception support_pkg which is installed by hand
extract.only-delete ${support_pkg_version}.tar.xz
depends_build-append \
                    port:xz
post-patch {
    # unfortunately -DRETDEC_ENABLE_SUPPORT_YARA_STATIC_CODE=OFF doesn't switch
    # off downloading of support_pkg, do it by hand
    reinplace -E "s|\(if.*RETDEC_ENABLE_SUPPORT_YARA_STATIC_CODE\)|\\1 AND FALSE|" \
        ${worksrcpath}/support/CMakeLists.txt

}

post-destroot {
    set support_pkg_path ${destroot}${prefix}/share/${name}/support
    xinstall -d -m 0755 ${support_pkg_path}
    system -W ${support_pkg_path} "xz -dc [shellescape ${distpath}/${support_pkg_version}.tar.xz] | ${portutil::autoconf::tar_command} -xf -"
}

# Before Catalina it also requires macports-libcxx
if {${os.platform} eq "darwin" && ${os.major} < 19} {
    depends_lib-append          port:macports-libcxx
    configure.cxxflags-append   -nostdinc++ -isystem${prefix}/include/libcxx/v1
    configure.ldflags-append    -L${prefix}/lib/libcxx
}

compiler.cxx_standard \
                    2017

# Extend blacklist to cover macOS10.13
compiler.blacklist-append \
                    {clang < 1100} {macports-clang-[4-9].0} macports-clang-10

build.args-append   CC=${configure.cc} \
                    CXX=${configure.cxx}

build.env-append    CC=${configure.cc} \
                    CXX=${configure.cxx}

depends_build-append \
                    port:autoconf \
                    port:automake \
                    port:cctools \
                    port:git \
                    port:gmake \
                    port:ld64

depends_lib-append  port:python${python_version} \
                    port:zlib

depends_run-append  path:bin/dot:graphviz \
                    path:bin/upx:upx

# NOTE: both available PG legacysupport brokes build with error
# libstdc++ version must be at least 4.8.
if {${os.platform} eq "darwin" && ${os.major} < 11} {
    depends_lib-append \
                    path:lib/libMacportsLegacySupport.dylib:legacy-support

    configure.cflags-append \
                    -isystem${prefix}/include/LegacySupport
    configure.cxxflags-append \
                    -isystem${prefix}/include/LegacySupport
    configure.ldflags-append \
                    -L${prefix}/lib -lMacportsLegacySupport

    # NOTE: bin2pat and pat2yara compilation by MacPort's clang-11 is failed on 10.6 as
    # clang: error: unable to execute command: Segmentation fault
    # Right now it is impossible to disable specified project.
    # A full list of options is available inside cmake/options.cmake
    # See: https://github.com/avast/retdec/issues/1112
    configure.args-append \
                    -DRETDEC_ENABLE_ALL:BOOL=OFF \
                    -DRETDEC_ENABLE_AR_EXTRACTOR:BOOL=ON \
                    -DRETDEC_ENABLE_AR_EXTRACTORTOOL:BOOL=ON \
                    -DRETDEC_ENABLE_BIN2LLVMIR:BOOL=ON \
                    -DRETDEC_ENABLE_BIN2LLVMIRTOOL:BOOL=ON \
                    -DRETDEC_ENABLE_BIN2PAT:BOOL=OFF \
                    -DRETDEC_ENABLE_CAPSTONE2LLVMIR:BOOL=ON \
                    -DRETDEC_ENABLE_CAPSTONE2LLVMIRTOOL:BOOL=ON \
                    -DRETDEC_ENABLE_COMMON:BOOL=ON \
                    -DRETDEC_ENABLE_CONFIG:BOOL=ON \
                    -DRETDEC_ENABLE_CPDETECT:BOOL=ON \
                    -DRETDEC_ENABLE_CTYPES:BOOL=ON \
                    -DRETDEC_ENABLE_CTYPESPARSER:BOOL=ON \
                    -DRETDEC_ENABLE_DEBUGFORMAT:BOOL=ON \
                    -DRETDEC_ENABLE_DEMANGLER:BOOL=ON \
                    -DRETDEC_ENABLE_DEMANGLERTOOL:BOOL=ON \
                    -DRETDEC_ENABLE_FILEFORMAT:BOOL=ON \
                    -DRETDEC_ENABLE_FILEINFO:BOOL=ON \
                    -DRETDEC_ENABLE_GETSIG:BOOL=ON \
                    -DRETDEC_ENABLE_IDR2PAT:BOOL=ON \
                    -DRETDEC_ENABLE_LLVMIR2HLL:BOOL=ON \
                    -DRETDEC_ENABLE_LLVMIR_EMUL:BOOL=ON \
                    -DRETDEC_ENABLE_LLVM_SUPPORT:BOOL=ON \
                    -DRETDEC_ENABLE_LOADER:BOOL=ON \
                    -DRETDEC_ENABLE_MACHO_EXTRACTOR:BOOL=ON \
                    -DRETDEC_ENABLE_MACHO_EXTRACTORTOOL:BOOL=ON \
                    -DRETDEC_ENABLE_PAT2YARA:BOOL=OFF \
                    -DRETDEC_ENABLE_PATTERNGEN:BOOL=ON \
                    -DRETDEC_ENABLE_PDBPARSER:BOOL=ON \
                    -DRETDEC_ENABLE_PELIB:BOOL=ON \
                    -DRETDEC_ENABLE_RETDEC:BOOL=ON \
                    -DRETDEC_ENABLE_RETDECTOOL:BOOL=ON \
                    -DRETDEC_ENABLE_RETDEC_DECOMPILER:BOOL=ON \
                    -DRETDEC_ENABLE_RTTI_FINDER:BOOL=ON \
                    -DRETDEC_ENABLE_SERDES:BOOL=ON \
                    -DRETDEC_ENABLE_STACOFIN:BOOL=ON \
                    -DRETDEC_ENABLE_STACOFINTOOL:BOOL=ON \
                    -DRETDEC_ENABLE_UNPACKER:BOOL=ON \
                    -DRETDEC_ENABLE_UNPACKERTOOL:BOOL=ON \
                    -DRETDEC_ENABLE_UTILS:BOOL=ON \
                    -DRETDEC_ENABLE_YARACPP:BOOL=ON
}

# error: /usr/bin/ranlib: unknown option character `n' in: -no_warning_for_no_symbols
# error: /Developer/usr/bin/libtool: malformed object (unknown load command 2)
configure.args-append \
                    -DCMAKE_LINKER=${prefix}/bin/ld \
                    -DCMAKE_RANLIB=${prefix}/bin/ranlib \
                    -DCMAKE_LIBTOOL=${prefix}/bin/libtool

# NOTE: Force use of gmake; otherwise, macOS' make will be used for top-level
# subprocesses, while gmake will be used for lower-level ones. And jobserver
# coordination doesn't work between them, preventing proper limiting of parallel
# jobs!
# See: https://trac.macports.org/ticket/63999
build.cmd           ${prefix}/bin/gmake
configure.args-append \
                    -DCMAKE_MAKE_PROGRAM=${prefix}/bin/gmake

post-destroot {
    reinplace "s|^#!/usr/bin/env python3|#!${configure.python}|" \
        {*}[glob ${destroot}${prefix}/bin/*.py]
}


github.livecheck.regex  {([^"v]+)}
