Renato Golin | 7bbb208 | 2016-06-11 00:27:14 +0100 | [diff] [blame] | 1 | #!/usr/bin/env perl |
| 2 | |
| 3 | use strict; |
| 4 | use warnings; |
| 5 | |
| 6 | # This script updates the .ycm_extra_conf.py in $LLVM_SRC to work with |
| 7 | # $LLVM_BLD's compile_commands.json and include/library paths. |
| 8 | # |
| 9 | # Syntax: llvm-ycm |
| 10 | # |
| 11 | # It will: |
| 12 | # * automatically detect $LLVM_SRC/BLD and change the paths. |
| 13 | # * also bail if those variables or .ycm_extra_conf.py don't exist. |
| 14 | # * exit successfully and silently if the file has already changed. |
| 15 | |
| 16 | my ($SRC, $BLD) = ($ENV{'LLVM_SRC'}, $ENV{'LLVM_BLD'}); |
| 17 | if (!$SRC || !$BLD || $ARGV[0] || ! -d $SRC || ! -d $BLD) { |
| 18 | die "Syntax: llvm-ycm\n\nUse llvm-env to set \$LLVM_SRC/BLD\n"; |
| 19 | } |
| 20 | my ($ycm_conf, $compile_db) = (".ycm_extra_conf.py", "compile_commands.json"); |
| 21 | |
| 22 | # If the file doesn't exist, don't try to guess the location of YCM-Generator |
| 23 | # Vim can do that on its own, if properly setup. Educate the user. |
| 24 | if (! -f "$SRC/$ycm_conf") { |
| 25 | die "$SRC/$ycm_conf doesn't exist!\n". |
| 26 | "You need to generate it using YCM-Generator.\n". |
| 27 | "\nTo install, run:\n". |
| 28 | " mkdir -p ~/.vim/ext\n". |
| 29 | " cd ~/.vim/ext\n". |
| 30 | " git clone https://github.com/rdnetto/YCM-Generator.git\n". |
| 31 | "\nTo enable in vim, add this line to your .vimrc:\n". |
| 32 | " set rtp+=~/.vim/ext/YCM-Generator\n". |
| 33 | "\nTo generate the file, open Vim inside your project and type:\n". |
| 34 | " :YcmGenerateConfig\n". |
| 35 | "\nIf all goes well, you should have an $ycm_conf in $SRC\n"; |
| 36 | } |
| 37 | |
| 38 | # If the compile database doesn't exist, there's no point in even trying. |
| 39 | # Educate the user. |
| 40 | if (! -f "$BLD/$compile_db") { |
| 41 | die "$BLD/$compile_db doesn't exist!\n". |
| 42 | "You need to generate it using LLVM's CMake file.\n". |
| 43 | "\nTo create the compile database every build, add this option to CMake:\n". |
| 44 | " -DCMAKE_EXPORT_COMPILE_COMMANDS=ON\n". |
| 45 | "\nRebuild, and check if the file is there, then run this script again.\n"; |
| 46 | } |
| 47 | |
| 48 | my @lines = (); |
| 49 | my $bld_re = $BLD; |
| 50 | $bld_re =~ s/\//\\\//g; |
| 51 | |
| 52 | # We need to open the file, buffer and close, so we can write back to the same |
| 53 | # file again. |
| 54 | open(FH, "$SRC/$ycm_conf") || die "Can't open file '$SRC/$ycm_conf': $!\n"; |
| 55 | foreach my $line (<FH>) { |
| 56 | # If the file has already been changed, let go. |
| 57 | if (&is_changed($line)) { |
| 58 | close FH; |
| 59 | exit(0); |
| 60 | } |
| 61 | |
| 62 | # Change lines if needed |
| 63 | &change(\$line); |
| 64 | |
| 65 | # Buffer |
| 66 | push @lines, $line; |
| 67 | } |
| 68 | close FH; |
| 69 | |
| 70 | # Now we write back and all should be good. |
| 71 | open(FH, ">$SRC/$ycm_conf") || die "Can't open file '$SRC/$ycm_conf': $!\n"; |
| 72 | foreach my $line (@lines) { |
| 73 | print FH $line; |
| 74 | } |
| 75 | close FH; |
| 76 | |
| 77 | # All is good. |
| 78 | exit(0); |
| 79 | |
| 80 | ################################ Methods |
| 81 | |
| 82 | # is_changed($line): checks whether the line has been changed by this script. |
| 83 | sub is_changed($) { |
| 84 | my ($line) = @_; |
| 85 | return ($line =~ /-I$bld_re/); |
| 86 | } |
| 87 | |
| 88 | # change($$line): change the line in-place to make it work with YCM. |
| 89 | sub change($) { |
| 90 | my ($line) = @_; |
| 91 | # from: -I/tmp/tmpUF9s_w/include |
| 92 | # to: -I$LLVM_BLD/include |
| 93 | my $changed = ($$line =~ s/\/tmp\/\w+/$BLD/); |
| 94 | return if ($changed); |
| 95 | |
| 96 | # Changing this line will make header and include files not work for |
| 97 | # completion, since they're not in the compilation database. Just using the |
| 98 | # whole lot of flags above works fine. |
| 99 | # from: compilation_database_folder = '' |
| 100 | # to: compilation_database_folder = '$LLVM_BLD' |
| 101 | #$$line =~ s/(compilation_database_folder) = ''/$1 = '$BLD'/; |
| 102 | } |