Skip to content

Commit 2793673

Browse files
committed
🔨 Add a powershell script to simplify compilation of projects
1 parent 7ace4b1 commit 2793673

File tree

3 files changed

+247
-58
lines changed

3 files changed

+247
-58
lines changed

Plugin/InteropUnityCUDA.sublime-project

Lines changed: 12 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -6,109 +6,63 @@
66
],
77
"build_systems": [
88
{
9-
"name": "PluginInteropUnity - Build",
10-
"target": "terminus_exec",
11-
"focus": true,
12-
"timeit": false,
13-
"file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
14-
"cancel": "terminus_cancel_build",
15-
"shell_cmd": "MsBuild PluginInteropUnity.sln /t:build",
16-
"working_dir": "${project_path:${folder}}",
17-
},
18-
{
19-
"name": "PluginInteropUnity - Rebuild - Debug",
20-
"target": "terminus_exec",
21-
"focus": true,
22-
"timeit": false,
23-
"file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
24-
"cancel": "terminus_cancel_build",
25-
"cmd": [
26-
"MsBuild",
27-
"PluginInteropUnity.sln",
28-
"/t:rebuild",
29-
"/p:Configuration=Debug",
30-
],
31-
"working_dir": "${project_path:${folder}}",
32-
},
33-
{
34-
"name": "PluginInteropUnity - Rebuild - Release",
9+
"name": "Run Unit-Test",
3510
"target": "terminus_exec",
3611
"focus": true,
3712
"timeit": false,
3813
"file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
3914
"cancel": "terminus_cancel_build",
40-
"cmd": [
41-
"MsBuild",
42-
"PluginInteropUnity.sln",
43-
"/t:rebuild",
44-
"/p:Configuration=Release",
45-
],
15+
"shell_cmd": "powershell.exe -File ${project_path:${folder}}\\buildtools\\runUnitTest.ps1",
4616
"working_dir": "${project_path:${folder}}",
4717
},
4818
{
49-
"name": "PluginInteropUnity - Rebuild - Launch Unity",
19+
"name": "PluginInteropUnity - Build",
5020
"target": "terminus_exec",
5121
"focus": true,
5222
"timeit": false,
5323
"file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
5424
"cancel": "terminus_cancel_build",
55-
"shell_cmd": "MsBuild PluginInteropUnity.sln /t:rebuild && Unity -projectPath ${project_path:${folder}}\\..\\InteropUnityCUDA",
25+
"shell_cmd": "powershell.exe -File ${project_path:${folder}}\\buildtools\\compileProjects.ps1 -project \"all\" -action \"build\"",
5626
"working_dir": "${project_path:${folder}}",
5727
},
5828
{
59-
"name": "PluginInteropUnity - Clean",
29+
"name": "PluginInteropUnity - Rebuild - Debug",
6030
"target": "terminus_exec",
6131
"focus": true,
6232
"timeit": false,
6333
"file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
6434
"cancel": "terminus_cancel_build",
65-
"cmd": [
66-
"MsBuild",
67-
"PluginInteropUnity.sln",
68-
"/t:clean"
69-
],
35+
"shell_cmd": "powershell.exe -File ${project_path:${folder}}\\buildtools\\compileProjects.ps1 -project \"all\" -action \"rebuild\" -configuration \"debug\"",
7036
"working_dir": "${project_path:${folder}}",
7137
},
7238
{
73-
"name": "Proj - Utilities Plugin - Build",
39+
"name": "PluginInteropUnity - Rebuild - Release",
7440
"target": "terminus_exec",
7541
"focus": true,
7642
"timeit": false,
7743
"file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
7844
"cancel": "terminus_cancel_build",
79-
"cmd": [
80-
"MsBuild",
81-
"-target:Utilities",
82-
"/t:build"
83-
],
45+
"shell_cmd": "powershell.exe -File ${project_path:${folder}}\\buildtools\\compileProjects.ps1 -project \"all\" -action \"rebuild\" -configuration \"release\"",
8446
"working_dir": "${project_path:${folder}}",
8547
},
8648
{
87-
"name": "Proj - SampleBasic - Build",
49+
"name": "PluginInteropUnity - Rebuild - Launch Unity",
8850
"target": "terminus_exec",
8951
"focus": true,
9052
"timeit": false,
9153
"file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
9254
"cancel": "terminus_cancel_build",
93-
"cmd": [
94-
"MsBuild",
95-
"-target:SampleBasic",
96-
"/t:build"
97-
],
55+
"shell_cmd": "powershell.exe -File ${project_path:${folder}}\\buildtools\\compileProjects.ps1 -project \"all\" -action \"rebuild\" && Unity -projectPath ${project_path:${folder}}\\..\\InteropUnityCUDA",
9856
"working_dir": "${project_path:${folder}}",
9957
},
10058
{
101-
"name": "Proj - PluginInteropUnityCUDA - Build",
59+
"name": "PluginInteropUnity - Clean",
10260
"target": "terminus_exec",
10361
"focus": true,
10462
"timeit": false,
10563
"file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
10664
"cancel": "terminus_cancel_build",
107-
"cmd": [
108-
"MsBuild",
109-
"-target:PluginInteropUnityCUDA",
110-
"/t:build"
111-
],
65+
"shell_cmd": "powershell.exe -File ${project_path:${folder}}\\buildtools\\compileProjects.ps1 -project \"all\" -action \"clean\"",
11266
"working_dir": "${project_path:${folder}}",
11367
},
11468
{

Plugin/buildtools/compileProjects.ps1

Lines changed: 222 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,222 @@
1+
<#
2+
.SYNOPSIS
3+
This script builds specified projects or the whole solution using MsBuild.
4+
5+
.DESCRIPTION
6+
This PowerShell script is used to build specific projects or the entire solution
7+
using the MsBuild tool. It takes in parameters for projects, action, and configuration
8+
and constructs and executes the appropriate MsBuild commands.
9+
10+
.PARAMETER projects
11+
A string containing the project name or solution to build. Must be one of this possibilities (store in $possibleProjectValues) :
12+
("PluginInteropUnityCUDA", "SampleBasic"
13+
, "Utilities")
14+
or "solution" or "sln" or "all" if you want to build the whole solution
15+
16+
.PARAMETER action
17+
A string indicating the action to perform (e.g., build, rebuild, clean).
18+
19+
.PARAMETER configuration
20+
A string indicating the build configuration (e.g., release, debug).
21+
22+
.EXAMPLE
23+
.\BuildProjects.ps1 -projects @("PluginInteropUnityCUDA", "SampleBasic") -action "build" -configuration "release"
24+
This example command builds the specified projects in release configuration.
25+
26+
.NOTES
27+
File Name : BuildProjects.ps1
28+
Author : David Algis
29+
Prerequisite : PowerShell v3 and MsBuild
30+
Copyright 2023 - Studio Nyx
31+
#>
32+
param(
33+
[string]$project,
34+
[string]$action,
35+
[string]$configuration
36+
)
37+
38+
39+
. $PSScriptRoot\dependencies.ps1
40+
41+
$version = $Env:HYBRIDIZATION_VERSION
42+
# For non dll project, write NO in description
43+
[string[][]]$possibleProjectValues = @(
44+
@("PluginInteropUnityCUDA", "Contains a library with the core of interoperability between Unity and CUDA."),
45+
@("SampleBasic", "NO"),
46+
@("Utilities", "Contains the source of a library that contains utilities and common tools for all the other library and executable.")
47+
)
48+
49+
50+
Write-Host "Launch $action of $project..."-ForegroundColor DarkMagenta
51+
# TODO
52+
53+
54+
$currentDir = Get-Location
55+
56+
$buildSolution = $false
57+
58+
# Check if the project name is in the list of possible project values
59+
$isValidProject = $false
60+
$projectDescription = ""
61+
62+
63+
64+
if ($action -eq "build" -or $action -eq "rebuild") {
65+
if ($configuration -ne "debug" -and $configuration -ne "release" -and $configuration -ne "") {
66+
Write-Warning "The configuration argument '$configuration' is not a valid configuration for the action : $action. Exiting the program."
67+
Exit
68+
}
69+
}
70+
else {
71+
if ($action -ne "clean") {
72+
Write-Warning "The action argument '$action' is not a valid action. Exiting the program."
73+
Exit
74+
}
75+
}
76+
77+
if ($configuration -ne "") {
78+
$configuration = "/p:Configuration=$configuration"
79+
}
80+
81+
if ($project -eq "sln" -or $project -eq "solution" -or $project -eq "all") {
82+
$buildSolution = $true
83+
}
84+
else {
85+
foreach ($possibleProject in $possibleProjectValues) {
86+
if ($possibleProject[0].ToLower() -eq $project.ToLower()) {
87+
$isValidProject = $true
88+
# to be sure it has the correct cases
89+
$project = $possibleProject[0]
90+
$projectDescription = "$($possibleProject[1]) $configuration)"
91+
break
92+
}
93+
}
94+
95+
if (-not $isValidProject) {
96+
Write-Warning "The project name '$project' is not a valid project. Exiting the program."
97+
Exit
98+
}
99+
100+
}
101+
102+
103+
104+
Set-Location -Path "$PSScriptRoot\..\"
105+
106+
107+
function TryGetCommitHash() {
108+
$result = ""
109+
# Test if git enabled ...
110+
if (Get-Command git -erroraction silentlycontinue) {
111+
# Test if we are in a git repository ...
112+
$process = start-process git -ArgumentList "status" -PassThru -Wait -WindowStyle Hidden
113+
if ($process.ExitCode -eq 0) {
114+
$result = & git rev-parse --verify HEAD
115+
}
116+
}
117+
return $result
118+
}
119+
120+
function TryGetCommitTime() {
121+
$result = ""
122+
# Test if git enabled ...
123+
if (Get-Command git -erroraction silentlycontinue) {
124+
# Test if we are in a git repository ...
125+
$process = start-process git -ArgumentList "status" -PassThru -Wait -WindowStyle Hidden
126+
if ($process.ExitCode -eq 0) {
127+
$result = & git log -n 1 --pretty='format:%cd' --date=format:'%Y-%m-%d %H:%M:%S %z'
128+
}
129+
}
130+
return $result
131+
}
132+
133+
function setupResources([System.IO.FileInfo]$outputPath, [String]$version, [String]$name, [String]$description) {
134+
# Get hash of current HEAD
135+
$commitHash = TryGetCommitHash
136+
137+
$metadata = @"
138+
#pragma once
139+
140+
#define NAME "$name"
141+
#define INTERNAL_NAME "$name.dll"
142+
#define COMPANY "Studio Nyx"
143+
#define COPYRIGHT "Copyright (C) 2023"
144+
#define DESCRIPTION "$description"
145+
#define VERSION "$version"
146+
#define COMMIT "$commitHash"
147+
"@
148+
Out-File -InputObject $metadata -FilePath $outputPath -Encoding Default
149+
150+
}
151+
152+
153+
154+
# if the user want to build the whole solutions
155+
if ($buildSolution) {
156+
157+
$solutionPath = ".\PluginInteropUnity.sln"
158+
Write-Host "Updating Metadata of whole solutions..."
159+
foreach ($possibleProject in $possibleProjectValues) {
160+
$projectName = $possibleProject[0]
161+
$projectDescription = "$($possibleProject[1]) $configuration"
162+
$projectPath = ".\$projectName"
163+
# Form the full project path using the solution's directory
164+
$fullProjectPath = Join-Path (Split-Path $solutionPath) $projectPath
165+
166+
$projectPath = "$fullProjectPath\$projectName.vcxproj"
167+
$metadataFile = [System.IO.FileInfo]"$fullProjectPath\include\versionRC.h"
168+
169+
# We setup ressources metadata only for dll projects
170+
if ($possibleProject[1] -ne "NO") {
171+
# Check if the file exists
172+
if (-not (Test-Path -Path $metadataFile)) {
173+
# Create the file
174+
Write-Host "Create $metadataFile"
175+
New-Item -Path $metadataFile -ItemType File
176+
}
177+
setupResources $metadataFile $version $projectName $projectDescription
178+
}
179+
180+
181+
182+
}
183+
184+
$msbuildArguments = @(
185+
"PluginInteropUnity.sln",
186+
"/t:$action",
187+
"/m",
188+
"$configuration"
189+
)
190+
Write-Host "Execute command : MsBuild $msbuildArguments"
191+
& MsBuild $msbuildArguments
192+
193+
Set-Location -Path $currentDir
194+
exit
195+
}
196+
197+
$metadataFile = [System.IO.FileInfo]".\$project\include\versionRC.h"
198+
199+
Write-Host "Updating Metadata of $project..."
200+
# We setup ressources metadata only for dll projects
201+
if ($possibleProject[1] -ne "NO") {
202+
# Check if the file exists
203+
if (-not (Test-Path -Path $metadataFile)) {
204+
# Create the file
205+
Write-Host "Create $metadataFile"
206+
New-Item -Path $metadataFile -ItemType File
207+
}
208+
setupResources $metadataFile $version $project $projectDescription
209+
}
210+
211+
212+
213+
$msbuildArguments = @(
214+
"-target:$project",
215+
"/t:$action",
216+
"/m",
217+
"$configuration"
218+
)
219+
Write-Host "Execute command : MsBuild $msbuildArguments"
220+
& MsBuild $msbuildArguments
221+
222+
Set-Location -Path $currentDir

Plugin/buildtools/dependencies.ps1

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
function AbsolutePath($path) {
2+
# Strips out any relative path modifiers like '..' and '.'
3+
$abs_path = [System.IO.Path]::GetFullPath($path)
4+
5+
return $abs_path
6+
}
7+
8+
$Env:INTEROP_UNITY_CUDA_VERSION = "1.0.1"
9+
$Env:INTEROP_UNITY_CUDA_PLUGIN_ROOT = AbsolutePath ([System.IO.Path]::GetDirectoryName( $MyInvocation.MyCommand.Definition) + "\..")
10+
$Env:INTEROP_UNITY_CUDA_UNITY_PROJECT_ROOT = AbsolutePath ([System.IO.Path]::GetDirectoryName( $MyInvocation.MyCommand.Definition) + "\..\..\InteropUnityCUDA")
11+
# Write-Host "$Env:INTEROP_UNITY_CUDA_UNITY_PROJECT_ROOT"
12+
# $Env:INTEROP_UNITY_CUDA_PLUGIN_TARGET = "\Assets\Runtime\Plugin"
13+
$Env:UNITY_2021_3_17 = "C:\Program Files\Unity\Hub\Editor\2021.3.17f1\Editor"

0 commit comments

Comments
 (0)