1 /*
   2  * Copyright (c) 2013, 2018, 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 
  35 MAC.includeNull3d = true
  36 
  37 // Lambda for naming the generated libs
  38 MAC.library = { name -> return "lib${name}.dylib" as String }
  39 
  40 MAC.libDest = "lib"
  41 
  42 /*
  43  * Define settings for Mac compilation. If we don't find the preferred SDK
  44  * in the default location then we will use "xcodebuild" to locate a suitable SDK.
  45  * In extreme cases you can provide your own properties in your home dir to
  46  * override these settings or pass them on the command line.
  47  */
  48 def prefSdkVersion = "10.11"
  49 def defaultSdkPath = "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX${prefSdkVersion}.sdk";
  50 
  51 // Set the minimum API version that we require (developers do not need to override this)
  52 // Note that this is not necessarily the same as the preferred SDK version
  53 defineProperty("MACOSX_MIN_VERSION", "10.10");
  54 
  55 // Create $buildDir/mac_tools.properties file and load props from it
  56 setupTools("mac_tools",
  57     { propFile ->
  58         propFile << ""
  59         if (!file(defaultSdkPath).isDirectory()) {
  60             // Get list of all macosx sdks
  61             ByteArrayOutputStream results = new ByteArrayOutputStream();
  62             exec {
  63                 commandLine("xcodebuild", "-version", "-showsdks");
  64                 setStandardOutput(results);
  65             }
  66 
  67             BufferedReader reader = new BufferedReader(new StringReader(results.toString().trim()));
  68             // If our preferred SDK is in the list use it, else use the default
  69             String sdk = "macosx"
  70             String prefSdk = sdk + prefSdkVersion
  71             while (true) {
  72                 def line = reader.readLine();
  73                 if (line == null) break;
  74                 if (line.contains("-sdk ${prefSdk}")) {
  75                     sdk = prefSdk
  76                     break;
  77                 }
  78             }
  79 
  80             results = new ByteArrayOutputStream();
  81             exec {
  82                 commandLine("xcodebuild", "-version", "-sdk", sdk, "Path");
  83                 setStandardOutput(results);
  84             }
  85             String sdkPath = results.toString().trim();
  86             propFile << "MACOSX_SDK_PATH=" << sdkPath << "\n";
  87         }
  88     },
  89     { properties ->
  90         defineProperty("MACOSX_SDK_PATH", properties, defaultSdkPath)
  91     }
  92 )
  93 
  94 println "MACOSX_MIN_VERSION = $MACOSX_MIN_VERSION"
  95 println "MACOSX_SDK_PATH = $MACOSX_SDK_PATH"
  96 
  97 if (!file(MACOSX_SDK_PATH).isDirectory()) {
  98     throw new GradleException("FAIL: Cannot find $MACOSX_SDK_PATH")
  99 }
 100 
 101 // NOTE: There is no space between -iframework and the specified path
 102 def commonParams = [
 103         "-mmacosx-version-min=$MACOSX_MIN_VERSION",
 104         "-isysroot", "$MACOSX_SDK_PATH",
 105         "-iframework$MACOSX_SDK_PATH/System/Library/Frameworks",
 106         "-arch", "x86_64"]
 107 
 108 def ccBaseFlags = [
 109         commonParams,
 110         "-I$JDK_HOME/include",
 111         "-I$JDK_HOME/include/darwin"].flatten()
 112 
 113 
 114 def ccFlags = [
 115         ccBaseFlags,
 116         "-std=c99",
 117         "-c",
 118         IS_DEBUG_NATIVE ? "-DDEBUG" : ["-O3", "-DNDEBUG"]].flatten()
 119 
 120 def linkFlags = [
 121         commonParams,
 122         "-framework", "AppKit",
 123         "-framework", "ApplicationServices",
 124         "-framework", "OpenGL",
 125         "-framework", "QuartzCore",
 126         "-framework", "Security",
 127         "-dynamiclib", "-lobjc"].flatten();
 128 
 129 
 130 def compiler = IS_COMPILE_PARFAIT ? "parfait-clang" : "clang";
 131 def linker = IS_COMPILE_PARFAIT ? "parfait-clang++" : "clang++";
 132 
 133 MAC.glass = [:]
 134 MAC.glass.javahInclude = [
 135     "com/sun/glass/events/**",
 136     "com/sun/glass/ui/*",
 137     "com/sun/glass/ui/mac/*"]
 138 MAC.glass.nativeSource = file("${project("graphics").projectDir}/src/main/native-glass/mac")
 139 MAC.glass.compiler = compiler
 140 MAC.glass.ccFlags = [ccFlags].flatten()
 141 MAC.glass.linker = linker
 142 MAC.glass.linkFlags = [linkFlags].flatten()
 143 MAC.glass.lib = "glass"
 144 
 145 MAC.decora = [:]
 146 MAC.decora.compiler = compiler
 147 MAC.decora.ccFlags = ["-O1", "-ffast-math", "-c", ccBaseFlags].flatten()
 148 MAC.decora.linker = linker
 149 MAC.decora.linkFlags = ["-dynamiclib", commonParams].flatten()
 150 MAC.decora.lib = "decora_sse"
 151 
 152 MAC.prism = [:]
 153 MAC.prism.javahInclude = ["com/sun/prism/impl/**/*", "com/sun/prism/PresentableState*"]
 154 MAC.prism.nativeSource = file("${project("graphics").projectDir}/src/main/native-prism")
 155 MAC.prism.compiler = compiler
 156 MAC.prism.ccFlags = ["-O3", "-DINLINE=inline", "-c", ccBaseFlags].flatten()
 157 MAC.prism.linker = linker
 158 MAC.prism.linkFlags = ["-dynamiclib", commonParams].flatten()
 159 MAC.prism.lib = "prism_common"
 160 
 161 MAC.prismSW = [:]
 162 MAC.prismSW.javahInclude = ["com/sun/pisces/**/*"]
 163 MAC.prismSW.nativeSource = file("${project("graphics").projectDir}/src/main/native-prism-sw")
 164 MAC.prismSW.compiler = compiler
 165 MAC.prismSW.ccFlags = [MAC.prism.ccFlags].flatten()
 166 MAC.prismSW.linker = linker
 167 MAC.prismSW.linkFlags = [MAC.prism.linkFlags].flatten()
 168 MAC.prismSW.lib = "prism_sw"
 169 
 170 MAC.iio = [:]
 171 MAC.iio.javahInclude = ["com/sun/javafx/iio/**/*"]
 172 MAC.iio.nativeSource = [
 173     file("${project("graphics").projectDir}/src/main/native-iio"),
 174     file("${project("graphics").projectDir}/src/main/native-iio/libjpeg9c")]
 175 MAC.iio.compiler = compiler
 176 MAC.iio.ccFlags = [ccFlags].flatten()
 177 MAC.iio.linker = linker
 178 MAC.iio.linkFlags = [linkFlags].flatten()
 179 MAC.iio.lib = "javafx_iio"
 180 
 181 MAC.prismES2 = [:]
 182 MAC.prismES2.javahInclude = ["com/sun/prism/es2/**/*"]
 183 MAC.prismES2.nativeSource = [
 184     file("${project("graphics").projectDir}/src/main/native-prism-es2"),
 185     file("${project("graphics").projectDir}/src/main/native-prism-es2/GL"),
 186     file("${project("graphics").projectDir}/src/main/native-prism-es2/macosx")
 187 ]
 188 MAC.prismES2.compiler = compiler
 189 MAC.prismES2.ccFlags = ["-DMACOSX", ccFlags].flatten()
 190 MAC.prismES2.linker = linker
 191 MAC.prismES2.linkFlags = [linkFlags].flatten()
 192 MAC.prismES2.lib = "prism_es2"
 193 
 194 def closedDir = file("$projectDir/../rt-closed")
 195 MAC.font = [:]
 196 MAC.font.javahInclude = [
 197         "com/sun/javafx/font/**/*",
 198         "com/sun/javafx/text/**/*"]
 199 MAC.font.nativeSource = [file("${project("graphics").projectDir}/src/main/native-font")]
 200 MAC.font.compiler = compiler
 201 MAC.font.ccFlags = ["-DJFXFONT_PLUS", ccFlags].flatten()
 202 MAC.font.linker = linker
 203 MAC.font.linkFlags = [linkFlags].flatten()
 204 MAC.font.lib = "javafx_font"
 205 
 206 MAC.media = [:]
 207 MAC.media.compiler = "${compiler} ${ccBaseFlags.join(" ")}"
 208 //MAC.media.ccFlags = ccBaseFlags
 209 MAC.media.linker = "${linker} ${commonParams.join(" ")}"
 210 //MAC.media.linkFlags = commonParams
 211 MAC.media.ar = "libtool"