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:
Abseil Team 2021-05-24 10:53:07 -04:00 committed by Derek Mauro
parent 9741c42200
commit c0777e6565
2 changed files with 36 additions and 27 deletions

View File

@ -56,12 +56,6 @@ config_setting(
values = {"define": "absl=1"},
)
config_setting(
name = "ios",
values = {"apple_platform_type": "ios"},
visibility = [":__subpackages__"],
)
# Library that defines the FRIEND_TEST macro.
cc_library(
name = "gtest_prod",
@ -92,10 +86,6 @@ cc_library(
"googlemock/include/gmock/*.h",
]),
copts = select({
":ios": [
"-xobjective-c++",
"-pthread",
],
":windows": [],
"//conditions:default": ["-pthread"],
}),

View File

@ -80,10 +80,6 @@
# include <zircon/syscalls.h>
#endif // GTEST_OS_FUCHSIA
#if GTEST_OS_IOS
#import <Foundation/Foundation.h>
#endif // GTEST_OS_IOS
#include "gtest/gtest-spi.h"
#include "gtest/gtest-message.h"
#include "gtest/internal/gtest-internal.h"
@ -692,8 +688,8 @@ class ThreadLocalRegistryImpl {
static Mutex thread_map_mutex_;
};
Mutex ThreadLocalRegistryImpl::mutex_(Mutex::kStaticMutex);
Mutex ThreadLocalRegistryImpl::thread_map_mutex_(Mutex::kStaticMutex);
Mutex ThreadLocalRegistryImpl::mutex_(Mutex::kStaticMutex); // NOLINT
Mutex ThreadLocalRegistryImpl::thread_map_mutex_(Mutex::kStaticMutex); // NOLINT
ThreadLocalValueHolderBase* ThreadLocalRegistry::GetValueOnCurrentThread(
const ThreadLocalBase* thread_local_instance) {
@ -1099,9 +1095,9 @@ class CapturedStream {
filename_ = temp_file_path;
# else
// There's no guarantee that a test has write access to the current
// directory, so we create the temporary file in the /tmp directory
// instead. We use /tmp on most systems, and /sdcard on Android.
// That's because Android doesn't have /tmp.
// directory, so we create the temporary file in a temporary directory.
std::string name_template;
# if GTEST_OS_LINUX_ANDROID
// Note: Android applications are expected to call the framework's
// Context.getExternalStorageDirectory() method through JNI to get
@ -1114,23 +1110,46 @@ class CapturedStream {
// The location /data/local/tmp is directly accessible from native code.
// '/sdcard' and other variants cannot be relied on, as they are not
// 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
NSString* temp_path = [NSTemporaryDirectory()
stringByAppendingPathComponent:@"gtest_captured_stream.XXXXXX"];
char user_temp_dir[PATH_MAX + 1];
char name_template[PATH_MAX + 1];
strncpy(name_template, [temp_path UTF8String], PATH_MAX);
// Documented alternative to NSTemporaryDirectory() (for obtaining creating
// 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
char name_template[] = "/tmp/captured_stream.XXXXXX";
name_template = "/tmp/";
# 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) {
GTEST_LOG_(WARNING)
<< "Failed to create tmp file " << name_template
<< " for test; does the test have access to the /tmp directory?";
}
filename_ = name_template;
filename_ = std::move(name_template);
# endif // GTEST_OS_WINDOWS
fflush(nullptr);
dup2(captured_fd, fd_);