Skip to content

Commit 458671f

Browse files
committed
[cxx indexer facebookincubator#3] Various small improvements
* Add `--batch=N` to dump indexer output every N files (uses `-dump_every` flag from the previous PR) * Add `--cmake-opt=OPT` to pass options to cmake * Add `--out=DIR` to store indexer output files in `DIR` * Add `--skip-indexing` to avoid running the indexer and produce a DB from files that were generated previously
1 parent 67f7914 commit 458671f

1 file changed

Lines changed: 39 additions & 8 deletions

File tree

  • glean/lang/clang/Glean/Indexer

glean/lang/clang/Glean/Indexer/Cpp.hs

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ import Control.Concurrent.Async
1616
import Control.Exception
1717
import Control.Monad
1818
import qualified Data.ByteString as BS
19+
import Data.List
20+
import Data.Maybe
1921
import Data.Proxy
2022
import Options.Applicative
2123
import qualified System.Console.ANSI as ANSI
@@ -64,9 +66,13 @@ data Clang = Clang
6466
-- ^ (optional) path to pre-existing @compile_commands.json@
6567
, clangTarget :: Maybe String -- ^ (optional) target to index
6668
, clangJobs :: Int -- ^ number of indexers to run concurrently
69+
, clangBatch :: Int -- ^ dump indexer output every N files
6770
, clangVerbose :: Bool -- ^ display debugging information
6871
, clangProgress :: Bool -- ^ display indexing progress
72+
, clangCmakeOpts :: [String] -- ^ extra flags to cmake
6973
, clangIncremental :: Bool -- ^ use incremental derivation
74+
, clangOutDir :: Maybe FilePath -- ^ where to put output files
75+
, clangSkipIndexing :: Bool -- ^ use existing indexer output files
7076
} deriving Show
7177

7278
options :: Parser Clang
@@ -88,13 +94,29 @@ options = do
8894
long "jobs" <>
8995
value 1 <>
9096
help "run N indexers in parallel"
97+
clangBatch <- option auto $
98+
short 'n' <>
99+
long "batch" <>
100+
value 100 <>
101+
help "index files in batches of N"
91102
clangVerbose <- switch $
92103
short 'v' <>
93104
long "verbose" <>
94105
help "Enable verbose logging from subprocesses"
95106
clangProgress <- switch $
96107
long "progress" <>
97108
help "Display indexing progress even in verbose mode"
109+
clangCmakeOpts <- many $ strOption $
110+
long "cmake-opt" <>
111+
help "Extra flag to pass to cmake"
112+
clangOutDir <- optional $ strOption $
113+
long "out" <>
114+
metavar "DIR" <>
115+
help "Directory to save indexer output"
116+
clangSkipIndexing <- switch $
117+
long "skip-indexing" <>
118+
help ("If indexing has already been done, " <>
119+
"use the existing indexer output files. Requires --out DIR")
98120
clangIncremental <- pure False -- internal, not a CLI flag
99121
return Clang{..}
100122

@@ -118,20 +140,27 @@ indexerWith deriveToo = Indexer {
118140
indexerRun = \clang@Clang{..} backend repo IndexerParams{..} -> do
119141
-- indexing
120142
let tmpDir = indexerOutput
143+
buildDir = indexerOutput </> "build"
144+
outDir = fromMaybe (indexerOutput </> "indexer") clangOutDir
121145
inventoryFile = tmpDir </> "inventory.data"
146+
createDirectoryIfMissing True buildDir
147+
createDirectoryIfMissing True outDir
122148
generateInventory backend repo inventoryFile
123149
compileDBDir <-
124150
case clangCompileDBDir of
125151
Nothing ->
126152
case clangTarget of
127-
Nothing -> cmake clangVerbose indexerRoot tmpDir >> return tmpDir
153+
Nothing -> cmake clang indexerRoot buildDir >> return buildDir
128154
Just target -> do
129155
cdb <- runBuckFullCompilationDatabase target
130156
return $ takeDirectory cdb
131157
Just dir -> return dir
132-
indexerData <-
133-
index clang inventoryFile indexerRoot compileDBDir indexerOutput
134-
writeToDB backend repo indexerData
158+
159+
index clang inventoryFile indexerRoot compileDBDir outDir
160+
161+
files <- map (outDir </>) . filter ("indexer-" `isPrefixOf`) <$>
162+
listDirectory outDir
163+
writeToDB backend repo files
135164

136165
-- deriving
137166
when deriveToo $ do
@@ -171,12 +200,12 @@ indexerWith deriveToo = Indexer {
171200
generateInventory backend repo outFile =
172201
serializeInventory backend repo >>= BS.writeFile outFile
173202

174-
cmake verbose srcDir tmpDir = withExe "cmake" Nothing $ \cmakeBin ->
175-
spawnAndConcurrentLog verbose cmakeBin
203+
cmake Clang{..} srcDir tmpDir = withExe "cmake" Nothing $ \cmakeBin ->
204+
spawnAndConcurrentLog clangVerbose cmakeBin $
176205
[ "-DCMAKE_EXPORT_COMPILE_COMMANDS=1"
177206
, "-S", srcDir
178207
, "-B", tmpDir
179-
]
208+
] <> clangCmakeOpts
180209

181210
index Clang{..} inventory srcDir buildDir tmpDir =
182211
withExe "clang-index" clangIndexBin $ \clangIndex -> do
@@ -220,13 +249,15 @@ indexerWith deriveToo = Indexer {
220249
let dataFile i = tmpDir </> "indexer-" <> show i <> ".data"
221250
workerargs i = args ++
222251
[ "-dump", dataFile i
252+
, "-dump_every", show clangBatch
223253
, "--work_file", wfile
224254
, "--worker_index", show i
225255
, "--worker_count", show workers
226256
]
227257
currentDir <- getCurrentDirectory
228258
let cdUp = not $ isPathPrefixOf currentDir buildDir
229-
forConcurrently_ [0 .. workers-1] $ \i -> bracket
259+
unless clangSkipIndexing $
260+
forConcurrently_ [0 .. workers-1] $ \i -> bracket
230261
-- createProcess_ because we don't want the stdout/stderr handles
231262
-- to be closed
232263
(createProcess_

0 commit comments

Comments
 (0)