pkgsrc-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[pkgsrc/trunk]: pkgsrc/mk Create a standalone awk script, depends-depth-first...



details:   https://anonhg.NetBSD.org/pkgsrc/rev/876ee3eb002c
branches:  trunk
changeset: 506511:876ee3eb002c
user:      jlam <jlam%pkgsrc.org@localhost>
date:      Wed Jan 18 00:10:07 2006 +0000

description:
Create a standalone awk script, depends-depth-first.awk, that encapsulates
the code that performs the dependency graph traversal (in depth-first
fashion).  This script has a hook that allows executing a shell command
line upon visiting a dependency's package directory in either prefix
or postfix order, and may be used to simplify the code in bsd.pkg.mk
that iterates over dependencies.

This awk script requires the target "show-depends-pkgpaths", which is
defined in a new Makefile pkgsrc/mk/bsd.utils.mk.  This file should
accumulate "utility" targets that current exist in bsd.pkg.mk, i.e.,
"helper" targets for various actions.

diffstat:

 mk/bsd.pkg.mk                      |    4 +-
 mk/bsd.utils.mk                    |   34 ++++
 mk/scripts/depends-depth-first.awk |  273 +++++++++++++++++++++++++++++++++++++
 3 files changed, 310 insertions(+), 1 deletions(-)

diffs (truncated from 333 to 300 lines):

diff -r 628113703f9d -r 876ee3eb002c mk/bsd.pkg.mk
--- a/mk/bsd.pkg.mk     Tue Jan 17 23:52:17 2006 +0000
+++ b/mk/bsd.pkg.mk     Wed Jan 18 00:10:07 2006 +0000
@@ -1,4 +1,4 @@
-#      $NetBSD: bsd.pkg.mk,v 1.1788 2006/01/12 23:43:56 jlam Exp $
+#      $NetBSD: bsd.pkg.mk,v 1.1789 2006/01/18 00:10:07 jlam Exp $
 #
 # This file is in the public domain.
 #
@@ -4742,6 +4742,8 @@
 .include "../../mk/plist/bsd.plist.mk"
 .endif # _USE_PLIST_MODULE
 
+.include "../../mk/bsd.utils.mk"
+
 .include "../../mk/subst.mk"
 
 #
diff -r 628113703f9d -r 876ee3eb002c mk/bsd.utils.mk
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/mk/bsd.utils.mk   Wed Jan 18 00:10:07 2006 +0000
@@ -0,0 +1,34 @@
+# $NetBSD: bsd.utils.mk,v 1.1 2006/01/18 00:10:07 jlam Exp $
+#
+# This Makefile fragment is included by bsd.pkg.mk and defines utility
+# and otherwise miscellaneous variables and targets.
+#
+
+# DEPENDS_TYPE is used by the "show-depends-pkgpaths" target and specifies
+# which class of dependencies to output.  The special value "all" means
+# to output every dependency.
+#
+DEPENDS_TYPE?=  all
+.if !empty(DEPENDS_TYPE:Mbuild) || !empty(DEPENDS_TYPE:Mall)
+_ALL_DEPENDS_PKGPATHS+=        \
+       ${BUILD_DEPENDS:C/^[^:]*://:S/^..\/..\///:S/${PKGSRCDIR}\///}
+.endif
+.if !empty(DEPENDS_TYPE:Minstall) || !empty(DEPENDS_TYPE:Mpackage) || \
+    !empty(DEPENDS_TYPE:Mall)
+_ALL_DEPENDS_PKGPATHS+=        \
+       ${DEPENDS:C/^[^:]*://:S/^..\/..\///:S/${PKGSRCDIR}\///}
+.endif
+
+.PHONY: show-depends-pkgpaths
+show-depends-pkgpaths:
+.for _deppath_ in ${_ALL_DEPENDS_PKGPATHS:O:u}
+       @${ECHO} ${_deppath_}
+.endfor
+
+# _DEPENDS_DEPTH_FIRST_CMD holds the command (sans arguments) to traverse
+# the dependency graph for a package.
+#
+_DEPENDS_DEPTH_FIRST_CMD=                                              \
+       ${SETENV} ECHO=${TOOLS_ECHO:Q} MAKE=${MAKE:Q}                   \
+               PKGSRCDIR=${PKGSRCDIR:Q} TEST=${TOOLS_TEST:Q}           \
+       ${AWK} -f ${.CURDIR}/../../mk/scripts/depends-depth-first.awk --
diff -r 628113703f9d -r 876ee3eb002c mk/scripts/depends-depth-first.awk
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/mk/scripts/depends-depth-first.awk        Wed Jan 18 00:10:07 2006 +0000
@@ -0,0 +1,273 @@
+#!/usr/bin/awk -f
+#
+# $NetBSD: depends-depth-first.awk,v 1.1 2006/01/18 00:10:07 jlam Exp $
+#
+# Copyright (c) 2006 The NetBSD Foundation, Inc.
+# All rights reserved.
+#
+# This code is derived from software contributed to The NetBSD Foundation
+# by Johnny C. Lam.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions
+# are met:
+# 1. Redistributions of source code must retain the above copyright
+#    notice, this list of conditions and the following disclaimer.
+# 2. Redistributions in binary form must reproduce the above copyright
+#    notice, this list of conditions and the following disclaimer in the
+#    documentation and/or other materials provided with the distribution.
+# 3. All advertising materials mentioning features or use of this software
+#    must display the following acknowledgement:
+#        This product includes software developed by the NetBSD
+#        Foundation, Inc. and its contributors.
+# 4. Neither the name of The NetBSD Foundation nor the names of its
+#    contributors may be used to endorse or promote products derived
+#    from this software without specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+# POSSIBILITY OF SUCH DAMAGE.
+#
+
+######################################################################
+#
+# NAME
+#      depends-depth-first.awk -- traverse the dependency graph
+#
+# SYNOPSIS
+#      depends-depth-first.awk -- [options] pkgpath ...
+#
+# DESCRIPTION
+#      depends-depth-first.awk performs a depth-first traversal of the
+#      dependency graph associated with the package directories specified
+#      on the command line, and provides a hook to allow a shell command
+#      line to be executed within each package directory during traversal.
+#
+# OPTIONS
+#      The following command line arguments are supported.
+#
+#      --              This is a mandatory option and must always be the
+#                      first option specified.
+#
+#      -c cmdline      Execute the specified command line when visiting
+#                      a package directory during traversal.  If the -c
+#                      option is not given, then the default action is
+#                      to output the package directory name.
+#
+#      -d depends-type
+#                      When searching for a package's dependencies, use
+#                      the named "depends-type".  This is passed as the
+#                      DEPENDS_TYPE argument of the "show-depends-pkgpaths"
+#                      target.  By default, the dependency type is "all".
+#
+#      -o order        The dependencies are visited during the dependency
+#                      graph traversal in the named order, which is either
+#                      "prefix" or "postfix".  By default, dependencies
+#                      are visited in "postfix" order.
+#
+#      -r              If the -r option is specified, then the package
+#                      directories specified on the command-line are
+#                      also visited.
+#
+#      -s pkgsrcdir    Use the specified directory as the path to the
+#                      pkgsrc directory tree.  By default, this is the
+#                      value stored in the PKGSRCDIR environment variable.
+#
+# ENVIRONMENT
+#      PKGSRCDIR       This is the location of the pkgsrc directory tree,
+#                      which defaults to "/usr/pkgsrc".
+#
+######################################################################
+
+######################################################################
+#
+# Initialize global variables, parse the command-line arguments, and
+# invoke the main() function
+#
+######################################################################
+BEGIN {
+       ECHO = ENVIRON["ECHO"] ? ENVIRON["ECHO"] : "echo"
+       MAKE = ENVIRON["MAKE"] ? ENVIRON["MAKE"] : "make"
+       PKGSRCDIR = ENVIRON["PKGSRCDIR"] ? ENVIRON["PKGSRCDIR"] : "/usr/pkgsrc"
+       TEST = ENVIRON["TEST"] ? ENVIRON["TEST"] : "test"
+
+       self = "depends-depth-first.awk"
+       cmd_hook = ""
+       depends_type = "all"
+       do_root = 0
+       walk_order = "postfix"
+
+       ARGSTART = 1
+       parse_options()
+       main()
+}
+
+######################################################################
+#
+# usage()
+#      Output the usage message to standard error.
+#
+######################################################################
+function usage() {
+       print "usage: " self " [-- [-c cmdline] [-d depends-type] [-o order] [-r] [-s pkgsrcdir]] [pkgpath ...]" > "/dev/stderr"
+}
+
+######################################################################
+#
+# parse_options()
+#      Adjust global variables based on the options passed on the
+#      command line.  After this function exists, ARGSTART is set
+#      to the index in the ARGV array of the first package directory
+#      in which to begin the traversal.
+#
+######################################################################
+function parse_options(                option) {
+       if (ARGV[ARGSTART] == "--") {
+               ARGSTART++
+       }
+       while (ARGSTART < ARGC) {
+               option = ARGV[ARGSTART]
+               if (option == "-c") {
+                       cmd_hook = ARGV[ARGSTART + 1]
+                       ARGSTART += 2
+               } else if (option == "-d") {
+                       depends_type = ARGV[ARGSTART + 1]
+                       ARGSTART += 2
+               } else if (option == "-o") {
+                       walk_order = ARGV[ARGSTART + 1]
+                       ARGSTART += 2
+               } else if (option == "-r") {
+                       do_root = 1
+                       ARGSTART++
+               } else if (option == "-s") {
+                       PKGSRCDIR = ARGV[ARGSTART + 1]
+                       ARGSTART += 2
+               } else if (option == "--") {
+                       ARGSTART++
+                       break;
+               } else if (match(option, /^-.*/) != 0) {
+                       option = substr(option, RSTART + 1, RLENGTH)
+                       print self ": unknown option -- " option > "/dev/stderr"
+                       usage()
+                       exit 1
+               } else {
+                       ARGSTART++
+               }
+       }
+       if (walk_order !~ /prefix|postfix/) {
+               print self ": unknown walk order -- " walk_order > "/dev/stderr"
+               usage()
+               exit 1
+       }
+}
+
+######################################################################
+#
+# main()
+#      This provides an implementation of the well-known non-recursive
+#      algorithm for depth-first-traversal of a graph, but with a
+#      small modification to allow visiting the nodes (package directories)
+#      in either "prefix" or "postfix" order.  This more closely mimics
+#      a function stack than the usual non-recursive DFS algorithm.
+#
+######################################################################
+function main(         cmd, depends_pkgpath, dir, pkgpath) {
+       #
+       # Push the given package directories onto the stack.
+       while (ARGC >= ARGSTART) {
+               ARGC--;
+               cmd = TEST " -d " PKGSRCDIR "/" ARGV[ARGC]
+               if (system(cmd) == 0) {
+                       if (do_root == 0) {
+                               root[ARGV[ARGC]] = ARGV[ARGC]
+                       }
+                       push(dir_stack, ARGV[ARGC])
+               }
+       }
+
+       # Depth-first traversal of dependency graph.
+       while (!empty(dir_stack)) {
+               pkgpath = top(dir_stack)
+               if (status[pkgpath] == "walked") {
+                       if (walk_order == "postfix") {
+                               visit(pkgpath)
+                       }
+                       pop(dir_stack)
+                       continue
+               }
+               status[pkgpath] = "walked"
+               if (walk_order == "prefix") {
+                       visit(pkgpath)
+               }
+
+               # Grab the "depends_type" dependencies of the current
+               # package and push them onto the stack.  We use the
+               # "show-depends-pkgpaths" target to fetch this information.
+               #
+               dir = PKGSRCDIR "/" pkgpath
+               cmd = "if " TEST " -d " dir "; then cd " dir " && " MAKE " show-depends-pkgpaths DEPENDS_TYPE=\"" depends_type "\"; fi"
+               while (cmd | getline depends_pkgpath) {
+                       if (status[depends_pkgpath] == "") {
+                               status[depends_pkgpath] = "pushed"
+                               push(dir_stack, depends_pkgpath)
+                       }
+               }
+               close(cmd)
+       }
+       exit 0
+}
+
+######################################################################
+#
+# visit(pkgpath)
+#      Visit the package directory by running the shell command
+#      specified on the command line and stored in "cmd_hook".
+#      If "cmd_hook" is empty, then just print the package directory
+#      name.
+#
+######################################################################
+function visit(pkgpath,                cmd, dir) {
+       if ((do_root == 0) && (root[pkgpath] != "")) {
+               return
+       }



Home | Main Index | Thread Index | Old Index