Home | History | Annotate | Download | only in tsan
      1 #!/bin/bash
      2 # This scripts builds a self-contained executable file for Valgrind.
      3 # Usage:
      4 #   ./mk-self-contained-valgrind.sh /path/to/valgrind/installation tool_name resulting_binary [tool_flag]
      5 
      6 # Take the valgrind installation from here:
      7 IN_DIR="$1"
      8 # Tool name:
      9 TOOL="$2"
     10 # Put the result here:
     11 OUT="$3"
     12 # If not empty, use as the --tool= value:
     13 if [ "$4" == "" ]
     14 then
     15   TOOLFLAG=$TOOL
     16 else
     17   TOOLFLAG="$4"
     18 fi
     19 
     20 # The files/dirs to take:
     21 IN_FILES="bin/valgrind lib/valgrind/vgpreload_core* lib/valgrind/*$TOOL* lib/valgrind/default.supp"
     22 EXCLUDE_FILES="lib/valgrind/*$TOOL-debug*"
     23 
     24 rm -f $OUT && touch $OUT && chmod +x $OUT || exit 1
     25 
     26 # Create the header.
     27 cat << 'EOF' >> $OUT || exit 1
     28 #!/bin/bash
     29 # This is a self-extracting executable of Valgrind.
     30 # This file is autogenerated by mk-self-contained-valgrind.sh.
     31 
     32 # We extract the temporary files to $VALGRIND_EXTRACT_DIR/valgrind.XXXXXX
     33 VALGRIND_EXTRACT_DIR=${VALGRIND_EXTRACT_DIR:-/tmp}
     34 EXTRACT_DIR="$(mktemp -d $VALGRIND_EXTRACT_DIR/valgrind.XXXXXX)"
     35 
     36 cleanup() {
     37   rm -rf $EXTRACT_DIR
     38 }
     39 # We will cleanup on exit.
     40 trap cleanup EXIT
     41 
     42 mkdir -p $EXTRACT_DIR
     43 chmod +rwx $EXTRACT_DIR
     44 EOF
     45 # end of header
     46 
     47 # Create the self-extractor
     48 
     49 # Create the runner
     50 cat << 'EOF' >> $OUT || exit 1
     51 # Extract:
     52 sed '1,/^__COMPRESSED_DATA_BELOW__$/d' $0 | tar xz -C $EXTRACT_DIR
     53 
     54 # Run
     55 # echo Extracting Valgrind to $EXTRACT_DIR
     56 export VALGRIND_LIB="$EXTRACT_DIR/lib/valgrind"
     57 export VALGRIND_LIB_INNER="$EXTRACT_DIR/lib/valgrind"
     58 EOF
     59 
     60 echo "\$EXTRACT_DIR/bin/valgrind --tool=$TOOLFLAG \"\$@\"" >> $OUT || exit 1
     61 
     62 cat << 'EOF' >> $OUT || exit 1
     63 EXIT_STATUS=$?
     64 cleanup # the trap above will handle the cleanup only if we are in bash 3.x
     65 exit $EXIT_STATUS # make sure to return the exit code from valgrind.
     66 
     67 __COMPRESSED_DATA_BELOW__
     68 EOF
     69 
     70 # Dump the compressed binary at the very end of the file.
     71 (cd $IN_DIR && tar zcvh --exclude=$EXCLUDE_FILES $IN_FILES) >> $OUT || exit 1
     72 
     73 echo "File $OUT successfully created"
     74