From 459b26d957defe7b7a93022d6743ef0ebce58c7b Mon Sep 17 00:00:00 2001
From: battlmonstr <battlmonstr@users.noreply.github.com>
Date: Tue, 10 May 2022 03:49:27 +0200
Subject: [PATCH] make: wmake refactoring (#4105)

* list "all" targets explicitly
* add missing targets
* add missing build flags
* add test-integration target
* show tests output
* use wmake test on CI
* update submodules for all targets (like Makefile)
* remove unused function Test-Administrator
---
 .github/workflows/ci.yml               |   5 +-
 .github/workflows/test-integration.yml |   2 +-
 wmake.ps1                              | 214 ++++++++++---------------
 3 files changed, 90 insertions(+), 131 deletions(-)

diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index e1670bd4b0..eac1b3c235 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -89,8 +89,11 @@ jobs:
           key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
           restore-keys: ${{ runner.os }}-go-
 
+      - name: Build
+        run: .\wmake.ps1 all
+
       - name: Test
-        run: .\wmake.ps1 && make test
+        run: .\wmake.ps1 test
 
   docker:
     runs-on: ubuntu-20.04
diff --git a/.github/workflows/test-integration.yml b/.github/workflows/test-integration.yml
index 48b9c58576..446a9f95e2 100644
--- a/.github/workflows/test-integration.yml
+++ b/.github/workflows/test-integration.yml
@@ -66,4 +66,4 @@ jobs:
           restore-keys: ${{ runner.os }}-go-
 
       - name: test-integration
-        run: .\wmake.ps1 && make test-integration
+        run: .\wmake.ps1 test-integration
diff --git a/wmake.ps1 b/wmake.ps1
index 9237d9119f..7202f105d3 100644
--- a/wmake.ps1
+++ b/wmake.ps1
@@ -14,12 +14,31 @@
    limitations under the License.
 #>
 
-
 Param(
     [Parameter(Position=0, 
     HelpMessage="Enter the build target")]
     [Alias("target")]
-    [ValidateSet("all", "clean", "test", "erigon", "rpcdaemon", "rpctest", "hack", "state", "integration", "db-tools", "sentry", "downloader")]
+    [ValidateSet(
+        "clean",
+        "cons",
+        "db-tools",
+        "devnettest",
+        "downloader",
+        "erigon",
+        "evm",
+        "hack",
+        "integration",
+        "observer",
+        "pics",
+        "rpcdaemon",
+        "rpctest",
+        "sentry",
+        "state",
+        "test",
+        "test-integration",
+        "txpool",
+        "all"
+    )]
     [string[]]$BuildTargets=@("erigon", "rpcdaemon", "sentry", "downloader", "integration")
 )
 
@@ -48,6 +67,24 @@ if ($BuildTargets.Count -gt 1) {
 
 }
 
+if ($BuildTargets[0] -eq "all") {
+    $BuildTargets = @(
+        "cons",
+        "devnettest",
+        "downloader",
+        "erigon",
+        "evm",
+        "hack",
+        "integration",
+        "observer",
+        "pics",
+        "rpcdaemon",
+        "rpctest",
+        "sentry",
+        "state",
+        "txpool"
+    )
+}
 
 # ====================================================================
 # Messages texts
@@ -61,7 +98,6 @@ $headerText = @"
  
 "@
 
-
 $gitErrorText = @"
 
  Requirement Error.
@@ -72,12 +108,13 @@ $gitErrorText = @"
 "@
 
 $goMinMinorVersion = 18
+$goMinVersion = "1.$goMinMinorVersion"
 
 $goErrorText = @"
 
  Requirement Error.
  You need to have Go Programming Language (aka golang) installed.
- Minimum required version is 1.$goMinMinorVersion
+ Minimum required version is $goMinVersion
  Please visit https://golang.org/dl/ and download the appropriate
  installer.
  Ensure that go.exe installation
@@ -93,9 +130,9 @@ $chocolateyErrorText = @"
  chocolatey [https://chocolatey.org/] with the following
  mandatory components: 
 
- - cmake 3.20.2
- - make 4.3
- - mingw 10.2.0
+ - cmake
+ - make
+ - mingw
 
 "@
 
@@ -121,20 +158,6 @@ $privilegeErrorText = @"
 # Functions
 # ====================================================================
 
-# -----------------------------------------------------------------------------
-# Function 		: Test-Administrator
-# -----------------------------------------------------------------------------
-# Description	: Checks the script is running with Administrator privileges
-# Returns       : $true / $false
-# -----------------------------------------------------------------------------
-function Test-Administrator {
-
-    $myWindowsID = [System.Security.Principal.WindowsIdentity]::GetCurrent();
-    $myWindowsPrincipal = New-Object System.Security.Principal.WindowsPrincipal($myWindowsID);
-    $adminRole = [System.Security.Principal.WindowsBuiltInRole]::Administrator;
-    Write-Output ($myWindowsPrincipal.IsInRole($adminRole))
-} 
-
 # -----------------------------------------------------------------------------
 # Function 		: Get-Env
 # -----------------------------------------------------------------------------
@@ -204,7 +227,6 @@ function Test-GO-Installed {
 # Returns       : $true / $false
 # -----------------------------------------------------------------------------
 function Test-Git-Installed {
-
     $Private:item   = Get-Uninstall-Item "^Git version [0-9\.]{1,}|^Git$"
     $Private:result = $false
 
@@ -223,7 +245,6 @@ function Test-Git-Installed {
 # Returns       : $true / $false
 # -----------------------------------------------------------------------------
 function Test-Choco-Installed {
-
     ## Test Chocolatey Install
     $script:chocolateyPath = Get-Env "chocolateyInstall"
     if(-not $chocolateyPath) {
@@ -338,19 +359,18 @@ if(!(Test-GO-Installed)) {
     exit 1
 }
 
-# ## Administrator Privileges
-# if (!(Test-Administrator)) {
-#    Write-Host $privilegeErrorText
-#    return
-# }
-
-
 # Build erigon binaries
 Set-Variable -Name "Erigon" -Value ([hashtable]::Synchronized(@{})) -Scope Script
 $Erigon.Commit     = [string]@(git.exe rev-list -1 HEAD)
 $Erigon.Branch     = [string]@(git.exe rev-parse --abbrev-ref HEAD)
 $Erigon.Tag        = [string]@(git.exe describe --tags)
-$Erigon.Build      = "go build -v -trimpath -ldflags ""-X github.com/ledgerwatch/erigon/params.GitCommit=$($Erigon.Commit) -X github.com/ledgerwatch/erigon/params.GitBranch=$($Erigon.Branch) -X github.com/ledgerwatch/erigon/params.GitTag=$($Erigon.Tag)"""
+
+$Erigon.BuildTags = "nosqlite,noboltdb"
+$Erigon.Package = "github.com/ledgerwatch/erigon"
+
+$Erigon.BuildFlags = "-trimpath -tags $($Erigon.BuildTags) -buildvcs=false"
+$Erigon.BuildFlags += " -ldflags ""-X $($Erigon.Package)/params.GitCommit=$($Erigon.Commit) -X $($Erigon.Package)/params.GitBranch=$($Erigon.Branch) -X $($Erigon.Package)/params.GitTag=$($Erigon.Tag)"""
+
 $Erigon.BinPath    = [string](Join-Path $MyContext.StartDir "\build\bin")
 $Erigon.Submodules = $false
 $env:GO111MODULE = "on"
@@ -375,8 +395,9 @@ Write-Host @"
 "@
 
 foreach($BuildTarget in $BuildTargets) {
+
 ## Choco components for building db-tools
-if ($BuildTarget -eq "all" -or $BuildTarget -eq "db-tools") {
+if ($BuildTarget -eq "db-tools") {
     if(!(Test-choco-Installed)) {
         exit 1
     }
@@ -435,11 +456,9 @@ if ($BuildTarget -eq "all" -or $BuildTarget -eq "db-tools") {
     Set-Location $MyContext.Directory
     # Eventually move all mdbx_*.exe to ./build/bin directory
     Move-Item -Path "$($Erigon.MDBXBuildPath)/mdbx_*.exe" -Destination $Erigon.BinPath -Force
-
 }
     
 if ($BuildTarget -eq "clean") {
-
     Write-Host " Cleaning ..."
 
     # Remove ./build/bin directory
@@ -449,115 +468,52 @@ if ($BuildTarget -eq "clean") {
     go.exe clean -cache
 
 } elseif ($BuildTarget -eq "test") {
-    
     Write-Host " Running tests ..."
-    $env:GODEBUG="cgocheck=0"
-    go test ./... -p 2 --timeout 30m
-    $TestCommand = "go test ./... -p 2 --timeout 30m"
-    $TestCommand += ';$?'
-    $success = Invoke-Expression -Command $TestCommand
-    if (-not $success) {
+    $env:GODEBUG = "cgocheck=0"
+    $TestCommand = "go test $($Erigon.BuildFlags) ./... -p 2 --timeout 30s"
+    Invoke-Expression -Command $TestCommand | Out-Host
+    if (!($?)) {
         Write-Host " ERROR : Tests failed"
         exit 1
     } else {
         Write-Host "`n Tests completed"
     }
 
-} else {
-
-    $binaries = @()
-
-    if ($BuildTarget -eq "all" -or $BuildTarget -eq "erigon") {
-        $binary = New-Object -TypeName psobject -Property @{
-            Executable="erigon.exe" 
-            Source="./cmd/erigon"
-        }
-        $binaries += $binary
-    }
-
-    if ($BuildTarget -eq "all" -or $BuildTarget -eq "rpcdaemon") {
-        $binary = New-Object -TypeName psobject -Property @{
-            Executable="rpcdaemon.exe" 
-            Source="./cmd/rpcdaemon"
-        }
-        $binaries += $binary
-    }
-    
-    if ($BuildTarget -eq "all" -or $BuildTarget -eq "rpctest") {
-        $binary = New-Object -TypeName psobject -Property @{
-            Executable="rpctest.exe" 
-            Source="./cmd/rpctest"
-        }
-        $binaries += $binary
-    }
-
-    if ($BuildTarget -eq "all" -or $BuildTarget -eq "integration") {
-        $binary = New-Object -TypeName psobject -Property @{
-            Executable="integration.exe" 
-            Source="./cmd/integration"
-        }
-        $binaries += $binary
-    }
-    
-    if ($BuildTarget -eq "all" -or $BuildTarget -eq "state") {
-        $binary = New-Object -TypeName psobject -Property @{
-            Executable="state.exe" 
-            Source="./cmd/state"
-        }
-        $binaries += $binary
-    }
-
-    if ($BuildTarget -eq "all" -or $BuildTarget -eq "hack") {
-        $binary = New-Object -TypeName psobject -Property @{
-            Executable="hack.exe" 
-            Source="./cmd/hack"
-        }
-        $binaries += $binary
-    }
-        
-    if ($BuildTarget -eq "all" -or $BuildTarget -eq "sentry") {
-        $binary = New-Object -TypeName psobject -Property @{
-            Executable="sentry.exe" 
-            Source="./cmd/sentry"
-        }
-        $binaries += $binary
+} elseif ($BuildTarget -eq "test-integration") {
+    Write-Host " Running integration tests ..."
+    $env:GODEBUG = "cgocheck=0"
+    $TestCommand = "go test $($Erigon.BuildFlags) ./... -p 2 --timeout 30m -tags $($Erigon.BuildTags),integration"
+    Invoke-Expression -Command $TestCommand | Out-Host
+    if (!($?)) {
+        Write-Host " ERROR : Tests failed"
+        exit 1
+    } else {
+        Write-Host "`n Tests completed"
     }
 
-    if ($BuildTarget -eq "all" -or $BuildTarget -eq "downloader") {
-        $binary = New-Object -TypeName psobject -Property @{
-            Executable="downloader.exe" 
-            Source="./cmd/downloader"
+} else {
+    if (!($Erigon.Submodules)) {
+        Write-Host " Updating git submodules ..."
+        Invoke-Expression -Command "git.exe submodule update --init --recursive --force" | Out-Host
+        if (!($?)) {
+            Write-Host " ERROR : Update submodules failed"
+            exit 1
         }
-        $binaries += $binary
+        $Erigon.Submodules = $true
     }
 
-    if ($binaries.Count -gt 0) {
-        $binaries | ForEach-Object {
-            if($_.Executable -ieq "erigon.exe" -or $_.Executable -ieq "downloader.exe") {
-                if(-not $Erigon.Submodules) {
-                    Write-Host " Updating git submodules ..."
-                    Invoke-Expression -Command "git.exe submodule update --init --recursive --force" | Out-Host
-                    if($LASTEXITCODE) {
-                        Write-Host " ERROR : Update submodules failed"
-                        exit 1
-                    }
-                    $Erigon.Submodules = $true
-                }
-            }
-            Write-Host "`n Building $($_.Executable)"
-            $outExecutable = [string](Join-Path $Erigon.BinPath $_.Executable)
-            $BuildCommand = "$($Erigon.Build) -o ""$($outExecutable)"" $($_.Source)"
-            $BuildCommand += ';$?'
-            Invoke-Expression -Command $BuildCommand | Out-Null
-            if ($LASTEXITCODE) {
-                Write-Host " ERROR : Could not build $($_.Executable)"
-                exit 1
-            } else {
-                Write-Host "`n Built $($_.Executable). Run $($outExecutable) to launch"
-            }
-        }
+    Write-Host "`n Building $BuildTarget"
+    $outExecutable = [string](Join-Path $Erigon.BinPath "$BuildTarget.exe")
+    $BuildCommand = "go build $($Erigon.BuildFlags) -o ""$($outExecutable)"" ./cmd/$BuildTarget"
+    Invoke-Expression -Command $BuildCommand | Out-Host
+    if (!($?)) {
+        Write-Host " ERROR : Could not build $BuildTarget"
+        exit 1
+    } else {
+        Write-Host "`n Built $($BuildTarget). Run $($outExecutable) to launch"
     }
 }
 }
+
 # Return to source folder
 Set-Location $MyContext.Directory
-- 
GitLab