aboutsummaryrefslogtreecommitdiff
path: root/XcodePlugin/GTMXcodeGCovItem.m
blob: 866f83ea5387da3bad3dbd7da2ad7f1651ee6f35 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
//
//  GTMXcodeGCovItem.m
//
//  Copyright 2007-2009 Google Inc.
//
//  Licensed under the Apache License, Version 2.0 (the "License"); you may not
//  use this file except in compliance with the License.  You may obtain a copy
//  of the License at
//
//  http://www.apache.org/licenses/LICENSE-2.0
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
//  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
//  License for the specific language governing permissions and limitations under
//  the License.
//

#import "GTMXcodeMenuItem.h"
#import "GTMXcodePlugin.h"
#import "PBXAppDelegate.h"
#import "PBXExtendedApplication.h"
#import "PBXProject.h"
#import "PBXTarget.h"
#import "GTMMethodCheck.h"
#import "NSTask+Script.h"
#import "GTMDefines.h"

// Indices for our various menu items
enum {
  kGTMXcodeGCovSeparatorItemIndex = 19,
  kGTMXcodeGCovEnableCoverageItemIndex,
  kGTMXcodeGCovCheckCoverageMenuItemIndex,
  kGTMXcodeGCovCleanCoverageMenuItemIndex,
  kGTMXcodeGCovCleanCoverageAndBuildMenuItemIndex,
};

// The different methods of open coverage provided.
typedef enum {
  kGTMXcodeGCovOpenFile,
  kGTMXcodeGCovOpenTarget,
  kGTMXcodeGCovOpenBuildFolder,
} GTMXcodeGCovOpenMode;

typedef enum {
  kGTMXcodeGCovCleanDataTarget,
  kGTMXcodeGCovCleanDataBuildFolder,
} GTMXcodeGCovCleanMode;

// Some paths that we resolve
NSString *const kObjectsDirPath
  = @"$(OBJECT_FILE_DIR)-$(BUILD_VARIANTS)";
NSString *const kObjectsDirNoArchPath
  = @"$(OBJECT_FILE_DIR)-$(BUILD_VARIANTS)";
NSString *const kBuildRootDirPath = @"$(BUILD_ROOT)";

// the title for our menu items w/ submenus
NSString *kShowCodeCoverageForMenuTitle = @"Show Code Coverage For";
NSString *kCleanCodeCoverageDataForMenuTitle = @"Clean Code Coverage Data For";

// Separator above the GCov menu items
@interface GTMXcodeGCovSeparatorItem : GTMXcodeMenuItem
@end

// Enable Code Coverage menu item
@interface GTMXcodeGCovEnableItem : GTMXcodeMenuItem
@end

// Check coverage for menu
@interface GTMXcodeGCovCoverageMenuItem : GTMXcodeMenuItem {
  NSString *title_;
  int index_;
}
- (id)initWithTitle:(NSString *)title index:(int)index;
@end

// Check coverage for option
@interface GTMXcodeGCovCheckCoverageItem : GTMXcodeMenuItem {
  NSString *title_;
  GTMXcodeGCovOpenMode mode_;
  int index_;
}
- (id)initWithTitle:(NSString *)title
               mode:(GTMXcodeGCovOpenMode)mode
              index:(int)index;
@end

// Clean coverage data item
@interface GTMXcodeGCovCleanItem : GTMXcodeMenuItem {
  NSString *title_;
  GTMXcodeGCovCleanMode mode_;
  int index_;
}
- (id)initWithTitle:(NSString *)title
               mode:(GTMXcodeGCovCleanMode)mode
              index:(int)index;
@end

@interface GTMXcodeGCovCleanAndBuildItem : GTMXcodeMenuItem
@end

// Category for checking if gcov is enabled on current target
@interface PBXExtendedApplication (GTMXcodeGCovMenuItemAdditions)
- (BOOL)gtm_gcovEnabledForActiveTarget;
@end

@interface NSString (GTMXcodeGCovItem)
- (BOOL)gtm_isCOrObjCFile;
@end

@implementation GTMXcodeGCovSeparatorItem
+ (void)load {
  [GTMXcodePlugin registerMenuItem:[[[self alloc] init] autorelease]];
}

- (NSString*)title {
  return @"-";
}

- (NSMenu*)insertionMenu {
  return [[NSApp delegate] buildMenu];
}

- (int)insertionIndex {
  return kGTMXcodeGCovSeparatorItemIndex;
}
@end

@implementation GTMXcodeGCovCoverageMenuItem
+ (void)load {
  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  GTMXcodeGCovCoverageMenuItem *item
    = [[[self alloc] initWithTitle:kShowCodeCoverageForMenuTitle
                             index:kGTMXcodeGCovCheckCoverageMenuItemIndex]
       autorelease];
  [GTMXcodePlugin registerMenuItem:item];
  item = [[[self alloc] initWithTitle:kCleanCodeCoverageDataForMenuTitle
                                index:kGTMXcodeGCovCleanCoverageMenuItemIndex]
          autorelease];
  [GTMXcodePlugin registerMenuItem:item];
  [pool release];
}

- (id)initWithTitle:(NSString *)title index:(int)idx {
  if ((self = [super init])) {
    title_ = title;
    index_ = idx;
  }
  return self;
}

- (NSString*)title {
  return title_;
}

- (NSMenu*)insertionMenu {
  return [[NSApp delegate] buildMenu];
}

- (int)insertionIndex {
  return index_;
}

- (void)wasInserted:(NSMenuItem*)item {
  NSMenu *menu = [[[NSMenu alloc] initWithTitle:title_] autorelease];
  [item setSubmenu:menu];
}
@end

@implementation GTMXcodeGCovCheckCoverageItem
GTM_METHOD_CHECK(NSTask, gtm_runScript:withArguments:);

+ (void)load {
  struct OpenItemDesc {
    NSString *title;
    GTMXcodeGCovOpenMode mode;
    int index;
  };

  struct OpenItemDesc items [] = {
    { @"Selected file", kGTMXcodeGCovOpenFile, 0 },
    { @"Current target", kGTMXcodeGCovOpenTarget, 1 },
    { @"Current project", kGTMXcodeGCovOpenBuildFolder, 2 },
  };
  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  for(size_t i = 0; i < sizeof(items) / sizeof(struct OpenItemDesc); ++i) {
    GTMXcodeGCovCheckCoverageItem *item
      = [[[self alloc] initWithTitle:items[i].title
                                mode:items[i].mode
                               index:items[i].index] autorelease];
    [GTMXcodePlugin registerMenuItem:item];
  }
  [pool release];
}

- (id)initWithTitle:(NSString *)title
               mode:(GTMXcodeGCovOpenMode)mode
              index:(int)idx {
  if ((self = [super init])) {
    title_ = title;
    mode_ = mode;
    index_ = idx;
  }
  return self;
}

- (NSMenu*)insertionMenu {
  NSMenu *menu = [[NSApp delegate] buildMenu];
  NSInteger menuIndex
    = [menu indexOfItemWithTitle:kShowCodeCoverageForMenuTitle];
  NSMenuItem *menuItem = [menu itemAtIndex:menuIndex];
  return [menuItem submenu];
}

- (NSString*)title {
  return title_;
}

- (void)action:(id)sender {
  NSString *pathToOpen = nil;
  PBXProject *project = [NSApp currentProject];
  PBXTarget *target = [project activeTarget];
  NSString *buildConfig = [project activeBuildConfigurationName];
  if (mode_ == kGTMXcodeGCovOpenFile) {
    NSArray *selectedPaths = [self selectedPaths];
    NSString *selectedPath = nil;
    if ([selectedPaths count] == 1) {
      NSString *path = [selectedPaths objectAtIndex:0];
      if ([path gtm_isCOrObjCFile]) {
        selectedPath = path;
      }
      if (selectedPath) {
        NSString *srcFileName = [selectedPath lastPathComponent];
        NSUInteger fileLength = [srcFileName length];
        NSUInteger extensionLength = [[srcFileName pathExtension] length];
        NSString *subStr
          = [srcFileName substringToIndex:(fileLength - extensionLength)];
        NSString *gcdaFileName = [NSString stringWithFormat:@"%@gcda", subStr];
        NSString *objectsDir = [self pathByExpandingString:kObjectsDirPath
                                     forBuildConfiguration:buildConfig
                                                  ofTarget:target];
        NSString *activeArchitecture = [project activeArchitecture];
        NSString *archPath 
          = [objectsDir stringByAppendingPathComponent:activeArchitecture];
        NSString *gcdaPath
          = [archPath stringByAppendingPathComponent:gcdaFileName];
        pathToOpen = gcdaPath;
      }
    }
  } else if (mode_ == kGTMXcodeGCovOpenTarget) {
    NSString *objectsDirNoArch 
      = [self pathByExpandingString:kObjectsDirNoArchPath
              forBuildConfiguration:buildConfig
                           ofTarget:target];    
    pathToOpen = objectsDirNoArch;
  } else if (mode_ == kGTMXcodeGCovOpenBuildFolder) {
    NSString *buildRootDir = [self pathByExpandingString:kBuildRootDirPath
                                   forBuildConfiguration:buildConfig
                                                ofTarget:target];    
    pathToOpen = buildRootDir;
  }
  if (pathToOpen) {
    [NSTask gtm_runScript:@"opencoverage" withArguments:pathToOpen, nil];
  }
}

- (BOOL)validateMenuItem:(NSMenuItem*)menuItem {
  BOOL isGood = NO;
  switch (mode_) {
    case kGTMXcodeGCovOpenFile:
      if ([NSApp gtm_gcovEnabledForActiveTarget]) {
        NSArray *selectedPaths = [self selectedPaths];
        if ([selectedPaths count] == 1) {
          NSString *path = [selectedPaths objectAtIndex:0];
          if ([path gtm_isCOrObjCFile]) {
            isGood = YES;
          }
        }
      }
      break;
    case kGTMXcodeGCovOpenTarget:
      isGood = [NSApp gtm_gcovEnabledForActiveTarget];
      break;
    case kGTMXcodeGCovOpenBuildFolder:
      isGood = ([NSApp currentProject] != nil);
      break;
  }
  return isGood;
}

- (int)depth {
  return 2;
}

- (int)insertionIndex {
  return index_;
}

- (BOOL)allowGDTMenuIcon {
  return NO;
}
@end


@implementation GTMXcodeGCovEnableItem
GTM_METHOD_CHECK(NSTask, gtm_runScript:withArguments:);
+ (void)load {
  [GTMXcodePlugin registerMenuItem:[[[self alloc] init] autorelease]];
}

- (NSMenu*)insertionMenu {
  return [[NSApp delegate] buildMenu];
}

- (NSString*)title {
  return @"";
}

- (void)action:(id)sender {
  NSString *enabled = [NSApp gtm_gcovEnabledForActiveTarget] ? @"NO" : @"YES";
  [NSTask gtm_runScript:@"EnableGCov" withArguments:enabled, nil];
}

- (BOOL)validateMenuItem:(NSMenuItem*)menuItem {
  BOOL isGood = NO;
  NSString *title = @"Turn Code Coverage On";
  PBXProject *project = [NSApp currentProject];
  if (project) {
    isGood = YES;
    if ([NSApp gtm_gcovEnabledForActiveTarget]) {
      title = @"Turn Code Coverage Off";
    }
  }
  [menuItem setTitle:title];
  return isGood;
}

- (int)insertionIndex {
  return kGTMXcodeGCovEnableCoverageItemIndex;
}
@end

@implementation GTMXcodeGCovCleanItem
GTM_METHOD_CHECK(NSTask, gtm_runScript:withArguments:);
+ (void)load {
  struct CleanItemDesc {
    NSString *title;
    GTMXcodeGCovCleanMode mode;
    int index;
  };

  struct CleanItemDesc items [] = {
    { @"Current target", kGTMXcodeGCovCleanDataTarget, 0 },
    { @"Current project", kGTMXcodeGCovCleanDataBuildFolder, 1 },
  };

  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  for(size_t i = 0; i < sizeof(items) / sizeof(struct CleanItemDesc); ++i) {
    GTMXcodeGCovCleanItem *item = [[[self alloc] initWithTitle:items[i].title
                                                         mode:items[i].mode
                                                        index:items[i].index]
                                  autorelease];
    [GTMXcodePlugin registerMenuItem:item];
  }
  [pool release];
}

- (id)initWithTitle:(NSString *)title
               mode:(GTMXcodeGCovCleanMode)mode
              index:(int)idx {
  if ((self = [super init])) {
    title_ = title;
    mode_ = mode;
    index_ = idx;
  }
  return self;
}

- (NSMenu*)insertionMenu {
  NSMenu *menu = [[NSApp delegate] buildMenu];
  NSInteger menuIndex
    = [menu indexOfItemWithTitle:kCleanCodeCoverageDataForMenuTitle];
  NSMenuItem *menuItem = [menu itemAtIndex:menuIndex];
  return [menuItem submenu];
}

- (NSString*)title {
  return title_;
}

- (void)action:(id)sender {
  NSString *pathToClean = nil;
  PBXProject *project = [NSApp currentProject];
  PBXTarget *target = [project activeTarget];
  NSString *buildConfig = [project activeBuildConfigurationName];
  if (mode_ == kGTMXcodeGCovCleanDataTarget) {
    NSString *objectsDirNoArch
      = [self pathByExpandingString:kObjectsDirNoArchPath
              forBuildConfiguration:buildConfig
                           ofTarget:target];    
    pathToClean = objectsDirNoArch;
  } else if (mode_ == kGTMXcodeGCovCleanDataBuildFolder) {
    NSString *buildRootDir = [self pathByExpandingString:kBuildRootDirPath
                                         forBuildConfiguration:buildConfig
                                                      ofTarget:target];   
    pathToClean = buildRootDir;
  }
  if (pathToClean) {
    [NSTask gtm_runScript:@"ResetGCov" withArguments:pathToClean, nil];
  }
}

- (BOOL)validateMenuItem:(NSMenuItem*)menuItem {
  BOOL isGood = NO;
  switch (mode_) {
    case kGTMXcodeGCovCleanDataTarget:
      isGood = [NSApp gtm_gcovEnabledForActiveTarget];
      break;
    case kGTMXcodeGCovCleanDataBuildFolder:
      isGood = ([NSApp currentProject] != nil);
      break;
  }
  return isGood;
}

- (int)depth {
  return 2;
}

- (int)insertionIndex {
  return index_;
}

- (BOOL)allowGDTMenuIcon {
  return NO;
}
@end

@implementation GTMXcodeGCovCleanAndBuildItem
+ (void)load {
  [GTMXcodePlugin registerMenuItem:[[[self alloc] init] autorelease]];
}

- (NSString*)title {
  return @"Clean Project Coverage and Build";
}

- (NSMenu*)insertionMenu {
  return [[NSApp delegate] buildMenu];
}

- (int)insertionIndex {
  return kGTMXcodeGCovCleanCoverageAndBuildMenuItemIndex;
}

- (BOOL)validateMenuItem:(NSMenuItem*)menuItem {
  return [NSApp gtm_gcovEnabledForActiveTarget];
}

- (void)action:(id)sender {
  NSString *pathToClean = nil;
  PBXProject *project = [NSApp currentProject];
  PBXTarget *target = [project activeTarget];
  NSString *buildConfig = [project activeBuildConfigurationName];
  NSString *buildRootDir = [self pathByExpandingString:kBuildRootDirPath
                                 forBuildConfiguration:buildConfig
                                              ofTarget:target];    
  pathToClean = buildRootDir;
  if (pathToClean) {
    [NSTask gtm_runScript:@"CleanCovAndBuild" withArguments:pathToClean, nil];
  }
}

@end

@implementation PBXExtendedApplication (GTMXcodeGCovMenuItemAdditions)
- (BOOL)gtm_gcovEnabledForActiveTarget {
  BOOL answer = NO;
  PBXProject *project = [NSApp currentProject];
  PBXTarget *target = [project activeTarget];
  NSString *buildConfig = [project activeBuildConfigurationName];
  if (project && target && buildConfig) {
    NSString *setting = [target stringByExpandingString:@"$(OTHER_LDFLAGS)"
                             forBuildConfigurationNamed:buildConfig];
    answer = [setting rangeOfString:@"-lgcov"].length != 0;
  }
  return answer;
}
@end

@implementation NSString (GTMXcodeGCovItem)
- (BOOL)gtm_isCOrObjCFile {
  return [self hasSuffix:@".c"] || [self hasSuffix:@".cpp"]
  || [self hasSuffix:@".cc"] || [self hasSuffix:@".cp"]
  || [self hasSuffix:@".m"] || [self hasSuffix:@".mm"];
}
@end