Initial support for using Sonar in Swift projects (#106)

Summary:
This PR adds support for using SonarKit clients in Swift apps. Fixes #13, fixes #87

1. Swift can't import Obj-C modules which have C++ headers. For this reason, we use SonarKit as an Obj-C++ wrapper around Sonar, which is written in C++. Due to search path misconfiguration, trying to import SonarKit into a Swift project would import `xplat/Sonar/SonarPlugin.h` instead of `iOS/SonarKit/SonarPlugin.h`, which caused `file not found` errors for C++ stdlib imports like #28 because new projects don't have their search paths set up correctly.
2. The network and layout plugins have C++ definitions (struct methods, classes) in some of their headers. This causes the compiler to get confused for Swift projects, because it only supports importing Objective-C files in umbrella headers, meaning that the `SonarKit` won't build.

1. I updated the `HEADER_SEARCH_PATHS` of SonarKit.podspec's build configuration to include `${PODS_ROOT}/Headers/Private/SonarKit/**` first, which alleviates the search path issue. The Obj-C `Sample` project seems to have worked around this by including a hardcoded `${PODS_ROOT}/SonarKit/**` search path in the pbxproj, which is why Sample works but new projects (like those referenced in #28) don't. I removed this since it's no longer necessary.
2. I added a `SampleSwift` app to demonstrate using Sonar with a Swift project.
3. Because the Podfiles for `Sample` and `SampleSwift` referenced podspecs using `:podspec` instead of a concrete version, Cocoapods wouldn't copy local header files (instead, it downloads them from the source). To enable local development of these sample apps using `:path`, I added a symlink to SonarKit.podspec in the root of the directory.
4. I changed SonarKit.podspec to use a tag-based `source`, since v0.0.1 pulls from the master branch of this repo.

The layout and network plugins still don't work with Swift - in order to fix this, we'll need to work on extracting the C++ out of their headers and writing Obj-C++ wrappers for them. I decided to push this off to a later PR since this one is quite large already.

This means that we need to be able to `import SonarKit` without importing all the network/layout plugin headers. In order to make this work, I made "SonarKit/Core" the spec's `default_subspecs`.

priteshrnandgaonkar, let me know if you have any thoughts on this implementation. You can verify that the SampleSwift app works by checking out this branch, `pod install`ing in the SampleSwift directory, and building it :)

![image](https://user-images.githubusercontent.com/1168853/41928625-ac195bd8-792a-11e8-82b8-65d6233a1fbb.png)
Pull Request resolved: https://github.com/facebook/Sonar/pull/106

Reviewed By: jknoxville

Differential Revision: D8890010

Pulled By: priteshrnandgaonkar

fbshipit-source-id: 449305bcc5cbeb5787c23f51b1ecb80a5cbdad32
This commit is contained in:
Noah Gilmore
2018-07-20 04:33:03 -07:00
committed by Facebook Github Bot
parent fcfa0409e4
commit 8177675465
10 changed files with 56 additions and 178 deletions

4
.gitignore vendored
View File

@@ -1,11 +1,13 @@
node_modules
dist
website/build
yarn-error.log
# iOS / Xcode
*.xcworkspace
**/Pods/
**/xcuserdata/
build/
yarn-error.log
android/third-party/external/

1
SonarKit.podspec Symbolic link
View File

@@ -0,0 +1 @@
iOS/SonarKit.podspec

View File

@@ -112,9 +112,9 @@ and install the dependencies by running `pod install`. When you open the Xcode w
* We haven't released the dependency to CocoaPods yet, here is the [issue](https://github.com/facebook/Sonar/issues/132) by which you can track.
* If you do not use CocoaPods as a dependency management tool then currently there is no way to integrate SonarKit other than manually including all the dependencies and building it.
* For Android, Sonar works with both emulators and physical devices connected through USB. However on iOS, we don't yet support physical devices.
* Also Sonar doesn't work with swift projects as its written in C++ and had C++ dependencies. But we are working on supporting sonar for swift projects. You can find this issue [here](https://github.com/facebook/Sonar/issues/13)
* The Sonar layout and network plugins aren't supported in Swift projects since they include C++ dependencies. We're working on supporting these plugins for Swift apps. You can join the discussion on the [issues page](https://github.com/facebook/Sonar/issues).
</div>
## Ready for takeoff
Finally you need to add plugins to your Sonar client. See [Network Plugin](network-plugin.md) and [Layout Inspector Plugin](layout-plugin.md) on how to add them.
Finally, you need to add plugins to your Sonar client. See [Network Plugin](network-plugin.md) and [Layout Inspector Plugin](layout-plugin.md) for information on how to add them.

View File

@@ -1,58 +0,0 @@
PODS:
- boost-for-react-native (1.63.0)
- CocoaAsyncSocket (7.6.3)
- CocoaLibEvent (1.0.0)
- DoubleConversion (1.1.5)
- Folly (1.0.0):
- boost-for-react-native
- CocoaLibEvent (~> 1.0)
- DoubleConversion
- glog
- OpenSSL-Static (= 1.0.2.c1)
- glog (0.3.4)
- OpenSSL-Static (1.0.2.c1)
- PeerTalk (0.0.2)
- RSocket (0.10.0):
- Folly
- Sonar (0.0.1):
- Folly
- RSocket
DEPENDENCIES:
- CocoaAsyncSocket (~> 7.6)
- DoubleConversion (~> 1.1)
- Folly (~> 1.0)
- glog (~> 0.3)
- PeerTalk (~> 0.0)
- RSocket (~> 0.10)
- Sonar (~> 0.0.1)
SPEC REPOS:
https://github.com/cocoapods/specs.git:
- boost-for-react-native
- CocoaAsyncSocket
- CocoaLibEvent
- DoubleConversion
- glog
- OpenSSL-Static
https://github.com/facebook/Sonar.git:
- Folly
- PeerTalk
- RSocket
- Sonar
SPEC CHECKSUMS:
boost-for-react-native: 39c7adb57c4e60d6c5479dd8623128eb5b3f0f2c
CocoaAsyncSocket: eafaa68a7e0ec99ead0a7b35015e0bf25d2c8987
CocoaLibEvent: 2fab71b8bd46dd33ddb959f7928ec5909f838e3f
DoubleConversion: e22e0762848812a87afd67ffda3998d9ef29170c
Folly: ac702dfbe9294be1002f9cddaa0c2ddb99b16288
glog: 1de0bb937dccdc981596d3b5825ebfb765017ded
OpenSSL-Static: bd17e34564a8591ad76b740318683a6caa19a13e
PeerTalk: 77481b0a8136f226b90ccf828d6061f70139ffde
RSocket: 4fdb7e562db30a2d4fceddefdc601749ffc9ebe2
Sonar: 38a48b88369cfa48f1a36150d57e2a72ef6fd33c
PODFILE CHECKSUM: d3418576aca52cddf5a9c1c7f61e1b7c606caba4
COCOAPODS: 1.5.2

View File

@@ -7,11 +7,11 @@
*/
#import "AppDelegate.h"
#import <SKIOSNetworkPlugin/SKIOSNetworkAdapter.h>
#import <SonarKit/SonarClient.h>
#import <SonarKitLayoutComponentKitSupport/SonarKitLayoutComponentKitSupport.h>
#import <SonarKitLayoutPlugin/SonarKitLayoutPlugin.h>
#import <SonarKitNetworkPlugin/SonarKitNetworkPlugin.h>
#import <SonarKitLayoutComponentKitSupport/SonarKitLayoutComponentKitSupport.h>
#import <SKIOSNetworkPlugin/SKIOSNetworkAdapter.h>
#import "MainViewController.h"
#import "RootViewController.h"
@@ -36,7 +36,7 @@
withDescriptorMapper: layoutDescriptorMapper]];
[[SonarClient sharedClient] addPlugin: [[SonarKitNetworkPlugin alloc] initWithNetworkAdapter:[SKIOSNetworkAdapter new]]];
[[SonarClient sharedClient] start];
[client start];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryBoard" bundle:nil];
MainViewController *mainViewController = [storyboard instantiateViewControllerWithIdentifier:@"MainViewController"];
@@ -47,6 +47,8 @@
[_window setRootViewController: [[UINavigationController alloc] initWithRootViewController: mainViewController]];
[_window makeKeyAndVisible];
NSLog(@"Hello from Sonar in an Objc app!");
return YES;
}

View File

@@ -1,13 +1,14 @@
project 'Sample.xcodeproj'
source 'https://github.com/facebook/Sonar.git'
source 'https://github.com/CocoaPods/Specs'
# Uncomment the next line to define a global platform for your project
swift_version = "4.1"
sonarkit_version = '0.6.12'
target 'Sample' do
pod 'SonarKit', '~>'+sonarkit_version
pod 'SonarKit/SonarKitLayoutComponentKitSupport', '~>'+sonarkit_version
pod 'SonarKit/SKIOSNetworkPlugin', '~>'+sonarkit_version
post_install do |installer|
installer.pods_project.targets.each do |target|

View File

@@ -1,91 +0,0 @@
PODS:
- boost-for-react-native (1.63.0)
- CocoaAsyncSocket (7.6.3)
- CocoaLibEvent (1.0.0)
- ComponentKit (0.23):
- Yoga (~> 1.6)
- DoubleConversion (1.1.5)
- Folly (1.0.0):
- boost-for-react-native
- CocoaLibEvent (~> 1.0)
- DoubleConversion
- glog
- OpenSSL-Static (= 1.0.2.c1)
- glog (0.3.4)
- OpenSSL-Static (1.0.2.c1)
- PeerTalk (0.0.2)
- RSocket (0.10.0):
- Folly
- Sonar (0.6.12):
- Folly (~> 1.0)
- RSocket (~> 0.10)
- SonarKit (0.6.12):
- SonarKit/Core (= 0.6.12)
- SonarKit/SKIOSNetworkPlugin (= 0.6.12)
- SonarKit/SonarKitLayoutComponentKitSupport (= 0.6.12)
- SonarKit/SonarKitLayoutPlugin (= 0.6.12)
- SonarKit/SonarKitNetworkPlugin (= 0.6.12)
- SonarKit/Core (0.6.12):
- CocoaAsyncSocket (~> 7.6)
- Folly (~> 1.0)
- OpenSSL-Static (= 1.0.2.c1)
- PeerTalk (~> 0.0.2)
- Sonar (~> 0.6)
- SonarKit/SKIOSNetworkPlugin (0.6.12):
- SonarKit/Core
- SonarKit/SonarKitNetworkPlugin
- SonarKit/SonarKitLayoutComponentKitSupport (0.6.12):
- ComponentKit
- SonarKit/Core
- SonarKit/SonarKitLayoutPlugin
- Yoga (~> 1.9)
- SonarKit/SonarKitLayoutPlugin (0.6.12):
- SonarKit/Core
- Yoga (~> 1.9)
- YogaKit (~> 1.8)
- SonarKit/SonarKitNetworkPlugin (0.6.12):
- SonarKit/Core
- Yoga (1.9.0)
- YogaKit (1.9.0):
- Yoga (~> 1.9)
DEPENDENCIES:
- SonarKit (~> 0.6)
SPEC REPOS:
https://github.com/cocoapods/specs.git:
- boost-for-react-native
- CocoaAsyncSocket
- CocoaLibEvent
- ComponentKit
- DoubleConversion
- glog
- OpenSSL-Static
- Yoga
- YogaKit
https://github.com/facebook/Sonar.git:
- Folly
- PeerTalk
- RSocket
- Sonar
- SonarKit
SPEC CHECKSUMS:
boost-for-react-native: 39c7adb57c4e60d6c5479dd8623128eb5b3f0f2c
CocoaAsyncSocket: eafaa68a7e0ec99ead0a7b35015e0bf25d2c8987
CocoaLibEvent: 2fab71b8bd46dd33ddb959f7928ec5909f838e3f
ComponentKit: bf8b4c4e7448bd15b3a8e5a71174ea7aa6cea98d
DoubleConversion: e22e0762848812a87afd67ffda3998d9ef29170c
Folly: ac702dfbe9294be1002f9cddaa0c2ddb99b16288
glog: 1de0bb937dccdc981596d3b5825ebfb765017ded
OpenSSL-Static: bd17e34564a8591ad76b740318683a6caa19a13e
PeerTalk: 77481b0a8136f226b90ccf828d6061f70139ffde
RSocket: 4fdb7e562db30a2d4fceddefdc601749ffc9ebe2
Sonar: f11a40a5e9efbf5541b8f9bfd9d09ad60c5846ab
SonarKit: 94669b66d013be2d8c4d4340f6ddf4938ae50426
Yoga: aaae8abea68951f60bee05f6277d3eed90bb91bb
YogaKit: d447a9bb808718e6f58e52a2255a8050081a3ead
PODFILE CHECKSUM: 09df00fabd58860485ad428096003778865f468d
COCOAPODS: 1.5.2

View File

@@ -340,7 +340,6 @@
"\"${PODS_ROOT}/Headers/Public/Yoga\"",
"\"${PODS_ROOT}/Headers/Public/boost-for-react-native\"",
"\"${PODS_ROOT}/Headers/Public/glog\"/**",
"\"${PODS_ROOT}/SonarKit\"/**",
);
INFOPLIST_FILE = "$(SRCROOT)/Info.plist";
IPHONEOS_DEPLOYMENT_TARGET = 11.3;
@@ -405,7 +404,6 @@
"\"${PODS_ROOT}/Headers/Public/Yoga\"",
"\"${PODS_ROOT}/Headers/Public/boost-for-react-native\"",
"\"${PODS_ROOT}/Headers/Public/glog\"/**",
"\"${PODS_ROOT}/SonarKit\"/**",
);
INFOPLIST_FILE = "$(SRCROOT)/Info.plist";
IPHONEOS_DEPLOYMENT_TARGET = 11.3;

View File

@@ -1,10 +1,10 @@
folly_compiler_flags = '-DFB_SONARKIT_ENABLED=1 -DFOLLY_NO_CONFIG -DFOLLY_MOBILE=1 -DFOLLY_USE_LIBCPP=1 -DFOLLY_HAVE_LIBGFLAGS=0 -DFOLLY_HAVE_LIBJEMALLOC=0 -DFOLLY_HAVE_PREADV=0 -DFOLLY_HAVE_PWRITEV=0 -DFOLLY_HAVE_TFO=0 -DFOLLY_USE_SYMBOLIZER=0'
yoga_version = '~> 1.9'
yogakit_version = '~>1.8'
sonarkit_version = '0.6.12'
sonarkit_version = '0.6.13'
Pod::Spec.new do |spec|
spec.name = 'SonarKit'
spec.version = '0.6.12'
spec.version = sonarkit_version
spec.license = { :type => 'MIT' }
spec.homepage = 'https://github.com/facebook/Sonar'
spec.summary = 'Sonar iOS podspec'
@@ -14,28 +14,47 @@ Pod::Spec.new do |spec|
:tag=> "v"+sonarkit_version }
spec.module_name = 'SonarKit'
spec.platforms = { :ios => "8.4" }
spec.default_subspecs = "Core"
# This subspec is necessary since FBMacros.h is imported as <FBDefines/FBMacros.h>
# inside SKMacros.h, which is a public header file. Defining this directory as a
# subspec with header_dir = 'FBDefines' allows this to work, even though it wouldn't
# generally (you would need to import <SonarKit/FBDefines/FBMacros.h>)
spec.subspec 'FBDefines' do |ss|
ss.header_dir = 'FBDefines'
ss.compiler_flags = folly_compiler_flags
ss.source_files = 'iOS/FBDefines/**/*.h'
ss.public_header_files = 'iOS/FBDefines/**/*.h'
end
spec.subspec 'FBCxxUtils' do |ss|
ss.header_dir = 'FBCxxUtils'
ss.compiler_flags = folly_compiler_flags
ss.source_files = 'iOS/SonarKit/FBCxxUtils/**/*.{h,mm}'
# We set these files as private headers since they only need to be accessed
# by other SonarKit source files
ss.private_header_files = 'iOS/SonarKit/FBCxxUtils/**/*.h'
end
spec.subspec "Core" do |ss|
ss.dependency 'SonarKit/FBDefines'
ss.dependency 'SonarKit/FBCxxUtils'
ss.dependency 'Folly', '~>1.0'
ss.dependency 'Sonar', '~>'+sonarkit_version
ss.dependency 'CocoaAsyncSocket', '~> 7.6'
ss.dependency 'PeerTalk', '~>0.0.2'
ss.dependency 'OpenSSL-Static', '1.0.2.c1'
ss.source_files = 'iOS/FBDefines/*.{h,cpp,m,mm}', 'iOS/SonarKit/**/*.{h,cpp,m,mm}', 'iOS/SonarKit/FBCxxUtils/*.{h, mm}'
ss.public_header_files = 'iOS/SonarKit/CppBridge/*.{h}',
'iOS/SonarKit/SonarClient.h',
'iOS/SonarKit/SonarDeviceData.h',
'iOS/SonarKit/SonarPlugin.h',
'iOS/SonarKit/SonarResponder.h',
'iOS/SonarKit/SonarConnection.h',
'iOS/SonarKit/SKMacros.h'
ss.compiler_flags = '-DFB_SONARKIT_ENABLED=1 -DFOLLY_NO_CONFIG -DFOLLY_MOBILE=1 -DFOLLY_USE_LIBCPP=1 -DFOLLY_HAVE_LIBGFLAGS=0 -DFOLLY_HAVE_LIBJEMALLOC=0 -DFOLLY_HAVE_PREADV=0 -DFOLLY_HAVE_PWRITEV=0 -DFOLLY_HAVE_TFO=0 -DFOLLY_USE_SYMBOLIZER=0'
ss.compiler_flags = folly_compiler_flags
ss.source_files = 'iOS/SonarKit/FBDefines/*.{h,cpp,m,mm}', 'iOS/SonarKit/CppBridge/*.{h,mm}', 'iOS/SonarKit/FBCxxUtils/*.{h,mm}', 'iOS/SonarKit/Utilities/**/*.{h,m}', 'iOS/SonarKit/*.{h,m,mm}'
ss.public_header_files = 'iOS/SonarKit/**/{SonarClient,SonarPlugin,SonarConnection,SonarResponder,SKMacros}.h'
header_search_paths = "\"$(PODS_ROOT)/SonarKit/iOS/SonarKit\" \"$(PODS_ROOT)\"/Headers/Private/SonarKit/** \"$(PODS_ROOT)/boost-for-react-native\" \"$(PODS_ROOT)/DoubleConversion\" \"$(PODS_ROOT)/PeerTalkSonar\""
ss.pod_target_xcconfig = { "USE_HEADERMAP" => "NO",
"HEADER_SEARCH_PATHS" => "\"$(PODS_TARGET_SRCROOT)\"/** \"$(PODS_ROOT)/boost-for-react-native\" \"$(PODS_ROOT)/DoubleConversion\" \"$(PODS_ROOT)/PeerTalkSonar\" \"$(PODS_ROOT)/ComponentKit\"/**" }
"DEFINES_MODULE" => "YES",
"HEADER_SEARCH_PATHS" => header_search_paths }
end
spec.subspec "SonarKitLayoutPlugin" do |ss|
ss.header_dir = "SonarKitLayoutPlugin"
ss.dependency 'SonarKit/Core'
ss.dependency 'Yoga', yoga_version
ss.dependency 'YogaKit', yogakit_version
@@ -57,20 +76,23 @@ Pod::Spec.new do |spec|
end
spec.subspec "SonarKitLayoutComponentKitSupport" do |ss|
ss.header_dir = "SonarKitLayoutComponentKitSupport"
ss.dependency 'SonarKit/Core'
ss.dependency 'Yoga', yoga_version
ss.dependency 'ComponentKit'
ss.dependency 'SonarKit/SonarKitLayoutPlugin'
ss.compiler_flags = folly_compiler_flags
ss.dependency 'SonarKit/SonarKitLayoutPlugin'
ss.public_header_files = 'iOS/Plugins/SonarKitLayoutPlugin/SonarKitLayoutComponentKitSupport/SonarKitLayoutComponentKitSupport.h',
'iOS/Plugins/SonarKitLayoutPlugin/SonarKitLayoutComponentKitSupport/SKComponentLayoutWrapper.h'
ss.source_files = "iOS/Plugins/SonarKitLayoutPlugin/SonarKitLayoutComponentKitSupport/**/*.{h,cpp,m,mm}"
ss.pod_target_xcconfig = { "USE_HEADERMAP" => "NO",
"HEADER_SEARCH_PATHS" => "\"$(PODS_TARGET_SRCROOT)\"" }
"HEADER_SEARCH_PATHS" => "\"$(PODS_ROOT)\"/Headers/Private/SonarKit/**" }
end
spec.subspec "SonarKitNetworkPlugin" do |ss|
ss.header_dir = "SonarKitNetworkPlugin"
ss.dependency 'SonarKit/Core'
ss.compiler_flags = folly_compiler_flags
ss.public_header_files = 'iOS/Plugins/SonarKitNetworkPlugin/SonarKitNetworkPlugin/SonarKitNetworkPlugin.h',
@@ -79,16 +101,17 @@ Pod::Spec.new do |spec|
'iOS/Plugins/SonarKitNetworkPlugin/SonarKitNetworkPlugin/SKNetworkReporter.h'
ss.source_files = "iOS/Plugins/SonarKitNetworkPlugin/SonarKitNetworkPlugin/*.{h,cpp,m,mm}"
ss.pod_target_xcconfig = { "USE_HEADERMAP" => "NO",
"HEADER_SEARCH_PATHS" => "\"$(PODS_TARGET_SRCROOT)\"" }
"HEADER_SEARCH_PATHS" => "\"$(PODS_ROOT)\"/Headers/Private/SonarKit/**" }
end
spec.subspec "SKIOSNetworkPlugin" do |ss|
ss.header_dir = "SKIOSNetworkPlugin"
ss.dependency 'SonarKit/Core'
ss.dependency 'SonarKit/SonarKitNetworkPlugin'
ss.compiler_flags = folly_compiler_flags
ss.public_header_files = 'iOS/Plugins/SonarKitNetworkPlugin/SKIOSNetworkPlugin/SKIOSNetworkAdapter.h'
ss.source_files = "iOS/Plugins/SonarKitNetworkPlugin/SKIOSNetworkPlugin/**/*.{h,cpp,m,mm}"
ss.pod_target_xcconfig = { "USE_HEADERMAP" => "NO",
"HEADER_SEARCH_PATHS" => "\"$(PODS_TARGET_SRCROOT)\"" }
"HEADER_SEARCH_PATHS" => "\"$(PODS_ROOT)\"/Headers/Private/SonarKit/**" }
end
end

View File

@@ -1,4 +1,4 @@
sonarkit_version = '0.6.12'
sonarkit_version = '0.6.13'
Pod::Spec.new do |spec|
spec.name = 'Sonar'
spec.version = sonarkit_version