gen-sorted.awk 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #!/usr/bin/awk -f
  2. # Generate sorted list of directories. The sorting is stable but with
  3. # dependencies between directories resolved by moving dependees in front.
  4. # Copyright (C) 1998-2026 Free Software Foundation, Inc.
  5. BEGIN {
  6. cnt = split(subdirs, all) + 1
  7. dnt = 0
  8. }
  9. # Let input files have comments.
  10. { sub(/[ ]*#.*$/, "") }
  11. NF == 0 { next }
  12. {
  13. subdir = type = FILENAME;
  14. sub(/^.*\//, "", type);
  15. sub(/\/[^/]+$/, "", subdir);
  16. sub(/^.*\//, "", subdir);
  17. thisdir = "";
  18. }
  19. type == "Depend" && NF == 1 {
  20. from[dnt] = subdir;
  21. to[dnt] = $1;
  22. ++dnt;
  23. next
  24. }
  25. type == "Subdirs" && NF == 1 { thisdir = $1 }
  26. type == "Subdirs" && NF == 2 && $1 == "first" {
  27. thisdir = $2;
  28. # Make the first dir in the list depend on this one.
  29. from[dnt] = all[1];
  30. to[dnt] = thisdir;
  31. ++dnt;
  32. }
  33. type == "Subdirs" && NF == 2 && $1 == "inhibit" {
  34. inhibit[$2] = subdir;
  35. next
  36. }
  37. type == "Subdirs" && thisdir {
  38. all[cnt++] = thisdir;
  39. this_srcdir = srcpfx thisdir
  40. if (system("test -d " this_srcdir) != 0) {
  41. print FILENAME ":" FNR ":", "cannot find", this_srcdir > "/dev/stderr";
  42. exit 2
  43. }
  44. file = this_srcdir "/Depend";
  45. if (system("test -f " file) == 0) {
  46. ARGV[ARGC++] = file;
  47. # Emit a dependency on the implicitly-read file.
  48. if (srcpfx)
  49. sub(/^\.\.\//, "", file);
  50. if (file !~ /^\/.*$/)
  51. file = "$(..)" file;
  52. print "$(common-objpfx)sysd-sorted:", "$(wildcard", file ")";
  53. }
  54. next
  55. }
  56. {
  57. print FILENAME ":" FNR ":", "what type of file is this?" > "/dev/stderr";
  58. exit 2
  59. }
  60. END {
  61. do {
  62. moved = 0
  63. for (i = 0; i < dnt; ++i) {
  64. for (j = 1; j < cnt; ++j) {
  65. if (all[j] == from[i]) {
  66. for (k = j + 1; k < cnt; ++k) {
  67. if (all[k] == to[i]) {
  68. break;
  69. }
  70. }
  71. if (k < cnt) {
  72. for (l = k - 1; l >= j; --l) {
  73. all[l + 1] = all[l]
  74. }
  75. all[j] = to[i]
  76. break;
  77. }
  78. }
  79. }
  80. if (j < cnt) {
  81. moved = 1
  82. break
  83. }
  84. }
  85. } while (moved);
  86. # Make sure we list "elf" last.
  87. saw_elf = 0;
  88. printf "sorted-subdirs :=";
  89. for (i = 1; i < cnt; ++i) {
  90. if (all[i] in inhibit)
  91. continue;
  92. if (all[i] == "elf")
  93. saw_elf = 1;
  94. else
  95. printf " %s", all[i];
  96. }
  97. printf "%s\n", saw_elf ? " elf" : "";
  98. print "sysd-sorted-done := t"
  99. }