Skip to content

Commit

Permalink
Move GenerateSources, PackageNames and GLibCompileResources classes t…
Browse files Browse the repository at this point in the history
…o other packages
  • Loading branch information
jwharm committed Sep 7, 2023
1 parent ec7524a commit ec5e20e
Show file tree
Hide file tree
Showing 9 changed files with 199 additions and 373 deletions.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import org.apache.tools.ant.taskdefs.condition.Os
import io.github.jwharm.javagi.generator.GenerateSources
import io.github.jwharm.javagi.GenerateSources

/* Common build settings for Java-GI modules:
*
Expand Down
122 changes: 122 additions & 0 deletions buildSrc/src/main/java/io/github/jwharm/javagi/GenerateSources.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/* Java-GI - Java language bindings for GObject-Introspection-based libraries
* Copyright (C) 2022-2023 Jan-Willem Harmannij
*
* SPDX-License-Identifier: LGPL-2.1-or-later
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/

package io.github.jwharm.javagi;

import io.github.jwharm.javagi.generator.*;
import io.github.jwharm.javagi.model.Repository;
import io.github.jwharm.javagi.model.Module;
import org.gradle.api.DefaultTask;
import org.gradle.api.file.Directory;
import org.gradle.api.file.DirectoryProperty;
import org.gradle.api.provider.Property;
import org.gradle.api.tasks.*;
import org.xml.sax.SAXException;

import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
import java.nio.file.Path;

/**
* GenerateSources is a Gradle task that will parse a GIR file (and all included GIR files)
* and generate Java source files for the types defined in the GIR file.
*/
public abstract class GenerateSources extends DefaultTask {

@InputDirectory
abstract DirectoryProperty getInputDirectory();

@OutputDirectory
abstract DirectoryProperty getOutputDirectory();

@Input
abstract Property<String> getGirFile();

@Input @Optional
abstract Property<String> getUrlPrefix();

@Input @Optional
abstract Property<Patch> getPatch();

@TaskAction
void execute() {
try {
Module windows = parse(Platform.WINDOWS, getInputDirectory().get(), getGirFile().get(),
getUrlPrefix().getOrElse(null), getPatch().getOrElse(null));
Module linux = parse(Platform.LINUX, getInputDirectory().get(), getGirFile().get(),
getUrlPrefix().getOrElse(null), getPatch().getOrElse(null));
Module macos = parse(Platform.MACOS, getInputDirectory().get(), getGirFile().get(),
getUrlPrefix().getOrElse(null), getPatch().getOrElse(null));

Module module = new Merge().merge(windows, linux, macos);

for (Repository repository : module.repositories.values()) {
if (repository.generate) {
Path basePath = getOutputDirectory().get().file(repository.namespace.pathName).getAsFile().toPath();
repository.generate(basePath);
}
}
} catch (Exception e) {
throw new TaskExecutionException(this, e);
}
}

private static Module parse(Platform platform, Directory sourceDirectory, String girFile, String urlPrefix, Patch patch)
throws SAXException, ParserConfigurationException {
Module module = new Module(platform);
Directory girPath = sourceDirectory.dir(platform.name().toLowerCase());
if (! girPath.getAsFile().exists()) {
System.out.println("Not found: " + girPath);
return null;
}
GirParser parser = new GirParser(girPath.getAsFile().toPath(), module);

// Parse the GI files into Repository objects
try {
// Parse the file
Repository r = parser.parse(girFile);

// Check if this one has already been parsed
if (module.repositories.containsKey(r.namespace.name)) {
r = module.repositories.get(r.namespace.name);
} else {
// Add the repository to the module
module.repositories.put(r.namespace.name, r);
}

r.urlPrefix = urlPrefix;

// Flag unsupported va_list methods so they will not be generated
module.flagVaListFunctions();

// Apply patch
if (patch != null) {
patch.patch(r);
}

} catch (IOException ignored) {
// Gir file not found for this platform: This will generate code with UnsupportedPlatformExceptions
}

// Link the type references to the GIR type definition across the GI repositories
module.link();

return module;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/* Java-GI - Java language bindings for GObject-Introspection-based libraries
* Copyright (C) 2022-2023 Jan-Willem Harmannij
*
* SPDX-License-Identifier: LGPL-2.1-or-later
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/

package io.github.jwharm.javagi.configuration;

import java.util.Map;

import static java.util.Map.entry;

/**
* The PackageNames class defines the mapping between GIR namespaces
* and Java package names.
*/
public class PackageNames {

public static Map<String, String> getMap() {
return Map.ofEntries(
entry("GLib", "org.gnome.glib"),
entry("GObject", "org.gnome.gobject"),
entry("GModule", "org.gnome.gmodule"),
entry("Gio", "org.gnome.gio"),
entry("cairo", "org.freedesktop.cairo"),
entry("freetype2", "org.freedesktop.freetype"),
entry("HarfBuzz", "org.freedesktop.harfbuzz"),
entry("Pango", "org.gnome.pango"),
entry("PangoCairo", "org.gnome.pango.cairo"),
entry("GdkPixbuf", "org.gnome.gdkpixbuf"),
entry("Gdk", "org.gnome.gdk"),
entry("Graphene", "org.gnome.graphene"),
entry("Gsk", "org.gnome.gsk"),
entry("Gtk", "org.gnome.gtk"),
entry("GtkSource", "org.gnome.gtksourceview"),
entry("Adw", "org.gnome.adw"),
entry("Soup", "org.gnome.soup"),
entry("JavaScriptCore", "org.gnome.webkit.jsc"),
entry("WebKit", "org.gnome.webkit"),
entry("WebKitWebProcessExtension", "org.gnome.webkit.wpe"),
entry("Gst", "org.freedesktop.gstreamer"),
entry("GstBase", "org.freedesktop.gstreamer.base"),
entry("GstAudio", "org.freedesktop.gstreamer.audio"),
entry("GstPbutils", "org.freedesktop.gstreamer.pbutils"),
entry("GstVideo", "org.freedesktop.gstreamer.video")
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,21 @@

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import io.github.jwharm.javagi.configuration.PackageNames;
import io.github.jwharm.javagi.model.*;
import io.github.jwharm.javagi.model.Module;

/**
* Utility functions to convert names and keywords
*/
public class Conversions {

/**
* Map of namespaces to Java package names
*/
public static Map<String, String> packageNames;

/**
* Convert "Gdk" to "org.gnome.gdk"
*/
public static String namespaceToJavaPackage(String ns) {
return Objects.requireNonNullElse(packageNames.get(ns), ns);
return Objects.requireNonNullElse(PackageNames.getMap().get(ns), ns);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

package io.github.jwharm.javagi.model;

import io.github.jwharm.javagi.generator.Conversions;
import io.github.jwharm.javagi.configuration.PackageNames;
import io.github.jwharm.javagi.generator.Platform;

import java.util.HashMap;
Expand All @@ -45,7 +45,7 @@ public Namespace(GirElement parent, String name, String version, String sharedLi
this.sharedLibrary = sharedLibrary;
this.cIdentifierPrefix = cIdentifierPrefix;
this.cSymbolPrefix = cSymbolPrefix;
this.packageName = Conversions.packageNames.get(name);
this.packageName = PackageNames.getMap().get(name);
this.globalClassName = (name.equals("GObject") ? "GObjects" : name);
this.pathName = packageName.replace('.', '/') + '/';
}
Expand Down
Loading

0 comments on commit ec5e20e

Please sign in to comment.