Subject: [PATCH] gnu: Add riscv-openocd.

From: Malte Frank Gerdes <[email-masked]>
Date: Wed, 07 Oct 2020 22:02:09 +0200

Hi,

this adds OpenOCD with RISC-V support to Guix.

There is a problem though: riscv-openocd bundles libjaylink, which is
also available as a package. The available package cannot be used
because it is too old and riscv-openocd has riscv-specific additions
made to it. So the question is: does this justify using the bundled
library or should I do something? (if yes, what?)

I have successfully used the package to communicate via JTAG with a
Sipeed Nano and a JLink V8.

I built this package successfully on: x86_64-linux, aarch64-linux

Malte

>From ccbb612dcf87e2e628febfee74f893726b6ca309 Mon Sep 17 00:00:00 2001
From: Malte Frank Gerdes <[email-masked]>
Date: Wed, 7 Oct 2020 20:27:51 +0200
Subject: [PATCH] gnu: Add riscv-openocd.

* gnu/packages/embedded.scm (riscv-openocd): New variable.
* gnu/packages/patches/riscv-openocd-reproducibility.patch: New file.
* gnu/local.mk (dist_patch_DATA): Register patch.
---
 gnu/local.mk                                  |  2 +
 gnu/packages/embedded.scm                     | 80 +++++++++++++++++++
 .../riscv-openocd-reproducibility.patch       | 57 +++++++++++++
 3 files changed, 139 insertions(+)
 create mode 100644 gnu/packages/patches/riscv-openocd-reproducibility.patch

diff --git a/gnu/local.mk b/gnu/local.mk
index f3b5b17e84..4fa460d4a1 100644
--- a/gnu/local.mk
+++ b/gnu/local.mk
@@ -37,6 +37,7 @@
 # Copyright © 2020 Brice Waegeneire <[email-masked]>
 # Copyright © 2020 Tanguy Le Carrour <[email-masked]>
 # Copyright © 2020 Martin Becze <[email-masked]>
+# Copyright © 2020 Malte Frank Gerdes <[email-masked]>
 #
 # This file is part of GNU Guix.
 #
@@ -1407,6 +1408,7 @@ dist_patch_DATA =						\
   %D%/packages/patches/pango-skip-libthai-test.patch		\
   %D%/packages/patches/pciutils-hurd-configure.patch		\
   %D%/packages/patches/ppsspp-disable-upgrade-and-gold.patch		\
+  %D%/packages/patches/riscv-openocd-reproducibility.patch		\
   %D%/packages/patches/samba-fix-fcntl-hint-detection.patch		\
   %D%/packages/patches/sdl-pango-api_additions.patch		\
   %D%/packages/patches/sdl-pango-blit_overflow.patch		\
diff --git a/gnu/packages/embedded.scm b/gnu/packages/embedded.scm
index f9f0e04238..89fdf81349 100644
--- a/gnu/packages/embedded.scm
+++ b/gnu/packages/embedded.scm
@@ -7,6 +7,7 @@
 ;;; Copyright © 2018, 2019 Clément Lassieur <[email-masked]>
 ;;; Copyright © 2020 Marius Bakke <[email-masked]>
 ;;; Copyright © 2020 Björn Höfling <[email-masked]>
+;;; Copyright © 2020 Malte Frank Gerdes <[email-masked]>
 ;;;
 ;;; This file is part of GNU Guix.
 ;;;
@@ -38,6 +39,7 @@
   #:use-module (gnu packages)
   #:use-module (gnu packages autotools)
   #:use-module ((gnu packages base) #:prefix base:)
+  #:use-module (gnu packages bash)
   #:use-module (gnu packages bison)
   #:use-module (gnu packages cross-base)
   #:use-module (gnu packages dejagnu)
@@ -54,6 +56,7 @@
   #:use-module (gnu packages python-xyz)
   #:use-module (gnu packages swig)
   #:use-module (gnu packages texinfo)
+  #:use-module (gnu packages version-control)
   #:use-module (gnu packages xorg)
   #:use-module (srfi srfi-1))
 
@@ -580,6 +583,83 @@ language.")
 with a layered architecture of JTAG interface and TAP support.")
     (license license:gpl2+)))
 
+(define-public riscv-openocd
+  (let ((commit "675259382bacade0d4cc44d1944be2aa6a893adc")
+        (revision "0"))
+    (package
+      (name "riscv-openocd")
+      (version (string-append "0.10.0-" revision "."
+                              (string-take commit 7)))
+      (source (origin
+                (method git-fetch)
+                (uri (git-reference
+                     (url "https://github.com/riscv/riscv-openocd")
+                     (commit commit)))
+                (sha256
+                 (base32
+                  "083xyx2dq2f9yfkjpl8gm0icn96im1kip479y85nxl0z9ih7fxbf"))
+                (patches
+                 (search-patches "riscv-openocd-reproducibility.patch"))))
+      (build-system gnu-build-system)
+      (native-inputs
+       `(("autoconf" ,autoconf)
+         ("automake" ,automake)
+         ("which" ,base:which)
+         ("libtool" ,libtool)
+         ("pkg-config" ,pkg-config)
+         ("bash-minimal" ,bash-minimal)
+         ("texinfo" ,texinfo)))
+      (inputs
+       `(("hidapi" ,hidapi)
+         ("jimtcl" ,jimtcl)
+         ("libftdi" ,libftdi)
+         ("libusb-compat" ,libusb-compat)))
+      (arguments
+       '(#:configure-flags
+         (append (list "--disable-werror"
+                       "--enable-sysfsgpio"
+                       "--disable-internal-jimtcl")
+                 (map (lambda (programmer)
+                        (string-append "--enable-" programmer))
+                      '("amtjtagaccel" "armjtagew" "buspirate" "ftdi"
+                        "gw16012" "jlink" "opendous" "osbdm"
+                        "parport" "aice" "cmsis-dap" "dummy" "jtag_vpi"
+                        "remote-bitbang" "rlink" "stlink" "ti-icdi" "ulink"
+                        "usbprog" "vsllink" "usb-blaster-2" "usb_blaster"
+                        "presto" "openjtag")))
+         #:phases
+         (modify-phases %standard-phases
+           (replace 'bootstrap
+             (lambda _
+               (chmod "src/jtag/drivers/libjaylink/autogen.sh" #o644)
+               (invoke "sh" "bootstrap" "nosubmodule")))
+           (add-after 'bootstrap 'bootstrap-jaylink
+             ;; we are using the bundled libjaylink here, because the original
+             ;; repo (https://git.zapb.de/libjaylink.git) is now behind a login
+             ;; and cannot be cloned anymore. This package has custom additions
+             ;; to libjaylink.
+             (lambda _
+               (with-directory-excursion "src/jtag/drivers/libjaylink"
+                 (patch-shebang "autogen.sh")
+                 (invoke "sh"  "autogen.sh"))))
+           (add-before 'configure 'change-udev-group
+             (lambda _
+               (substitute* "contrib/60-openocd.rules"
+                 (("plugdev") "dialout"))
+               #t))
+           (add-after 'install 'install-udev-rules
+             (lambda* (#:key outputs #:allow-other-keys)
+               (install-file "contrib/60-openocd.rules"
+                             (string-append
+                              (assoc-ref outputs "out")
+                              "/lib/udev/rules.d/"))
+               #t)))))
+      (home-page "http://openocd.org")
+      (synopsis "On-Chip Debugger with RISC-V Support.")
+      (description "OpenOCD provides on-chip programming and debugging support
+with a layered architecture of JTAG interface and TAP support.")
+      (license license:gpl2+))))
+
 ;; The commits for all propeller tools are the stable versions published at
 ;; https://github.com/propellerinc/propgcc in the release_1_0.  According to
 ;; personal correspondence with the developers in July 2017, more recent
diff --git a/gnu/packages/patches/riscv-openocd-reproducibility.patch b/gnu/packages/patches/riscv-openocd-reproducibility.patch
new file mode 100644
index 0000000000..3f7227e7a3
--- /dev/null
+++ b/gnu/packages/patches/riscv-openocd-reproducibility.patch
@@ -0,0 +1,57 @@
+diff --git a/Makefile.am b/Makefile.am
+index fcfd554c2..55088fef8 100644
+--- a/Makefile.am
++++ b/Makefile.am
+@@ -94,9 +94,7 @@ TCL_FILES = find $(srcdir)/$(TCL_PATH) -name '*.cfg' -o -name '*.tcl' -o -name '
+ 		sed -e 's,^$(srcdir)/$(TCL_PATH),,'
+
+ dist-hook:
+-	if test -d $(srcdir)/.git -a \( ! -e $(distdir)/ChangeLog -o -w $(distdir)/ChangeLog \) ; then \
+-		git --git-dir $(srcdir)/.git log | $(srcdir)/tools/git2cl/git2cl > $(distdir)/ChangeLog ; \
+-	fi
++
+ 	for i in $$($(TCL_FILES)); do \
+ 		j="$(distdir)/$(TCL_PATH)/$$i" && \
+ 		mkdir -p "$$(dirname $$j)" && \
+@@ -122,7 +120,7 @@ distclean-local:
+ # instead we have a hook that enforces this in each workspace. To make sure
+ # that users actually use those hooks, we point git at them here.
+ all-local:
+-	cd $(srcdir) && git config core.hooksPath ./git-hooks
++	echo "no git"
+
+ DISTCLEANFILES = doxygen.log
+
+diff --git a/doc/Makefile.am b/doc/Makefile.am
+index 67592038d..3a2e86049 100644
+--- a/doc/Makefile.am
++++ b/doc/Makefile.am
+@@ -4,8 +4,3 @@ info_TEXINFOS += %D%/openocd.texi
+ dist_man_MANS += %D%/openocd.1
+
+ EXTRA_DIST += %D%/manual
+-
+-MAINTAINERCLEANFILES += \
+-	%D%/mdate-sh \
+-	%D%/stamp-vti \
+-	%D%/version.texi
+diff --git a/src/Makefile.am b/src/Makefile.am
+index 07981aa67..98e0c98c1 100644
+--- a/src/Makefile.am
++++ b/src/Makefile.am
+@@ -22,14 +22,9 @@ endif
+
+ # banner output includes RELSTR appended to $VERSION from the configure script
+ # guess-rev.sh returns either a repository version ID or "-snapshot"
+-if RELEASE
+ %C%_libopenocd_la_CPPFLAGS += -DRELSTR=\"\"
+ %C%_libopenocd_la_CPPFLAGS += -DGITVERSION=\"\"
+-else
+-%C%_libopenocd_la_CPPFLAGS += -DRELSTR=\"`$(top_srcdir)/guess-rev.sh $(top_srcdir)`\"
+-%C%_libopenocd_la_CPPFLAGS += -DGITVERSION=\"`cd $(top_srcdir) && git describe`\"
+-%C%_libopenocd_la_CPPFLAGS += -DPKGBLDDATE=\"`date +%F-%R`\"
+-endif
++
+
+ # add default CPPFLAGS
+ %C%_libopenocd_la_CPPFLAGS += $(AM_CPPFLAGS) $(CPPFLAGS)
-- 
2.28.0