mieki256's diary



2026/03/04(水) [n年前の日記]

#2 [nim] nimble build について

Nim には nimble というパッケージ管理ツール?があるけれど、これを使ってビルドの指示もできるらしい。

_NimWorld | パッケージ
_Nimble入門 #入門 - Qiita

使用ライブラリを指定 :

.nimble には、そのアプリが利用するライブラリも記述しておかないといけない。

今回は以下のような記述にした。
# Dependencies

requires "nim >= 2.2.8"
requires "winim"
requires "opengl"
  • winim と opengl を利用してるので、requires "xxxx" といった形で列挙していく。
  • バージョン指定もできる。上記の例では、nim は 2.2.8 以上が必要、という記述になっている。

コンパイル前後の処理を指定 :

コンパイルをする前と、した後に、何かしらの処理をさせることもできる。before build: もしくは after build: という指定を使う。

今回は以下のような記述になった。コンパイルする前にリソースファイルの変換を行い、コンパイルした後に .exe を .scr としてコピーしてる。
before build:
  echo "--- [Task] Compiling Resource File ---"
  exec "windres resource.rc -O coff -o resource.res"

after build:
  echo "--- [Task] Creating Screensaver File ---"
  cpFile("ssnimsample.exe", "ssnimsample.scr")
  echo "Done: 'ssnimsample.scr' has been updated successfully."
  • echo "hoge" でコンソールにメッセージ "hoge" を出せる。
  • exec "hoge.exe" で処理を実行する。上記の例では windres を使って、リソースファイル .rc を .res に変換している。
  • cpFile("SOURCE.exe", "DIST.scr") でファイルのコピーができる。

この状態で nimble build と打ったら、リソースファイルを作ってから、コンパイルをして .exe ができて、その .exe が .scr としてコピーされた。

特定の処理を指定 :

.nimble は、タスクと呼ばれる一連の処理を記述していくこともできる。

例えば、以下の記述をすると…。
task hello, "Debug test":
  echo "Hello! Nimble is reading this task correctly."

nimble tasks で、利用できるタスクの一覧が表示される。
> nimble tasks
hello      Debug test

nimble hello と打ってみると…。メッセージが表示された。task hello で指定した処理が行われている。
> nimble hello
  Executing task hello in D:\home2\prg\nim\_public\ssnimsample\ssnimsample\ssnimsample.nimble
Hello! Nimble is reading this task correctly.


生成ファイルを全消去するタスクを用意してみる。いわゆる make clean。

ここでちょっと注意。Makefile に慣れていると、タスク名として clean を記述したくなるけれど、clean というタスク名は既に予約されているので別のタスク名にしないといけない。今回は distclean としてみた。
task distclean, "Remove intermediate files and generated .scr":
  echo "Cleaning project..."
  
  if fileExists("ssnimsample.scr"):
    rmFile("ssnimsample.scr")
    echo "Deleted: ssnimsample.scr"
    
  if fileExists("ssnimsample.exe"):
    rmFile("ssnimsample.exe")
    echo "Deleted: ssnimsample.exe"
    
  if fileExists("resource.res"):
    rmFile("resource.res")
    echo "Deleted: resource.res"
    
  if dirExists("nimcache"):
    rmDir("nimcache")
    echo "Deleted: nimcache directory"
    
  echo "Clean up process completed."
  • if fileExists("hoge.exe"): で、hoge.exe が存在していたら、という指定になる。
  • rmFile() でファイル削除。
  • rmDir() でディレクトリ削除。

nimble distclean と打つと、.scr, .exe, .res が削除された。
> nimble distclean
  Executing task distclean in D:\home2\prg\nim\_public\ssnimsample\ssnimsample\ssnimsample.nimble
Cleaning project...
Deleted: ssnimsample.scr
Deleted: ssnimsample.exe
Deleted: resource.res
Clean up process completed.

以上です。

過去ログ表示

Prev - 2026/03 -
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31

カテゴリで表示

検索機能は Namazu for hns で提供されています。(詳細指定/ヘルプ


注意: 現在使用の日記自動生成システムは Version 2.19.6 です。
公開されている日記自動生成システムは Version 2.19.5 です。

Powered by hns-2.19.6, HyperNikkiSystem Project