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