project(
  'zlib',
  'c',
  version: '1.2.13',
  license: 'zlib',
  meson_version: '>=0.49.0',
)

cc = meson.get_compiler('c', native: true)

link_args = cc.get_supported_link_arguments(
  '-Wl,--version-script,@0@/zlib.map'.format(meson.current_source_dir()),
)

compile_args = cc.get_supported_arguments(
  '/wd4131', # 'function' : uses old-style declarator
  '/wd4244', # 'conversion' conversion from 'type1' to 'type2', possible loss of data
  '/wd4245', # 'conversion' : conversion from 'type1' to 'type2', signed/unsigned mismatch
  '/wd4267', # 'var' : conversion from 'size_t' to 'type', possible loss of data
  '/wd4127', # conditional expression is constant
  '-Wno-implicit-fallthrough',
  '-Wno-implicit-function-declaration',
)

if cc.get_argument_syntax() == 'msvc'
  compile_args += ['-D_CRT_SECURE_NO_DEPRECATE', '-D_CRT_NONSTDC_NO_DEPRECATE']
endif

src = files(
  'adler32.c',
  'crc32.c',
  'deflate.c',
  'infback.c',
  'inffast.c',
  'inflate.c',
  'inftrees.c',
  'trees.c',
  'zutil.c',
)

headers = files('zconf.h', 'zlib.h')

if build_machine.system() == 'windows'
  win = import('windows')
  win_args = []
  if cc.get_argument_syntax() != 'msvc'
    win_args += '-DGCC_WINDRES'
  endif
  src += win.compile_resources('win32/zlib1.rc', args: win_args)
endif

solo_args = []
if get_option('solo')
  if build_machine.system() == 'windows' and get_option('default_library') != 'static'
    error('-Dsolo=true is incompatible with building a DLL')
  endif
  solo_args = ['-DZ_SOLO']
  add_project_arguments(solo_args, language: 'c')
else
  src += files(
    'compress.c',
    'uncompr.c',
    'gzclose.c',
    'gzlib.c',
    'gzread.c',
    'gzwrite.c',
  )
endif

zlib = library(
  'z',
  src,
  c_args: compile_args,
  link_args: link_args,
  native: true,
  install: false,
)

depinc = include_directories('.')
zlib_dep = declare_dependency(
  compile_args: solo_args,
  link_with: zlib,
  include_directories: depinc,
)

install_headers(headers)

pkg = import('pkgconfig')

pkg.generate(
  zlib,
  name: 'zlib',
  description: 'zlib compression library',
  extra_cflags: solo_args,
)
