#import "XCPBuildSystem.h"
#import "XCPDependencyGraph.h"
#import <objc/runtime.h>
#import <sys/sysctl.h>
#define HOOK_CALL_ORIG_NA(hook_name) __hookorig_##hook_name(self, _sel);
#define HOOK_CALL_ORIG(hook_name,...) __hookorig_##hook_name(self, _sel, __VA_ARGS__);
#define DECLARE_AND_DEFINE_HOOK_NA(hook_name, return_type) \
typedef return_type (*__hooktype_##hook_name)(id self, SEL _sel); \
__hooktype_##hook_name __hookorig_##hook_name = nil; \
return_type __hooknew_##hook_name(id self, SEL _sel)
#define DECLARE_AND_DEFINE_HOOK(hook_name, return_type, ...) \
typedef return_type (*__hooktype_##hook_name)(id self, SEL _sel, __VA_ARGS__); \
__hooktype_##hook_name __hookorig_##hook_name = nil; \
return_type __hooknew_##hook_name(id self, SEL _sel, __VA_ARGS__)
#define HOOK_SETUP(hook_name, class_name, selector_name) { \
Method __hooked_method = class_getInstanceMethod([class_name class], @selector(selector_name)); \
(void*)__hookorig_##hook_name = (void*) method_setImplementation(__hooked_method, (IMP)__hooknew_##hook_name); \
}
DECLARE_AND_DEFINE_HOOK_NA(processDependencyGraphEvents, void) {
HOOK_CALL_ORIG_NA(processDependencyGraphEvents);
[self writeToGraphVizFileAtPath:[self expandedValueForString:@"$(TEMP_DIR)/depend.dot"]];
}
DECLARE_AND_DEFINE_HOOK(createDependencyGraphWithTargetDGSnapshot_, void, id dgs) {
HOOK_CALL_ORIG(createDependencyGraphWithTargetDGSnapshot_, dgs);
[self writeToGraphVizFileAtPath:[self expandedValueForString:@"$(TEMP_DIR)/depend-bis.dot"]];
}
void xcodeprofile_init() {
NSAutoreleasePool* pool = [NSAutoreleasePool new];
// Only express us inside the Xcode application
if(![[[NSProcessInfo processInfo] processName] isEqualToString:@"Xcode"]) goto exit;
// Setup
HOOK_SETUP(processDependencyGraphEvents, PBXTargetBuildContext, processDependencyGraphEvents);
HOOK_SETUP(createDependencyGraphWithTargetDGSnapshot_, PBXTargetBuildContext, createDependencyGraphWithTargetDGSnapshot:);
NSLog(@"Xcode Debugging Tools loaded");
exit:
[pool release];
}