From 9c7ea0c8487bbb7077d069a2c449ed25152345f7 Mon Sep 17 00:00:00 2001
From: "steven.shi@intel.com" <steven.shi@intel.com>
Date: Tue, 10 Aug 2021 23:15:57 +0800
Subject: [PATCH] clang + gnumake build in windows

---
 BaseTools/Source/C/BrotliCompress/GNUmakefile |  8 +-
 BaseTools/Source/C/Common/CommonLib.h         |  9 ++
 BaseTools/Source/C/Common/MyAlloc.c           |  4 +-
 BaseTools/Source/C/Common/MyAlloc.h           |  4 +-
 BaseTools/Source/C/DevicePath/GNUmakefile     |  6 +-
 BaseTools/Source/C/EfiRom/GNUmakefile         |  6 +-
 BaseTools/Source/C/GNUmakefile                | 74 +++++++++-------
 BaseTools/Source/C/GenCrc32/GNUmakefile       |  6 +-
 BaseTools/Source/C/GenFfs/GNUmakefile         |  6 +-
 BaseTools/Source/C/GenFv/GNUmakefile          |  6 +-
 BaseTools/Source/C/GenFw/GNUmakefile          |  6 +-
 BaseTools/Source/C/GenSec/GNUmakefile         |  6 +-
 BaseTools/Source/C/LzmaCompress/GNUmakefile   |  6 +-
 BaseTools/Source/C/Makefiles/app.makefile     |  5 +-
 BaseTools/Source/C/Makefiles/header.makefile  | 84 ++++++++++++++-----
 BaseTools/Source/C/Makefiles/lib.makefile     |  6 +-
 BaseTools/Source/C/Makefiles/ms.app           |  2 +-
 BaseTools/Source/C/Makefiles/ms.common        |  9 ++
 BaseTools/Source/C/TianoCompress/GNUmakefile  |  6 +-
 BaseTools/Source/C/VfrCompile/GNUmakefile     | 29 ++++++-
 .../C/VfrCompile/Pccts/antlr/AntlrMS.mak      |  9 +-
 .../Source/C/VfrCompile/Pccts/antlr/makefile  |  7 ++
 .../Source/C/VfrCompile/Pccts/dlg/DlgMS.mak   |  9 +-
 .../Source/C/VfrCompile/Pccts/dlg/makefile    |  7 ++
 BaseTools/Source/C/VolInfo/GNUmakefile        |  6 +-
 BaseTools/Tests/GNUmakefile                   | 17 ++--
 BaseTools/Tests/TestTools.py                  |  2 +-
 BaseTools/Tests/TianoCompress.py              |  5 +-
 BaseTools/toolsetup.bat                       | 14 +++-
 edksetup.bat                                  |  7 +-
 30 files changed, 286 insertions(+), 85 deletions(-)

diff --git a/BaseTools/Source/C/BrotliCompress/GNUmakefile b/BaseTools/Source/C/BrotliCompress/GNUmakefile
index b150e5dd2b..aefada57c0 100644
--- a/BaseTools/Source/C/BrotliCompress/GNUmakefile
+++ b/BaseTools/Source/C/BrotliCompress/GNUmakefile
@@ -6,7 +6,11 @@
 #
 MAKEROOT ?= ..
 
-APPNAME = BrotliCompress
+ifeq ($(OS),Windows_NT)
+  APPNAME = BrotliCompress.exe
+else
+  APPNAME = BrotliCompress
+endif
 
 OBJECTS = \
   BrotliCompress.o \
@@ -38,4 +42,6 @@ OBJECTS = \
 include $(MAKEROOT)/Makefiles/app.makefile
 
 TOOL_INCLUDE = -I ./brotli/c/include
+ifneq ($(OS),Windows_NT)
 LIBS += -lm
+endif
\ No newline at end of file
diff --git a/BaseTools/Source/C/Common/CommonLib.h b/BaseTools/Source/C/Common/CommonLib.h
index 0f05d88db2..13b640e1cf 100644
--- a/BaseTools/Source/C/Common/CommonLib.h
+++ b/BaseTools/Source/C/Common/CommonLib.h
@@ -443,6 +443,15 @@ Returns:
 }
 #endif
 
+#ifndef __GNUC__
+#include <stdio.h>
+#include <sys/stat.h>
+#define stricmp _stricmp // https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/stricmp-wcsicmp
+#define getcwd _getcwd //https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/getcwd
+#define access _access //https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/access-crt
+#define fileno _fileno //https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/posix-fileno
+#endif
+
 #ifdef __GNUC__
 #include <stdio.h>
 #include <sys/stat.h>
diff --git a/BaseTools/Source/C/Common/MyAlloc.c b/BaseTools/Source/C/Common/MyAlloc.c
index d104795d46..0a1b5d9c85 100644
--- a/BaseTools/Source/C/Common/MyAlloc.c
+++ b/BaseTools/Source/C/Common/MyAlloc.c
@@ -159,7 +159,7 @@ MyCheck (
 // ////////////////////////////////////////////////////////////////////////////
 //
 //
-VOID *
+static VOID *
 MyAlloc (
   UINTN      Size,
   UINT8 File[],
@@ -402,7 +402,7 @@ MyRealloc (
 // ////////////////////////////////////////////////////////////////////////////
 //
 //
-VOID
+static VOID
 MyFree (
   VOID       *Ptr,
   UINT8 File[],
diff --git a/BaseTools/Source/C/Common/MyAlloc.h b/BaseTools/Source/C/Common/MyAlloc.h
index aff29d05ab..c4ed79ee8f 100644
--- a/BaseTools/Source/C/Common/MyAlloc.h
+++ b/BaseTools/Source/C/Common/MyAlloc.h
@@ -103,7 +103,7 @@ MyCheck (
 //
 // --*/
 //
-VOID  *
+static VOID  *
 MyAlloc (
   UINTN      Size,
   UINT8      File[],
@@ -167,7 +167,7 @@ MyRealloc (
 //
 // --*/
 //
-VOID
+static VOID
 MyFree (
   VOID       *Ptr,
   UINT8      File[],
diff --git a/BaseTools/Source/C/DevicePath/GNUmakefile b/BaseTools/Source/C/DevicePath/GNUmakefile
index 7ca08af966..b67861a3a7 100644
--- a/BaseTools/Source/C/DevicePath/GNUmakefile
+++ b/BaseTools/Source/C/DevicePath/GNUmakefile
@@ -7,7 +7,11 @@
 ARCH ?= IA32
 MAKEROOT ?= ..
 
-APPNAME = DevicePath
+ifeq ($(OS),Windows_NT)
+  APPNAME = DevicePath.exe
+else
+  APPNAME = DevicePath
+endif
 
 OBJECTS = DevicePath.o UefiDevicePathLib.o DevicePathFromText.o  DevicePathUtilities.o
 
diff --git a/BaseTools/Source/C/EfiRom/GNUmakefile b/BaseTools/Source/C/EfiRom/GNUmakefile
index 7220b2ae44..2d6b23db67 100644
--- a/BaseTools/Source/C/EfiRom/GNUmakefile
+++ b/BaseTools/Source/C/EfiRom/GNUmakefile
@@ -6,7 +6,11 @@
 #
 MAKEROOT ?= ..
 
-APPNAME = EfiRom
+ifeq ($(OS),Windows_NT)
+  APPNAME = EfiRom.exe
+else
+  APPNAME = EfiRom
+endif
 
 LIBS = -lCommon
 
diff --git a/BaseTools/Source/C/GNUmakefile b/BaseTools/Source/C/GNUmakefile
index 8c191e0c38..b4b90d017d 100644
--- a/BaseTools/Source/C/GNUmakefile
+++ b/BaseTools/Source/C/GNUmakefile
@@ -7,35 +7,47 @@
 #
 
 ifndef HOST_ARCH
-  #
-  # If HOST_ARCH is not defined, then we use 'uname -m' to attempt
-  # try to figure out the appropriate HOST_ARCH.
-  #
-  uname_m = $(shell uname -m)
-  $(info Attempting to detect HOST_ARCH from 'uname -m': $(uname_m))
-  ifneq (,$(strip $(filter $(uname_m), x86_64 amd64)))
-    HOST_ARCH=X64
+  ifeq ($(OS),Windows_NT)
+      set_pro = $(shell set pro)
+      $(info Attempting to detect HOST_ARCH from 'set pro': $(set_pro))
+      ifneq (,$(strip $(filter (x86) %AMD64, $(set_pro))))
+        HOST_ARCH=X64
+        $(info the HOST_ARCH is X64)
+      endif
+      ifeq ($(patsubst i%86,IA32,$(uname_m)),IA32)
+        HOST_ARCH=IA32
+        $(info the HOST_ARCH is IA32)
+      endif
+  else
+      #
+      # If HOST_ARCH is not defined, then we use 'uname -m' to attempt
+      # try to figure out the appropriate HOST_ARCH.
+      #
+      uname_m = $(shell uname -m)
+      $(info Attempting to detect HOST_ARCH from 'uname -m': $(uname_m))
+      ifneq (,$(strip $(filter $(uname_m), x86_64 amd64)))
+        HOST_ARCH=X64
+      endif
+      ifeq ($(patsubst i%86,IA32,$(uname_m)),IA32)
+        HOST_ARCH=IA32
+      endif
+      ifneq (,$(findstring aarch64,$(uname_m)))
+        HOST_ARCH=AARCH64
+      else ifneq (,$(findstring arm64,$(uname_m)))
+        HOST_ARCH=AARCH64
+      else ifneq (,$(findstring arm,$(uname_m)))
+        HOST_ARCH=ARM
+      endif
+      ifneq (,$(findstring riscv64,$(uname_m)))
+        HOST_ARCH=RISCV64
+      endif
+      ifndef HOST_ARCH
+        $(info Could not detected HOST_ARCH from uname results)
+        $(error HOST_ARCH is not defined!)
+      endif
+      $(info Detected HOST_ARCH of $(HOST_ARCH) using uname.)
+    endif
   endif
-  ifeq ($(patsubst i%86,IA32,$(uname_m)),IA32)
-    HOST_ARCH=IA32
-  endif
-  ifneq (,$(findstring aarch64,$(uname_m)))
-    HOST_ARCH=AARCH64
-  else ifneq (,$(findstring arm64,$(uname_m)))
-    HOST_ARCH=AARCH64
-  else ifneq (,$(findstring arm,$(uname_m)))
-    HOST_ARCH=ARM
-  endif
-  ifneq (,$(findstring riscv64,$(uname_m)))
-    HOST_ARCH=RISCV64
-  endif
-  ifndef HOST_ARCH
-    $(info Could not detected HOST_ARCH from uname results)
-    $(error HOST_ARCH is not defined!)
-  endif
-  $(info Detected HOST_ARCH of $(HOST_ARCH) using uname.)
-endif
-
 export HOST_ARCH
 
 MAKEROOT = .
@@ -66,9 +78,13 @@ SUBDIRS := $(LIBRARIES) $(APPLICATIONS)
 $(LIBRARIES): $(MAKEROOT)/libs
 $(APPLICATIONS): $(LIBRARIES) $(MAKEROOT)/bin $(VFRAUTOGEN)
 
+ifeq ($(OS),Windows_NT)
+  $(APPLICATIONS): $(BASE_TOOLS_PATH)\Bin\Win32
+endif
+
 .PHONY: outputdirs
 makerootdir:
-	-mkdir -p $(MAKEROOT)
+	-$(MKDIR) $(MAKEROOT)
 
 .PHONY: subdirs $(SUBDIRS)
 subdirs: $(SUBDIRS)
diff --git a/BaseTools/Source/C/GenCrc32/GNUmakefile b/BaseTools/Source/C/GenCrc32/GNUmakefile
index fe4575d5fc..a72a1c988a 100644
--- a/BaseTools/Source/C/GenCrc32/GNUmakefile
+++ b/BaseTools/Source/C/GenCrc32/GNUmakefile
@@ -6,7 +6,11 @@
 #
 MAKEROOT ?= ..
 
-APPNAME = GenCrc32
+ifeq ($(OS),Windows_NT)
+  APPNAME = GenCrc32.exe
+else
+  APPNAME = GenCrc32
+endif
 
 LIBS = -lCommon
 
diff --git a/BaseTools/Source/C/GenFfs/GNUmakefile b/BaseTools/Source/C/GenFfs/GNUmakefile
index 3d41a5855f..4099e74260 100644
--- a/BaseTools/Source/C/GenFfs/GNUmakefile
+++ b/BaseTools/Source/C/GenFfs/GNUmakefile
@@ -6,7 +6,11 @@
 #
 MAKEROOT ?= ..
 
-APPNAME = GenFfs
+ifeq ($(OS),Windows_NT)
+  APPNAME = GenFfs.exe
+else
+  APPNAME = GenFfs
+endif
 
 OBJECTS = GenFfs.o
 
diff --git a/BaseTools/Source/C/GenFv/GNUmakefile b/BaseTools/Source/C/GenFv/GNUmakefile
index 7c7b95ba1b..eb4e9722cc 100644
--- a/BaseTools/Source/C/GenFv/GNUmakefile
+++ b/BaseTools/Source/C/GenFv/GNUmakefile
@@ -6,7 +6,11 @@
 #
 MAKEROOT ?= ..
 
-APPNAME = GenFv
+ifeq ($(OS),Windows_NT)
+  APPNAME = GenFv.exe
+else
+  APPNAME = GenFv
+endif
 
 OBJECTS = GenFv.o GenFvInternalLib.o
 
diff --git a/BaseTools/Source/C/GenFw/GNUmakefile b/BaseTools/Source/C/GenFw/GNUmakefile
index 76cda7e7a3..ce0f3684ac 100644
--- a/BaseTools/Source/C/GenFw/GNUmakefile
+++ b/BaseTools/Source/C/GenFw/GNUmakefile
@@ -6,7 +6,11 @@
 #
 MAKEROOT ?= ..
 
-APPNAME = GenFw
+ifeq ($(OS),Windows_NT)
+  APPNAME = GenFw.exe
+else
+  APPNAME = GenFw
+endif
 
 OBJECTS = GenFw.o ElfConvert.o Elf32Convert.o Elf64Convert.o
 
diff --git a/BaseTools/Source/C/GenSec/GNUmakefile b/BaseTools/Source/C/GenSec/GNUmakefile
index 9f0844c1b8..f528ed6f91 100644
--- a/BaseTools/Source/C/GenSec/GNUmakefile
+++ b/BaseTools/Source/C/GenSec/GNUmakefile
@@ -6,7 +6,11 @@
 #
 MAKEROOT ?= ..
 
-APPNAME = GenSec
+ifeq ($(OS),Windows_NT)
+  APPNAME = GenSec.exe
+else
+  APPNAME = GenSec
+endif
 
 OBJECTS = GenSec.o
 
diff --git a/BaseTools/Source/C/LzmaCompress/GNUmakefile b/BaseTools/Source/C/LzmaCompress/GNUmakefile
index c837e77823..1c7d24545e 100644
--- a/BaseTools/Source/C/LzmaCompress/GNUmakefile
+++ b/BaseTools/Source/C/LzmaCompress/GNUmakefile
@@ -6,7 +6,11 @@
 #
 MAKEROOT ?= ..
 
-APPNAME = LzmaCompress
+ifeq ($(OS),Windows_NT)
+  APPNAME = LzmaCompress.exe
+else
+  APPNAME = LzmaCompress
+endif
 
 LIBS = -lCommon
 
diff --git a/BaseTools/Source/C/Makefiles/app.makefile b/BaseTools/Source/C/Makefiles/app.makefile
index 6a2a8f5e8a..e433f6cb33 100644
--- a/BaseTools/Source/C/Makefiles/app.makefile
+++ b/BaseTools/Source/C/Makefiles/app.makefile
@@ -9,13 +9,16 @@ MAKEROOT ?= ../..
 
 include $(MAKEROOT)/Makefiles/header.makefile
 
-APPLICATION = $(MAKEROOT)/bin/$(APPNAME)
+APPLICATION = $(MAKEROOT)$(SEP)bin$(SEP)$(APPNAME)
 
 .PHONY:all
 all: $(MAKEROOT)/bin $(APPLICATION)
 
 $(APPLICATION): $(OBJECTS)
 	$(LINKER) -o $(APPLICATION) $(BUILD_LFLAGS) $(OBJECTS) -L$(MAKEROOT)/libs $(LIBS)
+ifeq ($(OS),Windows_NT)
+	$(CP) $(APPLICATION) $(EDK_TOOLS_PATH)\Bin\Win32
+endif
 
 $(OBJECTS): $(MAKEROOT)/Include/Common/BuildVersion.h
 
diff --git a/BaseTools/Source/C/Makefiles/header.makefile b/BaseTools/Source/C/Makefiles/header.makefile
index 0df728f327..e02f479476 100644
--- a/BaseTools/Source/C/Makefiles/header.makefile
+++ b/BaseTools/Source/C/Makefiles/header.makefile
@@ -8,6 +8,24 @@
 # Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.<BR>
 # SPDX-License-Identifier: BSD-2-Clause-Patent
 
+ifeq ($(OS),Windows_NT)
+  SEP :=$(subst /,\,/)
+  EXE_EXT := .exe
+  MKDIR := mkdir
+  MV    := move /Y
+  RM    := del /F /S
+  CP    := copy /y
+else
+  SEP :=/
+  EXE_EXT :=
+  MKDIR := mkdir -p
+  MV    := mv -f
+  RM    := rm -f
+  CP    := cp -f
+  SED   := sed
+  TEST  := test
+endif
+
 ifndef HOST_ARCH
   #
   # If HOST_ARCH is not defined, then we use 'uname -m' to attempt
@@ -38,9 +56,12 @@ ifndef HOST_ARCH
   $(info Detected HOST_ARCH of $(HOST_ARCH) using uname.)
 endif
 
-CYGWIN:=$(findstring CYGWIN, $(shell uname -s))
-LINUX:=$(findstring Linux, $(shell uname -s))
-DARWIN:=$(findstring Darwin, $(shell uname -s))
+ifneq ($(OS),Windows_NT)
+  CYGWIN:=$(findstring CYGWIN, $(shell uname -s))
+  LINUX:=$(findstring Linux, $(shell uname -s))
+  DARWIN:=$(findstring Darwin, $(shell uname -s))
+endif
+
 ifeq ($(CXX), llvm)
 BUILD_CC ?= $(CLANG_BIN)clang
 BUILD_CXX ?= $(CLANG_BIN)clang++
@@ -74,32 +95,44 @@ else
 $(error Bad HOST_ARCH)
 endif
 
-INCLUDE = $(TOOL_INCLUDE) -I $(MAKEROOT) -I $(MAKEROOT)/Include/Common -I $(MAKEROOT)/Include/ -I $(MAKEROOT)/Include/IndustryStandard -I $(MAKEROOT)/Common/ -I .. -I . $(ARCH_INCLUDE)
-BUILD_CPPFLAGS = $(INCLUDE)
+INCLUDES = $(TOOL_INCLUDE) -I $(MAKEROOT) -I $(MAKEROOT)/Include/Common -I $(MAKEROOT)/Include/ -I $(MAKEROOT)/Include/IndustryStandard -I $(MAKEROOT)/Common/ -I .. -I . $(ARCH_INCLUDE) -I "$(INCLUDE)"
+BUILD_CPPFLAGS = $(INCLUDES)
 
 # keep EXTRA_OPTFLAGS last
 BUILD_OPTFLAGS = -O2 $(EXTRA_OPTFLAGS)
 
 ifeq ($(DARWIN),Darwin)
-# assume clang or clang compatible flags on OS X
-BUILD_CFLAGS = -MD -fshort-wchar -fno-strict-aliasing -Wall -Werror \
--Wno-deprecated-declarations -Wno-self-assign -Wno-unused-result -nostdlib -g
-else
-ifeq ($(CXX), llvm)
-BUILD_CFLAGS = -MD -fshort-wchar -fno-strict-aliasing -fwrapv \
--fno-delete-null-pointer-checks -Wall -Werror \
--Wno-deprecated-declarations -Wno-self-assign \
--Wno-unused-result -nostdlib -g
+  # assume clang or clang compatible flags on OS X
+  BUILD_CFLAGS = -MD -fshort-wchar -fno-strict-aliasing -Wall -Werror \
+  -Wno-deprecated-declarations -Wno-self-assign -Wno-unused-result -nostdlib -g
 else
-BUILD_CFLAGS = -MD -fshort-wchar -fno-strict-aliasing -fwrapv \
--fno-delete-null-pointer-checks -Wall -Werror \
--Wno-deprecated-declarations -Wno-stringop-truncation -Wno-restrict \
--Wno-unused-result -nostdlib -g
-endif
+  ifeq ($(CXX), llvm)
+    ifeq ($(OS),Windows_NT)
+      BUILD_CFLAGS = -MD -fshort-wchar -fno-strict-aliasing -fwrapv \
+      -fno-delete-null-pointer-checks -Wall -Werror \
+      -Wno-deprecated-declarations -Wno-self-assign \
+      -Wno-unused-result -nostdlib -Wno-unused-function -g -m32
+    else
+      BUILD_CFLAGS = -MD -fshort-wchar -fno-strict-aliasing -fwrapv \
+      -fno-delete-null-pointer-checks -Wall -Werror \
+      -Wno-deprecated-declarations -Wno-self-assign \
+      -Wno-unused-result -nostdlib -g
+    endif
+  else
+    BUILD_CFLAGS = -MD -fshort-wchar -fno-strict-aliasing -fwrapv \
+    -fno-delete-null-pointer-checks -Wall -Werror \
+    -Wno-deprecated-declarations -Wno-stringop-truncation -Wno-restrict \
+    -Wno-unused-result -nostdlib -g
+  endif
 endif
 ifeq ($(CXX), llvm)
-BUILD_LFLAGS =
-BUILD_CXXFLAGS = -Wno-deprecated-register -Wno-unused-result
+  ifeq ($(OS),Windows_NT)
+    BUILD_LFLAGS = -m32
+    BUILD_CXXFLAGS = -Wno-deprecated-register -Wno-unused-result -D_CRT_SECURE_NO_DEPRECATE -D_CRT_NONSTDC_NO_DEPRECATE -m32
+  else
+    BUILD_LFLAGS =
+    BUILD_CXXFLAGS = -Wno-deprecated-register -Wno-unused-result
+  endif
 else
 BUILD_LFLAGS =
 BUILD_CXXFLAGS = -Wno-unused-result
@@ -131,7 +164,12 @@ BUILD_LFLAGS += $(EXTRA_LDFLAGS)
 all:
 
 $(MAKEROOT)/libs:
-	mkdir $(MAKEROOT)/libs
+	$(MKDIR) $(MAKEROOT)$(SEP)libs
 
 $(MAKEROOT)/bin:
-	mkdir $(MAKEROOT)/bin
+	$(MKDIR) $(MAKEROOT)$(SEP)bin
+
+ifeq ($(OS),Windows_NT)
+  $(BASE_TOOLS_PATH)\Bin\Win32:
+		-$(MKDIR) $(BASE_TOOLS_PATH)\Bin\Win32
+endif
diff --git a/BaseTools/Source/C/Makefiles/lib.makefile b/BaseTools/Source/C/Makefiles/lib.makefile
index 2577c15380..28aaab5bed 100644
--- a/BaseTools/Source/C/Makefiles/lib.makefile
+++ b/BaseTools/Source/C/Makefiles/lib.makefile
@@ -7,7 +7,11 @@
 
 include $(MAKEROOT)/Makefiles/header.makefile
 
-LIBRARY = $(MAKEROOT)/libs/lib$(LIBNAME).a
+ifeq ($(OS),Windows_NT)
+  LIBRARY = $(MAKEROOT)/libs/$(LIBNAME).lib
+else
+  LIBRARY = $(MAKEROOT)/libs/lib$(LIBNAME).a
+endif
 
 all: $(MAKEROOT)/libs $(LIBRARY)
 
diff --git a/BaseTools/Source/C/Makefiles/ms.app b/BaseTools/Source/C/Makefiles/ms.app
index aecae37396..0dd0daf6f7 100644
--- a/BaseTools/Source/C/Makefiles/ms.app
+++ b/BaseTools/Source/C/Makefiles/ms.app
@@ -13,7 +13,7 @@ $(APPLICATION) : $(OBJECTS)
 	-@if not exist $(BIN_PATH) mkdir $(BIN_PATH)
 	$(LD) /nologo /debug /OPT:REF /OPT:ICF=10 /incremental:no /nodefaultlib:libc.lib /out:$@ $(LIBS) $**
 
-$(OBJECTS) : $(SOURCE_PATH)\Include\Common\BuildVersion.h
+#$(OBJECTS) : $(SOURCE_PATH)\Include\Common\BuildVersion.h
 
 .PHONY:clean
 .PHONY:cleanall
diff --git a/BaseTools/Source/C/Makefiles/ms.common b/BaseTools/Source/C/Makefiles/ms.common
index b2dbcf376c..ef58303189 100644
--- a/BaseTools/Source/C/Makefiles/ms.common
+++ b/BaseTools/Source/C/Makefiles/ms.common
@@ -60,3 +60,12 @@ INC = $(INC) -I . -I $(SOURCE_PATH)\Include -I $(ARCH_INCLUDE) -I $(SOURCE_PATH)
 CFLAGS = $(CFLAGS) /nologo /Zi /c /O2 /MT /W4 /WX /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE
 CPPFLAGS = $(CPPFLAGS) /EHsc /nologo /Zi /c /O2 /MT /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE
 
+!IF "$(CLANG_BUILD)"=="TRUE"
+CC = "$(CLANG_BIN)\clang-cl.exe"
+CXX = "$(CLANG_BIN)\clang-cl.exe"
+AR = "$(CLANG_BIN)\llvm-lib.exe"
+LD = "$(CLANG_BIN)\lld-link.exe"
+
+CFLAGS = $(CFLAGS) /nologo /Zi /c /O2 /MT /W4 /WX /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /w -m32
+CPPFLAGS = $(CPPFLAGS) /EHsc /nologo /Zi /c /O2 /MT /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /w -m32
+!ENDIF
diff --git a/BaseTools/Source/C/TianoCompress/GNUmakefile b/BaseTools/Source/C/TianoCompress/GNUmakefile
index d164d37b3d..07cb4b4119 100644
--- a/BaseTools/Source/C/TianoCompress/GNUmakefile
+++ b/BaseTools/Source/C/TianoCompress/GNUmakefile
@@ -6,7 +6,11 @@
 #
 MAKEROOT ?= ..
 
-APPNAME = TianoCompress
+ifeq ($(OS),Windows_NT)
+  APPNAME = TianoCompress.exe
+else
+  APPNAME = TianoCompress
+endif
 
 LIBS = -lCommon
 
diff --git a/BaseTools/Source/C/VfrCompile/GNUmakefile b/BaseTools/Source/C/VfrCompile/GNUmakefile
index fc329944b9..d663f50e84 100644
--- a/BaseTools/Source/C/VfrCompile/GNUmakefile
+++ b/BaseTools/Source/C/VfrCompile/GNUmakefile
@@ -7,7 +7,11 @@
 
 MAKEROOT ?= ..
 
-APPNAME = VfrCompile
+ifeq ($(OS),Windows_NT)
+  APPNAME = VfrCompile.exe
+else
+  APPNAME = VfrCompile
+endif
 
 LIBS = -lCommon
 
@@ -17,7 +21,11 @@ TOOL_INCLUDE = -I Pccts/h
 OBJECTS = AParser.o DLexerBase.o ATokenBuffer.o EfiVfrParser.o VfrLexer.o VfrSyntax.o \
           VfrFormPkg.o VfrError.o VfrUtilityLib.o VfrCompiler.o
 ifeq ($(CXX), llvm)
-VFR_CPPFLAGS = -Wno-deprecated-register -DPCCTS_USE_NAMESPACE_STD $(BUILD_CPPFLAGS)
+  ifeq ($(OS),Windows_NT)
+    VFR_CPPFLAGS = -Wno-deprecated-register -DPCCTS_USE_NAMESPACE_STD $(BUILD_CPPFLAGS) -D_CRT_SECURE_NO_DEPRECATE -D_CRT_NONSTDC_NO_DEPRECATE -m32
+  else
+    VFR_CPPFLAGS = -Wno-deprecated-register -DPCCTS_USE_NAMESPACE_STD $(BUILD_CPPFLAGS)
+  endif
 else
 VFR_CPPFLAGS = -DPCCTS_USE_NAMESPACE_STD $(BUILD_CPPFLAGS)
 endif
@@ -25,7 +33,11 @@ endif
 VFR_CXXFLAGS = $(BUILD_OPTFLAGS)
 
 # keep EXTRA_LDFLAGS last
+ifeq ($(OS),Windows_NT)
+VFR_LFLAGS = $(EXTRA_LDFLAGS) -m32
+else
 VFR_LFLAGS = $(EXTRA_LDFLAGS)
+endif
 
 LINKER = $(BUILD_CXX)
 
@@ -35,13 +47,16 @@ MAKEROOT ?= ../..
 
 include $(MAKEROOT)/Makefiles/header.makefile
 
-APPLICATION = $(MAKEROOT)/bin/$(APPNAME)
+APPLICATION = $(MAKEROOT)$(SEP)bin$(SEP)$(APPNAME)
 
 .PHONY:all
 all: $(MAKEROOT)/bin $(APPLICATION)
 
 $(APPLICATION): $(OBJECTS)
 	$(LINKER) -o $(APPLICATION) $(VFR_LFLAGS) $(OBJECTS) -L$(MAKEROOT)/libs $(LIBS)
+ifeq ($(OS),Windows_NT)
+	$(CP) $(APPLICATION) $(EDK_TOOLS_PATH)\Bin\Win32
+endif
 
 VfrCompiler.o: ../Include/Common/BuildVersion.h
 
@@ -54,10 +69,18 @@ VfrLexer.cpp VfrLexer.h: Pccts/dlg/dlg VfrParser.dlg
 	Pccts/dlg/dlg -C2 -i -CC -cl VfrLexer -o . VfrParser.dlg
 
 Pccts/antlr/antlr:
+ifeq ($(OS),Windows_NT)
+	set BIN_DIR='.'&& $(MAKE) -C Pccts/antlr
+else
 	BIN_DIR='.' $(MAKE) -C Pccts/antlr
+endif
 
 Pccts/dlg/dlg:
+ifeq ($(OS),Windows_NT)
+	set BIN_DIR='.'&& $(MAKE) -C Pccts/dlg
+else
 	BIN_DIR='.' $(MAKE) -C Pccts/dlg
+endif
 
 ATokenBuffer.o: Pccts/h/ATokenBuffer.cpp
 	$(BUILD_CXX) -c $(VFR_CPPFLAGS) $(INC) $(VFR_CXXFLAGS) $? -o $@
diff --git a/BaseTools/Source/C/VfrCompile/Pccts/antlr/AntlrMS.mak b/BaseTools/Source/C/VfrCompile/Pccts/antlr/AntlrMS.mak
index b30a73bb74..51ccb446dc 100644
--- a/BaseTools/Source/C/VfrCompile/Pccts/antlr/AntlrMS.mak
+++ b/BaseTools/Source/C/VfrCompile/Pccts/antlr/AntlrMS.mak
@@ -17,7 +17,14 @@ SET=$(PCCTS_HOME)\support\set
 CC = cl
 CFLAGS = /nologo -I "." -I "$(PCCTS_H)" -I "$(SET)" -D "USER_ZZSYN" -D "PC" \
         -D "ZZLEXBUFSIZE=65536"  /D "LONGFILENAMES" /Zi /W3 -D__USE_PROTOS /wd4700 \
-         /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE 
+         /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE
+
+!IF "$(CLANG_BUILD)"=="TRUE"
+CC = "$(CLANG_BIN)\clang-cl.exe"
+CFLAGS = /nologo -I "." -I "$(PCCTS_H)" -I "$(SET)" -D "USER_ZZSYN" -D "PC" \
+        -D "ZZLEXBUFSIZE=65536"  /D "LONGFILENAMES" /Zi /W3 -D__USE_PROTOS /wd4700 \
+         /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE  /w -m32
+!ENDIF
 
 ANTLR_OBJS = antlr.obj scan.obj err.obj bits.obj build.obj fset2.obj \
             fset.obj gen.obj globals.obj hash.obj lex.obj main.obj \
diff --git a/BaseTools/Source/C/VfrCompile/Pccts/antlr/makefile b/BaseTools/Source/C/VfrCompile/Pccts/antlr/makefile
index 559b1c99f1..6751e06961 100644
--- a/BaseTools/Source/C/VfrCompile/Pccts/antlr/makefile
+++ b/BaseTools/Source/C/VfrCompile/Pccts/antlr/makefile
@@ -174,7 +174,14 @@ ANTLR=${BIN_DIR}/antlr
 DLG=${BIN_DIR}/dlg
 OBJ_EXT=o
 OUT_OBJ = -o
+
+ifeq ($(OS),Windows_NT)
+BUILD_CFLAGS= $(COPT) -I. -I$(SET) -I$(PCCTS_H) -DUSER_ZZSYN $(COTHER) -DZZLEXBUFSIZE=65536 \
+        -D__USE_PROTOS -D_CRT_SECURE_NO_DEPRECATE -D_CRT_NONSTDC_NO_DEPRECATE -DPCCTS_USE_STDARG -m32
+else
 BUILD_CFLAGS= $(COPT) -I. -I$(SET) -I$(PCCTS_H) -DUSER_ZZSYN $(COTHER) -DZZLEXBUFSIZE=65536
+endif
+
 BUILD_CPPFLAGS=
 #
 # SGI Users, use this CFLAGS
diff --git a/BaseTools/Source/C/VfrCompile/Pccts/dlg/DlgMS.mak b/BaseTools/Source/C/VfrCompile/Pccts/dlg/DlgMS.mak
index 2714308d4f..fe9724ff96 100644
--- a/BaseTools/Source/C/VfrCompile/Pccts/dlg/DlgMS.mak
+++ b/BaseTools/Source/C/VfrCompile/Pccts/dlg/DlgMS.mak
@@ -17,7 +17,14 @@ SET=$(PCCTS_HOME)\support\set
 CC = cl
 CFLAGS = /nologo -I "." -I "$(PCCTS_H)" -I "$(SET)" -D "USER_ZZSYN" -D "PC" \
         -D "ZZLEXBUFSIZE=65536"  /D "LONGFILENAMES" /W3 /Zi \
-        /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE 
+        /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE
+
+!IF "$(CLANG_BUILD)"=="TRUE"
+CC = "$(CLANG_BIN)\clang-cl.exe"
+CFLAGS = /nologo -I "." -I "$(PCCTS_H)" -I "$(SET)" -D "USER_ZZSYN" -D "PC" \
+        -D "ZZLEXBUFSIZE=65536"  /D "LONGFILENAMES" /W3 /Zi \
+        /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D PCCTS_USE_STDARG /w -m32
+!ENDIF
 
 DLG_OBJS = dlg_p.obj dlg_a.obj main.obj err.obj support.obj \
            output.obj relabel.obj automata.obj
diff --git a/BaseTools/Source/C/VfrCompile/Pccts/dlg/makefile b/BaseTools/Source/C/VfrCompile/Pccts/dlg/makefile
index 5a3561edec..8e6616a5b4 100644
--- a/BaseTools/Source/C/VfrCompile/Pccts/dlg/makefile
+++ b/BaseTools/Source/C/VfrCompile/Pccts/dlg/makefile
@@ -122,7 +122,14 @@ endif
 COPT=-O
 ANTLR=${BIN_DIR}/antlr
 DLG=${BIN_DIR}/dlg
+
+ifeq ($(OS),Windows_NT)
+BUILD_CFLAGS= $(COPT) -I. -I$(SET) -I$(PCCTS_H) -DUSER_ZZSYN -DZZLEXBUFSIZE=65536 \
+        -D_CRT_SECURE_NO_DEPRECATE -D_CRT_NONSTDC_NO_DEPRECATE -DPCCTS_USE_STDARG -m32
+else
 BUILD_CFLAGS= $(COPT) -I. -I$(SET) -I$(PCCTS_H) -DUSER_ZZSYN -DZZLEXBUFSIZE=65536
+endif
+
 BUILD_CPPFLAGS=
 OBJ_EXT=o
 OUT_OBJ = -o
diff --git a/BaseTools/Source/C/VolInfo/GNUmakefile b/BaseTools/Source/C/VolInfo/GNUmakefile
index 9795b28add..c80f1d5e51 100644
--- a/BaseTools/Source/C/VolInfo/GNUmakefile
+++ b/BaseTools/Source/C/VolInfo/GNUmakefile
@@ -6,7 +6,11 @@
 #
 MAKEROOT ?= ..
 
-APPNAME = VolInfo
+ifeq ($(OS),Windows_NT)
+  APPNAME = VolInfo.exe
+else
+  APPNAME = VolInfo
+endif
 
 OBJECTS = VolInfo.o
 
diff --git a/BaseTools/Tests/GNUmakefile b/BaseTools/Tests/GNUmakefile
index 1cb77f84b1..870f1d73a8 100644
--- a/BaseTools/Tests/GNUmakefile
+++ b/BaseTools/Tests/GNUmakefile
@@ -7,9 +7,14 @@
 
 all: test
 
-test:
-	@if command -v $(PYTHON_COMMAND) >/dev/null 1; then $(PYTHON_COMMAND) RunTests.py; else python RunTests.py; fi
-
-clean:
-	find . -name '*.pyc' -exec rm '{}' ';'
-
+ifeq ($(OS),Windows_NT)
+  test:
+		python RunTests.py
+  clean:
+		echo ToDo
+else
+  test:
+		@if command -v $(PYTHON_COMMAND) >/dev/null 1; then $(PYTHON_COMMAND) RunTests.py; else python RunTests.py; fi
+  clean:
+		find . -name '*.pyc' -exec rm '{}' ';'
+endif
\ No newline at end of file
diff --git a/BaseTools/Tests/TestTools.py b/BaseTools/Tests/TestTools.py
index 1099fd4eea..5cad2b0150 100644
--- a/BaseTools/Tests/TestTools.py
+++ b/BaseTools/Tests/TestTools.py
@@ -135,7 +135,7 @@ class BaseToolsTest(unittest.TestCase):
         return open(os.path.join(self.testDir, fileName), mode)
 
     def ReadTmpFile(self, fileName):
-        f = open(self.GetTmpFilePath(fileName), 'r')
+        f = open(self.GetTmpFilePath(fileName), 'r', encoding="utf8")
         data = f.read()
         f.close()
         return data
diff --git a/BaseTools/Tests/TianoCompress.py b/BaseTools/Tests/TianoCompress.py
index 685968b18f..2b592a9106 100644
--- a/BaseTools/Tests/TianoCompress.py
+++ b/BaseTools/Tests/TianoCompress.py
@@ -21,7 +21,10 @@ class Tests(TestTools.BaseToolsTest):
 
     def setUp(self):
         TestTools.BaseToolsTest.setUp(self)
-        self.toolName = 'TianoCompress'
+        if sys.platform in ('win32', 'win64'):
+            self.toolName = 'TianoCompress.exe'
+        else:
+            self.toolName = 'TianoCompress'
 
     def testHelp(self):
         result = self.RunTool('--help', logFile='help')
diff --git a/BaseTools/toolsetup.bat b/BaseTools/toolsetup.bat
index 58fd26a4b5..cacd41d3e0 100755
--- a/BaseTools/toolsetup.bat
+++ b/BaseTools/toolsetup.bat
@@ -72,6 +72,11 @@ if /I "%1"=="/?" goto Usage
     set VSTool=VS2012
     goto loop
   )
+  if /I "%1"=="clang" (
+    shift
+    set CLANG_BUILD=TRUE
+    goto loop
+  )
   if "%1"=="" goto setup_workspace
   if exist %1 (
     if not defined BASE_TOOLS_PATH (
@@ -195,6 +200,11 @@ if defined VS2019 (
 ) else if defined VS2012 (
   call %EDK_TOOLS_PATH%\set_vsprefix_envs.bat VS2012
   call %EDK_TOOLS_PATH%\get_vsvars.bat VS2012
+) else if defined CLANG_BUILD (
+    @if exist "C:\Program Files\LLVM\bin\clang.exe" (
+        @set "CLANG_BIN=C:\Program Files\LLVM\bin\"
+        @echo   Found LLVM, setting CLANG_BIN environment variable to C:\Program Files\LLVM\bin\
+    )
 ) else (
   call %EDK_TOOLS_PATH%\set_vsprefix_envs.bat
   call %EDK_TOOLS_PATH%\get_vsvars.bat
@@ -472,7 +482,7 @@ goto end
 
 :Usage
   @echo.
-  echo  Usage: "%0 [-h | -help | --help | /h | /help | /?] [ Rebuild | ForceRebuild ] [Reconfig] [base_tools_path [edk_tools_path]] [VS2019] [VS2017] [VS2015] [VS2013] [VS2012]"
+  echo  Usage: "%0 [-h | -help | --help | /h | /help | /?] [ Rebuild | ForceRebuild ] [Reconfig] [base_tools_path [edk_tools_path]] [VS2019] [VS2017] [VS2015] [VS2013] [VS2012] [clang]"
   @echo.
   @echo         base_tools_path   BaseTools project path, BASE_TOOLS_PATH will be set to this path.
   @echo         edk_tools_path    EDK_TOOLS_PATH will be set to this path.
@@ -486,6 +496,7 @@ goto end
   @echo         VS2015            Set the env for VS2015 build.
   @echo         VS2017            Set the env for VS2017 build.
   @echo         VS2019            Set the env for VS2019 build.
+  @echo         clang             Set the env for clang-cl build.
   @echo.
 
 :end
@@ -498,5 +509,6 @@ set VS2015=
 set VS2013=
 set VS2012=
 set VSTool=
+set CLANG_BUILD=
 popd
 
diff --git a/edksetup.bat b/edksetup.bat
index 7ad137bb3e..537d1f7733 100755
--- a/edksetup.bat
+++ b/edksetup.bat
@@ -150,11 +150,12 @@ if /I "%1"=="VS2017" shift
 if /I "%1"=="VS2015" shift
 if /I "%1"=="VS2013" shift
 if /I "%1"=="VS2012" shift
+if /I "%1"=="clang" shift
 if "%1"=="" goto end
 
 :Usage
   @echo.
-  @echo  Usage: "%0 [-h | -help | --help | /h | /help | /?] [Reconfig] [Rebuild] [ForceRebuild] [VS2019] [VS2017] [VS2015] [VS2013] [VS2012]"
+  @echo  Usage: "%0 [-h | -help | --help | /h | /help | /?] [Reconfig] [Rebuild] [ForceRebuild] [VS2019] [VS2017] [VS2015] [VS2013] [VS2012] [clang]"
   @echo.
   @echo         Reconfig       Reinstall target.txt, tools_def.txt and build_rule.txt.
   @echo         Rebuild        Perform incremental rebuild of BaseTools binaries.
@@ -164,6 +165,10 @@ if "%1"=="" goto end
   @echo         VS2015         Set the env for VS2015 build.
   @echo         VS2017         Set the env for VS2017 build.
   @echo         VS2019         Set the env for VS2019 build.
+  @echo         clang          Set the env for clang-cl build.
+  @echo.
+  @echo  Unix-like Usage: "gnumake.exe -C BaseTools CXX=llvm"
+  @echo         Use clang + gunmake build base tools in windows.
   @echo.
   @echo  Note that target.template, tools_def.template and build_rules.template
   @echo  will only be copied to target.txt, tools_def.txt and build_rule.txt
-- 
2.28.0

