Skip to content

Commit

Permalink
Added tools
Browse files Browse the repository at this point in the history
  • Loading branch information
vestchainio committed Oct 31, 2018
1 parent e07bf87 commit e165fc7
Show file tree
Hide file tree
Showing 6 changed files with 388 additions and 0 deletions.
16 changes: 16 additions & 0 deletions tools/CMakeLists.txt
@@ -0,0 +1,16 @@
configure_file( vestiocpp.in vestiocpp @ONLY)
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/llvm-gcov.sh ${CMAKE_CURRENT_BINARY_DIR}/llvm-gcov.sh COPYONLY)
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/ctestwrapper.sh ${CMAKE_CURRENT_BINARY_DIR}/ctestwrapper.sh COPYONLY)
install( FILES ${CMAKE_CURRENT_BINARY_DIR}/vestiocpp DESTINATION ${CMAKE_INSTALL_FULL_BINDIR}
PERMISSIONS OWNER_READ
OWNER_WRITE
OWNER_EXECUTE
GROUP_READ
GROUP_EXECUTE
WORLD_READ
WORLD_EXECUTE
)

add_executable( print_floats print_floats.cpp )
target_include_directories( print_floats PRIVATE ${Boost_INCLUDE_DIR} )
target_link_libraries( print_floats PRIVATE ${Boost_LIBRARIES} )
4 changes: 4 additions & 0 deletions tools/ctestwrapper.sh
@@ -0,0 +1,4 @@
#!/bin/sh
# run ctest, disregard failure code
ctest --output-on-failure $@
exit 0
2 changes: 2 additions & 0 deletions tools/llvm-gcov.sh
@@ -0,0 +1,2 @@
#!/bin/bash
exec llvm-cov gcov "$@"
70 changes: 70 additions & 0 deletions tools/mas_sign.sh
@@ -0,0 +1,70 @@
#!/bin/bash

set -eu

if [ $# -ne 5 ]; then
echo Usage: $0 certificate-fingerprint appid provisioning-file keychain-group filename
exit 1
fi

CERT=$1
APPID=$2
PP=$3
KCG=$4
FILENAME=$5
IFS=. read TEAMID BUNDLEID <<<"${APPID}"

mkdir -p $FILENAME.app/Contents/MacOS
cp "$3" $FILENAME.app/Contents/embedded.provisionprofile
cp $FILENAME $FILENAME.app/Contents/MacOS/$FILENAME

cat > $FILENAME.app/Contents/Info.plist <<ENDENDEND
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>en</string>
<key>CFBundleExecutable</key>
<string>$FILENAME</string>
<key>CFBundleIdentifier</key>
<string>$BUNDLEID</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>$FILENAME</string>
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleSupportedPlatforms</key>
<array>
<string>MacOSX</string>
</array>
<key>CFBundleVersion</key>
<string>1</string>
</dict>
</plist>
ENDENDEND

TMPFILE=$(mktemp /tmp/signscript.XXXXXX)
trap "rm $TMPFILE" EXIT

cat > $TMPFILE <<ENDENDEND
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>com.apple.application-identifier</key>
<string>$APPID</string>
<key>com.apple.developer.team-identifier</key>
<string>$TEAMID</string>
<key>keychain-access-groups</key>
<array>
<string>$KCG</string>
</array>
</dict>
</plist>
ENDENDEND

codesign --force --sign $CERT --timestamp=none --entitlements $TMPFILE $FILENAME.app
99 changes: 99 additions & 0 deletions tools/print_floats.cpp
@@ -0,0 +1,99 @@


#include <iostream>
#include <iomanip>
#include <limits>
#include <random>

#include <boost/program_options.hpp>

namespace po = boost::program_options;

int main(int argc, const char **argv) {

unsigned int seed = std::mt19937::default_seed;

unsigned int num_cases = 10;


po::options_description desc("Options");
desc.add_options()
("help,h", "Print this help message and exit")
("seed,s", po::value<unsigned int>(&seed)->default_value(seed), "random seed")
("num-cases,n", po::value<unsigned int>(&num_cases)->default_value(num_cases), "number of test cases to generate" )
("avoid-specials", po::bool_switch()->default_value(false), "Avoid printing test cases for special values of double" )
;

po::variables_map vm;
po::store(po::parse_command_line(argc, argv, desc), vm);
po::notify(vm);

if( vm.count("help") ) {
std::cout << desc << std::endl;
return 1;
}

std::cout << "Seed: " << seed << std::endl;
std::mt19937 gen(seed);

double lbound = std::numeric_limits<double>::lowest();
double ubound = std::numeric_limits<double>::max();

std::uniform_int_distribution<uint64_t> dis(0, std::numeric_limits<uint64_t>::max());

std::cout.precision(std::numeric_limits<double>::digits10 + 1);

auto print_triplet = [&]( double d, int left_padding = 0 ) {
std::cout << std::setw(left_padding) << "";
std::cout << "[\"0x" << std::hex << *(uint64_t*)(&d) << "\", \""
<< std::hexfloat << d << "\", \"" << std::fixed << d << "\"]";
};

if( num_cases > 0 ) {
std::cout << std::endl;
std::cout << "Randomly generated double test cases (raw hex dump, hexidecimal float, decimal fixed) "
"within the interval [" << std::fixed << lbound << ", " << ubound << "]:" << std::endl
<< " [" << std::endl;
for( int n = 1, end = num_cases; n <= end; )
{

uint64_t x = dis(gen);
double v = *(double*)(&x);

if( !(lbound <= v && v <= ubound) ) continue;

print_triplet(v, 2);
if( n != end )
std::cout << ",";

std::cout << std::endl;

++n;
}
std::cout << "]" << std::endl;
}

if( !vm["avoid-specials"].as<bool>() ) {
std::cout << std::endl;
std::cout << "Special double value test cases (raw hex dump, hexidecimal float, decimal fixed):" << std::endl;

using nld = std::numeric_limits<double>;

std::cout << std::endl << " lowest(): "; print_triplet(nld::lowest());
std::cout << std::endl << " min(): "; print_triplet(nld::min());
std::cout << std::endl << " max(): "; print_triplet(nld::max());
std::cout << std::endl << " epsilon(): "; print_triplet(nld::epsilon());
std::cout << std::endl << " round_error(): "; print_triplet(nld::round_error());
std::cout << std::endl << " +0: "; print_triplet(0.0);
std::cout << std::endl << " -0: "; print_triplet(-0.0);
std::cout << std::endl << " infinity(): "; print_triplet(nld::infinity());
std::cout << std::endl << " -infinity(): "; print_triplet(-nld::infinity());
std::cout << std::endl << " quiet_NaN(): "; print_triplet(nld::quiet_NaN());
std::cout << std::endl << " signaling_NaN(): "; print_triplet(nld::signaling_NaN());
std::cout << std::endl << " denorm_min(): "; print_triplet(nld::denorm_min());
std::cout << std::endl;

}

return 0;
}
197 changes: 197 additions & 0 deletions tools/vestiocpp.in
@@ -0,0 +1,197 @@
#!/bin/bash

VESTIO_BIN_INSTALL_DIR=`dirname $0`
if [ "${VESTIO_BIN_INSTALL_DIR}" == "." ]; then
VESTIO_BIN_INSTALL_DIR=`pwd`
fi
VESTIO_INSTALL_DIR=`dirname ${VESTIO_BIN_INSTALL_DIR}`
if [ -x "@CMAKE_BINARY_DIR@/programs/vestio-abigen/vestio-abigen" ]; then
ABIGEN="@CMAKE_BINARY_DIR@/programs/vestio-abigen/vestio-abigen"
elif [ -x "${VESTIO_INSTALL_DIR}/bin/vestio-abigen" ]; then
ABIGEN=${VESTIO_INSTALL_DIR}/bin/vestio-abigen
fi
if [ -x "@CMAKE_BINARY_DIR@/externals/binaryen/bin/vestio-s2wasm" ]; then
VESTIO_S2WASM="@CMAKE_BINARY_DIR@/externals/binaryen/bin/vestio-s2wasm"
elif [ -x "${VESTIO_INSTALL_DIR}/bin/vestio-s2wasm" ]; then
VESTIO_S2WASM="${VESTIO_INSTALL_DIR}/bin/vestio-s2wasm"
else
echo "vestio-s2wasm not found either built or installed"
exit 12
fi
if [ -x "@CMAKE_BINARY_DIR@/libraries/wasm-jit/Source/Programs/vestio-wast2wasm" ]; then
VESTIO_WAST2WASM="@CMAKE_BINARY_DIR@/libraries/wasm-jit/Source/Programs/vestio-wast2wasm"
elif [ -x "${VESTIO_INSTALL_DIR}/bin/vestio-wast2wasm" ]; then
VESTIO_WAST2WASM="${VESTIO_INSTALL_DIR}/bin/vestio-wast2wasm"
else
echo "vestio-wast2wasm not found either built or installed"
exit 14
fi
BOOST_INCLUDE_DIR=@Boost_INCLUDE_DIR@
function copy_skeleton {
set -e
cp -r "${VESTIO_INSTALL_DIR}/share/vestio/skeleton/." $newname

for file in $(find ./$newname -name 'skeleton.*')
do
newfile=`echo $file | sed 's/skeleton\./'"$newname"'./'`
# echo mv "${file}" "${newfile}"
mv "${file}" "${newfile}"
exp=s/skeleton/${newname}/g
# echo sed -i ${exp} ${newfile}
sed ${exp} ${newfile} > ${newfile}1
mv ${newfile}1 ${newfile}
done
echo "created $newname from skeleton"
set +e
}

function build_contract {
set -e
workdir=`mktemp -d`

if [[ ${VERBOSE} == "1" ]]; then
PRINT_CMDS="set -x"
fi

($PRINT_CMDS; mkdir $workdir/built)

for file in $@; do
name=`basename $file`
filePath=`dirname $file`

($PRINT_CMDS; @WASM_CLANG@ -emit-llvm -O3 --std=c++14 --target=wasm32 -nostdinc \
-DBOOST_DISABLE_ASSERTS -DBOOST_EXCEPTION_DISABLE \
-nostdlib -nostdlibinc -ffreestanding -nostdlib \
-fno-threadsafe-statics -fno-rtti -fno-exceptions \
-I @CMAKE_BINARY_DIR@/contracts \
-I @CMAKE_SOURCE_DIR@/contracts \
-I @CMAKE_SOURCE_DIR@/contracts/libc++/upstream/include \
-I @CMAKE_SOURCE_DIR@/contracts/musl/upstream/include \
-I @CMAKE_SOURCE_DIR@/externals/magic_get/include \
-I ${VESTIO_INSTALL_DIR}/include \
-I${VESTIO_INSTALL_DIR}/include/libc++/upstream/include \
-I${VESTIO_INSTALL_DIR}/include/musl/upstream/include \
-I${BOOST_INCLUDE_DIR} \
-I $filePath \
${VESTIOCPP_CFLAGS} \
-c $file -o $workdir/built/$name
)

done

declare -a possible_libs=("@CMAKE_BINARY_DIR@/contracts/vestiolib/vestiolib.bc"
"@CMAKE_BINARY_DIR@/contracts/libc++/libc++.bc"
"@CMAKE_BINARY_DIR@/contracts/musl/libc.bc"
"${VESTIO_INSTALL_DIR}/usr/share/vestio/contractsdk/lib/vestiolib.bc"
"${VESTIO_INSTALL_DIR}/usr/share/vestio/contractsdk/lib/libc++.bc"
"${VESTIO_INSTALL_DIR}/usr/share/vestio/contractsdk/lib/libc.bc")
declare libs=""
for lib in "${possible_libs[@]}"; do
if [ -f "${lib}" ]; then
libs="${libs} ${lib}"
fi
done
($PRINT_CMDS; @WASM_LLVM_LINK@ -only-needed -o $workdir/linked.bc $workdir/built/* ${libs})
($PRINT_CMDS; @WASM_LLC@ -thread-model=single --asm-verbose=false -o $workdir/assembly.s $workdir/linked.bc)
($PRINT_CMDS; ${VESTIO_S2WASM} -o $outname -s 16384 $workdir/assembly.s)
($PRINT_CMDS; ${VESTIO_WAST2WASM} $outname ${outname%.*}.wasm -n)

($PRINT_CMDS; rm -rf $workdir)
set +e
}

function generate_abi {

if [[ ! -e "$1" ]]; then
echo "You must specify a file"
exit 1
fi

context_folder=$(cd "$(dirname "$1")" ; pwd -P)

${ABIGEN} -extra-arg=-c -extra-arg=--std=c++14 -extra-arg=--target=wasm32 \
-extra-arg=-nostdinc -extra-arg=-nostdinc++ -extra-arg=-DABIGEN \
-extra-arg=-I@CMAKE_SOURCE_DIR@/contracts \
-extra-arg=-I@CMAKE_BINARY_DIR@/contracts \
-extra-arg=-I@CMAKE_SOURCE_DIR@/contracts/libc++/upstream/include \
-extra-arg=-I@CMAKE_SOURCE_DIR@/contracts/musl/upstream/include \
-extra-arg=-I@CMAKE_SOURCE_DIR@/externals/magic_get/include \
-extra-arg=-I${VESTIO_INSTALL_DIR}/include/libc++/upstream/include \
-extra-arg=-I${VESTIO_INSTALL_DIR}/include/musl/upstream/include \
-extra-arg=-I${BOOST_INCLUDE_DIR} \
-extra-arg=${VESTIOCPP_CFLAGS} \
-extra-arg=-I${VESTIO_INSTALL_DIR}/include -extra-arg=-I$context_folder \
-extra-arg=-fparse-all-comments -destination-file=${outname} -verbose=0 \
-context=$context_folder $1 --

if [ "$?" -ne 0 ]; then
exit 1
fi

echo "Generated ${outname} ..."
}

function print_help {
echo "Usage: $0 -o output.wast contract.cpp [other.cpp ...]"
echo " OR"
echo " $0 -n mycontract"
echo " OR"
echo " $0 -g contract.abi types.hpp"
echo
echo "Options:"
echo " -n | --newcontract [name]"
echo " Create a new contract in the [name] folder, based on the example contract"
echo " OR"
echo " -o | --outname [output.wast] [input.cpp ...]"
echo " Generate the wast output file based on input cpp files"
echo " The wasm output will also be created as output.wasm"
echo " OR"
echo " -g | --genabi contract.abi types.hpp"
echo " Generate the ABI specification file [EXPERIMENTAL]"
}

command=""

while [[ $# -gt 1 ]]
do
key="$1"

case $key in
-h|--help)
print_help
break;
;;
-n|--newcontract)
newname="$2"
command="newcontract"
shift 2
break
;;
-o|--outname)
outname="$2"
command="outname"
shift 2
break
;;
-g|--genabi)
outname="$2"
command="genabi"
shift 2
;;
*)
echo "Unrecognized option: $1"
exit 1
;;
esac
done

if [[ "outname" == "$command" ]]; then
build_contract $@
elif [[ "newcontract" == "$command" ]]; then
copy_skeleton
elif [[ "genabi" == "$command" ]]; then
generate_abi $@
else
print_help
exit 1
fi

0 comments on commit e165fc7

Please sign in to comment.