Skip to content

Commit

Permalink
Merge pull request j2objc-contrib#512 from brunobowden/improve
Browse files Browse the repository at this point in the history
vendored_libraries for podspec
  • Loading branch information
brunobowden committed Oct 20, 2015
2 parents 7aad5b7 + a24617b commit cee7d0b
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -622,23 +622,23 @@ class J2objcConfig {
* <p/>
* See https://developer.apple.com/library/ios/documentation/DeveloperTools/Conceptual/cross_development/Configuring/configuring.html#//apple_ref/doc/uid/10000163i-CH1-SW2
*/
String minIosVersion = '8.3'
String minIosVersion = '6.0'

/**
* The minimum OS X version to build against. You cannot use APIs that are not supported
* in this version.
* <p/>
* See https://developer.apple.com/library/ios/documentation/DeveloperTools/Conceptual/cross_development/Configuring/configuring.html#//apple_ref/doc/uid/10000163i-CH1-SW2
*/
String minOsxVersion = '10.8'
String minOsxVersion = '10.4'

/**
* The minimum Watch OS version to build against. You cannot use APIs that are not supported
* in this version.
* <p/>
* See https://developer.apple.com/library/ios/documentation/DeveloperTools/Conceptual/cross_development/Configuring/configuring.html#//apple_ref/doc/uid/10000163i-CH1-SW2
*/
String minWatchosVersion = '2.0'
String minWatchosVersion = '1.0'

// XCODE
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import com.google.common.annotations.VisibleForTesting
import groovy.transform.CompileStatic
import org.gradle.api.DefaultTask
import org.gradle.api.InvalidUserDataException
import org.gradle.api.Project
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.OutputFile
import org.gradle.api.tasks.TaskAction
Expand Down Expand Up @@ -58,11 +59,21 @@ class PodspecTask extends DefaultTask {
@Input
File getDestLibDirFile() { return J2objcConfig.from(project).getDestLibDirFile() }

@Input
String getLibName() { return "${project.name}-j2objc" }

@Input
String getPodNameDebug() { "j2objc-${project.name}-debug" }
@Input
String getPodNameRelease() { "j2objc-${project.name}-release" }

@Input
String getMinIosVersion() { return J2objcConfig.from(project).getMinIosVersion() }
@Input
String getMinOsxVersion() { return J2objcConfig.from(project).getMinOsxVersion() }
@Input
String getMinWatchosVersion() { return J2objcConfig.from(project).getMinWatchosVersion() }


// CocoaPods podspec files that are referenced by the Podfile
@OutputFile
Expand All @@ -73,77 +84,62 @@ class PodspecTask extends DefaultTask {

@TaskAction
void podspecWrite() {
// podspec paths must be relative to podspec file, which is in buildDir
// NOTE: toURI() adds trailing slash in production but not in unit tests
URI buildDir = project.buildDir.toURI()

// Absolute path for header include, relative path for resource include
String headerIncludePath = getDestSrcMainObjDirFile().getAbsolutePath()
String resourceIncludePath = Utils.trimTrailingForwardSlash(
buildDir.relativize(getDestSrcMainResourcesDirFile().toURI()).toString())

// TODO: make this an explicit @Input
// Same for both debug and release builds
String libName = "${project.name}-j2objc"

// podspec creation
// TODO: allow custom list of libraries
// podspec paths must be relative to podspec file, which is in buildDir
String resourceIncludePath = relativizeToBuildDir(getDestSrcMainResourcesDirFile(), project)
// iOS packed libraries are shared with watchOS
String libDirIosDebug = new File(getDestLibDirFile(), '/iosDebug').absolutePath
String libDirIosRelease = new File(getDestLibDirFile(), '/iosRelease').absolutePath
String libDirOsxDebug = new File(getDestLibDirFile(), '/x86_64Debug').absolutePath
String libDirOsxRelease = new File(getDestLibDirFile(), '/x86_64Release').absolutePath

J2objcConfig j2objcConfig = J2objcConfig.from(project)
String libDirIosDebug = relativizeToBuildDir(new File(getDestLibDirFile(), 'iosDebug'), project)
String libDirIosRelease = relativizeToBuildDir(new File(getDestLibDirFile(), 'iosRelease'), project)
String libDirOsxDebug = relativizeToBuildDir(new File(getDestLibDirFile(), 'x86_64Debug'), project)
String libDirOsxRelease = relativizeToBuildDir(new File(getDestLibDirFile(), 'x86_64Release'), project)

String minIos = j2objcConfig.minIosVersion
String minOsx = j2objcConfig.minOsxVersion
String minWatchos = j2objcConfig.minWatchosVersion
validateNumericVersion(minIos, 'minIosVersion')
validateNumericVersion(minOsx, 'minOsxVersion')
validateNumericVersion(minWatchos, 'minWatchosVersion')
validateNumericVersion(getMinIosVersion(), 'minIosVersion')
validateNumericVersion(getMinOsxVersion(), 'minOsxVersion')
validateNumericVersion(getMinWatchosVersion(), 'minWatchosVersion')

String podspecContentsDebug =
genPodspec(getPodNameDebug(), headerIncludePath, resourceIncludePath,
libName, getJ2objcHome(),
libDirIosDebug, libDirOsxDebug, libDirIosDebug,
minIos, minOsx, minWatchos)
getMinIosVersion(), getMinOsxVersion(), getMinWatchosVersion(),
getLibName(), getJ2objcHome())
String podspecContentsRelease =
genPodspec(getPodNameRelease(), headerIncludePath, resourceIncludePath,
libName, getJ2objcHome(),
libDirIosRelease, libDirOsxRelease, libDirIosRelease,
minIos, minOsx, minWatchos)
getMinIosVersion(), getMinOsxVersion(), getMinWatchosVersion(),
getLibName(), getJ2objcHome())

logger.debug("Writing debug podspec... ${getPodspecDebug()}")
getPodspecDebug().write(podspecContentsDebug)
logger.debug("Writing release podspec... ${getPodspecRelease()}")
getPodspecRelease().write(podspecContentsRelease)
}

@VisibleForTesting
void validateNumericVersion(String version, String type) {
// Requires at least a major and minor version number
Matcher versionMatcher = (version =~ /^[0-9]*(\.[0-9]+)+$/)
if (!versionMatcher.find()) {
logger.warn("Non-numeric version for $type: $version")
}
static private String relativizeToBuildDir(File path, Project proj) {
// NOTE: toURI() adds trailing slash in production but not in unit tests
return Utils.trimTrailingForwardSlash(
proj.getBuildDir().toURI().relativize(path.toURI()).toString())
}

// Podspec references are relative to project.buildDir
@VisibleForTesting
static String genPodspec(String podname, String publicHeadersDir, String resourceDir,
String libName, String j2objcHome,
String libDirIos, String libDirOsx, String libDirWatchos,
String minIos, String minOsx, String minWatchos) {
String minIosVersion, String minOsxVersion, String minWatchosVersion,
String libName, String j2objcHome) {

// Relative paths for content referenced by CocoaPods
validatePodspecPath(libDirIos, true)
validatePodspecPath(libDirOsx, true)
validatePodspecPath(libDirWatchos, true)
validatePodspecPath(resourceDir, true)

// Absolute paths for Xcode command line
validatePodspecPath(libDirIos, false)
validatePodspecPath(libDirOsx, false)
validatePodspecPath(j2objcHome, false)

// Relative paths for content referenced by CocoaPods
validatePodspecPath(publicHeadersDir, false)
validatePodspecPath(resourceDir, true)

// TODO: CocoaPods strongly recommends switching from 'resources' to 'resource_bundles'
// http://guides.cocoapods.org/syntax/podspec.html#resource_bundles
Expand All @@ -159,27 +155,39 @@ class PodspecTask extends DefaultTask {
" spec.resources = '$resourceDir/**/*'\n" +
" spec.requires_arc = true\n" +
" spec.libraries = " + // continuation of same line
"'ObjC', 'guava', 'javax_inject', 'jre_emul', 'jsr305', 'z', 'icucore', '$libName'\n" +
"'ObjC', 'guava', 'javax_inject', 'jre_emul', 'jsr305', 'z', 'icucore'\n" +
" spec.ios.vendored_libraries = '$libDirIos/lib${libName}.a'\n" +
" spec.osx.vendored_libraries = '$libDirOsx/lib${libName}.a'\n" +
" spec.watchos.vendored_libraries = '$libDirWatchos/lib${libName}.a'\n" +
" spec.xcconfig = {\n" +
" 'HEADER_SEARCH_PATHS' => '$j2objcHome/include $publicHeadersDir'\n" +
" }\n" +
" spec.ios.xcconfig = {\n" +
" 'LIBRARY_SEARCH_PATHS' => '$j2objcHome/lib $libDirIos'\n" +
" 'LIBRARY_SEARCH_PATHS' => '$j2objcHome/lib'\n" +
" }\n" +
" spec.osx.xcconfig = {\n" +
" 'LIBRARY_SEARCH_PATHS' => '$j2objcHome/lib/macosx $libDirOsx'\n" +
" 'LIBRARY_SEARCH_PATHS' => '$j2objcHome/lib/macosx'\n" +
" }\n" +
" spec.watchos.xcconfig = {\n" +
" 'LIBRARY_SEARCH_PATHS' => '$j2objcHome/lib $libDirWatchos'\n" +
" 'LIBRARY_SEARCH_PATHS' => '$j2objcHome/lib'\n" +
" }\n" +
// http://guides.cocoapods.org/syntax/podspec.html#deployment_target
" spec.ios.deployment_target = '$minIos'\n" +
" spec.osx.deployment_target = '$minOsx'\n" +
" spec.watchos.deployment_target = '$minWatchos'\n" +
" spec.ios.deployment_target = '$minIosVersion'\n" +
" spec.osx.deployment_target = '$minOsxVersion'\n" +
" spec.watchos.deployment_target = '$minWatchosVersion'\n" +
" spec.osx.frameworks = 'ExceptionHandling'\n" +
"end\n"
}

@VisibleForTesting
void validateNumericVersion(String version, String type) {
// Requires at least a major and minor version number
Matcher versionMatcher = (version =~ /^[0-9]*(\.[0-9]+)+$/)
if (!versionMatcher.find()) {
logger.warn("Non-numeric version for $type: $version")
}
}

@VisibleForTesting
static void validatePodspecPath(String path, boolean relativeRequired) {
if (path.contains('//')) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ package com.github.j2objccontrib.j2objcgradle.tasks
import com.github.j2objccontrib.j2objcgradle.J2objcConfig
import org.gradle.api.InvalidUserDataException
import org.gradle.api.Project
import org.gradle.testfixtures.ProjectBuilder
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.rules.ExpectedException;
Expand Down Expand Up @@ -73,22 +71,25 @@ class PodspecTaskTest {
" spec.summary = 'Generated by the J2ObjC Gradle Plugin.'",
" spec.resources = 'j2objcOutputs/src/main/resources/**/*'",
" spec.requires_arc = true",
" spec.libraries = 'ObjC', 'guava', 'javax_inject', 'jre_emul', 'jsr305', 'z', 'icucore', '$libName'",
" spec.libraries = 'ObjC', 'guava', 'javax_inject', 'jre_emul', 'jsr305', 'z', 'icucore'",
" spec.ios.vendored_libraries = 'j2objcOutputs/lib/iosDebug/lib${libName}.a'",
" spec.osx.vendored_libraries = 'j2objcOutputs/lib/x86_64Debug/lib${libName}.a'",
" spec.watchos.vendored_libraries = 'j2objcOutputs/lib/iosDebug/lib${libName}.a'",
" spec.xcconfig = {",
" 'HEADER_SEARCH_PATHS' => '${j2objcHome}/include ${proj.file('build/j2objcOutputs/src/main/objc')}'",
" }",
" spec.ios.xcconfig = {",
" 'LIBRARY_SEARCH_PATHS' => '${j2objcHome}/lib ${proj.file('build/j2objcOutputs/lib/iosDebug').absolutePath}'",
" 'LIBRARY_SEARCH_PATHS' => '${j2objcHome}/lib'",
" }",
" spec.osx.xcconfig = {",
" 'LIBRARY_SEARCH_PATHS' => '${j2objcHome}/lib/macosx ${proj.file('build/j2objcOutputs/lib/x86_64Debug').absolutePath}'",
" 'LIBRARY_SEARCH_PATHS' => '${j2objcHome}/lib/macosx'",
" }",
" spec.watchos.xcconfig = {",
" 'LIBRARY_SEARCH_PATHS' => '${j2objcHome}/lib ${proj.file('build/j2objcOutputs/lib/iosDebug').absolutePath}'",
" 'LIBRARY_SEARCH_PATHS' => '${j2objcHome}/lib'",
" }",
" spec.ios.deployment_target = '8.3'",
" spec.osx.deployment_target = '10.8'",
" spec.watchos.deployment_target = '2.0'",
" spec.ios.deployment_target = '6.0'",
" spec.osx.deployment_target = '10.4'",
" spec.watchos.deployment_target = '1.0'",
" spec.osx.frameworks = 'ExceptionHandling'",
"end"]
File podspecDebug = proj.file("build/${podNameDebug}.podspec")
Expand All @@ -103,24 +104,28 @@ class PodspecTaskTest {
" spec.summary = 'Generated by the J2ObjC Gradle Plugin.'",
" spec.resources = 'j2objcOutputs/src/main/resources/**/*'",
" spec.requires_arc = true",
" spec.libraries = 'ObjC', 'guava', 'javax_inject', 'jre_emul', 'jsr305', 'z', 'icucore', '$libName'",
" spec.libraries = 'ObjC', 'guava', 'javax_inject', 'jre_emul', 'jsr305', 'z', 'icucore'",
" spec.ios.vendored_libraries = 'j2objcOutputs/lib/iosRelease/lib${libName}.a'",
" spec.osx.vendored_libraries = 'j2objcOutputs/lib/x86_64Release/lib${libName}.a'",
" spec.watchos.vendored_libraries = 'j2objcOutputs/lib/iosRelease/lib${libName}.a'",
" spec.xcconfig = {",
" 'HEADER_SEARCH_PATHS' => '${j2objcHome}/include ${proj.file('build/j2objcOutputs/src/main/objc')}'",
" }",
" spec.ios.xcconfig = {",
" 'LIBRARY_SEARCH_PATHS' => '${j2objcHome}/lib ${proj.file('build/j2objcOutputs/lib/iosRelease').absolutePath}'",
" 'LIBRARY_SEARCH_PATHS' => '${j2objcHome}/lib'",
" }",
" spec.osx.xcconfig = {",
" 'LIBRARY_SEARCH_PATHS' => '${j2objcHome}/lib/macosx ${proj.file('build/j2objcOutputs/lib/x86_64Release').absolutePath}'",
" 'LIBRARY_SEARCH_PATHS' => '${j2objcHome}/lib/macosx'",
" }",
" spec.watchos.xcconfig = {",
" 'LIBRARY_SEARCH_PATHS' => '${j2objcHome}/lib ${proj.file('build/j2objcOutputs/lib/iosRelease').absolutePath}'",
" 'LIBRARY_SEARCH_PATHS' => '${j2objcHome}/lib'",
" }",
" spec.ios.deployment_target = '8.3'",
" spec.osx.deployment_target = '10.8'",
" spec.watchos.deployment_target = '2.0'",
" spec.ios.deployment_target = '6.0'",
" spec.osx.deployment_target = '10.4'",
" spec.watchos.deployment_target = '1.0'",
" spec.osx.frameworks = 'ExceptionHandling'",
"end"]

File podspecRelease = proj.file("build/${podNameRelease}.podspec")
List<String> readPodspecRelease = podspecRelease.readLines()
assert expectedPodspecRelease == readPodspecRelease
Expand All @@ -130,10 +135,10 @@ class PodspecTaskTest {
void testGenPodspec() {
List<String> podspecDebug = PodspecTask.genPodspec(
'POD-NAME', '/HEADER_INCLUDE', 'MAIN-RESOURCES',
'LIB-NAME', '/J2OBJC_HOME',
'/LIB-DIR-IOS', '/LIB-DIR-OSX', '/LIB-DIR-WATCHOS',
'LIB-DIR-IOS', 'LIB-DIR-OSX', 'LIB-DIR-WATCHOS',
// Using non-existent OS version numbers to ensure that no defaults are being used
'8.3.1', '10.8.1', '2.0.1').split('\n')
'8.3.1', '10.8.1', '2.0.1',
'LIB-NAME', '/J2OBJC_HOME').split('\n')

List<String> expectedPodspecDebug = [
"Pod::Spec.new do |spec|",
Expand All @@ -142,18 +147,21 @@ class PodspecTaskTest {
" spec.summary = 'Generated by the J2ObjC Gradle Plugin.'",
" spec.resources = 'MAIN-RESOURCES/**/*'",
" spec.requires_arc = true",
" spec.libraries = 'ObjC', 'guava', 'javax_inject', 'jre_emul', 'jsr305', 'z', 'icucore', 'LIB-NAME'",
" spec.libraries = 'ObjC', 'guava', 'javax_inject', 'jre_emul', 'jsr305', 'z', 'icucore'",
" spec.ios.vendored_libraries = 'LIB-DIR-IOS/libLIB-NAME.a'",
" spec.osx.vendored_libraries = 'LIB-DIR-OSX/libLIB-NAME.a'",
" spec.watchos.vendored_libraries = 'LIB-DIR-WATCHOS/libLIB-NAME.a'",
" spec.xcconfig = {",
" 'HEADER_SEARCH_PATHS' => '/J2OBJC_HOME/include /HEADER_INCLUDE'",
" }",
" spec.ios.xcconfig = {",
" 'LIBRARY_SEARCH_PATHS' => '/J2OBJC_HOME/lib /LIB-DIR-IOS'",
" 'LIBRARY_SEARCH_PATHS' => '/J2OBJC_HOME/lib'",
" }",
" spec.osx.xcconfig = {",
" 'LIBRARY_SEARCH_PATHS' => '/J2OBJC_HOME/lib/macosx /LIB-DIR-OSX'",
" 'LIBRARY_SEARCH_PATHS' => '/J2OBJC_HOME/lib/macosx'",
" }",
" spec.watchos.xcconfig = {",
" 'LIBRARY_SEARCH_PATHS' => '/J2OBJC_HOME/lib /LIB-DIR-WATCHOS'",
" 'LIBRARY_SEARCH_PATHS' => '/J2OBJC_HOME/lib'",
" }",
" spec.ios.deployment_target = '8.3.1'",
" spec.osx.deployment_target = '10.8.1'",
Expand Down

0 comments on commit cee7d0b

Please sign in to comment.