Haxeに入門してみる
最近Blenderを勉強し始めました。 Blenderでゲーム作成、というと、実は今はArmoryという別のプロジェクト(プラグイン?)としてゲーム制作エンジンが有るようです。 こいつがプログラミング言語として標準でHaxeを利用できそうなので、じゃあちょっとHaxe触ってみるか?ということでHello Worldです。 Haxe
セットアップ
リポジトリ提供してるからaptとかからインストールできるよ!とのことですが、バージョン上げて動かなくなったりした際に戻したりするのが大変なので、今回はバイナリをダウンロードしました。 とりえず公式からLinux 64-bit Binariesをダウンロード。 私は以下のような感じにしました。
[koji:haxe]$ pwd
/home/koji/opt/haxe
[koji:haxe]$ ln -s haxe_20180221160843_bb7b827 haxe
[koji:haxe]$
[koji:haxe]$ ls -al haxe/
合計 11308
drwxr-xr-x 3 koji koji 4096 2月 21 2018 .
drwxrwxr-x 3 koji koji 4096 7月 24 12:17 ..
-rw-r--r-- 1 koji koji 86286 2月 21 2018 CHANGES.txt
-rw-r--r-- 1 koji koji 2651 2月 21 2018 CONTRIB.txt
-rw-r--r-- 1 koji koji 16461 2月 21 2018 LICENSE.txt
-rwxr-xr-x 1 koji koji 11218115 2月 21 2018 haxe
-rwxr-xr-x 1 koji koji 233122 2月 21 2018 haxelib
drwxr-xr-x 15 koji koji 4096 2月 21 2018 std
[koji:haxe]$
どうもバイナリは2つだけのようです。
さらに、NekoVMという物が必要らしいのでダウンロードします。
実態は共有ライブラリのようなのでLD_LIBRARY_PATH
も一緒に通しておきます。
[koji:haxe]$ pwd
/home/koji/opt/haxe
[koji:haxe]$ wget https://github.com/HaxeFoundation/neko/releases/download/v2-2-0/neko-2.2.0-linux64.tar.gz
[koji:haxe]$ tar zxvf neko-2.2.0-linux64.tar.gz
[koji:haxe]$ ln -s neko-2.2.0-linux64 neko
ということでこのhexe
、neko
ディレクトリにパスを通して、neko
ディレクトリをLD_LIBRARY_PATHに追加します。
私は.zshenv
に記述しました。
export HAXE_HOME="/home/koji/opt/haxe/haxe"
export HAXE_STD_PATH=$HAXE_HOME/std
export NEKOVM_HOME="/home/koji/opt/haxe/neko"
...抜粋...
path=(
...抜粋...
$HAXE_HOME
$NEKOVM_HOME
)
typeset -xT LD_LIBRARY_PATH ld_library_path
typeset -U ld_library_path
ld_library_path=(
$NEKOVM_HOME
$ld_library_path
)
こんな感じですね。
それぞれコマンドを叩いてみると以下の用にちゃんと動作していることがわかります。
[koji:~]$ haxe
Haxe Compiler 3.4.7 - (C)2005-2017 Haxe Foundation
Usage : haxe -main <class> [-swf|-js|-neko|-php|-cpp|-cppia|-as3|-cs|-java|-python|-hl|-lua] <output> [options]
Options :
-cp <path> : add a directory to find source files
-js <file> : compile code to JavaScript file
-lua <file> : compile code to Lua file
-swf <file> : compile code to Flash SWF file
-as3 <directory> : generate AS3 code into target directory
-neko <file> : compile code to Neko Binary
-php <directory> : generate PHP code into target directory
-cpp <directory> : generate C++ code into target directory
-cppia <file> : generate Cppia code into target file
-cs <directory> : generate C# code into target directory
-java <directory> : generate Java code into target directory
-python <file> : generate Python code as target file
-hl <file> : compile HL code as target file
-xml <file> : generate XML types description
-main <class> : select startup class
-lib <library[:version]> : use a haxelib library
-D <var[=value]> : define a conditional compilation flag
-v : turn on verbose mode
-debug : add debug information to the compiled code
-help : show extended help information
--help : show extended help information
--help-defines : print help for all compiler specific defines
--help-metas : print help for all compiler metadatas
<dot-path> : compile the module specified by dot-path
[koji:~]$
[koji:~]$ haxelib
Haxe Library Manager 3.3.0 - (c)2006-2017 Haxe Foundation
Usage: haxelib [command] [options]
Basic
install : install a given library, or all libraries from a hxml file
update : update a single library (if given) or all installed libraries
remove : remove a given library/version
list : list all installed libraries
set : set the current version for a library
Information
search : list libraries matching a word
info : list information on a given library
user : list information on a given user
config : print the repository path
path : give paths to libraries
version : print the currently used haxelib version
help : display this list of options
Development
submit : submit or update a library package
register : register a new user
dev : set the development directory for a given library
git : use Git repository as library
hg : use Mercurial (hg) repository as library
Miscellaneous
setup : set the haxelib repository path
newrepo : create a new local repository
deleterepo: delete the local repository
convertxml: convert haxelib.xml file to haxelib.json
run : run the specified library with parameters
proxy : setup the Http proxy
Available switches
--flat : do not use --recursive cloning for git
--always : answer all questions with yes
--debug : run in debug mode, imply not --quiet
--quiet : print less messages, imply not --debug
--system : run bundled haxelib version instead of latest update
--never : answer all questions with no
--global : force global repo if a local one exists
[koji:~]$
haxelibのセットアップ
haxelib
というHaxe用のパッケージマネージャが有るようです。
こいつは最初に、パッケージをダウンロードして保存するディレクトリを指定しておく必要が有るようです。
これまたシステム領域を汚したくないので、今回はとりあえずhaxeと同じディレクトリにします。
[koji:haxe]$ pwd
/home/koji/opt/haxe
[koji:haxe]$ mkdir haxe_librarieis
[koji:haxe]$ haxelib setup /home/koji/opt/haxe/haxe_librarieis
haxelib repository is now /home/koji/opt/haxe/haxe_librarieis
[koji:haxe]$
実行
以下のようなコードを、Main.hx
というファイル名で保存します。
class Main {
static public function main(): Void {
trace("こんにちわ世界1");
Sys.println("こんにちわ世界2");
trace("こんにちわ世界2");
}
}
で、同じディレクトリでhaxe -main Main --interp
を実行します。
[koji:haxe]$ haxe -main Main --interp
Main.hx:3: こんにちわ世界1
こんにちわ世界2
Main.hx:5: こんにちわ世界3
[koji:haxe]$
問題なく日本語も扱える状態でHaxeが動きました。
別のターゲットにコンパイル
Neko
[koji:haxe]$ haxe -main Main -neko neko.n
[koji:haxe]$ ls
Main.hx neko.n
[koji:haxe]$ neko neko.n
Main.hx:3: こんにちわ世界1
こんにちわ世界2
Main.hx:5: こんにちわ世界3
[koji:haxe]$
Java
まずは必要なライブラリをインストール
[koji:haxe]$ haxelib install hxjava
Downloading hxjava-3,2,0.zip...
Download complete : 4798202 bytes in 1s (4461.9KB/s)
Installing hxjava...
Current version is now 3.2.0
Done
そして実行!
[koji:haxe]$ haxe -main Main -java bin
haxelib run hxjava hxjava_build.txt --haxe-version 3407 --feature-level 1
javac "-sourcepath" "src" "-d" "obj" "-g:none" "@cmd"
[koji:haxe]$ ls
Main.hx bin neko.n
[koji:haxe]$
[koji:haxe]$ java -jar bin/Main.jar
Main.hx:3: こんにちわ世界1
こんにちわ世界2
Main.hx:5: こんにちわ世界3
[koji:haxe]$
Python
Javaと違ってPythonは何もインストールしなくてもOK
[koji:haxe]$ haxe -main Main -python main.py
[koji:haxe]$ python main.py
こんにちわ世界1
こんにちわ世界2
こんにちわ世界3
[koji:haxe]$
まとめ
とりあえずHaxeを動かせる状態にはなりました。 正直トランスパイル先の言語バージョンとか何処までどのタイミングで追随していってくれるの?等不安な部分はあります。 ただ、よく分かってはいないのですが、NekoVM自体はHaxe Foundationが開発しているらしく、その後継のHashLinkという仮想マシンも有るらしいので、Haxe自体の勉強はこれらのVM上で動かすことを前提で進めていこうと思います。
公開日:2019/07/24