Skip to content

Commit

Permalink
Use prebuilt libcurl (#176)
Browse files Browse the repository at this point in the history
* Fix

* Fix

* Update FetchCurl.cmake

* Update FetchCurl.cmake

* Update FetchCurl.cmake

* Update FetchCurl.cmake
  • Loading branch information
umireon authored Apr 21, 2024
1 parent ab491ad commit f170fa1
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 35 deletions.
19 changes: 5 additions & 14 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,16 @@ target_sources(
src/ScreenBuilder/ActionHandler.cpp
src/ScreenBuilder/StateMachine.cpp
src/UpdateChecker/UpdateDialog.cpp
src/UpdateChecker/UpdateChecker.cpp)
src/UpdateChecker/UpdateChecker.cpp
src/UpdateChecker/CurlClient.cpp)

include(cmake/FetchOpenCV.cmake)
target_link_libraries(${CMAKE_PROJECT_NAME} PRIVATE ${OpenCV_LIBS})
target_include_directories(${CMAKE_PROJECT_NAME} PRIVATE ${OpenCV_INCLUDE_DIRS})

include(cmake/FetchCurl.cmake)
target_link_libraries(${CMAKE_PROJECT_NAME} PRIVATE CURL::libcurl)

# TextRecognizer
if(APPLE)
add_subdirectory(src/TextRecognizer/VisionTextRecognizer)
Expand All @@ -60,18 +64,5 @@ elseif(MSVC)
target_link_libraries(${CMAKE_PROJECT_NAME} PRIVATE WinRTTextRecognizer)
endif()

# UpdateChecker
if(APPLE)
add_subdirectory(src/UpdateChecker/URLSessionClient)
target_link_libraries(${CMAKE_PROJECT_NAME} PRIVATE URLSessionClient)
elseif(UNIX)
add_subdirectory(src/UpdateChecker/CurlClient)
target_link_libraries(${CMAKE_PROJECT_NAME} PRIVATE CurlClient)
elseif(MSVC)
add_subdirectory(src/UpdateChecker/WinRTHttpClient)
target_link_libraries(${CMAKE_PROJECT_NAME} PRIVATE WinRTHttpClient)
endif()

target_include_directories(${CMAKE_PROJECT_NAME} PRIVATE ${CMAKE_SOURCE_DIR}/vendor/nlohmann-json/include)
target_include_directories(${CMAKE_PROJECT_NAME} PRIVATE ${CMAKE_SOURCE_DIR}/vendor/nameof/include)
set_target_properties_plugin(${CMAKE_PROJECT_NAME} PROPERTIES OUTPUT_NAME ${_name})
21 changes: 0 additions & 21 deletions cmake/BuildMyCurl.cmake

This file was deleted.

44 changes: 44 additions & 0 deletions cmake/FetchCurl.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
include(FetchContent)

set(USE_SYSTEM_CURL
OFF
CACHE STRING "Use system Curl")

set(Curl_Version 8.7.1-4)

set(Curl_BASEURL "https://github.com/kaito-tokyo/kaito-tokyo-obs-dep-curl/releases/download/${Curl_Version}")

if(OS_MACOS)
if(CMAKE_BUILD_TYPE STREQUAL Debug)
set(Curl_URL "${Curl_BASEURL}/curl-macos-${Curl_Version}-Debug.tar.gz")
set(Curl_HASH SHA256=d8b8777a6e00be2f645f9039e40adeb66467ee2127a3b7756313b92890d4f887)
elseif(CMAKE_BUILD_TYPE STREQUAL RelWithDebInfo)
set(Curl_URL "${Curl_BASEURL}/curl-macos-${Curl_Version}-RelWithDebInfo.tar.gz")
set(Curl_HASH SHA256=f3b92726b0b368bbc0f50e28d2fbfcc75faa5406a49e8675d29b52a9e0a64c4c)
else()
set(Curl_URL "${Curl_BASEURL}/curl-macos-${Curl_Version}-Release.tar.gz")
set(Curl_HASH SHA256=d3ab033f6a1e01e282c4e8c764d6fc99ace0cecbb4b68f6495f84356809967ac)
endif()
elseif(OS_WINDOWS)
if(CMAKE_BUILD_TYPE STREQUAL Debug)
set(Curl_URL "${Curl_BASEURL}/curl-windows-${Curl_Version}-Debug.zip")
set(Curl_HASH SHA256=04385be061c0b0598e0a42a8b380ded9854eae04df02ee5226f0e57915e1126c)
elseif(CMAKE_BUILD_TYPE STREQUAL RelWithDebInfo)
set(Curl_URL "${Curl_BASEURL}/curl-windows-${Curl_Version}-RelWithDebInfo.zip")
set(Curl_HASH SHA256=06d51772f759e368f8eecfbdbd7196d20709af18869708094211afa873a1b00f)
else()
set(Curl_URL "${Curl_BASEURL}/curl-windows-${Curl_Version}-Release.zip")
set(Curl_HASH SHA256=630aef8cf2425884d2cd7c50fbd10094a25984f9db88bcd219746fab415b29c9)
endif()
endif()

if(NOT USE_SYSTEM_CURL AND (OS_MACOS OR OS_WINDOWS))
FetchContent_Declare(
curl
URL ${Curl_URL}
URL_HASH ${Curl_HASH})
FetchContent_MakeAvailable(curl)
find_package(CURL REQUIRED HINTS "${curl_SOURCE_DIR}/lib/cmake")
else()
find_package(CURL REQUIRED)
endif()
44 changes: 44 additions & 0 deletions src/UpdateChecker/CurlClient.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include <functional>
#include <string>

#include <curl/curl.h>

#include <obs.h>

#include "plugin-support.h"

const std::string userAgent = std::string(PLUGIN_NAME) + "/" + PLUGIN_VERSION;

static std::size_t writeFunc(void *ptr, std::size_t size, size_t nmemb,
std::string *data)
{
data->append(static_cast<char *>(ptr), size * nmemb);
return size * nmemb;
}

void fetchStringFromUrl(const char *urlString,
std::function<void(std::string, int)> callback)
{
CURL *curl = curl_easy_init();
if (!curl) {
obs_log(LOG_INFO, "Failed to initialize curl");
callback("", CURL_LAST);
return;
}

std::string responseBody;
curl_easy_setopt(curl, CURLOPT_URL, urlString);
curl_easy_setopt(curl, CURLOPT_USERAGENT, userAgent.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeFunc);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &responseBody);

CURLcode code = curl_easy_perform(curl);
curl_easy_cleanup(curl);

if (code == CURLE_OK) {
callback(responseBody, 0);
} else {
obs_log(LOG_INFO, "Failed to get latest release info");
callback("", code);
}
}

0 comments on commit f170fa1

Please sign in to comment.