summaryrefslogtreecommitdiff
path: root/dist/mcode/windows/compile.ps1
blob: 6f208eea78ffa1f82312dc9c2b2b39a63fad980e (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
# 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 Script:	Script to compile GHDL for Windows
# 
#	Authors:						Patrick Lehmann	(ported batch file to PowerShell)
#											Brian Davis			(contributions to the batch file)
#											Tristan Gingold	(initial batch file for compilations on Windows)
# 
# Description:
# ------------------------------------
#	This is a PowerShell script (executable) which:
#		- sets up a compilation environment
#		- test all dependencies
#		- compiles GHDL with GNAT
#
# ==============================================================================
#	Copyright (C) 2002, 2003, 2004, 2005 Tristan Gingold
#	
#	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.
# ==============================================================================
<#
	.SYNOPSIS 
		GHDL for Windows - GHDL compile script
		Use 'compile.ps1 -Help' to see the integrated help page
	
	.EXAMPLE
		C:\PS> .\compile.ps1 -Verbose -Compile
#>

# define script parameters
[CmdletBinding()]
Param(
	# compile GHDL
	[switch]$Compile,
	
	# clean up all files and directories
	[switch]$Clean,
	
	# display this help"
	[switch]$Help
)

# configure script here
$Script_RelPathToRoot =	"..\..\.."
$GHDLVersion = 					"0.33dev"

# save parameters and current working directory
$Script_Parameters =		$args
$Script_ScriptDir =			$PSScriptRoot
$Script_WorkingDir =		Get-Location
$GHDLRootDir_AbsPath =	Convert-Path (Resolve-Path ($PSScriptRoot + "\" + $Script_RelPathToRoot))

# configure some variables: paths, executables, directory names, ...
$SourceDirName =				"src"
$BuildDirName =					"dist\mcode\build"

# TODO:
#	check if:
#		- program are installed / auto find programs / auto find paths
#		- program version
$GCCExecutable =				"gcc.exe"
$GNATExecutable =				"gnatmake.exe"
$StripExecutable =			"strip.exe"
$GHDLExecutable =				"ghdl.exe"
$GHDLFilterExecutable =	"ghdlfilter.exe"

# construct directories
$SourceDir =	$GHDLRootDir_AbsPath + "\" + $SourceDirName
$BuildDir =		$GHDLRootDir_AbsPath + "\" + $BuildDirName

# set default values
$Script_ExitCode = 			0
if ($PSCmdlet.MyInvocation.BoundParameters["Debug"].IsPresent) 		{	$Script_EnableDebug =		$true	}
if ($PSCmdlet.MyInvocation.BoundParameters["Verbose"].IsPresent)	{	$Script_EnableVerbose =	$true	}

# Compiler flags
$CFlags = @()				# start with an empty array
$CFlags += '-O'			# optimize; level ?
$CFlags += '-g'			# enable debug symbols

Write-Host "GHDL for Windows - GHDL and tools compile script" -ForegroundColor Yellow
Write-Host

if ($Help)
	{	Write-Host "Usage:"
		Write-Host "  compile.ps1 [-Verbose] [-Debug] (-Help|-Compile|-Clean)" -ForegroundColor Gray
		Write-Host
		Write-Host "Options:"
		Write-Host "  -Verbose    enable detailed messages"
		Write-Host "  -Debug      enable debug messages"
		Write-Host
		Write-Host "Commands:"
		Write-Host "  -Help       display this help"
		Write-Host "  -Compile    compile all library files"
		Write-Host "  -Clean      clean up all files and directories"
	}	# Help
elseif ($Clean)
 {	Write-Host "Removing all created files and directories..."
		Write-Host "  rmdir $BuildDir"
		
		Remove-Item $BuildDir -Force -Recurse -ErrorAction SilentlyContinue
	}	# Clean
elseif ($Compile)
 {	Write-Host "Compiling GHDL $GHDLVersion for Windows"
		Write-Host "Preparing..."

		# create build directory if it does not exist
		if (Test-Path -Path $BuildDir)
			{	Write-Host "  Directory '$BuildDir' already exists."}
		else
			{	Write-Host "  Creating directory '$BuildDir'."
				[void](New-Item -ItemType directory -Path $BuildDir -ErrorAction SilentlyContinue)
			}

		# change working directory to BuildDir
		Write-Host "  cd $BuildDir"
		Set-Location $BuildDir

		Write-Host
		Write-Host "Start compilation..."
		# 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'
				$Parameters += $CFlags
				$Parameters += $SourceFile.CFlags
				$Parameters += $SourceDir + "\" + $SourceFile.File
				
				Write-Host ("  compiling: " + $SourceFile.File)
				if ($Script_EnableDebug)
					{	Write-Host ("    file: " + $SourceDir + "\" + $SourceFile.File)
						Write-Host ("    call: " + $GCCExecutable + " " + ($Parameters -join ' '))
					}
				
				# call compiler
				& $GCCExecutable $Parameters
				if ($LastExitCode -ne 0)
					{	$Script_ExitCode = 1
						Write-Host "    ERROR while compiling" -ForegroundColor Red	
					}
			}

		if ($Script_ExitCode -eq 0)
			{	# compile with GNAT
				$Parameters = @()
				$Parameters += $CFlags
				$Parameters += '-gnatn'

				# add source include paths
				$Parameters += '-aI' + $GHDLRootDir_AbsPath + '\dist\mcode\windows'
				$Parameters += '-aI' + $SourceDir
				$Parameters += '-aI' + $SourceDir + '\ghdldrv'
				$Parameters += '-aI' + $SourceDir + '\psl'
				$Parameters += '-aI' + $SourceDir + '\grt'
				$Parameters += '-aI' + $SourceDir + '\ortho'
				$Parameters += '-aI' + $SourceDir + '\ortho\mcode'
				$Parameters += '-aI' + $SourceDir + '\vhdl'
				$Parameters += '-aI' + $SourceDir + '\vhdl\translate'
				$Parameters += 'ghdl_jit'

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

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

				# call compiler (GNAT)
				Write-Host "  compiling with GNAT"
				if ($Script_EnableDebug)
					{	#Write-Host ("    file: " + $SourceDir + "\" + $SourceFile.File)
						Write-Host ("    call: " + $GNATExecutable + " " + ($Parameters -join ' '))
					}

				& $GNATExecutable $Parameters
				if ($LastExitCode -ne 0)
					{	$Script_ExitCode = 1
						Write-Host "    ERROR while compiling" -ForegroundColor Red	}
			}
			
		if ($Script_ExitCode -eq 0)
			{	# 
				Write-Host "  stripping executable..."
				& $StripExecutable $GHDLExecutable
			}

		if ($Script_ExitCode -eq 0)
			{	# compile with GNAT
				$Parameters = @()
				$Parameters += $CFlags

				# add source include paths
				$Parameters += '-aI' + $GHDLRootDir_AbsPath + '\dist\mcode\windows'
				$Parameters += 'ghdlfilter'

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

				# call compiler (GNAT)
				Write-Host "  compiling with GNAT"
				if ($Script_EnableDebug)
					{	#Write-Host ("    file: " + $SourceDir + "\" + $SourceFile.File)
						Write-Host ("    call: " + $GNATExecutable + " " + ($Parameters -join ' '))
					}

				& $GNATExecutable $Parameters
				if ($LastExitCode -ne 0)
					{	$Script_ExitCode = 1
						Write-Host "    ERROR while compiling" -ForegroundColor Red	}
			}
	}	# compile
else
	{	Write-Host "ERROR: missing argument(s)" -ForegroundColor Red
		Write-Host
		Write-Host "Usage:"
		Write-Host "  compile.ps1 [-Verbose] [-Debug] (-Help|-Compile|-Clean)" -ForegroundColor Gray
		Write-Host
	}	# unknown command
	
# restore working directory if changed
Set-Location $Script_WorkingDir

# return exit status
exit $Script_ExitCode