Subject: [PATCH core-updates] gnu: rust: Add profiling support.

From: Milkey Mouse <[email-masked]>
Date: Wed, 19 May 2021 11:37:49 -0700

* gnu/packages/rust.scm (rust-1.49)[source]: Add patch.
[phases]{unpack-profiler-rt, enable-profiling}: New phases.
[native-inputs]{compiler-rt-source}: New input.
(rust-1.50)[source]: Remove "rust-1.49-llvm-cov-no-debug.patch".
* gnu/packages/patches/rust-1.49-llvm-cov-no-debug.patch: New file.
* gnu/local.mk (dist_patch_DATA): Add patch.
---
 gnu/local.mk                                  |  1 +
 .../patches/rust-1.49-llvm-cov-no-debug.patch | 27 +++++++++++++++
 gnu/packages/rust.scm                         | 33 +++++++++++++++++--
 3 files changed, 58 insertions(+), 3 deletions(-)
 create mode 100644 gnu/packages/patches/rust-1.49-llvm-cov-no-debug.patch

diff --git a/gnu/local.mk b/gnu/local.mk
index add01cf6d2..9dbf568d0c 100644
--- a/gnu/local.mk
+++ b/gnu/local.mk
@@ -1631,6 +1631,7 @@ dist_patch_DATA =						\
   %D%/packages/patches/rnp-disable-ruby-rnp-tests.patch		\
   %D%/packages/patches/rnp-unbundle-googletest.patch		\
   %D%/packages/patches/ruby-sanitize-system-libxml.patch	\
+  %D%/packages/patches/rust-1.49-llvm-cov-no-debug.patch	\
   %D%/packages/patches/rust-coresimd-doctest.patch		\
   %D%/packages/patches/rust-ndarray-remove-blas-src-dep.patch	\
   %D%/packages/patches/rust-nettle-disable-vendor.patch		 \
diff --git a/gnu/packages/patches/rust-1.49-llvm-cov-no-debug.patch b/gnu/packages/patches/rust-1.49-llvm-cov-no-debug.patch
new file mode 100644
index 0000000000..b72402a9a7
--- /dev/null
+++ b/gnu/packages/patches/rust-1.49-llvm-cov-no-debug.patch
@@ -0,0 +1,27 @@
+cherry-picked from commit fe56d267cae, in master as of 1.50
+diff --git a/src/test/run-make-fulldeps/coverage-reports-base/Makefile b/src/test/run-make-fulldeps/coverage-reports-base/Makefile
+index b175768e199..1e2aa056e40 100644
+--- a/src/test/run-make-fulldeps/coverage-reports-base/Makefile
++++ b/src/test/run-make-fulldeps/coverage-reports-base/Makefile
+@@ -13,15 +13,13 @@
+ BASEDIR=../coverage-reports-base
+ SOURCEDIR=../coverage
+ 
+-ifeq ($(UNAME),Darwin)
+-# FIXME(richkadel): It appears that --debug is not available on MacOS even when not running
+-# under CI.
+-NO_LLVM_ASSERTIONS=1
+-endif
+-
+ # The `llvm-cov show` flag `--debug`, used to generate the `counters` output files, is only enabled
+-# if LLVM assertions are enabled. Some CI builds disable debug assertions.
+-ifndef NO_LLVM_ASSERTIONS
++# if LLVM assertions are enabled. Requires Rust config `llvm/optimize` and not
++# `llvm/release_debuginfo`. Note that some CI builds disable debug assertions (by setting
++# `NO_LLVM_ASSERTIONS=1`), so it is not OK to fail the test, but `bless`ed test results cannot be
++# generated without debug assertions.
++LLVM_COV_DEBUG := $(shell "$(LLVM_BIN_DIR)"/llvm-cov show --debug 2>&1 | grep -q "Unknown command line argument '--debug'"; echo $$?)
++ifeq ($(LLVM_COV_DEBUG), 1)
+ DEBUG_FLAG=--debug
+ endif
+ 
diff --git a/gnu/packages/rust.scm b/gnu/packages/rust.scm
index 636420e25d..72355fa789 100644
--- a/gnu/packages/rust.scm
+++ b/gnu/packages/rust.scm
@@ -730,6 +730,10 @@ safety and thread safety guarantees.")
                     "0yf7kll517398dgqsr7m3gldzj0iwsp3ggzxrayckpqzvylfy2mm")))
     (package
       (inherit base-rust)
+      (source
+       (origin
+         (inherit (package-source base-rust))
+         (patches (search-patches "rust-1.49-llvm-cov-no-debug.patch"))))
       (outputs (cons "doc" (package-outputs base-rust)))
       (arguments
        (substitute-keyword-arguments (package-arguments base-rust)
@@ -817,11 +821,24 @@ safety and thread safety guarantees.")
                                 ((file) file))
                    (("fn ctrl_c_kills_everyone")
                     "#[ignore]\nfn ctrl_c_kills_everyone"))))
+             (add-after 'unpack 'unpack-profiler-rt
+               ;; Copy compiler-rt sources to where libprofiler_builtins
+               ;; looks for its vendored copy.
+               (lambda* (#:key inputs #:allow-other-keys)
+                 (mkdir-p "src/llvm-project/compiler-rt")
+                 (invoke "tar" "-xf" (assoc-ref inputs "compiler-rt-source")
+                   "-C" "src/llvm-project/compiler-rt" "--strip-components=1")
+                   #t))
              (add-after 'configure 'enable-docs
                (lambda _
                  (substitute* "config.toml"
                    (("docs = false")
                     "docs = true"))))
+             (add-after 'enable-codegen-tests 'enable-profiling
+               (lambda _
+                 (substitute* "config.toml"
+                   (("^profiler =.*$") "")
+                   (("[[]build[]]") "\n[build]\nprofiler = true\n"))))
              (add-after 'configure 'add-gdb-to-config
                (lambda* (#:key inputs #:allow-other-keys)
                  (let ((gdb (assoc-ref inputs "gdb")))
@@ -829,17 +846,27 @@ safety and thread safety guarantees.")
                      (("^python =.*" all)
                       (string-append all
                                      "gdb = \"" gdb "/bin/gdb\"\n"))))))))))
-      ;; Add test inputs.
       (native-inputs (cons*
+                      ;; Add test inputs.
                       ;; The tests fail when using GDB 10 (see:
                       ;; https://github.com/rust-lang/rust/issues/79009).
                       `("gdb" ,gdb-9.2)
                       `("procps" ,procps)
+                      ;; Add compiler-rt-source for libprofiler_builtins,
+                      ;; which typically vendors compiler-rt. Needed for PGO.
+                      `("compiler-rt-source" ,(package-source clang-runtime-11))
                       (package-native-inputs base-rust))))))
 
 (define-public rust-1.50
-  (rust-bootstrapped-package rust-1.49 "1.50.0"
-    "0pjs7j62maiyvkmhp9zrxl528g2n0fphp4rq6ap7aqdv0a6qz5wm"))
+  (let ((base-rust (rust-bootstrapped-package
+                    rust-1.49 "1.50.0"
+                    "0pjs7j62maiyvkmhp9zrxl528g2n0fphp4rq6ap7aqdv0a6qz5wm")))
+    (package
+      (inherit base-rust)
+      (source
+       (origin
+         (inherit (package-source base-rust))
+         (patches '()))))))
 
 (define-public rust-1.51
   (rust-bootstrapped-package rust-1.50 "1.51.0"
-- 
2.31.1