summaryrefslogtreecommitdiff
path: root/libraries
diff options
context:
space:
mode:
authorSylvain Henry <sylvain@haskus.fr>2023-01-12 10:11:58 +0100
committerMarge Bot <ben+marge-bot@smart-cactus.org>2023-02-17 15:59:16 -0500
commita203ad854ffee802e6bf0aca26e6c9a99bec3865 (patch)
tree0340d9fa199490961e97ea24c6574077f37f0d7b /libraries
parentbe0b7209c6aef22798fc4ba7baacd2099b5cb494 (diff)
downloadhaskell-a203ad854ffee802e6bf0aca26e6c9a99bec3865.tar.gz
Merge libiserv with ghci
`libiserv` serves no purpose. As it depends on `ghci` and doesn't have more dependencies than the `ghci` package, its code could live in the `ghci` package too. This commit also moves most of the code from the `iserv` program into the `ghci` package as well so that it can be reused. This is especially useful for the implementation of TH for the JS backend (#22261, !9779).
Diffstat (limited to 'libraries')
-rw-r--r--libraries/ghci/GHCi/Server.hs (renamed from libraries/libiserv/src/IServ.hs)68
-rw-r--r--libraries/ghci/GHCi/Utils.hsc (renamed from libraries/libiserv/src/GHCi/Utils.hsc)31
-rw-r--r--libraries/ghci/ghci.cabal.in2
-rw-r--r--libraries/libiserv/.gitignore4
-rw-r--r--libraries/libiserv/LICENSE62
-rw-r--r--libraries/libiserv/Makefile15
-rw-r--r--libraries/libiserv/cbits/iservmain.c17
-rw-r--r--libraries/libiserv/libiserv.cabal.in37
8 files changed, 93 insertions, 143 deletions
diff --git a/libraries/libiserv/src/IServ.hs b/libraries/ghci/GHCi/Server.hs
index 6361a8c04c..f46060a01c 100644
--- a/libraries/libiserv/src/IServ.hs
+++ b/libraries/ghci/GHCi/Server.hs
@@ -1,17 +1,27 @@
-{-# LANGUAGE RankNTypes, RecordWildCards, GADTs, ScopedTypeVariables #-}
-module IServ (serv) where
+{-# LANGUAGE CPP, RankNTypes, RecordWildCards, GADTs, ScopedTypeVariables #-}
+module GHCi.Server
+ ( serv
+ , defaultServer
+ )
+where
+import Prelude
import GHCi.Run
import GHCi.TH
import GHCi.Message
+import GHCi.Signals
+import GHCi.Utils
import Control.DeepSeq
import Control.Exception
import Control.Monad
+import Control.Concurrent (threadDelay)
import Data.Binary
+import Data.IORef
import Text.Printf
-import System.Environment (getProgName)
+import System.Environment (getProgName, getArgs)
+import System.Exit
type MessageHook = Msg -> IO Msg
@@ -84,3 +94,55 @@ serv verbose hook pipe restore = loop
Left UserInterrupt -> return () >> discardCtrlC
Left e -> throwIO e
_ -> return ()
+
+-- | Default server
+defaultServer :: IO ()
+defaultServer = do
+ args <- getArgs
+ (outh, inh, rest) <-
+ case args of
+ arg0:arg1:rest -> do
+ inh <- readGhcHandle arg1
+ outh <- readGhcHandle arg0
+ return (outh, inh, rest)
+ _ -> dieWithUsage
+
+ (verbose, rest') <- case rest of
+ "-v":rest' -> return (True, rest')
+ _ -> return (False, rest)
+
+ (wait, rest'') <- case rest' of
+ "-wait":rest'' -> return (True, rest'')
+ _ -> return (False, rest')
+
+ unless (null rest'') $
+ dieWithUsage
+
+ when verbose $
+ printf "GHC iserv starting (in: %s; out: %s)\n" (show inh) (show outh)
+ installSignalHandlers
+ lo_ref <- newIORef Nothing
+ let pipe = Pipe{pipeRead = inh, pipeWrite = outh, pipeLeftovers = lo_ref}
+
+ when wait $ do
+ when verbose $
+ putStrLn "Waiting 3s"
+ threadDelay 3000000
+
+ uninterruptibleMask $ serv verbose hook pipe
+
+ where hook = return -- empty hook
+ -- we cannot allow any async exceptions while communicating, because
+ -- we will lose sync in the protocol, hence uninterruptibleMask.
+
+dieWithUsage :: IO a
+dieWithUsage = do
+ prog <- getProgName
+ die $ prog ++ ": " ++ msg
+ where
+#if defined(WINDOWS)
+ msg = "usage: iserv <write-handle> <read-handle> [-v]"
+#else
+ msg = "usage: iserv <write-fd> <read-fd> [-v]"
+#endif
+
diff --git a/libraries/libiserv/src/GHCi/Utils.hsc b/libraries/ghci/GHCi/Utils.hsc
index 6b6613ad1b..43ab4a8550 100644
--- a/libraries/libiserv/src/GHCi/Utils.hsc
+++ b/libraries/ghci/GHCi/Utils.hsc
@@ -1,12 +1,15 @@
{-# LANGUAGE CPP #-}
module GHCi.Utils
- ( getGhcHandle
- ) where
+ ( getGhcHandle
+ , readGhcHandle
+ )
+where
+import Prelude
import Foreign.C
import GHC.IO.Handle (Handle())
#if defined(mingw32_HOST_OS)
-import Foreign.Ptr (ptrToIntPtr)
+import Foreign.Ptr (ptrToIntPtr,wordPtrToPtr)
import GHC.IO (onException)
import GHC.IO.Handle.FD (fdToHandle)
import GHC.Windows (HANDLE)
@@ -16,12 +19,13 @@ import GHC.IO.Device as IODevice
import GHC.IO.Encoding (getLocaleEncoding)
import GHC.IO.IOMode
import GHC.IO.Windows.Handle (fromHANDLE, Io(), NativeHandle())
+
+#include <fcntl.h> /* for _O_BINARY */
+
#else
import System.Posix
#endif
-#include <fcntl.h> /* for _O_BINARY */
-
-- | Gets a GHC Handle File description from the given OS Handle or POSIX fd.
#if defined(mingw32_HOST_OS)
@@ -48,3 +52,20 @@ foreign import ccall "io.h _open_osfhandle" _open_osfhandle ::
getGhcHandle :: CInt -> IO Handle
getGhcHandle fd = fdToHandle $ Fd fd
#endif
+
+-- | Read a handle passed on the command-line and prepare it to be used with the IO manager
+readGhcHandle :: String -> IO Handle
+readGhcHandle s = do
+#if defined(mingw32_HOST_OS)
+ let fd = wordPtrToPtr (Prelude.read s)
+# if defined(__IO_MANAGER_WINIO__)
+ -- register the handles we received with
+ -- our I/O manager otherwise we can't use
+ -- them correctly.
+ return () <!> associateHandle' fd
+# endif
+#else
+ let fd = Prelude.read s
+#endif
+ getGhcHandle fd
+
diff --git a/libraries/ghci/ghci.cabal.in b/libraries/ghci/ghci.cabal.in
index 78466a814a..700b7d62ea 100644
--- a/libraries/ghci/ghci.cabal.in
+++ b/libraries/ghci/ghci.cabal.in
@@ -57,6 +57,7 @@ library
GHCi.Signals
GHCi.StaticPtrTable
GHCi.TH
+ GHCi.Server
exposed-modules:
GHCi.BreakArray
@@ -66,6 +67,7 @@ library
GHCi.RemoteTypes
GHCi.FFI
GHCi.TH.Binary
+ GHCi.Utils
Build-Depends:
rts,
diff --git a/libraries/libiserv/.gitignore b/libraries/libiserv/.gitignore
deleted file mode 100644
index 89cf73d0b3..0000000000
--- a/libraries/libiserv/.gitignore
+++ /dev/null
@@ -1,4 +0,0 @@
-GNUmakefile
-/dist-install/
-/dist/
-ghc.mk
diff --git a/libraries/libiserv/LICENSE b/libraries/libiserv/LICENSE
deleted file mode 100644
index fe00a83ea9..0000000000
--- a/libraries/libiserv/LICENSE
+++ /dev/null
@@ -1,62 +0,0 @@
-This library (libraries/ghc-prim) is derived from code from several
-sources:
-
- * Code from the GHC project which is largely (c) The University of
- Glasgow, and distributable under a BSD-style license (see below),
-
- * Code from the Haskell 98 Report which is (c) Simon Peyton Jones
- and freely redistributable (but see the full license for
- restrictions).
-
-The full text of these licenses is reproduced below. All of the
-licenses are BSD-style or compatible.
-
------------------------------------------------------------------------------
-
-The Glasgow Haskell Compiler License
-
-Copyright 2004, The University Court of the University of Glasgow.
-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 name of the University nor the names of its contributors may be
-used to endorse or promote products derived from this software without
-specific prior written permission.
-
-THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY COURT OF THE UNIVERSITY OF
-GLASGOW AND THE 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
-UNIVERSITY COURT OF THE UNIVERSITY OF GLASGOW OR THE 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.
-
------------------------------------------------------------------------------
-
-Code derived from the document "Report on the Programming Language
-Haskell 98", is distributed under the following license:
-
- Copyright (c) 2002 Simon Peyton Jones
-
- The authors intend this Report to belong to the entire Haskell
- community, and so we grant permission to copy and distribute it for
- any purpose, provided that it is reproduced in its entirety,
- including this Notice. Modified versions of this Report may also be
- copied and distributed for any purpose, provided that the modified
- version is clearly presented as such, and that it does not claim to
- be a definition of the Haskell 98 Language.
-
diff --git a/libraries/libiserv/Makefile b/libraries/libiserv/Makefile
deleted file mode 100644
index 88656e7853..0000000000
--- a/libraries/libiserv/Makefile
+++ /dev/null
@@ -1,15 +0,0 @@
-# -----------------------------------------------------------------------------
-#
-# (c) 2009 The University of Glasgow
-#
-# This file is part of the GHC build system.
-#
-# To understand how the build system works and how to modify it, see
-# https://gitlab.haskell.org/ghc/ghc/wikis/building/architecture
-# https://gitlab.haskell.org/ghc/ghc/wikis/building/modifying
-#
-# -----------------------------------------------------------------------------
-
-dir = iserv
-TOP = ..
-include $(TOP)/mk/sub-makefile.mk
diff --git a/libraries/libiserv/cbits/iservmain.c b/libraries/libiserv/cbits/iservmain.c
deleted file mode 100644
index 5c88018d6b..0000000000
--- a/libraries/libiserv/cbits/iservmain.c
+++ /dev/null
@@ -1,17 +0,0 @@
-#include <rts/PosixSource.h>
-#include <Rts.h>
-
-#include <HsFFI.h>
-
-int main (int argc, char *argv[])
-{
- RtsConfig conf = defaultRtsConfig;
-
- // We never know what symbols GHC will look up in the future, so
- // we must retain CAFs for running interpreted code.
- conf.keep_cafs = 1;
-
- conf.rts_opts_enabled = RtsOptsAll;
- extern StgClosure ZCMain_main_closure;
- hs_main(argc, argv, &ZCMain_main_closure, conf);
-}
diff --git a/libraries/libiserv/libiserv.cabal.in b/libraries/libiserv/libiserv.cabal.in
deleted file mode 100644
index 6f13a3da42..0000000000
--- a/libraries/libiserv/libiserv.cabal.in
+++ /dev/null
@@ -1,37 +0,0 @@
--- WARNING: libiserv.cabal is automatically generated from libiserv.cabal.in by
--- ../../configure. Make sure you are editing libiserv.cabal.in, not
--- libiserv.cabal.
-
-Name: libiserv
-Version: @ProjectVersionMunged@
-Copyright: XXX
-License: BSD3
-License-File: LICENSE
-Author: XXX
-Maintainer: XXX
-Synopsis: Provides shared functionality between iserv and iserv-proxy.
-Description: Provides shared functionality between iserv and iserv-proxy.
-Category: Development
-build-type: Simple
-cabal-version: >=1.10
-
-Flag network
- Description: Build libiserv with over-the-network support
- Default: False
-
-Library
- Default-Language: Haskell2010
- Hs-Source-Dirs: src
- Exposed-Modules: IServ
- , GHCi.Utils
- Build-Depends: base >= 4 && < 5,
- binary >= 0.7 && < 0.11,
- bytestring >= 0.10 && < 0.12,
- containers >= 0.5 && < 0.7,
- deepseq >= 1.4 && < 1.5,
- ghci == @ProjectVersionMunged@
-
- if os(windows)
- Cpp-Options: -DWINDOWS
- else
- Build-Depends: unix >= 2.7 && < 2.9