Script to list dependencies
I would like to make a script (in whatever language is most suitable) that extracts the contents of "#include "Something.h"" statements.
Example Input File: Code:
some stuff Code:
Something.h How should I do this (or maybe there already is such a thing)? |
find, grep and sed seem like apt tools for this (in that order) - find to get the files, grep to search for #include lines, and sed to edit-out the bits you don't want.
Here's a single command-line that works for a bunch of files I have in one of my projects. It'll probably have bugs, and you'll probably want to wrap it in script-related niceness, but it's a start: Code:
find | xargs egrep '#include [<"].*[>"]' | sed -e 's|.*#include [<"]||g' -e 's|[>"].*||g' | sort | uniq |
gcc can do this: see Preprocessor Options (-M and -MM). The output is in Makefile format:
Code:
~/tmp$ cat dep.c |
It's hard to track down dependencies with local includes unless you're privy to the include paths to be specified at compile time, in which case you might as well just use automake and see if you can find where it stores dependency information (I've never bothered to look for it, but it's there somewhere.)
Kevin Barry |
I've started playing with JohnGraham's script, even thought it doesn't do what I want I think I can make it do what I want.
The main thing is that the script fount all the dependencies of all the files, I just need the dependencies of one single .c file recursively. Also, I only want to catch #include's with quotes (not brackets). Here's what I came up with so far: Code:
cat lsd Code:
lsd Cat.c |
Some more progress:
Code:
#!/bin/bash Now just the recursive part. EDIT: I figured out that the grep command is searching for the space, this is how I fixed it: Code:
grep '#include *["].*["]' $1 | sed 's:[^"]*"\([^"]*\)"[^"]*:\1:' |
I really have no idea how to do this.
Is it possible for a for loop in bash to iterate through individual lines of a block text stored in a variable? |
I found that you can use exec to redirect a file to stdin and then read to read stdin line by line, but how do you redirect the contents of a variable to stdin?
|
Code:
#!/usr/bin/env bash |
You still don't understand that I don't want to get all the dependencies of all the files. I just want the dependencies of 1 file, and the dependencies of those, and so on.
Also the example creates syntax errors. |
That's not really what you want to do. What you want to do is something like "draw a dependency graph", which doxygen can do for you with the help of graphvis.
What is it that you *really* want to do? |
Actually this is supposed to be a part of an automated script that checks which .c files must be compiled.
For this purpose, I want it so that you just apply a .c file as the argument and it lists out all the .h files it includes. |
Code:
#!/bin/bash |
Doesn't that go only 2 levels deep?
Here's my recursive version, but it just freezes at the highlited line and doesn't execute further: Code:
#!/bin/bash |
Quote:
|
The line "$files = $filedeps" doesn't work!?!?
|
Code:
#!/bin/bash |
Quote:
for example I am used to "normal" programming languages and this completely freaks me out: Code:
echo $files|sed 's/ /\n/g'|while read file The problem with most bash tutorials is that they treat it as a normal programming language, not even saying that things like the fact that "while" is actually independent command-line program that takes input and output, not a built-in construct. |
uh, while is a built-in construct. The problem is that piping creates subshells, and stuff that happens inside a subshell doesn't effect the parent.
You're right that pretty much all bash resources are misleading, especially ABS.. http://mywiki.wooledge.org/BashGuide ..much better Disillusion's script is more POSIX shell than Bash, but even there it's got problems, particularly with handling filenames with spaces. Code:
#!/usr/bin/env bash |
Quote:
|
Quote:
Quote:
|
Code:
awk 'BEGIN{s="find /usr/include -type f \\( "} |
Here's my current version, but the highlighted part doesn't seem to take effect:
Code:
#!/usr/bin/env bash |
Quote:
Kevin Barry |
Use arrays.
Code:
#!/usr/bin/env bash |
It works now.
|
All times are GMT -5. The time now is 04:36 PM. |