crossexec.sh 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. ########################################################################
  2. # #
  3. # This software is part of the ast package #
  4. # Copyright (c) 1994-2011 AT&T Intellectual Property #
  5. # Copyright (c) 2020-2022 Contributors to ksh 93u+m #
  6. # and is licensed under the #
  7. # Eclipse Public License, Version 2.0 #
  8. # #
  9. # A copy of the License is available at #
  10. # https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.html #
  11. # (with md5 checksum 84283fa8859daf213bdda5a9f8d1be1d) #
  12. # #
  13. # Glenn Fowler <gsf@research.att.com> #
  14. # Martijn Dekker <martijn@inlv.org> #
  15. # #
  16. ########################################################################
  17. : cross compiler a.out execution
  18. (command set -o posix) 2>/dev/null && set -o posix
  19. command=crossexec
  20. tmp=/tmp/cross$$
  21. case `(getopts '[-][123:xyz]' opt --xyz; echo 0$opt) 2>/dev/null` in
  22. 0123) ARGV0="-a $command"
  23. USAGE=$'
  24. [-?
  25. @(#)$Id: crossexec (AT&T Labs Research) 2004-01-04 $
  26. ]
  27. [-author?Glenn Fowler <gsf@research.att.com>]
  28. [-copyright?Copyright (c) 1994-2012 AT&T Intellectual Property]
  29. [-license?https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.html]
  30. [+NAME?crossexec - cross compiler a.out execution]
  31. [+DESCRIPTION?\bcrossexec\b runs a cross-compiled \acommand\a in an environment
  32. that supports a cross-compilation architecture different from the
  33. current host. The cross environment is determined by \acrosstype\a,
  34. usually a host type name produced by \bpackage\b(1). \acrosstype\a
  35. is used to find an entry in \b$HOME/.crossexec\b that specifies
  36. the cross compiler host and access details.]
  37. [+?The exit status of \bcrossexec\b is the exit status of \acommand\a.]
  38. [+CROSS ENVIRONMENT FILE?\b$HOME/.crossexec\b contains one line for each
  39. supported \acrosstype\a. Each line contains 5 tab separated fields.
  40. Field default values are specified as \b-\b. The fields are:]{
  41. [+crosstype?The host type produced by \bpackage\b(1).]
  42. [+host?The host name.]
  43. [+user?The user name on \ahost\a. The default is the current user.]
  44. [+dir?The directory to copy \acommand\a and execute it. The default
  45. is the \auser\a \b$HOME\b on \ahost\a.]
  46. [+shell?The command used to get shell access to \ahost\a. Currently
  47. only \brsh\b and \bssh\b are supported.]
  48. [+copy?The command used to copy \acommand\a to \ahost\a. Currently
  49. only \brcp\b and \bscp\b are supported.]
  50. }
  51. [n:show?Show the underlying commands but do not execute.]
  52. crosstype command [ option ... ] [ file ... ]
  53. [+SEE ALSO?\brcp\b(1), \brsh\b(1), \bscp\b(1), \bssh\b(1)]
  54. '
  55. ;;
  56. *) ARGV0=""
  57. USAGE="crosstype command [ option ... ] [ file ... ]"
  58. ;;
  59. esac
  60. usage()
  61. {
  62. OPTIND=0
  63. getopts $ARGV0 "$USAGE" OPT '-?'
  64. exit 2
  65. }
  66. exec=
  67. # get the options and operands
  68. while getopts $ARGV0 "$USAGE" OPT
  69. do case $OPT in
  70. n) exec=echo ;;
  71. *) usage ;;
  72. esac
  73. done
  74. shift $OPTIND-1
  75. case $# in
  76. [01]) usage ;;
  77. esac
  78. type=$1
  79. shift
  80. cmd=$1
  81. shift
  82. # get the host info
  83. info=$HOME/.$command
  84. if test ! -r $info
  85. then echo "$command: $info: not found" >&2
  86. exit 1
  87. fi
  88. ifs=${IFS-'
  89. '}
  90. while :
  91. do IFS=' '
  92. read hosttype hostname usr dir sh cp
  93. code=$?
  94. IFS=$ifs
  95. case $code in
  96. 0) ;;
  97. *) echo "$command: $type: unknown cross compiler host type" >&2
  98. exit 1
  99. ;;
  100. esac
  101. case $hosttype in
  102. $type) break ;;
  103. esac
  104. done < $info
  105. # fill in the defaults
  106. case $usr in
  107. -) cpu= shu= ;;
  108. *) cpu=${usr}@ shu="-l $usr" ;;
  109. esac
  110. case $dir in
  111. -) dir= ;;
  112. esac
  113. case $sh in
  114. ''|-) sh=ssh ;;
  115. esac
  116. case $cp in
  117. ''|-) cp=scp ;;
  118. scp) cp="$cp -q" ;;
  119. esac
  120. trap "rm -f $tmp" 0 1 2 3 15
  121. $exec $cp $cmd $cpu$hostname:$dir </dev/null || exit 1
  122. cmd=./${cmd##*/}
  123. $exec $sh $shu $hostname "cd $dir; LD_LIBRARY_PATH=: $cmd $@ </dev/null 2>/dev/null; code=\$?; rm -f $cmd; echo $command: exit \$code >&2" </dev/null 2>$tmp
  124. exit `sed -e '/^'$command': exit [0-9][0-9]*$/!d' -e 's/.* //' $tmp`