From 62476f0e0765b3d1847d364bb1f5687aeda63399 Mon Sep 17 00:00:00 2001
From: Andrea Lanfranchi <andrea.lanfranchi@gmail.com>
Date: Wed, 23 Jun 2021 13:25:06 +0200
Subject: [PATCH] Change MDBX tools build location so it does not dirty
 submodule (#2223)

* Change MDBX build location so it does not dirty submodule

* Update README.md

* Update README.md
---
 README.md |  4 ++--
 wmake.ps1 | 45 ++++++++++++++++++++++++++-------------------
 2 files changed, 28 insertions(+), 21 deletions(-)

diff --git a/README.md b/README.md
index cff22c8b54..50a55d82f5 100644
--- a/README.md
+++ b/README.md
@@ -100,13 +100,13 @@ Windows users may run erigon in 3 possible ways:
   There are some requirements for a successful native build on windows :
   * [Git](https://git-scm.com/downloads) for Windows must be installed. If you're cloning this repository is very likely you already have it
   * [GO Programming Language](https://golang.org/dl/) must be installed. Minimum required version is 1.16
-  * If you need to build MDBX tools then [Chocolatey package manager](https://chocolatey.org/) for Windows must be installed. By Chocolatey you need to install the following components : `cmake`, `make`, `mingw` by `choco install cmake make mingw`.
+  * If you need to build MDBX tools (i.e. `.\wmake.ps1 db-tools`) then [Chocolatey package manager](https://chocolatey.org/) for Windows must be installed. By Chocolatey you need to install the following components : `cmake`, `make`, `mingw` by `choco install cmake make mingw`.
 
   **Important note about Anti-Viruses**
   During MinGW's compiler detection phase some temporary executables are generated to test compiler capabilities. It's been reported some anti-virus programs detect
   those files as possibly infected by `Win64/Kryptic.CIS` trojan horse (or a variant of it). Although those are false positives we have no control over 100+ vendors of
   security products for Windows and their respective detection algorythms and we understand this might make your experience with Windows builds uncomfortable. To
-  workaround the issue you might either set exlusions for your antivirus specifically for `ethdb\mdbx\dist\CMakeFiles` folder or you can run erigon on Docker or WSL
+  workaround the issue you might either set exlusions for your antivirus specifically for `build\bin\mdbx\CMakeFiles` sub-folder of the cloned repo or you can run erigon using the following other two options
 
 * Use Docker :  see [docker-compose.yml](./docker-compose.yml)
 
diff --git a/wmake.ps1 b/wmake.ps1
index 58a8d8193c..dae77a4a17 100644
--- a/wmake.ps1
+++ b/wmake.ps1
@@ -368,21 +368,32 @@ if ($BuildTarget -eq "all" -or $BuildTarget -eq "db-tools") {
         exit 1
     }
 
-    # Enter MDBX directory and build libmdbx.dll
-    Set-Location (Join-Path $MyContext.Directory "libmdbx")
-    if(!$?) {
-        Write-Host @" 
-     
-       Error ! Can't locate ""libmdbx"" folder
-       Are you sure you have cloned the repository properly ?
-     
+    $Erigon.MDBXSourcePath = [string](Join-Path $MyContext.StartDir "\libmdbx")
+    if (!Test-Path -Path $Erigon.MDBXSourcePath -PathType Directory) {
+        Write-Host @"
+
+        Error ! Can't locate ""libmdbx"" folder
+        Are you sure you have cloned the repository properly ?
 "@
-    exit 1
-     }
-     
+        exit 1
+    }
+
+    if (!Test-Path -Path [string](Join-Path $Erigon.MDBXSourcePath "\.git") -PathType Directory) {
+        git.exe submodule update --init --recursive
+        if($LASTEXITCODE) {
+            Write-Host "An error has occurred while updating libmdbx submodule"
+            exit $LASTEXITCODE
+        }
+    }
+
+    # Create build directory for mdbx and enter it
+    $Erigon.MDBXBuildPath = [string](Join-Path $Erigon.BinPath "\mdbx")
+    New-Item -Path $Erigon.MDBXBuildPath -ItemType Directory -Force | Out-Null
+    Set-Location $Erigon.MDBXBuildPath
+
     Write-Host " Building db-tools ..."
 
-    cmake -G "MinGW Makefiles" . `
+    cmake -G "MinGW Makefiles" "$($Erigon.MDBXSourcePath)" `
     -D CMAKE_MAKE_PROGRAM:PATH=""$(Join-Path $chocolateyBinPath "make.exe")"" `
     -D CMAKE_C_COMPILER:PATH=""$(Join-Path $chocolateyBinPath "gcc.exe")"" `
     -D CMAKE_CXX_COMPILER:PATH=""$(Join-Path $chocolateyBinPath "g++.exe")"" `
@@ -402,14 +413,15 @@ if ($BuildTarget -eq "all" -or $BuildTarget -eq "db-tools") {
                      -or !(Test-Path "mdbx_copy.exe" -PathType leaf) `
                      -or !(Test-Path "mdbx_dump.exe" -PathType leaf) `
                      -or !(Test-Path "mdbx_load.exe" -PathType leaf) `
-                     -or !(Test-Path "mdbx_drop.exe" -PathType leaf)) {
+                     -or !(Test-Path "mdbx_drop.exe" -PathType leaf) `
+                     -or !(Test-Path "mdbx_test.exe" -PathType leaf)) {
         Write-Host "An error has occurred while building MDBX tools"
         exit $LASTEXITCODE
     }
 
     Set-Location $MyContext.Directory
     # Eventually move all mdbx_*.exe to ./build/bin directory
-    Move-Item -Path "./libmdbx/mdbx_*.exe" -Destination $Erigon.BinPath -Force
+    Move-Item -Path "$($Erigon.MDBXBuildPath)/mdbx_*.exe" -Destination $Erigon.BinPath -Force
 
 }
     
@@ -417,11 +429,6 @@ if ($BuildTarget -eq "clean") {
 
     Write-Host " Cleaning ..."
 
-    # Clean ./libmdbx/mdbx_*.exe
-    Remove-Item -Path "./libmdbx/mdbx_*.exe" -Force
-    Remove-Item -Path "./libmdbx/CMakeCache.txt" -Force
-    Remove-Item -Path "./libmdbx/CMakeFiles" -Recurse -Force
-
     # Remove ./build/bin directory
     Remove-Item -Path "./build" -Recurse -Force
 
-- 
GitLab