Googletest export
Remove the dependency on Objective C++ in iOS builds.
252ce9c52d
introduced the use of
NSTemporaryDirectory() on iOS, which requires Core Foundation, and
Objective C++.
This CL replaces NSTemporaryDirectory() with an equivalent solution
(according to Apple's documentation at [1]) available to C/C++ code.
Avoiding Objective C++ and Core Foundation makes it easier to integrate
googletest in projects that can't use the supplied Bazel build files.
[1] https://developer.apple.com/library/archive/documentation/Security/Conceptual/SecureCodingGuide/Articles/RaceConditions.html#//apple_ref/doc/uid/TP40002585-SW10
PiperOrigin-RevId: 375474990
This commit is contained in:
parent
9741c42200
commit
c0777e6565
10
BUILD.bazel
10
BUILD.bazel
@ -56,12 +56,6 @@ config_setting(
|
|||||||
values = {"define": "absl=1"},
|
values = {"define": "absl=1"},
|
||||||
)
|
)
|
||||||
|
|
||||||
config_setting(
|
|
||||||
name = "ios",
|
|
||||||
values = {"apple_platform_type": "ios"},
|
|
||||||
visibility = [":__subpackages__"],
|
|
||||||
)
|
|
||||||
|
|
||||||
# Library that defines the FRIEND_TEST macro.
|
# Library that defines the FRIEND_TEST macro.
|
||||||
cc_library(
|
cc_library(
|
||||||
name = "gtest_prod",
|
name = "gtest_prod",
|
||||||
@ -92,10 +86,6 @@ cc_library(
|
|||||||
"googlemock/include/gmock/*.h",
|
"googlemock/include/gmock/*.h",
|
||||||
]),
|
]),
|
||||||
copts = select({
|
copts = select({
|
||||||
":ios": [
|
|
||||||
"-xobjective-c++",
|
|
||||||
"-pthread",
|
|
||||||
],
|
|
||||||
":windows": [],
|
":windows": [],
|
||||||
"//conditions:default": ["-pthread"],
|
"//conditions:default": ["-pthread"],
|
||||||
}),
|
}),
|
||||||
|
@ -80,10 +80,6 @@
|
|||||||
# include <zircon/syscalls.h>
|
# include <zircon/syscalls.h>
|
||||||
#endif // GTEST_OS_FUCHSIA
|
#endif // GTEST_OS_FUCHSIA
|
||||||
|
|
||||||
#if GTEST_OS_IOS
|
|
||||||
#import <Foundation/Foundation.h>
|
|
||||||
#endif // GTEST_OS_IOS
|
|
||||||
|
|
||||||
#include "gtest/gtest-spi.h"
|
#include "gtest/gtest-spi.h"
|
||||||
#include "gtest/gtest-message.h"
|
#include "gtest/gtest-message.h"
|
||||||
#include "gtest/internal/gtest-internal.h"
|
#include "gtest/internal/gtest-internal.h"
|
||||||
@ -692,8 +688,8 @@ class ThreadLocalRegistryImpl {
|
|||||||
static Mutex thread_map_mutex_;
|
static Mutex thread_map_mutex_;
|
||||||
};
|
};
|
||||||
|
|
||||||
Mutex ThreadLocalRegistryImpl::mutex_(Mutex::kStaticMutex);
|
Mutex ThreadLocalRegistryImpl::mutex_(Mutex::kStaticMutex); // NOLINT
|
||||||
Mutex ThreadLocalRegistryImpl::thread_map_mutex_(Mutex::kStaticMutex);
|
Mutex ThreadLocalRegistryImpl::thread_map_mutex_(Mutex::kStaticMutex); // NOLINT
|
||||||
|
|
||||||
ThreadLocalValueHolderBase* ThreadLocalRegistry::GetValueOnCurrentThread(
|
ThreadLocalValueHolderBase* ThreadLocalRegistry::GetValueOnCurrentThread(
|
||||||
const ThreadLocalBase* thread_local_instance) {
|
const ThreadLocalBase* thread_local_instance) {
|
||||||
@ -1099,9 +1095,9 @@ class CapturedStream {
|
|||||||
filename_ = temp_file_path;
|
filename_ = temp_file_path;
|
||||||
# else
|
# else
|
||||||
// There's no guarantee that a test has write access to the current
|
// There's no guarantee that a test has write access to the current
|
||||||
// directory, so we create the temporary file in the /tmp directory
|
// directory, so we create the temporary file in a temporary directory.
|
||||||
// instead. We use /tmp on most systems, and /sdcard on Android.
|
std::string name_template;
|
||||||
// That's because Android doesn't have /tmp.
|
|
||||||
# if GTEST_OS_LINUX_ANDROID
|
# if GTEST_OS_LINUX_ANDROID
|
||||||
// Note: Android applications are expected to call the framework's
|
// Note: Android applications are expected to call the framework's
|
||||||
// Context.getExternalStorageDirectory() method through JNI to get
|
// Context.getExternalStorageDirectory() method through JNI to get
|
||||||
@ -1114,23 +1110,46 @@ class CapturedStream {
|
|||||||
// The location /data/local/tmp is directly accessible from native code.
|
// The location /data/local/tmp is directly accessible from native code.
|
||||||
// '/sdcard' and other variants cannot be relied on, as they are not
|
// '/sdcard' and other variants cannot be relied on, as they are not
|
||||||
// guaranteed to be mounted, or may have a delay in mounting.
|
// guaranteed to be mounted, or may have a delay in mounting.
|
||||||
char name_template[] = "/data/local/tmp/gtest_captured_stream.XXXXXX";
|
name_template = "/data/local/tmp/";
|
||||||
# elif GTEST_OS_IOS
|
# elif GTEST_OS_IOS
|
||||||
NSString* temp_path = [NSTemporaryDirectory()
|
char user_temp_dir[PATH_MAX + 1];
|
||||||
stringByAppendingPathComponent:@"gtest_captured_stream.XXXXXX"];
|
|
||||||
|
|
||||||
char name_template[PATH_MAX + 1];
|
// Documented alternative to NSTemporaryDirectory() (for obtaining creating
|
||||||
strncpy(name_template, [temp_path UTF8String], PATH_MAX);
|
// a temporary directory) at
|
||||||
|
// https://developer.apple.com/library/archive/documentation/Security/Conceptual/SecureCodingGuide/Articles/RaceConditions.html#//apple_ref/doc/uid/TP40002585-SW10
|
||||||
|
//
|
||||||
|
// _CS_DARWIN_USER_TEMP_DIR (as well as _CS_DARWIN_USER_CACHE_DIR) is not
|
||||||
|
// documented in the confstr() man page at
|
||||||
|
// https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man3/confstr.3.html#//apple_ref/doc/man/3/confstr
|
||||||
|
// but are still available, according to the WebKit patches at
|
||||||
|
// https://trac.webkit.org/changeset/262004/webkit
|
||||||
|
// https://trac.webkit.org/changeset/263705/webkit
|
||||||
|
//
|
||||||
|
// The confstr() implementation falls back to getenv("TMPDIR"). See
|
||||||
|
// https://opensource.apple.com/source/Libc/Libc-1439.100.3/gen/confstr.c.auto.html
|
||||||
|
::confstr(_CS_DARWIN_USER_TEMP_DIR, user_temp_dir, sizeof(user_temp_dir));
|
||||||
|
|
||||||
|
name_template = user_temp_dir;
|
||||||
|
if (name_template.back() != GTEST_PATH_SEP_[0])
|
||||||
|
name_template.push_back(GTEST_PATH_SEP_[0]);
|
||||||
# else
|
# else
|
||||||
char name_template[] = "/tmp/captured_stream.XXXXXX";
|
name_template = "/tmp/";
|
||||||
# endif
|
# endif
|
||||||
const int captured_fd = mkstemp(name_template);
|
name_template.append("gtest_captured_stream.XXXXXX");
|
||||||
|
|
||||||
|
// mkstemp() modifies the string bytes in place, and does not go beyond the
|
||||||
|
// string's length. This results in well-defined behavior in C++17.
|
||||||
|
//
|
||||||
|
// The const_cast is needed below C++17. The constraints on std::string
|
||||||
|
// implementations in C++11 and above make assumption behind the const_cast
|
||||||
|
// fairly safe.
|
||||||
|
const int captured_fd = ::mkstemp(const_cast<char*>(name_template.data()));
|
||||||
if (captured_fd == -1) {
|
if (captured_fd == -1) {
|
||||||
GTEST_LOG_(WARNING)
|
GTEST_LOG_(WARNING)
|
||||||
<< "Failed to create tmp file " << name_template
|
<< "Failed to create tmp file " << name_template
|
||||||
<< " for test; does the test have access to the /tmp directory?";
|
<< " for test; does the test have access to the /tmp directory?";
|
||||||
}
|
}
|
||||||
filename_ = name_template;
|
filename_ = std::move(name_template);
|
||||||
# endif // GTEST_OS_WINDOWS
|
# endif // GTEST_OS_WINDOWS
|
||||||
fflush(nullptr);
|
fflush(nullptr);
|
||||||
dup2(captured_fd, fd_);
|
dup2(captured_fd, fd_);
|
||||||
|
Loading…
Reference in New Issue
Block a user