cc_driver.pl
This script parses a compilation database, and executes a specified command for each matching file in the database. It generates a command that includes the -I
and -D
parameters used in the original compilation.
Usage
cc_driver.pl [-d] [-v] [-s] [-n] [-p build_path] [-i include_pattern] [-x exclude_pattern] command [parameters]
Parameters
Parameter | Description |
---|---|
-d | Debug – print generated command to stdout, but don’t execute it. Implies -v (verbose). |
-v | Be verbose. Displays generated commands to stdout. |
-s | Generate system include paths. See System Include Files. |
-n | Don’t include -I or -D parameters in the generated command. This can be useful when using the script to execute “normal” commands (e.g., grep) that don’t take such parameters. |
-p build_path | Specifies the path to a compilation database in JSON format. It can be either an absolute path to a compilation database, or a directory (which will be searched for a compile_commands.json file). If omitted, the current directory is used. |
-i include_pattern | File paths matching include_pattern are included in generated commands. May be specified multiple times. If omitted, all files in the compilation database are included. |
-x exclude_pattern | File paths matching exclude_pattern are not included in generated commands. May be specified multiple times. All exclude_patterns are matched after any include patterns. |
command | Specifies the command to run against each file. (See below for details on how the generated command line is constructed). |
parameters | Any parameters for the specified command. Note that you may need to quote the parameters if they include quotes themselves to avoid quote removal. |
Environment Variables
If the -s
option is specified, the value of ${CXX}
is used as the name of the compiler. (If ${CXX}
is not defined, the default is g++
).
Notes
The generated command first changes to the original build directory before executing the specified command, so specifying [command] using a relative path is generally a mistake. Instead use an absolute path, or make sure that [command] can be found using the PATH
environment variable.
Generated command format
The script generates a command line of the form
cd {directory}; [command] [parameters] -I .. -D .. {file}
The clang tools require a slightly different format – if a clang tool is specified for [command], the generated command format is:
cd {directory}; [command] {file} -- [parameters] -I .. -D ..
System Include Files
Some of the tools that can be scripted with this command work better (sometimes much better) if they can see all the include files used in the original compilation. If the -s
flag is specified, the system include paths are appended to any include paths from the original compile command.
The system include paths are:
-
Any paths specified using the
-isystem
compiler flag. (See System Headers - The C Preprocessor for why you might want to use theisystem
flag). As with the compiler’s-isystem
flag, any directories specified are searched after all directories specified using the-I
flag, regardless of where it occurs on the command line. -
The default compiler search paths are appended to the generated command line. The compiler search paths are determined by parsing the output of
${CXX} -E -x c++ - -v 2>&1 1>/dev/null </dev/null
See this post for more information.
Code Listing
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
|