extract-upper-case.pl 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. #!/usr/bin/env perl
  2. use strict;
  3. use warnings;
  4. use POSIX qw(strftime);
  5. #my $cmake = "/home/pboettch/devel/upstream/cmake/build/bin/cmake";
  6. my $cmake = "cmake";
  7. my @variables;
  8. my @commands;
  9. my @properties;
  10. my @modules;
  11. my %keywords; # command => keyword-list
  12. # unwanted upper-cases
  13. my %unwanted = map { $_ => 1 } qw(VS CXX IDE NOTFOUND NO_ DFOO DBAR NEW);
  14. # cannot remove ALL - exists for add_custom_command
  15. # control-statements
  16. my %conditional = map { $_ => 1 } qw(if else elseif endif);
  17. my %loop = map { $_ => 1 } qw(foreach while endforeach endwhile);
  18. # decrecated
  19. my %deprecated = map { $_ => 1 } qw(build_name exec_program export_library_dependencies install_files install_programs install_targets link_libraries make_directory output_required_files remove subdir_depends subdirs use_mangled_mesa utility_source variable_requires write_file);
  20. # add some (popular) modules
  21. push @modules, "ExternalProject";
  22. # variables
  23. open(CMAKE, "$cmake --help-variable-list|") or die "could not run cmake";
  24. while (<CMAKE>) {
  25. next if /\</; # skip if containing < or >
  26. chomp;
  27. push @variables, $_;
  28. }
  29. close(CMAKE);
  30. # transform all variables in a hash - to be able to use exists later on
  31. my %variables = map { $_ => 1 } @variables;
  32. # commands
  33. open(CMAKE, "$cmake --help-command-list|");
  34. while (my $cmd = <CMAKE>) {
  35. chomp $cmd;
  36. push @commands, $cmd;
  37. }
  38. close(CMAKE);
  39. # now generate a keyword-list per command
  40. foreach my $cmd (@commands) {
  41. my @word = extract_upper("$cmake --help-command $cmd|");
  42. next if scalar @word == 0;
  43. $keywords{$cmd} = [ sort keys %{ { map { $_ => 1 } @word } } ];
  44. }
  45. # and now for modules
  46. foreach my $mod (@modules) {
  47. my @word = extract_upper("$cmake --help-module $mod|");
  48. next if scalar @word == 0;
  49. $keywords{$mod} = [ sort keys %{ { map { $_ => 1 } @word } } ];
  50. }
  51. # and now for generator-expressions
  52. my @generator_expr = extract_upper("$cmake --help-manual cmake-generator-expressions |");
  53. # properties
  54. open(CMAKE, "$cmake --help-property-list|");
  55. while (<CMAKE>) {
  56. next if /\</; # skip if containing < or >
  57. chomp;
  58. push @properties, $_;
  59. }
  60. close(CMAKE);
  61. # transform all properties in a hash
  62. my %properties = map { $_ => 1 } @properties;
  63. # version
  64. open(CMAKE, "$cmake --version|");
  65. my $version = 'unknown';
  66. while (<CMAKE>) {
  67. chomp;
  68. $version = $_ if /cmake version/;
  69. }
  70. close(CMAKE);
  71. # generate cmake.vim
  72. open(IN, "<cmake.vim.in") or die "could not read cmake.vim.in";
  73. open(OUT, ">syntax/cmake.vim") or die "could not write to syntax/cmake.vim";
  74. my @keyword_hi;
  75. while(<IN>)
  76. {
  77. if (m/\@([A-Z0-9_]+)\@/) { # match for @SOMETHING@
  78. if ($1 eq "COMMAND_LIST") {
  79. # do not include "special" commands in this list
  80. my @tmp = grep { ! exists $conditional{$_} and
  81. ! exists $loop{$_} and
  82. ! exists $deprecated{$_} } @commands;
  83. print OUT " " x 12 , "\\ ", join(" ", @tmp), "\n";
  84. } elsif ($1 eq "VARIABLE_LIST") {
  85. print OUT " " x 12 , "\\ ", join(" ", sort keys %variables), "\n";
  86. } elsif ($1 eq "MODULES") {
  87. print OUT " " x 12 , "\\ ", join("\n", @modules), "\n";
  88. } elsif ($1 eq "GENERATOR_EXPRESSIONS") {
  89. print OUT " " x 12 , "\\ ", join(" ", @generator_expr), "\n";
  90. } elsif ($1 eq "CONDITIONALS") {
  91. print OUT " " x 12 , "\\ ", join(" ", sort keys %conditional), "\n";
  92. } elsif ($1 eq "LOOPS") {
  93. print OUT " " x 12 , "\\ ", join(" ", sort keys %loop), "\n";
  94. } elsif ($1 eq "DEPRECATED") {
  95. print OUT " " x 12 , "\\ ", join(" ", sort keys %deprecated), "\n";
  96. } elsif ($1 eq "PROPERTIES") {
  97. print OUT " " x 12 , "\\ ", join(" ", sort keys %properties), "\n";
  98. } elsif ($1 eq "KEYWORDS") {
  99. foreach my $k (sort keys %keywords) {
  100. print OUT "syn keyword cmakeKW$k contained\n";
  101. print OUT " " x 12, "\\ ", join(" ", @{$keywords{$k}}), "\n";
  102. print OUT "\n";
  103. push @keyword_hi, "hi def link cmakeKW$k ModeMsg";
  104. }
  105. } elsif ($1 eq "KEYWORDS_HIGHLIGHT") {
  106. print OUT join("\n", @keyword_hi), "\n";
  107. } elsif ($1 eq "VERSION") {
  108. $_ =~ s/\@VERSION\@/$version/;
  109. print OUT $_;
  110. } elsif ($1 eq "DATE") {
  111. my $date = strftime "%Y %b %d", localtime;
  112. $_ =~ s/\@DATE\@/$date/;
  113. print OUT $_;
  114. } else {
  115. print "ERROR do not know how to replace $1\n";
  116. }
  117. } else {
  118. print OUT $_;
  119. }
  120. }
  121. close(IN);
  122. close(OUT);
  123. sub extract_upper
  124. {
  125. my $input = shift;
  126. my @word;
  127. open(KW, $input);
  128. while (<KW>) {
  129. foreach my $w (m/\b([A-Z_]{2,})\b/g) {
  130. next
  131. if exists $variables{$w} or # skip if it is a variable
  132. exists $unwanted{$w} or # skip if not wanted
  133. grep(/$w/, @word); # skip if already in array
  134. push @word, $w;
  135. }
  136. }
  137. close(KW);
  138. return @word;
  139. }