Skip to content

Commit bc8d5c4

Browse files
committed
feat: generate ghcversion.h on the fly
1 parent 5be899d commit bc8d5c4

File tree

5 files changed

+39
-45
lines changed

5 files changed

+39
-45
lines changed

Build.hs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,6 @@ buildBootLibraries cabal ghc ghcpkg derive_constants genapply genprimop opts dst
381381
-- Build the RTS
382382
src_rts <- makeAbsolute (src </> "libraries/rts")
383383
build_dir <- makeAbsolute (dst </> "cabal")
384-
ghcversionh <- makeAbsolute (src_rts </> "include/ghcversion.h")
385384

386385
let cabal_project_rts_path = dst </> "cabal.project-rts"
387386
-- cabal's code handling escaping is bonkers. We need to wrap the whole
@@ -449,7 +448,6 @@ buildBootLibraries cabal ghc ghcpkg derive_constants genapply genprimop opts dst
449448
, "rts"
450449
, "--with-compiler=" ++ ghcPath ghc
451450
, "--with-hc-pkg=" ++ ghcPkgPath ghcpkg
452-
, "--ghc-options=\"-ghcversion-file=" ++ ghcversionh ++ "\""
453451
, "--builddir=" ++ build_dir
454452
]
455453

@@ -635,7 +633,6 @@ buildBootLibraries cabal ghc ghcpkg derive_constants genapply genprimop opts dst
635633
, "--project-file=" ++ cabal_project_bootlibs_path
636634
, "--with-compiler=" ++ ghcPath ghc
637635
, "--with-hc-pkg=" ++ ghcPkgPath ghcpkg
638-
, "--ghc-options=\"-ghcversion-file=" ++ ghcversionh ++ "\""
639636
, "--builddir=" ++ build_dir
640637
, "-j"
641638

compiler/GHC/Driver/Pipeline/Execute.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,7 @@ runCcPhase cc_phase pipe_env hsc_env location input_fn = do
478478
-- very weakly typed, being derived from C--.
479479
["-fno-strict-aliasing"]
480480

481-
include_ghcVersionH <- getGhcVersionIncludeFlags dflags unit_env
481+
include_ghcVersionH <- getGhcVersionIncludeFlags dflags logger tmpfs
482482

483483
withAtomicRename output_fn $ \temp_outputFilename ->
484484
GHC.SysTools.runCc (phaseForeignLanguage cc_phase) logger tmpfs dflags (

compiler/GHC/SysTools/Cpp.hs

Lines changed: 38 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
module GHC.SysTools.Cpp
77
( doCpp
88
, CppOpts(..)
9-
, getGhcVersionPathName
109
, getGhcVersionIncludeFlags
1110
, applyCDefs
1211
, offsetIncludePaths
@@ -39,6 +38,7 @@ import Control.Monad
3938

4039
import System.Directory
4140
import System.FilePath
41+
import GHC.Settings.Config (cProjectVersionInt, cProjectPatchLevel1, cProjectVersion, cProjectPatchLevel2)
4242

4343
data CppOpts = CppOpts
4444
{ sourceCodePreprocessor :: !SourceCodePreprocessor
@@ -125,10 +125,10 @@ doCpp logger tmpfs dflags unit_env opts input_fn output_fn = do
125125
[homeUnitEnv_dflags . ue_findHomeUnitEnv uid $ unit_env | uid <- ue_transitiveHomeDeps (ue_currentUnit unit_env) unit_env]
126126
dep_pkg_extra_inputs = [offsetIncludePaths fs (includePaths fs) | fs <- home_pkg_deps]
127127

128-
let include_paths_global = foldr (\ x xs -> ("-I" ++ x) : xs) []
128+
let include_paths_global = map ("-I" ++)
129129
(includePathsGlobal cmdline_include_paths ++ pkg_include_dirs
130130
++ concatMap includePathsGlobal dep_pkg_extra_inputs)
131-
let include_paths_quote = foldr (\ x xs -> ("-iquote" ++ x) : xs) []
131+
let include_paths_quote = map ("-iquote" ++)
132132
(includePathsQuote cmdline_include_paths ++
133133
includePathsQuoteImplicit cmdline_include_paths)
134134
let include_paths = include_paths_quote ++ include_paths_global
@@ -176,7 +176,7 @@ doCpp logger tmpfs dflags unit_env opts input_fn output_fn = do
176176
let asserts_def = [ "-D__GLASGOW_HASKELL_ASSERTS_IGNORED__" | gopt Opt_IgnoreAsserts dflags]
177177

178178
-- Default CPP defines in Haskell source
179-
hsSourceCppOpts <- getGhcVersionIncludeFlags dflags unit_env
179+
hsSourceCppOpts <- getGhcVersionIncludeFlags dflags logger tmpfs
180180

181181
-- MIN_VERSION macros
182182
let uids = explicitUnits unit_state
@@ -262,40 +262,41 @@ generateMacros prefix name version =
262262
_ -> error "take3"
263263
(major1,major2,minor) = take3 $ map show (versionBranch version) ++ repeat "0"
264264

265-
getGhcVersionIncludeFlags :: DynFlags -> UnitEnv -> IO [String]
266-
getGhcVersionIncludeFlags dflags unit_env = do
267-
mghcversionh <- getGhcVersionPathName dflags unit_env
268-
pure $ case mghcversionh of
269-
Nothing -> []
270-
Just p -> ["-include", p]
271-
272-
-- | Find out path to @ghcversion.h@ file
273-
getGhcVersionPathName :: DynFlags -> UnitEnv -> IO (Maybe FilePath)
274-
getGhcVersionPathName dflags unit_env = do
265+
getGhcVersionIncludeFlags :: DynFlags -> Logger -> TmpFs -> IO [String]
266+
getGhcVersionIncludeFlags dflags logger tmpfs = do
275267
case ghcVersionFile dflags of
276-
-- the user has provided an explicit `ghcversion.h` file to use.
277-
Just path -> doesFileExist path >>= \case
278-
True -> pure (Just path)
279-
False -> throwGhcExceptionIO (InstallationError
280-
("ghcversion.h missing; tried user-supplied path: " ++ path))
281-
-- otherwise, try to find it in the rts' include-dirs.
282-
-- Note: only in the RTS include-dirs! not all preload units less we may
283-
-- use a wrong file. See #25106 where a globally installed
284-
-- /usr/include/ghcversion.h file was used instead of the one provided
285-
-- by the rts.
286-
Nothing -> case lookupUnitId (ue_homeUnitState unit_env) rtsUnitId of
287-
Nothing -> do
288-
-- print warning and return nothing
289-
putStrLn "Couldn't find ghcversion.h file: no rts unit available and -ghcversion-file flag not passed"
290-
pure Nothing
291-
Just info -> do
292-
let candidates = (</> "ghcversion.h") <$> collectIncludeDirs [info]
293-
found <- filterM doesFileExist candidates
294-
case found of
295-
[] -> throwGhcExceptionIO (InstallationError
296-
("ghcversion.h missing; tried: "
297-
++ intercalate ", " candidates))
298-
(x:_) -> return (Just x)
268+
-- the user has provided an explicit `ghcversion.h` file to use.
269+
Just path -> do
270+
found <- doesFileExist path
271+
unless found $
272+
throwGhcExceptionIO (InstallationError ("ghcversion.h missing; tried: " ++ path))
273+
return ["-include", path]
274+
Nothing -> do
275+
macro_stub <- newTempName logger tmpfs (tmpDir dflags) TFL_CurrentModule "h"
276+
writeFile macro_stub ghcVersionH
277+
return ["-include", macro_stub]
278+
279+
-- ---------------------------------------------------------------------------
280+
-- ghcversion.h
281+
282+
ghcVersionH :: String
283+
ghcVersionH =
284+
concat
285+
["#define __GLASGOW_HASKELL__ ", show cProjectVersionInt, "\n"
286+
,"#define __GLASGOW_HASKELL_FULL_VERSION__ ", show cProjectVersion, "\n"
287+
,"\n"
288+
,"#define __GLASGOW_HASKELL_PATCHLEVEL1__ ", show cProjectPatchLevel1, "\n"
289+
,"#define __GLASGOW_HASKELL_PATCHLEVEL2__ ", show cProjectPatchLevel2, "\n"
290+
,"\n"
291+
,"#define MIN_VERSION_GLASGOW_HASKELL(ma,mi,pl1,pl2) ( \\\n"
292+
," ((ma)*100+(mi)) < __GLASGOW_HASKELL__ || \\\n"
293+
," ((ma)*100+(mi)) == __GLASGOW_HASKELL__ \\\n"
294+
," && (pl1) < __GLASGOW_HASKELL_PATCHLEVEL1__ || \\\n"
295+
," ((ma)*100+(mi)) == __GLASGOW_HASKELL__ \\\n"
296+
," && (pl1) == __GLASGOW_HASKELL_PATCHLEVEL1__ \\\n"
297+
," && (pl2) <= __GLASGOW_HASKELL_PATCHLEVEL2__ )\n"
298+
,"\n\n"
299+
]
299300

300301
applyCDefs :: DefunctionalizedCDefs -> Logger -> DynFlags -> IO [String]
301302
applyCDefs NoCDefs _ _ = return []

rts/configure.ac

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ AC_DEFUN([GHC_INFO_FIELD],[
3030
])
3131

3232
AC_CONFIG_FILES([include/ghcplatform.h])
33-
AC_CONFIG_FILES([include/ghcversion.h])
3433
AC_CONFIG_HEADERS([include/ghcautoconf.h])
3534

3635
AC_ARG_WITH([compiler],

rts/rts.cabal

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ extra-source-files:
1616
config.guess
1717
config.sub
1818
include/ghcplatform.h.in
19-
include/ghcversion.h.in
2019
configure.ac
2120
external-symbols.list.in
2221
rts.buildinfo.in
@@ -345,13 +344,11 @@ library
345344
autogen-includes:
346345
ghcautoconf.h
347346
ghcplatform.h
348-
ghcversion.h
349347

350348
install-includes:
351349
ghcautoconf.h
352350
ghcconfig.h
353351
ghcplatform.h
354-
ghcversion.h
355352

356353
HsFFI.h
357354
Rts.h

0 commit comments

Comments
 (0)