Skip to content

Commit 73e713b

Browse files
committed
Reduce CPP usage as well #4272
1 parent f4a580f commit 73e713b

25 files changed

+141
-153
lines changed

package.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ dependencies:
9393
- project-template
9494
- regex-applicative-text
9595
- resourcet
96-
- retry
96+
- retry >= 0.7
9797
- rio
9898
- semigroups
9999
- split
@@ -258,6 +258,7 @@ library:
258258
- Stack.Upgrade
259259
- Stack.Upload
260260
- Text.PrettyPrint.Leijen.Extended
261+
- System.Permissions
261262
- System.Process.PagerEditor
262263
- System.Terminal
263264
when:

src/Data/Attoparsec/Interpreter.hs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
{-# LANGUAGE NoImplicitPrelude #-}
22
{-# LANGUAGE OverloadedStrings #-}
3-
{-# LANGUAGE CPP #-}
43
{- | This module implements parsing of additional arguments embedded in a
54
comment when stack is invoked as a script interpreter
65
@@ -145,11 +144,7 @@ getInterpreterArgs file = do
145144

146145
decodeError e =
147146
case e of
148-
#if MIN_VERSION_conduit_extra(1,2,0)
149147
ParseError ctxs _ (Position line col _) ->
150-
#else
151-
ParseError ctxs _ (Position line col) ->
152-
#endif
153148
if null ctxs
154149
then "Parse error"
155150
else ("Expecting " ++ intercalate " or " ctxs)

src/Network/HTTP/Download/Verified.hs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
{-# LANGUAGE NoImplicitPrelude #-}
2-
{-# LANGUAGE CPP #-}
32
{-# LANGUAGE DeriveDataTypeable #-}
43
{-# LANGUAGE MultiParamTypeClasses #-}
54
{-# LANGUAGE OverloadedStrings #-}
@@ -192,11 +191,7 @@ hashChecksToZipSink req = traverse_ (ZipSink . sinkCheckHash req)
192191
-- 'Control.Retry.recovering' customized for HTTP failures
193192
recoveringHttp :: forall env a. HasRunner env => RetryPolicy -> RIO env a -> RIO env a
194193
recoveringHttp retryPolicy =
195-
#if MIN_VERSION_retry(0,7,0)
196194
helper $ \run -> recovering retryPolicy (handlers run) . const
197-
#else
198-
helper $ \run -> recovering retryPolicy (handlers run)
199-
#endif
200195
where
201196
helper :: (UnliftIO (RIO env) -> IO a -> IO a) -> RIO env a -> RIO env a
202197
helper wrapper action = withUnliftIO $ \run -> wrapper run (unliftIO run action)

src/Stack/Build.hs

Lines changed: 6 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
{-# LANGUAGE NoImplicitPrelude #-}
2-
{-# LANGUAGE CPP #-}
32
{-# LANGUAGE ConstraintKinds #-}
43
{-# LANGUAGE DeriveDataTypeable #-}
54
{-# LANGUAGE FlexibleContexts #-}
@@ -46,16 +45,9 @@ import Stack.Types.Config
4645
import Stack.Types.NamedComponent
4746
import Stack.Types.Package
4847

49-
import Stack.Types.Compiler (compilerVersionText
50-
#ifdef WINDOWS
51-
,getGhcVersion
52-
#endif
53-
)
48+
import Stack.Types.Compiler (compilerVersionText, getGhcVersion)
5449
import System.FileLock (FileLock, unlockFile)
55-
56-
#ifdef WINDOWS
57-
import System.Win32.Console (setConsoleCP, setConsoleOutputCP, getConsoleCP, getConsoleOutputCP)
58-
#endif
50+
import System.Terminal (fixCodePage)
5951

6052
-- | Build.
6153
--
@@ -67,7 +59,10 @@ build :: HasEnvConfig env
6759
-> Maybe FileLock
6860
-> BuildOptsCLI
6961
-> RIO env ()
70-
build msetLocalFiles mbuildLk boptsCli = fixCodePage $ do
62+
build msetLocalFiles mbuildLk boptsCli = do
63+
mcp <- view $ configL.to configModifyCodePage
64+
ghcVersion <- view $ actualCompilerVersionL.to getGhcVersion
65+
fixCodePage mcp ghcVersion $ do
7166
bopts <- view buildOptsL
7267
let profiling = boptsLibProfile bopts || boptsExeProfile bopts
7368
let symbols = not (boptsLibStrip bopts || boptsExeStrip bopts)
@@ -282,53 +277,6 @@ loadPackage loc flags ghcOptions = do
282277
}
283278
resolvePackage pkgConfig <$> loadCabalFileImmutable loc
284279

285-
-- | Set the code page for this process as necessary. Only applies to Windows.
286-
-- See: https://github.com/commercialhaskell/stack/issues/738
287-
fixCodePage :: HasEnvConfig env => RIO env a -> RIO env a
288-
#ifdef WINDOWS
289-
fixCodePage inner = do
290-
mcp <- view $ configL.to configModifyCodePage
291-
ghcVersion <- view $ actualCompilerVersionL.to getGhcVersion
292-
if mcp && ghcVersion < mkVersion [7, 10, 3]
293-
then fixCodePage'
294-
-- GHC >=7.10.3 doesn't need this code page hack.
295-
else inner
296-
where
297-
fixCodePage' = do
298-
origCPI <- liftIO getConsoleCP
299-
origCPO <- liftIO getConsoleOutputCP
300-
301-
let setInput = origCPI /= expected
302-
setOutput = origCPO /= expected
303-
fixInput
304-
| setInput = bracket_
305-
(liftIO $ do
306-
setConsoleCP expected)
307-
(liftIO $ setConsoleCP origCPI)
308-
| otherwise = id
309-
fixOutput
310-
| setOutput = bracket_
311-
(liftIO $ do
312-
setConsoleOutputCP expected)
313-
(liftIO $ setConsoleOutputCP origCPO)
314-
| otherwise = id
315-
316-
case (setInput, setOutput) of
317-
(False, False) -> return ()
318-
(True, True) -> warn ""
319-
(True, False) -> warn " input"
320-
(False, True) -> warn " output"
321-
322-
fixInput $ fixOutput inner
323-
expected = 65001 -- UTF-8
324-
warn typ = logInfo $
325-
"Setting" <>
326-
typ <>
327-
" codepage to UTF-8 (65001) to ensure correct output from GHC"
328-
#else
329-
fixCodePage = id
330-
#endif
331-
332280
-- | Query information about the build and print the result to stdout in YAML format.
333281
queryBuildInfo :: HasEnvConfig env
334282
=> [Text] -- ^ selectors

src/Stack/Build/Cache.hs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
{-# LANGUAGE CPP #-}
21
{-# LANGUAGE NoImplicitPrelude #-}
32
{-# LANGUAGE DeriveGeneric #-}
43
{-# LANGUAGE MultiParamTypeClasses #-}
@@ -39,9 +38,7 @@ import qualified Data.ByteArray as Mem (convert)
3938
import qualified Data.ByteString.Base64.URL as B64URL
4039
import qualified Data.ByteString as B
4140
import qualified Data.ByteString.Char8 as S8
42-
#ifdef mingw32_HOST_OS
4341
import Data.Char (ord)
44-
#endif
4542
import qualified Data.Map as M
4643
import qualified Data.Set as Set
4744
import qualified Data.Store as Store
@@ -359,8 +356,9 @@ readPrecompiledCache loc copts depIDs = do
359356

360357
-- | Check if a filesystem path is too long.
361358
pathTooLong :: FilePath -> Bool
362-
#ifdef mingw32_HOST_OS
363-
pathTooLong path = utf16StringLength path >= win32MaxPath
359+
pathTooLong
360+
| osIsWindows = \path -> utf16StringLength path >= win32MaxPath
361+
| otherwise = const False
364362
where
365363
win32MaxPath = 260
366364
-- Calculate the length of a string in 16-bit units
@@ -370,6 +368,3 @@ pathTooLong path = utf16StringLength path >= win32MaxPath
370368
where
371369
utf16CharLength c | ord c < 0x10000 = 1
372370
| otherwise = 2
373-
#else
374-
pathTooLong _ = False
375-
#endif

src/Stack/Build/Execute.hs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
{-# LANGUAGE NoImplicitPrelude #-}
2-
{-# LANGUAGE CPP #-}
32
{-# LANGUAGE ConstraintKinds #-}
43
{-# LANGUAGE DataKinds #-}
54
{-# LANGUAGE FlexibleContexts #-}

src/Stack/Config.hs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
{-# LANGUAGE NoImplicitPrelude #-}
22
{-# LANGUAGE ConstraintKinds #-}
3-
{-# LANGUAGE CPP #-}
43
{-# LANGUAGE DataKinds #-}
54
{-# LANGUAGE DeriveFoldable #-}
65
{-# LANGUAGE DeriveFunctor #-}

src/Stack/Config/Docker.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{-# LANGUAGE NoImplicitPrelude #-}
22
{-# LANGUAGE OverloadedStrings #-}
3-
{-# LANGUAGE CPP, DeriveDataTypeable, RecordWildCards #-}
3+
{-# LANGUAGE DeriveDataTypeable, RecordWildCards #-}
44

55
-- | Docker configuration
66
module Stack.Config.Docker where

src/Stack/Constants.hs

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
{-# LANGUAGE CPP #-}
21
{-# LANGUAGE NoImplicitPrelude #-}
32
{-# LANGUAGE OverloadedStrings #-}
43
{-# LANGUAGE TemplateHaskell #-} -- keep TH usage here
@@ -132,6 +131,7 @@ import Path as FL
132131
import Stack.Prelude
133132
import Stack.Types.Compiler
134133
import Stack.Types.TemplateName
134+
import System.Permissions (osIsWindows)
135135

136136
-- | Extensions used for Haskell modules. Excludes preprocessor ones.
137137
haskellFileExts :: [Text]
@@ -325,15 +325,6 @@ maxTerminalWidth = 200
325325
defaultTerminalWidth :: Int
326326
defaultTerminalWidth = 100
327327

328-
-- | True if using Windows OS.
329-
osIsWindows :: Bool
330-
osIsWindows =
331-
#ifdef WINDOWS
332-
True
333-
#else
334-
False
335-
#endif
336-
337328
relFileSetupHs :: Path Rel File
338329
relFileSetupHs = $(mkRelFile "Setup.hs")
339330

src/Stack/Docker.hs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ import System.IO.Unsafe (unsafePerformIO)
6666
import qualified System.PosixCompat.User as User
6767
import qualified System.PosixCompat.Files as Files
6868
import System.Process.PagerEditor (editByteString)
69+
import System.Terminal (hIsTerminalDeviceOrMinTTY)
6970
import RIO.Process
7071
import Text.Printf (printf)
7172

@@ -347,6 +348,8 @@ runContainerAndExit getCmdArgs
347348
,[cmnd]
348349
,args])
349350
before
351+
-- MSS 2018-08-30 can the CPP below be removed entirely, and instead exec the
352+
-- `docker` process so that it can handle the signals directly?
350353
#ifndef WINDOWS
351354
run <- askRunInIO
352355
oldHandlers <- forM [sigINT,sigABRT,sigHUP,sigPIPE,sigTERM,sigUSR1,sigUSR2] $ \sig -> do

src/Stack/FileWatch.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import GHC.IO.Exception
1414
import Path
1515
import System.FSNotify
1616
import System.IO (hPutStrLn, getLine)
17+
import System.Terminal
1718

1819
fileWatch :: Handle
1920
-> ((Set (Path Abs File) -> IO ()) -> IO ())

src/Stack/Ghci.hs

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
{-# LANGUAGE TupleSections #-}
55
{-# LANGUAGE DeriveDataTypeable #-}
66
{-# LANGUAGE RecordWildCards #-}
7-
{-# LANGUAGE CPP #-}
87
{-# LANGUAGE ConstraintKinds #-}
98

109
-- | Run a GHCi configured with the user's package(s).
@@ -50,10 +49,7 @@ import Stack.Types.Package
5049
import Stack.Types.Runner
5150
import System.IO (putStrLn)
5251
import System.IO.Temp (getCanonicalTemporaryDirectory)
53-
54-
#ifndef WINDOWS
55-
import qualified System.Posix.Files as Posix
56-
#endif
52+
import System.Permissions (setScriptPerms)
5753

5854
-- | Command-line options for GHC.
5955
data GhciOpts = GhciOpts
@@ -887,20 +883,6 @@ getExtraLoadDeps loadAllDeps sourceMap targets =
887883
(_, Just PSRemote{}) -> return loadAllDeps
888884
(_, _) -> return False
889885

890-
setScriptPerms :: MonadIO m => FilePath -> m ()
891-
#ifdef WINDOWS
892-
setScriptPerms _ = do
893-
return ()
894-
#else
895-
setScriptPerms fp = do
896-
liftIO $ Posix.setFileMode fp $ foldl1 Posix.unionFileModes
897-
[ Posix.ownerReadMode
898-
, Posix.ownerWriteMode
899-
, Posix.groupReadMode
900-
, Posix.otherReadMode
901-
]
902-
#endif
903-
904886
unionTargets :: Ord k => Map k Target -> Map k Target -> Map k Target
905887
unionTargets = M.unionWith $ \l r ->
906888
case (l, r) of

src/Stack/Prelude.hs

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
{-# LANGUAGE CPP #-}
21
{-# LANGUAGE NoImplicitPrelude #-}
32
{-# LANGUAGE OverloadedStrings #-}
43
{-# LANGUAGE ScopedTypeVariables #-}
@@ -11,7 +10,6 @@ module Stack.Prelude
1110
, readProcessNull
1211
, withProcessContext
1312
, stripCR
14-
, hIsTerminalDeviceOrMinTTY
1513
, prompt
1614
, promptPassword
1715
, promptBool
@@ -31,10 +29,6 @@ import qualified Path.IO
3129

3230
import System.IO.Echo (withoutInputEcho)
3331

34-
#ifdef WINDOWS
35-
import System.Win32 (isMinTTYHandle, withHandleToHANDLE)
36-
#endif
37-
3832
import qualified Data.Conduit.Binary as CB
3933
import qualified Data.Conduit.List as CL
4034
import Data.Conduit.Process.Typed (withLoggedProcess_, createSource)
@@ -127,19 +121,6 @@ withProcessContext pcNew inner = do
127121
stripCR :: Text -> Text
128122
stripCR = T.dropSuffix "\r"
129123

130-
-- | hIsTerminaDevice does not recognise handles to mintty terminals as terminal
131-
-- devices, but isMinTTYHandle does.
132-
hIsTerminalDeviceOrMinTTY :: MonadIO m => Handle -> m Bool
133-
#ifdef WINDOWS
134-
hIsTerminalDeviceOrMinTTY h = do
135-
isTD <- hIsTerminalDevice h
136-
if isTD
137-
then return True
138-
else liftIO $ withHandleToHANDLE h isMinTTYHandle
139-
#else
140-
hIsTerminalDeviceOrMinTTY = hIsTerminalDevice
141-
#endif
142-
143124
-- | Prompt the user by sending text to stdout, and taking a line of
144125
-- input from stdin.
145126
prompt :: MonadIO m => Text -> m Text

src/Stack/Script.hs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
{-# LANGUAGE NoImplicitPrelude #-}
2-
{-# LANGUAGE CPP #-}
32
{-# LANGUAGE OverloadedStrings #-}
43
{-# LANGUAGE TupleSections #-}
54
module Stack.Script

src/Stack/Setup.hs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ import System.Exit (ExitCode (..), exitFailure)
9595
import System.IO.Error (isPermissionError)
9696
import System.FilePath (searchPathSeparator)
9797
import qualified System.FilePath as FP
98+
import System.Permissions (setFileExecutable)
9899
import RIO.Process
99100
import Text.Printf (printf)
100101

@@ -103,7 +104,6 @@ import System.Uname (uname, release)
103104
import Data.List.Split (splitOn)
104105
import Foreign.C (throwErrnoIfMinus1_, peekCString)
105106
import Foreign.Marshal (alloca)
106-
import System.Posix.Files (setFileMode)
107107
#endif
108108

109109
-- | Default location of the stack-setup.yaml file
@@ -1899,9 +1899,7 @@ downloadStackExe platforms0 archiveInfo destDir checkPath testExe = do
18991899
platform <- view platformL
19001900

19011901
liftIO $ do
1902-
#if !WINDOWS
1903-
setFileMode (toFilePath tmpFile) 0o755
1904-
#endif
1902+
setFileExecutable (toFilePath tmpFile)
19051903

19061904
testExe tmpFile
19071905

@@ -1986,9 +1984,7 @@ performPathChecking newFile = do
19861984
tmpFile <- parseAbsFile $ executablePath ++ ".tmp"
19871985
eres <- tryIO $ do
19881986
liftIO $ copyFile newFile tmpFile
1989-
#if !WINDOWS
1990-
liftIO $ setFileMode (toFilePath tmpFile) 0o755
1991-
#endif
1987+
setFileExecutable (toFilePath tmpFile)
19921988
liftIO $ renameFile tmpFile executablePath'
19931989
logInfo "Stack executable copied successfully!"
19941990
case eres of

src/Stack/Sig/Sign.hs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
{-# LANGUAGE NoImplicitPrelude #-}
2-
{-# LANGUAGE CPP #-}
32
{-# LANGUAGE FlexibleContexts #-}
43
{-# LANGUAGE OverloadedStrings #-}
54
{-# LANGUAGE RankNTypes #-}

0 commit comments

Comments
 (0)