日本語対応が安心な opensource COBOL ではなく、あえて国際版の GnuCOBOL を Windows でビルドする手順。
大雑把な流れ
まず外部ライブラリをビルドする。README によると、GnuCOBOL には以下の外部ライブラリを使うが、必須なのは 1 のみ。1, 3, 4 はランタイムと開発コンポーネントの両方が必要。
- 【必須】GNU MP 4.1.2 以上 または MPIR 1.3.1 以上。10進計算に使う。Windows で gcc 以外を使う場合は MPIR 推奨。
- 【ふつういらない】libltdl。Windows では不要。動的呼び出しに使う OS もあるらしい。
- 【ISAM に必要】Berkeley DB 4.1 以上 または VBISAM 2.0 以上 または DISAM。Berkeley DB 最新版だと dll ファイル名などが変わっていてうまくいかなかったため、ここでは VBISAM を使う。VBISAM は opensource COBOL v1.5.2J にも同梱されているので実績ありげ。
- 【SCREEN などに必要】Ncurses、Unix curses、PDCurses のいずれか。https://www.osscons.jp/osscobol/download/v1_5j/ 見ると PDCurses が実績あるっぽい。
次に、GnuCOBOL そのものをビルドする。
最後に配布用ファイルを作成する。
MPIR を x86 でビルドしようとするとハマるっぽい記事を目にしたので、以下では x64 でビルドする。
Visual Studio 2017 の C++ ツールのインストール
- https://visualstudio.microsoft.com/ja/downloads/ の下のほうにある「古いバージョンのダウンロード」から、Visual Studio Community 2017 あたりをダウンロードする (ここでは Visual Studio Community 2017 (version 15.9) Japanese を使用)
- インストーラーで「C++ によるデスクトップ開発」(右の詳細画面で Windows 8.1 SDK and UCRT SDK も! MPIR のビルドで必要。)、「Python 開発」にチェックを入れてインストールする。Python は MPIR の msbuild.bat で +tests オプションを使う場合に必要。
7-Zip のインストール
.7z ファイルの作成や tar の展開に必要なため、7-Zip をインストールしておく
MPIR のビルド
- mpir-3.0.0.zip をダウンロードし展開する
- スタートメニューから Visual Studio 2017 > 開発者コマンド プロンプト を開く
-
cd c:\path\to\mpir-3.0.0\build.vc15
vc15 というのは Visual Studio 2017 の C++ ツールのバージョン。 -
msbuild.bat gc DLL x64 Release
(ほかに +tests オプションを付けられるが、時間がかかる)
VBISAM のビルド
- vbisam-2.0.tar.gz をダウンロードし展開する (7-Zip では .gz の展開後、さらに .tar を展開する必要がある)
- 先人の努力に敬意を払いつつ、opensource-cobol-1.5.2J.zip をダウンロードし展開し、opensource-cobol-1.5.2J\win32\vbisam から config.h と vbisam.def を vbisam-2.0\libvbisam にコピーする
- vbisam.def の1行目の vbisam を libvbisam に変更
- スタートメニューから Visual Studio 2017 > VS 2017 用 x64 Native Tools コマンド プロンプト を開く
cd C:\path\to\vbisam-2.0\libvbisam
cl /O2 /I .. /I . /LD *.c /Fe:libvbisam /link /DEF:vbisam.def
PDCurses のビルド
- https://github.com/wmcbrine/PDCurses/releases から PDCurses-3.9.zip をダウンロードし展開する
- スタートメニューから Visual Studio 2017 > VS 2017 用 x64 Native Tools コマンド プロンプト を開く
cd C:\path\to\PDCurses-3.9\wincon
- nmake -f Makefile.vc WIDE=Y DLL=Y
GnuCOBOL のビルド
もし上記の手順の MPIR 以外のライブラリのビルドに失敗していたら、以下の手順で展開された gnucobol-3.0-rc1\build_windows\README.txt を参考に、build_windows\config.h を編集すること。
gnucobol-3.0-rc1_win.7z をダウンロードし展開する
-
gnucobol-3.0-rc1_win\gnucobol-3.0-rc1\build_windows にヘッダを配置する
- gmp.h (C:\path\to\mpir-3.0.0\build.vc15\dll_mpir_gc\x64\Release\gmp.h)
- vbisam.h (C:\path\to\vbisam-2.0\vbisam.h)
- vbdecimal.h (C:\path\to\vbisam-2.0\vbdecimal.h)
- curses.h (C:\path\to\PDCurses-3.9\curses.h)
- curspriv.h (C:\path\to\PDCurses-3.9\curspriv.h)
-
gnucobol-3.0-rc1_win\gnucobol-3.0-rc1\build_windows\x64 を作成し、lib を配置する (README.txt の説明は誤りみたい)
- mpir.lib (C:\path\to\mpir-3.0.0\build.vc15\dll_mpir_gc\x64\Release\mpir.lib)
- libvbisam.lib (C:\path\to\vbisam-2.0\libvbisam\libvbisam.lib)
- pdcurses.lib (C:\path\to\PDCurses-3.9\wincon\pdcurses.lib)
-
gnucobol-3.0-rc1_win\gnucobol-3.0-rc1\build_windows\x64\Release を作成し、dll を配置する
- mpir.dll (C:\path\to\mpir-3.0.0\build.vc15\dll_mpir_gc\x64\Release\mpir.dll)
- libvbisam.dll (C:\path\to\vbisam-2.0\libvbisam\libvbisam.dll)
- pdcurses.dll (C:\path\to\PDCurses-3.9\wincon\pdcurses.dll)
スタートメニューから Visual Studio 2017 > 開発者コマンド プロンプト を開く
cd C:\path\to\gnucobol-3.0-rc1_win\gnucobol-3.0-rc1\build_windows\vs2017
MSBuild GnuCOBOL.sln /p:Platform=x64 /p:Configuration=Release
0 Error(s)
なら OK!
配布パッケージの作成
cd C:\path\to\gnucobol-3.0-rc1\build_windows
- makedist.bat を実行
- Everything is Ok と出る
- GnuCOBOL_3.0-dev_vs_bin.7z ができる。
配布パッケージの展開
- 配布先に Visual Studio 2017 (または以前)の「C++ によるデスクトップ開発」をインストールする。
- 配布先に 7-Zip をインストールする
- 配布パッケージを展開する (ルートに大量のファイルがあるので、展開の際は「ここに展開」をしないように注意)。
set_env_vs_x64.bat というファイルができているので、コマンドプロンプトを開いた後にこれを実行すると、Visual Studio のツールや COBOL コンパイラにパスが通る。このファイルは Visual Studio 2019 には未対応。
テスト
環境設定バッチの実行
コマンドプロンプトを開き、以下を実行する。
C:\path\to\set_env_vs_x64.bat
hello world
以下のような hello.cob を作成する。
program-id. hello.
procedure division.
display 'hello, world!'.
環境設定バッチの実行済みのコマンドプロンプトで、
cobc -x --free hello.cob
hello.exe
で "hello, world!" と出れば勝利。
ISAM
同じく以下の isam.cob を作成し、コンパイルして実行する。
program-id. isam.
environment division.
input-output section.
file-control.
select isam
assign to 'isam'
access mode is random
organization is indexed
record key is pkey.
data division.
file section.
fd isam.
01 isam-rec.
02 pkey pic 9(2).
02 nam pic x(8) value space.
procedure division.
isam-write section.
open output isam.
move 1 to pkey.
move 'one' to nam.
write isam-rec.
move 2 to pkey.
move 'two' to nam.
write isam-rec.
move 3 to pkey.
move 'three' to nam.
write isam-rec.
close isam.
isam-read section.
open input isam.
move 2 to pkey.
read isam
display nam.
close isam.
環境設定バッチの実行済みのコマンドプロンプトで、
cobc -x --free isam.cob
isam.exe
で、two と出れば勝利。
SCREEN
同じく以下の screen.cob を作成し、コンパイルして実行する。
program-id. screen.
environment division.
configuration section.
special-names.
console is crt.
data division.
screen section.
01 mylabel column 3 line 3 pic x(18)
value 'hello from screen!'.
procedure division.
main section.
display mylabel.
環境設定バッチの実行済みのコマンドプロンプトで、
cobc -x --free screen.cob
screen.exe
で、
hello from screen!
end of program, please press a key to exit
のように出れば、勝利? SCREEN の使い方を知らなくて加減なサンプルなので、少々自信がない。
最後に
挑戦したけれどうまく行かなかった場合、コメントしてみてください。うまくいったけれどここの表現がわかりにくかったなどのコメントもぜひ。
あと https://www.arnoldtrembley.com/GnuCOBOL.htm に、バージョンは少し違うし Visual Studio のビルドではないけれど、install binaries なんてあるみたいなので、ビルドに失敗した人は、コマンド プロンプトで set_env.cmd を叩いてから cobc を実行すればコンパイルできるのではないかと思う。
ぼっちなのでハッカソンには参加できませんでしたが、うちの環境でたまたま問題なかった手順の誤りを AWS のおかげでみつけることができました。