exe 3.9 KB
Newer Older
S
Shengliang Guan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
#!/bin/bash
#*****************************************************************************#
#                                                                             #
#  Filename:	    exe							      #
#                                                                             #
#  Description:     Build simple C/C++ programs for Unix/Linux		      #
#                                                                             #
#  Notes:	    Usage: ./exe PROGRAM				      #
#                                                                             #
#                   Stores the executables in $OS.$PROC/[Debug/] for	      #
#                   consistency with the Windows build tools.                 #
#                   This allows sharing sources in a host system, and         #
#                   using VMs for building the various Windows and Linux      #
#                   versions in a set of OS-specific subdirectories.          #
#                                                                             #
#  History:                                                                   #
#    2013-12-16 JFL Added support for MinGW64.				      #
#    2015-12-12 JFL Help now displays the output directory name.              #
#    2016-01-07 JFL Added compilation option -Wall.                           #
#                                                                             #
#          Copyright 2016 Hewlett Packard Enterprise Development LP          #
# Licensed under the Apache 2.0 license - www.apache.org/licenses/LICENSE-2.0 #
#*****************************************************************************#

FileNoCase() # Case-independant search for a file.
{
  find . -type f | grep -i -E "./$1$" | sed s=./==
}

# Identify the OS
OS=`uname -s`
PROC=`uname -p`
if [[ "$OS" == "OSF1" && "`uname -m`" == "alpha" ]] ; then
  OS=Tru64
fi
if [[ "$OS" == "WindowsNT" ]] ; then
  OS=WIN32
fi
OUTDIR=$OS.$PROC
if [[ "${OS:0:7}" == "MINGW32" ]] ; then # Ex: "MINGW32_NT-6.1"
  OUTDIR=MINGW32 # MigGW shell if NOT case sensitive
  # 2013-12-16 Actually, the 64-bits tool chain also reports MINGW32_NT-6.1
  # So distinguish the two by whether /mingw is mounted on C:\MinGW or C:\MinGW64
  if mount | grep /mingw | grep 64 > /dev/null ; then
    OUTDIR=MINGW64 # MigGW shell if NOT case sensitive
  fi
fi
if [[ "${OS:0:7}" == "MINGW64" ]] ; then # Ex: ?
  OUTDIR=MINGW64
fi
if [[ "${OS:0:6}" == "CYGWIN" ]] ; then # Ex: "CYGWIN_NT-6.1-WOW64"
  OUTDIR=cygwin # Cygwin shell if case sensitive, so use lower case
fi

# Command line analysis.
case "$1" in
  "" | "-h" | "-?" | --help)
    echo "Build simple C/C++ programs, storing the executables in $OUTDIR/"
    echo "Usage: ./exe PROGRAM"
    exit 0
    ;;
esac

# Identify the source file and program to build.
PROGRAM=$1
shift
SOURCES=`FileNoCase ${PROGRAM}.c`
CFLAGS="-std=c99 -Wall" # Force compilation in C, even if there are // comments.
if [[ "${SOURCES}" == "" ]] ; then
  SOURCES=`FileNoCase ${PROGRAM}.cpp`
  CFLAGS="-std=gnu++98 -lstdc++"  # Force compilation in C++, even if plain C.
  # -lstdc++ prevents error "undefined reference to '__gxx_personality_v0'"
fi
if [[ "${SOURCES}" == "" ]] ; then
  echo "Failed to find ${PROGRAM} source."
  exit 1
fi

# Make sure our include directories are accessible
if [[ -d "/u/JFL/SRC/Include" ]] ; then
  if [[ ":$C_INCLUDE_PATH:" != *:/u/JFL/SRC/Include:* ]] ; then
    if [[ "$C_INCLUDE_PATH" == "" ]] ; then
      export C_INCLUDE_PATH="/u/JFL/SRC/Include"
    else
      export C_INCLUDE_PATH="$C_INCLUDE_PATH:/u/JFL/SRC/Include"
    fi
  fi
fi
echo "# C_INCLUDE_PATH=\"$C_INCLUDE_PATH\""

# Build it.
# gmake CC=gcc CFLAGS="$CFLAGS" SOURCES="$SOURCES" PROGRAM="$PROGRAM" OS="$OS" $*
mkdir -p $OUTDIR
echo "gcc $CFLAGS -U_DEBUG $SOURCES -o $OUTDIR/$PROGRAM"
gcc $CFLAGS -U_DEBUG $SOURCES -o $OUTDIR/$PROGRAM
mkdir -p $OUTDIR/debug
echo "gcc $CFLAGS -D_DEBUG $SOURCES -o $OUTDIR/debug/$PROGRAM"
gcc $CFLAGS -D_DEBUG $SOURCES -o $OUTDIR/debug/$PROGRAM