This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: -MM and other excitement


Mo McKinlay wrote:
> 
> # which is not what I want. I want foo/bar.o: bar.h. Is this possible? I
> # looked through the archive and found SUNPRO_DEPENDENCIES, but this
> # didnt' appear useful. Thoughts?
> 
> Last time I ran across this (which was pretty recently), I piped the
> output through sed to prepend the path. In my case this worked pretty well
> - I could see it breaking pretty majorly if you've got several
> subdirectories of sources, though.

I've run into this often enough that I wrote a bash script to handle it.
Here's the script; do whatever you like with it.

------------------------ cut here -------------------------
#!/bin/bash

# This script runs through all the source files (recognised by the
# list of suffixes below) in the current directory and all
# subdirectories, and produces two files containing a list of the
# resulting object files and their dependencies, in a format suitable
# for direct inclusion in a makefile.

dep_file=dependencies
obj_file=objects
suffixes=".c .cpp"
temp_obj_file=_${0##*/}_temp_1_

any_files() {
  # $@ = glob pattern
  if [ -n "$1" ] && [ -e "$1" ]; then
    return 0
  else
    return 1
  fi
}

search_directory() {
  # $1 = directory, relative to current
  local dir file prefix suffix
  if [ "$1" != "." ]; then
    prefix=$1/
  fi
  for suffix in $suffixes; do
    if any_files $prefix*$suffix; then
      gcc -I. -MM $prefix*$suffix \
        | sed "s=^[^ ]=$prefix&=" \
        >>$dep_file
      ls $prefix*$suffix \
        | sed "s=\\$suffix=\\.o=" \
        | sed "s=.\+= & \\\\=" \
        >>$temp_obj_file
    fi
  done
  for file in $(ls $1); do
    dir=$prefix$file
    if [ -d "$dir" ]; then
      search_directory $dir
    fi
  done
}

rm -f $dep_file
rm -f $obj_file
rm -f $temp_obj_file
search_directory .
echo -n "OBJECTS =" >$obj_file
sort $temp_obj_file | uniq >>$obj_file
echo >>$obj_file
rm $temp_obj_file
------------------------ cut here -------------------------

-- 
Ross Smith <ross.s@ihug.co.nz> The Internet Group, Auckland, New Zealand
========================================================================
"C++ is to programming as sex is to reproduction. Better ways might
technically exist but they're not nearly as much fun." -- Nikolai Irgens

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]