on master help sign in
Set environment with Idris flags while running `make`

When building a package with foreign code and a makefile, the makefile itself must find the compilation flags that Idris already knows about. Moreover, the `idris` executable found by the makefile may not be the same used to build the package. This change passes the build flags to the makefile though environment variables.

commit946c9e10d9f08d753a1957d696267bc9f95c1d41 authorAlex Queiroz <asandroq@gmail.com> dateThu 4 Jan 2018, 00:12 (9 years ago) parent97496b7
3 files changed+26−8
CHANGELOG.md+4−1 docs/reference/packages.rst+4−1 src/Idris/Package.hs+18−6
CHANGELOG.md+4−1
@@ -109,7 +109,10 @@
109109## Packaging Updates
110110
111111+ Package names now only accept a restrictive charset of letters, numbers and the `-_` characters.
112− Package names are also case insensitive
112+ Package names are also case insensitive.
113++ When building makefiles for the FFI, the environment variables
114+ `IDRIS_INCLUDES` and `IDRIS_LDFLAGS` are now set with the correct C
115+ flags.
113116
114117# New in 1.1.1
115118
docs/reference/packages.rst+4−1
@@ -91,7 +91,10 @@ external ``C`` libraries, the following options are available:
9191
9292+ ``makefile = <file>``, which specifies a ``Makefile``, to be built
9393 before the Idris modules, for example to support linking with a
94− ``C`` library.
94+ ``C`` library. When building, Idris sets the environment variables
95+ ``IDRIS_INCLUDES`` (with C include flags) and ``IDRIS_LDFLAGS``
96+ (with C linking flags) so they can be used from inside the
97+ ``Makefile``.
9598
9699+ ``libs = <libs>``, which takes a comma separated list of libraries
97100 which must be present for the package to be usable.
src/Idris/Package.hs+18−6
@@ -10,6 +10,7 @@ module Idris.Package where
1010
1111import System.Directory
1212import System.Directory (copyFile, createDirectoryIfMissing)
13+import System.Environment
1314import System.Exit
1415import System.FilePath (addExtension, addTrailingPathSeparator, dropExtension,
1516 hasExtension, takeDirectory, takeExtension,
@@ -437,17 +438,28 @@ inPkgDir pkgdesc action =
437438 return res
438439
439440-- ------------------------------------------------------- [ Makefile Commands ]
441+-- | Invoke a Makefile's target with an enriched system environment
442+makeTarget :: Maybe String -> Maybe String -> IO ()
443+makeTarget _ Nothing = return ()
444+makeTarget mtgt (Just s) = do incFlags <- intercalate " " <$> getIncFlags
445+ libFlags <- intercalate " " <$> getLibFlags
446+ newEnv <- (++ [("IDRIS_INCLUDES", incFlags),
447+ ("IDRIS_LDFLAGS", libFlags)]) <$> getEnvironment
448+ let cmdLine = case mtgt of
449+ Nothing -> "make -f " ++ s
450+ Just tgt -> "make -f " ++ s ++ " " ++ tgt
451+ (_, _, _, r) <- createProcess (shell cmdLine) { env = Just newEnv }
452+ waitForProcess r
453+ return ()
454+
455+
440456-- | Invoke a Makefile's default target.
441457make :: Maybe String -> IO ()
442−make Nothing = return ()
443−make (Just s) = do rawSystem "make" ["-f", s]
444− return ()
458+make = makeTarget Nothing
445459
446460-- | Invoke a Makefile's clean target.
447461clean :: Maybe String -> IO ()
448−clean Nothing = return ()
449−clean (Just s) = do rawSystem "make" ["-f", s, "clean"]
450− return ()
462+clean = makeTarget (Just "clean")
451463
452464-- | Merge an option list representing the command line options into
453465-- those specified for a package description.