Commit 008ca645 authored by Karl Wette's avatar Karl Wette

Various improvements to user environment scripts

- Require sed, which greatly simplifies output shell code. Use
  ./configure to find sed, then pass value of $(SED) to awk script.
- Delete output user env. script if awk script fails, just to
  make sure it gets re-built.
- Call sed only one per path to reduce overhead
- Using awk functions for printing
Original: e77e130e476994ca7eb9d3620bbbec781508f6ae
parent a3e8e255
# Environment script generation
# Author: Karl Wette, 2011
# before processing any file
# print an error message to standard error, and exit
function msg(str) {
print "generate_user_env.awk: " str >"/dev/stderr"
exit 1
}
# print a string to output file
function printout(str, first) {
if (first) {
print str >output
}
else {
print str >>output
}
}
# script setup
BEGIN {
prompt = "user_env.awk:"
# check that required variables were set on command line
if (SED == "") {
msg("no value for 'SED' given on command line")
}
if (package == "") {
print prompt, "no value for 'package' given on command line" >"/dev/stderr"
exit 1
msg("no value for 'package' given on command line")
}
if (output == "") {
print prompt, "no value for 'output' given on command line" >"/dev/stderr"
exit 1
msg("no value for 'output' given on command line")
}
sourcecount = 0
}
# source another environment setup file
# syntax: source FILE
$1 == "source" {
++sourcecount
sourcefiles[sourcecount] = $2
for (i = 2; i <= NF; ++i) {
sourcefiles[$i] = $i
}
}
# set a environment variable to a given value or path
......@@ -29,8 +45,7 @@ $1 == "source" {
$1 == "set" {
name = $2
if (name == "") {
print prompt, "no name given to prepend" > "/dev/stderr"
exit 1
msg("no name given to prepend")
}
setvars[name] = ""
value = ""
......@@ -50,8 +65,7 @@ $1 == "set" {
$1 == "prepend" {
name = $2
if (name == "") {
print prompt, "no name given to prepend" > "/dev/stderr"
exit 1
msg("no name given to prepend")
}
if (pathvars[name] == "") {
pathvars[name] = sprintf("${%s}", name)
......@@ -73,8 +87,7 @@ $1 == "prepend" {
$1 == "append" {
name = $2
if (name == "") {
print prompt, "no name given to prepend" > "/dev/stderr"
exit 1
msg("no name given to prepend")
}
if (pathvars[name] == "") {
pathvars[name] = sprintf("${%s}", name)
......@@ -95,69 +108,46 @@ $1 == "append" {
END {
# if output file ends in 'csh', use C shell syntax, otherwise Bourne shell syntax
csh = (output ~ /\.csh$/)
# print usage
printf "# source this file to access '%s'\n", package >output
printf "# usage:\n" >>output
printf "# source %s\n", output >>output
# output usage
printout("# source this file to access '" package "'", 1)
# output source files
for (i = 1; i <= sourcecount; ++i) {
for (sourcefile in sourcefiles) {
if (csh) {
printf "if ( -f %s.csh ) then\n", sourcefiles[i] >>output
printf " source %s.csh $argv:q\n", sourcefiles[i] >>output
printf "endif\n" >>output
printout("source " sourcefile ".csh")
}
else {
printf "if [ -f %s.sh ]; then\n", sourcefiles[i] >>output
printf " source %s.sh \"$@\"\n", sourcefiles[i] >>output
printf "fi\n" >>output
printout(". " sourcefile ".sh")
}
}
# output set variables
for (name in setvars) {
if (csh) {
printf "setenv %s \"%s\"\n", name, setvars[name] >>output
printout("setenv " name " \"" setvars[name] "\"")
}
else {
printf "%s=\"%s\"\n", name, setvars[name] >>output
printf "export %s\n", name >>output
printout(name "=\"" setvars[name] "\"")
printout("export " name)
}
}
# output prepend/append variables
# - use sed, if available, to first remove path
# elements from path variable if already present
for (name in pathvars) {
if (csh) {
printf "if ( ! ${?%s} ) setenv %s\n", name, name >>output
printf "which sed >&/dev/null\n" >>output
printf "if ( ! $status ) then\n" >>output
}
else {
printf "type -p sed >/dev/null\n" >>output
printf "if [ $? -eq 0 ]; then\n" >>output
}
sed_script = ""
split(pathvars[name], pathvar, ":")
for (i in pathvar) {
if (substr(pathvar[i], 1, 1) != "$") {
if (csh) {
printf " setenv %s `echo \":${%s}:\" | sed 's|:%s:|:|;s|^:||;s|:$||'`\n", name, name, pathvar[i] >>output
}
else {
printf " %s=`echo \":${%s}:\" | sed 's|:%s:|:|;s|^:||;s|:$||'`\n", name, name, pathvar[i] >>output
}
sed_script = sed_script "s|:" pathvar[i] ":|:|;"
}
}
sed_script = sed_script "s|::*|:|g;s|^:||;s|:$||"
if (csh) {
printf " setenv %s `echo \"%s\" | sed 's|^:||;s|:$||'`\n", name, pathvars[name] >>output
printf "else\n" >>output
printf " setenv %s \"%s\"\n", name, pathvars[name] >>output
printf "endif\n" >>output
printout("if ( ! ${?" name "} ) setenv " name)
printout("setenv " name " `echo \":${" name "}:\" | " SED " '" sed_script "'`")
printout("setenv " name " \"" pathvars[name] "\"")
}
else {
printf " %s=`echo \"%s\" | sed 's|^:||;s|:$||'`\n", name, pathvars[name] >>output
printf "else\n" >>output
printf " %s=\"%s\"\n", name, pathvars[name] >>output
printf "fi\n" >>output
printf "export %s\n", name >>output
printout(name "=`echo \":${" name "}:\" | " SED " '" sed_script "'`")
printout(name "=\"" pathvars[name] "\"")
printout("export " name)
}
}
}
......@@ -208,6 +208,7 @@ AC_PROG_CPP
AC_PROG_INSTALL
AC_PROG_LN_S
AC_PROG_AWK
AC_PROG_SED
LT_INIT
AM_PATH_PYTHON([2.4],,[AC_MSG_ERROR([Python-2.4, or higher, is required])])
AC_CHECK_PROGS([M4],[gm4 m4], m4)
......
......@@ -50,6 +50,6 @@ lal-user-env.shell: Makefile $(srcdir)/generate_user_env.awk
lal-user-env.csh lal-user-env.sh: lal-user-env.shell $(srcdir)/generate_user_env.awk
@echo "creating $@"
@$(AWK) -f $(srcdir)/generate_user_env.awk -v package=$(PACKAGE_NAME) -v output=$@ $<
@$(AWK) -f $(srcdir)/generate_user_env.awk -v SED=$(SED) -v package=$(PACKAGE_NAME) -v output=$@ $< || { rm -f $@; exit 1; }
@INCLUDEMKFILE@
......@@ -151,6 +151,7 @@ AC_PROG_CPP
AC_PROG_INSTALL
AC_PROG_LN_S
AC_PROG_AWK
AC_PROG_SED
# check for condor_compile
AC_ENABLE_SHARED
......
......@@ -50,4 +50,4 @@ lalapps-user-env.shell: Makefile $(srcdir)/generate_user_env.awk
lalapps-user-env.csh lalapps-user-env.sh: lalapps-user-env.shell $(srcdir)/generate_user_env.awk
@echo "creating $@"
@$(AWK) -f $(srcdir)/generate_user_env.awk -v package=$(PACKAGE_NAME) -v output=$@ $<
@$(AWK) -f $(srcdir)/generate_user_env.awk -v SED=$(SED) -v package=$(PACKAGE_NAME) -v output=$@ $< || { rm -f $@; exit 1; }
......@@ -89,6 +89,7 @@ AC_PROG_CPP
AC_PROG_INSTALL
AC_PROG_LN_S
AC_PROG_AWK
AC_PROG_SED
LT_INIT
AM_PATH_PYTHON([2.4],,[AC_MSG_ERROR([Python-2.4, or higher, is required])])
......
......@@ -72,7 +72,7 @@ lalburst-user-env.shell: Makefile $(srcdir)/generate_user_env.awk
lalburst-user-env.csh lalburst-user-env.sh: lalburst-user-env.shell $(srcdir)/generate_user_env.awk
@echo "creating $@"
@$(AWK) -f $(srcdir)/generate_user_env.awk -v package=$(PACKAGE_NAME) -v output=$@ $<
@$(AWK) -f $(srcdir)/generate_user_env.awk -v SED=$(SED) -v package=$(PACKAGE_NAME) -v output=$@ $< || { rm -f $@; exit 1; }
EXTRA_DIST = \
LALBurstConfig.h.in \
......
......@@ -95,6 +95,7 @@ AC_PROG_CPP
AC_PROG_INSTALL
AC_PROG_LN_S
AC_PROG_AWK
AC_PROG_SED
LT_INIT
AM_PATH_PYTHON([2.4],,[AC_MSG_ERROR([Python-2.4, or higher, is required])])
AC_CHECK_PROGS([M4],[gm4 m4],[m4])
......
......@@ -64,7 +64,7 @@ lalframe-user-env.shell: Makefile $(srcdir)/generate_user_env.awk
lalframe-user-env.csh lalframe-user-env.sh: lalframe-user-env.shell $(srcdir)/generate_user_env.awk
@echo "creating $@"
@$(AWK) -f $(srcdir)/generate_user_env.awk -v package=$(PACKAGE_NAME) -v output=$@ $<
@$(AWK) -f $(srcdir)/generate_user_env.awk -v SED=$(SED) -v package=$(PACKAGE_NAME) -v output=$@ $< || { rm -f $@; exit 1; }
EXTRA_DIST = \
generate_user_env.awk \
......
......@@ -96,6 +96,7 @@ AC_PROG_CPP
AC_PROG_INSTALL
AC_PROG_LN_S
AC_PROG_AWK
AC_PROG_SED
LT_INIT
AM_PATH_PYTHON([2.4],,[AC_MSG_ERROR([Python-2.4, or higher, is required])])
......
......@@ -58,7 +58,7 @@ lalinference-user-env.shell: Makefile $(srcdir)/generate_user_env.awk
lalinference-user-env.csh lalinference-user-env.sh: lalinference-user-env.shell $(srcdir)/generate_user_env.awk
@echo "creating $@"
@$(AWK) -f $(srcdir)/generate_user_env.awk -v package=$(PACKAGE_NAME) -v output=$@ $<
@$(AWK) -f $(srcdir)/generate_user_env.awk -v SED=$(SED) -v package=$(PACKAGE_NAME) -v output=$@ $< || { rm -f $@; exit 1; }
EXTRA_DIST = \
LALInferenceConfig.h.in \
......
......@@ -98,6 +98,7 @@ AC_PROG_CPP
AC_PROG_INSTALL
AC_PROG_LN_S
AC_PROG_AWK
AC_PROG_SED
LT_INIT
AM_PATH_PYTHON([2.4],,[AC_MSG_ERROR([Python-2.4, or higher, is required])])
......
......@@ -243,7 +243,7 @@ lalinspiral-user-env.shell: Makefile $(srcdir)/generate_user_env.awk
lalinspiral-user-env.csh lalinspiral-user-env.sh: lalinspiral-user-env.shell $(srcdir)/generate_user_env.awk
@echo "creating $@"
@$(AWK) -f $(srcdir)/generate_user_env.awk -v package=$(PACKAGE_NAME) -v output=$@ $<
@$(AWK) -f $(srcdir)/generate_user_env.awk -v SED=$(SED) -v package=$(PACKAGE_NAME) -v output=$@ $< || { rm -f $@; exit 1; }
EXTRA_DIST = \
LALInspiralConfig.h.in \
......
......@@ -88,6 +88,7 @@ AC_PROG_CPP
AC_PROG_INSTALL
AC_PROG_LN_S
AC_PROG_AWK
AC_PROG_SED
LT_INIT
AM_PATH_PYTHON([2.4],,[AC_MSG_ERROR([Python-2.4, or higher, is required])])
......
......@@ -55,7 +55,7 @@ lalmetaio-user-env.shell: Makefile $(srcdir)/generate_user_env.awk
lalmetaio-user-env.csh lalmetaio-user-env.sh: lalmetaio-user-env.shell $(srcdir)/generate_user_env.awk
@echo "creating $@"
@$(AWK) -f $(srcdir)/generate_user_env.awk -v package=$(PACKAGE_NAME) -v output=$@ $<
@$(AWK) -f $(srcdir)/generate_user_env.awk -v SED=$(SED) -v package=$(PACKAGE_NAME) -v output=$@ $< || { rm -f $@; exit 1; }
LALMetaIOVCSInfo.h: ._vcsinfo
......
......@@ -98,6 +98,7 @@ AC_PROG_CPP
AC_PROG_INSTALL
AC_PROG_LN_S
AC_PROG_AWK
AC_PROG_SED
LT_INIT
AM_PATH_PYTHON([2.4],,[AC_MSG_ERROR([Python-2.4, or higher, is required])])
......
......@@ -158,7 +158,7 @@ lalpulsar-user-env.shell: Makefile $(srcdir)/generate_user_env.awk
lalpulsar-user-env.csh lalpulsar-user-env.sh: lalpulsar-user-env.shell $(srcdir)/generate_user_env.awk
@echo "creating $@"
@$(AWK) -f $(srcdir)/generate_user_env.awk -v package=$(PACKAGE_NAME) -v output=$@ $<
@$(AWK) -f $(srcdir)/generate_user_env.awk -v SED=$(SED) -v package=$(PACKAGE_NAME) -v output=$@ $< || { rm -f $@; exit 1; }
EXTRA_DIST = \
LALPulsarConfig.h.in \
......
......@@ -92,6 +92,7 @@ AC_PROG_CPP
AC_PROG_INSTALL
AC_PROG_LN_S
AC_PROG_AWK
AC_PROG_SED
LT_INIT
AM_PATH_PYTHON([2.4],,[AC_MSG_ERROR([Python-2.4, or higher, is required])])
AC_CHECK_PROGS([M4],[gm4 m4],[m4])
......
......@@ -107,7 +107,7 @@ lalsimulation-user-env.shell: Makefile $(srcdir)/generate_user_env.awk
lalsimulation-user-env.csh lalsimulation-user-env.sh: lalsimulation-user-env.shell $(srcdir)/generate_user_env.awk
@echo "creating $@"
@$(AWK) -f $(srcdir)/generate_user_env.awk -v package=$(PACKAGE_NAME) -v output=$@ $<
@$(AWK) -f $(srcdir)/generate_user_env.awk -v SED=$(SED) -v package=$(PACKAGE_NAME) -v output=$@ $< || { rm -f $@; exit 1; }
EXTRA_DIST = \
generate_user_env.awk \
......
......@@ -89,6 +89,7 @@ AC_PROG_CPP
AC_PROG_INSTALL
AC_PROG_LN_S
AC_PROG_AWK
AC_PROG_SED
LT_INIT
AM_PATH_PYTHON([2.4],,[AC_MSG_ERROR([Python-2.4, or higher, is required])])
......
......@@ -54,7 +54,7 @@ lalstochastic-user-env.shell: Makefile $(srcdir)/generate_user_env.awk
lalstochastic-user-env.csh lalstochastic-user-env.sh: lalstochastic-user-env.shell $(srcdir)/generate_user_env.awk
@echo "creating $@"
@$(AWK) -f $(srcdir)/generate_user_env.awk -v package=$(PACKAGE_NAME) -v output=$@ $<
@$(AWK) -f $(srcdir)/generate_user_env.awk -v SED=$(SED) -v package=$(PACKAGE_NAME) -v output=$@ $< || { rm -f $@; exit 1; }
EXTRA_DIST = \
LALStochasticConfig.h.in \
......
......@@ -92,6 +92,7 @@ AC_PROG_CPP
AC_PROG_INSTALL
AC_PROG_LN_S
AC_PROG_AWK
AC_PROG_SED
LT_INIT
AM_PATH_PYTHON([2.4],,[AC_MSG_ERROR([Python-2.4, or higher, is required])])
......
......@@ -43,7 +43,7 @@ lalxml-user-env.shell: Makefile $(srcdir)/generate_user_env.awk
lalxml-user-env.csh lalxml-user-env.sh: lalxml-user-env.shell $(srcdir)/generate_user_env.awk
@echo "creating $@"
@$(AWK) -f $(srcdir)/generate_user_env.awk -v package=$(PACKAGE_NAME) -v output=$@ $<
@$(AWK) -f $(srcdir)/generate_user_env.awk -v SED=$(SED) -v package=$(PACKAGE_NAME) -v output=$@ $< || { rm -f $@; exit 1; }
EXTRA_DIST = \
generate_user_env.awk \
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment