Explorar el Código

emock: Add new package (#8073)

* add emock

* fix(try): mingw and windows

* fix: wasm, freebsd, android

* fix: freebsd

* ban iphoneos in on_install

* Update package description and add MinGW syslink

* opti: follow advice

* opti: remove duplicated lines

---------

Co-authored-by: star9029 <[email protected]>
Willaaaaaaa hace 1 semana
padre
commit
49deee93cb

+ 215 - 0
packages/e/emock/patches/v0.9.0/support_multiplat_and_fix_install.diff

@@ -0,0 +1,215 @@
+diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
+index 42ee9d1..6678504 100644
+--- a/src/CMakeLists.txt
++++ b/src/CMakeLists.txt
+@@ -180,17 +180,17 @@ ELSE()
+    SET(EMOCK_SRCS ${EMOCK_SRCS} ports/failure/stdexcept_report_failure.cpp)
+ ENDIF()
+ 
+-IF(MSVC)
++IF(WIN32)
+    SET(EMOCK_SRCS 
+        ${EMOCK_SRCS} 
+        WinCodeModifier.cpp
+-   ) 
+-ELSE(MSVC)
++   )
++ELSE()
+    SET(EMOCK_SRCS 
+        ${EMOCK_SRCS} 
+        UnixCodeModifier.cpp
+    )
+-ENDIF(MSVC)
++ENDIF()
+ ######################################################
+ SET(EMOCK_HEADERS_PATH ${EMOCK_SRC_ROOT}/include/emock)
+ 
+@@ -205,8 +205,8 @@ INSTALL(
+    TARGETS emock
+    ARCHIVE DESTINATION lib)
+ 
+-IF(MSVC)
++IF(0)
+ INSTALL(
+    FILES ${CMAKE_BINARY_DIR}/src/Debug/emock.pdb
+    DESTINATION lib)
+-ENDIF(MSVC)
++ENDIF()
+diff --git a/src/Formatter.cpp b/src/Formatter.cpp
+index 4a117ad..1858fe4 100644
+--- a/src/Formatter.cpp
++++ b/src/Formatter.cpp
+@@ -26,6 +26,16 @@
+ #include <emock/OutputStringStream.h>
+ #include <emock/Formatter.h>
+ 
++#ifdef _MSC_VER
++    #ifdef _WIN64 // [
++        typedef unsigned __int64  uintptr_t;
++    #else // _WIN64 ][
++        typedef _W64 unsigned int uintptr_t;
++    #endif // _WIN64 ]
++#else
++   #include <inttypes.h>
++#endif
++
+ EMOCK_NS_START
+ 
+ //////////////////////////////////////////
+@@ -38,7 +48,7 @@ std::string toPointerString(void* p)
+    oss << "0x";
+    oss.flags (std::ios::hex);
+    oss.fill('0'); oss.width(8);
+-   oss << reinterpret_cast<unsigned long>(p);
++   oss << reinterpret_cast<uintptr_t>(p);
+ 
+    return oss.str();
+ }
+diff --git a/src/SymbolRetriever.cpp b/src/SymbolRetriever.cpp
+index fe643da..1c38eea 100644
+--- a/src/SymbolRetriever.cpp
++++ b/src/SymbolRetriever.cpp
+@@ -15,12 +15,11 @@
+ #include <emock/TypeString.h>
+ #include <emock/ReportFailure.h>
+ 
+-#ifdef _MSC_VER
++#if defined(_MSC_VER) ||  defined(__MINGW32__)
+ 
+ 	#include <windows.h>
+ 	#include <dbghelp.h>
+     #include <vector>
+-	#pragma comment(lib, "Dbghelp.lib")
+ 
+     #ifdef _WIN64 // [
+         typedef unsigned __int64  uintptr_t;
+@@ -37,17 +36,20 @@
+ 	#include <unistd.h>
+ 	#include <cassert>
+ 	#include <stdio.h>
++    #include <dlfcn.h>
+     #if __APPLE__
+         #include <mach-o/dyld.h>
+         #include <mach-o/dyld_images.h>
+         #include <mach-o/fat.h>
+         #include <mach-o/nlist.h>
+-        #include <dlfcn.h>
+         #include <ar.h>
+         #include <limits.h>
+-    #else
+-	    #include <link.h>
++    #elif defined(__linux__)
++        #include <link.h>
+         #include <linux/limits.h>
++    #else
++        #include <link.h>
++        #include <limits.h>
+     #endif
+ 
+ #endif
+@@ -94,7 +96,7 @@ EMOCK_NS_START
+             return stringify.substr(start, stringify.find_last_not_of(')') - start + 1);
+         }
+ 
+-#ifdef _MSC_VER
++#if defined(_MSC_VER) ||  defined(__MINGW32__)
+         static std::string extractMethodSignatureName(const char* pmf_info, std::string& symbolName, const std::string& stringify) {
+ #else
+         static std::string extractMethodSignatureName(const char* pmf_info, const std::string& stringify) {
+@@ -107,7 +109,7 @@ EMOCK_NS_START
+                     std::string ret(from, pmf_info - from - 1);
+                     ret += extractMethodName(stringify);
+                     // extract symbol name
+-#ifdef _MSC_VER
++#if defined(_MSC_VER) ||  defined(__MINGW32__)
+                     symbolName = strchr(ret.c_str(), ' ') + 1;
+ #endif
+                     // extract arg list
+@@ -134,7 +136,7 @@ EMOCK_NS_START
+         }
+     }
+ 
+-#ifdef _MSC_VER
++#if defined(_MSC_VER) ||  defined(__MINGW32__)
+ 
+     std::map<std::pair<ULONG64, std::string>, std::map<int, std::string> > g_symbolCache;
+     void SymbolRetriever::reset() {
+@@ -188,7 +190,16 @@ EMOCK_NS_START
+                 }
+                 else {
+                     char filePath[MAX_PATH] = {0};
+-                    if(SymGetSymbolFile(GetCurrentProcess(), NULL, moduleInfo.ImageName, sfPdb, filePath, MAX_PATH, filePath, MAX_PATH))
++                    /*
++                    typedef enum {
++                        sfImage = 0,
++                        sfDbg,
++                        sfPdb,
++                        sfMpd,
++                        sfMax
++                    } IMAGEHLP_SF_TYPE;
++                    */
++                    if(SymGetSymbolFile(GetCurrentProcess(), NULL, moduleInfo.ImageName, 2/*sfPdb*/, filePath, MAX_PATH, filePath, MAX_PATH))
+                         return filePath;
+                 }
+             }
+diff --git a/src/Trampoline.cpp b/src/Trampoline.cpp
+index 52116da..d378ffb 100644
+--- a/src/Trampoline.cpp
++++ b/src/Trampoline.cpp
+@@ -14,7 +14,7 @@
+ #include <emock/ReportFailure.h>
+ #include <emock/ArgumentsMacroHelpers.h>
+ 
+-#ifdef _MSC_VER
++#if defined(_MSC_VER) ||  defined(__MINGW32__)
+ 
+     #include <windows.h>
+ 	#include <cassert>
+@@ -35,13 +35,19 @@
+         #include <limits.h>
+         #include <mach/mach_vm.h>
+         #include <mach/mach_init.h>
+-    #else
++    #elif defined(__linux__)
+         #include <linux/limits.h>
++    #else
++        #include <limits.h>
+ 
+     #endif
+ 
+ #endif
+ 
++#ifndef MAP_NORESERVE
++#define MAP_NORESERVE 0
++#endif
++
+ EMOCK_NS_START
+ 
+ namespace {
+@@ -62,7 +68,7 @@ static const size_t kMaxAllocationDelta = 0x80000000; // 2GB
+ static const size_t kAllocationSize     = EMOCK_PAGE_SIZE;  // 4KB
+ static const size_t kAlignmentSize      = 64;         // 64
+ 
+-#ifdef _MSC_VER
++#if defined(_MSC_VER) ||  defined(__MINGW32__)
+ 
+     #if BUILD_FOR_X64
+         #define _ADDRESS_MAX_VALUE 0x80000000000
+@@ -105,7 +111,7 @@ static const size_t kAlignmentSize      = 64;         // 64
+ 
+         if (allocated)
+         {
+-            if (std::abs((long)dst - (long)allocated) > kMaxAllocationDelta)
++            if (std::abs((int64_t)dst - (int64_t)allocated) > kMaxAllocationDelta)
+             {
+                 VirtualFree(allocated, 0, MEM_RELEASE);
+                 return NULL;
+@@ -284,7 +290,7 @@ static const size_t kAlignmentSize      = 64;         // 64
+     }
+ 
+     void* Trampoline::get4MemFunc(const void* src, void* dst) {
+-#ifdef _MSC_VER
++#if defined(_MSC_VER) ||  defined(__MINGW32__)
+         static const unsigned char ecxToArgList[] = { 0x58, 0x51, 0x50 };
+         // apply trampoline and push ecx
+     #if BUILD_FOR_X64

+ 65 - 0
packages/e/emock/xmake.lua

@@ -0,0 +1,65 @@
+package("emock")
+    set_homepage("https://github.com/ez8-co/emock")
+    set_description("Next generation cross-platform mock library for C/C++")
+    set_license("Apache-2.0")
+
+    add_urls("https://github.com/ez8-co/emock/archive/refs/tags/$(version).tar.gz",
+             "https://github.com/ez8-co/emock.git")
+
+    add_versions("v0.9.0", "376b3584e95642b10947da8244c9b592f62ac267c23949d875a0d5ffe5d32cf5")
+
+    add_patches("v0.9.0", "patches/v0.9.0/support_multiplat_and_fix_install.diff", "18af7bccff9849958cfc90f3210bfa73259ad2ebf462c41515ccce4478dbdb98")
+
+    add_configs("shared", {description = "Build shared library.", default = false, type = "boolean", readonly = true})
+    add_configs("namespace", {description = "Build with the `emock::` namespace.", default = true, type = "boolean"})
+    add_configs("test_framework", {description = "Choose the unit test framework for failure reporting", default = "STDEXCEPT", type = "string", values = {"STDEXCEPT", "gtest", "cpputest", "cppunit"}})
+
+    if is_plat("mingw", "windows") then
+        add_syslinks("dbghelp")
+    end
+
+    add_deps("cmake")
+
+    on_load(function (package)
+        local test_framework = package:config("test_framework")
+        if test_framework == "gtest" then
+            package:add("deps", "gtest")
+        elseif test_framework == "cpputest" then
+            package:add("deps", "cpputest")
+        elseif test_framework == "cppunit" then
+            package:add("deps", "cppunit")
+        end
+    end)
+
+    on_install("!iphoneos", function (package)
+        io.replace("src/CMakeLists.txt", "-fPIC", "", {plain = true})
+        io.replace("src/CMakeLists.txt", "IF(MSVC OR MINGW)\nINSTALL", "if(0)\nINSTALL", {plain = true})
+
+        -- gtest require c++17
+        local configs = {"-DCMAKE_CXX_STANDARD=17"}
+        table.insert(configs, "-DCMAKE_BUILD_TYPE=" .. (package:is_debug() and "Debug" or "Release"))
+        table.insert(configs, "-DEMOCK_NO_NAMESPACE=" .. (package:config("namespace") and "OFF" or "ON"))
+
+        local test_framework = package:config("test_framework")
+        table.insert(configs, "-DEMOCK_XUNIT=" .. test_framework)
+        if test_framework ~= "STDEXCEPT" then
+            local dir = package:dep(test_framework):installdir()
+            table.insert(configs, "-DEMOCK_XUNIT_HOME=" .. dir)
+        end
+        import("package.tools.cmake").install(package, configs)
+    end)
+
+    on_test(function (package)
+        assert(package:check_cxxsnippets({test = [[
+            int foobar(int x) {
+                return x;
+            }
+
+            void test() {
+                EMOCK(foobar)
+                    .stubs()
+                    .with(any())
+                    .will(returnValue(1));
+            }
+        ]]}, {configs = {languages = "c++17"}, includes = "emock/emock.hpp"}))
+    end)