0001-added-emscripten-support.patch 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. From 6ed0c9e58b11680779c571996a3007bd972e0853 Mon Sep 17 00:00:00 2001
  2. From: Sergiu Deitsch <sergiud@users.noreply.github.com>
  3. Date: Thu, 4 Aug 2022 22:52:47 +0200
  4. Subject: [PATCH] added emscripten support (#846)
  5. [Retrieved from:
  6. https://github.com/google/glog/commit/6ed0c9e58b11680779c571996a3007bd972e0853,
  7. to fix build without threads, a fix that is lost in the middle of a
  8. larger commit adding emscripten support.]
  9. Signed-off-by: Fabrice Fontaine <fontaine.fabrice@gmail.com>
  10. ---
  11. .github/workflows/emscripten.yml | 60 ++++++++++++++++++++++++++++++++
  12. CMakeLists.txt | 14 ++++----
  13. src/config.h.cmake.in | 6 ++--
  14. src/glog/logging.h.in | 6 ++--
  15. src/glog/platform.h | 2 ++
  16. src/logging.cc | 11 ++++--
  17. src/raw_logging.cc | 9 ++---
  18. src/stacktrace_unwind-inl.h | 2 +-
  19. src/symbolize.cc | 2 +-
  20. src/utilities.h | 2 +-
  21. 10 files changed, 93 insertions(+), 21 deletions(-)
  22. create mode 100644 .github/workflows/emscripten.yml
  23. diff --git a/.github/workflows/emscripten.yml b/.github/workflows/emscripten.yml
  24. new file mode 100644
  25. index 00000000..566c67eb
  26. --- /dev/null
  27. +++ b/.github/workflows/emscripten.yml
  28. @@ -0,0 +1,60 @@
  29. +name: Emscripten
  30. +
  31. +on: [push, pull_request]
  32. +
  33. +jobs:
  34. + build-linux:
  35. + defaults:
  36. + run:
  37. + shell: bash
  38. + name: Emscripten-C++${{matrix.std}}-${{matrix.build_type}}-${{matrix.lib}}-${{matrix.extra}}
  39. + runs-on: ubuntu-latest
  40. + container: emscripten/emsdk
  41. + strategy:
  42. + fail-fast: true
  43. + matrix:
  44. + build_type: [Release, Debug]
  45. + extra: [no-custom-prefix, custom-prefix]
  46. + lib: [static]
  47. + std: [98, 11, 14, 17, 20]
  48. +
  49. + steps:
  50. + - uses: actions/checkout@v2
  51. +
  52. + - name: Setup Dependencies
  53. + run: |
  54. + apt-get update
  55. + DEBIAN_FRONTEND=noninteractive sudo apt-get install -y \
  56. + cmake \
  57. + ninja-build
  58. +
  59. + - name: Setup C++98 Environment
  60. + if: matrix.std == '98'
  61. + run: |
  62. + echo 'CXXFLAGS=-Wno-error=variadic-macros -Wno-error=long-long ${{env.CXXFLAGS}}' >> $GITHUB_ENV
  63. +
  64. + - name: Configure
  65. + env:
  66. + CXXFLAGS: -Wall -Wextra -Wsign-conversion -Wtautological-compare -Wformat-nonliteral -Wundef -Werror -Wno-error=wasm-exception-spec ${{env.CXXFLAGS}}
  67. + run: |
  68. + cmake -S . -B build_${{matrix.build_type}} \
  69. + -DBUILD_SHARED_LIBS=${{matrix.lib == 'shared'}} \
  70. + -DCMAKE_AR=$(which emar) \
  71. + -DCMAKE_C_COMPILER=$(which emcc) \
  72. + -DCMAKE_CXX_COMPILER=$(which em++) \
  73. + -DCMAKE_CXX_STANDARD=${{matrix.std}} \
  74. + -DCMAKE_CXX_STANDARD_REQUIRED=ON \
  75. + -DCMAKE_FIND_ROOT_PATH_MODE_INCLUDE=ONLY \
  76. + -DCMAKE_FIND_ROOT_PATH_MODE_LIBRARY=ONLY \
  77. + -DCMAKE_FIND_ROOT_PATH_MODE_PACKAGE=ONLY \
  78. + -DCMAKE_FIND_ROOT_PATH_MODE_PROGRAM=NEVER \
  79. + -DCMAKE_INSTALL_PREFIX=${{github.workspace}}/install \
  80. + -DCMAKE_RANLIB=$(which emranlib) \
  81. + -DWITH_CUSTOM_PREFIX=${{matrix.extra == 'custom-prefix'}} \
  82. + -G Ninja \
  83. + -Werror
  84. +
  85. + - name: Build
  86. + run: |
  87. + cmake --build build_${{matrix.build_type}} \
  88. + --config ${{matrix.build_type}}
  89. diff --git a/CMakeLists.txt b/CMakeLists.txt
  90. index 643a8b8a..ce6daa40 100644
  91. --- a/CMakeLists.txt
  92. +++ b/CMakeLists.txt
  93. @@ -93,11 +93,11 @@ find_package (Unwind)
  94. if (Unwind_FOUND)
  95. set (HAVE_LIB_UNWIND 1)
  96. else (Unwind_FOUND)
  97. - check_include_file_cxx (unwind.h HAVE_UNWIND_H)
  98. # Check whether linking actually succeeds. ARM toolchains of LLVM unwind
  99. # implementation do not necessarily provide the _Unwind_Backtrace function
  100. # which causes the previous check to succeed but the linking to fail.
  101. check_cxx_symbol_exists (_Unwind_Backtrace unwind.h HAVE__UNWIND_BACKTRACE)
  102. + check_cxx_symbol_exists (_Unwind_GetIP unwind.h HAVE__UNWIND_GETIP)
  103. endif (Unwind_FOUND)
  104. check_include_file_cxx (dlfcn.h HAVE_DLFCN_H)
  105. @@ -197,9 +197,10 @@ int main(void)
  106. }
  107. " HAVE___SYNC_VAL_COMPARE_AND_SWAP)
  108. -cmake_push_check_state (RESET)
  109. -set (CMAKE_REQUIRED_LIBRARIES Threads::Threads)
  110. -check_cxx_source_compiles ("
  111. +if (Threads_FOUND)
  112. + cmake_push_check_state (RESET)
  113. + set (CMAKE_REQUIRED_LIBRARIES Threads::Threads)
  114. + check_cxx_source_compiles ("
  115. #define _XOPEN_SOURCE 500
  116. #include <pthread.h>
  117. int main(void)
  118. @@ -209,8 +210,9 @@ int main(void)
  119. pthread_rwlock_rdlock(&l);
  120. return 0;
  121. }
  122. -" HAVE_RWLOCK)
  123. -cmake_pop_check_state ()
  124. + " HAVE_RWLOCK)
  125. + cmake_pop_check_state ()
  126. +endif (Threads_FOUND)
  127. check_cxx_source_compiles ("
  128. __declspec(selectany) int a;
  129. diff --git a/src/config.h.cmake.in b/src/config.h.cmake.in
  130. index b67e8a77..20b5f1c4 100644
  131. --- a/src/config.h.cmake.in
  132. +++ b/src/config.h.cmake.in
  133. @@ -118,12 +118,12 @@
  134. /* Define to 1 if you have the <unistd.h> header file. */
  135. #cmakedefine HAVE_UNISTD_H ${HAVE_UNISTD_H}
  136. -/* Define if you have the <unwind.h> header file. */
  137. -#cmakedefine HAVE_UNWIND_H
  138. -
  139. /* Define if you linking to _Unwind_Backtrace is possible. */
  140. #cmakedefine HAVE__UNWIND_BACKTRACE
  141. +/* Define if you linking to _Unwind_GetIP is possible. */
  142. +#cmakedefine HAVE__UNWIND_GETIP
  143. +
  144. /* define if the compiler supports using expression for operator */
  145. #cmakedefine HAVE_USING_OPERATOR
  146. diff --git a/src/glog/logging.h.in b/src/glog/logging.h.in
  147. index c6def152..098e28fe 100644
  148. --- a/src/glog/logging.h.in
  149. +++ b/src/glog/logging.h.in
  150. @@ -97,7 +97,7 @@
  151. @ac_google_start_namespace@
  152. -#if @ac_cv_have_uint16_t@ // the C99 format
  153. +#if @ac_cv_have_stdint_h@ // the C99 format
  154. typedef int32_t int32;
  155. typedef uint32_t uint32;
  156. typedef int64_t int64;
  157. @@ -1822,8 +1822,8 @@ GLOG_EXPORT void SetEmailLogging(LogSeverity min_severity,
  158. // A simple function that sends email. dest is a commma-separated
  159. // list of addressess. Thread-safe.
  160. -GLOG_EXPORT bool SendEmail(const char *dest,
  161. - const char *subject, const char *body);
  162. +GLOG_EXPORT bool SendEmail(const char* dest, const char* subject,
  163. + const char* body);
  164. GLOG_EXPORT const std::vector<std::string>& GetLoggingDirectories();
  165. diff --git a/src/glog/platform.h b/src/glog/platform.h
  166. index e6144119..7893c45d 100644
  167. --- a/src/glog/platform.h
  168. +++ b/src/glog/platform.h
  169. @@ -50,6 +50,8 @@
  170. #define GLOG_OS_NETBSD
  171. #elif defined(__OpenBSD__)
  172. #define GLOG_OS_OPENBSD
  173. +#elif defined(__EMSCRIPTEN__)
  174. +#define GLOG_OS_EMSCRIPTEN
  175. #else
  176. // TODO(hamaji): Add other platforms.
  177. #error Platform not supported by glog. Please consider to contribute platform information by submitting a pull request on Github.
  178. diff --git a/src/logging.cc b/src/logging.cc
  179. index e65e80e9..1df1034a 100644
  180. --- a/src/logging.cc
  181. +++ b/src/logging.cc
  182. @@ -2188,6 +2188,7 @@ void SetExitOnDFatal(bool value) {
  183. } // namespace internal
  184. } // namespace base
  185. +#ifndef GLOG_OS_EMSCRIPTEN
  186. // Shell-escaping as we need to shell out ot /bin/mail.
  187. static const char kDontNeedShellEscapeChars[] =
  188. "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  189. @@ -2222,14 +2223,14 @@ static string ShellEscape(const string& src) {
  190. }
  191. return result;
  192. }
  193. -
  194. +#endif
  195. // use_logging controls whether the logging functions LOG/VLOG are used
  196. // to log errors. It should be set to false when the caller holds the
  197. // log_mutex.
  198. static bool SendEmailInternal(const char*dest, const char *subject,
  199. const char*body, bool use_logging) {
  200. -#ifndef __EMSCRIPTEN__
  201. +#ifndef GLOG_OS_EMSCRIPTEN
  202. if (dest && *dest) {
  203. if ( use_logging ) {
  204. VLOG(1) << "Trying to send TITLE:" << subject
  205. @@ -2275,6 +2276,12 @@ static bool SendEmailInternal(const char*dest, const char *subject,
  206. }
  207. }
  208. }
  209. +#else
  210. + (void)dest;
  211. + (void)subject;
  212. + (void)body;
  213. + (void)use_logging;
  214. + LOG(WARNING) << "Email support not available; not sending message";
  215. #endif
  216. return false;
  217. }
  218. diff --git a/src/raw_logging.cc b/src/raw_logging.cc
  219. index 43159832..befeac89 100644
  220. --- a/src/raw_logging.cc
  221. +++ b/src/raw_logging.cc
  222. @@ -59,11 +59,12 @@
  223. # include <unistd.h>
  224. #endif
  225. -#if (defined(HAVE_SYSCALL_H) || defined(HAVE_SYS_SYSCALL_H)) && (!(defined(GLOG_OS_MACOSX)))
  226. -# define safe_write(fd, s, len) syscall(SYS_write, fd, s, len)
  227. +#if (defined(HAVE_SYSCALL_H) || defined(HAVE_SYS_SYSCALL_H)) && \
  228. + (!(defined(GLOG_OS_MACOSX))) && !defined(GLOG_OS_EMSCRIPTEN)
  229. +#define safe_write(fd, s, len) syscall(SYS_write, fd, s, len)
  230. #else
  231. - // Not so safe, but what can you do?
  232. -# define safe_write(fd, s, len) write(fd, s, len)
  233. +// Not so safe, but what can you do?
  234. +#define safe_write(fd, s, len) write(fd, s, len)
  235. #endif
  236. _START_GOOGLE_NAMESPACE_
  237. diff --git a/src/stacktrace_unwind-inl.h b/src/stacktrace_unwind-inl.h
  238. index fbb5f988..dc1665b4 100644
  239. --- a/src/stacktrace_unwind-inl.h
  240. +++ b/src/stacktrace_unwind-inl.h
  241. @@ -73,7 +73,7 @@ static _Unwind_Reason_Code GetOneFrame(struct _Unwind_Context *uc, void *opq) {
  242. if (targ->skip_count > 0) {
  243. targ->skip_count--;
  244. } else {
  245. - targ->result[targ->count++] = (void *) _Unwind_GetIP(uc);
  246. + targ->result[targ->count++] = reinterpret_cast<void *>(_Unwind_GetIP(uc));
  247. }
  248. if (targ->count == targ->max_depth) {
  249. diff --git a/src/symbolize.cc b/src/symbolize.cc
  250. index 51025018..f56e97c9 100644
  251. --- a/src/symbolize.cc
  252. +++ b/src/symbolize.cc
  253. @@ -834,7 +834,7 @@ static ATTRIBUTE_NOINLINE bool SymbolizeAndDemangle(void *pc, char *out,
  254. _END_GOOGLE_NAMESPACE_
  255. -#elif defined(GLOG_OS_MACOSX) && defined(HAVE_DLADDR)
  256. +#elif (defined(GLOG_OS_MACOSX) || defined(GLOG_OS_EMSCRIPTEN)) && defined(HAVE_DLADDR)
  257. #include <dlfcn.h>
  258. #include <cstring>
  259. diff --git a/src/utilities.h b/src/utilities.h
  260. index bd0ec632..760c142c 100644
  261. --- a/src/utilities.h
  262. +++ b/src/utilities.h
  263. @@ -88,7 +88,7 @@
  264. #if defined(HAVE_LIB_UNWIND)
  265. # define STACKTRACE_H "stacktrace_libunwind-inl.h"
  266. -#elif defined(HAVE__UNWIND_BACKTRACE)
  267. +#elif defined(HAVE__UNWIND_BACKTRACE) && defined(HAVE__UNWIND_GETIP)
  268. # define STACKTRACE_H "stacktrace_unwind-inl.h"
  269. #elif !defined(NO_FRAME_POINTER)
  270. # if defined(__i386__) && __GNUC__ >= 2