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.LINUX = [:]
  27 
  28 // Declare whether this particular target file applies to the current system
  29 LINUX.canBuild = IS_LINUX;
  30 if (!LINUX.canBuild) return;
  31 
  32 // All desktop related packages should be built
  33 LINUX.compileSwing = true;
  34 LINUX.compileSWT = true;
  35 
  36 // Libraries end up in the lib/$OS_ARCH directory for Linux
  37 LINUX.libDest = "lib"
  38 
  39 // Lambda for naming the generated libs
  40 LINUX.library = { name -> return "lib${name}.so" as String }
  41 
  42 // A set of common parameters to use for both compiling and linking
  43 def commonFlags = [
  44         "-fno-strict-aliasing", "-fPIC", "-fno-omit-frame-pointer", // optimization flags
  45         "-fstack-protector",
  46         "-Wextra", "-Wall", "-Wformat-security", "-Wno-unused", "-Wno-parentheses", "-Werror=implicit-function-declaration"] // warning flags
  47 
  48 if (!IS_64) {
  49     commonFlags += "-m32"
  50 }
  51 
  52 // Specify the compilation parameters and link parameters
  53 def ccFlags = [
  54         commonFlags, "-I$JDK_HOME/include", "-I$JDK_HOME/include/linux", "-c",
  55         "-ffunction-sections", "-fdata-sections",
  56         IS_DEBUG_NATIVE ? ["-ggdb", "-DVERBOSE"] : ["-O2", "-DNDEBUG"]].flatten()
  57 def ccFlagsGTK3 = ccFlags
  58 //ccFlags.addAll(["-Wnon-virtual-dtor", "-Woverloaded-virtual", "-std=c++0x"])
  59 def linkFlags = ["-static-libgcc", "-static-libstdc++", "-shared", commonFlags,
  60                  "-z", "relro",
  61                  "-Wl,--gc-sections"].flatten()
  62 
  63 if (IS_DEBUG_NATIVE) {
  64     linkFlags += "-g"
  65 }
  66 
  67 def toolchainDir
  68 if (hasProperty('toolchainDir')) {
  69     toolchainDir = ext.toolchainDir + "/"
  70 } else {
  71     toolchainDir = ""
  72 }
  73 
  74 def gtk2CCFlags = [  ];
  75 def gtk3CCFlags = [ "-Wno-deprecated-declarations" ];
  76 def gtk2LinkFlags = [ ];
  77 def gtk3LinkFlags = [ ];
  78 LINUX.buildGTK3 = true
  79 
  80 // Create $buildDir/linux_tools.properties file and load props from it
  81 setupTools("linux_gtk2",
  82     { propFile ->
  83         ByteArrayOutputStream results1 = new ByteArrayOutputStream();
  84         exec {
  85             commandLine("${toolchainDir}pkg-config", "--cflags", "gtk+-2.0", "gthread-2.0", "xtst")
  86             setStandardOutput(results1);
  87         }
  88         propFile << "cflagsGTK2=" << results1.toString().trim() << "\n";
  89 
  90         ByteArrayOutputStream results3 = new ByteArrayOutputStream();
  91         exec {
  92             commandLine("${toolchainDir}pkg-config", "--libs", "gtk+-2.0", "gthread-2.0", "xtst")
  93             setStandardOutput(results3);
  94         }
  95         propFile << "libsGTK2=" << results3.toString().trim()  << "\n";
  96     },
  97     { properties ->
  98         def cflagsGTK2 = properties.getProperty("cflagsGTK2")
  99         def libsGTK2 = properties.getProperty("libsGTK2")
 100         if (cflagsGTK2 && libsGTK2) {
 101             gtk2CCFlags.addAll(cflagsGTK2.split(" "))
 102             gtk2LinkFlags.addAll(libsGTK2.split(" "))
 103         } else {
 104             throw new IllegalStateException("GTK2 development packages not found. If GTK2 packages are installed, please remove the build directory and try again.")
 105         }
 106     }
 107 )
 108 
 109 setupTools("linux_gtk3",
 110     { propFile ->
 111         ByteArrayOutputStream results2 = new ByteArrayOutputStream();
 112         exec {
 113             commandLine("${toolchainDir}pkg-config", "--cflags", "gtk+-3.0", "gthread-2.0", "xtst")
 114             setStandardOutput(results2);
 115             ignoreExitValue(true)
 116         }
 117         propFile << "cflagsGTK3=" << results2.toString().trim() << "\n";
 118 
 119         ByteArrayOutputStream results4 = new ByteArrayOutputStream();
 120         exec {
 121             commandLine("${toolchainDir}pkg-config", "--libs", "gtk+-3.0", "gthread-2.0", "xtst")
 122             setStandardOutput(results4);
 123             ignoreExitValue(true)
 124         }
 125         propFile << "libsGTK3=" << results4.toString().trim()  << "\n";
 126 
 127     },
 128     { properties ->
 129         def ccflags =  properties.getProperty("cflagsGTK3")
 130         def ldflags =  properties.getProperty("libsGTK3")
 131         if (ccflags && ldflags) {
 132             gtk3CCFlags.addAll(ccflags.split(" "))
 133             gtk3LinkFlags.addAll(ldflags.split(" "))
 134         } else {
 135             logger.info("Warning: GTK3 development packages not found, not building GTK3 support");
 136             LINUX.buildGTK3 = false
 137         }
 138     }
 139 )
 140 
 141 def pangoCCFlags = ["-D_ENABLE_PANGO"];
 142 def pangoLinkFlags = [];
 143 setupTools("linux_pango_tools",
 144     { propFile ->
 145         ByteArrayOutputStream results = new ByteArrayOutputStream();
 146         exec {
 147             commandLine "${toolchainDir}pkg-config", "--cflags", "pangoft2"
 148             standardOutput = results
 149         }
 150         propFile << "cflags=" << results.toString().trim() << "\n";
 151 
 152         results = new ByteArrayOutputStream();
 153         exec {
 154             commandLine "${toolchainDir}pkg-config", "--libs", "pangoft2"
 155             standardOutput = results
 156         }
 157         propFile << "libs=" << results.toString().trim();
 158     },
 159     { properties ->
 160         def cflags = properties.getProperty("cflags")
 161         def libs = properties.getProperty("libs")
 162         if (cflags && libs) {
 163             pangoCCFlags.addAll(cflags.split(" "))
 164             pangoLinkFlags.addAll(libs.split(" "))
 165         } else {
 166             throw new IllegalStateException("Linux pango packages not found.\nIf pango packages are installed, please remove the build directory and try again.")
 167         }
 168     }
 169 )
 170 
 171 def freetypeCCFlags = [ext.IS_COMPILE_PANGO ? "-D_ENABLE_PANGO" :
 172                        ext.IS_COMPILE_HARFBUZZ ? "-D_ENABLE_HARFBUZZ" : ""]
 173 def freetypeLinkFlags = []
 174 setupTools("linux_freetype_tools",
 175     { propFile ->
 176         ByteArrayOutputStream results = new ByteArrayOutputStream();
 177         exec {
 178             commandLine "${toolchainDir}pkg-config", "--cflags", "freetype2"
 179             standardOutput = results
 180         }
 181         propFile << "cflags=" << results.toString().trim() << "\n";
 182 
 183         results = new ByteArrayOutputStream();
 184         exec {
 185             commandLine "${toolchainDir}pkg-config", "--libs", "freetype2"
 186             standardOutput = results
 187         }
 188         propFile << "libs=" << results.toString().trim();
 189     },
 190     { properties ->
 191         def cflags = properties.getProperty("cflags")
 192         def libs = properties.getProperty("libs")
 193         if (cflags && libs) {
 194             freetypeCCFlags.addAll(cflags.split(" "))
 195             freetypeLinkFlags.addAll(libs.split(" "))
 196         } else {
 197             throw new IllegalStateException("Linux freetype packages not found.\nIf freetype pacakges are installed, please remove the build directory and try again.")
 198         }
 199     }
 200 )
 201 
 202 def compiler = IS_COMPILE_PARFAIT ? "parfait-gcc" : "${toolchainDir}gcc";
 203 def linker = IS_COMPILE_PARFAIT ? "parfait-g++" : "${toolchainDir}g++";
 204 
 205 LINUX.glass = [:]
 206 LINUX.glass.variants = ["glass", "glassgtk2"]
 207 if (LINUX.buildGTK3) {
 208     logger.info("Building libglassgtk3")
 209     LINUX.glass.variants += "glassgtk3"
 210 } else {
 211     logger.warn("NOT Building libglassgtk3")
 212 }
 213 
 214 FileTree ft_gtk_launcher = fileTree("${project(":graphics").projectDir}/src/main/native-glass/gtk/") {
 215     include("**/launcher.c")
 216 }
 217 
 218 FileTree ft_gtk = fileTree("${project(":graphics").projectDir}/src/main/native-glass/gtk/") {
 219     exclude("**/launcher.c")
 220 }
 221 
 222 LINUX.glass.glass = [:]
 223 LINUX.glass.glass.nativeSource = ft_gtk_launcher.getFiles()
 224 LINUX.glass.glass.compiler = compiler
 225 LINUX.glass.glass.ccFlags = [ccFlags, gtk2CCFlags,  "-Werror"].flatten()
 226 LINUX.glass.glass.linker = linker
 227 LINUX.glass.glass.linkFlags = [linkFlags, "-lX11", "-ldl" ].flatten()
 228 LINUX.glass.glass.lib = "glass"
 229 
 230 LINUX.glass.glassgtk2 = [:]
 231 LINUX.glass.glassgtk2.nativeSource =  ft_gtk.getFiles()
 232 LINUX.glass.glassgtk2.compiler = compiler
 233 LINUX.glass.glassgtk2.ccFlags = [ccFlags, gtk2CCFlags, "-Werror"].flatten()
 234 LINUX.glass.glassgtk2.linker = linker
 235 LINUX.glass.glassgtk2.linkFlags = [linkFlags, gtk2LinkFlags ].flatten()
 236 LINUX.glass.glassgtk2.lib = "glassgtk2"
 237 
 238 LINUX.glass.glassgtk3 = [:]
 239 LINUX.glass.glassgtk3.nativeSource =  ft_gtk.getFiles()
 240 LINUX.glass.glassgtk3.compiler = compiler
 241 LINUX.glass.glassgtk3.ccFlags = [ccFlags, gtk3CCFlags, "-Werror"].flatten()
 242 LINUX.glass.glassgtk3.linker = linker
 243 LINUX.glass.glassgtk3.linkFlags = [linkFlags, gtk3LinkFlags ].flatten()
 244 LINUX.glass.glassgtk3.lib = "glassgtk3"
 245 
 246 LINUX.decora = [:]
 247 LINUX.decora.compiler = compiler
 248 LINUX.decora.ccFlags = [ccFlags, "-ffast-math"].flatten()
 249 LINUX.decora.linker = linker
 250 LINUX.decora.linkFlags = [linkFlags].flatten()
 251 LINUX.decora.lib = "decora_sse"
 252 
 253 LINUX.prism = [:]
 254 LINUX.prism.nativeSource = file("${project(":graphics").projectDir}/src/main/native-prism")
 255 LINUX.prism.compiler = compiler
 256 LINUX.prism.ccFlags = [ccFlags, "-DINLINE=inline"].flatten()
 257 LINUX.prism.linker = linker
 258 LINUX.prism.linkFlags = [linkFlags].flatten()
 259 LINUX.prism.lib = "prism_common"
 260 
 261 LINUX.prismSW = [:]
 262 LINUX.prismSW.nativeSource = file("${project(":graphics").projectDir}/src/main/native-prism-sw")
 263 LINUX.prismSW.compiler = compiler
 264 LINUX.prismSW.ccFlags = [ccFlags, "-DINLINE=inline"].flatten()
 265 LINUX.prismSW.linker = linker
 266 LINUX.prismSW.linkFlags = [linkFlags].flatten()
 267 LINUX.prismSW.lib = "prism_sw"
 268 
 269 LINUX.iio = [:]
 270 LINUX.iio.nativeSource = [
 271     file("${project("graphics").projectDir}/src/main/native-iio"),
 272     file("${project("graphics").projectDir}/src/main/native-iio/libjpeg")]
 273 LINUX.iio.compiler = compiler
 274 LINUX.iio.ccFlags = [ccFlags].flatten()
 275 LINUX.iio.linker = linker
 276 LINUX.iio.linkFlags = [linkFlags].flatten()
 277 LINUX.iio.lib = "javafx_iio"
 278 
 279 LINUX.prismES2 = [:]
 280 LINUX.prismES2.nativeSource = [
 281     file("${project("graphics").projectDir}/src/main/native-prism-es2"),
 282     file("${project("graphics").projectDir}/src/main/native-prism-es2/GL"),
 283     file("${project("graphics").projectDir}/src/main/native-prism-es2/x11")
 284 ]
 285 LINUX.prismES2.compiler = compiler
 286 LINUX.prismES2.ccFlags = ["-DLINUX", ccFlags].flatten()
 287 LINUX.prismES2.linker = linker
 288 LINUX.prismES2.linkFlags = [linkFlags, "-lX11", "-lXxf86vm", "-lGL"].flatten()
 289 LINUX.prismES2.lib = "prism_es2"
 290 
 291 def closedDir = file("$projectDir/../rt-closed")
 292 LINUX.font = [:]
 293 LINUX.font.compiler = compiler
 294 LINUX.font.nativeSource = [file("${project("graphics").projectDir}/src/main/native-font")]
 295 LINUX.font.ccFlags = ["-DJFXFONT_PLUS", ccFlags].flatten()
 296 LINUX.font.linker = linker
 297 LINUX.font.linkFlags = [linkFlags].flatten()
 298 LINUX.font.lib = "javafx_font"
 299 
 300 LINUX.fontFreetype = [:]
 301 LINUX.fontFreetype.nativeSource = ["src/main/native-font/freetype.c"]
 302 LINUX.fontFreetype.compiler = compiler
 303 LINUX.fontFreetype.ccFlags = ["-DJFXFONT_PLUS", ccFlags, freetypeCCFlags].flatten()
 304 LINUX.fontFreetype.linker = linker
 305 LINUX.fontFreetype.linkFlags = [linkFlags, freetypeLinkFlags].flatten()
 306 LINUX.fontFreetype.lib = "javafx_font_freetype"
 307 
 308 LINUX.fontPango = [:]
 309 LINUX.fontPango.nativeSource = ["src/main/native-font/pango.c"]
 310 LINUX.fontPango.compiler = compiler
 311 LINUX.fontPango.ccFlags = ["-DJFXFONT_PLUS", ccFlags, pangoCCFlags].flatten()
 312 LINUX.fontPango.linker = linker
 313 LINUX.fontPango.linkFlags = [linkFlags, pangoLinkFlags].flatten()
 314 LINUX.fontPango.lib = "javafx_font_pango"
 315 
 316 LINUX.media = [:]
 317 LINUX.media.compiler = compiler
 318 LINUX.media.linker = linker
 319 LINUX.media.ar = "${toolchainDir}ar"
 320 
 321 LINUX.webkit = [:]
 322 LINUX.webkit.compiler = compiler
 323 LINUX.webkit.linker = linker
 324 LINUX.webkit.ccFlags = commonFlags.flatten()
 325 LINUX.webkit.linkFlags = linkFlags.flatten()