3 // This file is part of class-dump, a utility for examining the Objective-C segment of Mach-O files.
4 // Copyright (C) 1997-2019 Steve Nygard.
6 #import "CDLCBuildVersion.h"
8 #import "CDMachOFile.h"
10 static NSString *NSStringFromBuildVersionPlatform(uint32_t platform)
13 case PLATFORM_MACOS: return @"macOS";
14 case PLATFORM_IOS: return @"iOS";
15 case PLATFORM_TVOS: return @"tvOS";
16 case PLATFORM_WATCHOS: return @"watchOS";
17 case PLATFORM_BRIDGEOS: return @"bridgeOS";
18 case PLATFORM_IOSMAC: return @"iOS Mac";
19 case PLATFORM_IOSSIMULATOR: return @"iOS Simulator";
20 case PLATFORM_TVOSSIMULATOR: return @"tvOS Simulator";
21 case PLATFORM_WATCHOSSIMULATOR: return @"watchOS Simulator";
22 default: return [NSString stringWithFormat:@"Unknown platform %x", platform];
26 static NSString *NSStringFromBuildVersionTool(uint32_t tool)
29 case TOOL_CLANG: return @"clang";
30 case TOOL_SWIFT: return @"swift";
31 case TOOL_LD: return @"ld";
32 default: return [NSString stringWithFormat:@"Unknown tool %x", tool];
36 static NSString *NSStringFromBuildVersionToolNotATuple(uint64_t tuple)
38 uint32_t tool = tuple >> 32;
39 uint32_t version = tool & 0xffffffff;
40 return [NSString stringWithFormat:@"%@ %u", NSStringFromBuildVersionTool(tool), version];
43 @implementation CDLCBuildVersion
45 struct build_version_command _buildVersionCommand;
49 - (id)initWithDataCursor:(CDMachOFileDataCursor *)cursor;
51 if ((self = [super initWithDataCursor:cursor])) {
52 _buildVersionCommand.cmd = [cursor readInt32];
53 _buildVersionCommand.cmdsize = [cursor readInt32];
54 _buildVersionCommand.platform = [cursor readInt32];
55 _buildVersionCommand.minos = [cursor readInt32];
56 _buildVersionCommand.sdk = [cursor readInt32];
57 _buildVersionCommand.ntools = [cursor readInt32];
58 NSMutableArray *tools = [NSMutableArray array];
59 for (NSUInteger index = 0; index < _buildVersionCommand.ntools; index++) {
61 uint32_t tool = [cursor readInt32];
62 uint32_t version = [cursor readInt32];
63 uint64_t iso_tuples = ((uint64_t)tool << 32) | version;
64 [tools addObject:@(iso_tuples)];
66 _tools = [tools copy];
76 return _buildVersionCommand.cmd;
81 return _buildVersionCommand.cmdsize;
84 - (NSString *)buildVersionString;
86 return [NSString stringWithFormat:@"Platform: %@ %u.%u.%u, SDK: %u.%u.%u",
87 NSStringFromBuildVersionPlatform(_buildVersionCommand.platform),
88 _buildVersionCommand.minos >> 16,
89 (_buildVersionCommand.minos >> 8) & 0xff,
90 _buildVersionCommand.minos & 0xff,
92 _buildVersionCommand.sdk >> 16,
93 (_buildVersionCommand.sdk >> 8) & 0xff,
94 _buildVersionCommand.sdk & 0xff];
97 - (NSArray *)toolStrings;
99 NSMutableArray *tools = [NSMutableArray array];
101 for (NSNumber *tuple in _tools) {
102 [tools addObject:NSStringFromBuildVersionToolNotATuple([tuple unsignedLongValue])];
108 - (void)appendToString:(NSMutableString *)resultString verbose:(BOOL)isVerbose;
110 [super appendToString:resultString verbose:isVerbose];
112 [resultString appendFormat:@" Build version: %@\n", self.buildVersionString];