From bdaea2af007d9334091c6c500cb4605b4b390a0a Mon Sep 17 00:00:00 2001
From: Piraty <piraty1@inbox.ru>
Date: Mon, 20 May 2019 21:08:27 +0200
Subject: [PATCH] New package: heaptrack-1.1.0

---
 srcpkgs/heaptrack-gui                         |  1 +
 .../patches/001.compile-on-32bit.patch        | 70 +++++++++++++++++++
 .../heaptrack/patches/002.musl-fixes.patch    | 68 ++++++++++++++++++
 srcpkgs/heaptrack/patches/003.uint.patch      | 49 +++++++++++++
 srcpkgs/heaptrack/patches/004.desktop.patch   | 61 ++++++++++++++++
 srcpkgs/heaptrack/template                    | 28 ++++++++
 6 files changed, 277 insertions(+)
 create mode 120000 srcpkgs/heaptrack-gui
 create mode 100644 srcpkgs/heaptrack/patches/001.compile-on-32bit.patch
 create mode 100644 srcpkgs/heaptrack/patches/002.musl-fixes.patch
 create mode 100644 srcpkgs/heaptrack/patches/003.uint.patch
 create mode 100644 srcpkgs/heaptrack/patches/004.desktop.patch
 create mode 100644 srcpkgs/heaptrack/template

diff --git a/srcpkgs/heaptrack-gui b/srcpkgs/heaptrack-gui
new file mode 120000
index 00000000000..9a8c22eb92e
--- /dev/null
+++ b/srcpkgs/heaptrack-gui
@@ -0,0 +1 @@
+heaptrack
\ No newline at end of file
diff --git a/srcpkgs/heaptrack/patches/001.compile-on-32bit.patch b/srcpkgs/heaptrack/patches/001.compile-on-32bit.patch
new file mode 100644
index 00000000000..f7016cc6336
--- /dev/null
+++ b/srcpkgs/heaptrack/patches/001.compile-on-32bit.patch
@@ -0,0 +1,70 @@
+# reason: current release is unable to build on 32bit arches
+# src: upstream/master
+
+From 76fd2e84ba133e96d2cfdf90cb715e66e923eb8f Mon Sep 17 00:00:00 2001
+From: Milian Wolff <mail@milianw.de>
+Date: Thu, 17 May 2018 13:31:25 +0200
+Subject: [PATCH] Fix compile on 32bit
+
+BUG: 394330
+---
+ src/util/linewriter.h | 5 +++++
+ 1 file changed, 5 insertions(+)
+
+diff --git a/src/util/linewriter.h b/src/util/linewriter.h
+index 73a7930..e895e87 100644
+--- src/util/linewriter.h
++++ src/util/linewriter.h
+@@ -158,6 +158,11 @@ class LineWriter
+         return __builtin_clzl(V);
+     }
+ 
++    inline static unsigned clz(long long unsigned V)
++    {
++        return __builtin_clzll(V);
++    }
++
+     template <typename V>
+     static char* writeHexNumber(char* buffer, V value)
+     {
+
+
+
+From 49577e019ea791ee63962cdfe7e9c0c5b5c6ea4b Mon Sep 17 00:00:00 2001
+From: Milian Wolff <milian.wolff@kdab.com>
+Date: Tue, 29 May 2018 10:41:10 +0200
+Subject: [PATCH] Fix another compile error on 32bit
+
+Introduce a user-defined _u64 literal for uint64_t
+and use that instead of ul/ull.
+
+BUG: 394330
+---
+ tests/auto/tst_io.cpp | 7 ++++++-
+ 1 file changed, 6 insertions(+), 1 deletion(-)
+
+diff --git a/tests/auto/tst_io.cpp b/tests/auto/tst_io.cpp
+index 48abda6..fd31b31 100644
+--- tests/auto/tst_io.cpp
++++ tests/auto/tst_io.cpp
+@@ -27,6 +27,11 @@
+ 
+ using namespace std;
+ 
++constexpr uint64_t operator"" _u64(unsigned long long v)
++{
++    return static_cast<uint64_t>(v);
++}
++
+ TEST_CASE ("write data", "[write]") {
+     TempFile file;
+     REQUIRE(file.open());
+@@ -124,7 +129,7 @@ TEST_CASE ("read line 64bit", "[read]") {
+     REQUIRE(reader >> module);
+     REQUIRE(module == "/tmp/KDevelop-5.2.1-x86_64/usr/lib/libKF5Completion.so.5");
+ 
+-    for (uint64_t expected : {0x7f48beedc00ul, 0x0ul, 0x36854ul, 0x236858ul, 0x2700ul}) {
++    for (auto expected : {0x7f48beedc00_u64, 0x0_u64, 0x36854_u64, 0x236858_u64, 0x2700_u64}) {
+         uint64_t addr = 0;
+         REQUIRE(reader >> addr);
+         REQUIRE(addr == expected);
diff --git a/srcpkgs/heaptrack/patches/002.musl-fixes.patch b/srcpkgs/heaptrack/patches/002.musl-fixes.patch
new file mode 100644
index 00000000000..5dde897ae18
--- /dev/null
+++ b/srcpkgs/heaptrack/patches/002.musl-fixes.patch
@@ -0,0 +1,68 @@
+# reason: fix sources to work correctly with musl
+
+--- src/track/heaptrack_inject.cpp
++++ src/track/heaptrack_inject.cpp
+@@ -27,6 +27,7 @@
+ #include <link.h>
+ #include <malloc.h>
+ #include <unistd.h>
++#include <dlfcn.h>
+ 
+ #include <sys/mman.h>
+ 
+--- src/track/libheaptrack.cpp
++++ src/track/libheaptrack.cpp
+@@ -72,7 +72,7 @@
+     return chrono::duration_cast<chrono::milliseconds>(clock::now() - startTime());
+ }
+ 
+-__pid_t gettid()
++pid_t gettid()
+ {
+     return syscall(SYS_gettid);
+ }
+
+--- src/track/heaptrack_inject.cpp
++++ src/track/heaptrack_inject.cpp
+@@ -38,9 +38,10 @@
+  * @brief Experimental support for symbol overloading after runtime injection.
+  */
+ 
+-#if __WORDSIZE == 64
++#include <limits.h>
++#if ULONG_MAX == 0xffffffffffffffff
+ #define ELF_R_SYM(i) ELF64_R_SYM(i)
+-#elif __WORDSIZE == 32
++#elif ULONG_MAX == 0xffffffff
+ #define ELF_R_SYM(i) ELF32_R_SYM(i)
+ #else
+ #error unsupported word size
+
+# simplify stdint.h include which failed on musl due to obscure assumptions
+--- 3rdparty/libbacktrace/backtrace.h
++++ 3rdparty/libbacktrace/backtrace.h
+@@ -36,24 +36,8 @@
+ #include <stddef.h>
+ #include <stdio.h>
+ 
+-/* We want to get a definition for uintptr_t, but we still care about
+-   systems that don't have <stdint.h>.  */
+-#if defined(__GLIBC__) && __GLIBC__ >= 2
+-
+-#include <stdint.h>
+-
+-#elif defined(HAVE_STDINT_H)
+-
+ #include <stdint.h>
+ 
+-#else
+-
+-/* Systems that don't have <stdint.h> must provide gstdint.h, e.g.,
+-   from GCC_HEADER_STDINT in configure.ac.  */
+-#include "gstdint.h"
+-
+-#endif
+-
+ #ifdef __cplusplus
+ extern "C" {
+ #endif
diff --git a/srcpkgs/heaptrack/patches/003.uint.patch b/srcpkgs/heaptrack/patches/003.uint.patch
new file mode 100644
index 00000000000..ccf4821a5d3
--- /dev/null
+++ b/srcpkgs/heaptrack/patches/003.uint.patch
@@ -0,0 +1,49 @@
+# src: upstream/master
+
+From f540b95cc7446fbf41b4b3fa9b6f275b580de638 Mon Sep 17 00:00:00 2001
+From: Sergio Martins <smartins@kde.org>
+Date: Sun, 26 May 2019 01:48:21 +0100
+Subject: [PATCH] Use 'unsigned int' instead of 'uint'
+
+The former is cross platform and makes it buildable with MSVC.
+As it's just a couple of places there's no need for a typedef.
+---
+ src/analyze/accumulatedtracedata.cpp | 4 ++--
+ src/util/indices.h                   | 2 +-
+ 2 files changed, 3 insertions(+), 3 deletions(-)
+
+diff --git a/src/analyze/accumulatedtracedata.cpp b/src/analyze/accumulatedtracedata.cpp
+index f3f07a7..268c17f 100644
+--- src/analyze/accumulatedtracedata.cpp
++++ src/analyze/accumulatedtracedata.cpp
+@@ -186,7 +186,7 @@ bool AccumulatedTraceData::read(istream& in, const ParsePass pass)
+     for (auto& allocation : allocations) {
+         allocation.clearCost();
+     }
+-    uint fileVersion = 0;
++    unsigned int fileVersion = 0;
+     bool debuggeeEncountered = false;
+ 
+     // required for backwards compatibility
+@@ -385,7 +385,7 @@ bool AccumulatedTraceData::read(istream& in, const ParsePass pass)
+             totalCost = {};
+             fromAttached = true;
+         } else if (reader.mode() == 'v') {
+-            uint heaptrackVersion = 0;
++            unsigned int heaptrackVersion = 0;
+             reader >> heaptrackVersion;
+             if (!(reader >> fileVersion) && heaptrackVersion == 0x010200) {
+                 // backwards compatibility: before the 1.0.0, I actually
+diff --git a/src/util/indices.h b/src/util/indices.h
+index ee1f777..8f21bba 100644
+--- src/util/indices.h
++++ src/util/indices.h
+@@ -64,7 +64,7 @@ struct Index
+ };
+ 
+ template <typename Base>
+-uint qHash(const Index<Base> index, uint seed = 0) noexcept
++unsigned int qHash(const Index<Base> index, unsigned int seed = 0) noexcept
+ {
+     return qHash(index.index, seed);
+ }
diff --git a/srcpkgs/heaptrack/patches/004.desktop.patch b/srcpkgs/heaptrack/patches/004.desktop.patch
new file mode 100644
index 00000000000..3493481e86b
--- /dev/null
+++ b/srcpkgs/heaptrack/patches/004.desktop.patch
@@ -0,0 +1,61 @@
+# src: upstream/master
+
+From f2349d480fc30cf55281218b2b8063c4e5207abb Mon Sep 17 00:00:00 2001
+From: "Friedrich W. H. Kossebau" <kossebau@kde.org>
+Date: Tue, 5 Feb 2019 04:08:55 +0100
+Subject: [PATCH] Set StartupWMClass in desktop file
+
+Used by non-Plasma unixoid X-based shells to map windows to desktop files
+e.g. in launcher UIs, which do not know about _KDE_NET_WM_DESKTOP_FILE and
+instead expect (one of) the WM_CLASS entries to match the basename of the
+desktop file by default. StartupWMClass overwrites that default.
+---
+ src/analyze/gui/org.kde.heaptrack.desktop | 1 +
+ 1 file changed, 1 insertion(+)
+
+diff --git a/src/analyze/gui/org.kde.heaptrack.desktop b/src/analyze/gui/org.kde.heaptrack.desktop
+index 8c92706..ddae134 100644
+--- src/analyze/gui/org.kde.heaptrack.desktop
++++ src/analyze/gui/org.kde.heaptrack.desktop
+@@ -7,6 +7,7 @@ MimeType=application/x-heaptrack;
+ Icon=heaptrack
+ # FIXME: write docs!
+ # X-DocPath=kcachegrind/index.html
++StartupWMClass=heaptrack_gui
+ Terminal=false
+ Name=Heaptrack
+ Name[ca]=Heaptrack
+
+
+From 30ca42deadcb3bf0a73cd37806ceca5d290fbbce Mon Sep 17 00:00:00 2001
+From: "Friedrich W. H. Kossebau" <kossebau@kde.org>
+Date: Tue, 5 Feb 2019 04:23:58 +0100
+Subject: [PATCH] Add correct desktopfilename property for heaptrack_gui
+
+---
+ src/analyze/gui/gui.cpp | 5 ++++-
+ 1 file changed, 4 insertions(+), 1 deletion(-)
+
+diff --git a/src/analyze/gui/gui.cpp b/src/analyze/gui/gui.cpp
+index 96048b1..846d5fb 100644
+--- src/analyze/gui/gui.cpp
++++ src/analyze/gui/gui.cpp
+@@ -19,6 +19,7 @@
+ #include <QApplication>
+ #include <QCommandLineParser>
+ 
++#include <kcoreaddons_version.h>
+ #include <KAboutData>
+ #include <KLocalizedString>
+ 
+@@ -45,7 +46,9 @@ int main(int argc, char** argv)
+                         QStringLiteral("http://milianw.de"));
+ 
+     aboutData.setOrganizationDomain("kde.org");
+-
++#if KCOREADDONS_VERSION >= QT_VERSION_CHECK(5, 16, 0)
++    aboutData.setDesktopFileName(QStringLiteral("org.kde.heaptrack"));
++#endif
+     KAboutData::setApplicationData(aboutData);
+     app.setWindowIcon(QIcon::fromTheme(QStringLiteral("office-chart-area")));
+ 
diff --git a/srcpkgs/heaptrack/template b/srcpkgs/heaptrack/template
new file mode 100644
index 00000000000..958e0b27c1c
--- /dev/null
+++ b/srcpkgs/heaptrack/template
@@ -0,0 +1,28 @@
+# Template file for 'heaptrack'
+pkgname=heaptrack
+version=1.1.0
+revision=1
+build_style=cmake
+# one test fails
+configure_args="-DBUILD_TESTING=OFF"
+makedepends="boost-devel libunwind-devel zlib-devel libdwarf-devel
+ qt5-devel libzstd-devel ecm-devel"
+#gui depends
+hostmakedepends+="qt5-qmake qt5-host-tools extra-cmake-modules kcoreaddons"
+makedepends+=" ki18n-devel kitemmodels-devel kio-devel
+ kconfigwidgets-devel threadweaver-devel kdiagram-devel"
+depends="gdb"
+short_desc="Heap memory profiler for Linux"
+maintainer="Piraty <piraty1@inbox.ru>"
+license="LGPL-2.1-or-later"
+homepage="https://github.com/KDE/heaptrack"
+distfiles="https://github.com/KDE/heaptrack/archive/v${version}.tar.gz"
+checksum=bd247ac67d1ecf023ec7e2a2888764bfc03e2f8b24876928ca6aa0cdb3a07309
+
+heaptrack-gui_package() {
+	short_desc+=" - GUI"
+	pkg_install() {
+		vmove usr/bin/heaptrack_gui
+		vmove usr/share
+	}
+}
