12e5b6d6dSopenharmony_ci#!/bin/sh
22e5b6d6dSopenharmony_ci#
32e5b6d6dSopenharmony_ci# Copyright (C) 2017 and later: Unicode, Inc. and others.
42e5b6d6dSopenharmony_ci# License & terms of use: http://www.unicode.org/copyright.html
52e5b6d6dSopenharmony_ci#
62e5b6d6dSopenharmony_ci# Copyright (C) 2011-2014 IBM Corporation and Others. All Rights Reserved.
72e5b6d6dSopenharmony_ci#
82e5b6d6dSopenharmony_ci# This is designed for building and running single-source-file ICU programs.
92e5b6d6dSopenharmony_ci#
102e5b6d6dSopenharmony_ci# You can always download the latest from http://source.icu-project.org/repos/icu/tools/trunk/scripts/icurun
112e5b6d6dSopenharmony_ci# Or, as an unofficial link, http://bit.ly/icu-run
122e5b6d6dSopenharmony_ci#
132e5b6d6dSopenharmony_ci# In its simplest usage,  simply type:
142e5b6d6dSopenharmony_ci#
152e5b6d6dSopenharmony_ci#     icurun  yourprogram.c
162e5b6d6dSopenharmony_ci#       (or .cpp if it is a C++ program)
172e5b6d6dSopenharmony_ci#
182e5b6d6dSopenharmony_ci#  The tool will compile and then run the program
192e5b6d6dSopenharmony_ci#
202e5b6d6dSopenharmony_ci#  FINDING ICU
212e5b6d6dSopenharmony_ci#  To find ICU, the following search order is used by priority:
222e5b6d6dSopenharmony_ci#
232e5b6d6dSopenharmony_ci#  1)    "-i <path>" .. the <path> will be searched for either a direct path to icu-config,
242e5b6d6dSopenharmony_ci#            or a directory containing it, or a directory containing '/bin' containing it.
252e5b6d6dSopenharmony_ci#        In other words, if icu-config is  /opt/local/bin/icu-config, any of the following will work:
262e5b6d6dSopenharmony_ci#                 -i /opt/local
272e5b6d6dSopenharmony_ci#                 -i /opt/local/bin
282e5b6d6dSopenharmony_ci#                 -i /opt/local/bin/icu-config
292e5b6d6dSopenharmony_ci#
302e5b6d6dSopenharmony_ci#         Additionally, /icu/source is a built ICU source directory, either of the following will work:
312e5b6d6dSopenharmony_ci#
322e5b6d6dSopenharmony_ci#                 -i /icu
332e5b6d6dSopenharmony_ci#                 -i /icu/source
342e5b6d6dSopenharmony_ci#
352e5b6d6dSopenharmony_ci#         Also, if /build is an out-of-source build, this will work:
362e5b6d6dSopenharmony_ci#
372e5b6d6dSopenharmony_ci#                 -i /build
382e5b6d6dSopenharmony_ci#
392e5b6d6dSopenharmony_ci#  2)    If there is an executable ~/.icurunrc script, it can set the variable "ICU_CONFIG" to point
402e5b6d6dSopenharmony_ci#        directly to the icu-config file.
412e5b6d6dSopenharmony_ci#        An example ~/.icurunrc script  contains just this line:
422e5b6d6dSopenharmony_ci#
432e5b6d6dSopenharmony_ci#              ICU_CONFIG=/home/srl/E/II/bin/icu-config
442e5b6d6dSopenharmony_ci#
452e5b6d6dSopenharmony_ci#  3)    ICU_CONFIG can be set in the environment to point to icu-config  ( it's overridden by the .icurunrc script )
462e5b6d6dSopenharmony_ci#
472e5b6d6dSopenharmony_ci#  4)    if "icu-config" is on the PATH, it will be used.
482e5b6d6dSopenharmony_ci#
492e5b6d6dSopenharmony_ci#
502e5b6d6dSopenharmony_ci#  RUNNING
512e5b6d6dSopenharmony_ci#   Any additional arguments following the file will be passed to the application.
522e5b6d6dSopenharmony_ci#
532e5b6d6dSopenharmony_ci#  TODO
542e5b6d6dSopenharmony_ci#   * should support pkg-config, which is preferred over icu-config, although icu-config makes this usage
552e5b6d6dSopenharmony_ci#     easier
562e5b6d6dSopenharmony_ci#
572e5b6d6dSopenharmony_ci#   * need to test the $PATH and $ICU_CONFIG usage models
582e5b6d6dSopenharmony_ci
592e5b6d6dSopenharmony_ciSCRIPTVER='$Revision$'
602e5b6d6dSopenharmony_ci
612e5b6d6dSopenharmony_ci
622e5b6d6dSopenharmony_ciICU_OVERRIDE=""
632e5b6d6dSopenharmony_ci
642e5b6d6dSopenharmony_ciusage()
652e5b6d6dSopenharmony_ci{
662e5b6d6dSopenharmony_ci    echo "Script Version ${SCRIPTVER}"
672e5b6d6dSopenharmony_ci    echo "Usage: $0 [ -i /path/to/icu | -i /path/to/icu-config ]  file.c{pp} [ program args ...]"
682e5b6d6dSopenharmony_ci}
692e5b6d6dSopenharmony_ci
702e5b6d6dSopenharmony_ciif [ $# -lt 1 ];
712e5b6d6dSopenharmony_cithen
722e5b6d6dSopenharmony_ci        usage
732e5b6d6dSopenharmony_ci        exit 1
742e5b6d6dSopenharmony_cifi
752e5b6d6dSopenharmony_ci
762e5b6d6dSopenharmony_ciif [ "$1" = "-?" -o $1 = "-h" ];
772e5b6d6dSopenharmony_cithen
782e5b6d6dSopenharmony_ci        usage
792e5b6d6dSopenharmony_ci        exit 0
802e5b6d6dSopenharmony_cifi
812e5b6d6dSopenharmony_ci
822e5b6d6dSopenharmony_ci
832e5b6d6dSopenharmony_ciif [ $1 = "-i" ];
842e5b6d6dSopenharmony_cithen
852e5b6d6dSopenharmony_ci        shift
862e5b6d6dSopenharmony_ci        ICU_OVERRIDE=$1
872e5b6d6dSopenharmony_ci        shift
882e5b6d6dSopenharmony_cifi
892e5b6d6dSopenharmony_ci
902e5b6d6dSopenharmony_ciif [ $1 = "-O" ];
912e5b6d6dSopenharmony_cithen
922e5b6d6dSopenharmony_ci    shift
932e5b6d6dSopenharmony_ci    XTRA_OPTS=$1
942e5b6d6dSopenharmony_ci    shift
952e5b6d6dSopenharmony_cifi
962e5b6d6dSopenharmony_ci
972e5b6d6dSopenharmony_ciif [ ! -x "${ICU_CONFIG}" ];
982e5b6d6dSopenharmony_cithen
992e5b6d6dSopenharmony_ci    ICU_CONFIG=`which icu-config 2>/dev/null || echo`
1002e5b6d6dSopenharmony_cifi
1012e5b6d6dSopenharmony_ci
1022e5b6d6dSopenharmony_ci
1032e5b6d6dSopenharmony_ci# now, search
1042e5b6d6dSopenharmony_ciif [ -x ~/.icurunrc ];
1052e5b6d6dSopenharmony_cithen
1062e5b6d6dSopenharmony_ci        . ~/.icurunrc
1072e5b6d6dSopenharmony_cifi
1082e5b6d6dSopenharmony_ci
1092e5b6d6dSopenharmony_ciIN_SOURCE=
1102e5b6d6dSopenharmony_ciICU_CONFIG_OPTS=
1112e5b6d6dSopenharmony_ci
1122e5b6d6dSopenharmony_ciif [ "x${ICU_OVERRIDE}" != "x" ];
1132e5b6d6dSopenharmony_cithen
1142e5b6d6dSopenharmony_ci        if [ -f "${ICU_OVERRIDE}" -a -x "${ICU_OVERRIDE}" ];
1152e5b6d6dSopenharmony_ci        then
1162e5b6d6dSopenharmony_ci                ICU_CONFIG="${ICU_OVERRIDE}"
1172e5b6d6dSopenharmony_ci        elif [ -x "${ICU_OVERRIDE}/icu-config" ];
1182e5b6d6dSopenharmony_ci        then
1192e5b6d6dSopenharmony_ci                ICU_CONFIG="${ICU_OVERRIDE}/icu-config"
1202e5b6d6dSopenharmony_ci        elif [ -x "${ICU_OVERRIDE}/bin/icu-config" ];
1212e5b6d6dSopenharmony_ci        then
1222e5b6d6dSopenharmony_ci                ICU_CONFIG="${ICU_OVERRIDE}/bin/icu-config"
1232e5b6d6dSopenharmony_ci        elif [ -x "${ICU_OVERRIDE}/source/config/icu-config" ];
1242e5b6d6dSopenharmony_ci        then
1252e5b6d6dSopenharmony_ci                ICU_CONFIG="${ICU_OVERRIDE}/source/config/icu-config"
1262e5b6d6dSopenharmony_ci                IN_SOURCE="${ICU_OVERRIDE}/source"
1272e5b6d6dSopenharmony_ci        elif [ -x "${ICU_OVERRIDE}/config/icu-config" ];
1282e5b6d6dSopenharmony_ci        then
1292e5b6d6dSopenharmony_ci                ICU_CONFIG="${ICU_OVERRIDE}/config/icu-config"
1302e5b6d6dSopenharmony_ci                IN_SOURCE="${ICU_OVERRIDE}"
1312e5b6d6dSopenharmony_ci        else
1322e5b6d6dSopenharmony_ci                echo "$0: Don't know what to do with $ICU_OVERRIDE - not an executable or a directory containing ICU source or install" >&2
1332e5b6d6dSopenharmony_ci                exit 1
1342e5b6d6dSopenharmony_ci        fi
1352e5b6d6dSopenharmony_cifi
1362e5b6d6dSopenharmony_ci
1372e5b6d6dSopenharmony_ciif [ ! -x "${ICU_CONFIG}" ];
1382e5b6d6dSopenharmony_cithen
1392e5b6d6dSopenharmony_ci    echo "$0: Error: \"${ICU_CONFIG}\" is not an icu-config script. Goodbye." >&2
1402e5b6d6dSopenharmony_ci    exit 1
1412e5b6d6dSopenharmony_cifi
1422e5b6d6dSopenharmony_ci
1432e5b6d6dSopenharmony_ciif ! fgrep -q -s noverify "${ICU_CONFIG}" ;
1442e5b6d6dSopenharmony_cithen
1452e5b6d6dSopenharmony_ci    rm -rf  "${ICU_CONFIG}".junk
1462e5b6d6dSopenharmony_ci    grep -v '^sanity$' < "${ICU_CONFIG}" > "${ICU_CONFIG}".junk
1472e5b6d6dSopenharmony_ci    chmod u+rwx  "${ICU_CONFIG}".junk
1482e5b6d6dSopenharmony_ci    ICU_CONFIG="${ICU_CONFIG}.junk"
1492e5b6d6dSopenharmony_cielse
1502e5b6d6dSopenharmony_ci    SANITY_OPTS=--noverify
1512e5b6d6dSopenharmony_cifi
1522e5b6d6dSopenharmony_ci
1532e5b6d6dSopenharmony_ciif [ -d "${IN_SOURCE}" ];
1542e5b6d6dSopenharmony_cithen
1552e5b6d6dSopenharmony_ci    echo "ICU workspace dir:" "${IN_SOURCE}"
1562e5b6d6dSopenharmony_ci    # revisit
1572e5b6d6dSopenharmony_ci    if [ -f "${IN_SOURCE}/common/unicode/utypes.h" ];
1582e5b6d6dSopenharmony_ci    then
1592e5b6d6dSopenharmony_ci        top_srcdir="${IN_SOURCE}"
1602e5b6d6dSopenharmony_ci    else
1612e5b6d6dSopenharmony_ci        top_srcdir=`sed -n -e 's%^top_srcdir = \(.*\)%\1%p' < "${IN_SOURCE}/Makefile"`
1622e5b6d6dSopenharmony_ci        if [ ! -d "${top_srcdir}" ];
1632e5b6d6dSopenharmony_ci        then
1642e5b6d6dSopenharmony_ci            echo "Sorry: cannot find top_srcdir from ${IN_SOURCE}/Makefile"
1652e5b6d6dSopenharmony_ci            exit 1
1662e5b6d6dSopenharmony_ci        fi
1672e5b6d6dSopenharmony_ci    fi
1682e5b6d6dSopenharmony_ci    if [ ! -f "${top_srcdir}/common/unicode/utypes.h" ];
1692e5b6d6dSopenharmony_ci    then
1702e5b6d6dSopenharmony_ci        echo Sorry: cannot find "${top_srcdir}/common/unicode/utypes.h"
1712e5b6d6dSopenharmony_ci        exit 1
1722e5b6d6dSopenharmony_ci    fi
1732e5b6d6dSopenharmony_ci    echo "ICU top level source dir:" "${top_srcdir}"
1742e5b6d6dSopenharmony_ci    
1752e5b6d6dSopenharmony_ci    ICU_CONFIG_OPTS=${SANITY_OPTS}
1762e5b6d6dSopenharmony_ci    LINKOPTS="--ldflags-libsonly --ldflags-system --ldflags-icuio"
1772e5b6d6dSopenharmony_ci    OURLIBPATH=${IN_SOURCE}/lib:${IN_SOURCE}/stubdata:${IN_SOURCE}/tools/ctestfw
1782e5b6d6dSopenharmony_ci    SRC_OPTS="-I${top_srcdir}/common -I${top_srcdir}/i18n  -I${top_srcdir}/io -I${top_srcdir}/tools/ctestfw -I${top_srcdir}/tools/toolutil -I${IN_SOURCE}/common -L${IN_SOURCE}/lib -L${IN_SOURCE}/stubdata -L${IN_SOURCE}/tools/ctestfw"
1792e5b6d6dSopenharmony_ci    INVOKE=`${ICU_CONFIG} ${ICU_CONFIG_OPTS} --invoke`:"${OURLIBPATH}"
1802e5b6d6dSopenharmony_cielse
1812e5b6d6dSopenharmony_ci    # no cppflags for in-source
1822e5b6d6dSopenharmony_ci    CPPOPTS="--cppflags"
1832e5b6d6dSopenharmony_ci    LINKOPTS="--ldflags --ldflags-icuio"
1842e5b6d6dSopenharmony_ci    INVOKE=`${ICU_CONFIG} ${ICU_CONFIG_OPTS} --invoke`
1852e5b6d6dSopenharmony_cifi
1862e5b6d6dSopenharmony_ci
1872e5b6d6dSopenharmony_ciecho 'ICU ' version: `${ICU_CONFIG} ${ICU_CONFIG_OPTS} --version` prefix: `${ICU_CONFIG} ${ICU_CONFIG_OPTS} --prefix`
1882e5b6d6dSopenharmony_ci
1892e5b6d6dSopenharmony_ciFILE=$1
1902e5b6d6dSopenharmony_cishift
1912e5b6d6dSopenharmony_ci
1922e5b6d6dSopenharmony_ciif [ ! -f "${FILE}" ];
1932e5b6d6dSopenharmony_cithen
1942e5b6d6dSopenharmony_ci    echo "$0: Can't open ${FILE}" >&2
1952e5b6d6dSopenharmony_ci    usage
1962e5b6d6dSopenharmony_ci    exit 1
1972e5b6d6dSopenharmony_cifi
1982e5b6d6dSopenharmony_ci
1992e5b6d6dSopenharmony_ci
2002e5b6d6dSopenharmony_ci
2012e5b6d6dSopenharmony_cicase "${FILE}" in
2022e5b6d6dSopenharmony_ci     *.cpp)
2032e5b6d6dSopenharmony_ci        COMP=`${ICU_CONFIG} ${ICU_CONFIG_OPTS} --cxx --cxxflags ${CPPOPTS} ${LINKOPTS}`
2042e5b6d6dSopenharmony_ci        OUT=`basename ${FILE} .cpp`
2052e5b6d6dSopenharmony_ci     ;;
2062e5b6d6dSopenharmony_ci
2072e5b6d6dSopenharmony_ci     *.c)
2082e5b6d6dSopenharmony_ci        COMP=`${ICU_CONFIG}  ${ICU_CONFIG_OPTS} --cc --cflags ${CPPOPTS} ${LINKOPTS}`
2092e5b6d6dSopenharmony_ci        OUT=`basename ${FILE} .c`
2102e5b6d6dSopenharmony_ci     ;;
2112e5b6d6dSopenharmony_ci
2122e5b6d6dSopenharmony_ci     *)
2132e5b6d6dSopenharmony_ci        echo "$0: error, don't know what to do with ${FILE}" >&2
2142e5b6d6dSopenharmony_ci        exit 1
2152e5b6d6dSopenharmony_ci     ;;
2162e5b6d6dSopenharmony_ciesac
2172e5b6d6dSopenharmony_ci
2182e5b6d6dSopenharmony_ciecho "# ${COMP}" "${SRC_OPTS}" -o "${OUT}" "${FILE}" "${XTRA_OPTS}"
2192e5b6d6dSopenharmony_ci( ${COMP} ${SRC_OPTS} -o "${OUT}" "${FILE}" ${XTRA_OPTS} || (rm -f "${OUT}" ; exit 1) )  && ( echo "# ${INVOKE} ${LEAK_CHECKER} ./${OUT}" "$@" ; "${SHELL}" -c "${INVOKE} ${LEAK_CHECKER} ./${OUT} $*")
220