1 #!/bin/sh
   2 
   3 #
   4 # Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
   5 # DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   6 #
   7 # This code is free software; you can redistribute it and/or modify it
   8 # under the terms of the GNU General Public License version 2 only, as
   9 # published by the Free Software Foundation.
  10 #
  11 # This code is distributed in the hope that it will be useful, but WITHOUT
  12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13 # FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14 # version 2 for more details (a copy is included in the LICENSE file that
  15 # accompanied this code).
  16 #
  17 # You should have received a copy of the GNU General Public License version
  18 # 2 along with this work; if not, write to the Free Software Foundation,
  19 # Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20 #
  21 # Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22 # or visit www.oracle.com if you need additional information or have any
  23 # questions.
  24 #
  25 
  26 # This file should be used to set the Visual Studio environment
  27 #   variables normally set by the vcvars32.bat or vcvars64.bat file or
  28 #   SetEnv.cmd for older SDKs.
  29 
  30 # Use cygpath?
  31 isCygwin="`uname -s | grep CYGWIN`"
  32 if [ "${isCygwin}" != "" ] ; then
  33   cygpath="/usr/bin/cygpath"
  34   cygpath_short="${cygpath} -m -s"
  35   cygpath_windows="${cygpath} -w -s"
  36   cygpath_path="${cygpath} -p"
  37   pathsep=':'
  38 else
  39   cygpath="dosname"
  40   cygpath_short="${cygpath} -s"
  41   cygpath_windows="${cygpath} -s"
  42   cygpath_path="echo"
  43   pathsep=';'
  44 fi
  45 
  46 ########################################################################
  47 # Error functions
  48 msg() # message
  49 {
  50   echo "$1" 1>&2
  51 }
  52 error() # message
  53 {
  54   msg "ERROR: $1"
  55   exit 1
  56 }
  57 warning() # message
  58 {
  59   msg "WARNING: $1"
  60 }
  61 envpath() # path
  62 {
  63   if [ "${cygpath_short}" != "" -a -d "$1" ] ; then
  64     ${cygpath_short} "$1"
  65   else
  66     echo "$1"
  67   fi
  68 }
  69 ########################################################################
  70 
  71 
  72 # Defaults settings
  73 debug="false"
  74 verbose="false"
  75 shellStyle="sh"
  76 parentCsh="` ps -p ${PPID} 2> /dev/null | grep csh `"
  77 if [ "${parentCsh}" != "" ] ; then
  78   shellStyle="csh"
  79 fi
  80 
  81 set -e
  82 
  83 # Check environment first
  84 if [ "${PROGRAMFILES}" != "" ] ; then
  85   progfiles=`envpath "${PROGRAMFILES}"`
  86 elif [ "${ProgramFiles}" != "" ] ; then
  87   progfiles=`envpath "${ProgramFiles}"`
  88 elif [ "${SYSTEMDRIVE}" != "" ] ; then
  89   progfiles=`envpath "${SYSTEMDRIVE}/Program Files"`
  90 elif [ "${SystemDrive}" != "" ] ; then
  91   progfiles=`envpath "${SystemDrive}/Program Files"`
  92 else
  93   error "No PROGRAMFILES or SYSTEMDRIVE defined in environment"
  94 fi
  95 
  96 # Arch data model
  97 if [ "${PROCESSOR_IDENTIFIER}" != "" ] ; then
  98   arch=`echo "${PROCESSOR_IDENTIFIER}" | cut -d' ' -f1`
  99 elif [ "${MACHTYPE}" != "" ] ; then
 100   if [ "`echo ${MACHTYPE} | grep 64`" != "" ] ; then
 101     # Assume this is X64, not IA64
 102     arch="x64"
 103   else
 104     arch="x86"
 105   fi
 106 else
 107  arch="`uname -m`"
 108 fi
 109 if [ "${arch}" = "X86" -o \
 110      "${arch}" = "386" -o "${arch}" = "i386" -o \
 111      "${arch}" = "486" -o "${arch}" = "i486" -o \
 112      "${arch}" = "586" -o "${arch}" = "i586" -o \
 113      "${arch}" = "686" -o "${arch}" = "i686" -o \
 114      "${arch}" = "86" ] ; then
 115   arch="x86"
 116 fi
 117 if [ "${arch}" = "X64"     -o \
 118      "${arch}" = "8664"    -o "${arch}" = "i8664"   -o \
 119      "${arch}" = "amd64"   -o "${arch}" = "AMD64"   -o \
 120      "${arch}" = "EM64T"   -o "${arch}" = "emt64t"  -o \
 121      "${arch}" = "intel64" -o "${arch}" = "Intel64" -o \
 122      "${arch}" = "64" ] ; then
 123   arch="x64"
 124   binarch64="/amd64"
 125 fi
 126 if [ "${arch}" = "IA64" ] ; then
 127   arch="ia64"
 128   binarch64="/ia64"
 129 fi
 130 if [ "${arch}" != "x86" -a "${arch}" != "x64" -a "${arch}" != "ia64" ] ; then
 131  error "No PROCESSOR_IDENTIFIER or MACHTYPE environment variables and uname -m is not helping"
 132 fi
 133 if [ "${arch}" = "x86" ] ; then
 134   arch_data_model=32
 135   progfiles32="${progfiles}"
 136   progfiles64="${progfiles}"
 137 else
 138   arch_data_model=64
 139   progfiles32="${progfiles}"
 140   if [ "${PROGRAMW6432}" != "" ] ; then
 141     progfiles64=`envpath "${PROGRAMW6432}"`
 142   else
 143     progfiles64=`envpath "C:/Program Files"`
 144   fi
 145 fi
 146 
 147 # VS2010 (VC10)
 148 if [ "${VS100COMNTOOLS}" = "" ] ; then
 149   VS100COMNTOOLS="${progfiles32}/Microsoft Visual Studio 10.0/Common7/Tools/"
 150   export VS100COMNTOOLS
 151 fi
 152 vc10Bin32Dir=`envpath "${VS100COMNTOOLS}"`/../../VC/Bin
 153 vc10Bin64Dir="${vc10Bin32Dir}${binarch64}"
 154 vc10vars32Bat="vcvars32.bat"
 155 vc10vars64Bat="vcvars64.bat"
 156 
 157 # VS2008 (VC9)
 158 if [ "${VS90COMNTOOLS}" = "" ] ; then
 159   VS90COMNTOOLS="${progfiles32}/Microsoft Visual Studio 9.0/Common7/Tools/"
 160   export VS90COMNTOOLS
 161 fi
 162 vc9Bin32Dir=`envpath "${VS90COMNTOOLS}"`/../../VC/Bin
 163 vc9Bin64Dir="${vc9Bin32Dir}"
 164 vc9vars32Bat="vcvars32.bat"
 165 vc9vars64Bat="vcvars64.bat"
 166 
 167 # VS2005 (VC8)
 168 if [ "${VS80COMNTOOLS}" = "" ] ; then
 169   VS80COMNTOOLS="${progfiles32}/Microsoft Visual Studio 8.0/Common7/Tools/"
 170   export VS80COMNTOOLS
 171 fi
 172 vc8Bin32Dir=`envpath "${VS80COMNTOOLS}"`/../../VC/Bin
 173 vc8Bin64Dir="${progfiles64}/Microsoft Platform SDK"
 174 vc8vars32Bat="vcvars32.bat"
 175 vc8vars64Bat="SetEnv.cmd /X64"
 176 
 177 # VS2003 (VC7)
 178 if [ "${VS71COMNTOOLS}" = "" ] ; then
 179   VS71COMNTOOLS="${progfiles32}/Microsoft Visual Studio .NET 2003/Common7/Tools/"
 180   export VS71COMNTOOLS
 181 fi
 182 vc7Bin32Dir=`envpath "${VS71COMNTOOLS}"`/../../VC7/Bin
 183 vc7Bin64Dir="${progfiles64}/Microsoft Platform SDK"
 184 vc7vars32Bat="vcvars32.bat"
 185 vc7vars64Bat="SetEnv.cmd /X64"
 186 
 187 # Force user to select
 188 vcSelection=""
 189 
 190 # Parse options
 191 usage="Usage: $0 [-help] [-debug] [-v] [-c] [-s] [-p] [-v10] [-v9] [-v8] [-v7] [-32] [-64]"
 192 while [ $# -gt 0 ] ; do
 193   if [ "$1" = "-help" ] ; then
 194     msg "${usage}"
 195     msg "  -help    Print out this help message"
 196     msg "  -debug   Print out extra env variables to help debug this script"
 197     msg "  -v       Verbose output warns about missing directories"
 198     msg "  -c       Print out csh style output"
 199     msg "  -s       Print out sh style output"
 200     msg "  -p       Print out properties style output"
 201     msg "  -v10     Use Visual Studio 10 VS2010"
 202     msg "  -v9      Use Visual Studio 9 VS2008"
 203     msg "  -v8      Use Visual Studio 8 VS2005"
 204     msg "  -v7      Use Visual Studio 7 VS2003"
 205     msg "  -32      Force 32bit"
 206     msg "  -64      Force 64bit"
 207     exit 0
 208   elif [ "$1" = "-debug" ] ; then
 209     debug="true"
 210     shift
 211   elif [ "$1" = "-v" ] ; then
 212     verbose="true"
 213     shift
 214   elif [ "$1" = "-c" ] ; then
 215     shellStyle="csh"
 216     shift
 217   elif [ "$1" = "-s" ] ; then
 218     shellStyle="sh"
 219     shift
 220   elif [ "$1" = "-p" ] ; then
 221     shellStyle="props"
 222     shift
 223   elif [ "$1" = "-v10" ] ; then
 224     vcBin32Dir="${vc10Bin32Dir}"
 225     vcBin64Dir="${vc10Bin64Dir}"
 226     vcvars32Bat="${vc10vars32Bat}"
 227     vcvars64Bat="${vc10vars64Bat}"
 228     vcSelection="10"
 229     shift
 230   elif [ "$1" = "-v9" ] ; then
 231     vcBin32Dir="${vc9Bin32Dir}"
 232     vcBin64Dir="${vc9Bin64Dir}"
 233     vcvars32Bat="${vc9vars32Bat}"
 234     vcvars64Bat="${vc9vars64Bat}"
 235     vcSelection="9"
 236     shift
 237   elif [ "$1" = "-v8" ] ; then
 238     vcBin32Dir="${vc8Bin32Dir}"
 239     vcBin64Dir="${vc8Bin64Dir}"
 240     vcvars32Bat="${vc8vars32Bat}"
 241     vcvars64Bat="${vc8vars64Bat}"
 242     vcSelection="8"
 243     shift
 244   elif [ "$1" = "-v7" ] ; then
 245     vcBin32Dir="${vc7Bin32Dir}"
 246     vcBin64Dir="${vc7Bin64Dir}"
 247     vcvars32Bat="${vc7vars32Bat}"
 248     vcvars64Bat="${vc7vars64Bat}"
 249     vcSelection="7"
 250     shift
 251   elif [ "$1" = "-32" ] ; then
 252     arch_data_model=32
 253     shift
 254   elif [ "$1" = "-64" ] ; then
 255     arch_data_model=64
 256     shift
 257   else
 258     msg "${usage}"
 259     error "Unknown option: $1"
 260   fi
 261 done
 262 
 263 # Need to pick
 264 if [ "${vcSelection}" = "" ] ; then
 265   msg "${usage}"
 266   error "You must pick the version"
 267 fi
 268 
 269 # Which vcvars bat file to run
 270 if [ "${arch_data_model}" = "32" ] ; then
 271   vcBinDir="${vcBin32Dir}"
 272   vcvarsBat="${vcvars32Bat}"
 273 fi
 274 if [ "${arch_data_model}" = "64" ] ; then
 275   vcBinDir="${vcBin64Dir}"
 276   vcvarsBat="${vcvars64Bat}"
 277 fi
 278 
 279 # Do not allow any error returns
 280 set -e
 281 
 282 # Different systems have different awk's
 283 if [ -f /usr/bin/nawk ] ; then
 284   awk="nawk"
 285 elif [ -f /usr/bin/gawk ] ; then
 286   awk="gawk"
 287 else
 288   awk="awk"
 289 fi
 290 
 291 if [ "${verbose}" = "true" ] ; then
 292   echo "# Welcome to verbose mode"
 293   set -x
 294 fi
 295 
 296 if [ "${debug}" = "true" ] ; then
 297   echo "# Welcome to debug mode"
 298   set -x
 299 fi
 300 
 301 # Temp file area
 302 tmp="/tmp/vsvars.$$"
 303 rm -f -r ${tmp}
 304 mkdir -p ${tmp}
 305 
 306 # Check paths
 307 checkPaths() # var path sep
 308 {
 309   set -e
 310   sep="$3"
 311   checklist="${tmp}/checklist"
 312   printf "%s\n" "$2" | \
 313     sed -e 's@\\@/@g' | \
 314     sed -e 's@//@/@g' | \
 315     ${awk} -F"${sep}" '{for(i=1;i<=NF;i++){printf "%s\n",$i;}}'  \
 316       > ${checklist}
 317   cat ${checklist} | while read orig; do
 318     if [ "${orig}" != "" ] ; then
 319       if [ ! -d "${orig}" ] ; then
 320         warning "Directory in $1 does not exist: ${orig}"
 321       fi
 322     fi
 323   done
 324 }
 325 
 326 # Remove all duplicate entries
 327 removeDeadDups() # string sep
 328 {
 329   set -e
 330   sep="$2"
 331   pathlist="${tmp}/pathlist"
 332   printf "%s\n" "$1" | \
 333     sed -e 's@\\@/@g' | \
 334     sed -e 's@//@/@g' | \
 335     ${awk} -F"${sep}" '{for(i=1;i<=NF;i++){printf "%s\n",$i;}}'  \
 336       > ${pathlist}
 337   upaths="${tmp}/upaths"
 338   cat ${pathlist} | while read orig; do
 339     p="${orig}"
 340     if [ "${cygpath_short}" != "" ] ; then
 341       if [ "${p}" != "" ] ; then
 342         if [ -d "${p}" ] ; then
 343           short=`${cygpath_short} "${p}"`
 344           if [ "${short}" != "" -a -d "${short}" ] ; then
 345             p=`${cygpath} "${short}"`
 346           fi
 347           echo "${p}" >> ${upaths}
 348         fi
 349       fi
 350     fi
 351   done
 352   newpaths=""
 353   for i in  `cat ${upaths}` ; do
 354     # For some reason, \r characters can get into this
 355     i=`echo "${i}" | tr -d '\r' | sed -e 's@/$@@'`
 356     if [ "${newpaths}" = "" ] ; then
 357       newpaths="${i}"
 358     else
 359       newpaths="${newpaths}${sep}${i}"
 360     fi
 361   done
 362   printf "%s\n" "${newpaths}" | \
 363     ${awk} -F"${sep}" \
 364        '{a[$1];printf "%s",$1;for(i=2;i<=NF;i++){if(!($i in a)){a[$i];printf "%s%s",FS,$i;}};printf "\n";}'
 365 }
 366 
 367 # Create bat file to process Visual Studio vcvars*.bat files
 368 createBat() # batfile bindir command
 369 {
 370   bat="$1"
 371   bindir="$2"
 372   command="$3"
 373   stdout="${bat}.stdout"
 374   rm -f ${bat} ${stdout}
 375   echo "Output from: ${command}" > ${stdout}
 376   bdir=`envpath "${bindir}"`
 377   cat > ${bat} << EOF  
 378 REM Pick the right vcvars bat file
 379 REM Empty these out so we only get the additions we want
 380 set INCLUDE=
 381 set LIB=
 382 set LIBPATH=
 383 set MSVCDIR=
 384 set MSSdk=
 385 set Mstools=
 386 set DevEnvDir=
 387 set VCINSTALLDIR=
 388 set VSINSTALLDIR=
 389 set WindowsSdkDir=
 390 REM Run the vcvars bat file, send all output to stderr
 391 call `${cygpath_windows} ${bdir}`\\${command} > `${cygpath_windows} "${stdout}"`
 392 REM Echo out env var settings
 393 echo VS_VS71COMNTOOLS="%VS71COMNTOOLS%"
 394 echo export VS_VS71COMNTOOLS
 395 echo VS_VS80COMNTOOLS="%VS80COMNTOOLS%"
 396 echo export VS_VS80COMNTOOLS
 397 echo VS_VS90COMNTOOLS="%VS90COMNTOOLS%"
 398 echo export VS_VS90COMNTOOLS
 399 echo VS_VS100COMNTOOLS="%VS100COMNTOOLS%"
 400 echo export VS_VS100COMNTOOLS
 401 echo VS_VCINSTALLDIR="%VCINSTALLDIR%"
 402 echo export VS_VCINSTALLDIR
 403 echo VS_VSINSTALLDIR="%VSINSTALLDIR%"
 404 echo export VS_VSINSTALLDIR
 405 echo VS_DEVENVDIR="%DevEnvDir%"
 406 echo export VS_DEVENVDIR
 407 echo VS_MSVCDIR="%MSVCDIR%"
 408 echo export VS_MSVCDIR
 409 echo VS_MSSDK="%MSSdk%"
 410 echo export VS_MSSDK
 411 echo VS_MSTOOLS="%Mstools%"
 412 echo export VS_MSTOOLS
 413 echo VS_WINDOWSSDKDIR="%WindowsSdkDir%"
 414 echo export VS_WINDOWSSDKDIR
 415 echo VS_INCLUDE="%INCLUDE%"
 416 echo export VS_INCLUDE
 417 echo VS_LIB="%LIB%"
 418 echo export VS_LIB
 419 echo VS_LIBPATH="%LIBPATH%"
 420 echo export VS_LIBPATH
 421 echo VS_WPATH="%PATH%"
 422 echo export VS_WPATH
 423 EOF
 424   chmod a+x ${bat}
 425 }
 426 
 427 # Create env file
 428 createEnv() # batfile envfile
 429 {
 430   rm -f ${1}.stdout
 431   cmd.exe /Q /C `${cygpath_short} $1` | \
 432     sed -e 's@\\@/@g' | \
 433     sed -e 's@//@/@g' > $2
 434   if [ -f "${1}.stdout" ] ; then
 435     cat ${1}.stdout 1>&2
 436   fi
 437   chmod a+x $2
 438 }
 439 
 440 printEnv() # name pname vsname val
 441 {
 442   name="$1"
 443   pname="$2"
 444   vsname="$3"
 445   val="$4"
 446   if [ "${val}" != "" ] ; then
 447     if [ "${shellStyle}" = "csh" ] ; then
 448       if [ "${debug}" = "true" ] ; then
 449         echo "setenv ${vsname} \"${val}\";"
 450       fi
 451       echo "setenv ${name} \"${val}\";"
 452     elif [ "${shellStyle}" = "sh" ] ; then
 453       if [ "${debug}" = "true" ] ; then
 454         echo "${vsname}=\"${val}\";"
 455         echo "export ${vsname};"
 456       fi
 457       echo "${name}=\"${val}\";"
 458       echo "export ${name};"
 459     elif [ "${shellStyle}" = "props" ] ; then
 460       echo "vs.${pname}=${val}"
 461     fi
 462   fi
 463 }
 464 
 465 #############################################################################
 466 
 467 # Get Visual Studio settings
 468 if [ "${cygpath}" != "" ] ; then
 469 
 470   # Create bat file to run
 471   batfile="${tmp}/vs-to-env.bat"
 472   if [ ! -d "${vcBinDir}" ] ; then
 473     error "Does not exist: ${vcBinDir}"
 474   elif [ "${vcvarsBat}" = "" ] ; then
 475     error "No vcvars script: ${vcvarsBat}"
 476   else
 477     createBat "${batfile}" "${vcBinDir}" "${vcvarsBat}"
 478   fi
 479 
 480   # Run bat file to create environment variable settings
 481   envfile="${tmp}/env.sh"
 482   createEnv "${batfile}" "${envfile}"
 483 
 484   # Read in the VS_* settings
 485   . ${envfile}
 486 
 487   # Derive unix style path, save old, and define new (remove dups)
 488   VS_UPATH=`${cygpath_path} "${VS_WPATH}"`
 489   export VS_UPATH
 490   VS_OPATH=`printf "%s" "${PATH}" | sed -e 's@\\\\@/@g'`
 491   export VS_OPATH
 492   VS_PATH=`removeDeadDups "${VS_UPATH}${pathsep}${VS_OPATH}" "${pathsep}"`
 493   export VS_PATH
 494 
 495 fi
 496 
 497 # Adjustments due to differences in vcvars*bat files
 498 if [ "${VS_MSVCDIR}" = "" ] ; then
 499   VS_MSVCDIR="${VS_VCINSTALLDIR}"
 500 fi
 501 if [ "${VS_DEVENVDIR}" = "" ] ; then
 502   VS_DEVENVDIR="${VS_VSINSTALLDIR}"
 503 fi
 504 
 505 # Print env settings
 506 #        env           vs.prop       vs env           value
 507 #        -------       -------       ----------       -----
 508 printEnv INCLUDE       include       VS_INCLUDE       "${VS_INCLUDE}"
 509 printEnv LIB           lib           VS_LIB           "${VS_LIB}"
 510 printEnv LIBPATH       libpath       VS_LIBPATH       "${VS_LIBPATH}"
 511 if [ "${debug}" = "true" ] ; then
 512   printEnv UPATH         upath         VS_UPATH         "${VS_UPATH}"
 513   printEnv WPATH         wpath         VS_WPATH         "${VS_WPATH}"
 514   printEnv OPATH         opath         VS_OPATH         "${VS_OPATH}"
 515 fi
 516 printEnv PATH          path          VS_PATH          "${VS_PATH}"
 517 printEnv VCINSTALLDIR  vcinstalldir  VS_VCINSTALLDIR  "${VS_VCINSTALLDIR}"
 518 printEnv VSINSTALLDIR  vsinstalldir  VS_VSINSTALLDIR  "${VS_VSINSTALLDIR}"
 519 printEnv MSVCDIR       msvcdir       VS_MSVCDIR       "${VS_MSVCDIR}"
 520 printEnv MSSDK         mssdk         VS_MSSDK         "${VS_MSSDK}"
 521 printEnv MSTOOLS       mstools       VS_MSTOOLS       "${VS_MSTOOLS}"
 522 printEnv DEVENVDIR     devenvdir     VS_DEVENVDIR     "${VS_DEVENVDIR}"
 523 printEnv WINDOWSSDKDIR windowssdkdir VS_WINDOWSSDKDIR "${VS_WINDOWSSDKDIR}"
 524 if [ "${vcSelection}" = "10" ] ; then
 525   printEnv VS100COMNTOOLS vs100comntools VS_VS100COMNTOOLS "${VS_VS100COMNTOOLS}"
 526 elif [ "${vcSelection}" = "9" ] ; then
 527   printEnv VS90COMNTOOLS vs90comntools VS_VS90COMNTOOLS "${VS_VS90COMNTOOLS}"
 528 elif [ "${vcSelection}" = "7" ] ; then
 529   printEnv VS71COMNTOOLS vs71comntools VS_VS71COMNTOOLS "${VS_VS71COMNTOOLS}"
 530 elif [ "${vcSelection}" = "8" ] ; then
 531   printEnv VS80COMNTOOLS vs80comntools VS_VS80COMNTOOLS "${VS_VS80COMNTOOLS}"
 532 fi
 533 
 534 # Check final settings
 535 if [ "${verbose}" = "true" ] ; then
 536   checkPaths "Windows PATH" "${VS_WPATH}" ";"
 537   checkPaths LIB "${VS_LIB}" ";"
 538   checkPaths INCLUDE "${VS_INCLUDE}" ";"
 539   checkPaths PATH "${VS_PATH}" "${pathsep}"
 540 fi
 541 
 542 # Remove all temp files
 543 rm -f -r ${tmp}
 544 
 545 exit 0
 546