Open3

warp: 共有ライブラリのワークフロー整理

okuokuokuoku

そもそも、どういう機能性をSDKとしてサポートしないといけないのか考えたい。 ...いやまぁWebAssemblyのRPATHとかまだ誰も使ってないと思うけど。。

okuokuokuoku

基本的な形を作る(1-pass integration)

とりあえずビルドを通す。

https://github.com/okuoku/shlibtest/blob/7370e2f9836215226d80ce160be56d9d885b098e/testmod/CMakeLists.txt#L1-L8

シンボルのexport/import属性はプラットフォーム毎に異なるので専用の移植性ヘッダが必要になる。CMakeは専用のモジュール GenerateExportHeader を提供している(何故?) https://cmake.org/cmake/help/latest/module/GenerateExportHeader.html

https://github.com/okuoku/shlibtest/blob/7370e2f9836215226d80ce160be56d9d885b098e/include/addtwo.h

このようにしてビルドすると、LinuxのようなELFプラットフォームではビルドディレクトリから直接実行できる。(RPATHが設定されるため)

  {
    "directory": "/home/okuoku/repos/shlibtest/buildinteg/_build",
    "command": ": && /usr/bin/cc   mainexec/CMakeFiles/mainexec.dir/main.c.o -o mainexec/mainexec  -Wl,-rpath,/home/okuoku/repos/shlibtest/buildinteg/_build/testmod  testmod/libaddtwo.so && :",
    "file": "mainexec/CMakeFiles/mainexec.dir/main.c.o",
    "output": "mainexec/mainexec"
  },

逆に、 この性質があるため、install()しないと正常なexecutableを得られない という問題がある。

okuokuokuoku

install()を書く

https://github.com/okuoku/shlibtest/commit/a4c561c2e123a9f41a6bb8d3dffc5eac4dea54fd

単純に1-passビルドをwrapするだけならやることはそこまで多くなく、

  1. <ライブラリ名>Config.cmake を用意する。内容は include("${CMAKE_CURRENT_LIST_DIR}/<ライブラリ名>Targets.cmake") だけ 空で良い
  2. <ライブラリ名>Targets.cmakeexport コマンドで生成する ライブラリをALIASしておく
  3. CMAKE_PREFIX_PATH に上記の .cmake ファイルの生成先を追加する
  4. find_package を呼ぶ (こうすることで2-passビルド時も同じCMakeLists.txtを使える)
  5. リンクするライブラリの名前をnamespaceされたものに替えておく

↑のコミットだと無からビルドするときにエラーになる:

okuoku@stripe:~/repos/shlibtest/buildinteg/_build$ cmake .
-- Prefix = /home/okuoku/repos/shlibtest/buildinteg/_build/pkgs
CMake Error at _build/pkgs/ShlibtestAddtwoConfig.cmake:1 (include):
  include could not find requested file:

    /home/okuoku/repos/shlibtest/buildinteg/_build/pkgs/ShlibtestAddtwoTargets.cmake
Call Stack (most recent call first):
  /home/okuoku/repos/shlibtest/mainexec/CMakeLists.txt:4 (find_package)

CMP0024 https://cmake.org/cmake/help/latest/policy/CMP0024.html はALIASを使えといってるのでそうする。。

https://github.com/okuoku/shlibtest/commit/c29d67fc7be5c1e85a6d2952095f111fedef1884