wayland_version = meson.project_version().split('.')
wayland_version_h = configuration_data()
wayland_version_h.set('WAYLAND_VERSION', meson.project_version())
wayland_version_h.set('WAYLAND_VERSION_MAJOR', wayland_version[0].to_int())
wayland_version_h.set('WAYLAND_VERSION_MINOR', wayland_version[1].to_int())
wayland_version_h.set('WAYLAND_VERSION_MICRO', wayland_version[2].to_int())
configure_file(
	input: 'wayland-version.h.in',
	output: 'wayland-version.h',
	configuration: wayland_version_h,
	install: get_option('libraries'),
	install_dir: join_paths(get_option('prefix'), get_option('includedir'))
)


wayland_util = static_library(
	'wayland-util',
	sources: 'wayland-util.c'
)

wayland_util_dep = declare_dependency(
	link_with: wayland_util,
	include_directories: include_directories('.')
)

if get_option('scanner')
	# wayland-scanner

	configure_file(
		input: '../protocol/wayland.dtd',
		output: 'wayland.dtd.embed',
		copy: true
	)

	wayland_scanner_sources = [ 'scanner.c', 'dtddata.S' ]
	wayland_scanner_includes = [ root_inc, protocol_inc ]

	wayland_scanner = executable(
		'wayland-scanner',
		wayland_scanner_sources,
		c_args: [ '-include', 'config.h' ],
		include_directories: wayland_scanner_includes,
		dependencies: [ scanner_deps, wayland_util_dep, ],
		install: true
	)

	pkgconfig.generate(
		name: 'Wayland Scanner',
		description: 'Wayland scanner',
		version: meson.project_version(),
		variables: [
			'datarootdir=' + join_paths('${prefix}', get_option('datadir')),
			'pkgdatadir=' + join_paths('${datarootdir}', meson.project_name()),
			'bindir=' + join_paths('${prefix}', get_option('bindir')),
			'wayland_scanner=${bindir}/wayland-scanner'
		],
		filebase: 'wayland-scanner'
	)
endif

if meson.is_cross_build() or not get_option('scanner')
	scanner_dep = dependency('wayland-scanner', native: true, version: meson.project_version())
	wayland_scanner_for_build = find_program(scanner_dep.get_pkgconfig_variable('wayland_scanner'))
else
	wayland_scanner_for_build = wayland_scanner
endif

if get_option('libraries')
	# wayland libraries

	mathlib_dep = cc.find_library('m', required: false)
	threads_dep = dependency('threads', required: false)

	wayland_private = static_library(
		'wayland-private',
		sources: [
			'connection.c',
			'wayland-os.c'
		],
		dependencies: [ ffi_dep, rt_dep ]
	)

	wayland_private_dep = declare_dependency(
		link_with: wayland_private,
		include_directories: include_directories('.')
	)

	generated_headers = [
		{
			'scanner_args': ['server-header'],
			'output': 'wayland-server-protocol.h',
			'install': true,
		},
		{
			'scanner_args': ['server-header', '-c'],
			'output': 'wayland-server-protocol-core.h',
			'install': false,
		},
		{
			'scanner_args': ['client-header'],
			'output': 'wayland-client-protocol.h',
			'install': true,
		},
		{
			'scanner_args': ['client-header', '-c'],
			'output': 'wayland-client-protocol-core.h',
			'install': false,
		},
	]

	foreach gen: generated_headers
		scanner_args = gen['scanner_args']
		output_file = gen['output']
		install_file = gen['install']
		install_dir = join_paths(get_option('prefix'), get_option('includedir'))
		target_name = output_file.underscorify()

		target = custom_target(
			target_name,
			command: [
				wayland_scanner_for_build, '-s', scanner_args,
				'@INPUT@', '@OUTPUT@'
			],
			input: wayland_protocol_xml,
			output: output_file,
			install: install_file,
			install_dir: install_dir
		)

		set_variable(target_name, target)
	endforeach

	wayland_protocol_c = custom_target(
		'protocol source',
		command: [
			wayland_scanner_for_build, '-s', 'public-code', '@INPUT@', '@OUTPUT@'
		],
		input: wayland_protocol_xml,
		output: 'wayland-protocol.c'
	)

	wayland_server = library(
		'wayland-server',
		sources: [
			wayland_server_protocol_core_h,
			wayland_server_protocol_h,
			wayland_protocol_c,
			'wayland-server.c',
			'wayland-shm.c',
			'event-loop.c'
		],
		version: '0.1.0',
		dependencies: [
			ffi_dep,
			wayland_private_dep,
			wayland_util_dep,
			mathlib_dep,
			threads_dep,
			rt_dep
		],
		include_directories: root_inc,
		install: true
	)

	wayland_server_dep = declare_dependency(
		link_with: wayland_server,
		include_directories: [ root_inc, include_directories('.') ],
		dependencies: [ ffi_dep, mathlib_dep, threads_dep ],
		sources: [
			wayland_server_protocol_core_h,
			wayland_server_protocol_h
		]
	)

	pkgconfig.generate(
		wayland_server,
		name: 'Wayland Server',
		description: 'Server side implementation of the Wayland protocol',
		version: meson.project_version(),
		filebase: 'wayland-server',
		variables: [
			'datarootdir=' + join_paths('${prefix}', get_option('datadir')),
			'pkgdatadir=' + join_paths('${datarootdir}', meson.project_name())
		]
	)

	wayland_client = library(
		'wayland-client',
		sources: [
			wayland_client_protocol_core_h,
			wayland_client_protocol_h,
			wayland_protocol_c,
			'wayland-client.c'
		],
		version: '0.3.0',
		dependencies: [
			ffi_dep,
			wayland_private_dep,
			wayland_util_dep,
			mathlib_dep,
			threads_dep
		],
		include_directories: root_inc,
		install: true
	)

	pkgconfig.generate(
		wayland_client,
		name: 'Wayland Client',
		description: 'Wayland client side library',
		version: meson.project_version(),
		filebase: 'wayland-client',
		variables: [
			'datarootdir=' + join_paths('${prefix}', get_option('datadir')),
			'pkgdatadir=' + join_paths('${datarootdir}', meson.project_name())
		]
	)

	wayland_client_dep = declare_dependency(
		link_with: wayland_client,
		include_directories: [ root_inc, include_directories('.') ],
		sources: [
			wayland_client_protocol_core_h,
			wayland_client_protocol_h
		]
	)

	install_headers([
		'wayland-util.h',
		'wayland-server.h',
		'wayland-server-core.h',
		'wayland-client.h',
		'wayland-client-core.h',
	])
endif