if get_option('tests')
  subdir('tests')
endif

fwupd_version_h = configure_file(
  input: 'fwupd-version.h.in',
  output: 'fwupd-version.h',
  configuration: conf,
)

base_dir = 'fwupd-' + libfwupd_lt_current

fwupd_structs = [
  'fwupd-bios-setting.rs', # fuzzing
  'fwupd-client.rs', # fuzzing
  'fwupd-codec.rs', # fuzzing
  'fwupd-device.rs', # fuzzing
  'fwupd-enums.rs', # fuzzing
  'fwupd-error.rs', # fuzzing
  'fwupd-json.rs', # fuzzing
  'fwupd-plugin.rs', # fuzzing
  'fwupd-release.rs', # fuzzing
  'fwupd-remote.rs', # fuzzing
  'fwupd-report.rs', # fuzzing
  'fwupd-request.rs', # fuzzing
  'fwupd-security-attr.rs', # fuzzing
]

# do not use structgen as these are referenced in headers too...
fwupd_rs_targets = []
foreach fwupd_struct : fwupd_structs
  fwupd_rs_targets += custom_target(
    'lib' + fwupd_struct + '.h',
    input: fwupd_struct,
    output: fwupd_struct.replace('.rs', '-struct.h'),
    command: [
      python3,
      join_paths(meson.project_source_root(), 'libfwupdplugin', 'rustgen.py'),
      '--include',
      'libfwupd/fwupd-build.h',
      '@INPUT@',
      '--outh',
      '@OUTPUT@',
    ],
    install: true,
    install_dir: join_paths(includedir, base_dir, 'libfwupd'),
  )
  fwupd_rs_targets += custom_target(
    'lib' + fwupd_struct + '.c',
    input: fwupd_struct,
    output: fwupd_struct.replace('.rs', '-struct.c'),
    command: [
      python3,
      join_paths(meson.project_source_root(), 'libfwupdplugin', 'rustgen.py'),
      '--include',
      'fwupd-build.h',
      '@INPUT@',
      '--outc',
      '@OUTPUT@',
    ],
  )
endforeach

install_headers(
  'fwupd.h',
  subdir: base_dir,
)

install_headers(
  [
    'fwupd-build.h',
    'fwupd-client.h',
    'fwupd-client-sync.h',
    'fwupd-common.h',
    'fwupd-codec.h',
    'fwupd-device.h',
    'fwupd-enums.h',
    'fwupd-error.h',
    'fwupd-remote.h',
    'fwupd-report.h',
    'fwupd-request.h',
    'fwupd-json-array.h',
    'fwupd-json-common.h',
    'fwupd-json-node.h',
    'fwupd-json-object.h',
    'fwupd-json-parser.h',
    'fwupd-bios-setting.h',
    'fwupd-security-attr.h',
    'fwupd-release.h',
    'fwupd-plugin.h',
    fwupd_version_h,
  ],
  subdir: join_paths(base_dir, 'libfwupd'),
)

libfwupd_deps = [giounix, libjcat, libcurl]

libfwupd_src = [
  'fwupd-client.c',
  'fwupd-client-sync.c',
  'fwupd-common.c', # fuzzing
  'fwupd-codec.c', # fuzzing
  'fwupd-device.c', # fuzzing
  'fwupd-error.c', # fuzzing
  'fwupd-bios-setting.c', # fuzzing
  'fwupd-json-array.c', # fuzzing
  'fwupd-json-common.c', # fuzzing
  'fwupd-json-node.c', # fuzzing
  'fwupd-json-object.c', # fuzzing
  'fwupd-json-parser.c', # fuzzing
  'fwupd-security-attr.c', # fuzzing
  'fwupd-release.c', # fuzzing
  'fwupd-plugin.c',
  'fwupd-remote.c',
  'fwupd-report.c', # fuzzing
  'fwupd-request.c', # fuzzing
  'fwupd-version.c',
]

fwupd_mapfile = 'fwupd.map'
vflag = '-Wl,--version-script,@0@/@1@'.format(meson.current_source_dir(), fwupd_mapfile)
fwupd = library(
  'fwupd',
  fwupd_rs_targets,
  sources: libfwupd_src,
  soversion: libfwupd_lt_current,
  version: libfwupd_lt_version,
  dependencies: libfwupd_deps,
  c_args: ['-DG_LOG_DOMAIN="Fwupd"', '-DLOCALSTATEDIR="' + localstatedir + '"'],
  include_directories: root_incdir,
  link_args: cc.get_supported_link_arguments([vflag]),
  link_depends: fwupd_mapfile,
  install: true,
  install_tag: 'lib',
)

# see https://mesonbuild.com/FAQ.html#how-do-i-tell-meson-that-my-sources-use-generated-headers
fwupd_rs_headers = []
foreach fwupd_rs_target : fwupd_rs_targets
  fwupd_rs_headers += fwupd_rs_target
endforeach
fwupd_rs_dep = declare_dependency(
  link_with: fwupd,
  sources: fwupd_rs_headers,
)

libfwupd_dep = declare_dependency(
  link_with: fwupd,
  include_directories: [root_incdir, include_directories('.')],
  dependencies: libfwupd_deps,
)

pkgg = import('pkgconfig')
pkgg.generate(
  fwupd,
  requires: ['gio-2.0'],
  subdirs: base_dir,
  version: meson.project_version(),
  name: 'fwupd',
  filebase: 'fwupd',
  description: 'fwupd is a system daemon for installing device firmware',
)

if introspection.allowed()
  fwupd_gir_deps = [giounix, libcurl]
  fwupd_gir = gnome.generate_gir(
    fwupd,
    sources: [
      'fwupd-client.c',
      'fwupd-client.h',
      'fwupd-client-sync.c',
      'fwupd-client-sync.h',
      'fwupd-common.c',
      'fwupd-common.h',
      'fwupd-common-private.h',
      'fwupd-codec.c',
      'fwupd-codec.h',
      'fwupd-device.c',
      'fwupd-device.h',
      'fwupd-device-private.h',
      'fwupd-enums.h',
      'fwupd-enums-private.h',
      'fwupd-error.c',
      'fwupd-error.h',
      'fwupd-bios-setting.c',
      'fwupd-bios-setting.h',
      'fwupd-json-array.c',
      'fwupd-json-array.h',
      'fwupd-json-common.c',
      'fwupd-json-common.h',
      'fwupd-json-node.c',
      'fwupd-json-node.h',
      'fwupd-json-object.c',
      'fwupd-json-object.h',
      'fwupd-json-parser.c',
      'fwupd-json-parser.h',
      'fwupd-security-attr.c',
      'fwupd-security-attr.h',
      'fwupd-security-attr-private.h',
      'fwupd-release.c',
      'fwupd-release.h',
      'fwupd-plugin.c',
      'fwupd-plugin.h',
      'fwupd-remote.c',
      'fwupd-remote.h',
      'fwupd-remote-private.h',
      'fwupd-report.c',
      'fwupd-report.h',
      'fwupd-request.c',
      'fwupd-request.h',
      'fwupd-request-private.h',
      'fwupd-version.c',
      fwupd_version_h,
      fwupd_rs_targets,
    ],
    nsversion: '2.0',
    namespace: 'Fwupd',
    symbol_prefix: 'fwupd',
    identifier_prefix: ['Fwupd', 'fwupd'],
    export_packages: 'fwupd',
    header: 'fwupd.h',
    dependencies: fwupd_gir_deps,
    includes: ['Gio-2.0', 'GObject-2.0'],
    install: true,
  )

  gnome.generate_vapi(
    'fwupd',
    sources: fwupd_gir[0],
    packages: ['gio-2.0'],
    install: true,
  )

  # Verify the map file is correct -- note we can't actually use the generated
  # file for two reasons:
  #
  #  1. We don't hard depend on GObject Introspection
  #  2. The map file is required to build the lib that the GIR is built from
  #
  # To avoid the circular dep, and to ensure we don't change exported API
  # accidentally actually check in a version of the version script to git.
  mapfile_target = custom_target(
    'fwupd_mapfile',
    input: fwupd_gir[0],
    output: 'fwupd.map',
    command: [generate_version_script, 'LIBFWUPD', '@INPUT@', '@OUTPUT@'],
  )
  test(
    'fwupd-exported-api',
    diffcmd,
    args: ['-urNp', files('fwupd.map'), mapfile_target],
  )
endif

if get_option('tests')
  env = environment()
  env.set('G_TEST_SRCDIR', meson.current_source_dir())
  env.set('G_TEST_BUILDDIR', meson.current_build_dir())

  libfwupdtest = static_library(
    'fwupd-test',
    fwupd_rs_targets,
    sources: ['fwupd-test.c'] + libfwupd_src,
    include_directories: [root_incdir],
    dependencies: [libfwupd_deps],
  )
  foreach test_name: [
    'bios-setting',
    'client',
    'common',
    'device',
    'enums',
    'json',
    'plugin',
    'release',
    'remote',
    'report',
    'request',
    'security-attr',
  ]
    e = executable(
      'fwupd-' + test_name + '-test',
      fwupd_rs_targets,
      sources: [
        'fwupd-' + test_name + '-test.c',
      ],
      include_directories: [root_incdir],
      dependencies: [libfwupd_deps, fwupd_rs_dep],
      link_with: libfwupdtest,
      c_args: [
        '-DG_LOG_DOMAIN="Fwupd"',
        '-DSRCDIR="' + meson.current_source_dir() + '"',
        '-DLOCALSTATEDIR="' + localstatedir + '"',
      ],
    )
    test(
      'fwupd-' + test_name + '-test',
      e,
      timeout: 60,
      env: env,
    )
  endforeach

  if run_sanitize_unsafe_tests and gio.version().version_compare('>= 2.64.0')
    e = executable(
      'fwupd-thread-test',
      sources: ['fwupd-thread-test.c'],
      include_directories: [root_incdir],
      dependencies: [libfwupd_deps, fwupd_rs_dep],
      link_with: fwupd,
      c_args: ['-DG_LOG_DOMAIN="Fwupd"'],
    )
    test(
      'fwupd-thread-test',
      e,
      timeout: 60,
    )
    e = executable(
      'fwupd-context-test',
      sources: ['fwupd-context-test.c'],
      include_directories: [root_incdir],
      dependencies: [libfwupd_deps, fwupd_rs_dep],
      link_with: fwupd,
      c_args: ['-DG_LOG_DOMAIN="Fwupd"'],
    )
    test(
      'fwupd-context-test',
      e,
      timeout: 60,
    )
  endif
endif

fwupd_incdir = include_directories('.')
