1 /*
   2  * Copyright (c) 2013, 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 ext.MAC = [:]
  27 
  28 MAC.canBuild = IS_MAC && IS_64
  29 if (!MAC.canBuild) return;
  30 
  31 // All desktop related packages should be built
  32 MAC.compileSwing = true;
  33 MAC.compileSWT = true;
  34 MAC.compileFXPackager = true;
  35 
  36 MAC.includeNull3d = true
  37 
  38 // Lambda for naming the generated libs
  39 MAC.library = { name -> return "lib${name}.dylib" as String }
  40 
  41 MAC.libDest = "lib"
  42 
  43 /*
  44  * Define settings for Mac compilation. If we don't find the preferred SDK
  45  * in the default location then we will use "xcodebuild" to locate a suitable SDK.
  46  * In extreme cases you can provide your own properties in your home dir to
  47  * override these settings or pass them on the command line.
  48  */
  49 def prefSdkVersion = "10.9"
  50 def defaultSdkPath = "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX${prefSdkVersion}.sdk";
  51 
  52 // Set the minimum API version that we require (developers do not need to override this)
  53 // Note that this is not necessarily the same as the preferred SDK version
  54 defineProperty("MACOSX_MIN_VERSION", "10.9");
  55 
  56 // Create $buildDir/mac_tools.properties file and load props from it
  57 setupTools("mac_tools",
  58     { propFile ->
  59         propFile << ""
  60         if (!file(defaultSdkPath).isDirectory()) {
  61             // Get list of all macosx sdks
  62             ByteArrayOutputStream results = new ByteArrayOutputStream();
  63             exec {
  64                 commandLine("xcodebuild", "-version", "-showsdks");
  65                 setStandardOutput(results);
  66             }
  67 
  68             BufferedReader reader = new BufferedReader(new StringReader(results.toString().trim()));
  69             // If our preferred SDK is in the list use it, else use the default
  70             String sdk = "macosx"
  71             String prefSdk = sdk + prefSdkVersion
  72             while (true) {
  73                 def line = reader.readLine();
  74                 if (line == null) break;
  75                 if (line.contains("-sdk ${prefSdk}")) {
  76                     sdk = prefSdk
  77                     break;
  78                 }
  79             }
  80 
  81             results = new ByteArrayOutputStream();
  82             exec {
  83                 commandLine("xcodebuild", "-version", "-sdk", sdk, "Path");
  84                 setStandardOutput(results);
  85             }
  86             String sdkPath = results.toString().trim();
  87             propFile << "MACOSX_SDK_PATH=" << sdkPath << "\n";
  88         }
  89     },
  90     { properties ->
  91         defineProperty("MACOSX_SDK_PATH", properties, defaultSdkPath)
  92     }
  93 )
  94 
  95 println "MACOSX_MIN_VERSION = $MACOSX_MIN_VERSION"
  96 println "MACOSX_SDK_PATH = $MACOSX_SDK_PATH"
  97 
  98 if (!file(MACOSX_SDK_PATH).isDirectory()) {
  99     throw new GradleException("FAIL: Cannot find $MACOSX_SDK_PATH")
 100 }
 101 
 102 // NOTE: There is no space between -iframework and the specified path
 103 def commonParams = [
 104         "-mmacosx-version-min=$MACOSX_MIN_VERSION",
 105         "-isysroot", "$MACOSX_SDK_PATH",
 106         "-iframework$MACOSX_SDK_PATH/System/Library/Frameworks",
 107         "-arch", "x86_64"]
 108 
 109 def ccBaseFlags = [
 110         commonParams,
 111         "-I$JDK_HOME/include",
 112         "-I$JDK_HOME/include/darwin"].flatten()
 113 
 114 
 115 def ccFlags = [
 116         ccBaseFlags,
 117         "-std=c99",
 118         "-c",
 119         IS_DEBUG_NATIVE ? "-DDEBUG" : ["-O3", "-DNDEBUG"]].flatten()
 120 
 121 def linkFlags = [
 122         commonParams,
 123         "-framework", "AppKit",
 124         "-framework", "ApplicationServices",
 125         "-framework", "OpenGL",
 126         "-framework", "QuartzCore",
 127         "-framework", "Security",
 128         "-dynamiclib", "-lobjc"].flatten();
 129 
 130 
 131 def compiler = IS_COMPILE_PARFAIT ? "parfait-clang" : "clang";
 132 def linker = IS_COMPILE_PARFAIT ? "parfait-clang++" : "clang++";
 133 
 134 MAC.glass = [:]
 135 MAC.glass.javahInclude = [
 136     "com/sun/glass/events/**",
 137     "com/sun/glass/ui/*",
 138     "com/sun/glass/ui/mac/*"]
 139 MAC.glass.nativeSource = file("${project("graphics").projectDir}/src/main/native-glass/mac")
 140 MAC.glass.compiler = compiler
 141 MAC.glass.ccFlags = [ccFlags].flatten()
 142 MAC.glass.linker = linker
 143 MAC.glass.linkFlags = [linkFlags].flatten()
 144 MAC.glass.lib = "glass"
 145 
 146 MAC.decora = [:]
 147 MAC.decora.compiler = compiler
 148 MAC.decora.ccFlags = ["-O1", "-ffast-math", "-c", ccBaseFlags].flatten()
 149 MAC.decora.linker = linker
 150 MAC.decora.linkFlags = ["-dynamiclib", commonParams].flatten()
 151 MAC.decora.lib = "decora_sse"
 152 
 153 MAC.prism = [:]
 154 MAC.prism.javahInclude = ["com/sun/prism/impl/**/*", "com/sun/prism/PresentableState*"]
 155 MAC.prism.nativeSource = file("${project("graphics").projectDir}/src/main/native-prism")
 156 MAC.prism.compiler = compiler
 157 MAC.prism.ccFlags = ["-O3", "-DINLINE=inline", "-c", ccBaseFlags].flatten()
 158 MAC.prism.linker = linker
 159 MAC.prism.linkFlags = ["-dynamiclib", commonParams].flatten()
 160 MAC.prism.lib = "prism_common"
 161 
 162 MAC.prismSW = [:]
 163 MAC.prismSW.javahInclude = ["com/sun/pisces/**/*"]
 164 MAC.prismSW.nativeSource = file("${project("graphics").projectDir}/src/main/native-prism-sw")
 165 MAC.prismSW.compiler = compiler
 166 MAC.prismSW.ccFlags = [MAC.prism.ccFlags].flatten()
 167 MAC.prismSW.linker = linker
 168 MAC.prismSW.linkFlags = [MAC.prism.linkFlags].flatten()
 169 MAC.prismSW.lib = "prism_sw"
 170 
 171 MAC.launcher = [:]
 172 MAC.launcher.compiler = compiler
 173 MAC.launcher.ccFlags = [
 174         "-std=c99",
 175         ccBaseFlags,
 176         "-framework", "Cocoa",
 177         IS_DEBUG_NATIVE ? ["-DDEBUG", "-O0"] : ["-O3", "-DNDEBUG"]].flatten()
 178 MAC.launcher.linker = linker
 179 MAC.launcher.linkFlags = ["-ldl"]
 180 
 181 MAC.launcherlibrary = [:]
 182 MAC.launcherlibrary.compiler = compiler
 183 MAC.launcherlibrary.ccFlags = [
 184         "-c",
 185         ccBaseFlags,
 186         IS_DEBUG_NATIVE ? ["-DDEBUG", "-O0"] : ["-O3", "-DNDEBUG"]].flatten()
 187     MAC.launcherlibrary.ccFlags += "-stdlib=libstdc++"
 188 MAC.launcherlibrary.linker = linker
 189 MAC.launcherlibrary.linkFlags = ["-ldl", "-dynamiclib",
 190         "-framework", "Cocoa",
 191         "-stdlib=libstdc++"]
 192 
 193 MAC.iio = [:]
 194 MAC.iio.javahInclude = ["com/sun/javafx/iio/**/*"]
 195 MAC.iio.nativeSource = [
 196     file("${project("graphics").projectDir}/src/main/native-iio"),
 197     file("${project("graphics").projectDir}/src/main/native-iio/libjpeg7")]
 198 MAC.iio.compiler = compiler
 199 MAC.iio.ccFlags = [ccFlags].flatten()
 200 MAC.iio.linker = linker
 201 MAC.iio.linkFlags = [linkFlags].flatten()
 202 MAC.iio.lib = "javafx_iio"
 203 
 204 MAC.prismES2 = [:]
 205 MAC.prismES2.javahInclude = ["com/sun/prism/es2/**/*"]
 206 MAC.prismES2.nativeSource = [
 207     file("${project("graphics").projectDir}/src/main/native-prism-es2"),
 208     file("${project("graphics").projectDir}/src/main/native-prism-es2/GL"),
 209     file("${project("graphics").projectDir}/src/main/native-prism-es2/macosx")
 210 ]
 211 MAC.prismES2.compiler = compiler
 212 MAC.prismES2.ccFlags = ["-DMACOSX", ccFlags].flatten()
 213 MAC.prismES2.linker = linker
 214 MAC.prismES2.linkFlags = [linkFlags].flatten()
 215 MAC.prismES2.lib = "prism_es2"
 216 
 217 def closedDir = file("$projectDir/../rt-closed")
 218 MAC.font = [:]
 219 MAC.font.javahInclude = [
 220         "com/sun/javafx/font/**/*",
 221         "com/sun/javafx/text/**/*"]
 222 MAC.font.nativeSource = [file("${project("graphics").projectDir}/src/main/native-font")]
 223 MAC.font.compiler = compiler
 224 MAC.font.ccFlags = ["-DJFXFONT_PLUS", ccFlags].flatten()
 225 MAC.font.linker = linker
 226 MAC.font.linkFlags = [linkFlags].flatten()
 227 MAC.font.lib = "javafx_font"
 228 
 229 MAC.fontT2K = [:]
 230 MAC.fontT2K.javahInclude = ["com/sun/javafx/font/t2k/**/*"]
 231 MAC.fontT2K.nativeSource = [
 232         file("$closedDir/javafx-font-t2k-native/src"),
 233         file("$closedDir/javafx-font-t2k-native/src/layout")]
 234 MAC.fontT2K.compiler = compiler
 235 MAC.fontT2K.ccFlags = ["-DJFXFONT_PLUS", "-DLE_STANDALONE", ccFlags].flatten()
 236 MAC.fontT2K.linker = linker
 237 MAC.fontT2K.linkFlags = [linkFlags].flatten()
 238 MAC.fontT2K.lib = "javafx_font_t2k"
 239 
 240 MAC.media = [:]
 241 MAC.media.compiler = "${compiler} ${ccBaseFlags.join(" ")}"
 242 //MAC.media.ccFlags = ccBaseFlags
 243 MAC.media.linker = "${linker} ${commonParams.join(" ")}"
 244 //MAC.media.linkFlags = commonParams
 245 MAC.media.ar = "libtool"