Skip to content

Added ability to import cmake CDB #146

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@ dist
pygmalion.sqlite
compile_commands.json
cabal-dev
.cabal-sandbox/
TAGS
cabal.sandbox.config
3 changes: 3 additions & 0 deletions pygmalion.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ executable pygmalion
async,
directory,
mtl,
aeson,
text,
bytestring,
process,
pygmalion,
safe,
Expand Down
12 changes: 12 additions & 0 deletions src/Pygmalion/Index/Command.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

module Pygmalion.Index.Command
( getCommandInfo
, mkCommandInfo
) where

import Control.Applicative
Expand All @@ -26,6 +27,17 @@ getCommandInfo cmd args = do
finalArgs
(inferLang args sf)

mkCommandInfo :: String -> String -> [String] -> Maybe CommandInfo
mkCommandInfo wd cmd args = mci
where finalArgs = map B.fromString . absArgs wd . filterArgs $ args
mci = find hasSourceExtension args >>=
absNormPath wd >>= \sf ->
return $ CommandInfo (mkSourceFile sf)
(B.fromString wd)
(B.fromString cmd)
finalArgs
(inferLang args sf)

inferLang :: [String] -> String -> Language
inferLang as f = fromMaybe UnknownLanguage $ inferLangFromArgs as <|>
(Just . extensionLanguage) f
Expand Down
45 changes: 37 additions & 8 deletions tools/Pygmalion.hs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
{-# LANGUAGE OverloadedStrings, ViewPatterns #-}
{-# LANGUAGE OverloadedStrings, ViewPatterns, ScopedTypeVariables #-}

import Control.Concurrent.Async
import Control.Exception (catch, SomeException)
import Control.Monad
import Control.Monad.Trans (liftIO)
import qualified Data.ByteString.UTF8 as B
import qualified Data.ByteString.Lazy as BS
import Data.List (intercalate, isPrefixOf, sortBy)
import Data.Maybe
import Safe (readMay)
Expand All @@ -14,6 +15,9 @@ import System.Exit
import System.IO (withFile, IOMode(ReadWriteMode))
import System.Path
import System.Process
import Data.Aeson
import qualified Data.Text as Text
import Control.Applicative

import Pygmalion.Config
import Pygmalion.Core
Expand Down Expand Up @@ -41,6 +45,7 @@ usage = do
putStrLn " stop-server Terminates the pygmalion daemon."
putStrLn " init Initializes a pygmalion index rooted at"
putStrLn " the current directory."
putStrLn " import-cdb [file] Import a cmake generated compilation database"
putStrLn " index ([file]|[command]) Manually request indexing. If a single"
putStrLn " argument is provided, it's interpreted"
putStrLn " as a file to index using the default"
Expand Down Expand Up @@ -84,6 +89,7 @@ parseConfiglessArgs _ c = c
parseArgs :: Config -> FilePath -> [String] -> IO ()
parseArgs _ _ ["start-server"] = startServer
parseArgs c _ ["stop-server"] = stopServer c
parseArgs c _ ("import-cdb" : file : []) = importCDB c file
parseArgs c wd ("index" : file : []) = indexFile c (asSourceFile wd file)
parseArgs c _ ("index" : cmd : args) = indexCommand c cmd args
parseArgs c _ ("make" : cmd : args) = makeCommand c cmd args
Expand Down Expand Up @@ -129,16 +135,39 @@ initialize = do
createDirectoryIfMissing True pygmalionDir
withFile configFile ReadWriteMode (\_ -> return ())

newtype CmdInfo = CmdInfo { unCmdInfo :: CommandInfo }

instance FromJSON CmdInfo where
parseJSON (Object v) = do
(cmd : args) <- (map Text.unpack . Text.splitOn " ") <$>
v .: "command"
wd <- v .: "directory"
maybe mzero (return . CmdInfo) $ mkCommandInfo wd cmd args
parseJSON _ = mzero

importCDB :: Config -> String -> IO ()
importCDB cf cdb = do
let complainAndExit :: SomeException -> IO (BS.ByteString)
complainAndExit _ = error $ "Cannot open file " ++ cdb
mes :: Maybe [CmdInfo] <- decode <$>
BS.readFile cdb `catch` complainAndExit
case mes of
Just es -> mapM_ (indexCommand_ cf . Just . unCmdInfo) es
Nothing -> return ()

indexCommand :: Config -> String -> [String] -> IO ()
indexCommand cf cmd args = do
mayCI <- getCommandInfo cmd args
case mayCI of
Just ci -> do let sf = ciSourceFile ci
mayMTime <- getMTime sf
case mayMTime of
Just mtime -> withRPC cf $ runRPC (rpcIndexCommand ci mtime)
Nothing -> putStrLn $ "Couldn't read file " ++ show sf
Nothing -> return () -- Couldn't parse command. Very likely a linker command.
indexCommand_ cf mayCI

indexCommand_ :: Config -> Maybe CommandInfo -> IO ()
indexCommand_ cf (Just ci) = do
let sf = ciSourceFile ci
mayMTime <- getMTime sf
case mayMTime of
Just mtime -> withRPC cf $ runRPC (rpcIndexCommand ci mtime)
Nothing -> putStrLn $ "Couldn't read file " ++ show sf
indexCommand_ _ _ = return ()

indexFile :: Config -> SourceFile -> IO ()
indexFile cf f = do
Expand Down