From b95b63806b5a9764902d3ae556ba31d17ecd1ec3 Mon Sep 17 00:00:00 2001
From: John Ericson <git@JohnEricson.me>
Date: Sun, 7 Apr 2019 22:33:21 -0400
Subject: [PATCH] Make stage 1 GHC target independent

Now that the target macros are not being used, we remove them. This
prevents target hardcoding regressions.
---
 compiler/ghc.mk                    | 16 +---------------
 hadrian/src/Rules/Generate.hs      | 24 ++++--------------------
 includes/ghc.mk                    |  8 --------
 libraries/ghc-prim/GHC/Prim/Ext.hs |  4 ++--
 4 files changed, 7 insertions(+), 45 deletions(-)

diff --git a/compiler/ghc.mk b/compiler/ghc.mk
index f80d7b75dfd1..629a1ed56073 100644
--- a/compiler/ghc.mk
+++ b/compiler/ghc.mk
@@ -101,28 +101,21 @@ compiler/stage1/$(PLATFORM_H) : mk/config.mk mk/project.mk | $$(dir $$@)/.
 	@echo                                                     >> $@
 	@echo "#define $(BuildPlatform_CPP)_BUILD 1"              >> $@
 	@echo "#define $(HostPlatform_CPP)_HOST 1"                >> $@
-	@echo "#define $(TargetPlatform_CPP)_TARGET 1"            >> $@
 	@echo                                                     >> $@
 	@echo "#define $(BuildArch_CPP)_BUILD_ARCH 1"             >> $@
 	@echo "#define $(HostArch_CPP)_HOST_ARCH 1"               >> $@
-	@echo "#define $(TargetArch_CPP)_TARGET_ARCH 1"           >> $@
 	@echo "#define BUILD_ARCH \"$(BuildArch_CPP)\""           >> $@
 	@echo "#define HOST_ARCH \"$(HostArch_CPP)\""             >> $@
-	@echo "#define TARGET_ARCH \"$(TargetArch_CPP)\""         >> $@
 	@echo                                                     >> $@
 	@echo "#define $(BuildOS_CPP)_BUILD_OS 1"                 >> $@
 	@echo "#define $(HostOS_CPP)_HOST_OS 1"                   >> $@
-	@echo "#define $(TargetOS_CPP)_TARGET_OS 1"               >> $@
 	@echo "#define BUILD_OS \"$(BuildOS_CPP)\""               >> $@
 	@echo "#define HOST_OS \"$(HostOS_CPP)\""                 >> $@
-	@echo "#define TARGET_OS \"$(TargetOS_CPP)\""             >> $@
 	@echo                                                     >> $@
 	@echo "#define $(BuildVendor_CPP)_BUILD_VENDOR 1"         >> $@
 	@echo "#define $(HostVendor_CPP)_HOST_VENDOR 1"           >> $@
-	@echo "#define $(TargetVendor_CPP)_TARGET_VENDOR  1"      >> $@
 	@echo "#define BUILD_VENDOR \"$(BuildVendor_CPP)\""       >> $@
 	@echo "#define HOST_VENDOR \"$(HostVendor_CPP)\""         >> $@
-	@echo "#define TARGET_VENDOR \"$(TargetVendor_CPP)\""     >> $@
 	@echo                                                     >> $@
 	@echo "#endif /* __PLATFORM_H__ */"                       >> $@
 	@echo "Done."
@@ -141,28 +134,21 @@ compiler/stage2/$(PLATFORM_H) : mk/config.mk mk/project.mk | $$(dir $$@)/.
 	@echo                                                     >> $@
 	@echo "#define $(HostPlatform_CPP)_BUILD 1"               >> $@
 	@echo "#define $(TargetPlatform_CPP)_HOST 1"              >> $@
-	@echo "#define $(TargetPlatform_CPP)_TARGET 1"            >> $@
 	@echo                                                     >> $@
 	@echo "#define $(HostArch_CPP)_BUILD_ARCH 1"              >> $@
 	@echo "#define $(TargetArch_CPP)_HOST_ARCH 1"             >> $@
-	@echo "#define $(TargetArch_CPP)_TARGET_ARCH 1"           >> $@
 	@echo "#define BUILD_ARCH \"$(HostArch_CPP)\""            >> $@
 	@echo "#define HOST_ARCH \"$(TargetArch_CPP)\""           >> $@
-	@echo "#define TARGET_ARCH \"$(TargetArch_CPP)\""         >> $@
 	@echo                                                     >> $@
 	@echo "#define $(HostOS_CPP)_BUILD_OS 1"                  >> $@
 	@echo "#define $(TargetOS_CPP)_HOST_OS 1"                 >> $@
-	@echo "#define $(TargetOS_CPP)_TARGET_OS 1"               >> $@
 	@echo "#define BUILD_OS \"$(HostOS_CPP)\""                >> $@
 	@echo "#define HOST_OS \"$(TargetOS_CPP)\""               >> $@
-	@echo "#define TARGET_OS \"$(TargetOS_CPP)\""             >> $@
 	@echo                                                     >> $@
 	@echo "#define $(HostVendor_CPP)_BUILD_VENDOR 1"          >> $@
 	@echo "#define $(TargetVendor_CPP)_HOST_VENDOR 1"         >> $@
-	@echo "#define $(TargetVendor_CPP)_TARGET_VENDOR  1"      >> $@
 	@echo "#define BUILD_VENDOR \"$(HostVendor_CPP)\""        >> $@
 	@echo "#define HOST_VENDOR \"$(TargetVendor_CPP)\""       >> $@
-	@echo "#define TARGET_VENDOR \"$(TargetVendor_CPP)\""     >> $@
 	@echo                                                     >> $@
 	@echo "#endif /* __PLATFORM_H__ */"                       >> $@
 	@echo "Done."
@@ -204,7 +190,7 @@ compiler_HC_OPTS += $(addprefix -I,$(GHC_INCLUDE_DIRS))
 
 define preprocessCompilerFiles
 # $0 = stage
-compiler/stage$1/build/primops.txt: compiler/prelude/primops.txt.pp compiler/stage$1/$$(PLATFORM_H)
+compiler/stage$1/build/primops.txt: compiler/prelude/primops.txt.pp
 	$$(HS_CPP) -P $$(compiler_CPP_OPTS) -Icompiler/stage$1 -x c $$< | grep -v '^#pragma GCC' > $$@
 
 compiler/stage$1/build/primop-data-decl.hs-incl: compiler/stage$1/build/primops.txt $$$$(genprimopcode_INPLACE)
diff --git a/hadrian/src/Rules/Generate.hs b/hadrian/src/Rules/Generate.hs
index 7a584130f5b7..b14bccc09c1b 100644
--- a/hadrian/src/Rules/Generate.hs
+++ b/hadrian/src/Rules/Generate.hs
@@ -258,18 +258,13 @@ generateGhcPlatformH = do
         , "#define BUILD_VENDOR " ++ show hostVendor
         , "#define HOST_VENDOR "  ++ show targetVendor
         , ""
-        , "/* These TARGET macros are for backwards compatibility... DO NOT USE! */"
-        , "#define TargetPlatform_TYPE " ++ cppify targetPlatform
-        , "#define " ++ cppify targetPlatform ++ "_TARGET 1"
-        , "#define " ++ targetArch ++ "_TARGET_ARCH 1"
-        , "#define TARGET_ARCH " ++ show targetArch
-        , "#define " ++ targetOs ++ "_TARGET_OS 1"
-        , "#define TARGET_OS " ++ show targetOs
-        , "#define " ++ targetVendor ++ "_TARGET_VENDOR 1" ]
+        ]
         ++
         [ "#define UnregisterisedCompiler 1" | ghcUnreg ]
         ++
-        [ "\n#endif /* __GHCPLATFORM_H__ */" ]
+        [ ""
+        , "#endif /* __GHCPLATFORM_H__ */"
+        ]
 
 generateSettings :: Expr String
 generateSettings = do
@@ -413,10 +408,6 @@ generateGhcBootPlatformH = do
     hostArch       <- chooseSetting HostArch      TargetArch
     hostOs         <- chooseSetting HostOs        TargetOs
     hostVendor     <- chooseSetting HostVendor    TargetVendor
-    targetPlatform <- getSetting TargetPlatform
-    targetArch     <- getSetting TargetArch
-    targetOs       <- getSetting TargetOs
-    targetVendor   <- getSetting TargetVendor
     return $ unlines
         [ "#if !defined(__PLATFORM_H__)"
         , "#define __PLATFORM_H__"
@@ -426,28 +417,21 @@ generateGhcBootPlatformH = do
         , ""
         , "#define " ++ cppify buildPlatform  ++ "_BUILD 1"
         , "#define " ++ cppify hostPlatform   ++ "_HOST 1"
-        , "#define " ++ cppify targetPlatform ++ "_TARGET 1"
         , ""
         , "#define " ++ buildArch  ++ "_BUILD_ARCH 1"
         , "#define " ++ hostArch   ++ "_HOST_ARCH 1"
-        , "#define " ++ targetArch ++ "_TARGET_ARCH 1"
         , "#define BUILD_ARCH "  ++ show buildArch
         , "#define HOST_ARCH "   ++ show hostArch
-        , "#define TARGET_ARCH " ++ show targetArch
         , ""
         , "#define " ++ buildOs  ++ "_BUILD_OS 1"
         , "#define " ++ hostOs   ++ "_HOST_OS 1"
-        , "#define " ++ targetOs ++ "_TARGET_OS 1"
         , "#define BUILD_OS "  ++ show buildOs
         , "#define HOST_OS "   ++ show hostOs
-        , "#define TARGET_OS " ++ show targetOs
         , ""
         , "#define " ++ buildVendor  ++ "_BUILD_VENDOR 1"
         , "#define " ++ hostVendor   ++ "_HOST_VENDOR 1"
-        , "#define " ++ targetVendor ++ "_TARGET_VENDOR  1"
         , "#define BUILD_VENDOR "  ++ show buildVendor
         , "#define HOST_VENDOR "   ++ show hostVendor
-        , "#define TARGET_VENDOR " ++ show targetVendor
         , ""
         , "#endif /* __PLATFORM_H__ */" ]
 
diff --git a/includes/ghc.mk b/includes/ghc.mk
index ccbd8abbc5fb..7ad63ba8ef7a 100644
--- a/includes/ghc.mk
+++ b/includes/ghc.mk
@@ -149,14 +149,6 @@ $(includes_H_PLATFORM) : includes/Makefile | $$(dir $$@)/.
 	@echo "#define BUILD_VENDOR  \"$(HostVendor_CPP)\"" >> $@
 	@echo "#define HOST_VENDOR  \"$(TargetVendor_CPP)\"" >> $@
 	@echo >> $@
-	@echo "/* These TARGET macros are for backwards compatibility... DO NOT USE! */" >> $@
-	@echo "#define TargetPlatform_TYPE $(TargetPlatform_CPP)" >> $@
-	@echo "#define $(TargetPlatform_CPP)_TARGET  1" >> $@
-	@echo "#define $(TargetArch_CPP)_TARGET_ARCH  1" >> $@
-	@echo "#define TARGET_ARCH  \"$(TargetArch_CPP)\"" >> $@
-	@echo "#define $(TargetOS_CPP)_TARGET_OS  1" >> $@  
-	@echo "#define TARGET_OS  \"$(TargetOS_CPP)\"" >> $@
-	@echo "#define $(TargetVendor_CPP)_TARGET_VENDOR  1" >> $@
 ifeq "$(GhcUnregisterised)" "YES"
 	@echo "#define UnregisterisedCompiler 1" >> $@
 endif
diff --git a/libraries/ghc-prim/GHC/Prim/Ext.hs b/libraries/ghc-prim/GHC/Prim/Ext.hs
index 402d5725c8ee..b9f644ef1edf 100644
--- a/libraries/ghc-prim/GHC/Prim/Ext.hs
+++ b/libraries/ghc-prim/GHC/Prim/Ext.hs
@@ -30,7 +30,7 @@ module GHC.Prim.Ext
     INT64
   , WORD64
   -- * Delay\/wait operations
-#if defined(mingw32_TARGET_OS)
+#if defined(mingw32_HOST_OS)
   , asyncRead#
   , asyncWrite#
   , asyncDoProc#
@@ -66,7 +66,7 @@ type WORD64 =
 -- Delay/wait operations
 ------------------------------------------------------------------------
 
-#if defined(mingw32_TARGET_OS)
+#if defined(mingw32_HOST_OS)
 
 -- | Asynchronously read bytes from specified file descriptor.
 foreign import prim "stg_asyncReadzh" asyncRead#
-- 
GitLab