#!/bin/sh
# (c) 1992-2023 Intel Corporation.                                              
# Intel, the Intel logo, Intel, MegaCore, NIOS II, Quartus and TalkBack         
# words and logos are trademarks of Intel Corporation or its                    
# subsidiaries in the U.S. and/or other countries. Other marks and              
# brands may be claimed as the property of others.                              
# See Trademarks on intel.com for full list of Intel trademarks or the          
# Trademarks & Brands Names Database (if Intel)                                 
# or See www.Intel.com/legal (if Altera)                                        
# Your use of Intel Corporation's design tools, logic functions and             
# other software and tools, and its AMPP partner logic functions, and           
# any output files any of the foregoing (including device programming           
# or simulation files), and any associated documentation or information         
# are expressly subject to the terms and conditions of the Altera               
# Program License Subscription Agreement, Intel MegaCore Function               
# License Agreement, or other applicable license agreement, including,          
# without limitation, that your use is for the sole purpose of                  
# programming logic devices manufactured by Intel and sold by Intel or          
# its authorized distributors.                                                  
# Please refer to the applicable agreement for further details.                 


#rem Generate Profile Utility Script 

USAGE="Usage: `basename $0` <sourcename>.bc"

if [ ! $# -eq 1 ]; then
    echo $USAGE >&2
    exit 1
fi

if [ ! -r "$1" -o ! -s "$1" ]; then
    echo "`basename $0`: Can't find input file $1" >&2
    exit 1
fi

if [ "x$INTELFPGAOCLSDKROOT" = "x" ]; then
	echo "Error: You must set environment variable INTELFPGAOCLSDKROOT to the absolute path of the root of the Intel(R) FPGA SDK for OpenCL(TM) software installation"
	exit 1
fi
if [ ! -x "${INTELFPGAOCLSDKROOT}"/llvm/aocl-bin/aocl-clang ]; then
	echo "Error: You must set environment variable INTELFPGAOCLSDKROOT to the absolute path of the root of the Intel(R) FPGA SDK for OpenCL(TM) software installation"
	exit 2
fi

# Need QUARTUS_ROOTDIR to find embedded perl.
if [ "x$QUARTUS_ROOTDIR" = "x" ]; then
	echo "Error: You must set environment variable QUARTUS_ROOTDIR to the absolute path of the quartus subdirectory inside ACDS"
	exit 1
fi
if [ ! -e "$QUARTUS_ROOTDIR/common/tcl/internal" ]; then
	echo "Error: You must set environment variable QUARTUS_ROOTDIR to the absolute path of the quartus subdirectory inside ACDS"
	exit 1
fi
export PERL5LIB="${INTELFPGAOCLSDKROOT}"/share/lib/perl:"${INTELFPGAOCLSDKROOT}"/share/lib/perl/5.8.8:"${PERL5LIB}"
export LD_LIBRARY_PATH="${INTELFPGAOCLSDKROOT}"/linux64/lib:"${LD_LIBRARY_PATH}"


$QUARTUS_ROOTDIR/bin/quartus_stp -t "${INTELFPGAOCLSDKROOT}"/share/lib/tcl/get-profile-data.tcl
if [ ! -s "profile.out" ]; then
  echo "`basename $0`: No profile data found" >&2
  exit 1
fi
"${INTELFPGAOCLSDKROOT}"/linux64/aocl-bin/aocl-opt -digest $1 > /dev/null

if [ $? == 0 ]; then
  name=${1%\.*}
  echo "Report generated in file profile.${name}.html"
fi
