on master help sign in
Merge pull request #2079 from melted/toolchain

Implement bundled toolchain feature

commit2e919c86d015f0b3e31eb56a9914585046558b4a authorDavid Christiansen <david@davidchristiansen.dk> dateThu 2 Apr 2015, 20:19 (12 years ago) parent4359edd2ad80e8
8 files changed+78−9
CHANGELOG+1 Setup.hs+23−6 codegen/idris-c/Main.hs+4−1 docs/faq/faq.rst+15 idris.cabal+1 main/Main.hs+2 src/Util/System.hs+30−2 win-release.sh+2
CHANGELOG+1
@@ -1,5 +1,6 @@
11New in 0.9.18:
22--------------
3+* Add support for bundled toolchains.
34* Strings are now UTF8 encoded in the default back end
45* Idris source files are now assumed to be in UTF8, regardless of locale
56 settings.
Setup.hs+23−6
@@ -137,17 +137,33 @@ generateTargetModule verbosity dir targetDir = do
137137 ++ " dir <- getDataDir\n"
138138 ++ " return (dir ++ \"/\" ++ name)"
139139
140+-- a module that has info about existence and location of a bundled toolchain
141+generateToolchainModule verbosity srcDir toolDir = do
142+ let commonContent = "module Tools_idris where\n\n"
143+ let toolContent = case toolDir of
144+ Just dir -> "hasBundledToolchain = True\n" ++
145+ "getToolchainDir = \"" ++ dir ++ "\"\n"
146+ Nothing -> "hasBundledToolchain = False\n" ++
147+ "getToolchainDir = \"\""
148+ let toolPath = srcDir </> "Tools_idris" Px.<.> "hs"
149+ createDirectoryIfMissingVerbose verbosity True srcDir
150+ rewriteFile toolPath (commonContent ++ toolContent)
140151
141152idrisConfigure _ flags _ local = do
142− configureRTS
143− generateVersionModule verbosity (autogenModulesDir local) (isRelease (configFlags local))
144− when (isFreestanding $ configFlags local) (do
145− targetDir <- lookupEnv "IDRIS_INSTALL_DIR"
153+ configureRTS
154+ generateVersionModule verbosity (autogenModulesDir local) (isRelease (configFlags local))
155+ if (isFreestanding $ configFlags local)
156+ then (do
157+ toolDir <- lookupEnv "IDRIS_TOOLCHAIN_DIR"
158+ generateToolchainModule verbosity (autogenModulesDir local) toolDir
159+ targetDir <- lookupEnv "IDRIS_LIB_DIR"
146160 case targetDir of
147161 Just d -> generateTargetModule verbosity (autogenModulesDir local) d
148162 Nothing -> error $ "Trying to build freestanding without a target directory."
149− ++ " Set it by defining IDRIS_INSTALL_DIR.")
150− where
163+ ++ " Set it by defining IDRIS_LIB_DIR.")
164+ else
165+ generateToolchainModule verbosity (autogenModulesDir local) Nothing
166+ where
151167 verbosity = S.fromFlag $ S.configVerbosity flags
152168 version = pkgVersion . package $ localPkgDescr local
153169
@@ -162,6 +178,7 @@ idrisPreSDist args flags = do
162178 let verb = S.fromFlag (S.sDistVerbosity flags)
163179 generateVersionModule verb ("src") True
164180 generateTargetModule verb "src" "./libs"
181+ generateToolchainModule verb "src" Nothing
165182 preSDist simpleUserHooks args flags
166183
167184idrisPostSDist args flags desc lbi = do
codegen/idris-c/Main.hs+4−1
@@ -14,6 +14,8 @@ import Control.Monad
1414
1515import Paths_idris
1616
17+import Util.System
18+
1719data Opts = Opts { inputs :: [FilePath],
1820 interface :: Bool,
1921 output :: FilePath }
@@ -31,7 +33,8 @@ getOpts = do xs <- getArgs
3133 process opts [] = opts
3234
3335c_main :: Opts -> Idris ()
34−c_main opts = do elabPrims
36+c_main opts = do runIO setupBundledCC
37+ elabPrims
3538 loadInputs (inputs opts) Nothing
3639 mainProg <- if interface opts
3740 then liftM Just elabMain
docs/faq/faq.rst+15
@@ -131,6 +131,21 @@ choice by the developers of Idris, Idris uses the C style convention.
131131That is, the names ``Float`` and ``Double`` are used to describe
132132single and double precision numbers.
133133
134+What is -ffreestanding?
135+=======================
136+
137+The freestanding flag is used to build Idris binaries which have their
138+libs and compiler in a relative path. This is useful for building binaries
139+where the install directory is unknown at build time. When passing this
140+flag, the IDRIS_LIB_DIR environment variable needs to be set to the path
141+where the Idris libs reside relative to the idris executable. The
142+IDRIS_TOOLCHAIN_DIR environment variable is optional, if that is set,
143+Idris will use that path to find the C compiler.
144+
145+Example:
146+.. code-block::
147+ IDRIS_LIB_DIR="./libs" IDRIS_TOOLCHAIN_DIR="./mingw/bin" CABALFLAGS="-fffi -ffreestanding -frelease" make
148+
134149What does the name ‘Idris’ mean?
135150================================
136151
idris.cabal+1
@@ -806,6 +806,7 @@ Library
806806 -- Auto Generated
807807 , Paths_idris
808808 , Version_idris
809+ , Tools_idris
809810
810811 Build-depends: base >=4 && <5
811812 , annotated-wl-pprint >= 0.5.3 && < 0.6
main/Main.hs+2
@@ -29,6 +29,7 @@ import Idris.CmdOptions
2929import IRTS.System ( getLibFlags, getIdrisLibDir, getIncFlags )
3030
3131import Util.DynamicLinker
32+import Util.System
3233
3334import Pkg.Package
3435
@@ -43,6 +44,7 @@ main = do opts <- runArgParser
4344
4445runIdris :: [Opt] -> Idris ()
4546runIdris opts = do
47+ runIO setupBundledCC
4648 when (ShowIncs `elem` opts) $ runIO showIncs
4749 when (ShowLibs `elem` opts) $ runIO showLibs
4850 when (ShowLibdir `elem` opts) $ runIO showLibdir
src/Util/System.hs+30−2
@@ -1,5 +1,6 @@
1+{-# LANGUAGE CPP #-}
12module Util.System(tempfile,withTempdir,rmFile,catchIO, isWindows,
2− readSource, writeSource) where
3+ writeSource, readSource, setupBundledCC) where
34
45-- System helper functions.
56import Control.Monad (when)
@@ -7,13 +8,19 @@ import System.Directory (getTemporaryDirectory
78 , removeFile
89 , removeDirectoryRecursive
910 , createDirectoryIfMissing
11+ , doesDirectoryExist
1012 )
11−import System.FilePath ((</>), normalise)
13+import System.FilePath ((</>), normalise, isAbsolute, dropFileName)
1214import System.IO
1315import System.Info
1416import System.IO.Error
1517import Control.Exception as CE
1618
19+#ifdef FREESTANDING
20+import Tools_idris
21+import System.Environment (getEnv, setEnv, getExecutablePath)
22+#endif
23+
1724catchIO :: IO a -> (IOError -> IO a) -> IO a
1825catchIO = CE.catch
1926
@@ -54,3 +61,24 @@ rmFile f = do putStrLn $ "Removing " ++ f
5461 catchIO (removeFile f)
5562 (\ioerr -> putStrLn $ "WARNING: Cannot remove file "
5663 ++ f ++ ", Error msg:" ++ show ioerr)
64+
65+setupBundledCC :: IO()
66+#ifdef FREESTANDING
67+setupBundledCC = when hasBundledToolchain
68+ $ do
69+ exePath <- getExecutablePath
70+ path <- getEnv "PATH"
71+ tcDir <- return getToolchainDir
72+ absolute <- return $ isAbsolute tcDir
73+ target <- return $
74+ if absolute
75+ then tcDir
76+ else dropFileName exePath ++ tcDir
77+ let pathSep = if isWindows then ";" else ":"
78+ present <- doesDirectoryExist target
79+ when present
80+ $ do newPath <- return $ target ++ pathSep ++ path
81+ setEnv "PATH" newPath
82+#else
83+setupBundledCC = return ()
84+#endif
win-release.shnew file+2
@@ -0,0 +1,2 @@
1+
2+IDRIS_LIB_DIR="./libs" IDRIS_TOOLCHAIN_DIR="./mingw/bin" CABALFLAGS="-fffi -ffreestanding -frelease" make