From ddcdd88c2c95445a87ee028f215d1e876939a4d9 Mon Sep 17 00:00:00 2001
From: Rodrigo Mesquita <rodrigo.m.mesquita@gmail.com>
Date: Tue, 20 Jun 2023 20:31:29 +0100
Subject: [PATCH] Split GHC.Platform.ArchOS from ghc-boot into ghc-platform

Split off the `GHC.Platform.ArchOS` module from the `ghc-boot` package
into this reinstallable standalone package which abides by the PVP, in
part motivated by the ongoing work on `ghc-toolchain` towards runtime
retargetability.
---
 hadrian/src/Packages.hs                       |  7 +++--
 hadrian/src/Rules/ToolArgs.hs                 |  1 +
 hadrian/src/Settings/Default.hs               |  1 +
 libraries/ghc-boot/ghc-boot.cabal.in          |  6 +++-
 libraries/ghc-platform/CHANGELOG.md           |  7 +++++
 libraries/ghc-platform/LICENSE                | 30 +++++++++++++++++++
 libraries/ghc-platform/ghc-platform.cabal     | 20 +++++++++++++
 .../src}/GHC/Platform/ArchOS.hs               | 12 ++++----
 8 files changed, 73 insertions(+), 11 deletions(-)
 create mode 100644 libraries/ghc-platform/CHANGELOG.md
 create mode 100644 libraries/ghc-platform/LICENSE
 create mode 100644 libraries/ghc-platform/ghc-platform.cabal
 rename libraries/{ghc-boot => ghc-platform/src}/GHC/Platform/ArchOS.hs (94%)

diff --git a/hadrian/src/Packages.hs b/hadrian/src/Packages.hs
index 056b050a7a01..dcb1e4cfc994 100644
--- a/hadrian/src/Packages.hs
+++ b/hadrian/src/Packages.hs
@@ -4,7 +4,7 @@ module Packages (
     array, base, binary, bytestring, cabal, cabalSyntax, checkPpr,
     checkExact, countDeps,
     compareSizes, compiler, containers, deepseq, deriveConstants, directory, dumpDecls,
-    exceptions, filepath, genapply, genprimopcode, ghc, ghcBignum, ghcBoot, ghcBootTh,
+    exceptions, filepath, genapply, genprimopcode, ghc, ghcBignum, ghcBoot, ghcBootTh, ghcPlatform,
     ghcCompact, ghcConfig, ghcHeap, ghci, ghciWrapper, ghcPkg, ghcPrim, haddock, haskeline,
     hsc2hs, hp2ps, hpc, hpcBin, integerGmp, integerSimple, iserv, iservProxy,
     libffi, mtl, parsec, pretty, primitive, process, remoteIserv, rts,
@@ -36,7 +36,7 @@ ghcPackages :: [Package]
 ghcPackages =
     [ array, base, binary, bytestring, cabalSyntax, cabal, checkPpr, checkExact, countDeps
     , compareSizes, compiler, containers, deepseq, deriveConstants, directory, dumpDecls
-    , exceptions, filepath, genapply, genprimopcode, ghc, ghcBignum, ghcBoot, ghcBootTh
+    , exceptions, filepath, genapply, genprimopcode, ghc, ghcBignum, ghcBoot, ghcBootTh, ghcPlatform
     , ghcCompact, ghcConfig, ghcHeap, ghci, ghciWrapper, ghcPkg, ghcPrim, haddock, haskeline, hsc2hs
     , hp2ps, hpc, hpcBin, integerGmp, integerSimple, iserv, libffi, mtl
     , parsec, pretty, process, rts, runGhc, stm, semaphoreCompat, templateHaskell
@@ -52,7 +52,7 @@ isGhcPackage = (`elem` ghcPackages)
 -- | Package definitions, see 'Package'.
 array, base, binary, bytestring, cabalSyntax, cabal, checkPpr, checkExact, countDeps,
   compareSizes, compiler, containers, deepseq, deriveConstants, directory, dumpDecls,
-  exceptions, filepath, genapply, genprimopcode, ghc, ghcBignum, ghcBoot, ghcBootTh,
+  exceptions, filepath, genapply, genprimopcode, ghc, ghcBignum, ghcBoot, ghcBootTh, ghcPlatform,
   ghcCompact, ghcConfig, ghcHeap, ghci, ghciWrapper, ghcPkg, ghcPrim, haddock, haskeline, hsc2hs,
   hp2ps, hpc, hpcBin, integerGmp, integerSimple, iserv, iservProxy, remoteIserv, libffi, mtl,
   parsec, pretty, primitive, process, rts, runGhc, semaphoreCompat, stm, templateHaskell,
@@ -84,6 +84,7 @@ ghc                 = prg  "ghc-bin"         `setPath` "ghc"
 ghcBignum           = lib  "ghc-bignum"
 ghcBoot             = lib  "ghc-boot"
 ghcBootTh           = lib  "ghc-boot-th"
+ghcPlatform         = lib  "ghc-platform"
 ghcCompact          = lib  "ghc-compact"
 ghcConfig           = prg  "ghc-config"      `setPath` "testsuite/ghc-config"
 ghcHeap             = lib  "ghc-heap"
diff --git a/hadrian/src/Rules/ToolArgs.hs b/hadrian/src/Rules/ToolArgs.hs
index 31e136db82a7..11a15bf035bf 100644
--- a/hadrian/src/Rules/ToolArgs.hs
+++ b/hadrian/src/Rules/ToolArgs.hs
@@ -158,6 +158,7 @@ toolTargets = [ binary
               -- , runGhc  -- # depends on ghc library
               , ghcBoot
               , ghcBootTh
+              , ghcPlatform
               , ghcHeap
               , ghci
               , ghcPkg  -- # executable
diff --git a/hadrian/src/Settings/Default.hs b/hadrian/src/Settings/Default.hs
index a67b9f98efdc..baa00344552c 100644
--- a/hadrian/src/Settings/Default.hs
+++ b/hadrian/src/Settings/Default.hs
@@ -85,6 +85,7 @@ stage0Packages = do
              , runGhc
              , ghcBoot
              , ghcBootTh
+             , ghcPlatform
              , ghcHeap
              , ghci
              , ghcPkg
diff --git a/libraries/ghc-boot/ghc-boot.cabal.in b/libraries/ghc-boot/ghc-boot.cabal.in
index 990f7d3adf73..7a4d8d3b80ee 100644
--- a/libraries/ghc-boot/ghc-boot.cabal.in
+++ b/libraries/ghc-boot/ghc-boot.cabal.in
@@ -51,7 +51,6 @@ Library
             GHC.Serialized
             GHC.ForeignSrcLang
             GHC.HandleEncoding
-            GHC.Platform.ArchOS
             GHC.Platform.Host
             GHC.Settings.Utils
             GHC.UniqueSubdir
@@ -65,6 +64,10 @@ Library
             , GHC.ForeignSrcLang.Type
             , GHC.Lexeme
 
+    -- reexport platform modules from ghc-platform
+    reexported-modules:
+              GHC.Platform.ArchOS
+
     -- but done by Hadrian
     autogen-modules:
             GHC.Version
@@ -77,6 +80,7 @@ Library
                    directory  >= 1.2 && < 1.4,
                    filepath   >= 1.3 && < 1.5,
                    deepseq    >= 1.4 && < 1.5,
+                   ghc-platform >= 0.1,
                    ghc-boot-th == @ProjectVersionMunged@
     if !os(windows)
         build-depends:
diff --git a/libraries/ghc-platform/CHANGELOG.md b/libraries/ghc-platform/CHANGELOG.md
new file mode 100644
index 000000000000..3b68d0c1046a
--- /dev/null
+++ b/libraries/ghc-platform/CHANGELOG.md
@@ -0,0 +1,7 @@
+# Revision history for ghc-platform
+
+## 0.1.0.0 -- 2023-06-20
+
+* First version. Split off the `GHC.Platform.ArchOS` module from the `ghc-boot`
+    package into this reinstallable standalone package which abides by the PVP,
+    in part motivated by the ongoing work on `ghc-toolchain` towards runtime retargetability.
diff --git a/libraries/ghc-platform/LICENSE b/libraries/ghc-platform/LICENSE
new file mode 100644
index 000000000000..fa64e6584d4a
--- /dev/null
+++ b/libraries/ghc-platform/LICENSE
@@ -0,0 +1,30 @@
+Copyright (c) 2023, Rodrigo Mesquita
+
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+    * Redistributions of source code must retain the above copyright
+      notice, this list of conditions and the following disclaimer.
+
+    * Redistributions in binary form must reproduce the above
+      copyright notice, this list of conditions and the following
+      disclaimer in the documentation and/or other materials provided
+      with the distribution.
+
+    * Neither the name of Rodrigo Mesquita nor the names of other
+      contributors may be used to endorse or promote products derived
+      from this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
diff --git a/libraries/ghc-platform/ghc-platform.cabal b/libraries/ghc-platform/ghc-platform.cabal
new file mode 100644
index 000000000000..84caf429e453
--- /dev/null
+++ b/libraries/ghc-platform/ghc-platform.cabal
@@ -0,0 +1,20 @@
+cabal-version:      3.0
+name:               ghc-platform
+version:            0.1.0.0
+synopsis:           Platform information used by GHC and friends
+license:            BSD-3-Clause
+license-file:       LICENSE
+author:             Rodrigo Mesquita
+maintainer:         ghc-devs@haskell.org
+build-type:         Simple
+extra-doc-files:    CHANGELOG.md
+
+common warnings
+    ghc-options: -Wall
+
+library
+    import:           warnings
+    exposed-modules:  GHC.Platform.ArchOS
+    build-depends:    base >=4.15.0.0
+    hs-source-dirs:   src
+    default-language: Haskell2010
diff --git a/libraries/ghc-boot/GHC/Platform/ArchOS.hs b/libraries/ghc-platform/src/GHC/Platform/ArchOS.hs
similarity index 94%
rename from libraries/ghc-boot/GHC/Platform/ArchOS.hs
rename to libraries/ghc-platform/src/GHC/Platform/ArchOS.hs
index 4aa42baa3b84..39edf792a524 100644
--- a/libraries/ghc-boot/GHC/Platform/ArchOS.hs
+++ b/libraries/ghc-platform/src/GHC/Platform/ArchOS.hs
@@ -1,17 +1,19 @@
 {-# LANGUAGE LambdaCase, ScopedTypeVariables #-}
 
 -- | Platform architecture and OS
---
--- We need it in ghc-boot because ghc-pkg needs it.
 module GHC.Platform.ArchOS
    ( ArchOS(..)
+
+     -- * Architectures
    , Arch(..)
-   , OS(..)
    , ArmISA(..)
    , ArmISAExt(..)
    , ArmABI(..)
    , PPC_64ABI(..)
    , stringEncodeArch
+
+     -- * Operating systems
+   , OS(..)
    , stringEncodeOS
    )
 where
@@ -27,10 +29,6 @@ data ArchOS
    deriving (Read, Show, Eq, Ord)
 
 -- | Architectures
---
--- TODO: It might be nice to extend these constructors with information about
--- what instruction set extensions an architecture might support.
---
 data Arch
    = ArchUnknown
    | ArchX86
-- 
GitLab