lpromero | df93a88 | 2017-05-04 09:42:20 | [diff] [blame] | 1 | // 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 | |
zhaoyangli | 64179ab0 | 2020-02-04 22:34:41 | [diff] [blame] | 9 | #if !defined(NDEBUG) && BUILDFLAG(IOS_ENABLE_COVERAGE) && \ |
| 10 | TARGET_IPHONE_SIMULATOR |
lpromero | df93a88 | 2017-05-04 09:42:20 | [diff] [blame] | 11 | extern "C" void __llvm_profile_set_filename(const char* name); |
| 12 | #endif |
| 13 | |
| 14 | namespace coverage_util { |
| 15 | |
| 16 | void ConfigureCoverageReportPath() { |
zhaoyangli | 64179ab0 | 2020-02-04 22:34:41 | [diff] [blame] | 17 | // 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 |
lpromero | df93a88 | 2017-05-04 09:42:20 | [diff] [blame] | 21 | static dispatch_once_t once_token; |
| 22 | dispatch_once(&once_token, ^{ |
zhaoyangli | 64179ab0 | 2020-02-04 22:34:41 | [diff] [blame] | 23 | // 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 Li | f63193a | 2020-10-14 23:49:28 | [diff] [blame] | 31 | // %c helps preserve coverage data at crash. |
| 32 | NSString* file_name = [NSString |
| 33 | stringWithFormat:@"%@-%%m-%%c.profraw", NSUUID.UUID.UUIDString]; |
zhaoyangli | 64179ab0 | 2020-02-04 22:34:41 | [diff] [blame] | 34 | NSString* file_path = |
| 35 | [shared_resources_path stringByAppendingPathComponent:file_name]; |
lpromero | df93a88 | 2017-05-04 09:42:20 | [diff] [blame] | 36 | |
| 37 | // For documentation, see: |
| 38 | // http://clang.llvm.org/docs/SourceBasedCodeCoverage.html |
| 39 | __llvm_profile_set_filename( |
zhaoyangli | 64179ab0 | 2020-02-04 22:34:41 | [diff] [blame] | 40 | [file_path cStringUsingEncoding:NSUTF8StringEncoding]); |
lpromero | df93a88 | 2017-05-04 09:42:20 | [diff] [blame] | 41 | |
| 42 | // Print the path for easier retrieval. |
zhaoyangli | 64179ab0 | 2020-02-04 22:34:41 | [diff] [blame] | 43 | NSLog(@"Coverage data at %@.", file_path); |
lpromero | df93a88 | 2017-05-04 09:42:20 | [diff] [blame] | 44 | }); |
zhaoyangli | 64179ab0 | 2020-02-04 22:34:41 | [diff] [blame] | 45 | #endif // !defined(NDEBUG) && BUILDFLAG(IOS_ENABLE_COVERAGE) && |
| 46 | // TARGET_IPHONE_SIMULATOR |
lpromero | df93a88 | 2017-05-04 09:42:20 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | } // namespace coverage_util |