Search code examples
fluttercmakeflutter-windows

Flutter windows: can I use relative path to bundle `.dll` library in CMakeLists.txt?


I am building a flutter plugin which calls native functions from lib.dll file and everything works as expected in my computer.

But I use relative path to link that lib such as

E:/_Projects/mahesabu/client/packages/server/windows/lib.dll

Now I want to move the build process in CI/CD which I believe using relative path such as ./lib.dll would be very easy.

Of cource I am new to cmake configuration. And in one comment it is written

List of absolute paths to libraries that should be bundled with the plugin

I wonder how can I use relative path there, because if I try build fails. The following is CMakeLists.txt

cmake_minimum_required(VERSION 3.14)
set(PROJECT_NAME "server")
project(${PROJECT_NAME} LANGUAGES CXX)

# This value is used when generating builds using this plugin, so it must
# not be changed
set(PLUGIN_NAME "server_plugin")

add_library(${PLUGIN_NAME} SHARED
  "server_plugin.cpp"
)
apply_standard_settings(${PLUGIN_NAME})
set_target_properties(${PLUGIN_NAME} PROPERTIES
  CXX_VISIBILITY_PRESET hidden)
target_compile_definitions(${PLUGIN_NAME} PRIVATE FLUTTER_PLUGIN_IMPL)
target_include_directories(${PLUGIN_NAME} INTERFACE
  "${CMAKE_CURRENT_SOURCE_DIR}/include")
target_link_libraries(${PLUGIN_NAME} PRIVATE flutter flutter_wrapper_plugin)

# List of absolute paths to libraries that should be bundled with the plugin
set(server_bundled_libraries
  ""
  "E:/_Projects/mahesabu/client/packages/server/windows/lib.dll" #USE RELATIVE PATH HERE
  PARENT_SCOPE
)

Any help will be appreciated.


Solution

  • Just use:

    set(server_bundled_libraries "${CMAKE_CURRENT_SOURCE_DIR}/lib.dll" PARENT_SCOPE)
    

    The CMAKE_CURRENT_SOURCE_DIR variable will expand to current source directory as tracked by add_subdirectory. This is usually, but not always, the directory in which the present CMakeLists.txt resides. Presumably, this is E:/_Projects/mahesabu/client/packages/server/windows on your computer (given your remark that you expect ./lib.dll to work), but will be somewhere else on CI or elsewhere.