1 #!tclsh
2 #
3 # Copyright (c) 1987 Regents of the University of California.
4 # All rights reserved.
5 #
6 # Redistribution and use in source and binary forms are permitted
7 # provided that this notice is preserved and that due credit is given
8 # to the University of California at Berkeley. The name of the University
9 # may not be used to endorse or promote products derived from this
10 # software without specific prior written permission. This software
11 # is provided ``as is'' without express or implied warranty.
12 #
13
14 set makefile Makefile
15 set srcdir ""
16 set cc "g++"
17
18 while {1} {
19 set arg [lindex $argv 0]
20 switch -- $arg {
21 "-f" {
22 set makefile [lindex $argv 1]
23 set argv [lrange $argv 2 end]
24 }
25 "-s" {
26 set srcdir [lindex $argv 1]
27 set argv [lrange $argv 2 end]
28 }
29 "-c" {
30 set cc [lindex $argv 1]
31 set argv [lrange $argv 2 end]
32 }
33 default {
34 break
35 }
36 }
37 }
38
39 if { [llength $argv] == 0 } {
40 puts stderr "usage: mkdep [-s srcdir] [-f makefile] [flags] file..."
41 exit 1
42 }
43
44 if { ![file writable $makefile] } {
45 puts stderr "mkdep: no writeable makefile: $makefile"
46 exit 1
47 }
48
49 set tmpfile "/tmp/mkdep[pid]"
50
51 if [catch {open $makefile r} ifp] {
52 puts stderr "Can't read makefile: $ifp"
53 exit 1
54 }
55 if [catch {open $tmpfile w} ofp] {
56 puts stderr "Can't write temp file $tmpfile: $ofp"
57 exit 1
58 }
59
60 while { ![eof $ifp] } {
61 set line [gets $ifp]
62 if [string match " DO NOT DELETE THIS LINE*" $line] {
63 break
64 }
65 puts $ofp $line
66 }
67 close $ifp
68
69 puts $ofp {
70 # DO NOT DELETE THIS LINE -- mkdep uses it.
71 # DO NOT PUT ANYTHING AFTER THIS LINE, IT WILL GO AWAY.
72
73 }
74
75 set cmd [list $cc "-M"]
76 if { $srcdir != "" } {
77 foreach arg $argv {
78 if [string match "-*" $arg] {
79 lappend cmd $arg
80 } else {
81 lappend cmd "$srcdir/$arg"
82 }
83 }
84 } else {
85 set cmd [concat $cmd $argv]
86 }
87
88 #puts "running $cmd"
89
90 if [catch {open "|$cmd" r} ifp] {
91 puts stderr "Can't exec $cc: $ifp"
92 exit 1
93 }
94
95 while { ![eof $ifp] } {
96 set line [gets $ifp]
97
98 # XXX this was in the old mkdep:
99 # sed s; \./; ;g
100
101 if [regexp -- {^([^ ]+): ([^ ]+) (.*)} $line match target file rest] {
102 set oline $line
103
104 # GNU cpp outputs assumes objects land in current directory
105 # but we put them in subdirectories, so fix up the rules
106 # accordingly i.e.,
107 # make "foo.o: dir/foo.cc blah" turn into "dir/foo.o: dir/..."
108 if { [string first "/" $target] == -1 && [string first "/" $file] != -1 } {
109 set target "[file dirname $file]/$target"
110 }
111
112 # if we have srcdir at the beginning of the object
113 # file path, strip it off
114 regsub -- "^${srcdir}/" $target "" target
115
116 set line "$target: $file $rest"
117 }
118 puts $ofp $line
119 }
120
121 puts $ofp {
122
123 # IF YOU PUT ANYTHING HERE IT WILL GO AWAY
124 }
125 close $ofp
126
127 if [file exists $makefile.bak] {
128 file delete $makefile.bak
129 }
130 file rename $makefile $makefile.bak
131 file copy $tmpfile $makefile
132 file delete $tmpfile $makefile.bak
133
134 exit 0
135
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.