mozc_emacs_helperをMacOS (Sonoma, Apple M1)でビルドし、Emacsからmozcを使う

はじめに

mozc_emacs_helperをビルドしてEmacsからmozcを使いたかったときのログ。

Mozcのビルド手順

あらかじめ、XCodeやBazelのインストールは済ませておく。

リポジトリをクローンし、Mozcをビルド&インストールする。

$ python3 -m pip install requests
$ git clone https://github.com/google/mozc.git
$ cd mozc/src
$ python3 build_tools/update_deps.py
$ python3 build_tools/build_qt.py --release --confirm_license
$ MOZC_QT_PATH=${PWD}/third_party/qt bazel build package --config oss_macos -c opt
$ open bazel-bin/mac/Mozc.pkg

これが終わったら念のため再起動。

mozc_emacs_helperのビルド手順

Google 日本語入力と連携させたい場合、あらかじめインストールしておく。

ninjaのインストール。

$ brew install ninja

以下はおまじない:

$ sudo xcode-select -s /Applications/Xcode.app/Contents/Developer
$ sudo ln -s /opt/homebrew/bin/python3 /usr/local/bin/python

これらをやっておかないとコンパイル時にエラーになったので。特に後者はビルド中にpythonを参照しようとするため、python3のバイナリを参照するようにシンボリックリンクを張った。

以下を参考にsrc/build_mozc.pyおよびsrc/mac/mac.gypを書き換える。

続いてSDKのバージョン確認。

$ ls /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs

実行例は下図のようになった。バージョンは14.0であった。

SDKのバージョン確認

さらに続いてGoogle 日本語入力のバージョンを確認する。下図より、2.28.5050.1 であった。

図:Google 日本語入力のバージョン確認

上記で確認したバージョンをもとに、src/data/version/mozc_version_template.bzlを書き換える。

MAJOR = 2

# MINOR = 29
MINOR = 28

# BUILD number used for the OSS version.
# BUILD_OSS = 5291
BUILD_OSS = 5050

# Number to be increased. This value may be replaced by other tools.
BUILD = BUILD_OSS

# Represent the platform and release channel.
# REVISION = 100
REVISION = 1

確認したSDKのバージョン(14.0)およびMacOS自体のバージョン(このときのSonomaは14.2.1だった)を指定してビルドする(注意: --noqt の後ろにもコマンドは続いている!)。

$ GYP_DEFINES="mac_sdk=14.0 mac_deployment_target=14.2" python3 build_mozc.py gyp --noqt --branding=GoogleJapaneseInput
$ python3 build_mozc.py build -c Release unix/emacs/emacs.gyp:mozc_emacs_helper

動作確認。

$ echo -e '(0 CreateSession)\n(1 SendKey 1 97)' | out_mac/Release/mozc_emacs_helper

バイナリをパスの通った場所にコピーする。

$ sudo cp out_mac/Release/mozc_emacs_helper /usr/local/bin

もっとも、--branding=GoogleJapaneseInputの指定をしなければ、Google 日本語入力との連携は発生しない。ゆえにmozc_version_template.bzlやmac.gypの書き換えは必要ない。その状態でhelperをビルドしても確かに動作する。指定はお好みで。

Emacsの設定

mozc.elはpackage-installからインストールできる。 ついでにmozc-popupもインストールしておく。

設定は例えばこんな感じに。

;; 言語設定 & mozc
(set-language-environment "Japanese")
(setq default-input-method "japanese-mozc")
(prefer-coding-system 'utf-8)

;; 要 mozc-popup <- package-install
(require 'mozc-popup)
(setq mozc-candidate-style 'popup)

また、mozc-tempによるモードレスな入力も試してみると良いだろう。そのご利益は、わざわざ日本語入力開始時/終了時にtoggle-input-methodを発動する必要がない点である。

mozc-tempを使ったモードレス日本語入力が快適 :: 四時随順

参考

xcode-select: error: tool 'xcodebuild' requires Xcode, but active developer directory '/Library/Developer/CommandLineTools' is a command line tools instance · Issue #569 · nodejs/node-gyp · GitHub

emacs with mozc on macos sierra · GitHub