Linux 環境で LWJGL ライブラリをビルドしてみる
2013 年 4 月 21 日に LWJGL 2.9.0 がリリースされました。maven リポジトリにも ver 2.9.0 がアップされているので、maven を使う場合には pom.xml のバージョン番号を更新するだけで新しい jar ファイルを入手することができます。今回はそこからさらに一歩踏み込んで、自分で LWJGL をビルドしてみたいと思います。環境はいつも通り Linux (Fedora17) です。LWJGL のビルドでは、jar ファイルの他に JNI として呼び出されるネイティブライブラリも生成されます。これはビルド環境用のライブラリのみビルドされるので、Windows 用のネイティブライブラリを作りたい場合は Windows でビルドしなければなりません。
また、LWJGL はビルドプロセス中で Annotation Processing Tool を使ってソースコードを自動生成しているため、リポジトリ内に必要なすべてのソースコードがコミットされている訳ではありません。もしリポジトリからソースコードを持ってきて自分で LWJGL の開発環境を作るのであれば、一度ビルドしてから eclipse などの IDE に読み込ませてください。
ビルドに必要なものをインストールする
LWJGL のビルドには、java の他にもいくつか必要なツールがあります。まずはこれらを用意してください。git
git は LWJGL の git リポジトリからソースコードを取ってくるのに必要になります。yum でインストールできます。# yum install -y gitLWJGL のリポジトリも時代の流れを受けてか github に移行したようです。
ちなみに source forge の旧 svn リポジトリも現段階では残ってはいますが、2013 年 1 月頃からあまりコミット(git からのマージなど)されなくなっているので、素直に git リポジトリを見に行った方が良さそうです。
ant
ビルドスクリプトを動かすためのツールです。LWJGL では JNI で呼び出すネイティブライブラリのビルドも含まれるため、maven ではなくこちらを採用しているようです。ant も yum コマンドでインストールできます。
# yum install -y antただ、私は一緒に openjdk もインストールされるのが嫌(jdk は oracle のものを使っています)なので、公式ページからアーカイブを取ってきて、自分で PATH を通しました。
各種ヘッダーファイル
LWJGL のネイティブライブラリをビルドするのに、ヘッダーファイル等が必要になります。Fedora 17 にはデフォルトインストールではこれらが含まれないようで、C 言語のコンパイルエラーを解決するのがなかなか面倒でした。以下のパッケージをインストールすることでビルドできるので、まとめて入れておいてください。
# yum install -y \ libX11-devel \ libXt-devel \ libXcursor-devel \ libXxf86vm-devel \ libXrandr-develネイティブライブラリのビルドエラー時のメッセージはトラブルシューティングのために末尾に載せておきます。
LWJGL をビルドする
ビルド手順はとてもシンプルです。LWJGL のリポジトリを git clone してソースコードを取ってきたら、あとは ant を叩くだけです。
$ git clone https://github.com/LWJGL/lwjgl.git $ cd lwjgl $ ant
ビルドが成功すると、libs 下に成果物が生成されます。libs ディレクトリ内にはあらかじめビルドされた jar ファイルが配置されていますが、ビルド後にはタイムスタンプが更新されているはずです。
$ ll libs/ 合計 2024 -rwxrwxr-x 1 momokan momokan 4189 6月 24 21:35 AppleJavaExtensions.jar -rw-rw-r-- 1 momokan momokan 321450 6月 24 21:35 asm-debug-all.jar -rw-rw-r-- 1 momokan momokan 214859 6月 24 21:35 jinput.jar drwxrwxr-x 3 momokan momokan 4096 6月 24 22:19 linux -rw-rw-r-- 1 momokan momokan 996627 6月 24 22:24 lwjgl.jar -rw-rw-r-- 1 momokan momokan 283392 6月 24 22:24 lwjgl_test.jar -rw-rw-r-- 1 momokan momokan 173360 6月 24 22:24 lwjgl_util.jar -rw-rw-r-- 1 momokan momokan 36200 6月 24 22:24 lwjgl_util_applet.jar -rw-rw-r-- 1 momokan momokan 5762 6月 24 21:35 lzma.jar drwxrwxr-x 2 momokan momokan 4096 6月 24 21:35 macosx drwxrwxr-x 2 momokan momokan 4096 6月 24 21:35 solaris drwxrwxr-x 2 momokan momokan 4096 6月 24 21:35 windowslinux, windows, macosx などのディレクトリにはネイティブライブラリが入っています。
ネイティブライブラリはビルドする環境用のものだけが作られるので、64bit Linux 環境での成果物は libs/linux/liblwjgl64.so になります。
ビルドをやり直したい場合には、ant の clean ターゲットを実行すれば、ビルド途中のものを綺麗に削除できます。
$ ant clean
LWJGL ネイティブライブラリのビルドエラー集
ネイティブライブラリのビルドに必要なヘッダーファイルがないなどでコンパイルエラーとなる場合、自分で必要なヘッダーファイルを用意する必要があります。私の環境で発生したエラーのメッセージと、エラーを解決するのに必要なパッケージをトラブるシューティングのため載せておきます。
ちなみに、あれこれビルドを試していたのは 32 bit Fedora 17 だったので、ディレクトリ名等は 32 bit 用のものになっています。
X11/Xlib.h
エラーメッセージcompile32: [mkdir] Created dir: /home/momokan/workspace/lwjgl/bin/lwjgl/x32 [apply] In file included from /home/momokan/workspace/lwjgl/src/native/common/org_lwjgl_opengl_AWTSurfaceLock.c:45:0: [apply] /usr/java/jdk1.6.0_35/jre/../include/linux/jawt_md.h:11:22: 致命的エラー: X11/Xlib.h: そのようなファイルやディレクトリはありません [apply] コンパイルを停止しました。必要なパッケージ: libX11-devel
X11/Intrinsic.h
エラーメッセージcompile32: [mkdir] Created dir: /home/momokan/workspace/lwjgl/bin/lwjgl/x32 [apply] In file included from /home/momokan/workspace/lwjgl/src/native/common/org_lwjgl_opengl_AWTSurfaceLock.c:45:0: [apply] /usr/java/jdk1.6.0_35/jre/../include/linux/jawt_md.h:13:27: 致命的エラー: X11/Intrinsic.h: そのようなファイルやディレクトリはありません [apply] コンパイルを停止しました。必要なパッケージ: libXt-devel
X11/Xcursor/Xcursor.h
エラーメッセージcompile32: [apply] /home/momokan/workspace/lwjgl/src/native/generated/opencl/org_lwjgl_opencl_CL12.c: 関数 ‘Java_org_lwjgl_opencl_CL12_nclCompileProgramMulti’ 内: [apply] /home/momokan/workspace/lwjgl/src/native/generated/opencl/org_lwjgl_opencl_CL12.c:87:3: 警告: 1 番目の ‘strlen’ の引数を渡すときのポインタの先の符号が異なります [-Wpointer-sign] [apply] In file included from /home/momokan/workspace/lwjgl/src/native/common/common_tools.h:44:0, [apply] from /home/momokan/workspace/lwjgl/src/native/common/extcl.h:44, [apply] from /home/momokan/workspace/lwjgl/src/native/generated/opencl/org_lwjgl_opencl_CL12.c:4: [apply] /usr/include/string.h:399:15: 備考: expected ‘const char *’ but argument is of type ‘cl_char *’ [apply] /home/momokan/workspace/lwjgl/src/native/linux/org_lwjgl_input_Cursor.c:44:33: 致命的エラー: X11/Xcursor/Xcursor.h: そのようなファイルやディレクトリはありません [apply] コンパイルを停止しました。必要なパッケージ: libXcursor-devel
X11/extensions/xf86vmode.h
エラーメッセージcompile32: [apply] /home/momokan/workspace/lwjgl/src/native/generated/opencl/org_lwjgl_opencl_CL12.c: 関数 ‘Java_org_lwjgl_opencl_CL12_nclCompileProgramMulti’ 内: [apply] /home/momokan/workspace/lwjgl/src/native/generated/opencl/org_lwjgl_opencl_CL12.c:87:3: 警告: 1 番目の ‘strlen’ の引数を渡すときのポインタの先の符号が異なります [-Wpointer-sign] [apply] In file included from /home/momokan/workspace/lwjgl/src/native/common/common_tools.h:44:0, [apply] from /home/momokan/workspace/lwjgl/src/native/common/extcl.h:44, [apply] from /home/momokan/workspace/lwjgl/src/native/generated/opencl/org_lwjgl_opencl_CL12.c:4: [apply] /usr/include/string.h:399:15: 備考: expected ‘const char *’ but argument is of type ‘cl_char *’ [apply] /home/momokan/workspace/lwjgl/src/native/linux/org_lwjgl_opengl_LinuxKeyboard.c: 関数 ‘Java_org_lwjgl_opengl_LinuxKeyboard_keycodeToKeySym’ 内: [apply] /home/momokan/workspace/lwjgl/src/native/linux/org_lwjgl_opengl_LinuxKeyboard.c:81:2: 警告: ‘XKeycodeToKeysym’ は廃止されました (宣言位置 /usr/include/X11/Xlib.h:1695) [-Wdeprecated-declarations] [apply] /home/momokan/workspace/lwjgl/src/native/linux/opengl/display.c:44:38: 致命的エラー: X11/extensions/xf86vmode.h: そのようなファイルやディレクトリはありません [apply] コンパイルを停止しました。必要なパッケージ: libXxf86vm-devel
X11/extensions/Xrandr.h
エラーメッセージcompile32: [apply] /home/momokan/workspace/lwjgl/src/native/generated/opencl/org_lwjgl_opencl_CL12.c: 関数 ‘Java_org_lwjgl_opencl_CL12_nclCompileProgramMulti’ 内: [apply] /home/momokan/workspace/lwjgl/src/native/generated/opencl/org_lwjgl_opencl_CL12.c:87:3: 警告: 1 番目の ‘strlen’ の引数を渡すときのポインタの先の符号が異なります [-Wpointer-sign] [apply] In file included from /home/momokan/workspace/lwjgl/src/native/common/common_tools.h:44:0, [apply] from /home/momokan/workspace/lwjgl/src/native/common/extcl.h:44, [apply] from /home/momokan/workspace/lwjgl/src/native/generated/opencl/org_lwjgl_opencl_CL12.c:4: [apply] /usr/include/string.h:399:15: 備考: expected ‘const char *’ but argument is of type ‘cl_char *’ [apply] /home/momokan/workspace/lwjgl/src/native/linux/org_lwjgl_opengl_LinuxKeyboard.c: 関数 ‘Java_org_lwjgl_opengl_LinuxKeyboard_keycodeToKeySym’ 内: [apply] /home/momokan/workspace/lwjgl/src/native/linux/org_lwjgl_opengl_LinuxKeyboard.c:81:2: 警告: ‘XKeycodeToKeysym’ は廃止されました (宣言位置 /usr/include/X11/Xlib.h:1695) [-Wdeprecated-declarations] [apply] /home/momokan/workspace/lwjgl/src/native/linux/opengl/display.c:45:35: 致命的エラー: X11/extensions/Xrandr.h: そのようなファイルやディレクトリはありません [apply] コンパイルを停止しました。必要なパッケージ: libXrandr-devel
ヘッダーファイルが含まれるパッケージの探し方
yum コマンドの provides オプションを使うと、特定のファイルがどのパッケージに含まれているかを検索することができます。例えば X11/extensions/Xrandr.h を探したい場合は以下のようにして検索します。
# yum provides '*/X11/extensions/Xrandr.h' 読み込んだプラグイン:langpacks, presto, refresh-packagekit rpmfusion-free-updates/filelists_db | 246 kB 00:00 rpmfusion-nonfree-updates/filelists_db | 285 kB 00:01 updates/filelists_db | 15 MB 00:04 libXrandr-devel-1.3.1-3.fc17.i686 : X.Org X11 libXrandr development package リポジトリー : fedora 一致 : ファイル名 : /usr/include/X11/extensions/Xrandr.h libXrandr-devel-1.3.1-3.fc17.x86_64 : X.Org X11 libXrandr development package リポジトリー : fedora 一致 : ファイル名 : /usr/include/X11/extensions/Xrandr.h libXrandr-devel-1.3.1-3.fc17.x86_64 : X.Org X11 libXrandr development package リポジトリー : @fedora 一致 : ファイル名 : /usr/include/X11/extensions/Xrandr.h上にあげたエラーメッセージ以外のコンパイルエラーが発生した場合には、参考にしてみてください。
Maven 用の jar ファイルとしてビルドする
ここまでで LWJGL 自体のビルドはできますが、他の maven プロジェクトでビルドしたライブラリを使うには、更に Maven 用の jar ファイルとしてビルドする必要があります。LWJGL のソースコードには maven で使える jar ファイルを作るためのビルドスクリプトが用意されているので、これを使うことで自分でビルドしたオレオレ LWJGL を、ローカル環境の maven リポジトリにインストールすることができます。ここからは 2014/02/01 の追記になります。
ビルドする LWJGL は 2.9.2 開発用、ビルド環境は Fedora 20 です。
とはいえ、2.9.0 の頃とそれほどバージョン差による影響はありません。
ビルドする LWJGL は 2.9.2 開発用、ビルド環境は Fedora 20 です。
とはいえ、2.9.0 の頃とそれほどバージョン差による影響はありません。
Maven ant tasks をインストールする
このビルドスクリプトは内部で ant から maven を呼び出して使用する箇所があるため、Maven ant tasks をビルド環境にインストールしておく必要があります。現時点での最新版は maven-ant-tasks-2.1.3.jar です。これをダウンロードしたら、~/.ant/lib/ 下に配置し、ant からタスクとして呼び出せるようにしておきます。$ mkdir -p ~/.ant/lib/ $ cd ~/.ant/lib/ $ wget http://www.apache.org/dyn/closer.cgi/maven/ant-tasks/2.1.3/binaries/maven-ant-tasks-2.1.3.jar
ant が Maven タスクを認識できないと、出来上がった jar ファイルをローカルリポジトリにインストールする段階で、以下のエラーが発生します。
$ ant install Buildfile: /home/momokan/workspace2/lwjgl/temp/maven/build.xml install: execute-install: BUILD FAILED /home/momokan/workspace2/lwjgl/temp/maven/build.xml:7: The following error occurred while executing this line: /home/momokan/workspace2/lwjgl/temp/maven/build.xml:105: Problem: failed to create task or type antlib:org.apache.maven.artifact.ant:mvn Cause: The name is undefined. Action: Check the spelling. Action: Check that any custom tasks/types have been declared. Action: Check that any <presetdef>/<macrodef> declarations have taken place. No types or tasks have been defined in this namespace yet This appears to be an antlib declaration. Action: Check that the implementing library exists in one of: -/usr/java/ant/lib -/home/momokan/.ant/lib -a directory added on the command line with the -lib argument Total time: 0 seconds
ビルドするライブラリのバージョンを変更する
ビルドするオレオレ LWJGL ライブラリのバージョンを任意のバージョンに指定しておきます。本家で公開されているバージョンと同じ値にしてしまうと、同じバージョンの jar のはずがローカル環境だけ動作が異なるというような混乱を引き起こしかねないので、必ず違うバージョンを設定しておきましょう。ビルドするライブラリのバージョンは、platform_build/build-definitions.xml 内の lwjgl.version というプロパティの値として設定します。
1 <project name="definitions"> 2 <!-- ================================================================== --> 3 <!-- Global properties for build --> 4 <!-- ================================================================== --> ... 15 <property name="lwjgl.version" value="2.9.1.momokan" /> ...ここでは、2.9.1.momokan としておきます。
Maven 用にビルドしてローカルリポジトリにインストールする
まず、オレオレの LWJGL ライブラリを Maven 用の jar ファイルとしてビルドするには、プロジェクトのトップディレクトリで ant の maven-full ターゲットを実行します。$ ant maven-full -Doverridejinput=true-Doverridejinput=true を指定しない場合、以下のエラーが発生します。
... -checkjinputversion: [script] JINPUT Version: 2.0.0-b01 - DeclaredVersion: 2.0.5 [script] JINPUT Version don't match BUILD FAILED /home/momokan/workspace2/lwjgl/platform_build/build-maven.xml:83: The following error occurred while executing this line: /home/momokan/workspace2/lwjgl/platform_build/build-maven.xml:73: Jinput version in project (2.0.0-b01) is different from the declared jinput version for maven (2.0.5) add -Doverridejinput=true as a command line option to avoid this check Total time: 29 secondsエラーメッセージを読む限り、これは JInput のバージョンチェックに関するエラーのようです。LWJGL 自体のソースコードが開発中のもの(master ブランチのもの)なので、それの影響で出ているのかもしれません。
次にビルドした Maven 用の jar ファイルを、maven のローカルリポジトリにインストールします。
$ cd temp/maven/ $ ant install今度はプロジェクトのトップではなく、temp/maven ディレクトリで実行してください。
maven のローカルリポジトリを覗いてみると、オレオレ LWJGL ライブラリがインストールされているのがわかります。
$ ls -1 ~/.m2/repository/org/lwjgl/lwjgl/lwjgl/ 2.9.1 2.9.1.momokan maven-metadata-local.xml $ ls -1 ~/.m2/repository/org/lwjgl/lwjgl/lwjgl/2.9.1.momokan/ lwjgl-2.9.1.momokan-javadoc.jar lwjgl-2.9.1.momokan-sources.jar lwjgl-2.9.1.momokan.jar lwjgl-2.9.1.momokan.pom
あとはいつも通り、LWJGL を使う側のプロジェクト(ゲーム本体のほう)でビルドしたバージョンの LWJGL を使うように設定するだけです。
pom.xml の lwjgl.jar のバージョンを修正し、mvn clean eclipse:eclipse してみてください。
LWJGL, Linux コメント (0) 2013/06/24 23:49:28