HandmadeHero/scripts/build.ps1

347 lines
10 KiB
PowerShell
Raw Normal View History

2023-10-01 17:17:14 -07:00
if ( $CursorPosition ) {
Clear-Host
}
$target_arch = Join-Path $PSScriptRoot 'helpers/target_arch.psm1'
$devshell = Join-Path $PSScriptRoot 'helpers/devshell.ps1'
$format_cpp = Join-Path $PSScriptRoot 'helpers/format_cpp.psm1'
$config_toolchain = Join-Path $PSScriptRoot 'helpers/configure_toolchain.ps1'
2023-09-08 09:42:24 -07:00
2023-09-23 18:03:33 -07:00
$path_root = git rev-parse --show-toplevel
$path_build = Join-Path $path_root 'build'
2023-09-23 18:03:33 -07:00
Import-Module $target_arch
2023-10-01 17:17:14 -07:00
Import-Module $format_cpp
2023-09-08 09:42:24 -07:00
Push-Location $path_root
#region Arguments
$vendor = $null
$optimize = $null
2023-09-18 17:16:40 -07:00
$debug = $null
2023-09-15 18:35:27 -07:00
$analysis = $false
2023-09-18 17:16:40 -07:00
$dev = $false
2023-09-26 14:26:35 -07:00
$platform = $null
$engine = $null
$game = $null
2023-10-01 17:17:14 -07:00
$verbose = $null
2023-09-08 09:42:24 -07:00
[array] $vendors = @( "clang", "msvc" )
# This is a really lazy way of parsing the args, could use actual params down the line...
if ( $args ) { $args | ForEach-Object {
switch ($_){
2023-09-15 18:35:27 -07:00
{ $_ -in $vendors } { $vendor = $_; break }
"optimize" { $optimize = $true }
2023-09-15 18:35:27 -07:00
"debug" { $debug = $true }
"analysis" { $analysis = $true }
2023-09-18 17:16:40 -07:00
"dev" { $dev = $true }
2023-09-26 14:26:35 -07:00
"platform" { $platform = $true }
"engine" { $engine = $true }
"game" { $game = $true }
2023-10-01 17:17:14 -07:00
"verbose" { $verbose = $true }
2023-09-08 09:42:24 -07:00
}
}}
2023-09-15 18:35:27 -07:00
#endregion Argument
2023-09-08 09:42:24 -07:00
2023-10-01 17:17:14 -07:00
# Load up toolchain configuraion
. $config_toolchain
2023-09-08 09:42:24 -07:00
2023-09-26 14:26:35 -07:00
#region Building
2023-09-08 14:50:22 -07:00
$path_project = Join-Path $path_root 'project'
2023-09-26 14:26:35 -07:00
$path_data = Join-Path $path_root 'data'
2023-09-26 22:16:41 -07:00
$path_binaries = Join-Path $path_data 'binaries'
2023-09-08 14:50:22 -07:00
$path_deps = Join-Path $path_project 'dependencies'
$path_codegen = Join-Path $path_project 'codegen'
2023-09-08 14:50:22 -07:00
$path_gen = Join-Path $path_project 'gen'
$path_platform = Join-Path $path_project 'platform'
2023-09-26 14:26:35 -07:00
$path_engine = Join-Path $path_project 'engine'
2023-09-08 09:42:24 -07:00
$update_deps = Join-Path $PSScriptRoot 'update_deps.ps1'
$handmade_process_active = Get-Process | Where-Object {$_.Name -like 'handmade_win32*'}
2023-09-08 09:42:24 -07:00
if ( (Test-Path $path_build) -eq $false ) {
New-Item $path_build -ItemType Directory
}
if ( (Test-Path $path_deps) -eq $false ) {
& $update_deps
}
2023-09-26 22:16:41 -07:00
if ( (Test-Path $path_binaries) -eq $false ) {
New-Item $path_binaries -ItemType Directory
}
#region Handmade Runtime
$includes = @(
2023-09-26 14:26:35 -07:00
$path_project
)
# Microsoft
2023-09-08 18:08:57 -07:00
$lib_gdi32 = 'Gdi32.lib'
$lib_xinput = 'Xinput.lib'
$lib_user32 = 'User32.lib'
2023-09-22 12:13:18 -07:00
$lib_winmm = 'Winmm.lib'
2023-09-08 15:54:16 -07:00
# Github
$lib_jsl = Join-Path $path_deps 'JoyShockLibrary/x64/JoyShockLibrary.lib'
2023-09-16 15:41:07 -07:00
$stack_size = 1024 * 1024 * 4
2023-09-15 18:35:27 -07:00
$compiler_args = @(
($flag_define + 'UNICODE'),
($flag_define + '_UNICODE')
2023-09-16 15:41:07 -07:00
# ($flag_set_stack_size + $stack_size)
2023-09-20 21:26:23 -07:00
$flag_wall
$flag_warnings_as_errors
$flag_optimize_intrinsics
2023-09-23 18:03:33 -07:00
($flag_define + 'Build_Unity=1' )
2023-09-15 18:35:27 -07:00
)
2023-09-08 14:50:22 -07:00
2023-09-18 17:16:40 -07:00
if ( $dev ) {
$compiler_args += ( $flag_define + 'Build_Development=1' )
}
else {
$compiler_args += ( $flag_define + 'Build_Development=0' )
}
2023-10-01 17:17:14 -07:00
function build-engine
2023-09-26 14:26:35 -07:00
{
2023-09-28 10:44:43 -07:00
$path_pdb_lock = Join-Path $path_binaries 'handmade_engine.pdb.lock'
2023-10-01 17:17:14 -07:00
New-Item $path_pdb_lock -ItemType File -Force
2023-09-28 10:44:43 -07:00
# Delete old PDBs
[Array]$pdb_files = Get-ChildItem -Path $path_binaries -Filter "handmade_engine_*.pdb"
2023-09-26 22:16:41 -07:00
foreach ($file in $pdb_files) {
Remove-Item -Path $file.FullName -Force
2023-10-01 17:17:14 -07:00
if ( $verbose ) { Write-Host "Deleted $file" -ForegroundColor Green }
2023-09-26 22:16:41 -07:00
}
2023-10-01 17:17:14 -07:00
$local:compiler_args = $script:compiler_args
$compiler_args += ($flag_define + 'Build_DLL=1' )
2023-09-26 14:26:35 -07:00
if ( $vendor -eq 'msvc' )
{
2023-10-01 17:17:14 -07:00
$compiler_args += ($flag_define + 'Engine_API=__declspec(dllexport)')
2023-09-26 14:26:35 -07:00
}
if ( $vendor -eq 'clang' )
{
2023-10-01 17:17:14 -07:00
$compiler_args += ($flag_define + 'Engine_API=__attribute__((visibility("default")))')
2023-09-26 14:26:35 -07:00
}
2023-10-01 17:17:14 -07:00
$local:linker_args = @(
2023-09-28 18:57:52 -07:00
$flag_link_dll
# $flag_link_optimize_references
2023-09-26 14:26:35 -07:00
)
2023-09-08 09:42:24 -07:00
2023-09-26 22:16:41 -07:00
$unit = Join-Path $path_project 'handmade_engine.cpp'
$dynamic_library = Join-Path $path_binaries 'handmade_engine.dll'
2023-09-26 14:26:35 -07:00
2023-10-01 17:17:14 -07:00
build-simple $includes $compiler_args $linker_args $unit $dynamic_library
2023-09-26 14:26:35 -07:00
2023-10-01 17:17:14 -07:00
Remove-Item $path_pdb_lock -Force
2023-09-26 14:26:35 -07:00
2023-10-01 17:17:14 -07:00
#region CodeGen Post-Build
if ( -not $handmade_process_active ) {
# Create the symbol table
if ( Test-Path $dynamic_library )
{
# $data_path = Join-Path $path_data 'handmade_engine.dll'
# move-item $dynamic_library $data_path -Force
$path_lib = $dynamic_library -replace '\.dll', '.lib'
$path_exp = $dynamic_library -replace '\.dll', '.exp'
Remove-Item $path_lib -Force
if ( Test-Path $path_exp ) { Remove-Item $path_exp -Force }
# We need to generate the symbol table so that we can lookup the symbols we need when loading/reloading the library at runtime.
# This is done by sifting through the emitter.map file from the linker for the base symbol names
# and mapping them to their found decorated name
# Initialize the hashtable with the desired order of symbols
$engine_symbols = [ordered]@{
'on_module_reload' = $null
'startup' = $null
'shutdown' = $null
'update_and_render' = $null
'update_audio' = $null
}
2023-10-01 17:17:14 -07:00
$path_engine_obj = Join-Path $path_build 'handmade_engine.obj'
$path_engine_map = Join-Path $path_build 'handmade_engine.map'
$maxNameLength = ($engine_symbols.Keys | Measure-Object -Property Length -Maximum).Maximum
Get-Content -Path $path_engine_map | ForEach-Object {
# If all symbols are found, exit the loop
if ($engine_symbols.Values -notcontains $null) {
return
}
# Split the line into tokens
$tokens = $_ -split '\s+', 4
# Extract only the decorated name using regex for both MSVC and Clang conventions
$decoratedName = ($tokens[2] -match '(\?[\w@]+|_Z[\w@]+)') ? $matches[1] : $null
# Check the origin of the symbol
# If the origin matches 'handmade_engine.obj', then process the symbol
$originParts = $tokens[3] -split '\s+'
$origin = if ($originParts.Count -eq 3) { $originParts[2] } else { $originParts[1] }
# Diagnostic output
if ( $false -and $decoratedName) {
write-host "Found decorated name: $decoratedName" -ForegroundColor Yellow
write-host "Origin : $origin" -ForegroundColor Yellow
}
if ($origin -like 'handmade_engine.obj') {
# Check each regular name against the current line
$engine_symbols.Keys | Where-Object { $engine_symbols[$_] -eq $null } | ForEach-Object {
if ($decoratedName -like "*$_*") {
$engine_symbols[$_] = $decoratedName
}
}
2023-09-26 14:26:35 -07:00
}
}
2023-10-01 17:17:14 -07:00
if ($verbose) { write-host "Engine Symbol Table:" -ForegroundColor Green }
$engine_symbols.GetEnumerator() | ForEach-Object {
$paddedName = $_.Key.PadRight($maxNameLength)
$decoratedName = $_.Value
if ($verbose ) { write-host "`t$paddedName, $decoratedName" -ForegroundColor Green }
}
2023-09-28 10:44:43 -07:00
2023-10-01 17:17:14 -07:00
# Write the symbol table to a file
$path_engine_symbols = Join-Path $path_build 'handmade_engine.symbols'
$engine_symbols.Values | Out-File -Path $path_engine_symbols
}
2023-09-28 18:57:52 -07:00
# Delete old PDBs
$pdb_files = Get-ChildItem -Path $path_build -Filter "engine_postbuild_gen_*.pdb"
foreach ($file in $pdb_files) {
Remove-Item -Path $file.FullName -Force
2023-10-01 17:17:14 -07:00
if ($verbose) { Write-Host "Deleted $file" -ForegroundColor Green }
2023-09-28 18:57:52 -07:00
}
2023-10-01 17:17:14 -07:00
$compiler_args = @()
$compiler_args += ( $flag_define + 'GEN_TIME' )
2023-10-01 17:17:14 -07:00
$linker_args = @(
$flag_link_win_subsystem_console
)
$unit = Join-Path $path_codegen 'engine_postbuild_gen.cpp'
$executable = Join-Path $path_build 'engine_postbuild_gen.exe'
2023-10-01 17:17:14 -07:00
build-simple $includes $compiler_args $linker_args $unit $executable
Push-Location $path_build
2023-10-01 17:17:14 -07:00
$time_taken = Measure-Command {
& $executable 2>&1 | ForEach-Object {
write-host `t $_ -ForegroundColor Green
}
}
Pop-Location
$path_generated_file = Join-Path $path_build 'engine_symbol_table.hpp'
move-item $path_generated_file (join-path $path_gen (split-path $path_generated_file -leaf)) -Force
}
2023-10-01 17:17:14 -07:00
}
if ( $engine ) {
build-engine
}
2023-10-01 17:17:14 -07:00
function build-platform
{
2023-10-01 17:17:14 -07:00
# CodeGen Pre-Build
if ( $true )
{
# Delete old PDBs
$pdb_files = Get-ChildItem -Path $path_build -Filter "platform_gen_*.pdb"
foreach ($file in $pdb_files) {
Remove-Item -Path $file.FullName -Force
if ( $verbose ) { Write-Host "Deleted $file" -ForegroundColor Green }
}
$path_platform_gen = Join-Path $path_platform 'gen'
if ( -not (Test-Path $path_platform_gen) ) {
New-Item $path_platform_gen -ItemType Directory
}
$local:compiler_args = @()
$compiler_args += ( $flag_define + 'GEN_TIME' )
$local:linker_args = @(
$flag_link_win_subsystem_console
)
$unit = Join-Path $path_codegen 'platform_gen.cpp'
$executable = Join-Path $path_build 'platform_gen.exe'
build-simple $includes $compiler_args $linker_args $unit $executable
Push-Location $path_platform
$time_taken = Measure-Command {
& $executable 2>&1 | ForEach-Object {
write-host `t $_ -ForegroundColor Green
}
}
Pop-Location
}
# Delete old PDBs
2023-09-26 22:16:41 -07:00
$pdb_files = Get-ChildItem -Path $path_binaries -Filter "handmade_win32_*.pdb"
foreach ($file in $pdb_files) {
Remove-Item -Path $file.FullName -Force
2023-10-01 17:17:14 -07:00
if ( $verbose ) { Write-Host "Deleted $file" -ForegroundColor Green }
2023-09-26 22:16:41 -07:00
}
2023-10-01 17:17:14 -07:00
$local:compiler_args = $script:compiler_args
$compiler_args += ($flag_define + 'Build_DLL=0' )
2023-09-26 14:26:35 -07:00
2023-10-01 17:17:14 -07:00
$local:linker_args = @(
2023-09-26 14:26:35 -07:00
$lib_gdi32,
# $lib_xinput,
$lib_user32,
$lib_winmm,
$lib_jsl,
$flag_link_win_subsystem_windows
$flag_link_optimize_references
2023-09-26 14:26:35 -07:00
)
2023-09-26 22:16:41 -07:00
$unit = Join-Path $path_project 'handmade_win32.cpp'
$executable = Join-Path $path_binaries 'handmade_win32.exe'
2023-09-26 14:26:35 -07:00
2023-10-01 17:17:14 -07:00
build-simple $includes $compiler_args $linker_args $unit $executable
2023-09-26 14:26:35 -07:00
2023-09-26 22:16:41 -07:00
# if ( Test-Path $executable )
# {
# $data_path = Join-Path $path_data 'handmade_win32.exe'
# move-item $executable $data_path -Force
# }
}
2023-10-01 17:17:14 -07:00
if ( $platform ) {
build-platform
}
2023-09-26 22:16:41 -07:00
$path_jsl_dll = Join-Path $path_binaries 'JoyShockLibrary.dll'
if ( (Test-Path $path_jsl_dll) -eq $false )
{
$path_jsl_dep_dll = Join-Path $path_deps 'JoyShockLibrary/x64/JoyShockLibrary.dll'
Copy-Item $path_jsl_dep_dll $path_jsl_dll
2023-09-26 14:26:35 -07:00
}
#endregion Handmade Runtime
2023-10-01 17:17:14 -07:00
$include = @(
'*.cpp'
'*.hpp'
)
format-cpp $path_gen $include
format-cpp (Join-Path $path_platform 'gen' ) $include
Pop-Location
2023-09-26 14:26:35 -07:00
#endregion Building