spice vdagent cmake使用

spice vdagent cmake使用

近期开发spice vd_agent发现开源库已经将visual studio .sln项目文件删除。故需要使用cmake生成 .sln文件。
在此记录下编译步骤:
1. 下载cmake程序cmake下载地址 根据不同操作系统下载对应版本
spice vdagent cmake使用_第1张图片
2. CMakeLists.txt 进行修改,以下为修改后文件

cmake_minimum_required(VERSION 3.0)
if(${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.15")
    # this is for MSVC_RUNTIME_LIBRARY property below
    cmake_policy(SET CMP0091 NEW)
endif()
project(vdagent-win)

# configure
file(WRITE "${CMAKE_BINARY_DIR}/config.h.in" "/* Automatic generated by CMake */\n\n")

macro(config_write str)
    file(APPEND "${CMAKE_BINARY_DIR}/config.h.in" "${str}")
endmacro()

string(TIMESTAMP BUILD_YEAR "%Y")
config_write("#define BUILD_YEAR \"${BUILD_YEAR}\"\n")

if(EXISTS "${CMAKE_SOURCE_DIR}/.tarball-version")
    file(STRINGS "${CMAKE_SOURCE_DIR}/.tarball-version" VER)
    list(GET VER 0 VER)
    set(VER "v${VER}")
else()
    find_package(Git)
    if(GIT_FOUND)
        execute_process(
            COMMAND ${GIT_EXECUTABLE} describe --abbrev=4 --match=v* HEAD
            WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}"
            OUTPUT_VARIABLE VER
            ERROR_QUIET
            OUTPUT_STRIP_TRAILING_WHITESPACE
        )
    else()
        message(FATAL_ERROR "GIT not found")
    endif()
endif()

if("${CMAKE_VERSION}" MATCHES "^v([0-9]+)\\.([0-9]+)\\.([0-9]+)")
    set(RC_PRODUCTVERSION "${CMAKE_MATCH_1}, ${CMAKE_MATCH_2}, ${CMAKE_MATCH_3}, 0")
else()
    #message(FATAL_ERROR "wrong VERSION format ${VER}")
endif()
config_write("#define RC_PRODUCTVERSION ${RC_PRODUCTVERSION}\n")
config_write("#define RC_PRODUCTVERSION_STR \"${RC_PRODUCTVERSION}\"\n")

configure_file(${CMAKE_BINARY_DIR}/config.h.in ${CMAKE_BINARY_DIR}/config.h)

#set(png_DIR "D:\\vd_agent\\vd_agent-master\\")

if(MSVC)
    if(${CMAKE_VERSION} VERSION_LESS "3.15")
        # compile MSVCRT into executable (link it statically)
        foreach(flag_var
            CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE
            CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO)
            if(${flag_var} MATCHES "/MD")
                string(REGEX REPLACE "/MD" "/MT" ${flag_var} "${${flag_var}}")
            endif(${flag_var} MATCHES "/MD")
        endforeach(flag_var)
    endif()

    # generate PDB files
    # see https://stackoverflow.com/questions/28178978/how-to-generate-pdb-files-for-release-build-with-cmake-flags/31264946
    set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /Zi")
    set(CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} /DEBUG /OPT:REF /OPT:ICF" CACHE STRING "" FORCE)

    # find using vcpkg packages
    #find_package(png CONFIG REQUIRED)
    #find_path(PNG_INCLUDE_DIR NAMES png.h)

    set(PNG_LIBRARY png_static)
    set(ZLIB_LIBRARY)
    set(COMMSUPPW_LIBRARY comsuppw)
else(MSVC)
    set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static -s")

    find_package(PNG REQUIRED)
    set(COMMSUPPW_LIBRARY)
endif(MSVC)

include_directories(common ${CMAKE_BINARY_DIR} spice-protocol ${PNG_INCLUDE_DIR})
add_definitions(-DUNICODE -D_UNICODE -DOLDMSVCRT -DWINVER=0x0601)

add_executable(vdagent WIN32
    common/vdcommon.cpp
    common/vdcommon.h
    common/vdlog.cpp
    common/vdlog.h
    vdagent/display_configuration.cpp
    vdagent/display_configuration.h
    vdagent/desktop_layout.cpp
    vdagent/desktop_layout.h
    vdagent/display_setting.cpp
    vdagent/display_setting.h
    vdagent/file_xfer.cpp
    vdagent/file_xfer.h
    vdagent/vdagent.cpp
    vdagent/as_user.cpp
    vdagent/as_user.h
    vdagent/image.cpp
    vdagent/image.h
    vdagent/imagepng.cpp
    vdagent/imagepng.h
    vdagent/shell.cpp
    vdagent/shell.h
    vdagent/vdagent.rc
)
target_link_libraries(vdagent
    ${PNG_LIBRARY}
    ${ZLIB_LIBRARY}
    wtsapi32
    uuid
    ole32
    oleaut32
    ${COMMSUPPW_LIBRARY}
)

add_executable(vdservice
    common/stdint.h
    common/vdcommon.cpp
    common/vdcommon.h
    common/vdlog.cpp
    common/vdlog.h
    vdservice/vdservice.cpp
    vdservice/vdservice.rc
)
target_link_libraries(vdservice
    wtsapi32
)

enable_testing()
add_custom_target(check COMMAND ctest -C $)

if(CMAKE_CROSSCOMPILING)
    set(CMAKE_CROSSCOMPILING_EMULATOR wine)
endif()

add_executable(imagetest EXCLUDE_FROM_ALL
    common/vdcommon.cpp
    common/vdcommon.h
    common/vdlog.cpp
    common/vdlog.h
    vdagent/imagetest.cpp
    vdagent/image.cpp
    vdagent/image.h
    vdagent/imagepng.cpp
    vdagent/imagepng.h
)
target_link_libraries(imagetest
    ${PNG_LIBRARY}
    ${ZLIB_LIBRARY}
    wtsapi32
)
add_test(NAME test-png COMMAND bash "${CMAKE_SOURCE_DIR}/test-png")

add_executable(test-log EXCLUDE_FROM_ALL
    common/vdcommon.cpp
    common/vdcommon.h
    common/vdlog.cpp
    common/vdlog.h
    common/test-log.cpp
)
add_test(NAME test-log COMMAND test-log)

add_executable(test-shell EXCLUDE_FROM_ALL
    vdagent/test-shell.cpp
)
target_link_libraries(test-shell
    uuid
    ole32
    oleaut32
    ${COMMSUPPW_LIBRARY}
)
add_test(NAME test-shell COMMAND test-shell)

add_dependencies(check test-log test-shell imagetest)

if(MSVC)
    # select static CRT without debugging (/MT option)
    if(${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.15")
        set_property(TARGET vdagent vdservice imagetest
            PROPERTY MSVC_RUNTIME_LIBRARY "MultiThreaded")
    endif()
endif()

install(TARGETS vdagent vdservice)
  1. 使用cmake . -G “Visual Studio 12 2013” 生成 .sln 文件
  2. 编译第三方库 pnglib.lib
  3. 配置项目环境变量
  4. 编译

你可能感兴趣的:(编译工具,windows)