Tue, 30 Oct 2012 15:04:15 +0400
8001764: vsvars.sh should support VS2012
Summary: Update the vsvars.sh script to support VS2012
Reviewed-by: ohair, tbell
1 #!/bin/sh
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 #
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.
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
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 ########################################################################
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
81 set -e
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
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
147 # VS2012 (VC11)
148 if [ "${VS110COMNTOOLS}" = "" ] ; then
149 VS110COMNTOOLS="${progfiles32}/Microsoft Visual Studio 11.0/Common7/Tools/"
150 export VS110COMNTOOLS
151 fi
152 vc11Bin32Dir=`envpath "${VS110COMNTOOLS}"`/../../VC/Bin
153 vc11Bin64Dir="${vc11Bin32Dir}"
154 vc11vars32Bat="vcvars32.bat"
155 vc11vars64Bat="vcvars64.bat"
157 # VS2010 (VC10)
158 if [ "${VS100COMNTOOLS}" = "" ] ; then
159 VS100COMNTOOLS="${progfiles32}/Microsoft Visual Studio 10.0/Common7/Tools/"
160 export VS100COMNTOOLS
161 fi
162 vc10Bin32Dir=`envpath "${VS100COMNTOOLS}"`/../../VC/Bin
163 vc10Bin64Dir="${vc10Bin32Dir}${binarch64}"
164 vc10vars32Bat="vcvars32.bat"
165 vc10vars64Bat="vcvars64.bat"
167 # VS2008 (VC9)
168 if [ "${VS90COMNTOOLS}" = "" ] ; then
169 VS90COMNTOOLS="${progfiles32}/Microsoft Visual Studio 9.0/Common7/Tools/"
170 export VS90COMNTOOLS
171 fi
172 vc9Bin32Dir=`envpath "${VS90COMNTOOLS}"`/../../VC/Bin
173 vc9Bin64Dir="${vc9Bin32Dir}"
174 vc9vars32Bat="vcvars32.bat"
175 vc9vars64Bat="vcvars64.bat"
177 # VS2005 (VC8)
178 if [ "${VS80COMNTOOLS}" = "" ] ; then
179 VS80COMNTOOLS="${progfiles32}/Microsoft Visual Studio 8.0/Common7/Tools/"
180 export VS80COMNTOOLS
181 fi
182 vc8Bin32Dir=`envpath "${VS80COMNTOOLS}"`/../../VC/Bin
183 vc8Bin64Dir="${progfiles64}/Microsoft Platform SDK"
184 vc8vars32Bat="vcvars32.bat"
185 vc8vars64Bat="SetEnv.cmd /X64"
187 # VS2003 (VC7)
188 if [ "${VS71COMNTOOLS}" = "" ] ; then
189 VS71COMNTOOLS="${progfiles32}/Microsoft Visual Studio .NET 2003/Common7/Tools/"
190 export VS71COMNTOOLS
191 fi
192 vc7Bin32Dir=`envpath "${VS71COMNTOOLS}"`/../../VC7/Bin
193 vc7Bin64Dir="${progfiles64}/Microsoft Platform SDK"
194 vc7vars32Bat="vcvars32.bat"
195 vc7vars64Bat="SetEnv.cmd /X64"
197 # Force user to select
198 vcSelection=""
200 # Parse options
201 usage="Usage: $0 [-help] [-debug] [-v] [-c] [-s] [-p] [-v11] [-v10] [-v9] [-v8] [-v7] [-32] [-64]"
202 while [ $# -gt 0 ] ; do
203 if [ "$1" = "-help" ] ; then
204 msg "${usage}"
205 msg " -help Print out this help message"
206 msg " -debug Print out extra env variables to help debug this script"
207 msg " -v Verbose output warns about missing directories"
208 msg " -c Print out csh style output"
209 msg " -s Print out sh style output"
210 msg " -p Print out properties style output"
211 msg " -v11 Use Visual Studio 11 VS2012"
212 msg " -v10 Use Visual Studio 10 VS2010"
213 msg " -v9 Use Visual Studio 9 VS2008"
214 msg " -v8 Use Visual Studio 8 VS2005"
215 msg " -v7 Use Visual Studio 7 VS2003"
216 msg " -32 Force 32bit"
217 msg " -64 Force 64bit"
218 exit 0
219 elif [ "$1" = "-debug" ] ; then
220 debug="true"
221 shift
222 elif [ "$1" = "-v" ] ; then
223 verbose="true"
224 shift
225 elif [ "$1" = "-c" ] ; then
226 shellStyle="csh"
227 shift
228 elif [ "$1" = "-s" ] ; then
229 shellStyle="sh"
230 shift
231 elif [ "$1" = "-p" ] ; then
232 shellStyle="props"
233 shift
234 elif [ "$1" = "-v11" ] ; then
235 vcBin32Dir="${vc11Bin32Dir}"
236 vcBin64Dir="${vc11Bin64Dir}"
237 vcvars32Bat="${vc11vars32Bat}"
238 vcvars64Bat="${vc11vars64Bat}"
239 vcSelection="11"
240 shift
241 elif [ "$1" = "-v10" ] ; then
242 vcBin32Dir="${vc10Bin32Dir}"
243 vcBin64Dir="${vc10Bin64Dir}"
244 vcvars32Bat="${vc10vars32Bat}"
245 vcvars64Bat="${vc10vars64Bat}"
246 vcSelection="10"
247 shift
248 elif [ "$1" = "-v9" ] ; then
249 vcBin32Dir="${vc9Bin32Dir}"
250 vcBin64Dir="${vc9Bin64Dir}"
251 vcvars32Bat="${vc9vars32Bat}"
252 vcvars64Bat="${vc9vars64Bat}"
253 vcSelection="9"
254 shift
255 elif [ "$1" = "-v8" ] ; then
256 vcBin32Dir="${vc8Bin32Dir}"
257 vcBin64Dir="${vc8Bin64Dir}"
258 vcvars32Bat="${vc8vars32Bat}"
259 vcvars64Bat="${vc8vars64Bat}"
260 vcSelection="8"
261 shift
262 elif [ "$1" = "-v7" ] ; then
263 vcBin32Dir="${vc7Bin32Dir}"
264 vcBin64Dir="${vc7Bin64Dir}"
265 vcvars32Bat="${vc7vars32Bat}"
266 vcvars64Bat="${vc7vars64Bat}"
267 vcSelection="7"
268 shift
269 elif [ "$1" = "-32" ] ; then
270 arch_data_model=32
271 shift
272 elif [ "$1" = "-64" ] ; then
273 arch_data_model=64
274 shift
275 else
276 msg "${usage}"
277 error "Unknown option: $1"
278 fi
279 done
281 # Need to pick
282 if [ "${vcSelection}" = "" ] ; then
283 msg "${usage}"
284 error "You must pick the version"
285 fi
287 # Which vcvars bat file to run
288 if [ "${arch_data_model}" = "32" ] ; then
289 vcBinDir="${vcBin32Dir}"
290 vcvarsBat="${vcvars32Bat}"
291 fi
292 if [ "${arch_data_model}" = "64" ] ; then
293 vcBinDir="${vcBin64Dir}"
294 vcvarsBat="${vcvars64Bat}"
295 fi
297 # Do not allow any error returns
298 set -e
300 # Different systems have different awk's
301 if [ -f /usr/bin/nawk ] ; then
302 awk="nawk"
303 elif [ -f /usr/bin/gawk ] ; then
304 awk="gawk"
305 else
306 awk="awk"
307 fi
309 if [ "${verbose}" = "true" ] ; then
310 echo "# Welcome to verbose mode"
311 set -x
312 fi
314 if [ "${debug}" = "true" ] ; then
315 echo "# Welcome to debug mode"
316 set -x
317 fi
319 # Temp file area
320 tmp="/tmp/vsvars.$$"
321 rm -f -r ${tmp}
322 mkdir -p ${tmp}
324 # Check paths
325 checkPaths() # var path sep
326 {
327 set -e
328 sep="$3"
329 checklist="${tmp}/checklist"
330 printf "%s\n" "$2" | \
331 sed -e 's@\\@/@g' | \
332 sed -e 's@//@/@g' | \
333 ${awk} -F"${sep}" '{for(i=1;i<=NF;i++){printf "%s\n",$i;}}' \
334 > ${checklist}
335 cat ${checklist} | while read orig; do
336 if [ "${orig}" != "" ] ; then
337 if [ ! -d "${orig}" ] ; then
338 warning "Directory in $1 does not exist: ${orig}"
339 fi
340 fi
341 done
342 }
344 # Remove all duplicate entries
345 removeDeadDups() # string sep
346 {
347 set -e
348 sep="$2"
349 pathlist="${tmp}/pathlist"
350 printf "%s\n" "$1" | \
351 sed -e 's@\\@/@g' | \
352 sed -e 's@//@/@g' | \
353 ${awk} -F"${sep}" '{for(i=1;i<=NF;i++){printf "%s\n",$i;}}' \
354 > ${pathlist}
355 upaths="${tmp}/upaths"
356 cat ${pathlist} | while read orig; do
357 p="${orig}"
358 if [ "${cygpath_short}" != "" ] ; then
359 if [ "${p}" != "" ] ; then
360 if [ -d "${p}" ] ; then
361 short=`${cygpath_short} "${p}"`
362 if [ "${short}" != "" -a -d "${short}" ] ; then
363 p=`${cygpath} "${short}"`
364 fi
365 echo "${p}" >> ${upaths}
366 fi
367 fi
368 fi
369 done
370 newpaths=""
371 for i in `cat ${upaths}` ; do
372 # For some reason, \r characters can get into this
373 i=`echo "${i}" | tr -d '\r' | sed -e 's@/$@@'`
374 if [ "${newpaths}" = "" ] ; then
375 newpaths="${i}"
376 else
377 newpaths="${newpaths}${sep}${i}"
378 fi
379 done
380 printf "%s\n" "${newpaths}" | \
381 ${awk} -F"${sep}" \
382 '{a[$1];printf "%s",$1;for(i=2;i<=NF;i++){if(!($i in a)){a[$i];printf "%s%s",FS,$i;}};printf "\n";}'
383 }
385 # Create bat file to process Visual Studio vcvars*.bat files
386 createBat() # batfile bindir command
387 {
388 bat="$1"
389 bindir="$2"
390 command="$3"
391 stdout="${bat}.stdout"
392 rm -f ${bat} ${stdout}
393 echo "Output from: ${command}" > ${stdout}
394 bdir=`envpath "${bindir}"`
395 cat > ${bat} << EOF
396 REM Pick the right vcvars bat file
397 REM Empty these out so we only get the additions we want
398 set INCLUDE=
399 set LIB=
400 set LIBPATH=
401 set MSVCDIR=
402 set MSSdk=
403 set Mstools=
404 set DevEnvDir=
405 set VCINSTALLDIR=
406 set VSINSTALLDIR=
407 set WindowsSdkDir=
408 REM Run the vcvars bat file, send all output to stderr
409 call `${cygpath_windows} ${bdir}`\\${command} > `${cygpath_windows} "${stdout}"`
410 REM Echo out env var settings
411 echo VS_VS71COMNTOOLS="%VS71COMNTOOLS%"
412 echo export VS_VS71COMNTOOLS
413 echo VS_VS80COMNTOOLS="%VS80COMNTOOLS%"
414 echo export VS_VS80COMNTOOLS
415 echo VS_VS90COMNTOOLS="%VS90COMNTOOLS%"
416 echo export VS_VS90COMNTOOLS
417 echo VS_VS100COMNTOOLS="%VS100COMNTOOLS%"
418 echo export VS_VS100COMNTOOLS
419 echo VS_VS110COMNTOOLS="%VS110COMNTOOLS%"
420 echo export VS_VS110COMNTOOLS
421 echo VS_VCINSTALLDIR="%VCINSTALLDIR%"
422 echo export VS_VCINSTALLDIR
423 echo VS_VSINSTALLDIR="%VSINSTALLDIR%"
424 echo export VS_VSINSTALLDIR
425 echo VS_DEVENVDIR="%DevEnvDir%"
426 echo export VS_DEVENVDIR
427 echo VS_MSVCDIR="%MSVCDIR%"
428 echo export VS_MSVCDIR
429 echo VS_MSSDK="%MSSdk%"
430 echo export VS_MSSDK
431 echo VS_MSTOOLS="%Mstools%"
432 echo export VS_MSTOOLS
433 echo VS_WINDOWSSDKDIR="%WindowsSdkDir%"
434 echo export VS_WINDOWSSDKDIR
435 echo VS_INCLUDE="%INCLUDE%"
436 echo export VS_INCLUDE
437 echo VS_LIB="%LIB%"
438 echo export VS_LIB
439 echo VS_LIBPATH="%LIBPATH%"
440 echo export VS_LIBPATH
441 echo VS_WPATH="%PATH%"
442 echo export VS_WPATH
443 EOF
444 chmod a+x ${bat}
445 }
447 # Create env file
448 createEnv() # batfile envfile
449 {
450 rm -f ${1}.stdout
451 cmd.exe /Q /C `${cygpath_short} $1` | \
452 sed -e 's@\\@/@g' | \
453 sed -e 's@//@/@g' > $2
454 if [ -f "${1}.stdout" ] ; then
455 cat ${1}.stdout 1>&2
456 fi
457 chmod a+x $2
458 }
460 printEnv() # name pname vsname val
461 {
462 name="$1"
463 pname="$2"
464 vsname="$3"
465 val="$4"
466 if [ "${val}" != "" ] ; then
467 if [ "${shellStyle}" = "csh" ] ; then
468 if [ "${debug}" = "true" ] ; then
469 echo "setenv ${vsname} \"${val}\";"
470 fi
471 echo "setenv ${name} \"${val}\";"
472 elif [ "${shellStyle}" = "sh" ] ; then
473 if [ "${debug}" = "true" ] ; then
474 echo "${vsname}=\"${val}\";"
475 echo "export ${vsname};"
476 fi
477 echo "${name}=\"${val}\";"
478 echo "export ${name};"
479 elif [ "${shellStyle}" = "props" ] ; then
480 echo "vs.${pname}=${val}"
481 fi
482 fi
483 }
485 #############################################################################
487 # Get Visual Studio settings
488 if [ "${cygpath}" != "" ] ; then
490 # Create bat file to run
491 batfile="${tmp}/vs-to-env.bat"
492 if [ ! -d "${vcBinDir}" ] ; then
493 error "Does not exist: ${vcBinDir}"
494 elif [ "${vcvarsBat}" = "" ] ; then
495 error "No vcvars script: ${vcvarsBat}"
496 else
497 createBat "${batfile}" "${vcBinDir}" "${vcvarsBat}"
498 fi
500 # Run bat file to create environment variable settings
501 envfile="${tmp}/env.sh"
502 createEnv "${batfile}" "${envfile}"
504 # Read in the VS_* settings
505 . ${envfile}
507 # Derive unix style path, save old, and define new (remove dups)
508 VS_UPATH=`${cygpath_path} "${VS_WPATH}"`
509 export VS_UPATH
510 VS_OPATH=`printf "%s" "${PATH}" | sed -e 's@\\\\@/@g'`
511 export VS_OPATH
512 VS_PATH=`removeDeadDups "${VS_UPATH}${pathsep}${VS_OPATH}" "${pathsep}"`
513 export VS_PATH
515 fi
517 # Adjustments due to differences in vcvars*bat files
518 if [ "${VS_MSVCDIR}" = "" ] ; then
519 VS_MSVCDIR="${VS_VCINSTALLDIR}"
520 fi
521 if [ "${VS_DEVENVDIR}" = "" ] ; then
522 VS_DEVENVDIR="${VS_VSINSTALLDIR}"
523 fi
525 # Print env settings
526 # env vs.prop vs env value
527 # ------- ------- ---------- -----
528 printEnv INCLUDE include VS_INCLUDE "${VS_INCLUDE}"
529 printEnv LIB lib VS_LIB "${VS_LIB}"
530 printEnv LIBPATH libpath VS_LIBPATH "${VS_LIBPATH}"
531 if [ "${debug}" = "true" ] ; then
532 printEnv UPATH upath VS_UPATH "${VS_UPATH}"
533 printEnv WPATH wpath VS_WPATH "${VS_WPATH}"
534 printEnv OPATH opath VS_OPATH "${VS_OPATH}"
535 fi
536 printEnv PATH path VS_PATH "${VS_PATH}"
537 printEnv VCINSTALLDIR vcinstalldir VS_VCINSTALLDIR "${VS_VCINSTALLDIR}"
538 printEnv VSINSTALLDIR vsinstalldir VS_VSINSTALLDIR "${VS_VSINSTALLDIR}"
539 printEnv MSVCDIR msvcdir VS_MSVCDIR "${VS_MSVCDIR}"
540 printEnv MSSDK mssdk VS_MSSDK "${VS_MSSDK}"
541 printEnv MSTOOLS mstools VS_MSTOOLS "${VS_MSTOOLS}"
542 printEnv DEVENVDIR devenvdir VS_DEVENVDIR "${VS_DEVENVDIR}"
543 printEnv WINDOWSSDKDIR windowssdkdir VS_WINDOWSSDKDIR "${VS_WINDOWSSDKDIR}"
544 if [ "${vcSelection}" = "11" ] ; then
545 printEnv VS110COMNTOOLS vs110comntools VS_VS110COMNTOOLS "${VS_VS110COMNTOOLS}"
546 elif [ "${vcSelection}" = "10" ] ; then
547 printEnv VS100COMNTOOLS vs100comntools VS_VS100COMNTOOLS "${VS_VS100COMNTOOLS}"
548 elif [ "${vcSelection}" = "9" ] ; then
549 printEnv VS90COMNTOOLS vs90comntools VS_VS90COMNTOOLS "${VS_VS90COMNTOOLS}"
550 elif [ "${vcSelection}" = "7" ] ; then
551 printEnv VS71COMNTOOLS vs71comntools VS_VS71COMNTOOLS "${VS_VS71COMNTOOLS}"
552 elif [ "${vcSelection}" = "8" ] ; then
553 printEnv VS80COMNTOOLS vs80comntools VS_VS80COMNTOOLS "${VS_VS80COMNTOOLS}"
554 fi
556 # Check final settings
557 if [ "${verbose}" = "true" ] ; then
558 checkPaths "Windows PATH" "${VS_WPATH}" ";"
559 checkPaths LIB "${VS_LIB}" ";"
560 checkPaths INCLUDE "${VS_INCLUDE}" ";"
561 checkPaths PATH "${VS_PATH}" "${pathsep}"
562 fi
564 # Remove all temp files
565 rm -f -r ${tmp}
567 exit 0