Skip to content

Commit

Permalink
Redo FindJsonCpp and fix CI
Browse files Browse the repository at this point in the history
- Commonize samples/ cmakelists txt
  • Loading branch information
Royna2544 committed Oct 15, 2024
1 parent 347536a commit ddcb057
Show file tree
Hide file tree
Showing 17 changed files with 96 additions and 203 deletions.
3 changes: 2 additions & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
!samples
!src
!test
!CMakeLists.txt
!CMakeLists.txt
!CMake
27 changes: 27 additions & 0 deletions CMake/FindJsonCppCustom.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# FindJsonCpp.cmake

# Try to locate the jsoncpp package using pkg-config
find_package(PkgConfig QUIET)

if (PKG_CONFIG_FOUND)
pkg_check_modules(JSONCPP_PKG jsoncpp)

if (JSONCPP_PKG_FOUND)
# Create the jsoncpp imported target
if (NOT TARGET JsonCpp::JsonCpp)
add_library(JsonCpp::JsonCpp SHARED IMPORTED)
set_target_properties(JsonCpp::JsonCpp PROPERTIES
IMPORTED_LOCATION "${JSONCPP_PKG_LIBRARY_DIRS}/libjsoncpp.so"
INTERFACE_INCLUDE_DIRECTORIES "${JSONCPP_PKG_INCLUDE_DIRS}"
)

# Optionally, set any additional properties, such as dependencies or version
message(STATUS "Found JsonCpp: ${JSONCPP_PKG_LIBRARY_DIRS}/libjsoncpp.so")
endif()

else()
message(FATAL_ERROR "Pkg-config found, but JsonCpp not found via pkg-config.")
endif()
else()
message(FATAL_ERROR "Pkg-config not found, cannot locate JsonCpp.")
endif()
32 changes: 13 additions & 19 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -68,27 +68,20 @@ if (CURL_FOUND)
endif()

## jsoncpp
find_package(jsoncpp QUIET)
if (NOT TARGET jsoncpp_lib)
# Check for pkg-config and try to find jsoncpp using it
find_package(PkgConfig QUIET)
if (PKG_CONFIG_FOUND)
pkg_check_modules(JSONCPP_PKG jsoncpp)
if (JSONCPP_PKG_FOUND)
add_library(jsoncpp_lib INTERFACE)
target_include_directories(jsoncpp_lib INTERFACE ${JSONCPP_PKG_INCLUDE_DIRS})
target_link_directories(jsoncpp_lib INTERFACE ${JSONCPP_PKG_LIBRARY_DIRS})
target_link_libraries(jsoncpp_lib INTERFACE ${JSONCPP_PKG_LIBRARIES})
install(
TARGETS jsoncpp_lib EXPORT ${PROJECT_NAME}-targets DESTINATION lib
)
if (NOT TARGET JsonCpp::JsonCpp)
find_package(jsoncpp)
if (NOT TARGET JsonCpp::JsonCpp)
message(STATUS "Using alternative findjsoncpp")
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/CMake")
find_package(JsonCppCustom)
if (NOT TARGET JsonCpp::JsonCpp)
message(FATAL_ERROR "Jsoncpp is not available")
endif()
endif()
endif()
if (NOT TARGET jsoncpp_lib)
message(FATAL_ERROR "Jsoncpp is not available")
endif()
include_directories(${jsoncpp_INCLUDE_DIRS} ${JSONCPP_PKG_INCLUDE_DIRS})
install(FILES CMake/FindJsonCppCustom.cmake
DESTINATION lib/cmake/tgbot-cpp
)

## boost
set(Boost_USE_MULTITHREADED ON)
Expand All @@ -109,7 +102,7 @@ set(LIB_LIST
${ZLIB_LIBRARIES}
${OPENSSL_LIBRARIES}
${Boost_LIBRARIES}
jsoncpp_lib
JsonCpp::JsonCpp
)

if (CURL_FOUND)
Expand All @@ -125,6 +118,7 @@ add_library(${PROJECT_NAME} ${SRC_LIST})
target_include_directories(${PROJECT_NAME} PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>)
target_include_directories(${PROJECT_NAME} PUBLIC ${jsoncpp_INCLUDE_DIRS} ${JSONCPP_PKG_INCLUDE_DIRS})
target_link_libraries(${PROJECT_NAME} ${LIB_LIST})
include(GNUInstallDirs)
install(TARGETS ${PROJECT_NAME}
Expand Down
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ WORKDIR /usr/src/tgbot-cpp
COPY include include
COPY src src
COPY CMakeLists.txt ./
COPY CMake CMake

RUN cmake . && \
make -j$(nproc) && \
Expand Down
4 changes: 3 additions & 1 deletion Dockerfile_test
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,16 @@ RUN apt-get -qq update && \
python2.7-dev \
wget \
zlib1g-dev \
libjsoncpp-dev && \
libjsoncpp-dev \
pkg-config && \
rm -rf /var/lib/apt/lists/*

WORKDIR /usr/src/tgbot-cpp
COPY include include
COPY src src
COPY test test
COPY CMakeLists.txt ./
COPY CMake CMake

RUN cmake -DENABLE_TESTS=ON . && \
make -j$(nproc) && \
Expand Down
20 changes: 20 additions & 0 deletions samples/common_defs.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall")
set(Boost_USE_MULTITHREADED ON)

find_package(Threads REQUIRED)
find_package(OpenSSL REQUIRED)
find_package(Boost COMPONENTS system REQUIRED)
find_package(JsonCpp QUIET)
if (NOT TARGET JsonCpp::JsonCPP)
include(/usr/local/lib/cmake/tgbot-cpp/FindJsonCppCustom.cmake)
endif()
find_package(CURL)
include_directories(/usr/local/include ${OPENSSL_INCLUDE_DIR} ${Boost_INCLUDE_DIR})
if (CURL_FOUND)
include_directories(${CURL_INCLUDE_DIRS})
add_definitions(-DHAVE_CURL)
endif()
set(LINK_LIB_LIST /usr/local/lib/libTgBot.a ${CMAKE_THREAD_LIBS_INIT} ${OPENSSL_LIBRARIES} ${Boost_LIBRARIES} ${CURL_LIBRARIES} JsonCpp::JsonCpp)
21 changes: 3 additions & 18 deletions samples/echobot-curl-client/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,21 +1,6 @@
cmake_minimum_required(VERSION 3.10.2)
project(echobot-curl-client)

set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall")
set(Boost_USE_MULTITHREADED ON)

find_package(Threads REQUIRED)
find_package(OpenSSL REQUIRED)
find_package(Boost COMPONENTS system REQUIRED)
find_package(CURL)
include_directories(/usr/local/include ${OPENSSL_INCLUDE_DIR} ${Boost_INCLUDE_DIR})
if (CURL_FOUND)
include_directories(${CURL_INCLUDE_DIRS})
add_definitions(-DHAVE_CURL)
endif()

add_executable(echobot-curl-client src/main.cpp)

target_link_libraries(echobot-curl-client /usr/local/lib/libTgBot.a ${CMAKE_THREAD_LIBS_INIT} ${OPENSSL_LIBRARIES} ${Boost_LIBRARIES} ${CURL_LIBRARIES})
include(../common_defs.cmake)
add_executable(${PROJECT_NAME} src/main.cpp)
target_link_libraries(${PROJECT_NAME} ${LINK_LIB_LIST})
4 changes: 1 addition & 3 deletions samples/echobot-curl-client/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ int main() {
string token(getenv("TOKEN"));
printf("Token: %s\n", token.c_str());

CurlHttpClient curlHttpClient;

Bot bot(token, curlHttpClient);
Bot bot(token, std::make_unique<CurlHttpClient>());
bot.getEvents().onCommand("start", [&bot](Message::Ptr message) {
bot.getApi().sendMessage(message->chat->id, "Hi!");
});
Expand Down
21 changes: 3 additions & 18 deletions samples/echobot-setmycommands/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,21 +1,6 @@
cmake_minimum_required(VERSION 3.10.2)
project(echobot-setmycommands)

set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall")
set(Boost_USE_MULTITHREADED ON)

find_package(Threads REQUIRED)
find_package(OpenSSL REQUIRED)
find_package(Boost COMPONENTS system REQUIRED)
find_package(CURL)
include_directories(/usr/local/include ${OPENSSL_INCLUDE_DIR} ${Boost_INCLUDE_DIR})
if (CURL_FOUND)
include_directories(${CURL_INCLUDE_DIRS})
add_definitions(-DHAVE_CURL)
endif()

add_executable(echobot-setmycommands src/main.cpp)

target_link_libraries(echobot-setmycommands /usr/local/lib/libTgBot.a ${CMAKE_THREAD_LIBS_INIT} ${OPENSSL_LIBRARIES} ${Boost_LIBRARIES} ${CURL_LIBRARIES})
include(../common_defs.cmake)
add_executable(${PROJECT_NAME} src/main.cpp)
target_link_libraries(${PROJECT_NAME} ${LINK_LIB_LIST})
19 changes: 2 additions & 17 deletions samples/echobot-submodule/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,22 +1,7 @@
cmake_minimum_required(VERSION 3.10.2)
project(echobot-submodule)

set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall")
set(Boost_USE_MULTITHREADED ON)

find_package(Threads REQUIRED)
find_package(OpenSSL REQUIRED)
find_package(Boost COMPONENTS system REQUIRED)
find_package(CURL)
include_directories(/usr/local/include ${OPENSSL_INCLUDE_DIR} ${Boost_INCLUDE_DIR})
if (CURL_FOUND)
include_directories(${CURL_INCLUDE_DIRS})
add_definitions(-DHAVE_CURL)
endif()

include(../common_defs.cmake)
add_subdirectory(tgbot-cpp)
add_executable(echobot-submodule src/main.cpp)

target_link_libraries(echobot-submodule TgBot ${CMAKE_THREAD_LIBS_INIT} ${OPENSSL_LIBRARIES} ${Boost_LIBRARIES} ${CURL_LIBRARIES})
target_link_libraries(echobot-submodule TgBot ${CMAKE_THREAD_LIBS_INIT} ${OPENSSL_LIBRARIES} ${Boost_LIBRARIES} ${CURL_LIBRARIES} JsonCpp::JsonCpp)
21 changes: 3 additions & 18 deletions samples/echobot-webhook-server/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,21 +1,6 @@
cmake_minimum_required(VERSION 3.10.2)
project(echobot-webhook-server)

set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall")
set(Boost_USE_MULTITHREADED ON)

find_package(Threads REQUIRED)
find_package(OpenSSL REQUIRED)
find_package(Boost COMPONENTS system REQUIRED)
find_package(CURL)
include_directories(/usr/local/include ${OPENSSL_INCLUDE_DIR} ${Boost_INCLUDE_DIR})
if (CURL_FOUND)
include_directories(${CURL_INCLUDE_DIRS})
add_definitions(-DHAVE_CURL)
endif()

add_executable(echobot-webhook-server src/main.cpp)

target_link_libraries(echobot-webhook-server /usr/local/lib/libTgBot.a ${CMAKE_THREAD_LIBS_INIT} ${OPENSSL_LIBRARIES} ${Boost_LIBRARIES})
include(../common_defs.cmake)
add_executable(${PROJECT_NAME} src/main.cpp)
target_link_libraries(${PROJECT_NAME} ${LINK_LIB_LIST})
21 changes: 3 additions & 18 deletions samples/echobot/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,21 +1,6 @@
cmake_minimum_required(VERSION 3.10.2)
project(echobot)

set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall")
set(Boost_USE_MULTITHREADED ON)

find_package(Threads REQUIRED)
find_package(OpenSSL REQUIRED)
find_package(Boost COMPONENTS system REQUIRED)
find_package(CURL)
include_directories(/usr/local/include ${OPENSSL_INCLUDE_DIR} ${Boost_INCLUDE_DIR})
if (CURL_FOUND)
include_directories(${CURL_INCLUDE_DIRS})
add_definitions(-DHAVE_CURL)
endif()

add_executable(echobot src/main.cpp)

target_link_libraries(echobot /usr/local/lib/libTgBot.a ${CMAKE_THREAD_LIBS_INIT} ${OPENSSL_LIBRARIES} ${Boost_LIBRARIES} ${CURL_LIBRARIES})
include(../common_defs.cmake)
add_executable(${PROJECT_NAME} src/main.cpp)
target_link_libraries(echobot ${LINK_LIB_LIST})
21 changes: 3 additions & 18 deletions samples/inline-keyboard/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,21 +1,6 @@
cmake_minimum_required(VERSION 3.10.2)
project(inline-keyboard)

set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall")
set(Boost_USE_MULTITHREADED ON)

find_package(Threads REQUIRED)
find_package(OpenSSL REQUIRED)
find_package(Boost COMPONENTS system REQUIRED)
find_package(CURL)
include_directories(/usr/local/include ${OPENSSL_INCLUDE_DIR} ${Boost_INCLUDE_DIR})
if (CURL_FOUND)
include_directories(${CURL_INCLUDE_DIRS})
add_definitions(-DHAVE_CURL)
endif()

add_executable(inline-keyboard src/main.cpp)

target_link_libraries(inline-keyboard /usr/local/lib/libTgBot.a ${CMAKE_THREAD_LIBS_INIT} ${OPENSSL_LIBRARIES} ${Boost_LIBRARIES} ${CURL_LIBRARIES})
include(../common_defs.cmake)
add_executable(${PROJECT_NAME} src/main.cpp)
target_link_libraries(${PROJECT_NAME} ${LINK_LIB_LIST})
21 changes: 3 additions & 18 deletions samples/photo/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,21 +1,6 @@
cmake_minimum_required(VERSION 3.10.2)
project(photo)

set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall")
set(Boost_USE_MULTITHREADED ON)

find_package(Threads REQUIRED)
find_package(OpenSSL REQUIRED)
find_package(Boost COMPONENTS system REQUIRED)
find_package(CURL)
include_directories(/usr/local/include ${OPENSSL_INCLUDE_DIR} ${Boost_INCLUDE_DIR})
if (CURL_FOUND)
include_directories(${CURL_INCLUDE_DIRS})
add_definitions(-DHAVE_CURL)
endif()

add_executable(photo src/main.cpp)

target_link_libraries(photo /usr/local/lib/libTgBot.a ${CMAKE_THREAD_LIBS_INIT} ${OPENSSL_LIBRARIES} ${Boost_LIBRARIES} ${CURL_LIBRARIES})
include(../common_defs.cmake)
add_executable(${PROJECT_NAME} src/main.cpp)
target_link_libraries(${PROJECT_NAME} ${LINK_LIB_LIST})
21 changes: 3 additions & 18 deletions samples/receive-file/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,21 +1,6 @@
cmake_minimum_required(VERSION 3.10.2)
project(receive-file)

set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall")
set(Boost_USE_MULTITHREADED ON)

find_package(Threads REQUIRED)
find_package(OpenSSL REQUIRED)
find_package(Boost COMPONENTS system REQUIRED)
find_package(CURL)
include_directories(/usr/local/include ${OPENSSL_INCLUDE_DIR} ${Boost_INCLUDE_DIR})
if (CURL_FOUND)
include_directories(${CURL_INCLUDE_DIRS})
add_definitions(-DHAVE_CURL)
endif()

add_executable(receive-file src/main.cpp)

target_link_libraries(receive-file /usr/local/lib/libTgBot.a ${CMAKE_THREAD_LIBS_INIT} ${OPENSSL_LIBRARIES} ${Boost_LIBRARIES} ${CURL_LIBRARIES})
include(../common_defs.cmake)
add_executable(${PROJECT_NAME} src/main.cpp)
target_link_libraries(${PROJECT_NAME} ${LINK_LIB_LIST})
21 changes: 3 additions & 18 deletions samples/received-text-processing/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,21 +1,6 @@
cmake_minimum_required(VERSION 3.10.2)
project(received-text-processing)

set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall")
set(Boost_USE_MULTITHREADED ON)

find_package(Threads REQUIRED)
find_package(OpenSSL REQUIRED)
find_package(Boost COMPONENTS system REQUIRED)
find_package(CURL)
include_directories(/usr/local/include ${OPENSSL_INCLUDE_DIR} ${Boost_INCLUDE_DIR})
if (CURL_FOUND)
include_directories(${CURL_INCLUDE_DIRS})
add_definitions(-DHAVE_CURL)
endif()

add_executable(received-processing-text src/main.cpp)

target_link_libraries(received-processing-text /usr/local/lib/libTgBot.a ${CMAKE_THREAD_LIBS_INIT} ${OPENSSL_LIBRARIES} ${Boost_LIBRARIES} ${CURL_LIBRARIES})
include(../common_defs.cmake)
add_executable(${PROJECT_NAME} src/main.cpp)
target_link_libraries(${PROJECT_NAME} ${LINK_LIB_LIST})
Loading

0 comments on commit ddcb057

Please sign in to comment.