# Similarly to libpikathumb/pikathumb-enums.c, libpika/pikaenums.c is
# not versionned so we don't use the mkenums_wrap.
# Moreover it is generated in 2 steps, first with common mkenums
# arguments, then concatenating with a "tail" file.
pikaenums_notail = custom_target('pikaenums.c.notail',
  input : [ 'pikaenums.h', stamp_enumcode],
  output: [ 'pikaenums.c.notail', ],
  command: [
    pika_mkenums,
    '--fhead','#include "config.h"\n'
            + '#include <gio/gio.h>\n'
            + '#include <gegl.h>\n'
            + '#undef PIKA_DISABLE_DEPRECATED\n'
            + '#include "libpikabase/pikabase.h"\n'
            + '#include "libpikabase/pikabase-private.h"\n'
            + '#include "libpikaconfig/pikaconfigenums.h"\n'
            + '#include "pdb/stamp-enumcode.h"\n'
            + '#include "pikaenums.h"\n',
    '--fprod',
      '/* enumerations from "@basename@" */',
    '--vhead',
      'GType\n'+
      '@enum_name@_get_type (void)\n'+
      '{\n'+
      '  static const G@Type@Value values[] =\n'+
      '  {',
    '--vprod',
      '    { @VALUENAME@, "@VALUENAME@", "@valuenick@" },',
    '--vtail',
      '    { 0, NULL, NULL }\n'+
      '  };\n',
    '--dhead',
      '  static const Pika@Type@Desc descs[] =\n'+
      '  {',
    '--dprod',
      '    { @VALUENAME@, @valuedesc@, @valuehelp@ },'+
      '@if (\'@valueabbrev@\' ne \'NULL\')@\n'+
      '    /* Translators: this is an abbreviated version of @valueudesc@.\n'+
      '       Keep it short. */\n'+
      '    { @VALUENAME@, @valueabbrev@, NULL },'+
      '@endif@',
    '--dtail',
      libpika_mkenums_dtails,
    '@INPUT0@',
  ],
  capture: true,
)

pikaenums = custom_target('pikaenums.c',
  input : [ pikaenums_notail, 'pikaenums.c.tail', ],
  output: [ 'pikaenums.c', ],
  command: [ 'cat', '@INPUT@' ],
  capture: true,
)

pikauimarshal = gnome.genmarshal('pikauimarshal',
  prefix: '_pikaui_marshal',
  sources: 'pikauimarshal.list',
  install_header: false,
)

pdb_wrappers_sources = [
  'pika_pdb.c',
  'pikabrush_pdb.c',
  'pikabrushes_pdb.c',
  'pikabrushselect_pdb.c',
  'pikabuffer_pdb.c',
  'pikachannel_pdb.c',
  'pikacontext_pdb.c',
  'pikadebug_pdb.c',
  'pikadisplay_pdb.c',
  'pikadrawable_pdb.c',
  'pikadrawablecolor_pdb.c',
  'pikadrawableedit_pdb.c',
  'pikadrawableselect_pdb.c',
  'pikadynamics_pdb.c',
  'pikaedit_pdb.c',
  'pikafile_pdb.c',
  'pikafloatingsel_pdb.c',
  'pikafont_pdb.c',
  'pikafonts_pdb.c',
  'pikafontselect_pdb.c',
  'pikapikarc_pdb.c',
  'pikagradient_pdb.c',
  'pikagradients_pdb.c',
  'pikagradientselect_pdb.c',
  'pikahelp_pdb.c',
  'pikaimage_pdb.c',
  'pikaimagecolorprofile_pdb.c',
  'pikaimageconvert_pdb.c',
  'pikaimagegrid_pdb.c',
  'pikaimageguides_pdb.c',
  'pikaimagesamplepoints_pdb.c',
  'pikaimageselect_pdb.c',
  'pikaimagetransform_pdb.c',
  'pikaimageundo_pdb.c',
  'pikaitem_pdb.c',
  'pikaitemtransform_pdb.c',
  'pikalayer_pdb.c',
  'pikamessage_pdb.c',
  'pikapainttools_pdb.c',
  'pikapalette_pdb.c',
  'pikapalettes_pdb.c',
  'pikapaletteselect_pdb.c',
  'pikapattern_pdb.c',
  'pikapatterns_pdb.c',
  'pikapatternselect_pdb.c',
  'pikaprogress_pdb.c',
  'pikaresource_pdb.c',
  'pikaselection_pdb.c',
  'pikatextlayer_pdb.c',
  'pikatexttool_pdb.c',
  'pikavectors_pdb.c',
]

pdb_wrappers_headers = [
  'pika_pdb_headers.h',
  'pika_pdb.h',
  'pikabrush_pdb.h',
  'pikabrushes_pdb.h',
  'pikabrushselect_pdb.h',
  'pikabuffer_pdb.h',
  'pikachannel_pdb.h',
  'pikacontext_pdb.h',
  'pikadebug_pdb.h',
  'pikadisplay_pdb.h',
  'pikadrawable_pdb.h',
  'pikadrawablecolor_pdb.h',
  'pikadrawableedit_pdb.h',
  'pikadrawableselect_pdb.h',
  'pikadynamics_pdb.h',
  'pikaedit_pdb.h',
  'pikafile_pdb.h',
  'pikafloatingsel_pdb.h',
  'pikafont_pdb.h',
  'pikafonts_pdb.h',
  'pikafontselect_pdb.h',
  'pikapikarc_pdb.h',
  'pikagradient_pdb.h',
  'pikagradients_pdb.h',
  'pikagradientselect_pdb.h',
  'pikahelp_pdb.h',
  'pikaimage_pdb.h',
  'pikaimagecolorprofile_pdb.h',
  'pikaimageconvert_pdb.h',
  'pikaimagegrid_pdb.h',
  'pikaimageguides_pdb.h',
  'pikaimagesamplepoints_pdb.h',
  'pikaimageselect_pdb.h',
  'pikaimagetransform_pdb.h',
  'pikaimageundo_pdb.h',
  'pikaitem_pdb.h',
  'pikaitemtransform_pdb.h',
  'pikalayer_pdb.h',
  'pikamessage_pdb.h',
  'pikapainttools_pdb.h',
  'pikapalette_pdb.h',
  'pikapalettes_pdb.h',
  'pikapaletteselect_pdb.h',
  'pikapattern_pdb.h',
  'pikapatterns_pdb.h',
  'pikapatternselect_pdb.h',
  'pikaprogress_pdb.h',
  'pikaresource_pdb.h',
  'pikaselection_pdb.h',
  'pikatextlayer_pdb.h',
  'pikatexttool_pdb.h',
  'pikavectors_pdb.h',
]

libpika_sources_introspectable = [
  'pika.c',
  'pikabatchprocedure.c',
  'pikabrush.c',
  'pikachannel.c',
  'pikadisplay.c',
  'pikadrawable.c',
  'pikafileprocedure.c',
  'pikafont.c',
  'pikapikarc.c',
  'pikagradient.c',
  'pikaimage.c',
  'pikaimagecolorprofile.c',
  'pikaimagemetadata.c',
  'pikaimagemetadata-save.c',
  'pikaimageprocedure.c',
  'pikaitem.c',
  'pikalayer.c',
  'pikalayermask.c',
  'pikaloadprocedure.c',
  'pikapalette.c',
  'pikaparamspecs.c',
  'pikapattern.c',
  'pikapdb.c',
  'pikaplugin.c',
  'pikaprocedure.c',
  'pikaprocedureconfig.c',
  'pikaprogress.c',
  'pikaresource.c',
  'pikaresourceselect.c',
  'pikasaveprocedure.c',
  'pikaselection.c',
  'pikatextlayer.c',
  'pikathumbnailprocedure.c',
  'pikavectors.c',
  pikaenums,
  pdb_wrappers_sources,
]

libpika_sources = [
  libpika_sources_introspectable,
  'pika-debug.c',
  'pika-shm.c',
  'pikagpparams.c',
  'pikaparamspecs-desc.c',
  'pikapdb_pdb.c',
  'pikapdbprocedure.c',
  'pikapixbuf.c',
  'pikaplugin_pdb.c',
  'pikaunit_pdb.c',
  'pikaunitcache.c',
  'pikatilebackendplugin.c',
]

libpika_headers_introspectable = [
  # Order matters. Leave these headers at the top.
  'pika.h',
  'pikatypes.h',
  'pikaenums.h',

  # Other headers
  'pikabatchprocedure.h',
  'pikabrush.h',
  'pikachannel.h',
  'pikadisplay.h',
  'pikadrawable.h',
  'pikafileprocedure.h',
  'pikafont.h',
  'pikapikarc.h',
  'pikagradient.h',
  'pikaimage.h',
  'pikaimagecolorprofile.h',
  'pikaimagemetadata.h',
  'pikaimageprocedure.h',
  'pikaitem.h',
  'pikalayer.h',
  'pikalayermask.h',
  'pikaloadprocedure.h',
  'pikapalette.h',
  'pikaparamspecs.h',
  'pikapattern.h',
  'pikapdb.h',
  'pikaplugin.h',
  'pikaprocedure.h',
  'pikaprocedureconfig.h',
  'pikaprogress.h',
  'pikaresource.h',
  'pikaresourceselect.h',
  'pikasaveprocedure.h',
  'pikaselection.h',
  'pikatextlayer.h',
  'pikathumbnailprocedure.h',
  'pikavectors.h',
  pdb_wrappers_headers,
]

libpika_headers = [
  libpika_headers_introspectable,
  'pikaprocedure-params.h',
]

libpikaui_sources_introspectable = [
  'pikaaspectpreview.c',
  'pikabrushchooser.c',
  'pikadrawablechooser.c',
  'pikadrawablepreview.c',
  'pikaexport.c',
  'pikafontchooser.c',
  'pikagradientchooser.c',
  'pikaimagecombobox.c',
  'pikaitemcombobox.c',
  'pikapalettechooser.c',
  'pikapatternchooser.c',
  'pikaprocbrowserdialog.c',
  'pikaproceduredialog.c',
  'pikaprocview.c',
  'pikaprogressbar.c',
  'pikapropwidgets.c',
  'pikaresourcechooser.c',
  'pikasaveproceduredialog.c',
  'pikaui.c',
  'pikazoompreview.c',
]

libpikaui_sources = [
  libpikaui_sources_introspectable,
  pikauimarshal,
]

libpikaui_headers_introspectable = [
  # Order matters. Leave these headers at the top.
  'pikaui.h',
  'pikauitypes.h',

  # Other headers
  'pikaaspectpreview.h',
  'pikabrushchooser.h',
  'pikadrawablechooser.h',
  'pikadrawablepreview.h',
  'pikaexport.h',
  'pikafontchooser.h',
  'pikagradientchooser.h',
  'pikaimagecombobox.h',
  'pikaitemcombobox.h',
  'pikapalettechooser.h',
  'pikapatternchooser.h',
  'pikaprocbrowserdialog.h',
  'pikaproceduredialog.h',
  'pikaprocview.h',
  'pikaprogressbar.h',
  'pikapropwidgets.h',
  'pikaresourcechooser.h',
  'pikasaveproceduredialog.h',
  'pikazoompreview.h',
]

libpika_introspectable = [
  libpika_sources_introspectable,
  libpika_headers_introspectable,
]

libpikaui_introspectable = [
  libpikaui_sources_introspectable,
  libpikaui_headers_introspectable,
]


libpika = library('pika-'+ pika_api_version,
  libpika_sources,
  include_directories: rootInclude,
  dependencies: [
    cairo,
    gdk_pixbuf,
    gegl,
    gexiv2,
    drmingw,
    pango,
    pangoft2,
  ],
  sources: [
    pdbgen
  ],
  c_args: [ '-DG_LOG_DOMAIN="LibPika"', '-DPIKA_COMPILATION', ],
  link_with: [
    libpikabase,
    libpikacolor,
    libpikaconfig,
    libpikamodule,
  ],
  vs_module_defs: 'pika.def',
  install: true,
  version: so_version,
)

# Define an internal dependency for ease of reuse
libpika_dep = declare_dependency(
  dependencies: [
    cairo,
    gdk_pixbuf,
    gegl,
  ],
  link_with: [
    libpika,
    libpikabase,
    libpikacolor,
    libpikaconfig,
    libpikamath,
    libpikamodule,
  ],
  include_directories: rootInclude,
)


libpikaui = library('pikaui-'+ pika_api_version,
  libpikaui_sources,
  dependencies: [
    libpika_dep,
    gexiv2,
    gtk3,
  ],
  c_args: [ '-DG_LOG_DOMAIN="LibPikaUI"', '-DPIKA_COMPILATION', ],
  link_with: [
    libpikawidgets,
  ],
  vs_module_defs: 'pikaui.def',
  install: true,
  version: so_version,
)

# Define an internal dependency for ease of reuse
libpikaui_dep = declare_dependency(
  dependencies: [
    libpika_dep,
    gtk3,
  ],
  link_with: [
    libpikaui,
    libpikawidgets,
  ],
  include_directories: rootInclude,
)


install_headers(
  libpika_headers,
  libpikaui_headers_introspectable,
  subdir: pika_api_name / 'libpika',
)



### GObject introspection

libpika_introspectable_files = [
  libpika_introspectable,
  libpikabase_introspectable,
  libpikacolor_introspectable,
  libpikaconfig_introspectable,
  libpikamath_introspectable,
  libpikamodule_introspectable,
  'pikaparamspecs-body.c'
]

libpikaui_introspectable_files = [
  libpikaui_introspectable,
  libpikawidgets_introspectable,
]

if meson.is_cross_build() and not get_option('can-crosscompile-gir')
  have_gobject_introspection = false
  gobject_introspection_warning = '\n      '.join(['',
    'GObject Introspection is disabled while cross-compiling because',
    'GI tools do not manage to properly generate introspection data',
    'while cross-compiling. Nevertheless we consider our introspected',
    'API as a core component of PIKA, thus you are expected to build',
    'this data natively.',
    'To forcefully build with GObject Introspection, set:',
    '-Dcan-crosscompile-gir=true'
  ])
  warning(gobject_introspection_warning)
  warnings += gobject_introspection_warning
elif get_option('b_sanitize') != 'none'
  have_gobject_introspection = false
  gobject_introspection_warning = '\n      '.join(['',
    'GObject Introspection is disabled while building with a sanitizer.',
    'See: https://gitlab.gnome.org/GNOME/gobject-introspection/-/issues/375'
  ])
  warning(gobject_introspection_warning)
  warnings += gobject_introspection_warning
else
  have_gobject_introspection = true
  libpika_gir_includes = [
    'Babl-0.1',
    'cairo-1.0',
    'GdkPixbuf-2.0',
    'Gegl-0.4',
    'Gio-2.0',
    'GLib-2.0',
    'GObject-2.0',
    'Pango-1.0',
  ]

  gir_args = [
    '-DPIKA_COMPILATION',
    '-DPIKA_BASE_COMPILATION',
    '-DPIKA_COLOR_COMPILATION',
    '-DPIKA_CONFIG_COMPILATION',
    '-DPIKA_MATH_COMPILATION',
    '-DPIKA_MODULE_COMPILATION',
    '-DPIKA_THUMB_COMPILATION',
    '-DPIKA_WIDGETS_COMPILATION',
    '-DPIKA_DISABLE_DEPRECATED',
    '--quiet',
  ]

  libpika_gir = gnome.generate_gir(
    libpika,
    libpikabase,
    libpikacolor,
    libpikaconfig,
    libpikamath,
    libpikamodule,

    sources: libpika_introspectable_files,
    nsversion: pika_api_version,
    namespace: 'Pika',
    identifier_prefix: 'Pika',
    symbol_prefix: 'pika',
    export_packages: 'pika-' + pika_api_version,
    header: 'libpika/pika.h',
    includes: libpika_gir_includes,
    extra_args: gir_args,
    include_directories: rootInclude,
    install: true,
  )

  libpikaui_gir = gnome.generate_gir(
    libpikaui,
    libpikawidgets,

    sources: libpikaui_introspectable_files,
    nsversion: pika_api_version,
    namespace: 'PikaUi',
    identifier_prefix: 'Pika',
    symbol_prefix: [ 'pika_ui', 'pika' ],
    export_packages: 'pika-ui-' + pika_api_version,
    header: 'libpika/pikaui.h',
    includes: [ libpika_gir_includes, 'Gtk-3.0', libpika_gir[0] ],
    extra_args: gir_args,
    include_directories: rootInclude,
    install: true,
  )

  if have_vala
    vapi_deps = [
      'babl-0.1',
      'cairo-1.0',
      'gdk-pixbuf-2.0',
      'gegl-0.4',
      'gio-2.0',
      'glib-2.0',
      'gobject-2.0',
      'gtk+-3.0',
    ]

    libpika_vapi = gnome.generate_vapi('pika-' + pika_api_version,
      sources: libpika_gir[0],
      packages: vapi_deps,
      install: true,
    )

    libpikaui_vapi = gnome.generate_vapi('pika-ui-' + pika_api_version,
      sources: libpikaui_gir[0],
      packages: vapi_deps + [ libpika_vapi ],
      install: true,
    )
  endif
endif

if have_python and not platform_windows and not platform_osx
  # Verify .def files for Windows.
  # Ironically we only check this on non-Windows platform, since the
  # script expects .so libraries, and I'm not sure that the `nm` tool is
  # available on Windows.
  # On the other hand, it is how it always has been, since we used to
  # only run this on dist step (which we historically run on a GNU/Linux
  # machine). And it worked fine until now.
  custom_target('check-def-files',
    input: [
      '../libpikabase/pikabase.def',
      '../libpikacolor/pikacolor.def',
      '../libpikaconfig/pikaconfig.def',
      '../libpika/pika.def',
      '../libpika/pikaui.def',
      '../libpikamath/pikamath.def',
      '../libpikamodule/pikamodule.def',
      '../libpikathumb/pikathumb.def',
      '../libpikawidgets/pikawidgets.def',
    ],
    depends: [
      libpikabase,
      libpikacolor,
      libpikaconfig,
      libpika,
      libpikaui,
      libpikamath,
      libpikamodule,
      libpikathumb,
      libpikawidgets
    ],
    output: [ 'check-def-files', ],
    command: [
      python, meson.project_source_root() / 'tools' / 'defcheck.py', meson.project_source_root(),
    ],
    build_by_default: true,
  )
endif
