Skip to content

Commit

Permalink
Use a header for build-time configuration
Browse files Browse the repository at this point in the history
This allows for less polluted valac invocation and nicer meson.build.
Build-time constants now live in a separate VAPI file.
  • Loading branch information
v1993 committed Nov 11, 2023
1 parent 86ad961 commit 75129b3
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 17 deletions.
11 changes: 4 additions & 7 deletions src/Application.vala
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@
* SPDX-License-Identifier: GPL-3.0-or-later
*/

extern const string NXDC_VERSION;
extern const string NXDC_ICONS_PATH;

namespace NXDumpClient {
/// A convenience method to show errors to user
internal void report_error(string desc, string message) {
Expand Down Expand Up @@ -255,7 +252,7 @@ namespace NXDumpClient {

public override int handle_local_options(VariantDict opt) {
if (opt.lookup("version", "b", null)) {
print("nxdumpclient %s\n", NXDC_VERSION);
print("nxdumpclient %s\n", BuildConfig.VERSION);
return 0;
}

Expand Down Expand Up @@ -316,8 +313,8 @@ namespace NXDumpClient {

if (Xdp.Portal.running_under_flatpak()) {
// Fix About dialog icon
debug("Adding to icons path: %s", NXDC_ICONS_PATH);
Gtk.IconTheme.get_for_display(Gdk.Display.get_default()).add_search_path(NXDC_ICONS_PATH);
debug("Adding to icons path: %s", BuildConfig.ICONS_PATH);
Gtk.IconTheme.get_for_display(Gdk.Display.get_default()).add_search_path(BuildConfig.ICONS_PATH);
}
#endif
}
Expand Down Expand Up @@ -413,7 +410,7 @@ namespace NXDumpClient {
}

private void on_about_action() {
var about = new Adw.AboutWindow.from_appdata("/org/v1993/NXDumpClient/appdata.xml", NXDC_VERSION) {
var about = new Adw.AboutWindow.from_appdata("/org/v1993/NXDumpClient/appdata.xml", BuildConfig.VERSION) {
transient_for = main_window,
translator_credits = _("translator-credits"),
developers = {
Expand Down
4 changes: 1 addition & 3 deletions src/PreferencesWindow.vala
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
* SPDX-License-Identifier: GPL-3.0-or-later
*/

extern const string NXDC_EXECUTABLE;

// Debug option to use libportal even out of sandbox.
// This is theoretically safe to have always, but may hit bugs in portal backends.
private const bool FORCE_LIBPORTAL = false;
Expand Down Expand Up @@ -186,7 +184,7 @@ namespace NXDumpClient {
}

var autostart_cmd = new GenericArray<unowned string>(2);
autostart_cmd.add(NXDC_EXECUTABLE);
autostart_cmd.add(BuildConfig.EXECUTABLE);
autostart_cmd.add("--background");
// D-Bus activation is not an option because of having to pass a flag
var flags = need_autostart ? Xdp.BackgroundFlags.AUTOSTART : Xdp.BackgroundFlags.NONE;
Expand Down
8 changes: 8 additions & 0 deletions src/config.vapi
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// lower_case_cprefix is required even if unused in practice
[CCode (cprefix = "NXDC_", lower_case_cprefix = "nxdc_", cheader_filename = "config.h")]
namespace BuildConfig {
public const string VERSION;
public const string LOCALE_DIR;
public const string ICONS_PATH;
public const string EXECUTABLE;
}
4 changes: 2 additions & 2 deletions src/main.vala
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@
*/

internal string argv0;

extern const string GETTEXT_PACKAGE;
extern const string NXDC_LOCALE_DIR;

int main(string[] args) {
argv0 = args[0] ?? "nxdumpclient";

Intl.setlocale();
Intl.bindtextdomain(GETTEXT_PACKAGE, NXDC_LOCALE_DIR);
Intl.bindtextdomain(GETTEXT_PACKAGE, BuildConfig.LOCALE_DIR);
Intl.bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
Intl.textdomain(GETTEXT_PACKAGE);

Expand Down
17 changes: 12 additions & 5 deletions src/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ nxdc_sources = []

nxdc_sources_native = [
'main.vala',
'config.vapi',

'Application.vala',
'Window.vala',
Expand Down Expand Up @@ -103,19 +104,25 @@ else
message('Current meson version has a bug; pass -Denforce_build_order=true in case this is not a development build')
endif

conf_data = configuration_data()
conf_data.set_quoted('NXDC_VERSION', meson.project_version())
conf_data.set_quoted('NXDC_LOCALE_DIR', get_option('prefix') / get_option('localedir'))
conf_data.set_quoted('NXDC_ICONS_PATH', get_option('prefix') / get_option('datadir') / 'icons')
conf_data.set_quoted('NXDC_EXECUTABLE', get_option('prefix') / get_option('bindir') / 'nxdumpclient')
configure_file(
output: 'config.h',
configuration: conf_data
)

executable('nxdumpclient', nxdc_sources,
extra_files: nxdc_sources_native,
dependencies: nxdc_deps,
vala_args: extra_vala_args,
install: true,

c_args: [
# These must be defined before GLib includes
'-DGETTEXT_PACKAGE="nxdumpclient"',
'-DG_LOG_DOMAIN="nxdumpclient"',
'-DNXDC_VERSION="@0@"'.format(meson.project_version()),
'-DNXDC_LOCALE_DIR="@0@"'.format(get_option('prefix') / get_option('localedir')),
'-DNXDC_ICONS_PATH="@0@"'.format(get_option('prefix') / get_option('datadir') / 'icons'),
'-DNXDC_EXECUTABLE="@0@"'.format(get_option('prefix') / get_option('bindir') / 'nxdumpclient'),
],
)

0 comments on commit 75129b3

Please sign in to comment.