Rework of debian packaging

Change-Id: Ifede85d3af36f3ee6c6f8f92dcf5db0ed8f1bfeb
Signed-off-by: Damjan Marion <damarion@cisco.com>
Signed-off-by: Dave Barach <dave@barachs.net>
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 09d132a..05ecd0b 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -97,7 +97,7 @@
   find_package(OpenSSL REQUIRED)
   set(SUBDIRS
     vppinfra svm vlib vlibmemory vlibapi vnet vpp vat vcl plugins
-    vpp-api tools/vppapigen tools/g2 tools/elftool tools/perftool cmake
+    vpp-api tools/vppapigen tools/g2 tools/elftool tools/perftool cmake pkg
   )
 elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "Darwin")
   set(SUBDIRS vppinfra)
diff --git a/src/cmake/plugin.cmake b/src/cmake/plugin.cmake
index 1cff910..af3033f 100644
--- a/src/cmake/plugin.cmake
+++ b/src/cmake/plugin.cmake
@@ -21,7 +21,7 @@
   set(plugin_name ${name}_plugin)
   set(api_includes)
   if(NOT PLUGIN_COMPONENT)
-    set(PLUGIN_COMPONENT vpp-plugin-misc)
+    set(PLUGIN_COMPONENT vpp-plugin-core)
   endif()
   if(NOT PLUGIN_DEV_COMPONENT)
     if(NOT VPP_EXTERNAL_PROJECT)
@@ -92,4 +92,3 @@
     COMPONENT ${PLUGIN_COMPONENT}
   )
 endmacro()
-
diff --git a/src/pkg/CMakeLists.txt b/src/pkg/CMakeLists.txt
new file mode 100644
index 0000000..a725e66
--- /dev/null
+++ b/src/pkg/CMakeLists.txt
@@ -0,0 +1,43 @@
+# Copyright (c) 2019 Cisco and/or its affiliates.
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at:
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+get_cmake_property(VPP_COMPONENTS COMPONENTS)
+string(REPLACE ";" " " VPP_COMPONENTS "${VPP_COMPONENTS}")
+
+execute_process(
+  COMMAND date -R
+  OUTPUT_VARIABLE TIMESTAMP
+  OUTPUT_STRIP_TRAILING_WHITESPACE
+)
+
+foreach(f rules changelog)
+  configure_file(
+    ${CMAKE_CURRENT_SOURCE_DIR}/debian/${f}.in
+    ${CMAKE_BINARY_DIR}/debian/${f}
+  )
+endforeach()
+
+foreach(f control copyright vpp.postinst vpp.postrm vpp.postinst vpp.service)
+  file(COPY
+    ${CMAKE_CURRENT_SOURCE_DIR}/debian/${f}
+    DESTINATION ${CMAKE_BINARY_DIR}/debian
+  )
+endforeach()
+
+file(WRITE ${CMAKE_BINARY_DIR}/debian/compat "9\n")
+
+add_custom_target(package-deb
+  COMMENT "Building .deb packages..."
+  WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
+  COMMAND "dpkg-buildpackage" "-us" "-uc" "-b"
+)
diff --git a/src/pkg/debian/changelog.in b/src/pkg/debian/changelog.in
new file mode 100644
index 0000000..4d9b87a
--- /dev/null
+++ b/src/pkg/debian/changelog.in
@@ -0,0 +1,6 @@
+vpp (@VPP_VERSION@) unstable; urgency=low
+
+  * no description
+
+ -- fd.io VPP <vpp-dev@fd.io>  @TIMESTAMP@
+
diff --git a/src/pkg/debian/control b/src/pkg/debian/control
new file mode 100644
index 0000000..c9d4c46
--- /dev/null
+++ b/src/pkg/debian/control
@@ -0,0 +1,75 @@
+Source: vpp
+Section: net
+Priority: extra
+Maintainer: fd.io VPP Packaging Team <vpp-dev@fd.io>
+Build-Depends: debhelper (>= 9),
+               dh-systemd,
+	       dh-python,
+	       python-all
+Standards-Version: 3.9.4
+
+Package: vpp
+Architecture: any
+Depends: libvppinfra (= ${source:Version}),
+         ${shlibs:Depends},
+	 ${misc:Depends},
+	 ${python:Depends}
+Description: Vector Packet Processing--executables
+ This package provides VPP executables: vpp, vpp_api_test, vpp_json_test
+ vpp - the vector packet engine
+ vpp_api_test - vector packet engine API test tool
+ vpp_json_test - vector packet engine JSON test tool
+
+Package: vpp-dbg
+Architecture: any
+Depends: ${misc:Depends}
+Description: Vector Packet Processing--debug symbols
+
+Package: vpp-dev
+Architecture: any
+Depends: libvppinfra-dev (= ${source:Version}),
+         ${misc:Depends},
+	 ${python:Depends}
+Description: Vector Packet Processing--development support
+ This package contains development support files for the VPP libraries
+ .
+
+Package: libvppinfra
+Architecture: any
+Depends: ${shlibs:Depends},
+         ${misc:Depends}
+Description: Vector Packet Processing--runtime libraries
+ This package contains the VPP shared libraries, including:
+ .
+
+Package: libvppinfra-dev
+Architecture: any
+Depends: ${misc:Depends}
+Description: Vector Packet Processing--runtime libraries
+ This package contains the VPP shared libraries, including:
+ .
+
+Package: vpp-plugin-core
+Architecture: any
+Depends: vpp (= ${source:Version}),
+         ${misc:Depends}
+Description: Vector Packet Processing--runtime core plugins
+ This package contains VPP core plugins
+ .
+
+Package: vpp-plugin-dpdk
+Architecture: any
+Depends: vpp (= ${source:Version}),
+         ${misc:Depends}
+Description: Vector Packet Processing--runtime dpdk plugin
+ This package contains the VPP dpdk plugin
+ .
+
+Package: vpp-api-python
+Architecture: any
+Depends: vpp (= ${source:Version}),
+         ${python2:Depends},
+	 ${misc:Depends}
+Description: VPP Python API bindings
+  This package contains VPP python api bindings
+  .
diff --git a/src/pkg/debian/copyright b/src/pkg/debian/copyright
new file mode 100644
index 0000000..f9775c1
--- /dev/null
+++ b/src/pkg/debian/copyright
@@ -0,0 +1,9 @@
+Format: http://www.debian.org/doc/packaging-manuals/copyright-format/1.0/
+Upstream-Name: optional.
+Upstream-Contact: optional.
+Source: optional.
+Disclaimer: optional.
+Comment: optional.
+License: Apache-2.0
+Copyright: 2015 Cisco and/or its affiliates and others.
+
diff --git a/src/pkg/debian/rules.in b/src/pkg/debian/rules.in
new file mode 100755
index 0000000..4d2bb40
--- /dev/null
+++ b/src/pkg/debian/rules.in
@@ -0,0 +1,45 @@
+#!/usr/bin/make -f
+# See debhelper(7) (uncomment to enable)
+# output every command that modifies files on the build system.
+DH_VERBOSE = 1
+
+# see EXAMPLES in dpkg-buildflags(1) and read /usr/share/dpkg/*
+DPKG_EXPORT_BUILDFLAGS = 1
+include /usr/share/dpkg/default.mk
+
+export PYBUILD_NAME = vpp-api-python
+export PYBUILD_DIR = @CMAKE_SOURCE_DIR@/vpp-api/python
+export PYBUILD_DESTDIR_python2=debian/vpp-api-python/
+export PYBUILD_DISABLE_python2=test
+export PYBUILD_SYSTEM=distutils
+
+# main packaging script based on dh7 syntax
+%:
+	dh $@ --with systemd,python2 --buildsystem=pybuild
+
+override_dh_shlibdeps:
+	@dh_shlibdeps -X_plugin  2>&1 \
+	  | { grep -v -e "probably a plugin" || true; }
+
+override_dh_strip:
+	dh_strip --dbg-package=vpp-dbg
+
+DEB_HOST_MULTIARCH = $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)
+
+override_dh_install:
+	for c in @VPP_COMPONENTS@; do \
+	  @CMAKE_COMMAND@ \
+	    -D CMAKE_INSTALL_CONFIG_NAME=@CMAKE_BUILD_TYPE@ \
+	    -D CMAKE_INSTALL_COMPONENT=$$c \
+	    -D CMAKE_INSTALL_PREFIX=@CMAKE_BINARY_DIR@/debian/$$c \
+	    -P @CMAKE_BINARY_DIR@/cmake_install.cmake ; \
+	  if [ -d debian/$$c/lib ] ; then \
+	    mv debian/$$c/lib debian/$$c/$(DEB_HOST_MULTIARCH) ; \
+	    mkdir -p debian/$$c/usr/lib ; \
+	    mv debian/$$c/$(DEB_HOST_MULTIARCH) debian/$$c/usr/lib ; \
+	  fi ; \
+	  if [ -d debian/$$c/bin ] ; then \
+	    mv debian/$$c/bin debian/$$c/usr/bin ; \
+	  fi ; \
+	  @CMAKE_SOURCE_DIR@/scripts/remove-rpath debian/$$c ; \
+	done
diff --git a/src/pkg/debian/vpp.postinst b/src/pkg/debian/vpp.postinst
new file mode 100644
index 0000000..78fcac2
--- /dev/null
+++ b/src/pkg/debian/vpp.postinst
@@ -0,0 +1,8 @@
+#!/bin/sh -e
+
+# try to set the required values now. This may or may not work.
+sysctl --system
+
+#DEBHELPER#
+
+exit 0
diff --git a/src/pkg/debian/vpp.postrm b/src/pkg/debian/vpp.postrm
new file mode 100644
index 0000000..24b4842
--- /dev/null
+++ b/src/pkg/debian/vpp.postrm
@@ -0,0 +1,21 @@
+#!/bin/sh -e
+
+removed=
+
+# Unbind user-mode PCI drivers
+pci_dirs=`find /sys/bus/pci/drivers -type d -name igb_uio -o -name uio_pci_generic -o -name vfio-pci`
+for d in $pci_dirs; do
+    for f in ${d}/*; do
+        [ -e "${f}/config" ] || continue
+        echo ${f##*/} > ${d}/unbind
+        basename `dirname ${f}` | xargs echo -n "Removing driver"; echo " for PCI ID" `basename ${f}`
+        removed=y
+    done
+done
+if [ -n "${removed}" ]; then
+    echo "There are changes in PCI drivers, rescaning"
+    echo 1 > /sys/bus/pci/rescan
+else
+    echo "There weren't PCI devices binded"
+fi
+
diff --git a/src/pkg/debian/vpp.preinst b/src/pkg/debian/vpp.preinst
new file mode 100644
index 0000000..d33cacf
--- /dev/null
+++ b/src/pkg/debian/vpp.preinst
@@ -0,0 +1,4 @@
+#!/bin/sh -e
+
+# Add the vpp group
+groupadd -f -r vpp
diff --git a/src/pkg/debian/vpp.service b/src/pkg/debian/vpp.service
new file mode 100644
index 0000000..2e86941
--- /dev/null
+++ b/src/pkg/debian/vpp.service
@@ -0,0 +1,13 @@
+[Unit]
+Description=vector packet processing engine
+After=network.target
+
+[Service]
+Type=simple
+ExecStartPre=-/sbin/modprobe uio_pci_generic
+ExecStart=/usr/bin/vpp -c /etc/vpp/startup.conf
+ExecStopPost=/bin/rm -f /dev/shm/db /dev/shm/global_vm /dev/shm/vpe-api
+Restart=always
+
+[Install]
+WantedBy=multi-user.target
diff --git a/src/scripts/remove-rpath b/src/scripts/remove-rpath
new file mode 100755
index 0000000..3e20b06
--- /dev/null
+++ b/src/scripts/remove-rpath
@@ -0,0 +1,37 @@
+#!/bin/bash
+
+# Copyright (c) 2015 Cisco and/or its affiliates.
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at:
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+if [ -z $1 ]; then
+	echo "Please specify path"
+	exit 1
+fi
+
+which chrpath &> /dev/null
+
+if [ $? -ne 0 ] ; then
+	echo "Please install chrpath tool"
+	exit 1
+fi
+
+libs=$(find $1 -type f -name \*.so\*)
+execs=$(find $1 -type f -path \*/bin/\* )
+
+for i in $libs $execs; do
+	chrpath $i 2> /dev/null | grep -q build-root
+	if [ $? -eq 0 ] ; then
+		chrpath -d $i
+	fi
+done
+
diff --git a/src/vat/main.c b/src/vat/main.c
index 295ccec..a542114 100644
--- a/src/vat/main.c
+++ b/src/vat/main.c
@@ -15,6 +15,7 @@
 #include "vat.h"
 #include "plugin.h"
 #include <signal.h>
+#include <limits.h>
 
 vat_main_t vat_main;
 
@@ -283,6 +284,37 @@
     }
 }
 
+static void
+vat_find_plugin_path ()
+{
+  extern char *vat_plugin_path;
+  char *p, path[PATH_MAX];
+  int rv;
+  u8 *s;
+
+  /* find executable path */
+  if ((rv = readlink ("/proc/self/exe", path, PATH_MAX - 1)) == -1)
+    return;
+
+  /* readlink doesn't provide null termination */
+  path[rv] = 0;
+
+  /* strip filename */
+  if ((p = strrchr (path, '/')) == 0)
+    return;
+  *p = 0;
+
+  /* strip bin/ */
+  if ((p = strrchr (path, '/')) == 0)
+    return;
+  *p = 0;
+
+  s = format (0, "%s/lib/" CLIB_TARGET_TRIPLET "/vpp_api_test_plugins:"
+	      "%s/lib/vpp_api_test_plugins", path, path);
+  vec_add1 (s, 0);
+  vat_plugin_path = (char *) s;
+}
+
 int
 main (int argc, char **argv)
 {
@@ -309,6 +341,8 @@
   vec_validate (vam->cmd_reply, 0);
   vec_reset_length (vam->cmd_reply);
 
+  vat_find_plugin_path ();
+
   unformat_init_command_line (a, argv);
 
   while (unformat_check_input (a) != UNFORMAT_END_OF_INPUT)