From 8bec894e5e10af2374effe57a2d5381c8427437f Mon Sep 17 00:00:00 2001
From: Nicolas Werner <nicolas.werner@hotmail.de>
Date: Fri, 2 Jul 2021 00:36:26 +0200
Subject: [PATCH 1/6] Add libevent wrap

---
 .gitignore                                    |  2 ++
 .gitlab-ci.yml                                | 21 +++++++++++++++++++
 subprojects/libevent.wrap                     | 14 +++++++++++++
 .../packagefiles/libevent-meson/meson.build   | 19 +++++++++++++++++
 4 files changed, 56 insertions(+)
 create mode 100644 subprojects/libevent.wrap
 create mode 100644 subprojects/packagefiles/libevent-meson/meson.build

diff --git a/.gitignore b/.gitignore
index 90c9f5f..6b789ac 100644
--- a/.gitignore
+++ b/.gitignore
@@ -5,4 +5,6 @@
 compile_commands.json
 /subprojects/packagecache
 /subprojects/doctest-*
+/subprojects/libevent-2.1.12-stable/
+/subprojects/packagecache/
 /*.pem
diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index f88acf9..ba224ee 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -26,6 +26,27 @@ build:
     reports:
       junit: http*.xml
 
+build-with-wraps:
+  stage: build
+  image: alpine:latest
+  tags: [docker]
+  before_script:
+    - echo 'https://dl-cdn.alpinelinux.org/alpine/edge/testing' >> /etc/apk/repositories
+    - apk update && apk add clang-extra-tools meson git python3 py3-pip py3-flask curl-dev g++ cmake pkgconf openssl make
+    - export PATH="$PATH:/root/.local/bin"
+    - pip3 install --user reuse
+    - ./scripts/run_tls_testserver.sh &
+    - ./scripts/run_testserver.sh &
+  script:
+    - meson setup builddir -Dtests=true -Dexamples=true --wrap-mode=forcefallback
+    - meson compile -C builddir
+    - ./builddir/tests/requests_tls -d --reporters=junit --out=https.xml
+    - ./builddir/tests/requests -d --reporters=junit --out=http.xml
+    - ./builddir/examples/coeurl_example
+  artifacts:
+    reports:
+      junit: http*.xml
+
 build-cmake:
   stage: build
   image: alpine:latest
diff --git a/subprojects/libevent.wrap b/subprojects/libevent.wrap
new file mode 100644
index 0000000..0923a5c
--- /dev/null
+++ b/subprojects/libevent.wrap
@@ -0,0 +1,14 @@
+[wrap-file]
+directory = libevent-2.1.12-stable
+
+source_url = https://github.com/libevent/libevent/releases/download/release-2.1.12-stable/libevent-2.1.12-stable.tar.gz
+source_filename = libevent-2.1.12-stable.tar.gz
+source_hash = 92e6de1be9ec176428fd2367677e61ceffc2ee1cb119035037a27d346b0403bb
+
+patch_directory = libevent-meson
+
+[provide]
+libevent_core = libevent_core_dep
+libevent_pthreads = libevent_pthreads_dep
+libevent_windows = libevent_windows_dep
+
diff --git a/subprojects/packagefiles/libevent-meson/meson.build b/subprojects/packagefiles/libevent-meson/meson.build
new file mode 100644
index 0000000..c92ab02
--- /dev/null
+++ b/subprojects/packagefiles/libevent-meson/meson.build
@@ -0,0 +1,19 @@
+project('Libevent meson wrapper', 'c',
+  meson_version : '>=0.57.0',
+)
+
+mod = import('unstable_external_project')
+
+p = mod.add_project('configure',
+  configure_options : [
+                       '--disable-openssl',
+                      ],
+)
+
+libevent_core_dep = p.dependency('libevent_core')
+if target_machine.system() == 'windows'
+  libevent_windows_dep = p.dependency('libevent_pthreads')
+else
+  libevent_pthreads_dep = p.dependency('libevent_pthreads')
+endif
+
-- 
GitLab


From 57a36bf9b4132f458832db6d9d91fbdd4dc0f5c7 Mon Sep 17 00:00:00 2001
From: Nicolas Werner <nicolas.werner@hotmail.de>
Date: Fri, 2 Jul 2021 00:43:08 +0200
Subject: [PATCH 2/6] Add spdlog wrap

---
 .gitignore              |  3 ++-
 subprojects/spdlog.wrap | 12 ++++++++++++
 2 files changed, 14 insertions(+), 1 deletion(-)
 create mode 100644 subprojects/spdlog.wrap

diff --git a/.gitignore b/.gitignore
index 6b789ac..b2dc85d 100644
--- a/.gitignore
+++ b/.gitignore
@@ -5,6 +5,7 @@
 compile_commands.json
 /subprojects/packagecache
 /subprojects/doctest-*
-/subprojects/libevent-2.1.12-stable/
+/subprojects/libevent-*/
+/subprojects/spdlog-*/
 /subprojects/packagecache/
 /*.pem
diff --git a/subprojects/spdlog.wrap b/subprojects/spdlog.wrap
new file mode 100644
index 0000000..27cb985
--- /dev/null
+++ b/subprojects/spdlog.wrap
@@ -0,0 +1,12 @@
+[wrap-file]
+directory = spdlog-1.8.5
+source_url = https://github.com/gabime/spdlog/archive/v1.8.5.tar.gz
+source_filename = v1.8.5.tar.gz
+source_hash = 944d0bd7c763ac721398dca2bb0f3b5ed16f67cef36810ede5061f35a543b4b8
+patch_url = https://wrapdb.mesonbuild.com/v1/projects/spdlog/1.8.5/1/get_zip
+patch_filename = spdlog-1.8.5-1-wrap.zip
+patch_hash = 3c38f275d5792b1286391102594329e98b17737924b344f98312ab09929b74be
+
+[provide]
+spdlog = spdlog_dep
+
-- 
GitLab


From 24098c9f19c5529c85f21ab30ad08143a50c1900 Mon Sep 17 00:00:00 2001
From: Nicolas Werner <nicolas.werner@hotmail.de>
Date: Fri, 2 Jul 2021 00:46:57 +0200
Subject: [PATCH 3/6] Fix library names in libevent wrap

---
 subprojects/packagefiles/libevent-meson/meson.build | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/subprojects/packagefiles/libevent-meson/meson.build b/subprojects/packagefiles/libevent-meson/meson.build
index c92ab02..c68a55c 100644
--- a/subprojects/packagefiles/libevent-meson/meson.build
+++ b/subprojects/packagefiles/libevent-meson/meson.build
@@ -10,10 +10,10 @@ p = mod.add_project('configure',
                       ],
 )
 
-libevent_core_dep = p.dependency('libevent_core')
+libevent_core_dep = p.dependency('event_core')
 if target_machine.system() == 'windows'
-  libevent_windows_dep = p.dependency('libevent_pthreads')
+  libevent_windows_dep = p.dependency('event_pthreads')
 else
-  libevent_pthreads_dep = p.dependency('libevent_pthreads')
+  libevent_pthreads_dep = p.dependency('event_pthreads')
 endif
 
-- 
GitLab


From 06f20d2eb5495c65e476c61fe1ef24c34f458ff9 Mon Sep 17 00:00:00 2001
From: Nicolas Werner <nicolas.werner@hotmail.de>
Date: Fri, 2 Jul 2021 00:57:25 +0200
Subject: [PATCH 4/6] Build as static when using wraps

---
 .gitlab-ci.yml | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index ba224ee..c2968a0 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -38,7 +38,7 @@ build-with-wraps:
     - ./scripts/run_tls_testserver.sh &
     - ./scripts/run_testserver.sh &
   script:
-    - meson setup builddir -Dtests=true -Dexamples=true --wrap-mode=forcefallback
+    - meson setup builddir -Dtests=true -Dexamples=true --wrap-mode=forcefallback -Ddefault_library=static
     - meson compile -C builddir
     - ./builddir/tests/requests_tls -d --reporters=junit --out=https.xml
     - ./builddir/tests/requests -d --reporters=junit --out=http.xml
-- 
GitLab


From badf92783434f5fa8078e7616d21dbd20e5d7aaa Mon Sep 17 00:00:00 2001
From: Nicolas Werner <nicolas.werner@hotmail.de>
Date: Fri, 2 Jul 2021 19:58:00 +0200
Subject: [PATCH 5/6] Use cmake for libevent dependency

---
 meson.build                                   | 58 ++++++++++++++-----
 subprojects/libevent.wrap                     |  7 ---
 .../packagefiles/libevent-meson/meson.build   | 19 ------
 3 files changed, 44 insertions(+), 40 deletions(-)
 delete mode 100644 subprojects/packagefiles/libevent-meson/meson.build

diff --git a/meson.build b/meson.build
index a170c04..965a7c6 100644
--- a/meson.build
+++ b/meson.build
@@ -1,21 +1,51 @@
 project('coeurl', 'cpp',
   version : '0.1',
   default_options : ['warning_level=3',
-                     'cpp_std=c++17'])
+    'cpp_std=c++17'])
 
-deps = [
-  dependency('threads', required: true),
-  dependency('libevent_core', required: true),
-  dependency('libcurl', required: true),
-  dependency('spdlog', required: true)
-  ]
+  deps = [
+    dependency('threads', required: true),
+    dependency('libcurl', required: true),
+    dependency('spdlog', required: true)
+    ]
 
+
+  libevent_dep = dependency('libevent_core', required: get_option('wrap_mode') == 'nofallback',)
 if target_machine.system() == 'windows'
-  deps += dependency('libevent_windows', required: true)
+  libevent_threads_dep = dependency('libevent_windows', required: get_option('wrap_mode') == 'nofallback',)
 else
-  deps += dependency('libevent_pthreads', required: true)
+  libevent_threads_dep = dependency('libevent_pthreads', required: get_option('wrap_mode') == 'nofallback',)
 endif
 
+if (not libevent_dep.found()
+  or not libevent_threads_dep.found()
+  or get_option('wrap_mode') == 'forcefallback'
+  or 'libevent' in get_option('force_fallback_for'))
+cmake = import('cmake')
+libevent_options = cmake.subproject_options()
+    libevent_options.add_cmake_defines({
+      'CMAKE_C_FLAGS': '-fPIC',
+      'EVENT_LIBRARY_TYPE': 'STATIC',
+      'BUILD_SHARED_LIBS': false,
+      'BUILD_SHARED_AND_STATIC_LIBS': false,
+      'EVENT__DISABLE_OPENSSL': true,
+      'EVENT__DISABLE_BENCHMARK': true,
+      })
+    libevent_options.set_override_option('werror', 'false')
+    libevent_options.set_override_option('warning_level', '0')
+    libevent_proj = cmake.subproject('libevent', options: libevent_options)
+    libevent_dep = libevent_proj.dependency('event_core_static')
+
+    if target_machine.system() == 'windows'
+      # the cmake project links the thread support into the library directly...
+      libevent_threads_dep = dependency('', required : false)
+    else
+      libevent_threads_dep = libevent_proj.dependency('event_pthreads_static')
+    endif
+endif
+
+deps += [libevent_dep, libevent_threads_dep]
+
 include = include_directories('include')
 
 lib = library('coeurl', ['lib/client.cpp', 'lib/request.cpp'],
@@ -37,11 +67,11 @@ endif
 
 pkg = import('pkgconfig')
 pkg.generate(lib,
-             libraries : [lib],
-             version : meson.project_version(),
-             filebase : meson.project_name(),
-             description : 'Simple library to do http requests asynchronously via CURL in C++.',
-             url : 'https://nheko.im/nheko-reborn/cocurl')
+  libraries : [lib],
+  version : meson.project_version(),
+  filebase : meson.project_name(),
+  description : 'Simple library to do http requests asynchronously via CURL in C++.',
+  url : 'https://nheko.im/nheko-reborn/cocurl')
 
 # No idea how to export working cmake config files, so don't do that for now
 #cmake = import('cmake')
diff --git a/subprojects/libevent.wrap b/subprojects/libevent.wrap
index 0923a5c..2406345 100644
--- a/subprojects/libevent.wrap
+++ b/subprojects/libevent.wrap
@@ -5,10 +5,3 @@ source_url = https://github.com/libevent/libevent/releases/download/release-2.1.
 source_filename = libevent-2.1.12-stable.tar.gz
 source_hash = 92e6de1be9ec176428fd2367677e61ceffc2ee1cb119035037a27d346b0403bb
 
-patch_directory = libevent-meson
-
-[provide]
-libevent_core = libevent_core_dep
-libevent_pthreads = libevent_pthreads_dep
-libevent_windows = libevent_windows_dep
-
diff --git a/subprojects/packagefiles/libevent-meson/meson.build b/subprojects/packagefiles/libevent-meson/meson.build
deleted file mode 100644
index c68a55c..0000000
--- a/subprojects/packagefiles/libevent-meson/meson.build
+++ /dev/null
@@ -1,19 +0,0 @@
-project('Libevent meson wrapper', 'c',
-  meson_version : '>=0.57.0',
-)
-
-mod = import('unstable_external_project')
-
-p = mod.add_project('configure',
-  configure_options : [
-                       '--disable-openssl',
-                      ],
-)
-
-libevent_core_dep = p.dependency('event_core')
-if target_machine.system() == 'windows'
-  libevent_windows_dep = p.dependency('event_pthreads')
-else
-  libevent_pthreads_dep = p.dependency('event_pthreads')
-endif
-
-- 
GitLab


From 5996b40313b98e66b3e6faffbec31de7ff512ad4 Mon Sep 17 00:00:00 2001
From: Nicolas Werner <nicolas.werner@hotmail.de>
Date: Fri, 2 Jul 2021 20:26:20 +0200
Subject: [PATCH 6/6] Add curl wrap

---
 .gitignore            |  1 +
 .gitlab-ci.yml        |  4 ++--
 meson.build           | 26 ++++++++++++++++++++++----
 subprojects/curl.wrap |  9 +++++++++
 4 files changed, 34 insertions(+), 6 deletions(-)
 create mode 100644 subprojects/curl.wrap

diff --git a/.gitignore b/.gitignore
index b2dc85d..2244196 100644
--- a/.gitignore
+++ b/.gitignore
@@ -7,5 +7,6 @@ compile_commands.json
 /subprojects/doctest-*
 /subprojects/libevent-*/
 /subprojects/spdlog-*/
+/subprojects/curl-*/
 /subprojects/packagecache/
 /*.pem
diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index c2968a0..4df2067 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -9,7 +9,7 @@ build:
   tags: [docker]
   before_script:
     - echo 'https://dl-cdn.alpinelinux.org/alpine/edge/testing' >> /etc/apk/repositories
-    - apk update && apk add clang-extra-tools meson git python3 py3-pip py3-flask lcov gcovr curl-dev libevent-dev spdlog-dev grep g++ cmake pkgconf openssl
+    - apk update && apk add meson git python3 py3-pip py3-flask lcov gcovr curl-dev libevent-dev spdlog-dev grep g++ cmake pkgconf openssl
     - export PATH="$PATH:/root/.local/bin"
     - pip3 install --user reuse
     - ./scripts/run_tls_testserver.sh &
@@ -32,7 +32,7 @@ build-with-wraps:
   tags: [docker]
   before_script:
     - echo 'https://dl-cdn.alpinelinux.org/alpine/edge/testing' >> /etc/apk/repositories
-    - apk update && apk add clang-extra-tools meson git python3 py3-pip py3-flask curl-dev g++ cmake pkgconf openssl make
+    - apk update && apk add meson git python3 py3-pip py3-flask g++ cmake pkgconf openssl openssl-dev make
     - export PATH="$PATH:/root/.local/bin"
     - pip3 install --user reuse
     - ./scripts/run_tls_testserver.sh &
diff --git a/meson.build b/meson.build
index 965a7c6..b020960 100644
--- a/meson.build
+++ b/meson.build
@@ -3,14 +3,14 @@ project('coeurl', 'cpp',
   default_options : ['warning_level=3',
     'cpp_std=c++17'])
 
-  deps = [
+deps = [
     dependency('threads', required: true),
-    dependency('libcurl', required: true),
     dependency('spdlog', required: true)
-    ]
+  ]
 
+libcurl_dep = dependency('libcurl', required: get_option('wrap_mode') == 'nofallback')
 
-  libevent_dep = dependency('libevent_core', required: get_option('wrap_mode') == 'nofallback',)
+libevent_dep = dependency('libevent_core', required: get_option('wrap_mode') == 'nofallback',)
 if target_machine.system() == 'windows'
   libevent_threads_dep = dependency('libevent_windows', required: get_option('wrap_mode') == 'nofallback',)
 else
@@ -46,6 +46,24 @@ endif
 
 deps += [libevent_dep, libevent_threads_dep]
 
+if (not libcurl_dep.found()
+  or get_option('wrap_mode') == 'forcefallback'
+  or 'libcurl' in get_option('force_fallback_for'))
+cmake = import('cmake')
+libcurl_options = cmake.subproject_options()
+    libcurl_options.add_cmake_defines({
+      'CMAKE_C_FLAGS': '-fPIC',
+      'BUILD_SHARED_LIBS': false,
+      'BUILD_SHARED_AND_STATIC_LIBS': false,
+      'HTTP_ONLY': true,
+      })
+    libcurl_options.set_override_option('werror', 'false')
+    libcurl_options.set_override_option('warning_level', '0')
+    libcurl_proj = cmake.subproject('curl', options: libcurl_options)
+    libcurl_dep = libcurl_proj.dependency('libcurl')
+endif
+
+deps += [libcurl_dep]
 include = include_directories('include')
 
 lib = library('coeurl', ['lib/client.cpp', 'lib/request.cpp'],
diff --git a/subprojects/curl.wrap b/subprojects/curl.wrap
new file mode 100644
index 0000000..f1510b4
--- /dev/null
+++ b/subprojects/curl.wrap
@@ -0,0 +1,9 @@
+[wrap-file]
+directory = curl-7.77.0
+
+source_url = https://github.com/curl/curl/releases/download/curl-7_77_0/curl-7.77.0.tar.xz
+source_filename = curl-7.77.0.tar.xz
+source_hash = 0f64582c54282f31c0de9f0a1a596b182776bd4df9a4c4a2a41bbeb54f62594b
+
+[provides]
+dependency_names = libcurl
-- 
GitLab