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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
|
<#
.SYNOPSIS
Build all installers of psqlodbc project.
.DESCRIPTION
Build psqlodbc_x86.msi(msm), psqlodbc_x64.msi(msm).
.PARAMETER cpu
Specify build cpu type, "both"(default), "x86" or "x64" is
available.
.PARAMETER AlongWithDrivers
Specify when you'd like to build drivers before building installers.
.PARAMETER ExcludeRuntime
Specify when you'd like to exclude a msvc runtime dll from the installer.
.PARAMETER RedistUCRT
Specify when you'd like to redistribute Visual C++ 2015(or later) Redistributable.
.PARAMETER NoPDB
Specify when you'd rather not include PDB files.
.PARAMETER BuildConfigPath
Specify the configuration xml file name if you want to use
the configuration file other than standard one.
The relative path is relative to the current directory.
.EXAMPLE
> .\buildInstallers
Build 32bit and 64bit installers.
.EXAMPLE
> .\buildInstallers x86
Build 32bit installers.
.NOTES
Author: Hiroshi Inoue
Date: July 4, 2014
#>
# build 32bit and/or 64bit installers
#
Param(
[ValidateSet("x86", "x64", "both")]
[string]$cpu="both",
[switch]$AlongWithDrivers,
[switch]$ExcludeRuntime,
[switch]$RedistUCRT,
[switch]$NoPDB,
[string]$BuildConfigPath
)
[int]$ucrt_version=14
[String]$str_msvcr="msvcr"
[String]$str_vcrun="vcruntime"
[String]$str_msvcp="msvcp"
[String]$msrun_ptn="msvcr|vcruntime"
function msvcrun([int]$runtime_version)
{
[String]$str = if ($runtime_version -lt $ucrt_version) {$str_msvcr} else {$str_vcrun}
return $str
}
function env_vcversion_no()
{
$viver = $env:VisualStudioVersion
if ("$viver" -ne "") {
if ("$viver" -match "(\d+)\.0") {
return [int]$matches[1]
}
}
return 0
}
function toolset_no_to_runtimeversion([int]$toolset_no)
{
return [int] ($toolset_no / 10)
}
function runtimeversion_to_toolset_no([int]$runtime_version)
{
[int]$toolset_no = $runtime_version * 10
if ($runtime_version -eq 14) { # possibly be v141
[int]$vc_ver = 15
if ((env_vcversion_no) -eq $vc_ver) { # v141
$toolseto++
} elseif ((Find-VSDir $vc_ver) -ne "") { # v141
$toolset_no++
}
}
return $toolset_no
}
function toolset_no_to_vcversion([int]$toolset_no)
{
return [int] ($toolset_no / 10 + $toolset_no % 10)
}
function findRuntime([int]$toolset_no, [String]$pgmvc)
{
$runtime_version = toolset_no_to_runtimeversion($toolset_no)
$vcversion_no = toolset_no_to_vcversion($toolset_no)
# where's the dll?
[String]$rt_dllname = (msvcrun $runtime_version) + "${runtime_version}0.dll"
if ("$pgmvc" -ne "") {
$dllspecified = "${pgmvc}\${rt_dllname}"
if (Test-Path -Path $dllspecified) {
return $dllspecified, ""
}
}
$dllinredist = "${LIBPQBINDIR}\${rt_dllname}"
if (Test-Path -Path $dllinredist) {
return $dllinredist, ""
}
if ($env:PROCESSOR_ARCHITECTURE -eq "x86") {
$pgmvc = "$env:ProgramFiles"
} else {
$pgmvc = "${env:ProgramFiles(x86)}"
}
$dllinredist = ""
$vsdir = Find-VSDir $vcversion_no
if ("$vsdir" -ne "") {
if ($vcversion_no -gt 14) { # VC15 ~ VC??
$lslist = @(Get-ChildItem "${vsdir}VC\Redist\MSVC\*\${CPUTYPE}\Microsoft.VC${toolset_no}.CRT\${rt_dllname}" -ErrorAction SilentlyContinue)
if ($lslist.Count -gt 0) {
$dllinredist = $lslist[0].FullName
}
} else { # VC10 ~ VC14
$dllinredist = "${vsdir}VC\redist\${CPUTYPE}\Microsoft.VC${toolset_no}.CRT\${rt_dllname}"
}
}
if (("$dllinredist" -ne "") -and (Test-Path -Path $dllinredist)) {
return $dllinredist, ""
} else {
$messageSpec = "Please specify Configuration.$CPUTYPE.runtime_folder element of the configuration file where msvc runtime dll $rt_dllname can be found"
if ($CPUTYPE -eq "x86") {
if ($env:PROCESSOR_ARCHITECTURE -eq "x86") {
$pgmvc = "${env:SystemRoot}\system32"
} else {
$pgmvc = "${env:SystemRoot}\syswow64"
}
} else {
if ($env:PROCESSOR_ARCHITECTURE -eq "AMD64") {
$pgmvc = "${env:SystemRoot}\system32"
} elseif ($env:PROCESSOR_ARCHITEW6432 -eq "AMD64") {
$pgmvc = "${env:SystemRoot}\sysnative"
} else {
throw "${messageSpec}`n$dllinredist doesn't exist unfortunately"
}
}
$dllinsystem = "${pgmvc}\${rt_dllname}"
if (-not(Test-Path -Path $dllinsystem)) {
throw "${messageSpec}`nneither $dllinredist nor $dllinsystem exists unfortunately"
}
}
return "", $rt_dllname
}
function buildInstaller([string]$CPUTYPE)
{
$LIBPQBINDIR=getPGDir $configInfo $CPUTYPE "bin"
# msvc runtime psqlodbc links
$PODBCMSVCDLL = ""
$PODBCMSVPDLL = ""
$PODBCMSVCSYS = ""
$PODBCMSVPSYS = ""
# msvc runtime libpq links
$LIBPQMSVCDLL = ""
$LIBPQMSVCSYS = ""
$pgmvc = $configInfo.Configuration.$CPUTYPE.runtime_folder
$runtime_list = @()
if (-not $ExcludeRuntime) {
$toolset = $configInfo.Configuration.BuildResult.PlatformToolset
if ($toolset -match "^v(\d+)") {
$toolset_no0 = [int]$matches[1]
} else {
$toolset_no0 = 100
}
$runtime_version0 = toolset_no_to_runtimeversion($toolset_no0)
# where's the msvc runtime dll psqlodbc links?
if ($runtime_version0 -ge $ucrt_version -and $RedistUCRT) {
$script:wRedist=$true
} else {
$dlls=findRuntime $toolset_no0 $pgmvc
$PODBCMSVCDLL=$dlls[0]
if ("$PODBCMSVCDLL" -ne "") {
Write-Host "psqlodbc picks $PODBCMSVCDLL"
$runtime_list += $PODBCMSVCDLL
}
$PODBCMSVCSYS=$dlls[1]
if ("$PODBCMSVCSYS" -ne "") {
Write-Host "psqlodbc picks system $PODBCMSVCSYS"
$runtime_list += $PODBCMSVCSYS
}
$PODBCMSVPDLL=$PODBCMSVCDLL.Replace((msvcrun $runtime_version0), $str_msvcp)
if ("$PODBCMSVPDLL" -ne "") {
$runtime_list += $PODBCMSVPDLL
}
$PODBCMSVPSYS=$PODBCMSVCSYS.Replace((msvcrun $runtime_version0), $str_msvcp)
if ("$PODBCMSVPSYS" -ne "") {
$runtime_list += $PODBCMSVPSYS
}
}
# where's the runtime dll libpq links?
$msvclist=& ${dumpbinexe} /imports $LIBPQBINDIR\libpq.dll | select-string -pattern "^\s*($msrun_ptn)(\d+)0\.dll" | % {$_.Matches.Groups[2].Value}
if ($msvclist -ne $Null -and $msvclist.length -gt 0) {
if ($msvclist.GetType().Name -eq "String") {
$runtime_version1=[int]$msvclist
} else {
$runtime_version1=[int]$msvclist[0]
}
if ($runtime_version1 -eq $runtime_version0) {
$toolset_no1 = $toolset_no0
} else {
$toolset_no1 = runtimeversion_to_toolset_no($runtime_version1)
}
if ($runtime_version1 -ge $ucrt_version -and $RedistUCRT) {
$script:wRedist=$true
} elseif ($runtime_version1 -ne $runtime_version0) {
$dlls=findRuntime $toolset_no1 $pgmvc
$LIBPQMSVCDLL=$dlls[0]
if ("$LIBPQMSVCDLL" -ne "") {
Write-Host "LIBPQ picks $LIBPQMSVCDLL"
$runtime_list += $LIBPQMSVCDLL
}
$LIBPQMSVCSYS=$dlls[1]
if ("$LIBPQMSVCSYS" -ne "") {
Write-Host "LIBPQ picks system $LIBPQMSVCSYS"
$runtime_list += $LIBPQMSVCSYS
}
}
} else {
$script:wRedist=$true
}
}
Write-Host "CPUTYPE : $CPUTYPE"
Write-Host "VERSION : $VERSION"
Write-Host "LIBPQBINDIR: $LIBPQBINDIR"
if ($env:WIX -ne "")
{
$wix = "$env:WIX"
$env:Path += ";$WIX/bin"
}
# The subdirectory to install into
$SUBLOC=$VERSION.substring(0, 2) + $VERSION.substring(3, 2)
#
$maxmem=10
$libpqmem=Get-RelatedDlls "libpq.dll" $LIBPQBINDIR
for ($i=0; $i -lt $libpqmem.length; ) {
if ($runtime_list -contains $libpqmem[$i]) {
$libpqmem[$i]=$Null
} else {
$i++
}
}
if ($libpqmem.length -gt $maxmem) {
throw("number of libpq related dlls exceeds $maxmem")
}
for ($i=$libpqmem.length; $i -lt $maxmem; $i++) {
$libpqmem += ""
}
[string []]$libpqRelArgs=@()
for ($i=0; $i -lt $maxmem; $i++) {
$libpqRelArgs += "-d", ("LIBPQMEM$i=" + $libpqmem[$i])
}
if (-not(Test-Path -Path $CPUTYPE)) {
New-Item -ItemType directory -Path $CPUTYPE | Out-Null
}
try {
pushd "$scriptPath"
Write-Host ".`nBuilding psqlODBC/$SUBLOC merge module..."
$BINBASE = GetObjbase ".."
$INSTBASE = GetObjbase ".\$CPUTYPE" "installer\$CPUTYPE"
wix build --nologo -arch $CPUTYPE $libpqRelArgs -d "VERSION=$VERSION" -d "SUBLOC=$SUBLOC" -d "LIBPQBINDIR=$LIBPQBINDIR" -d "LIBPQMSVCDLL=$LIBPQMSVCDLL" -d "LIBPQMSVCSYS=$LIBPQMSVCSYS" -d "PODBCMSVCDLL=$PODBCMSVCDLL" -d "PODBCMSVPDLL=$PODBCMSVPDLL" -d "PODBCMSVCSYS=$PODBCMSVCSYS" -d "PODBCMSVPSYS=$PODBCMSVPSYS" -d "NoPDB=$NoPDB" -d "BINBASE=$BINBASE" -o $INSTBASE\psqlodbc_$CPUTYPE.msm psqlodbcm_cpu.wxs
if ($LASTEXITCODE -ne 0) {
throw "Failed to build merge module"
}
Write-Host ".`nBuilding psqlODBC installer database..."
wix build --nologo -arch $CPUTYPE -ext WixToolset.UI.wixext -d "VERSION=$VERSION" -d "SUBLOC=$SUBLOC" -d "INSTBASE=$INSTBASE" -o $INSTBASE\psqlodbc_$CPUTYPE.msi psqlodbc_cpu.wxs
if ($LASTEXITCODE -ne 0) {
throw "Failed to build installer database"
}
Write-Host ".`nDone!`n"
}
catch [Exception] {
Write-Host ".`Aborting build!"
throw $error[0]
}
finally {
popd
}
}
$scriptPath = (Split-Path $MyInvocation.MyCommand.Path)
$modulePath="${scriptPath}\..\winbuild"
Import-Module ${modulePath}\Psqlodbc-config.psm1
$defaultConfigDir=$modulePath
$configInfo = LoadConfiguration $BuildConfigPath $defaultConfigDir
if ($AlongWithDrivers) {
try {
pushd "$scriptpath"
$platform = $cpu
if ($cpu -eq "x86") {
$platform = "win32"
}
..\winbuild\BuildAll.ps1 -Platform $platform -BuildConfigPath "$BuildConfigPath"
if ($LASTEXITCODE -ne 0) {
throw "Failed to build binaries"
}
} catch [Exception] {
if ("$_.Exception.Message" -ne "") {
Write-Host ("Error: " + $_.Exception.Message) -ForegroundColor Red
} else {
echo $_.Exception | Format-List -Force
}
Remove-Module Psqlodbc-config
return
} finally {
popd
}
}
Import-Module ${scriptPath}\..\winbuild\MSProgram-Get.psm1
try {
if ($configInfo.Configuration.BuildResult.Date -eq "") {
Write-Host "!! Driver dlls haven't been built yet !!"
Write-Host "!! Please build driver dlls first !!"
return
}
$dumpbinexe = Find-Dumpbin
$wRedist=$false
$VERSION = GetPackageVersion $configInfo "$scriptPath/.."
if ($cpu -eq "both") {
buildInstaller "x86"
buildInstaller "x64"
Write-Host "wRedist=$wRedist"
Remove-Module Psqlodbc-config
try {
pushd "$scriptPath"
psqlodbc-setup\buildBootstrapper.ps1 -version $VERSION -withRedist:$wRedist
if ($LASTEXITCODE -ne 0) {
throw "Failed to build bootstrapper"
}
} catch [Exception] {
throw $error[0]
} finally {
popd
}
}
else {
buildInstaller $cpu
Remove-Module Psqlodbc-config
}
} catch [Exception] {
if ("$_.Exception.Message" -ne "") {
Write-Host ("Error: " + $_.Exception.Message) -ForegroundColor Red
} else {
echo $_.Exception | Format-List -Force
}
return
} finally {
Remove-Module MSProgram-Get
if (Get-Module Psqlodbc-config) {
Remove-Module Psqlodbc-config
}
}
|