macOSでmltermをインストールする-もしくは、ビルドできなかった話

~~今回も最終目標はhomebrew formulaの作成です。~~
~~まず、手動でビルドしてみます。~~

そのつもりだったのですがビルドできず、配布バイナリは使用できたのでそちらを使っています。
配布バイナリからのセットアップは別途記事にする予定です。
こちらは記録用兼報告用として記事を残しておきます。

---


# 前提

依存パッケージ:

  - `fribidi` -- homebrewから入れる
  - `gtk+3` -- homebrew。GTK+2とどちらがいいのか悩んだのですが、`configure`で3.0を使うオプションがあったのであげてみました。
  - `xquartz` -- homebrew cask。X11の後継みたいなやつ...だっけ...??
  - `libssh2` -- homebrew。`ssh`とどう違うのかわかないですが`2`が必要な模様。
  - `gdk-pixbuf` -- homebrew。


```:原文
  All you need for basic compilation is libc and Xlib.  You don't need
  internationalization support of OS because mlterm has own i18n code.

  XFree86 4.0.2 or above and FreeType 2.0.2 or above are needed for
  anti-alias.

  gdk-pixbuf (Gtk+ 2.0.1 or higher) for background image.

  Gtk+ (2.x ?) for GUI configurator "mlconfig" and "mlterm-menu".

  Gtk+ (2.x ?) for libvte compatible library. (see gtk/INSTALL)

  Fribidi (0.9.0 - ?) for Bidi.

  libssh2 (http://www.libssh2.org) for ssh2 client and scp.
  (If you want to use camellia as common key cryptography, download libssh2
   camellia branch from https://bitbucket.org/arakiken/libssh2/downloads)
```


# ﾁｮｯﾄﾀﾞｹ成功例

なんかわからないけどできました。(ただし完璧ではない)

```bash
$ ./configure CFLAGS="-I/opt/X11/include/freetype2" --with-type-engines=cairo --prefix=/Users/Cj-bc/workspace/mlterm/mlterm-3.8.7/OUT --with-gui=quartz --with-gtk=3.0
$ make
$ make install
$ cp -R cocoa/mlterm.app ~/
$ $(top_srcdir)/cocoa/install.sh /Users/Cj-bc/workspace/mlterm/mlterm-3.8.7/OUT
```

makeログ全文: https://gist.github.com/6caf3561dd5b3bce6df35f5772f6d0c5

ただしこれだとまだ表示がおかしい問題があります:

https://twitter.com/Cj_bc_sd/status/1087504033886031872

# 失敗例

## README通りに実行

以下のようにして実行

```
$ ./configure --with-gui=quartz --prefix /Users/Cj-bc/workspace/mlterm/mlterm-3.8.7/OUT --with-type-engines=cairo --with-gtk=3.0
$ make
```

しかしmakeで失敗

```bash
$ make
...
ld: warning: ignoring file ../uitoolkit/libuitoolkit.a, file was built for archive which is not the architecture being linked (x86_64): ../uitoolkit/libuitoolkit.a
ld: warning: ignoring file ../vtemu/libmlterm.a, file was built for archive which is not the architecture being linked (x86_64): ../vtemu/libmlterm.a
Undefined symbols for architecture x86_64:
  "_global_args", referenced from:
      _main in main-cocoa.o
  "_ui_customize_font_file", referenced from:
      _main_loop_init in main_loop.o
  "_ui_event_source_add_fd", referenced from:
      _daemon_init in daemon.o
  "_ui_event_source_final", referenced from:
      _main_loop_final in main_loop.o
  "_ui_event_source_init", referenced from:
      _main_loop_init in main_loop.o
  "_ui_event_source_process", referenced from:
      _main_loop_start in main_loop.o
  "_ui_font_use_point_size", referenced from:
      _main_loop_init in main_loop.o
  "_ui_main_config_init", referenced from:
      _main_loop_init in main_loop.o
  "_ui_mlclient", referenced from:
      _client_connected in daemon.o
  "_ui_prepare_for_main_config", referenced from:
      _main_loop_init in main_loop.o
  "_ui_screen_manager_final", referenced from:
      _main_loop_final in main_loop.o
  "_ui_screen_manager_init", referenced from:
      _main_loop_init in main_loop.o
  "_ui_screen_manager_startup", referenced from:
      _main_loop_start in main_loop.o
  "_ui_set_font_size_range", referenced from:
      _main_loop_init in main_loop.o
  "_ui_set_mod_meta_prefix", referenced from:
      _main_loop_final in main_loop.o
  "_vt_set_word_separators", referenced from:
      _main_loop_final in main_loop.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[1]: *** [mlterm] Error 1
make: *** [all] Error 2
```

### warningを探してみる

`configure`時に何かあったのかなと思い、とりあえずwarningだけ抽出

```bash
<X_X>:mlterm-3.8.7$ cat config.log | grep warning
warning: (x86_64)  could not find object file symbol for symbol _main
ld: warning: ignoring file ./libconftest.a, file was built for archive which is not the architecture being linked (x86_64): ./libconftest.a
warning: unknown remark option '-R/usr/X11/lib' [-Wunknown-warning-option]
1 warning generated.
warning: unknown remark option '-R' [-Wunknown-warning-option]
1 warning generated.
conftest.c:28:6: warning: incompatible redeclaration of library function 'pow' [-Wincompatible-library-redeclaration]
1 warning generated.
conftest.c:29:37: warning: null passed to a callee that requires a non-null argument [-Wnonnull]
conftest.c:29:37: warning: null passed to a callee that requires a non-null argument [-Wnonnull]
2 warnings generated.
```

...しかしこれでは本当に出た警告なのか、それとも確認の都合で出ている正常なものなのかがわからないので前後5行も出力させてみます。

```bash
<X_X>:mlterm-3.8.7$ cat config.log | grep --context=5 --color=always warning
configure:6693: gcc -c -g -O2  conftest.c >&5
configure:6696: $? = 0
configure:6700: /usr/local/bin/nm -B conftest.o \| sed -n -e 's/^.*[ ]\([ABCDGIRSTW][ABCDGIRSTW]*\)[ ][ ]*_\([_A-Za-z][_A-Za-z0-9]*\)$/\1 _\2 \2/p' | sed '/ __gnu_lto/d' \> conftest.nm
configure:6703: $? = 0
configure:6769: gcc -o conftest -g -O2   conftest.c conftstm.o >&5
warning: (x86_64)  could not find object file symbol for symbol _main
configure:6772: $? = 0
configure:6810: result: ok
configure:6857: checking for sysroot
configure:6887: result: no
configure:6894: checking for a working dd
--
--
gcc -g -O2 -c -o conftest.o conftest.c
ar cru libconftest.a conftest.o
ar: `u' modifier ignored since `D' is the default (see `U')
ranlib libconftest.a
gcc -g -O2  -o conftest conftest.c -Wl,-force_load,./libconftest.a
ld: warning: ignoring file ./libconftest.a, file was built for archive which is not the architecture being linked (x86_64): ./libconftest.a
configure:7944: result: no
configure:8021: checking how to run the C preprocessor
configure:8052: gcc -E  conftest.c
configure:8052: $? = 0
configure:8066: gcc -E  conftest.c
--
--
|   return 0;
| }
configure:16138: result: libraries /usr/X11/lib, headers /usr/X11/include
configure:16158: checking whether -R must be followed by a space
configure:16174: gcc -o conftest -g -O2   conftest.c  -R/usr/X11/lib >&5
warning: unknown remark option '-R/usr/X11/lib' [-Wunknown-warning-option]
1 warning generated.
configure:16174: $? = 0
configure: failed program was:
| /* confdefs.h */
| #define PACKAGE_NAME ""
| #define PACKAGE_TARNAME ""
--
--
|
|   ;
|   return 0;
| }
configure:16191: gcc -o conftest -g -O2   conftest.c  -R /usr/X11/lib >&5
warning: unknown remark option '-R' [-Wunknown-warning-option]
1 warning generated.
ld: can't map file, errno=22 file '/usr/X11/lib' for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
configure:16191: $? = 1
configure: failed program was:
| /* confdefs.h */
--
--
configure:17554: $PKG_CONFIG --exists --print-errors "freetype2"
configure:17557: $? = 0
configure:17595: result: yes
configure:17719: checking for pow in -lm
configure:17744: gcc -o conftest -g -O2   conftest.c -lm   >&5
conftest.c:28:6: warning: incompatible redeclaration of library function 'pow' [-Wincompatible-library-redeclaration]
char pow ();
     ^
conftest.c:28:6: note: 'pow' is a builtin with type 'double (double, double)'
--
char pow ();
     ^
conftest.c:28:6: note: 'pow' is a builtin with type 'double (double, double)'
1 warning generated.
configure:17744: $? = 0
configure:17753: result: yes
configure:18063: checking regex.h usability
configure:18063: gcc -c -g -O2  conftest.c >&5
configure:18063: $? = 0
--
--
configure:20215: gcc -o conftest -g -O2   conftest.c  >&5
configure:20215: $? = 0
configure:20223: result: yes
configure:20241: checking for CFPreferencesCopyAppValue
configure:20259: gcc -o conftest -g -O2   conftest.c  -Wl,-framework -Wl,CoreFoundation >&5
conftest.c:29:37: warning: null passed to a callee that requires a non-null argument [-Wnonnull]
CFPreferencesCopyAppValue(NULL, NULL)
                          ~~~~      ^
--
CFPreferencesCopyAppValue(NULL, NULL)
                          ~~~~      ^
conftest.c:29:37: warning: null passed to a callee that requires a non-null argument [-Wnonnull]
CFPreferencesCopyAppValue(NULL, NULL)
                                ~~~~^
--
CFPreferencesCopyAppValue(NULL, NULL)
                                ~~~~^
2 warnings generated.
configure:20259: $? = 0
configure:20268: result: yes
configure:20275: checking for CFLocaleCopyCurrent
configure:20293: gcc -o conftest -g -O2   conftest.c  -Wl,-framework -Wl,CoreFoundation >&5
configure:20293: $? = 0
```

以上のログから、正常そうなものを判別すると残りのwarningは:

```diff
  warning: (x86_64)  could not find object file symbol for symbol _main
  ld: warning: ignoring file ./libconftest.a, file was built for archive which is not the architecture being linked (x86_64): ./libconftest.a
- warning: unknown remark option '-R/usr/X11/lib' [-Wunknown-warning-option]
- 1 warning generated.
- warning: unknown remark option '-R' [-Wunknown-warning-option]
- 1 warning generated.
  conftest.c:28:6: warning: incompatible redeclaration of library function 'pow' [-Wincompatible-library-redeclaration]
- 1 warning generated.
  conftest.c:29:37: warning: null passed to a callee that requires a non-null argument [-Wnonnull]
  conftest.c:29:37: warning: null passed to a callee that requires a non-null argument [-Wnonnull]
- 2 warnings generated.
```

これについてはもう、Google先生のお力を頼ることに。


## freetypeを指定する

参考: http://yskwkzhr.blogspot.com/2014/04/use-mlterm-to-display-sixel-graphics-on-osx-mavericks.html

明らかに違うやろワレと思いつつとりあえず試してみます。

```bash
sudo ln -s /opt/X11/include/freetype2/freetype /opt/X11/include/freetype
./configure CFLAGS="-I/opt/X11/include/freetype2" --with-gui=quartz --prefix /Users/Cj-bc/workspace/mlterm/mlterm-3.8.7/OUT --with-type-engines=cairo --with-gtk=3.0
```

相変わらずエラーは変わらず...


## --with-imagelib=gdk-pixbufを追加する

参考: https://github.com/macports/macports-ports/blob/master/x11/mlterm/Portfile

macportsでは生きているらしいので、まぁこれも明らか違うやろと思ったけれどとりあえず試してみます。

```bash
./configure CFLAGS="-I/opt/X11/include/freetype2" --with-gui=quartz --prefix /Users/Cj-bc/workspace/mlterm/mlterm-3.8.7/OUT --with-type-engines=cairo --with-gtk=3.0 --with-imagelib=gdk-pixbuf
```

まぁ変わらないですね。

## オプションを最低限にする

オプションを最低限の`--prefix`のみにしてビルド
一応例のエラーは出なくなったのだがそれでも何かが足りないのか、他のエラーが吐かれるようになりました。

吐かれるエラー:

```bash
ar: libuitoolkit.a: Inappropriate file type or format
make[1]: *** [libuitoolkit.a] Error 1
```

全文: https://gist.github.com/ccd791f328ff3834700c452d2cba2e9e

ちなみに、この後はオプションを一つづつ指定し直していっていましたが、`

quartzを付け足した場合: https://gist.github.com/194c5233f63a7730118e0392e5cd85d3

## homebrew版(GNU)のar/ranlibを一時的に消す

参考: https://stackoverflow.com/questions/22107616/static-library-built-for-archive-which-is-not-the-architecture-being-linked-x86
    : https://qiita.com/nagomiso/items/dc6021beb72d09f2128f
    : http://naoyat.hatenablog.jp/entry/2012/01/31/033312

調べているとどうやら、macに標準であるar/ranlibと、binutilで入れているものが混ざっていると良くないというのを見かけたので、binutilを外してみます。

```bash
$ brew unlink binutil
$ ./configure CFLAGS="-I/opt/X11/include/freetype2" --with-gui=quartz --prefix /Users/Cj-bc/workspace/mlterm/mlterm-3.8.7/OUT --with-type-engines=cairo --with-gtk=3.0 --with-imagelib=gdk-pixbuf
```

## なんかちょっとうまくいった

なんかわからないけどコンパイル成功。但し、画面はおかしい...

```bash
$ ./configure CFLAGS="-I/opt/X11/include/freetype2" --with-type-engines=cairo --prefix=/Users/Cj-bc/workspace/mlterm/mlterm-3.8.7/OUT --with-gui=quartz --with-gtk=3.0
$ make
$ make install
$ cp -R cocoa/mlterm.app ~/
$ $(top_srcdir)/cocoa/install.sh /Users/Cj-bc/workspace/mlterm/mlterm-3.8.7/OUT
```

makeログ全文: https://gist.github.com/6caf3561dd5b3bce6df35f5772f6d0c5

表示がおかしい問題:

https://twitter.com/Cj_bc_sd/status/1087504033886031872

## gettextのパスを通す

`msgmerge`なるものがないというエラーが吐かれており、gettextのものだったようなのでパスを通す。
どうやら、homebrewのgettextはkeg only（macで同名のものが提供され、紛らわしいので実行可能ファイルやライブラリをパスが通ったところにインストールしない）のようでそのせいで見つからなかったようです。

```
$ PATH="/usr/local/opt/gettext/bin:$PATH" ./configure CPPFLAGS="-I/usr/local/opt/gettext/include" LDFLAGS="-L/usr/local/opt/gettext/lib" --with-gtk=3.0 --with-gui=quartz --prefix /Users/Cj-bc/workspace/mlterm/mlterm-3.8.7/OUT
```

...でもこれでもパス通らなかった...

## gtkのバージョンを2.0にしてみる

表示関係ということで`quartz`か`gtk`の問題かなと思い、比較的簡単にできる`gtk`のバージョン変更をしてみました

```bash
$ ./configure CFLAGS="-I/opt/X11/include/freetype2" --with-type-engines=cairo --prefix=/Users/Cj-bc/workspace/mlterm/mlterm-3.8.7/OUT --with-gui=quartz --with-gtk=2.0
$ make
$ make install
$ cp -R cocoa/mlterm.app ~/
$ $(top_srcdir)/cocoa/install.sh /Users/Cj-bc/workspace/mlterm/mlterm-3.8.7/OUT
```

...が、相変わらず表示はおかしい

https://twitter.com/Cj_bc_sd/status/1087614868243439616

## CFLAGSをいじる

どこかの記事でこれとは違う`freetype`を参照させているものがあったので試してみます。
元記事の通りにシンボリックリンク貼っているけど別に直接にしても問題ない気がしています。

```bash
$ sudo ln -s /opt/X11/include/freetype2/freetype /opt/X11/include/freetype
$ ./configure CFLAGS="-I/opt/X11/include/freetype" --with-type-engines=cairo --prefix=/Users/Cj-bc/workspace/mlterm/mlterm-3.8.7/OUT --with-gui=quartz --with-gtk=2.0
$ make
$ make install
$ cp -R cocoa/mlterm.app ~/
$ $(top_srcdir)/cocoa/install.sh /Users/Cj-bc/workspace/mlterm/mlterm-3.8.7/OUT
```

が、別段何も変わらなかった...

## ./cocoa/install.shで見つからなかった.soファイルを配置する


インストール手順の中に、`cocoa/install.sh`を使う場面があるのですが、そこでいくつかファイルが見つからないと怒られます。

```
<X_X>:mlterm-3.8.7$ ./cocoa/install.sh /Users/Cj-bc/workspace/mlterm/mlterm-3.8.7/OUT
cp: /Users/Cj-bc/workspace/mlterm/mlterm-3.8.7/OUT/lib/libmlterm_core.dylib: No such file or directory
cp: /Users/Cj-bc/workspace/mlterm/mlterm-3.8.7/OUT/lib/mlterm/libctl_iscii.so: No such file or directory
cp: /Users/Cj-bc/workspace/mlterm/mlterm-3.8.7/OUT/lib/mlterm/libind_bengali.so: No such file or directory
cp: /Users/Cj-bc/workspace/mlterm/mlterm-3.8.7/OUT/lib/mlterm/libind_hindi.so: No such file or directory
cp: /Users/Cj-bc/workspace/mlterm/mlterm-3.8.7/OUT/lib/mlterm/libotl-quartz.so: No such file or directory
Update lib/libpobl.1.dylib
Update lib/libmef.2.dylib
Update lib/libmlterm_coreotl.dylib
Update lib/mlterm/libctl_bidi.so
Update lib/mlterm/libotl-quartz.so
Update lib/mlterm/libim-skk.so
Update lib/mlterm/libim-kbd.so
Update mlterm
Update mlconfig
```

とりあえず本当に存在しないのかを探してみます:

```bash
$ find . -name libmlterm_core.dylib
$ find . -name libctl_iscii.so
$ find . -name libind_bengali.so
$ find . -name libind_hindi.so
$ find . -name libotl-quartz.so
./uitoolkit/libotl/.libs/libotl-quartz.so
```

... **一個だけはパス違うけどあるやんけ！！！**

ということでこれも処理に含めるようにします。
一度手動でやったのですが面倒になったので素直にシンボリックリンク貼ります。

```bash
$ ln -s uitoolkit/libotl/.libs/libotl-quartz.so lib/mlterm/libotl-quartz.so
$ ./cocoa/install.sh /Users/Cj-bc/workspace/mlterm/mlterm-3.8.7/OUT
```

これの結果:

https://twitter.com/Cj_bc_sd/status/1087646557149659136

---