mono/packages/media/cpp/CMakeLists.txt
2026-04-12 23:17:00 +02:00

152 lines
4.3 KiB
CMake

cmake_minimum_required(VERSION 3.20)
project(media-image-service
VERSION 0.1.0
DESCRIPTION "Polymech image resize service (CLI, REST, IPC) — libvips"
LANGUAGES CXX
)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/dist")
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG "${CMAKE_SOURCE_DIR}/dist")
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE "${CMAKE_SOURCE_DIR}/dist")
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELWITHDEBINFO "${CMAKE_SOURCE_DIR}/dist")
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_MINSIZEREL "${CMAKE_SOURCE_DIR}/dist")
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
# Windows: official libvips dev tree (see scripts/fetch-vips-windows.ps1) — vips-dev-* under third_party/
if(WIN32)
file(GLOB _MEDIA_VIPS_SDK "${CMAKE_CURRENT_SOURCE_DIR}/third_party/vips-dev-*")
if(_MEDIA_VIPS_SDK)
list(APPEND CMAKE_PREFIX_PATH ${_MEDIA_VIPS_SDK})
endif()
endif()
include(FetchContent)
set(JSON_BuildTests OFF CACHE BOOL "" FORCE)
FetchContent_Declare(
cli11
GIT_REPOSITORY https://github.com/CLIUtils/CLI11.git
GIT_TAG v2.4.2
GIT_SHALLOW TRUE
)
FetchContent_Declare(
asio
GIT_REPOSITORY https://github.com/chriskohlhoff/asio.git
GIT_TAG asio-1-28-0
GIT_SHALLOW TRUE
)
FetchContent_Declare(
nlohmann_json
GIT_REPOSITORY https://github.com/nlohmann/json.git
GIT_TAG v3.11.3
GIT_SHALLOW TRUE
)
FetchContent_Declare(
cpp_httplib
GIT_REPOSITORY https://github.com/yhirose/cpp-httplib.git
GIT_TAG v0.16.3
GIT_SHALLOW TRUE
)
FetchContent_MakeAvailable(cli11 asio nlohmann_json cpp_httplib)
# laserpants/dotenv-cpp — load .env (same pattern as packages/kbot/cpp).
FetchContent_Declare(
laserpants_dotenv
GIT_REPOSITORY https://github.com/laserpants/dotenv-cpp.git
GIT_TAG master
GIT_SHALLOW TRUE
)
FetchContent_GetProperties(laserpants_dotenv)
if(NOT laserpants_dotenv_POPULATED)
FetchContent_Populate(laserpants_dotenv)
endif()
add_library(laserpants_dotenv INTERFACE)
target_include_directories(laserpants_dotenv INTERFACE ${laserpants_dotenv_SOURCE_DIR}/include)
add_library(laserpants::dotenv ALIAS laserpants_dotenv)
find_package(Vips REQUIRED)
add_executable(media-img
src/main.cpp
src/core/resize.cpp
src/http/serve.cpp
src/ipc/ipc_serve.cpp
)
target_include_directories(media-img PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/src
${asio_SOURCE_DIR}/asio/include
)
target_compile_definitions(media-img PRIVATE
ASIO_STANDALONE
ASIO_NO_DEPRECATED
CPPHTTPLIB_NO_EXCEPTIONS=0
)
if(WIN32)
target_compile_definitions(media-img PRIVATE
_WIN32_WINNT=0x0A00
NOMINMAX
)
endif()
target_link_libraries(media-img PRIVATE
CLI11::CLI11
nlohmann_json::nlohmann_json
httplib::httplib
laserpants::dotenv
Vips::vips
)
# GObject (g_object_ref / g_object_unref) — not re-exported through libvips import lib on MSVC.
if(WIN32)
file(GLOB _MEDIA_VIPS_SDK_LIST "${CMAKE_CURRENT_SOURCE_DIR}/third_party/vips-dev-*")
if(_MEDIA_VIPS_SDK_LIST)
list(GET _MEDIA_VIPS_SDK_LIST 0 _MEDIA_VIPS_SDK)
if(EXISTS "${_MEDIA_VIPS_SDK}/lib/libgobject-2.0.lib")
target_link_libraries(media-img PRIVATE
"${_MEDIA_VIPS_SDK}/lib/libgobject-2.0.lib"
"${_MEDIA_VIPS_SDK}/lib/libglib-2.0.lib"
)
endif()
endif()
endif()
if(UNIX AND NOT APPLE)
target_link_libraries(media-img PRIVATE pthread)
endif()
target_compile_definitions(media-img PRIVATE
"MEDIA_IMG_VERSION=\"${PROJECT_VERSION}\""
)
# Runtime: libvips and deps are DLLs next to media-img.exe (Windows dev bundle bin/).
if(WIN32)
file(GLOB _MEDIA_VIPS_SDK_LIST "${CMAKE_CURRENT_SOURCE_DIR}/third_party/vips-dev-*")
if(_MEDIA_VIPS_SDK_LIST)
list(GET _MEDIA_VIPS_SDK_LIST 0 _MEDIA_VIPS_SDK)
if(EXISTS "${_MEDIA_VIPS_SDK}/bin")
add_custom_command(
TARGET media-img
POST_BUILD
COMMAND "${CMAKE_COMMAND}" -E copy_directory
"${_MEDIA_VIPS_SDK}/bin"
"${CMAKE_SOURCE_DIR}/dist"
COMMENT "Copy libvips DLLs to dist/"
)
endif()
endif()
endif()