1#! /bin/sh
2#
3# %CopyrightBegin%
4#
5# Copyright Ericsson AB 2007-2016. All Rights Reserved.
6#
7# Licensed under the Apache License, Version 2.0 (the "License");
8# you may not use this file except in compliance with the License.
9# You may obtain a copy of the License at
10#
11#     http://www.apache.org/licenses/LICENSE-2.0
12#
13# Unless required by applicable law or agreed to in writing, software
14# distributed under the License is distributed on an "AS IS" BASIS,
15# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16# See the License for the specific language governing permissions and
17# limitations under the License.
18#
19# %CopyrightEnd%
20#
21# This little helper digs out the current version of microsoft CRT
22# by compiling hello world and "parsing" the manifest file...
23
24# To debug using a fake version:
25
26# echo "8.0.50727.763"
27# exit 0
28
29if [ "$1" = "-n" ]; then
30    SWITCH=$1
31    shift
32else
33    SWITCH=""
34fi
35
36cat > hello.c <<EOF
37#include <windows.h>
38#include <stdio.h>
39
40int main(void)
41{
42    printf("Hello world\n");
43    return 0;
44}
45
46EOF
47cl.exe -MD hello.c  > /dev/null 2>&1
48if [ '!' -f hello.exe.manifest ]; then
49    # Gah - VC 2010 changes the way it handles DLL's and manifests... Again...
50    # need another way of getting the version
51    DLLNAME=`dumpbin.exe -imports hello.exe | egrep MSVCR.*dll`
52    DLLNAME=`echo $DLLNAME`
53    if [ -z "$DLLNAME" ]; then
54        DLLNAME=`dumpbin.exe -imports hello.exe | egrep VCRUNTIME.*dll`
55	DLLNAME=`echo $DLLNAME`
56    fi
57    if [ '!' -z "$1" ]; then
58	FILETOLOOKIN=$1
59    else
60	FILETOLOOKIN=$DLLNAME
61    fi
62    cat > helper.c <<EOF
63#include <windows.h>
64#include <stdio.h>
65
66#define REQ_MODULE "$FILETOLOOKIN"
67
68int main(void)
69{
70  DWORD dummy;
71  DWORD versize;
72  int i,n;
73  unsigned char *versinfo;
74  char buff[100];
75
76  char *vs_verinfo;
77  unsigned int vs_ver_size;
78  
79  WORD *translate;
80  unsigned int tr_size;
81  
82  if (!(versize = GetFileVersionInfoSize(REQ_MODULE,&dummy))) {
83    fprintf(stderr,"No version info size in %s!\n",REQ_MODULE);
84    exit(1);
85  }
86  versinfo=malloc(versize);
87  if (!GetFileVersionInfo(REQ_MODULE,dummy,versize,versinfo)) {
88    fprintf(stderr,"No version info in %s!\n",REQ_MODULE);
89    exit(2);
90  }
91  if (!VerQueryValue(versinfo,"\\\\VarFileInfo\\\\Translation",&translate,&tr_size)) {
92    fprintf(stderr,"No translation info in %s!\n",REQ_MODULE);
93    exit(3);
94  }
95  n = tr_size/(2*sizeof(*translate));
96  for(i=0; i < n; ++i) {
97    sprintf(buff,"\\\\StringFileInfo\\\\%04x%04x\\\\FileVersion",
98	    translate[i*2],translate[i*2+1]);
99    if (VerQueryValue(versinfo,buff,&vs_verinfo,&vs_ver_size) && vs_ver_size > 2) {
100        if(vs_verinfo[1] == 0) // Wide char (depends on compiler version!!)
101            printf("%S\n",(unsigned short *) vs_verinfo);
102        else 
103            printf("%s\n",(char *) vs_verinfo);
104        return 0;
105    }
106  } 
107  fprintf(stderr,"Failed to find file version of %s\n",REQ_MODULE);
108  return 0;
109}
110EOF
111    cl.exe -MD helper.c version.lib > /dev/null 2>&1
112    if [ '!' -f helper.exe ]; then
113	echo "Failed to build helper program." >&2
114	exit 1
115    fi
116    NAME=$DLLNAME
117    VERSION=`./helper.exe`
118else
119    VERSION=`grep '<assemblyIdentity' hello.exe.manifest | sed 's,.*version=.\([0-9\.]*\).*,\1,g' | grep -v '<'`
120    NAME=`grep '<assemblyIdentity' hello.exe.manifest | sed 's,.*name=.[A-Za-z\.]*\([0-9]*\).*,msvcr\1.dll,g' | grep -v '<'`
121fi
122#rm -f hello.c hello.obj hello.exe hello.exe.manifest helper.c helper.obj helper.exe helper.exe.manifest
123if [ "$SWITCH" = "-n" ]; then
124    ASKEDFOR=$NAME
125else
126    ASKEDFOR=$VERSION
127fi
128if [ -z "$ASKEDFOR" ]; then
129    exit 1
130fi
131echo $ASKEDFOR
132exit 0
133