gen-sorted.awk 2.2 KB

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