検索

universal-ctagsをソースからコンパイルしてCentOS7にインストール

投稿日:2019年7月25日

ctagsはプログラムソースを読みこませてクラス名、メソッド名、変数名他各種識別子をインデックスしたtagsファイルを作成するプログラムツールです。

tagsファイルをvimやemacsなどの外部プログラムから読み込むことでタグジャンプが使えるようになり、効率的にソースを追えるようになります。

メジャーなctagsプログラムとして「exuberant ctags」と「universal ctags」があり、前者はもう開発が行われておらず、にも関わらずyum install ctagsすると前者がインストールされてしまう具合の悪さが有ります。

後者の「universal ctags」をCentOS 7にインストールするにはgithubからソースを落としてコンパイルしてインストールする必要があります。

universal-ctagsをgit clone

適当なディレクトリにクローン。

git clone https://github.com/universal-ctags/ctags.git

ビルドを始める前に、githubサイトにビルドの際の注意書きがあります。


How to build and install


To build with Autotools, see docs/autotools.rst for more information. (To build on GNU/Linux, Autotools is your choice.)

ビルドとインストールの仕方

Autotoolsを使ってインストールするにはautotools.rstを見てね。


https://github.com/universal-ctags/ctags

とあるのでdoc/autotools.rstファイルを見てみます。

Building with configure (\*nix including GNU/Linux)
---------------------------------------------------------------------
Like most Autotools-based projects, you need to do::

    $ ./autogen.sh
    $ ./configure --prefix=/where/you/want # defaults to /usr/local
    $ make
    $ make install # may require extra privileges depending on where to install

After installation the `ctags` executable will be in `$prefix/bin/`.

`autogen.sh` runs `autoreconf` internally.
If you use a (binary oriented) GNU/Linux distribution, `autoreconf` may
be part of the `autoconf` package. In addition you may have to install
`automake` and/or `pkg-config`, too.

    (snip)
  1. ./autogen.sh
  2. ./configure
  3. make
  4. make install

を実行すればuniversal-ctagsをインスト―ル出来そうですが、autogen.shの中でautoreconfコマンドを使うのでautotools関連のパッケージを入れる必要が有りそうです。実際autogen.shを実行してみるとautoreconfコマンドが見つからずエラーになります。

[root@spock ctags]# ./autogen.sh
+ type autoreconf
./autogen.sh: 5 行: type: autoreconf: 見つかりません

autotools関連コマンドをyumインストール

ということでautoconf、automake、libtoolをyumインストールします。

[root@spock ctags]# yum install autoconf automake libtool
読み込んだプラグイン:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
epel/x86_64/metalink                                                                                                 | 6.7 kB  00:00:00
 * base: ftp.iij.ad.jp
 * epel: fedora.cs.nctu.edu.tw
 * extras: ftp.iij.ad.jp
 * updates: ftp.iij.ad.jp
base                                                                                                                 | 3.6 kB  00:00:00
docker-ce-stable                                                                                                     | 3.5 kB  00:00:00
extras                                                                                                               | 3.4 kB  00:00:00
updates                                                                                                              | 3.4 kB  00:00:00
依存性の解決をしています
--> トランザクションの確認を実行しています。
---> パッケージ autoconf.noarch 0:2.69-11.el7 を インストール
--> 依存性の処理をしています: m4 >= 1.4.14 のパッケージ: autoconf-2.69-11.el7.noarch
---> パッケージ automake.noarch 0:1.13.4-3.el7 を インストール
--> 依存性の処理をしています: perl(Thread::Queue) のパッケージ: automake-1.13.4-3.el7.noarch
--> 依存性の処理をしています: perl(TAP::Parser) のパッケージ: automake-1.13.4-3.el7.noarch
---> パッケージ libtool.x86_64 0:2.4.2-22.el7_3 を インストール
--> トランザクションの確認を実行しています。
---> パッケージ m4.x86_64 0:1.4.16-10.el7 を インストール
---> パッケージ perl-Test-Harness.noarch 0:3.28-3.el7 を インストール
---> パッケージ perl-Thread-Queue.noarch 0:3.02-2.el7 を インストール
--> 依存性解決を終了しました。

依存性を解決しました

============================================================================================================================================
 Package                                 アーキテクチャー             バージョン                           リポジトリー                容量
============================================================================================================================================
インストール中:
 autoconf                                noarch                       2.69-11.el7                          base                       701 k
 automake                                noarch                       1.13.4-3.el7                         base                       679 k
 libtool                                 x86_64                       2.4.2-22.el7_3                       base                       588 k
依存性関連でのインストールをします:
 m4                                      x86_64                       1.4.16-10.el7                        base                       256 k
 perl-Test-Harness                       noarch                       3.28-3.el7                           base                       302 k
 perl-Thread-Queue                       noarch                       3.02-2.el7                           base                        17 k

トランザクションの要約
============================================================================================================================================
インストール  3 パッケージ (+3 個の依存関係のパッケージ)

総ダウンロード容量: 2.5 M
インストール容量: 7.2 M
Is this ok [y/d/N]: Y
Downloading packages:
(1/6): autoconf-2.69-11.el7.noarch.rpm                                                                               | 701 kB  00:00:00
(2/6): automake-1.13.4-3.el7.noarch.rpm                                                                              | 679 kB  00:00:00
(3/6): libtool-2.4.2-22.el7_3.x86_64.rpm                                                                             | 588 kB  00:00:00
(4/6): m4-1.4.16-10.el7.x86_64.rpm                                                                                   | 256 kB  00:00:00
(5/6): perl-Thread-Queue-3.02-2.el7.noarch.rpm                                                                       |  17 kB  00:00:00
(6/6): perl-Test-Harness-3.28-3.el7.noarch.rpm                                                                       | 302 kB  00:00:00
--------------------------------------------------------------------------------------------------------------------------------------------
合計                                                                                                         17 MB/s | 2.5 MB  00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  インストール中          : m4-1.4.16-10.el7.x86_64                                                                                     1/6
  インストール中          : autoconf-2.69-11.el7.noarch                                                                                 2/6
  インストール中          : perl-Thread-Queue-3.02-2.el7.noarch                                                                         3/6
  インストール中          : perl-Test-Harness-3.28-3.el7.noarch                                                                         4/6
  インストール中          : automake-1.13.4-3.el7.noarch                                                                                5/6
  インストール中          : libtool-2.4.2-22.el7_3.x86_64                                                                               6/6
  検証中                  : autoconf-2.69-11.el7.noarch                                                                                 1/6
  検証中                  : perl-Test-Harness-3.28-3.el7.noarch                                                                         2/6
  検証中                  : perl-Thread-Queue-3.02-2.el7.noarch                                                                         3/6
  検証中                  : automake-1.13.4-3.el7.noarch                                                                                4/6
  検証中                  : m4-1.4.16-10.el7.x86_64                                                                                     5/6
  検証中                  : libtool-2.4.2-22.el7_3.x86_64                                                                               6/6

インストール:
  autoconf.noarch 0:2.69-11.el7                automake.noarch 0:1.13.4-3.el7                libtool.x86_64 0:2.4.2-22.el7_3

依存性関連をインストールしました:
  m4.x86_64 0:1.4.16-10.el7             perl-Test-Harness.noarch 0:3.28-3.el7             perl-Thread-Queue.noarch 0:3.02-2.el7

完了しました!

先程無いよと言われたautoreconfコマンドもインストールされました。

[root@spock ~]# which autoreconf
/bin/autoreconf

[root@spock ~]# autoreconf --version
autoreconf (GNU Autoconf) 2.69
Copyright (C) 2012 Free Software Foundation, Inc.
License GPLv3+/Autoconf: GNU GPL version 3 or later
<http://gnu.org/licenses/gpl.html>, <http://gnu.org/licenses/exceptions.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by David J. MacKenzie and Akim Demaille.

universal-ctagsのインストール再開

./autogen.sh

再度autogen.shを実行。今度は成功します。

[root@spock ctags]# ./autogen.sh
+ type autoreconf
autoreconf は /bin/autoreconf です
+ type pkg-config
pkg-config は /bin/pkg-config です
+ '[' -z '' ']'
+ type make
+ MAKE=make
++ make -s -f makefiles/list-optlib2c-input.mak
+ ctags_files='optlib/RSpec.ctags
optlib/cmake.ctags
optlib/ctags-optlib.ctags
optlib/elixir.ctags

    (snip)

+ o=optlib/inko.c
+ echo 'optlib2c: translating optlib/inko.ctags to optlib/inko.c'
optlib2c: translating optlib/inko.ctags to optlib/inko.c
+ ./misc/optlib2c optlib/inko.ctags
+ exit 0

./configure

./configureを実行。Makefile他各種が出来ます。インストールする場所を変える場合は–prefix=/インストールするパス、を後ろに続けます。

[root@spock ctags]# ./configure

make && make install

コンパイルとコマンドファイルをインストールします。

[root@spock ctags]# make && make install

    (snip)

REPOINFO   main/repoinfo.h
make  install-recursive
make[1]: ディレクトリ `/tmp/ctags' に入ります
make[2]: ディレクトリ `/tmp/ctags' に入ります
REPOINFO   main/repoinfo.h
  CCLD     ctags
make[3]: ディレクトリ `/tmp/ctags' に入ります
REPOINFO   main/repoinfo.h
  CCLD     ctags
 /bin/mkdir -p '/usr/local/bin'
  /bin/install -c ctags readtags '/usr/local/bin'
make  install-exec-hook
make[4]: ディレクトリ `/tmp/ctags' に入ります
make[4]: `install-exec-hook' に対して行うべき事はありません.
make[4]: ディレクトリ `/tmp/ctags' から出ます
make[3]: ディレクトリ `/tmp/ctags' から出ます
make[2]: ディレクトリ `/tmp/ctags' から出ます
make[1]: ディレクトリ `/tmp/ctags' から出ます

コンパイル成功、/usr/local/bin/ctagsが入りました。

[root@spock ctags]# which ctags
/usr/local/bin/ctags
[root@spock ctags]# ctags --version
Universal Ctags 0.0.0(92b2f21), Copyright (C) 2015 Universal Ctags Team
Universal Ctags is derived from Exuberant Ctags.
Exuberant Ctags 5.8, Copyright (C) 1996-2009 Darren Hiebert
  Compiled: Jul 21 2019, 00:26:02
  URL: https://ctags.io/
  Optional compiled features: +wildcards, +regex, +iconv, +option-directory, +packcc

ctagsコマンドインストール完了です。

universal-ctagsが対応するプログラミング言語の一覧を見てみる

[root@spock tmp]# ctags --list-languages
  1. Ada
  2. Ant
  3. Asciidoc
  4. Asm
  5. Asp
  6. Autoconf
  7. AutoIt
  8. Automake
  9. Awk
  10. Basic
  11. BETA
  12. BibTeX
  13. C
  14. C#
  15. C++
  16. Clojure
  17. CMake
  18. Cobol
  19. CPreProcessor
  20. CSS
  21. Ctags
  22. CUDA
  23. D
  24. Diff
  25. DosBatch
  26. DTD
  27. DTS
  28. Eiffel
  29. Elixir
  30. Elm
  31. Erlang
  32. Falcon
  33. Flex
  34. Fortran
  35. Fypp
  36. Gdbinit
  37. Go
  38. HTML
  39. Iniconf
  40. Inko
  41. ITcl
  42. Java
  43. JavaProperties
  44. JavaScript
  45. JSON
  46. LdScript
  47. Lisp
  48. Lua
  49. M4
  50. Make
  51. Man
  52. Markdown
  53. MatLab
  54. Moose
  55. Myrddin
  56. NSIS
  57. ObjectiveC
  58. OCaml
  59. OldC [disabled]
  60. OldC++ [disabled]
  61. Pascal
  62. Passwd
  63. Perl
  64. Perl6
  65. PHP
  66. Pod
  67. Protobuf
  68. PuppetManifest
  69. Python
  70. PythonLoggingConfig
  71. QemuHX
  72. QtMoc
  73. R
  74. ReStructuredText
  75. REXX
  76. Robot
  77. RpmSpec
  78. RSpec
  79. Ruby
  80. Rust
  81. Scheme
  82. Sh
  83. SLang
  84. SML
  85. SQL
  86. SystemdUnit
  87. SystemTap
  88. SystemVerilog
  89. Tcl
  90. TclOO
  91. Tex
  92. TTCN
  93. TypeScript
  94. Varlink
  95. Vera
  96. Verilog
  97. VHDL
  98. Vim
  99. WindRes
  100. YACC
  101. YumRepo
  102. Zephir

使用頻度の高いC、Java、Javascript、Python、Ruby,PHPあたりは普通に対応しているので安心です。

viでタグジャンプする

Javaで適当に書いたspringを使うソースを書きます。
タグは静的に解析されるのでコンパイルは不要です。

~/src/HogeService.java

[root@spock src]# cat HogeService.java
package jp.co.hoge;

import org.springframework.stereotype.Service;

@Service
public class HogeService {

    public void execute(){
        System.out.println("hogehoge");
    }
}

/tmpにでもspring-frameworkのgitクローンしてきます。

[root@spock tmp]# git clone  https://github.com/spring-projects/spring-framework.git

両方のディレクトリをctagsで再帰的に解析して~/.tagsファイルを作る。

[root@spock src]# ctags -f ~/.tags -R ~/src /tmp/spring-framework/

~/.vimrcに以下を追加。

set tags=~/.tags

再度~/src/HogeService.javaをviで開き、
「import org.springframework.stereotype.Service」
の「Service」の部分にキャレットを当て、Ctrl + ]します。

Serviceの定義元である以下のソースにvi画面が移動されるはずです。

/tmp/spring-framework/spring-context/src/main/java/org/springframework/stereotype/Service.java

/*
 * Copyright 2002-2017 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.stereotype;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import org.springframework.core.annotation.AliasFor;

/**
 * Indicates that an annotated class is a "Service", originally defined by Domain-Driven
 * Design (Evans, 2003) as "an operation offered as an interface that stands alone in the
 * model, with no encapsulated state."
 *
 * <p>May also indicate that a class is a "Business Service Facade" (in the Core J2EE
 * patterns sense), or something similar. This annotation is a general-purpose stereotype
 * and individual teams may narrow their semantics and use as appropriate.
 *
 * <p>This annotation serves as a specialization of {@link Component @Component},
 * allowing for implementation classes to be autodetected through classpath scanning.
 *
 * @author Juergen Hoeller
 * @since 2.5
 * @see Component
 * @see Repository
 */
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Service {

元のHogeService.javaに戻るにはCtrl + tを実行します。

でももうviでソース書くことないし・・・

ctagsで作成したインデックスファイルはviやemacsだけでなく、ブラウザでソースを追えるOpenGrokなどからも使えます。

スマホからでもソースが追えるOpenGrokの構築は以下で行っています。

-検索
-, ,

執筆者:

関連記事

OpenGrokをインストールしてソースリーディング環境を作る

職場でもプライベートでも「あのフレームワークのあの処理はどういうコードになってるのか」気になってソースを落としてくることがあるかと思います。 職場や自宅ならIDEにソースを入れてビルドしてソースを追え …

無料で自由に使える日本全国の企業データベースをPostgreSQLで作る

開発するシステムよっては一覧から企業を選択する機能があるかも知れません。「企業データベース」でググると沢山の商用企業データベースが出てきます。 「でも・・・お高いんでしょう?」 いいえ、なんと今なら・ …

テキストファイル内の改行コード差異を見つけるワンライナー

ファイル内容は同じなのにdiffで違いが出るWeb上でソースレビューする時に同じ内容の行なのに差分が出て紛らわしい これらの原因はWindows、Mac、Linuxが入り混じった、改行コードが統一され …

 

shingo.nakanishi
 

東京在勤、1977年生まれ、IT職歴2n年、生涯技術者として楽しく生きることを目指しています。デスマに負けず健康第一。