Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[QUESTION]how to use CPM include this project? #386

Open
WangHHY19931001 opened this issue Sep 1, 2022 · 8 comments
Open

[QUESTION]how to use CPM include this project? #386

WangHHY19931001 opened this issue Sep 1, 2022 · 8 comments

Comments

@WangHHY19931001
Copy link

cmake_minimum_required(VERSION 3.14 FATAL_ERROR)

project ("redis_cpp")
if(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
set(CMAKE_POLICY_DEFAULT_CMP0077 NEW)
endif()

set(CPM_DOWNLOAD_LOCATION "${PROJECT_SOURCE_DIR}/cmake/CPM.cmake")
set(CPM_GET_CPM_DOWNLOAD_LOCATION "${PROJECT_SOURCE_DIR}/cmake/get_cpm.cmake")

if(NOT EXISTS ${CPM_DOWNLOAD_LOCATION})
message(STATUS "Downloading CPM.cmake")
file(DOWNLOAD
https://github.com/cpm-cmake/CPM.cmake/releases/latest/download/CPM.cmake
${CPM_DOWNLOAD_LOCATION})
else()
message(STATUS "CPM.cmake exists pass download")
endif(NOT EXISTS ${CPM_DOWNLOAD_LOCATION})

if(NOT EXISTS ${CPM_GET_CPM_DOWNLOAD_LOCATION})
message(STATUS "Downloading get_cpm.cmake")
file(DOWNLOAD
https://github.com/cpm-cmake/CPM.cmake/releases/latest/download/get_cpm.cmake
${CPM_GET_CPM_DOWNLOAD_LOCATION})
else()
message(STATUS "get_cpm.cmake exists pass download")
endif(NOT EXISTS ${CPM_GET_CPM_DOWNLOAD_LOCATION})

#启用c++11
set(CMAKE_CXX_STANDARD 11)

include(GNUInstallDirs)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR})
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR})
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_BINDIR})

include(${CPM_DOWNLOAD_LOCATION})

CPMAddPackage(
NAME hiredis # <- use OpenSSL here
GITHUB_REPOSITORY redis/hiredis
GIT_TAG v1.0.2
VERSION v1.0.2
EXCLUDE_FROM_ALL true
OPTIONS
"BUILD_TESTING OFF"
"BUILD_SHARED_LIBS OFF"
"BUILD_STATIC_LIBS ON"
)

包含子项目。

set(ENABLE_SSL OFF)
set(REDIS_PLUS_PLUS_BUILD_STATIC ON)
set(REDIS_PLUS_PLUS_BUILD_SHARED OFF)
set(REDIS_PLUS_PLUS_BUILD_TEST OFF)
add_subdirectory("3rd/redis-plus-plus")

use cmake file like this, but not work。

1> 命令行: "C:\WINDOWS\system32\cmd.exe" /c "%SYSTEMROOT%\System32\chcp.com 65001 >NUL && "C:\PROGRAM FILES\MICROSOFT VISUAL STUDIO\2022\COMMUNITY\COMMON7\IDE\COMMONEXTENSIONS\MICROSOFT\CMAKE\CMake\bin\cmake.exe" -G "Ninja" -DCMAKE_BUILD_TYPE:STRING="Debug" -DCMAKE_INSTALL_PREFIX:PATH="C:\work\redis_cpp\redis_cpp\out\install\x64-Debug" -DVCPKG_TARGET_TRIPLET:STRING="x64-windows" -DCMAKE_C_COMPILER:FILEPATH="C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.33.31629/bin/Hostx64/x64/cl.exe" -DCMAKE_CXX_COMPILER:FILEPATH="C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.33.31629/bin/Hostx64/x64/cl.exe" -DCMAKE_MAKE_PROGRAM="C:\PROGRAM FILES\MICROSOFT VISUAL STUDIO\2022\COMMUNITY\COMMON7\IDE\COMMONEXTENSIONS\MICROSOFT\CMAKE\Ninja\ninja.exe" -DCMAKE_TOOLCHAIN_FILE="C:/vcpkg/scripts/buildsystems/vcpkg.cmake" "C:\work\redis_cpp\redis_cpp" 2>&1"
1> 工作目录: C:\work\redis_cpp\redis_cpp\out\build\x64-Debug
1> [CMake] -- CPM.cmake exists pass download
1> [CMake] -- get_cpm.cmake exists pass download
1> [CMake] -- CPM: adding package hiredis@v1.0.2 (v1.0.2)
1> [CMake] Detected version: 1.0.2
1> [CMake] -- redis-plus-plus version: 1.3.5
1> [CMake] -- redis-plus-plus build type: Debug
1> [CMake] -- redis-plus-plus build with CXX standard: c++17
1> [CMake] -- redis-plus-plus TLS support: OFF
1> [CMake] -- redis-plus-plus build static library: ON
1> [CMake] -- redis-plus-plus build static library with position independent code: ON
1> [CMake] -- redis-plus-plus build shared library: OFF
1> [CMake] -- redis-plus-plus build test: OFF
1> [CMake] -- Debian package name: .deb
1> [CMake] -- Configuring done
1> [CMake] CMake Error at 3rd/redis-plus-plus/CMakeLists.txt:162 (target_link_libraries):
1> [CMake] Target "redis++_static" links to:
1> [CMake]
1> [CMake] hiredis::hiredis
1> [CMake]
1> [CMake] but the target was not found. Possible reasons include:
1> [CMake]
1> [CMake] * There is a typo in the target name.
1> [CMake] * A find_package call is missing for an IMPORTED target.
1> [CMake] * An ALIAS target is missing.
1> [CMake]
1> [CMake]
1> [CMake]
1> [CMake] -- Generating done
1> [CMake] CMake Generate step failed. Build files cannot be regenerated correctly.

@sewenew
Copy link
Owner

sewenew commented Sep 1, 2022

I didn't used CPM before, and I did some research. Looks like it needs FetchContent support. However, so far, redis-plus-plus does not support FetchContent feature. So it looks like that you cannot compile it with CPM by now.

Regards

@WangHHY19931001
Copy link
Author

but use add_submodule also can't include, with hiredis be include by add_submodule

@WangHHY19931001
Copy link
Author

how can i do? just want include you project by src

@sewenew
Copy link
Owner

sewenew commented Sep 2, 2022

What do you mean by including redis-plus-plus by src?

B.T.W. instead of FetchContent, you can use ExternalProject feature of cmake to include redis-plus-plus. this issue gives an example, and hope it helpful for you.

Regards

@WangHHY19931001
Copy link
Author

with the issue

1> 命令行: "C:\WINDOWS\system32\cmd.exe" /c "%SYSTEMROOT%\System32\chcp.com 65001 >NUL && "C:\PROGRAM FILES\MICROSOFT VISUAL STUDIO\2022\COMMUNITY\COMMON7\IDE\COMMONEXTENSIONS\MICROSOFT\CMAKE\CMake\bin\cmake.exe" -G "Ninja" -DCMAKE_BUILD_TYPE:STRING="Debug" -DCMAKE_INSTALL_PREFIX:PATH="C:\work\redis_cpp\redis_cpp\out\install\x64-Debug" -DVCPKG_TARGET_TRIPLET:STRING="x64-windows" -DCMAKE_C_COMPILER:FILEPATH="C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.33.31629/bin/Hostx64/x64/cl.exe" -DCMAKE_CXX_COMPILER:FILEPATH="C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.33.31629/bin/Hostx64/x64/cl.exe" -DCMAKE_MAKE_PROGRAM="C:\PROGRAM FILES\MICROSOFT VISUAL STUDIO\2022\COMMUNITY\COMMON7\IDE\COMMONEXTENSIONS\MICROSOFT\CMAKE\Ninja\ninja.exe" -DCMAKE_TOOLCHAIN_FILE="C:/vcpkg/scripts/buildsystems/vcpkg.cmake" "C:\work\redis_cpp\redis_cpp" 2>&1"
1> 工作目录: C:\work\redis_cpp\redis_cpp\out\build\x64-Debug
1> [CMake] CMake Warning (dev) at C:/Program Files/Microsoft Visual Studio/2022/Community/Common7/IDE/CommonExtensions/Microsoft/CMake/CMake/share/cmake-3.23/Modules/ExternalProject.cmake:2147 (message):
1> [CMake] Policy CMP0114 is not set: ExternalProject step targets fully adopt their
1> [CMake] steps. Run "cmake --help-policy CMP0114" for policy details. Use the
1> [CMake] cmake_policy command to set the policy and suppress this warning.
1> [CMake]
1> [CMake] ExternalProject target 'hiredis' would depend on the targets for step(s)
1> [CMake] 'install' under policy CMP0114, but this is being left out for
1> [CMake] compatibility since the policy is not set.
1> [CMake] Call Stack (most recent call first):
1> [CMake] CMakeLists.txt:14 (ExternalProject_Add_StepTargets)
1> [CMake] This warning is for project developers. Use -Wno-dev to suppress it.
1> [CMake]
1> [CMake] -- Configuring done
1> [CMake] -- Generating done
1> [CMake] -- Build files have been written to: C:/work/redis_cpp/redis_cpp/out/build/x64-Debug

C:\work\redis_cpp\redis_cpp\out\build\x64-Debug\ninja : error : '../../install/x64-Debug/lib/libredis++.a', needed by 'app.exe', missing and no known rule to make it

@WangHHY19931001
Copy link
Author

cmake_minimum_required(VERSION 3.14)

project(app)
set(CMAKE_CXX_STANDARD 17)
include(GNUInstallDirs)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR})
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR})
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_BINDIR})

include(FetchContent)

用于在cmake低于3.14是使用FetchContent_MakeAvailable

set(FetchContentHelp_LOCATION "${PROJECT_SOURCE_DIR}/cmake/FetchContentHelp.cmake")
include(${FetchContentHelp_LOCATION})

FetchContent_Declare(
hiredis
URL https://github.com/redis/hiredis/archive/refs/tags/v1.0.2.zip
)
#这种方式需要用cmake 3.14
FetchContent_MakeAvailable(hiredis)

FetchContent_getproperties(hiredis INSTALL_DIR)
set(HIREDIS_HEADER_PATH ${INSTALL_DIR}/include)
set(HIREDIS_LIB_PATH ${INSTALL_DIR}/lib)

FetchContent_Declare(
redis++
URL https://github.com/sewenew/redis-plus-plus/archive/refs/tags/1.3.5.zip
)

FetchContent_MakeAvailable(redis++)

FetchContent_getproperties(redis++ INSTALL_PATH)
set(REDIS_PLUS_PLUS_HEADER_PATH ${INSTALL_DIR}/include)
set(REDIS_PLUS_PLUS_LIB_PATH ${INSTALL_DIR}/lib)

add_executable(app app.cpp)
target_include_directories(app PRIVATE ${HIREDIS_HEADER_PATH} ${REDIS_PLUS_PLUS_HEADER_PATH})
target_link_libraries(app redis++::redis++ hiredis)

in this way it work,maybe you can add it to README.md

@WangHHY19931001
Copy link
Author

FetchContentHelp.cmake:

if(${CMAKE_VERSION} VERSION_LESS 3.14)
message("build not have FetchContent_MakeAvailable, so use self")
macro(FetchContent_MakeAvailable NAME)
FetchContent_GetProperties(${NAME})
if(NOT ${NAME}_POPULATED)
FetchContent_Populate(${NAME})
add_subdirectory(${${NAME}_SOURCE_DIR} ${${NAME}_BINARY_DIR})
endif()
endmacro()
endif()

@sewenew
Copy link
Owner

sewenew commented Sep 3, 2022

Great! Thanks a lot for your work! I'll take a try!

Regards

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants