blob: 53e135052776a7503924f874cab79dd02ed047ee [file] [log] [blame]
lpromerodf93a882017-05-04 09:42:201// Copyright 2017 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#import <Foundation/Foundation.h>
6
7#import "testing/gtest/ios_enable_coverage.h"
8
zhaoyangli64179ab02020-02-04 22:34:419#if !defined(NDEBUG) && BUILDFLAG(IOS_ENABLE_COVERAGE) && \
10 TARGET_IPHONE_SIMULATOR
lpromerodf93a882017-05-04 09:42:2011extern "C" void __llvm_profile_set_filename(const char* name);
12#endif
13
14namespace coverage_util {
15
16void ConfigureCoverageReportPath() {
zhaoyangli64179ab02020-02-04 22:34:4117// Targets won't build on real devices with BUILDFLAG(IOS_ENABLE_COVERAGE)
18// because of llvm library linking issue for arm64 architecture.
19#if !defined(NDEBUG) && BUILDFLAG(IOS_ENABLE_COVERAGE) && \
20 TARGET_IPHONE_SIMULATOR
lpromerodf93a882017-05-04 09:42:2021 static dispatch_once_t once_token;
22 dispatch_once(&once_token, ^{
zhaoyangli64179ab02020-02-04 22:34:4123 // Writes the profraw file to the simulator shared resources directory,
24 // where the app has write rights, and will be preserved after app is
25 // killed.
26 NSString* shared_resources_path =
27 NSProcessInfo.processInfo
28 .environment[@"SIMULATOR_SHARED_RESOURCES_DIRECTORY"];
29 // UUID ensures that there won't be a conflict when multiple apps are
30 // launched in one test suite in EG2. %m enables on-line profile merging.
Zhaoyang Lif63193a2020-10-14 23:49:2831 // %c helps preserve coverage data at crash.
32 NSString* file_name = [NSString
33 stringWithFormat:@"%@-%%m-%%c.profraw", NSUUID.UUID.UUIDString];
zhaoyangli64179ab02020-02-04 22:34:4134 NSString* file_path =
35 [shared_resources_path stringByAppendingPathComponent:file_name];
lpromerodf93a882017-05-04 09:42:2036
37 // For documentation, see:
38 // http://clang.llvm.org/docs/SourceBasedCodeCoverage.html
39 __llvm_profile_set_filename(
zhaoyangli64179ab02020-02-04 22:34:4140 [file_path cStringUsingEncoding:NSUTF8StringEncoding]);
lpromerodf93a882017-05-04 09:42:2041
42 // Print the path for easier retrieval.
zhaoyangli64179ab02020-02-04 22:34:4143 NSLog(@"Coverage data at %@.", file_path);
lpromerodf93a882017-05-04 09:42:2044 });
zhaoyangli64179ab02020-02-04 22:34:4145#endif // !defined(NDEBUG) && BUILDFLAG(IOS_ENABLE_COVERAGE) &&
46 // TARGET_IPHONE_SIMULATOR
lpromerodf93a882017-05-04 09:42:2047}
48
49} // namespace coverage_util