1#!/bin/bash
2# This scripts builds a self-contained executable file for ThreadSanitizer.
3# Usage:
4#   ./mk-self-contained-tsan.sh            \
5#      /pin/root                           \
6#      /dir/where/tsan-pin-files/reside    \
7#      resulting_binary
8
9# take Pin from here:
10PIN_ROOT="$1"
11# Our .so files are here:
12IN_DIR="$2"
13# Put the result here:
14OUT="$3"
15# The files/dirs to take:
16IN_FILES="tsan_pin.sh bin/*ts_pin.so"
17
18rm -rf $OUT           # remove the old one
19touch  $OUT           # create the new one
20chmod +x $OUT
21
22# Create the header.
23cat << 'EOF' >> $OUT
24#!/bin/bash
25# This is a self-extracting executable of ThreadSanitizerPin.
26# This file is autogenerated by mk-self-contained-tsan-pin.sh.
27
28# We extract the temporary files to $TSAN_EXTRACT_DIR/tsan_pin.XXXXXX
29TSAN_EXTRACT_DIR=${TSAN_EXTRACT_DIR:-/tmp}
30EXTRACT_DIR="$(mktemp -d $TSAN_EXTRACT_DIR/tsan_pin.XXXXXX)"
31
32cleanup() {
33  rm -rf $EXTRACT_DIR
34}
35# We will cleanup on exit.
36trap cleanup EXIT
37
38mkdir -p $EXTRACT_DIR
39chmod +rwx $EXTRACT_DIR
40EOF
41# end of header
42
43# Create the self-extractor
44
45# Exclude unneeded binaries.
46TAR_EXCLUDE="$TAR_EXCLUDE --exclude=*/doc/*       \
47                          --exclude=*/include/*   \
48                          --exclude=*/examples/*   \
49                          "
50# Create the running part.
51
52cat << 'EOF' >> $OUT
53# Extract:
54echo Extracting ThreadSanitizerPin to $EXTRACT_DIR
55sed '1,/^__COMPRESSED_DATA_BELOW__$/d' $0 | tar xz -C $EXTRACT_DIR
56
57export PIN_ROOT=$EXTRACT_DIR
58export TS_ROOT=$EXTRACT_DIR
59$EXTRACT_DIR/tsan_pin.sh "$@"
60EXIT_STATUS=$?
61cleanup # the trap above will handle the cleanup only if we are in bash 3.x
62exit $EXIT_STATUS # make sure to return the exit code from the tool.
63
64__COMPRESSED_DATA_BELOW__
65EOF
66
67# Dump the compressed binary at the very end of the file.
68echo tar zcvh -C $IN_DIR $TAR_EXCLUDE $IN_FILES
69tar zcvh -C $IN_DIR $TAR_EXCLUDE $IN_FILES -C $PIN_ROOT ./{extras,ia32,intel64,pin}  >> $OUT
70
71echo "File $OUT successfully created"
72
73