aboutsummaryrefslogtreecommitdiffstats
path: root/dist/mcode/windows/targets.psm1
blob: db6f970a89b6984b1e13a4fba790e7082b405038 (plain)
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
# EMACS settings: -*-	tab-width: 2; indent-tabs-mode: t -*-
# vim: tabstop=2:shiftwidth=2:noexpandtab
# kate: tab-width 2; replace-tabs off; indent-width 2;
# 
# ==============================================================================
#	PowerShell Module:	The module provides build targets for GHDL.
# 
#	Authors:						Patrick Lehmann
# 
# Description:
# ------------------------------------
#	This PowerShell module provides build targets for GHDL.
#
# ==============================================================================
#	Copyright (C) 2016 Patrick Lehmann
#	
#	GHDL is free software; you can redistribute it and/or modify it under
#	the terms of the GNU General Public License as published by the Free
#	Software Foundation; either version 2, or (at your option) any later
#	version.
#	
#	GHDL is distributed in the hope that it will be useful, but WITHOUT ANY
#	WARRANTY; without even the implied warranty of MERCHANTABILITY or
#	FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
#	for more details.
#	
#	You should have received a copy of the GNU General Public License
#	along with GHDL; see the file COPYING.  If not, write to the Free
#	Software Foundation, 59 Temple Place - Suite 330, Boston, MA
#	02111-1307, USA.
# ==============================================================================

# TODO:
#	check if:
#		- program are installed / auto find programs / auto find paths
#		- program version

# configure compiler tools
$GCCExecutable =					"gcc.exe"
$GNATMakeExecutable =			"gnatmake.exe"
$StripExecutable =				"strip.exe"

# configure output file
$GHDLExecutableName =			"ghdl.exe"
$FilterExecutable =				"filter.exe"

# configure directory structure
$CommonSourceDirName =		"src"
$WinMcodeSourceDirName =	"dist\mcode\windows"
# $WinLLVMSourceDirName =		"dist\llvm\windows"
$WinMcodeBuildDirName =		"dist\mcode\build"
# $WinLLVMBuildDirName =		"dist\llvm\build"

# construct file paths
$VersionFileName =				"version.ads"


function Invoke-Clean
{	<#
		.SYNOPSIS
		This CommandLet removes all generated files.
		.PARAMETER BuildDirectory
		The directory where all generated files are stored.
		.PARAMETER Quiet
		Disable outputs to the host console.
	#>
	[CmdletBinding()]
	param(
		[string]	$BuildDirectory,
		[switch]	$Quiet = $false
	)
	
	Write-Host "Executing build target 'Clean' ..." -ForegroundColor Yellow
	if ($Quiet -eq $false)
	{	Write-Host "  Removing all created files and directories..."
		Write-Host "    rmdir $BuildDirectory"
	}
	Remove-Item $BuildDirectory -Force -Recurse -ErrorAction SilentlyContinue
	
	return $false
}	# Invoke-Clean

function Invoke-CreateBuildDirectory
{	<#
		.SYNOPSIS
		This CommandLet creates a build directory if not existent, yet.
		.PARAMETER BuildDirectory
		The directory where all generated files are stored.
		.PARAMETER Quiet
		Disable outputs to the host console.
	#>
	[CmdletBinding()]
	param(
		[string]	$BuildDirectory,
		[switch]	$Quiet = $false
	)
	
	Write-Host "Executing build target 'CreateBuildDirectory' ..." -ForegroundColor Yellow
	if (Test-Path -Path $BuildDirectory)
	{	if ($Quiet -eq $false)
		{	Write-Host "  Directory '$BuildDirectory' already exists."		}
	}
	else
	{	if ($Quiet -eq $false)
		{	Write-Host "  Creating new directory '$BuildDirectory'."		}
		
		[void](New-Item -ItemType directory -Path $BuildDirectory -ErrorAction SilentlyContinue)
	}
	
	return $false
}	# Invoke-CreateBuildDirectory

function Get-GHDLVersion
{	<#
		.SYNOPSIS
		Returns the GHDL version string.
		.PARAMETER GHDLRootDir
		The repository root directory.
	#>
	[CmdletBinding()]
	param(
		[string]	$GHDLRootDir
	)
	# construct DirectoryPaths
	$SourceDirectory =		$GHDLRootDir + "\" + $CommonSourceDirName
	# construct FilePaths
	$VersionFilePath =		$SourceDirectory + "\" + $VersionFileName
	
	if (-not (Test-Path -Path $VersionFilePath))
	{	Write-Host "  Version file '$VersionFilePath' does not exists." -ForegroundColor Red
		return ""
	}
	$FileContent = Get-Content -Path $VersionFilePath
	foreach ($Line in $FileContent)
	{	if ($Line -match 'Ghdl_Ver(.+?)\"(.+?)\";')
		{ return $Matches[2]	}
	}
	return ""
}

function Invoke-PatchVersionFile
{	<#
		.SYNOPSIS
		This CommandLet patches the version file to include the git patch state.
		.PARAMETER GHDLRootDir
		The repository root directory.
		.PARAMETER GitBranchName
		The branch's name, where HEAD is located.
		.PARAMETER GitCommitDataString
		The DateTime when HEAD was commited.
		.PARAMETER GitCommitHash
		The Hash of HEAD.
		.PARAMETER Quiet
		Disable outputs to the host console.
	#>
	[CmdletBinding()]
	param(
		[string]	$GHDLRootDir,
		[string]	$GitBranchName = "unknown",
		[string]	$GitCommitDataString = "unknown",
		[string]	$GitCommitHash = "........",
		[switch]	$Quiet = $false
	)
	# construct DirectoryPaths
	$SourceDirectory =					$GHDLRootDir + "\" + $CommonSourceDirName
	# construct FilePaths
	$CurrentVersionFilePath =		$SourceDirectory + "\" + $VersionFileName
	$OriginalVersionFilePath =	$SourceDirectory + "\" + $VersionFileName + ".bak"
	
	Write-Host "Executing build target 'PatchVersionFile' ..." -ForegroundColor Yellow
	
	if (-not (Test-Path -Path $CurrentVersionFilePath))
	{	Write-Host "  Version file '$CurrentVersionFilePath' does not exists." -ForegroundColor Red
		return $true
	}
	if ($Quiet -eq $false)
	{	Write-Host "  Patching '$CurrentVersionFilePath'."		}
	$FileContent = Get-Content -Path $CurrentVersionFilePath -Encoding Ascii
	$FileContent = $FileContent -Replace "\s\(\d+\)\s", " (commit: $GitCommitDataString;  git branch: $GitBranchName';  hash: $GitCommitHash) "
	
	Move-Item $CurrentVersionFilePath $OriginalVersionFilePath -Force
	$FileContent | Out-File $CurrentVersionFilePath -Encoding Ascii
	
	return $false
}	# Invoke-PatchVersionFile

function Restore-PatchedVersionFile
{	<#
		.SYNOPSIS
		This CommandLet restores the original version file.
		.PARAMETER GHDLRootDir
		The repository root directory.
		.PARAMETER Quiet
		Disable outputs to the host console.
	#>
	[CmdletBinding()]
	param(
		[string]	$GHDLRootDir,
		[switch]	$Quiet = $false
	)
	# construct DirectoryPaths
	$SourceDirectory =					$GHDLRootDir + "\" + $CommonSourceDirName
	# construct FilePaths
	$CurrentVersionFilePath =		$SourceDirectory + "\" + $VersionFileName
	$OriginalVersionFilePath =	$SourceDirectory + "\" + $VersionFileName + ".bak"
	
	Write-Host "Executing build target 'PatchedVersionFile' ..." -ForegroundColor Yellow
	
	if (-not (Test-Path -Path "$CurrentVersionFilePath"))
	{	Write-Host "  Version file '$CurrentVersionFilePath' does not exists." -ForegroundColor Red
		return $true
	}
	if ($Quiet -eq $false)
	{	Write-Host "  Restoring '$CurrentVersionFilePath'."		}
	Move-Item $OriginalVersionFilePath $CurrentVersionFilePath -Force
	return $false
}	# Restore-PatchedVersionFile

function Get-CFlags
{	<#
		.SYNOPSIS
		Returns common ANSI C compiler flags for GCC.
	#>
	return @(
		"-O1",		# optimize; level 1
		"-g"			# enable debug symbols
	)
}

function Invoke-CompileCFiles
{	<#
		.SYNOPSIS
		This CommandLet compiles all C files with GCC.
		.PARAMETER GHDLRootDir
		The repository root directory.
		.PARAMETER BuildDirectory
		The directory where all generated files are stored.
		.PARAMETER Quiet
		Disable outputs to the host console
	#>
	[CmdletBinding()]
	param(
		[string]	$GHDLRootDir,
		[string]	$BuildDirectory,
		[switch]	$Quiet = $false
	)
	# construct DirectoryPaths
	$SourceDirectory =					$GHDLRootDir + "\" + $CommonSourceDirName
	
	Set-Location $BuildDirectory
	Write-Host "Executing build target 'CompileCFiles' ..." -ForegroundColor Yellow

	# list all files to be compiled; add additional CFlags if needed
	$SourceFiles = @()
	$SourceFiles += New-Object PSObject -Property @{File="grt\grt-cbinding.c";			CFlags=@()}
	$SourceFiles += New-Object PSObject -Property @{File="grt\grt-cvpi.c";					CFlags=@()}
	$SourceFiles += New-Object PSObject -Property @{File="grt\config\clock.c";			CFlags=@()}
	$SourceFiles += New-Object PSObject -Property @{File="grt\config\win32.c";			CFlags=@('-DWITH_GNAT_RUN_TIME')}
	$SourceFiles += New-Object PSObject -Property @{File="ortho\mcode\memsegs_c.c";	CFlags=@()}

	# compile C files
	foreach ($SourceFile in $SourceFiles)
	{	$Parameters = @()
		$Parameters += "-c"											# compile only
		$Parameters += Get-CFlags								# append common CFlags
		$Parameters += $SourceFile.CFlags
		$Parameters += $SourceDirectory + "\" + $SourceFile.File
		
		# call C compiler
		$InvokeExpr = "$GCCExecutable " + ($Parameters -join " ") + " 2>&1"
		
		Write-Host ("  compiling: " + $SourceFile.File)
		Write-Debug	"    call: $InvokeExpr"
		$ErrorRecordFound = Invoke-Expression $InvokeExpr | Restore-NativeCommandStream | Write-ColoredGHDLLine
		if ($LastExitCode -ne 0)
		{	return $true		}
	}
	
	return $false
}	# Invoke-CompileCFiles


function Invoke-CompileGHDLAdaFiles
{	<#
		.SYNOPSIS
		This CommandLet compiles all Ada files with GNAT.
		.PARAMETER GHDLRootDir
		The repository root directory.
		.PARAMETER BuildDirectory
		The directory where all generated files are stored.
		.PARAMETER Quiet
		Disable outputs to the host console
	#>
	[CmdletBinding()]
	param(
		[string]	$GHDLRootDir,
		[string]	$BuildDirectory,
		[switch]	$Quiet = $false
	)
	# construct DirectoryPaths
	$CommonSourceDirectory =				$GHDLRootDir + "\" + $CommonSourceDirName
	$WinMcodeSourceDirectory =			$GHDLRootDir + "\" + $WinMcodeSourceDirName
	
	Set-Location $BuildDirectory
	Write-Host "Executing build target 'CompileGHDLAdaFiles' ..." -ForegroundColor Yellow
	
	$Parameters = @()
	$Parameters += Get-CFlags								# append common CFlags
	$Parameters += '-gnatn'

	# append all source paths
	$Parameters += '-aI' + $WinMcodeSourceDirectory
	$Parameters += '-aI' + $CommonSourceDirectory
	$Parameters += '-aI' + $CommonSourceDirectory + '\ghdldrv'
	$Parameters += '-aI' + $CommonSourceDirectory + '\psl'
	$Parameters += '-aI' + $CommonSourceDirectory + '\grt'
	$Parameters += '-aI' + $CommonSourceDirectory + '\ortho'
	$Parameters += '-aI' + $CommonSourceDirectory + '\ortho\mcode'
	$Parameters += '-aI' + $CommonSourceDirectory + '\vhdl'
	$Parameters += '-aI' + $CommonSourceDirectory + '\vhdl\translate'
	
	# top level
	$Parameters += 'ghdl_jit'

	# add output filename
	$Parameters += '-o'
	$Parameters += $GHDLExecutableName

	# append linker parameters
	$Parameters += '-largs'
	$Parameters += 'grt-cbinding.o'
	$Parameters += 'clock.o'
	$Parameters += 'grt-cvpi.o'
	$Parameters += 'memsegs_c.o'
	$Parameters += 'win32.o'
	$Parameters += '-ldbghelp'
	$Parameters += '-largs'
	# $Parameters += '-Wl,--stack,8404992'

	# call Ada compiler (GNAT)
	$InvokeExpr = "$GNATMakeExecutable " + ($Parameters -join " ") + " 2>&1"
	
	Write-Host "  compiling with GNAT"
	Write-Debug "    call: $InvokeExpr"
	$ErrorRecordFound = Invoke-Expression $InvokeExpr | Restore-NativeCommandStream | Write-ColoredGHDLLine
	return ($LastExitCode -ne 0)
}	# Invoke-CompileGHDLAdaFiles

function Invoke-CompileFilterAdaFiles
{	<#
		.SYNOPSIS
		This CommandLet compiles all Ada files with GNAT.
		.PARAMETER SourceDirectory
		The directory where all source files are located.
		.PARAMETER BuildDirectory
		The directory where all generated files are stored.
		.PARAMETER Quiet
		Disable outputs to the host console
	#>
	[CmdletBinding()]
	param(
		[string]	$GHDLRootDir,
		[string]	$BuildDirectory,
		[switch]	$Quiet = $false
	)
	# construct DirectoryPaths
	$SourceDirectory =					$GHDLRootDir + "\" + $CommonSourceDirName
	
	Set-Location $BuildDirectory
	Write-Host "Executing build target 'CompileFilterAdaFiles' ..." -ForegroundColor Yellow
	
	$Parameters = @()
	$Parameters += Get-CFlags								# append common CFlags
	$Parameters += '-gnatn'

	# append all source paths
	$Parameters += '-aI' + $SourceDirectory + '\..\dist\mcode\windows'
	
	# top level
	$Parameters += 'ghdlfilter'

	# add output filename
	$Parameters += '-o'
	$Parameters += $FilterExecutable

	# call Ada compiler (GNAT)
	$InvokeExpr = "$GNATMakeExecutable " + ($Parameters -join " ") + " 2>&1"
	
	Write-Host "  compiling with GNAT"
	Write-Debug "    call: $InvokeExpr"
	$ErrorRecordFound = Invoke-Expression $InvokeExpr | Restore-NativeCommandStream | Write-ColoredGHDLLine
	return ($LastExitCode -ne 0)
}	# Invoke-CompileFilterAdaFiles

function Invoke-StripGHDLExecutable
{	<#
		.SYNOPSIS
		This CommandLet strips the result files.
		.PARAMETER BuildDirectory
		The directory where all generated files are stored.
		.PARAMETER Quiet
		Disable outputs to the host console
	#>
	[CmdletBinding()]
	param(
		[string]	$BuildDirectory,
		[switch]	$Quiet = $false
	)

	Set-Location $BuildDirectory
	Write-Host "Executing build target 'StripGHDLExecutable' ..." -ForegroundColor Yellow
	
	# call striping tool (strip)
	Write-Host "  stripping '$GHDLExecutableName'"
	Write-Debug "    call: $StripExecutable $GHDLExecutableName"
	& $StripExecutable $GHDLExecutableName
	return ($LastExitCode -ne 0)
}	# Invoke-StripGHDLExecutable

function Test-GHDLVersion
{	<#
		.SYNOPSIS
		This CommandLet executes ghdl to read the version information
		.PARAMETER BuildDirectory
		The directory where all generated files are stored.
		.PARAMETER Quiet
		Disable outputs to the host console
	#>
	[CmdletBinding()]
	param(
		[string]	$BuildDirectory,
		[switch]	$Quiet = $false
	)

	Set-Location $BuildDirectory
	Write-Host "Executing build target 'GHDLVersion' ..." -ForegroundColor Yellow
	
	if (-not (Test-Path -Path $GHDLExecutableName))
	{	Write-Host "  GHDL executable '$GHDLExecutableName' does not exists." -ForegroundColor Red
		return $true
	}
	
	# call ghdl
	$InvokeExpr = "$GHDLExecutableName --version 2>&1"
	
	Write-Host "  executing '$GHDLExecutableName'"
	Write-Host "    call: $InvokeExpr"
	Write-Host "----------------------------------------"
	Invoke-Expression $InvokeExpr | Restore-NativeCommandStream
	Write-Host "----------------------------------------"
	return ($LastExitCode -ne 0)
}	# Test-GHDLVersion


# export functions
Export-ModuleMember -Function 'Get-GHDLVersion'
Export-ModuleMember -Function 'Invoke-Clean'
Export-ModuleMember -Function 'Invoke-CreateBuildDirectory'
Export-ModuleMember -Function 'Invoke-PatchVersionFile'
Export-ModuleMember -Function 'Restore-PatchedVersionFile'
Export-ModuleMember -Function 'Invoke-CompileCFiles'
Export-ModuleMember -Function 'Invoke-CompileGHDLAdaFiles'
Export-ModuleMember -Function 'Invoke-CompileFilterAdaFiles'
Export-ModuleMember -Function 'Invoke-StripGHDLExecutable'
Export-ModuleMember -Function 'Test-GHDLVersion'