packages feed

shake-ats (empty) → 0.1.0.0

raw patch · 6 files changed

+231/−0 lines, 6 filesdep +basedep +directorydep +language-atssetup-changed

Dependencies added: base, directory, language-ats, shake, shake-ext, text

Files

+ LICENSE view
@@ -0,0 +1,11 @@+Copyright Vanessa McHale (c) 2018++Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:++1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.++2. 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.++3. Neither the name of the copyright holder 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 COPYRIGHT HOLDERS AND 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 COPYRIGHT HOLDER OR 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.
+ README.md view
@@ -0,0 +1,5 @@+# shake-ats++## Installation++## Configuration
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ shake-ats.cabal view
@@ -0,0 +1,41 @@+name:                shake-ats+version:             0.1.0.0+synopsis:            Utilities for building ATS projects with shake+description:         Various helper functions for building [ATS](http://www.ats-lang.org/) with the [shake](http://shakebuild.com/) library+homepage:            https://github.com/vmchale/shake-ats#readme+license:             BSD3+license-file:        LICENSE+author:              Vanessa McHale+maintainer:          vamchale@gmail.com+copyright:           Copyright: (c) 2018 Vanessa McHale+category:            Development, Build, ATS+build-type:          Simple+extra-doc-files:     README.md+extra-source-files:  stack.yaml+cabal-version:       1.18++Flag development {+  Description: Enable `-Werror`+  manual: True+  default: False+}++library+  hs-source-dirs:      src+  exposed-modules:     Development.Shake.ATS+  build-depends:       base >= 4.7 && < 5+                     , language-ats+                     , shake-ext+                     , directory+                     , text+                     , shake+  default-language:    Haskell2010+  if flag(development)+    ghc-options:       -Werror+  if impl(ghc >= 8.0)+    ghc-options:       -Wincomplete-uni-patterns -Wincomplete-record-updates -Wcompat+  ghc-options:         -Wall++source-repository head+  type:     darcs+  location: https://hub.darcs.net/vmchale/ats
+ src/Development/Shake/ATS.hs view
@@ -0,0 +1,163 @@+{-# LANGUAGE FlexibleInstances    #-}+{-# LANGUAGE ScopedTypeVariables  #-}+{-# LANGUAGE TypeSynonymInstances #-}++module Development.Shake.ATS ( -- * Shake Rules+                               cgen+                             , cgenPretty+                             , cleanATS+                             , atsBin+                             , atsLex+                             -- * Actions+                             , patsHome+                             -- Types+                             , Version (..)+                             ) where++import           Control.Monad+import           Control.Monad.IO.Class+import           Data.Either                (fromRight)+import           Data.Maybe                 (fromMaybe)+import           Data.Semigroup             (Semigroup (..))+import qualified Data.Text.Lazy             as TL+import           Development.Shake+import           Development.Shake.FilePath+import           Language.ATS+import           System.Directory           (copyFile, createDirectoryIfMissing)+import           System.Exit                (ExitCode (ExitSuccess))++newtype Version = Version [Integer]++instance Show Version where+    show (Version [])     = ""+    show (Version [x])    = show x+    show (Version (x:xs)) = show x ++ "." ++ show (Version xs)++pkgHome :: Action String+pkgHome = fromMaybe "/usr/local" <$> mh+    where mh = fmap (++ "/.atspkg/") <$> getEnv "HOME"++patsHome :: Version -> Action String+patsHome v = fmap (++ (show v ++ "/")) pkgHome++atsCommand :: CmdResult r => Version -> Version -> String -> String -> Action r+atsCommand v v' sourceFile out = do+    h <- patsHome v'+    let home = h ++ "lib/ats2-postiats-" ++ show v+    let atsArgs = [EchoStderr False, AddEnv "PATSHOME" home]+        patsc = "patsopt"+    command atsArgs patsc ["--output", out, "-dd", sourceFile, "-cc"]++gcFlag :: Bool -> String+gcFlag False = "-DATS_MEMALLOC_LIBC"+gcFlag True  = "-DATS_MEMALLOC_GCBDW"++-- Copy source files to the appropriate place. This is necessary because+-- @#include@s in ATS are fucked up.+copySources :: Version -> Version -> [FilePath] -> Action ()+copySources v v' sources =+    forM_ sources $ \dep -> do+        h <- patsHome v'+        let home = h ++ "lib/ats2-postiats-" ++ show v+        liftIO $ createDirectoryIfMissing True (home ++ "/" ++ takeDirectory dep)+        liftIO $ copyFile dep (home ++ "/" ++ dep)++-- TODO musl?+atsBin :: Version -- ^ Library version+       -> Version -- ^ Compiler version+       -> Bool -- ^ Whether to use the garbage collector+       -> [String] -- ^ A list of libraries against which to link+       -> String -- ^ Source file+       -> String -- ^ Binary target+       -> Rules ()+atsBin v v' gc libs sourceFile out =++    out %> \_ -> do+        sources <- transitiveDeps [sourceFile]+        h <- patsHome v'+        let home = h ++ "lib/ats2-postiats-" ++ show v+        need sources+        copySources v v' sources+        cmd_ ["mkdir", "-p", dropDirectory1 out]+        path <- fromMaybe "" <$> getEnv "PATH"+        let toLibs = fmap ("-l" <>)+        -- -D_GNU_SOURCE ???+        -- -latslib+        command+            [EchoStderr False, AddEnv "PATSHOME" home, AddEnv "PATH" (home ++ "/bin:" ++ path), AddEnv "PATSHOMELOCS" "./.atspkg/contrib"]+            (home ++ "/bin/patscc")+            ([sourceFile, "-atsccomp", "gcc -flto -I" ++ h ++ "/ccomp/runtime/ -I" ++ h, gcFlag gc, "-o", out, "-cleanaft", "-O2", "-mtune=native", "-flto"] <> toLibs libs)++-- | Build a @.lats@ file.+atsLex :: Rules ()+atsLex =+    "*.dats" %> \out -> do+        lats <- liftIO $ readFile (out -<.> "lats")+        (Stdout contents) <- command [Stdin lats] "atslex" []+        liftIO $ writeFile out contents++cleanATS :: Rules ()+cleanATS =++    "clean" ~> do+        removeFilesAfter "." ["//*.c", "//tags"]+        removeFilesAfter ".atspkg" ["//*"]+        removeFilesAfter "ats-deps" ["//*"]++handleSource :: Version -> Version -> FilePath -> Action ()+handleSource v v' sourceFile = do+        sources <- transitiveDeps [sourceFile]+        need sources+        copySources v v' sources++-- | This provides rules for generating C code from ATS source files in the+-- @ats-src@ directory.+cgen :: Version -- ^ Library version+     -> Version -- ^ Compiler version+     -> FilePath -- ^ Directory for the generated C code+     -> Rules ()+cgen v v' dir =++    "//*.c" %> \out -> do+        let sourceFile = dir ++ "/" ++ (dropDirectory1 out -<.> "dats")+        handleSource v v' sourceFile+        atsCommand v v' sourceFile out++fixDir :: FilePath -> String -> String+fixDir p =+      TL.unpack+    . TL.replace (TL.pack "./") (TL.pack $ p ++ "/")+    . TL.replace (TL.pack "../") (TL.pack $ joinPath (init $ splitPath p) ++ "/")+    . TL.replace (TL.pack "$PATSHOMELOCS") (TL.pack ".atspkg/contrib")+    . TL.replace (TL.pack "\\\n") mempty+    . TL.pack++trim :: String -> String+trim = init . drop 1++transitiveDeps :: [FilePath] -> Action [FilePath]+transitiveDeps [] = pure []+transitiveDeps ps = fmap join $ forM ps $ \p -> do+    contents <- liftIO $ readFile p+    let ats = fromRight (error p) . parseATS . lexATS $ contents+    let dir = takeDirectory p+    deps <- filterM doesFileExist $ fixDir dir . trim <$> getDependencies ats+    deps' <- transitiveDeps deps+    pure $ (p:deps) ++ deps'++-- | This uses @pats-filter@ to prettify the errors.+cgenPretty :: Version -- ^ Library version+           -> Version -- ^ Compiler version+           -> FilePath+           -> Rules ()+cgenPretty v v' dir =++    "//*.c" %> \out -> do++        let sourceFile = dir ++ "/" ++ (dropDirectory1 out -<.> "dats")+        handleSource v v' sourceFile+        (Exit c, Stderr err) :: (Exit, Stderr String) <- atsCommand v v' sourceFile out+        cmd_ [Stdin err] Shell "pats-filter"+        if c /= ExitSuccess+            then error "patscc failure"+            else pure ()
+ stack.yaml view
@@ -0,0 +1,9 @@+---+resolver: lts-10.3+packages:+  - '.'+extra-deps: []+flags:+  shake-ats:+    development: false+extra-package-dbs: []