cross.sh 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. #!/bin/bash
  2. # $Id$
  3. # Copyright 2006 Michael Mohr
  4. # See LICENSE for licensing information.
  5. #######################################################################
  6. # Tor-cross: a tool to help cross-compile Tor
  7. #
  8. # mailto:tor-assistants@freehaven.net
  9. #
  10. # The purpose of a cross-compiler is to produce an executable for
  11. # one system (CPU) on another. This is useful, for example, when
  12. # the target system does not have a native compiler available.
  13. # You might, for example, wish to cross-compile a program on your
  14. # host (the computer you're working on now) for a target such as
  15. # a router or handheld computer.
  16. #
  17. # This script automatically patches two files in the Tor source:
  18. # configure.in : remove test programs
  19. # compat.h : remove check for NULL==0
  20. #
  21. # A number of environment variables must be set in order for this
  22. # script to work:
  23. # $PREFIX, $CROSSPATH, $ARCH_PREFIX, $HOST,
  24. # and (optionally) $BUILD
  25. # Please run the script for a description of each one. If automated
  26. # builds are desired, the above variables can be exported at the top
  27. # of this script.
  28. #
  29. # Recent releases of Tor include test programs in configure. Normally
  30. # this is a good thing, since it catches a number of problems.
  31. # However, this also presents a problem when cross compiling, since
  32. # you can't run binary images for the target system on the host.
  33. #
  34. # Tor-cross assumes that you know what you're doing and removes a
  35. # number of checks known to cause problems with this process.
  36. # Note that this does not guarantee that the program will run or
  37. # even compile; it simply allows configure to generate the Makefiles.
  38. #
  39. # Stripping the binaries should almost always be done for an
  40. # embedded environment where space is at an exacting premium.
  41. # However, the default is NOT to strip them since they are useful for
  42. # debugging. If you do not plan to do any debugging and you
  43. # don't care about the debugging symbols, set $STRIP to "yes" before
  44. # running this script.
  45. #
  46. # Tor-cross was written by Michael Mohr. He can be contacted at
  47. # m(dot)mohr(at)laposte(dot)net. Comments are appreciated, but
  48. # flames go to /dev/null.
  49. #
  50. # The target with which this script is tested is little-endian
  51. # MIPS Linux, built on an Athlon-based Linux desktop.
  52. #
  53. #######################################################################
  54. # disable some show-stopping bugs (see cross.patch for more)
  55. export CROSS_COMPILE=yes
  56. if [ ! -f configure.in ]
  57. then
  58. echo "Please run this script from the root of the Tor distribution."
  59. exit -1
  60. fi
  61. if [ -z $PREFIX ]
  62. then
  63. echo "You must define \$PREFIX since you are cross-compiling."
  64. echo "Select a non-system location (i.e. /tmp/tor-cross):"
  65. echo " export PREFIX=/tmp/tor-cross"
  66. exit -1
  67. fi
  68. if [ -z $CROSSPATH ]
  69. then
  70. echo "You must define the location of your cross-compiler's"
  71. echo "directory using \$CROSSPATH; for example,"
  72. echo " export CROSSPATH=/opt/cross/staging_dir_mipsel/bin"
  73. exit -1
  74. fi
  75. if [ -z $ARCH_PREFIX ]
  76. then
  77. echo "You must define \$ARCH_PREFIX to continue. For example,"
  78. echo "if you normally cross-compile applications using"
  79. echo "mipsel-linux-uclibc-gcc, you would set \$ARCH_PREFIX like so:"
  80. echo " export ARCH_PREFIX=mipsel-linux-uclibc-"
  81. exit -1
  82. fi
  83. if [ -z $HOST ]
  84. then
  85. echo "You must specify a target processor with \$HOST; for example:"
  86. echo " export HOST=mipsel-unknown-elf"
  87. exit -1
  88. fi
  89. if [ -z $BUILD ]
  90. then
  91. echo "You should specify the host machine's type with \$BUILD; for example:"
  92. echo " export BUILD=i686-pc-linux-gnu"
  93. echo "If you wish to let configure autodetect the host, set \$BUILD to 'auto':"
  94. echo " export BUILD=auto"
  95. exit -1
  96. fi
  97. # clean up any existing object files
  98. if [ -f src/or/tor ]
  99. then
  100. make clean
  101. fi
  102. # check if the source has already been patched
  103. patch -f -p1 -R --dry-run < contrib/cross.patch > /dev/null 2>&1
  104. # if it hasn't, rerun the autotools
  105. if [ $? -ne 0 ]
  106. then
  107. patch -p1 < contrib/cross.patch
  108. aclocal
  109. autoconf
  110. autoheader
  111. automake --add-missing
  112. fi
  113. # Set up the buld environment and try to run configure
  114. export PATH=$PATH:$CROSSPATH
  115. export RANLIB=${ARCH_PREFIX}ranlib
  116. export CC=${ARCH_PREFIX}gcc
  117. if [ $BUILD == "auto" ]
  118. then
  119. ./configure \
  120. --prefix=$PREFIX \
  121. --host=$HOST
  122. else
  123. ./configure \
  124. --prefix=$PREFIX \
  125. --host=$HOST \
  126. --build=$BUILD
  127. fi
  128. # has a problem occurred?
  129. if [ $? -ne 0 ]
  130. then
  131. echo ""
  132. echo "A problem has been detected with configure."
  133. echo "Please check the output above and rerun cross.sh"
  134. echo ""
  135. exit -1
  136. fi
  137. # Now we're cookin'
  138. make
  139. # if $STRIP has length (i.e. STRIP=yes), strip the binaries
  140. if [ ! -z $STRIP ]
  141. then
  142. ${ARCH_PREFIX}strip \
  143. src/or/tor \
  144. src/or/test \
  145. src/tools/tor-resolve
  146. fi
  147. echo ""
  148. echo "Tor should be compiled at this point. Now run 'make install' to"
  149. echo "install to $PREFIX"
  150. echo ""