extract-upper-case.pl 3.9 KB

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