Skip to content
Browse files

adding Common dir to hold shared code, moving Swizze functions there.

I need to use the same swizzle functions across multiple targets, so
lets stick them in this Common dir.

We're going to just include the source files themselves rather than
making a library out of them - our targets will be a mix of MRR, ARC,
and GC-code, and iOS / Mac, so linking a common library is not possible.
  • Loading branch information...
1 parent b89ca95 commit c7b3688a302415667067cb6d5c26f4dc91b00839 @fpotter fpotter committed
View
6 Common/Swizzle.h
@@ -0,0 +1,6 @@
+
+#import <objc/message.h>
+#import <objc/runtime.h>
+
+void XTSwizzleClassSelectorForFunction(Class cls, SEL sel, IMP newImp);
+void XTSwizzleSelectorForFunction(Class cls, SEL sel, IMP newImp);
View
40 Common/Swizzle.m
@@ -0,0 +1,40 @@
+
+#import "Swizzle.h"
+
+#import <Foundation/Foundation.h>
+
+void XTSwizzleClassSelectorForFunction(Class cls, SEL sel, IMP newImp)
+{
+ Class clscls = object_getClass((id)cls);
+ Method originalMethod = class_getClassMethod(cls, sel);
+
+ NSString *selectorName = [[NSString alloc] initWithFormat:
+ @"__%s_%s",
+ class_getName(cls),
+ sel_getName(sel)];
+ SEL newSelector = sel_registerName([selectorName UTF8String]);
+ [selectorName release];
+
+ class_addMethod(clscls, newSelector, newImp,
+ method_getTypeEncoding(originalMethod));
+ Method replacedMethod = class_getClassMethod(cls, newSelector);
+ method_exchangeImplementations(originalMethod, replacedMethod);
+}
+
+void XTSwizzleSelectorForFunction(Class cls, SEL sel, IMP newImp)
+{
+ Method originalMethod = class_getInstanceMethod(cls, sel);
+ const char *typeEncoding = method_getTypeEncoding(originalMethod);
+
+ NSString *selectorName = [[NSString alloc] initWithFormat:
+ @"__%s_%s",
+ class_getName(cls),
+ sel_getName(sel)];
+ SEL newSelector = sel_registerName([selectorName UTF8String]);
+ [selectorName release];
+
+ class_addMethod(cls, newSelector, newImp, typeEncoding);
+
+ Method newMethod = class_getInstanceMethod(cls, newSelector);
+ method_exchangeImplementations(originalMethod, newMethod);
+}
View
21 otest-shim/otest-shim.xcodeproj/project.pbxproj
@@ -11,6 +11,10 @@
282BFE2B1716044C0022F9FF /* otest-shim.m in Sources */ = {isa = PBXBuildFile; fileRef = 283CCA9916C2EE4C00F2E343 /* otest-shim.m */; };
283CCA9A16C2EE4C00F2E343 /* otest-shim.m in Sources */ = {isa = PBXBuildFile; fileRef = 283CCA9916C2EE4C00F2E343 /* otest-shim.m */; };
287A37E916C7776E00319E81 /* dyld-interposing.h in Headers */ = {isa = PBXBuildFile; fileRef = 287A37E816C7776E00319E81 /* dyld-interposing.h */; };
+ 28897FC9173E50F9004BA024 /* Swizzle.h in Headers */ = {isa = PBXBuildFile; fileRef = 28897FC7173E50F9004BA024 /* Swizzle.h */; };
+ 28897FCA173E50F9004BA024 /* Swizzle.h in Headers */ = {isa = PBXBuildFile; fileRef = 28897FC7173E50F9004BA024 /* Swizzle.h */; };
+ 28897FCB173E50F9004BA024 /* Swizzle.m in Sources */ = {isa = PBXBuildFile; fileRef = 28897FC8173E50F9004BA024 /* Swizzle.m */; };
+ 28897FCC173E50F9004BA024 /* Swizzle.m in Sources */ = {isa = PBXBuildFile; fileRef = 28897FC8173E50F9004BA024 /* Swizzle.m */; };
28A094B41731DA3500838CDD /* dyld_priv.h in Headers */ = {isa = PBXBuildFile; fileRef = 28A094B31731DA3500838CDD /* dyld_priv.h */; };
28E9B9DF16C32E9A00A52E4D /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 283CCA9416C2EE4C00F2E343 /* Foundation.framework */; };
/* End PBXBuildFile section */
@@ -28,6 +32,8 @@
283CCA9716C2EE4C00F2E343 /* otest-shim-Prefix.pch */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "otest-shim-Prefix.pch"; sourceTree = "<group>"; };
283CCA9916C2EE4C00F2E343 /* otest-shim.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "otest-shim.m"; sourceTree = "<group>"; };
287A37E816C7776E00319E81 /* dyld-interposing.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "dyld-interposing.h"; sourceTree = "<group>"; };
+ 28897FC7173E50F9004BA024 /* Swizzle.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Swizzle.h; sourceTree = "<group>"; };
+ 28897FC8173E50F9004BA024 /* Swizzle.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Swizzle.m; sourceTree = "<group>"; };
28A094B31731DA3500838CDD /* dyld_priv.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = dyld_priv.h; sourceTree = "<group>"; };
28E9B98216C2F2D400A52E4D /* otest-shim-ios.xcconfig */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xcconfig; path = "otest-shim-ios.xcconfig"; sourceTree = "<group>"; };
/* End PBXFileReference section */
@@ -68,6 +74,7 @@
282BFE29171602F80022F9FF /* otest-shim-osx.xcconfig */,
28E9B98216C2F2D400A52E4D /* otest-shim-ios.xcconfig */,
283CCA8E16C2EE4C00F2E343 /* Frameworks */,
+ 28897FC6173E50F9004BA024 /* Common */,
283CCA9516C2EE4C00F2E343 /* otest-shim */,
283CCA8D16C2EE4C00F2E343 /* Products */,
);
@@ -112,6 +119,16 @@
name = "Supporting Files";
sourceTree = "<group>";
};
+ 28897FC6173E50F9004BA024 /* Common */ = {
+ isa = PBXGroup;
+ children = (
+ 28897FC7173E50F9004BA024 /* Swizzle.h */,
+ 28897FC8173E50F9004BA024 /* Swizzle.m */,
+ );
+ name = Common;
+ path = ../Common;
+ sourceTree = "<group>";
+ };
/* End PBXGroup section */
/* Begin PBXHeadersBuildPhase section */
@@ -119,6 +136,7 @@
isa = PBXHeadersBuildPhase;
buildActionMask = 2147483647;
files = (
+ 28897FCA173E50F9004BA024 /* Swizzle.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -128,6 +146,7 @@
files = (
287A37E916C7776E00319E81 /* dyld-interposing.h in Headers */,
28A094B41731DA3500838CDD /* dyld_priv.h in Headers */,
+ 28897FC9173E50F9004BA024 /* Swizzle.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -201,6 +220,7 @@
buildActionMask = 2147483647;
files = (
282BFE2B1716044C0022F9FF /* otest-shim.m in Sources */,
+ 28897FCC173E50F9004BA024 /* Swizzle.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -209,6 +229,7 @@
buildActionMask = 2147483647;
files = (
283CCA9A16C2EE4C00F2E343 /* otest-shim.m in Sources */,
+ 28897FCB173E50F9004BA024 /* Swizzle.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
View
24 otest-shim/otest-shim/otest-shim.m
@@ -24,6 +24,7 @@
#import <SenTestingKit/SenTestingKit.h>
#import "../../xctool/xctool/Reporter.h"
+#import "Swizzle.h"
#import "dyld-interposing.h"
#import "dyld_priv.h"
@@ -66,19 +67,6 @@
return [[NSArray alloc] initWithObjects:className, methodName, nil];
}
-static void SwizzleClassSelectorForFunction(Class cls, SEL sel, IMP newImp)
-{
- Class clscls = object_getClass((id)cls);
- Method originalMethod = class_getClassMethod(cls, sel);
-
- NSString *selectorName = [NSString stringWithFormat:@"__%s_%s", class_getName(cls), sel_getName(sel)];
- SEL newSelector = sel_registerName([selectorName cStringUsingEncoding:[NSString defaultCStringEncoding]]);
-
- class_addMethod(clscls, newSelector, newImp, method_getTypeEncoding(originalMethod));
- Method replacedMethod = class_getClassMethod(cls, newSelector);
- method_exchangeImplementations(originalMethod, replacedMethod);
-}
-
static void PrintJSON(id JSONObject)
{
NSError *error = nil;
@@ -338,19 +326,19 @@ static ssize_t __writev(int fildes, const struct iovec *iov, int iovcnt)
// '.../SenTestingKit.framework/Versions/A/SenTestingKit'
if (strstr(info[i].imageFilePath, "SenTestingKit.framework") != NULL) {
// Since the 'SenTestLog' class now exists, we can swizzle it!
- SwizzleClassSelectorForFunction(NSClassFromString(@"SenTestLog"),
+ XTSwizzleClassSelectorForFunction(NSClassFromString(@"SenTestLog"),
@selector(testSuiteDidStart:),
(IMP)SenTestLog_testSuiteDidStart);
- SwizzleClassSelectorForFunction(NSClassFromString(@"SenTestLog"),
+ XTSwizzleClassSelectorForFunction(NSClassFromString(@"SenTestLog"),
@selector(testSuiteDidStop:),
(IMP)SenTestLog_testSuiteDidStop);
- SwizzleClassSelectorForFunction(NSClassFromString(@"SenTestLog"),
+ XTSwizzleClassSelectorForFunction(NSClassFromString(@"SenTestLog"),
@selector(testCaseDidStart:),
(IMP)SenTestLog_testCaseDidStart);
- SwizzleClassSelectorForFunction(NSClassFromString(@"SenTestLog"),
+ XTSwizzleClassSelectorForFunction(NSClassFromString(@"SenTestLog"),
@selector(testCaseDidStop:),
(IMP)SenTestLog_testCaseDidStop);
- SwizzleClassSelectorForFunction(NSClassFromString(@"SenTestLog"),
+ XTSwizzleClassSelectorForFunction(NSClassFromString(@"SenTestLog"),
@selector(testCaseDidFail:),
(IMP)SenTestLog_testCaseDidFail);
}
View
17 xcodebuild-shim/xcodebuild-fastsettings-shim/xcodebuild_fastsettings_shim.m
@@ -19,6 +19,8 @@
#import <Foundation/Foundation.h>
+#import "Swizzle.h"
+
// class-dump'ed from:
// /Applications/Xcode.app/Contents/PlugIns/Xcode3Core.ideplugin/Contents/Frameworks/DevToolsCore.framework/DevToolsCore
@interface Xcode3Target : NSObject
@@ -32,19 +34,6 @@ @interface Xcode3TargetBuildable : NSObject
@interface Xcode3TargetProduct : Xcode3TargetBuildable
@end
-static void SwizzleSelectorForFunction(Class cls, SEL sel, IMP newImp)
-{
- Method originalMethod = class_getInstanceMethod(cls, sel);
- const char *typeEncoding = method_getTypeEncoding(originalMethod);
-
- NSString *newSelectorName = [NSString stringWithFormat:@"__%s_%s", class_getName(cls), sel_getName(sel)];
- SEL newSelector = sel_registerName([newSelectorName UTF8String]);
- class_addMethod(cls, newSelector, newImp, typeEncoding);
-
- Method newMethod = class_getInstanceMethod(cls, newSelector);
- method_exchangeImplementations(originalMethod, newMethod);
-}
-
static NSArray *IDEBuildSchemeAction_buildablesForAllSchemeCommandsIncludingDependencies(id self, SEL cmd, BOOL arg)
{
NSArray *buildables = objc_msgSend(self, sel_getUid("__IDEBuildSchemeAction_buildablesForAllSchemeCommandsIncludingDependencies:"), NO);
@@ -68,7 +57,7 @@ static void SwizzleSelectorForFunction(Class cls, SEL sel, IMP newImp)
__attribute__((constructor)) static void EntryPoint()
{
- SwizzleSelectorForFunction(NSClassFromString(@"IDEBuildSchemeAction"),
+ XTSwizzleSelectorForFunction(NSClassFromString(@"IDEBuildSchemeAction"),
@selector(buildablesForAllSchemeCommandsIncludingDependencies:),
(IMP)IDEBuildSchemeAction_buildablesForAllSchemeCommandsIncludingDependencies);
View
21 xcodebuild-shim/xcodebuild-shim.xcodeproj/project.pbxproj
@@ -12,6 +12,10 @@
285F6F74171FD297001295EC /* xcodebuild-fastsettings-shim-Prefix.pch in Headers */ = {isa = PBXBuildFile; fileRef = 285F6F73171FD297001295EC /* xcodebuild-fastsettings-shim-Prefix.pch */; };
287A380F16C876A800319E81 /* Cocoa.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 283CCAAC16C2EE7200F2E343 /* Cocoa.framework */; };
287A381516C876A800319E81 /* xcodebuild_fastsettings_shim.m in Sources */ = {isa = PBXBuildFile; fileRef = 287A381416C876A800319E81 /* xcodebuild_fastsettings_shim.m */; };
+ 28897FC2173E50C5004BA024 /* Swizzle.h in Headers */ = {isa = PBXBuildFile; fileRef = 28897FC0173E50C5004BA024 /* Swizzle.h */; };
+ 28897FC3173E50C5004BA024 /* Swizzle.h in Headers */ = {isa = PBXBuildFile; fileRef = 28897FC0173E50C5004BA024 /* Swizzle.h */; };
+ 28897FC4173E50C5004BA024 /* Swizzle.m in Sources */ = {isa = PBXBuildFile; fileRef = 28897FC1173E50C5004BA024 /* Swizzle.m */; };
+ 28897FC5173E50C5004BA024 /* Swizzle.m in Sources */ = {isa = PBXBuildFile; fileRef = 28897FC1173E50C5004BA024 /* Swizzle.m */; };
/* End PBXBuildFile section */
/* Begin PBXFileReference section */
@@ -26,6 +30,8 @@
285F6F73171FD297001295EC /* xcodebuild-fastsettings-shim-Prefix.pch */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "xcodebuild-fastsettings-shim-Prefix.pch"; sourceTree = "<group>"; };
287A380E16C876A800319E81 /* xcodebuild-fastsettings-shim.dylib */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.dylib"; includeInIndex = 0; path = "xcodebuild-fastsettings-shim.dylib"; sourceTree = BUILT_PRODUCTS_DIR; };
287A381416C876A800319E81 /* xcodebuild_fastsettings_shim.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = xcodebuild_fastsettings_shim.m; sourceTree = "<group>"; };
+ 28897FC0173E50C5004BA024 /* Swizzle.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Swizzle.h; sourceTree = "<group>"; };
+ 28897FC1173E50C5004BA024 /* Swizzle.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Swizzle.m; sourceTree = "<group>"; };
/* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */
@@ -54,6 +60,7 @@
285F6F72171FD14F001295EC /* xcodebuild-shim.xcconfig */,
283CCAAB16C2EE7200F2E343 /* Frameworks */,
283CCAAA16C2EE7200F2E343 /* Products */,
+ 28897FBF173E50C5004BA024 /* Common */,
287A381016C876A800319E81 /* xcodebuild-fastsettings-shim */,
283CCAB216C2EE7200F2E343 /* xcodebuild-shim */,
);
@@ -121,6 +128,16 @@
name = "Supporting Files";
sourceTree = "<group>";
};
+ 28897FBF173E50C5004BA024 /* Common */ = {
+ isa = PBXGroup;
+ children = (
+ 28897FC0173E50C5004BA024 /* Swizzle.h */,
+ 28897FC1173E50C5004BA024 /* Swizzle.m */,
+ );
+ name = Common;
+ path = ../Common;
+ sourceTree = "<group>";
+ };
/* End PBXGroup section */
/* Begin PBXHeadersBuildPhase section */
@@ -128,6 +145,7 @@
isa = PBXHeadersBuildPhase;
buildActionMask = 2147483647;
files = (
+ 28897FC2173E50C5004BA024 /* Swizzle.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -136,6 +154,7 @@
buildActionMask = 2147483647;
files = (
285F6F74171FD297001295EC /* xcodebuild-fastsettings-shim-Prefix.pch in Headers */,
+ 28897FC3173E50C5004BA024 /* Swizzle.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -209,6 +228,7 @@
buildActionMask = 2147483647;
files = (
283CCAB716C2EE7200F2E343 /* xcodebuild_shim.m in Sources */,
+ 28897FC4173E50C5004BA024 /* Swizzle.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -217,6 +237,7 @@
buildActionMask = 2147483647;
files = (
287A381516C876A800319E81 /* xcodebuild_fastsettings_shim.m in Sources */,
+ 28897FC5173E50C5004BA024 /* Swizzle.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
View
6 xcodebuild-shim/xcodebuild-shim/xcodebuild_shim.m
@@ -29,7 +29,7 @@
static NSMutableSet *__begunLogSections = nil;
static NSMutableSet *__endedLogSections = nil;
-static void SwizzleSelectorForFunction(Class cls, SEL sel, IMP newImp)
+static void XTSwizzleSelectorForFunction(Class cls, SEL sel, IMP newImp)
{
Method originalMethod = class_getInstanceMethod(cls, sel);
const char *typeEncoding = method_getTypeEncoding(originalMethod);
@@ -236,13 +236,13 @@ static void Xcode3CommandLineBuildLogRecorder__finishEmittingClosedSection(id se
// This method is called once for every line item in the log, and is meant to announce the action
// that will be done. e.g., this would get called to print out the clang command that's about to
// be executed.
- SwizzleSelectorForFunction(NSClassFromString(@"Xcode3CommandLineBuildLogRecorder"),
+ XTSwizzleSelectorForFunction(NSClassFromString(@"Xcode3CommandLineBuildLogRecorder"),
@selector(_emitSection:),
(IMP)Xcode3CommandLineBuildLogRecorder__emitSection);
// Override -[Xcode3CommandLineBuildLogRecorder _finishEmittingClosedSection:(IDEActivityLogSection *)section]
// This method is called once for every line item in the log, and is meant to announce the result
// of something. e.g., this would print out the error text (if any) from a clang command that just ran.
- SwizzleSelectorForFunction(NSClassFromString(@"Xcode3CommandLineBuildLogRecorder"),
+ XTSwizzleSelectorForFunction(NSClassFromString(@"Xcode3CommandLineBuildLogRecorder"),
@selector(_finishEmittingClosedSection:),
(IMP)Xcode3CommandLineBuildLogRecorder__finishEmittingClosedSection);
View
10 xctool.xcworkspace/contents.xcworkspacedata
@@ -14,6 +14,16 @@
location = "group:otest-shim/otest-shim.xcodeproj">
</FileRef>
<Group
+ location = "group:Common"
+ name = "Common">
+ <FileRef
+ location = "group:Swizzle.h">
+ </FileRef>
+ <FileRef
+ location = "group:Swizzle.m">
+ </FileRef>
+ </Group>
+ <Group
location = "group:xcodebuild-lib/../Configurations"
name = "Configurations">
<FileRef

0 comments on commit c7b3688

Please sign in to comment.
Something went wrong with that request. Please try again.