extract-upper-case.pl 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. # find cmake/Modules/ | sed -rn 's/.*CMakeDetermine(.+)Compiler.cmake/\1/p' | sort
  13. my @languages = qw(ASM ASM_MASM ASM_NASM C CSharp CUDA CXX Fortran Java RC Swift);
  14. # unwanted upper-cases
  15. my %unwanted = map { $_ => 1 } qw(VS CXX IDE NOTFOUND NO_ DFOO DBAR NEW);
  16. # cannot remove ALL - exists for add_custom_command
  17. # control-statements
  18. my %conditional = map { $_ => 1 } qw(if else elseif endif);
  19. my %loop = map { $_ => 1 } qw(foreach while endforeach endwhile);
  20. # decrecated
  21. 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);
  22. # add some (popular) modules
  23. push @modules, "ExternalProject";
  24. # variables
  25. open(CMAKE, "$cmake --help-variable-list|") or die "could not run cmake";
  26. while (<CMAKE>) {
  27. chomp;
  28. if (/<(.*?)>/) {
  29. if ($1 eq 'LANG') {
  30. foreach my $lang (@languages) {
  31. (my $V = $_) =~ s/<.*>/$lang/;
  32. push @variables, $V;
  33. }
  34. next
  35. } else {
  36. next; # skip if containing < or >
  37. }
  38. }
  39. push @variables, $_;
  40. }
  41. close(CMAKE);
  42. # transform all variables in a hash - to be able to use exists later on
  43. my %variables = map { $_ => 1 } @variables;
  44. # commands
  45. open(CMAKE, "$cmake --help-command-list|");
  46. while (my $cmd = <CMAKE>) {
  47. chomp $cmd;
  48. push @commands, $cmd;
  49. }
  50. close(CMAKE);
  51. # now generate a keyword-list per command
  52. foreach my $cmd (@commands) {
  53. my @word = extract_upper("$cmake --help-command $cmd|");
  54. next if scalar @word == 0;
  55. $keywords{$cmd} = [ sort keys %{ { map { $_ => 1 } @word } } ];
  56. }
  57. # and now for modules
  58. foreach my $mod (@modules) {
  59. my @word = extract_upper("$cmake --help-module $mod|");
  60. next if scalar @word == 0;
  61. $keywords{$mod} = [ sort keys %{ { map { $_ => 1 } @word } } ];
  62. }
  63. # and now for generator-expressions
  64. my @generator_expr = extract_upper("$cmake --help-manual cmake-generator-expressions |");
  65. # properties
  66. open(CMAKE, "$cmake --help-property-list|");
  67. while (<CMAKE>) {
  68. next if /\</; # skip if containing < or >
  69. chomp;
  70. push @properties, $_;
  71. }
  72. close(CMAKE);
  73. # transform all properties in a hash
  74. my %properties = map { $_ => 1 } @properties;
  75. # version
  76. open(CMAKE, "$cmake --version|");
  77. my $version = 'unknown';
  78. while (<CMAKE>) {
  79. chomp;
  80. $version = $_ if /cmake version/;
  81. }
  82. close(CMAKE);
  83. # generate cmake.vim
  84. open(IN, "<cmake.vim.in") or die "could not read cmake.vim.in";
  85. open(OUT, ">syntax/cmake.vim") or die "could not write to syntax/cmake.vim";
  86. my @keyword_hi;
  87. while(<IN>)
  88. {
  89. if (m/\@([A-Z0-9_]+)\@/) { # match for @SOMETHING@
  90. if ($1 eq "COMMAND_LIST") {
  91. # do not include "special" commands in this list
  92. my @tmp = grep { ! exists $conditional{$_} and
  93. ! exists $loop{$_} and
  94. ! exists $deprecated{$_} } @commands;
  95. print_list(\*OUT, @tmp);
  96. } elsif ($1 eq "VARIABLE_LIST") {
  97. print_list(\*OUT, keys %variables);
  98. } elsif ($1 eq "MODULES") {
  99. print_list(\*OUT, @modules);
  100. } elsif ($1 eq "GENERATOR_EXPRESSIONS") {
  101. print_list(\*OUT, @generator_expr);
  102. } elsif ($1 eq "CONDITIONALS") {
  103. print_list(\*OUT, keys %conditional);
  104. } elsif ($1 eq "LOOPS") {
  105. print_list(\*OUT, keys %loop);
  106. } elsif ($1 eq "DEPRECATED") {
  107. print_list(\*OUT, keys %deprecated);
  108. } elsif ($1 eq "PROPERTIES") {
  109. print_list(\*OUT, keys %properties);
  110. } elsif ($1 eq "KEYWORDS") {
  111. foreach my $k (sort keys %keywords) {
  112. print OUT "syn keyword cmakeKW$k contained\n";
  113. print_list(\*OUT, @{$keywords{$k}});
  114. print OUT "\n";
  115. push @keyword_hi, "hi def link cmakeKW$k ModeMsg";
  116. }
  117. } elsif ($1 eq "KEYWORDS_HIGHLIGHT") {
  118. print OUT join("\n", @keyword_hi), "\n";
  119. } elsif ($1 eq "VERSION") {
  120. $_ =~ s/\@VERSION\@/$version/;
  121. print OUT $_;
  122. } elsif ($1 eq "DATE") {
  123. my $date = strftime "%Y %b %d", localtime;
  124. $_ =~ s/\@DATE\@/$date/;
  125. print OUT $_;
  126. } else {
  127. print "ERROR do not know how to replace $1\n";
  128. }
  129. } else {
  130. print OUT $_;
  131. }
  132. }
  133. close(IN);
  134. close(OUT);
  135. sub extract_upper
  136. {
  137. my $input = shift;
  138. my @word;
  139. open(KW, $input);
  140. while (<KW>) {
  141. foreach my $w (m/\b([A-Z_]{2,})\b/g) {
  142. next
  143. if exists $variables{$w} or # skip if it is a variable
  144. exists $unwanted{$w} or # skip if not wanted
  145. grep(/$w/, @word); # skip if already in array
  146. push @word, $w;
  147. }
  148. }
  149. close(KW);
  150. return @word;
  151. }
  152. sub print_list
  153. {
  154. my $O = shift;
  155. my $indent = " " x 12 . "\\ ";
  156. print $O $indent, join("\n" . $indent, sort @_), "\n";
  157. }