Skip to content

Commit 1669400

Browse files
ksperling-applepull[bot]
authored andcommitted
Use -fmacro-prefix-map to tidy up CHIPError source file names (#25939)
* Use -fmacro-prefix-map to tidy up CHIPError source file names This makes CHIPError source paths relative to chip_root, e.g. "src/core/*" instead ending up with long path prefixes relative to the build directory. Especially for the Darwin framework we otherwise end up with paths like "../../../../../../../../Sources/CHIPFramework/connectedhomeip/*" * Bump chip-build-android image to 0.7.0
1 parent a34b432 commit 1669400

File tree

5 files changed

+127
-13
lines changed

5 files changed

+127
-13
lines changed

.github/workflows/smoketest-android.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ jobs:
3838
if: github.actor != 'restyled-io[bot]'
3939

4040
container:
41-
image: connectedhomeip/chip-build-android:0.6.51
41+
image: connectedhomeip/chip-build-android:0.7.0
4242
volumes:
4343
- "/tmp/log_output:/tmp/test_logs"
4444

src/BUILD.gn

+7
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ declare_args() {
2626
chip_monolithic_tests = false
2727
}
2828

29+
# chip_root relative to root_build_dir for macro-prefix-map.
30+
# "/." avoids trailing "/" in result when chip_root is "//"
31+
build_relative_chip_root = rebase_path("${chip_root}/.", root_build_dir)
32+
2933
config("includes") {
3034
include_dirs = [
3135
"include",
@@ -37,6 +41,9 @@ config("includes") {
3741
include_dirs += [ "${chip_root}/zzz_generated/app-common" ]
3842

3943
defines = [ "CHIP_HAVE_CONFIG_H=1" ]
44+
45+
# Make __FILE__ and related macros relative to chip_root
46+
cflags = [ "-fmacro-prefix-map=${build_relative_chip_root}/=" ]
4047
}
4148

4249
if (chip_build_tests) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/**
2+
* Copyright (c) 2023 Project CHIP Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#import <Matter/Matter.h>
18+
#import <XCTest/XCTest.h>
19+
20+
#import "MTRTestKeys.h"
21+
#import "MTRTestStorage.h"
22+
23+
@interface MTRErrorTests : XCTestCase
24+
25+
@end
26+
27+
@implementation MTRErrorTests
28+
29+
- (void)testErrorSourcePaths
30+
{
31+
// Capture error logging
32+
os_unfair_lock lock = OS_UNFAIR_LOCK_INIT;
33+
os_unfair_lock_t lockPtr = &lock;
34+
NSMutableSet<NSString *> * errors = [[NSMutableSet alloc] init];
35+
MTRSetLogCallback(MTRLogTypeError, ^(MTRLogType type, NSString * moduleName, NSString * message) {
36+
os_unfair_lock_lock(lockPtr);
37+
[errors addObject:message];
38+
os_unfair_lock_unlock(lockPtr);
39+
});
40+
41+
// Provoke an error in the C++ layer
42+
__auto_type * factory = [MTRDeviceControllerFactory sharedInstance];
43+
XCTAssertNotNil(factory);
44+
XCTAssertFalse([factory isRunning]);
45+
__auto_type * storage = [[MTRTestStorage alloc] init];
46+
__auto_type * factoryParams = [[MTRDeviceControllerFactoryParams alloc] initWithStorage:storage];
47+
XCTAssertTrue([factory startControllerFactory:factoryParams error:NULL]);
48+
__auto_type * testKeys = [[MTRTestKeys alloc] init];
49+
__auto_type * params = [[MTRDeviceControllerStartupParams alloc] initWithIPK:testKeys.ipk fabricID:@1 nocSigner:testKeys];
50+
params.vendorID = @0xFFF1u;
51+
[storage setStorageData:[NSData data] forKey:@"f/1/n"]; // fabric table will refuse to overwrite
52+
NSError * error;
53+
XCTAssertNil([factory createControllerOnNewFabric:params error:&error]);
54+
XCTAssertNotNil(error);
55+
[factory stopControllerFactory];
56+
MTRSetLogCallback(MTRLogTypeError, nil);
57+
58+
// Check the source paths baked into SDK and framework error messages
59+
NSString * sdkError = [[errors objectsPassingTest:^BOOL(NSString * error, BOOL * stop) {
60+
return [error containsString:@".cpp:"];
61+
}] anyObject];
62+
63+
NSString * sdkSource = [self sourceFileFromErrorString:sdkError];
64+
XCTAssertTrue([sdkSource hasPrefix:@"src/"], @"sdkSource: %@", sdkSource);
65+
66+
NSString * frameworkError = [[errors objectsPassingTest:^BOOL(NSString * error, BOOL * stop) {
67+
return [error containsString:@".mm:"];
68+
}] anyObject];
69+
NSString * frameworkSource = [self sourceFileFromErrorString:frameworkError];
70+
XCTAssertTrue([frameworkSource scriptingEndsWith:@".mm"]);
71+
XCTAssertFalse([frameworkSource containsString:@"/"], @"frameworkSource: %@", frameworkSource);
72+
}
73+
74+
- (NSString *)sourceFileFromErrorString:(NSString *)error
75+
{
76+
// "... Error(path/to/source.cpp:123: CHIP Error ...)..."
77+
// "Creating NSError from path/to/source.cpp:456 CHIP Error ..."
78+
NSRegularExpression * regex = [NSRegularExpression regularExpressionWithPattern:@"[ (]([^ :]+):[0-9]+: CHIP Error"
79+
options:0
80+
error:NULL];
81+
NSTextCheckingResult * result = [regex firstMatchInString:error options:0 range:NSMakeRange(0, error.length)];
82+
if (!result) {
83+
return nil;
84+
}
85+
86+
return [error substringWithRange:[result rangeAtIndex:1]];
87+
}
88+
89+
- (void)tearDown
90+
{
91+
[MTRDeviceControllerFactory.sharedInstance stopControllerFactory];
92+
}
93+
94+
@end

src/darwin/Framework/Matter.xcodeproj/project.pbxproj

+18-12
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@
109109
3CF134AB289D8DF70017A19E /* MTRDeviceAttestationInfo.h in Headers */ = {isa = PBXBuildFile; fileRef = 3CF134AA289D8DF70017A19E /* MTRDeviceAttestationInfo.h */; settings = {ATTRIBUTES = (Public, ); }; };
110110
3CF134AD289D8E570017A19E /* MTRDeviceAttestationInfo.mm in Sources */ = {isa = PBXBuildFile; fileRef = 3CF134AC289D8E570017A19E /* MTRDeviceAttestationInfo.mm */; };
111111
3CF134AF289D90FF0017A19E /* MTROperationalCertificateIssuer.h in Headers */ = {isa = PBXBuildFile; fileRef = 3CF134AE289D90FF0017A19E /* MTROperationalCertificateIssuer.h */; settings = {ATTRIBUTES = (Public, ); }; };
112+
3D0C484B29DA4FA0006D811F /* MTRErrorTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 3D0C484A29DA4FA0006D811F /* MTRErrorTests.m */; };
112113
3D69868529383096007314E7 /* com.csa.matter.plist in Copy Logging Preferences */ = {isa = PBXBuildFile; fileRef = 3D69868029382EF4007314E7 /* com.csa.matter.plist */; };
113114
3D843711294977000070D20A /* NSStringSpanConversion.h in Headers */ = {isa = PBXBuildFile; fileRef = 3D84370E294977000070D20A /* NSStringSpanConversion.h */; };
114115
3D843712294977000070D20A /* MTRCallbackBridgeBase.h in Headers */ = {isa = PBXBuildFile; fileRef = 3D84370F294977000070D20A /* MTRCallbackBridgeBase.h */; };
@@ -370,6 +371,7 @@
370371
3CF134AA289D8DF70017A19E /* MTRDeviceAttestationInfo.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MTRDeviceAttestationInfo.h; sourceTree = "<group>"; };
371372
3CF134AC289D8E570017A19E /* MTRDeviceAttestationInfo.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = MTRDeviceAttestationInfo.mm; sourceTree = "<group>"; };
372373
3CF134AE289D90FF0017A19E /* MTROperationalCertificateIssuer.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MTROperationalCertificateIssuer.h; sourceTree = "<group>"; };
374+
3D0C484A29DA4FA0006D811F /* MTRErrorTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MTRErrorTests.m; sourceTree = "<group>"; };
373375
3D69868029382EF4007314E7 /* com.csa.matter.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = com.csa.matter.plist; sourceTree = "<group>"; };
374376
3D84370E294977000070D20A /* NSStringSpanConversion.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = NSStringSpanConversion.h; sourceTree = "<group>"; };
375377
3D84370F294977000070D20A /* MTRCallbackBridgeBase.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MTRCallbackBridgeBase.h; sourceTree = "<group>"; };
@@ -1043,6 +1045,7 @@
10431045
3DFCB3282966684500332B35 /* MTRCertificateInfoTests.m */,
10441046
99C65E0F267282F1003402F6 /* MTRControllerTests.m */,
10451047
5AE6D4E327A99041001F2493 /* MTRDeviceTests.m */,
1048+
3D0C484A29DA4FA0006D811F /* MTRErrorTests.m */,
10461049
5A6FEC9C27B5E48800F25F42 /* MTRXPCProtocolTests.m */,
10471050
5A7947DD27BEC3F500434CF2 /* MTRXPCListenerSampleTests.m */,
10481051
B2F53AF1245B0DCF0010745E /* MTRSetupPayloadParserTests.m */,
@@ -1453,6 +1456,7 @@
14531456
1E5801C328941C050033A199 /* MTRTestOTAProvider.m in Sources */,
14541457
5A6FEC9D27B5E48900F25F42 /* MTRXPCProtocolTests.m in Sources */,
14551458
5173A47929C0E82300F67F48 /* MTRFabricInfoTests.m in Sources */,
1459+
3D0C484B29DA4FA0006D811F /* MTRErrorTests.m in Sources */,
14561460
51742B4A29CB5FC1009974FE /* MTRTestResetCommissioneeHelper.m in Sources */,
14571461
5AE6D4E427A99041001F2493 /* MTRDeviceTests.m in Sources */,
14581462
510CECA8297F72970064E0B3 /* MTROperationalCertificateIssuerTests.m in Sources */,
@@ -1634,19 +1638,19 @@
16341638
MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE;
16351639
MTL_FAST_MATH = YES;
16361640
ONLY_ACTIVE_ARCH = YES;
1637-
OTHER_CFLAGS = (
1638-
"-Wformat",
1639-
"-Wformat-nonliteral",
1640-
"-Wformat-security",
1641-
"-Wconversion",
1642-
);
16431641
OTHER_LDFLAGS = "-Wl,-unexported_symbol,\"__Z*\"";
16441642
SDKROOT = iphoneos;
16451643
SUPPORTED_PLATFORMS = "macosx iphonesimulator iphoneos appletvos appletvsimulator watchos watchsimulator";
16461644
SUPPORTS_TEXT_BASED_API = YES;
16471645
TARGETED_DEVICE_FAMILY = "1,2,3,4";
16481646
VERSIONING_SYSTEM = "apple-generic";
16491647
VERSION_INFO_PREFIX = "";
1648+
WARNING_CFLAGS = (
1649+
"-Wformat",
1650+
"-Wformat-nonliteral",
1651+
"-Wformat-security",
1652+
"-Wconversion",
1653+
);
16501654
};
16511655
name = Debug;
16521656
};
@@ -1687,6 +1691,7 @@
16871691
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
16881692
IPHONEOS_DEPLOYMENT_TARGET = 13.4;
16891693
LIBRARY_SEARCH_PATHS = "$(TEMP_DIR)/out/lib";
1694+
OTHER_CFLAGS = "-fmacro-prefix-map=$(SRCROOT)/CHIP/=";
16901695
OTHER_LDFLAGS = "";
16911696
"OTHER_LDFLAGS[sdk=*]" = (
16921697
"-framework",
@@ -1785,12 +1790,6 @@
17851790
MTL_ENABLE_DEBUG_INFO = NO;
17861791
MTL_FAST_MATH = YES;
17871792
ONLY_ACTIVE_ARCH = YES;
1788-
OTHER_CFLAGS = (
1789-
"-Wformat",
1790-
"-Wformat-nonliteral",
1791-
"-Wformat-security",
1792-
"-Wconversion",
1793-
);
17941793
OTHER_LDFLAGS = "-Wl,-unexported_symbol,\"__Z*\"";
17951794
SDKROOT = iphoneos;
17961795
SUPPORTED_PLATFORMS = "macosx iphonesimulator iphoneos appletvos appletvsimulator watchos watchsimulator";
@@ -1799,6 +1798,12 @@
17991798
VALIDATE_PRODUCT = YES;
18001799
VERSIONING_SYSTEM = "apple-generic";
18011800
VERSION_INFO_PREFIX = "";
1801+
WARNING_CFLAGS = (
1802+
"-Wformat",
1803+
"-Wformat-nonliteral",
1804+
"-Wformat-security",
1805+
"-Wconversion",
1806+
);
18021807
};
18031808
name = Release;
18041809
};
@@ -1839,6 +1844,7 @@
18391844
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
18401845
IPHONEOS_DEPLOYMENT_TARGET = 13.4;
18411846
LIBRARY_SEARCH_PATHS = "$(TEMP_DIR)/out/lib";
1847+
OTHER_CFLAGS = "-fmacro-prefix-map=$(SRCROOT)/CHIP/=";
18421848
OTHER_LDFLAGS = "";
18431849
"OTHER_LDFLAGS[sdk=*]" = (
18441850
"-framework",

src/lib/core/tests/TestCHIPErrorStr.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,13 @@ static void CheckCoreErrorStr(nlTestSuite * inSuite, void * inContext)
194194
// by a presence of a colon proceeding the description.
195195
NL_TEST_ASSERT(inSuite, (strchr(errStr, ':') != nullptr));
196196
#endif // !CHIP_CONFIG_SHORT_ERROR_STR
197+
198+
#if CHIP_CONFIG_ERROR_SOURCE
199+
// GetFile() should be relative to ${chip_root}
200+
char const * const file = err.GetFile();
201+
NL_TEST_ASSERT(inSuite, file != nullptr);
202+
NL_TEST_ASSERT(inSuite, strstr(file, "src/lib/core/") == file);
203+
#endif // CHIP_CONFIG_ERROR_SOURCE
197204
}
198205
}
199206

0 commit comments

Comments
 (0)