depend.awk changes

- sort the dependencies
- sort the rules
- the above can be disabled in the BEGIN block
- usage documentation changes for next commit
This commit is contained in:
nhkeni
2024-03-04 13:38:17 -05:00
committed by nhkeni
parent 79867d78c8
commit a22ed4f232

View File

@@ -1,6 +1,6 @@
# depend.awk -- awk script used to construct makefile dependencies
# for nethack's source files (`make depend' support for Makefile.src).
# $NHDT-Date: 1697316508 2023/10/14 20:48:28 $ $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.16 $
# $NHDT-Date: 1709577497 2024/03/04 18:38:17 $ $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.18 $
#
# usage:
# awk -f depend.awk ../include/*.h list-of-.c/.cpp-files
@@ -9,6 +9,9 @@
# ( cd src ; make all ; cp ../sys/unix/Makefile.src ./Makefile ; \
# make depend ; cp ./Makefile ../sys/unix/Makefile.src ; \
# cd .. ; sh sys/unix/setup.sh [sys/unix/hints/FOO] )
# newer usage:
# cd sys/unix ; make -f Makefile.src updatedepend
#
# This awk program scans each file in sequence, looking for lines beginning
# with `#include "' and recording the name inside the quotes. For .h files,
@@ -31,6 +34,8 @@
# zlib.h ditto
#
BEGIN { FS = "\"" #for `#include "X"', $2 is X
dosort = 1
dorulesort = 1
special[++sp_cnt] = "../include/config.h"
special[++sp_cnt] = "../include/hack.h"
alt_deps["../include/extern.h"] = ""
@@ -73,7 +78,10 @@ FNR == 1 { output_dep() #finish previous file
}
deps[file] = deps[file] " " incl
}
END { output_dep() } #finish the last file
END {
output_dep() #finish the last file
output_final() #write output
}
#
@@ -81,7 +89,29 @@ END { output_dep() } #finish the last file
# don't do anything (we've just been collecting their dependencies);
# for .c files, output the `make' rule for corresponding .o file
#
function output_dep( base, targ, moc)
function output_dep(){
if(dorulesort){
worklist[++worklistctr] = file
} else {
output_final2()
}
}
function output_final( x)
{
if(dorulesort){
nhsort(worklist, 1, worklistctr-1, 1)
for(x=1;x<worklistctr;x++){
file = worklist[x]
output_final2()
}
} else {
return
}
}
function output_final2( base, targ, moc)
{
#get the file's base name (including suffix)
base = file; sub("^.+/", "", base)
@@ -142,6 +172,9 @@ function format_dep(target, source, col, n, i, list, prefix, moc)
#first: leading whitespace yields empty 1st element; not sure why moc
#files duplicate the target as next element but we need to skip that too
first = moc ? 3 : 2
if (dosort ){
nhsort(list, first, n, 0)
}
for (i = first; i <= n; i++) {
if (col + length(list[i]) >= (i < n ? 78 : 80) - 1) {
printf(" \\\n\t\t"); col = 16 #make a backslash+newline split
@@ -197,4 +230,55 @@ function depend(inout, name, skip, n, i, list)
return inout
}
#
# sort list[first]..list[last]
# Derived from: https://www.baeldung.com/linux/awk-begin-and-end-rules
#
function nhsort(list, first, last, cmpid, i,j,temp)
{
for (i = first; i <= last-1; i++) {
for (j = i+1; j <= last; j++) {
if (nhcmp(list[i], list[j], cmpid)) {
temp = list[i]
list[i] = list[j]
list[j] = temp
}
}
}
}
function nhcmp(a,b,cmpid)
{
if(cmpid == 0){ # sort dependencies
# 2 .c or .cpp files
if (a ~ /\.c(pp)?$/ && b ~ /\.c(pp)?$/ ){ return a > b }
# a .c or .cpp file and anything else
if (a ~ /\.c(pp)?$/){ return 0 }
if (b ~ /\.c(pp)?$/){ return 1 }
# default
return a > b
} else if(cmpid == 1){ # sort rules
# 2 .h files
if (a ~ /\.h$/ && b ~ /\/.h$/){ return a > b }
# a .h and anything else
if (a ~ /\.h$/){ return 0 }
if (b ~ /\.h$/){ return 1 }
# 2 .c or .cpp files
if (a ~ /\.c(pp)?$/ && b ~ /\.c(pp)?$/ ){ return a > b }
# a .c or .cpp file and anything else
if (a ~ /\.c(pp)?$/){ return 0 }
if (b ~ /\.c(pp)?$/){ return 1 }
# 2 .moc files
if (a ~ /\.moc$/ && b ~ /\.moc$/){ return a > b }
# a .moc and anything else
if (a ~ /\.moc$/){ return 0 }
if (b ~ /\.moc$/){ return 1 }
# default
return a > b
} else {
print "internal error cmpid=" cmpid
exit 1
}
}
#depend.awk#