Index: meson/part/compiler_args/meson.build
===================================================================
--- meson/part/compiler_args/meson.build	(revision 200b2113fc4b438d75d53ae122f1828de1728eb8)
+++ meson/part/compiler_args/meson.build	(revision 200b2113fc4b438d75d53ae122f1828de1728eb8)
@@ -0,0 +1,103 @@
+# This file sets the architecture-independent compiler flags used throughout
+# this repository.
+# For architecture-specific flags, see $srcroot/meson/cross/$arch.
+
+add_project_arguments(
+	# TODO: Remove from project arguments and only use where necessary.
+	# Any change in config forces everything to rebuild due to lack of granularity here.
+	'-imacros', meson.build_root() / 'config.h',
+	language : [ 'c' ],
+)
+
+add_project_link_arguments(
+	cc.get_supported_link_arguments([
+		'-Wl,--gc-sections',
+		'-Wl,--warn-common',
+	]),
+	'-Wl,--fatal-warnings',
+	language : [ 'c', 'cpp' ],
+)
+
+# TODO: enable more warnings
+# FIXME: -fno-builtin-strftime works around seemingly spurious format warning.
+# We should investigate what's going on there.
+
+extra_common_flags = [
+	'-O' + OPTIMIZATION,
+	'-fexec-charset=UTF-8',
+	'-finput-charset=UTF-8',
+
+	'-D_HELENOS_SOURCE',
+
+	'-Wa,--fatal-warnings',
+
+	'-Wall',
+	'-Wextra',
+	'-Werror-implicit-function-declaration',
+	'-Wwrite-strings',
+	'-Wunknown-pragmas',
+
+	'-Wno-unused-parameter',
+
+	'-pipe',
+
+	'-ffunction-sections',
+	'-fno-common',
+	'-fdebug-prefix-map=' + meson.source_root() + '=.',
+]
+
+if cc.get_id() != 'clang'
+	# Clang's own headers emit macro redefinition warnings.
+	extra_common_flags += '-Wsystem-headers'
+endif
+
+if UARCH != 'ia64'
+	extra_common_flags += [ '-fvar-tracking-assignments' ]
+endif
+
+if CONFIG_DEBUG
+	extra_common_flags += [ '-Werror' ]
+endif
+
+if CONFIG_LINE_DEBUG
+	extra_common_flags += [ '-gdwarf-4', '-g3' ]
+endif
+
+extra_cflags = extra_common_flags + [
+	'-Wmissing-prototypes',
+
+	'-Wno-missing-braces',
+	'-Wno-missing-field-initializers',
+	'-Wno-unused-command-line-argument',
+	'-Wno-unused-parameter',
+	'-Wno-typedef-redefinition',
+	'-Wno-clobbered',
+	'-Wno-nonnull-compare',
+
+	'-fno-builtin-strftime',
+]
+
+if CONFIG_UBSAN
+	extra_cflags += '-fsanitize=undefined'
+endif
+
+extra_cppflags = extra_common_flags + [
+	'-fno-exceptions',
+	'-frtti',
+]
+
+w_flags = {
+	'c': extra_cflags,
+	'cpp': extra_cppflags,
+}
+
+# TODO: To remove noise in Meson output, we may want to cut down on
+#       the explicitly checked flags and just enable those supported by
+#       both gcc and clang unconditionally.
+
+# Process flags. Only sets those that compiler supports.
+foreach lang : [ 'c', 'cpp' ]
+	extra_cflags = meson.get_compiler(lang).get_supported_arguments(w_flags.get(lang))
+	add_project_arguments(extra_cflags, language : [ lang ])
+	add_project_link_arguments(extra_cflags, language : [ lang ])
+endforeach
Index: meson/part/exports/config.mk.in
===================================================================
--- meson/part/exports/config.mk.in	(revision 200b2113fc4b438d75d53ae122f1828de1728eb8)
+++ meson/part/exports/config.mk.in	(revision 200b2113fc4b438d75d53ae122f1828de1728eb8)
@@ -0,0 +1,10 @@
+# Generated from config.mk.in using Meson. Do not modify.
+# Do not forget to set HELENOS_EXPORT_ROOT when using the file.
+HELENOS_CROSS_PATH="@HELENOS_CROSS_PATH@"
+HELENOS_ARCH="@HELENOS_ARCH@"
+HELENOS_TARGET="@HELENOS_TARGET@"
+HELENOS_CPPFLAGS="@HELENOS_CPPFLAGS@"
+HELENOS_CFLAGS="@HELENOS_CFLAGS@"
+HELENOS_CXXFLAGS="@HELENOS_CXXFLAGS@"
+HELENOS_LDFLAGS="@HELENOS_LDFLAGS@"
+HELENOS_LDLIBS="@HELENOS_LDLIBS@"
Index: meson/part/exports/meson.build
===================================================================
--- meson/part/exports/meson.build	(revision 200b2113fc4b438d75d53ae122f1828de1728eb8)
+++ meson/part/exports/meson.build	(revision 200b2113fc4b438d75d53ae122f1828de1728eb8)
@@ -0,0 +1,62 @@
+## Generate config.mk, config.sh
+
+# Get the directory where the compiler resides.
+
+cc_fullname = run_command(which, meson.get_compiler('c').cmd_array()[0].split(' ')[0], check: true).stdout().strip()
+cc_path = run_command(dirname, cc_fullname, check: true).stdout().strip()
+
+message(['Compiler directory is:', cc_path])
+
+export_cppflags = [
+	'-isystem', '$(HELENOS_EXPORT_ROOT)/include/posix',
+	'-isystem', '$(HELENOS_EXPORT_ROOT)/include/libc',
+	'-idirafter', '$(HELENOS_EXPORT_ROOT)/include',
+]
+
+export_ldflags = [
+	'-nostdlib',
+	'-L$(HELENOS_EXPORT_ROOT)/lib',
+	'-Wl,--whole-archive',
+	'$(HELENOS_EXPORT_ROOT)/lib/libstartfiles.a',
+	'-Wl,--no-whole-archive',
+]
+
+export_ldlibs = [
+	'-Wl,--as-needed',
+	'-lposix',
+	'-lmath',
+	'-lc',
+	'-lgcc',
+	'-Wl,--no-as-needed',
+]
+
+cc_arch = meson.get_cross_property('cc_arch')
+
+conf_data = configuration_data({
+	'HELENOS_CROSS_PATH' : cc_path,
+	'HELENOS_ARCH' : cc_arch,
+	'HELENOS_CFLAGS' : ' '.join(arch_uspace_c_args),
+	'HELENOS_CXXFLAGS' : ' '.join(arch_uspace_c_args),
+	'HELENOS_CPPFLAGS' : ' '.join(export_cppflags),
+	'HELENOS_LDFLAGS' : ' '.join(export_ldflags),
+	'HELENOS_LDLIBS' : ' '.join(export_ldlibs),
+	'HELENOS_TARGET' : cc_arch + '-helenos',
+})
+
+config_mk = configure_file(
+	input: 'config.mk.in',
+	output: 'config.mk',
+	configuration: conf_data,
+)
+
+config_sh = custom_target('config.sh',
+	input: config_mk,
+	output: 'config.sh',
+	command: [ sed, 's:$(HELENOS_EXPORT_ROOT):${HELENOS_EXPORT_ROOT}:g', '@INPUT@' ],
+	capture: true,
+)
+
+install_files += [[ 'config', meson.current_build_dir() / 'config.mk', 'config.mk' ]]
+install_deps += [ config_mk ]
+install_files += [[ 'config', meson.current_build_dir() / 'config.sh', 'config.sh' ]]
+install_deps += [ config_sh ]
Index: meson/part/extra_targets/meson.build
===================================================================
--- meson/part/extra_targets/meson.build	(revision 200b2113fc4b438d75d53ae122f1828de1728eb8)
+++ meson/part/extra_targets/meson.build	(revision 200b2113fc4b438d75d53ae122f1828de1728eb8)
@@ -0,0 +1,36 @@
+run_target('config',
+	command: [
+		sh,
+		'-c', 'cd $1 && $2 $3 $4',
+		'--',
+		meson.build_root(),
+		config_py.path(),
+		meson.source_root() / 'HelenOS.config',
+		meson.source_root() / 'defaults',
+	]
+)
+
+# TODO: Check when cross target has changed, since it won't work.
+
+if false
+	# TODO: doesn't work because it changes cross target
+	run_target('random-config',
+		command: [
+			sh,
+			'-c', 'cd $1 && $2 $3 $4 random',
+			'--',
+			meson.build_root(),
+			config_py.path(),
+			meson.source_root() / 'HelenOS.config',
+			meson.source_root() / 'defaults',
+		]
+	)
+endif
+
+
+# TODO text-xcw
+#ifeq ($(CONFIG_DEVEL_FILES),y)
+#	export PATH=$$PATH:$(abspath tools/xcw/bin) && $(MAKE) -r -C tools/xcw/demo
+#endif
+
+# TODO special target for posix and xcw exports, update coastline for it
Index: meson/part/initrd/install.sh.in
===================================================================
--- meson/part/initrd/install.sh.in	(revision 200b2113fc4b438d75d53ae122f1828de1728eb8)
+++ meson/part/initrd/install.sh.in	(revision 200b2113fc4b438d75d53ae122f1828de1728eb8)
@@ -0,0 +1,23 @@
+#!/bin/sh
+
+if [ "$#" -ge 1 ]; then
+	touch "$1"
+fi
+
+if [ "$#" -ge 2 ]; then
+	DESTDIR="$2"
+else
+	DESTDIR="${MESON_INSTALL_DESTDIR_PREFIX}"
+fi
+
+rm -rf "${DESTDIR}"
+
+mkdir -p "${DESTDIR}cfg/"
+mkdir -p "${DESTDIR}data/"
+mkdir -p "${DESTDIR}loc/"
+mkdir -p "${DESTDIR}log/"
+mkdir -p "${DESTDIR}tmp/"
+mkdir -p "${DESTDIR}vol/"
+mkdir -p "${DESTDIR}w/"
+
+@text@
Index: meson/part/initrd/meson.build
===================================================================
--- meson/part/initrd/meson.build	(revision 200b2113fc4b438d75d53ae122f1828de1728eb8)
+++ meson/part/initrd/meson.build	(revision 200b2113fc4b438d75d53ae122f1828de1728eb8)
@@ -0,0 +1,69 @@
+if CONFIG_DEVEL_FILES
+	# If devel files are requested, we have to install libgcc.
+	# We have to explicitly use gcc for this, because clang only prints
+	# file name instead of whole path.
+	libgcc = run_command(cc_arch + '-helenos-gcc',
+		arch_uspace_c_args, '-print-libgcc-file-name',
+		check: true,
+	).stdout().strip()
+
+	install_files += [[ 'lib', libgcc, 'libgcc.a' ]]
+	install_deps += [ files(libgcc) ]
+endif
+
+# Emit the install script.
+
+install_script_text = []
+
+# Copy uspace/dist.
+_uspace = meson.current_source_dir() / 'uspace'
+install_script_text += 'cp -r -L -T -u "@0@/dist" "${DESTDIR}"'.format(_uspace)
+
+# Copy uspace/overlay
+install_script_text += 'if ls @0@/overlay/* >/dev/null 2>/dev/null; then'.format(_uspace)
+install_script_text += 'cp -r -L @0@/overlay/* "${DESTDIR}"'.format(_uspace)
+install_script_text += 'fi'
+
+
+foreach f : install_files
+	_cmd = 'mkdir -p "${DESTDIR}@0@" && cp -L -T "@1@" "${DESTDIR}@0@/@2@"'
+	install_script_text += _cmd.format(f[0], f[1], f[2])
+endforeach
+
+install_script_text += uspace_lib_install_script_text
+
+install_script = configure_file(
+	configuration: { 'text' : '\n'.join(install_script_text) },
+	input: 'install.sh.in',
+	output: 'install.sh',
+)
+
+# Build up dist
+
+dist_dir = meson.current_build_dir()/'dist/'
+
+dist = custom_target('DIST',
+	output: 'dist.tag',
+	input: [ install_script, install_deps ],
+	command: [ sh, '@INPUT0@', '@OUTPUT@', dist_dir ],
+)
+
+# Build initrd image
+
+if RDFMT == 'tmpfs'
+	initrd_cmd = [ 'tar', '-c', '-f', '@OUTPUT@', '-C', dist_dir, '.' ]
+elif RDFMT == 'fat'
+	initrd_cmd = [ mkfat, '1048576', dist_dir, '@OUTPUT@' ]
+elif RDFMT == 'ext4fs'
+	initrd_cmd = [ mkext4, '1048576', dist_dir, '@OUTPUT@' ]
+else
+	error('Unknown RDFMT: ' + RDFMT)
+endif
+
+initrd_img = custom_target('initrd.img',
+	output: 'initrd.img',
+	input: dist,
+	command: initrd_cmd,
+)
+
+rd_init_binaries += [[ initrd_img, 'boot/initrd.img' ]]
Index: meson/part/initrd_manifest/meson.build
===================================================================
--- meson/part/initrd_manifest/meson.build	(revision 200b2113fc4b438d75d53ae122f1828de1728eb8)
+++ meson/part/initrd_manifest/meson.build	(revision 200b2113fc4b438d75d53ae122f1828de1728eb8)
@@ -0,0 +1,39 @@
+# Init binaries. These are actually baked into the bootloader.
+rd_init = [
+	# IMPORTANT: The order of entries is important for bootloader!
+	'srv/ns',
+	'srv/loader',
+	'app/init',
+	'srv/locsrv',
+	'srv/bd/rd',
+	'srv/vfs',
+	'srv/logger',
+	'srv/fs/' + RDFMT,
+]
+
+# Binaries allowed on the initrd image when CONFIG_BAREBONE is enabled.
+rd_essential = [
+	'app/bdsh',
+	'app/getterm',
+	'app/kio',
+
+	'srv/devman',
+	'srv/fs/locfs',
+	'srv/hid/console',
+	'srv/hid/input',
+	'srv/hid/output',
+	'srv/klog',
+
+	'drv/root/root',
+	'drv/root/virt',
+	'drv/fb/kfb',
+]
+
+if CONFIG_FB
+	rd_essential += [
+		'app/vlaunch',
+		'app/vterm',
+
+		'srv/hid/compositor',
+	]
+endif
Index: meson/part/read_config/meson.build
===================================================================
--- meson/part/read_config/meson.build	(revision 200b2113fc4b438d75d53ae122f1828de1728eb8)
+++ meson/part/read_config/meson.build	(revision 200b2113fc4b438d75d53ae122f1828de1728eb8)
@@ -0,0 +1,121 @@
+# Read variables from config file generated by config.py,
+# and version information from repository.
+
+_config_file = files(meson.build_root() / 'Makefile.config')
+_version_file = files(meson.source_root() / 'version')
+
+_config_variables = [
+	# Uspace and kernel
+	'CONFIG_BAREBONE',
+	'CONFIG_BUILD_SHARED_LIBS',
+	'CONFIG_DEBUG',
+	'CONFIG_DEVEL_FILES',
+	'CONFIG_FPU',
+	'CONFIG_LINE_DEBUG',
+	'CONFIG_LTO',
+	'CONFIG_PCUT_SELF_TESTS',
+	'CONFIG_PCUT_TESTS',
+	'CONFIG_RTLD',
+	'CONFIG_STRIP_BINARIES',
+	'CONFIG_UBSAN',
+	'CONFIG_USE_SHARED_LIBS',
+	'CROSS_TARGET',
+	'OPTIMIZATION',
+	'PROCESSOR',
+	'QUADFLOAT',
+	'RDFMT',
+
+	# Kernel only
+	'CONFIG_ACPI',
+	'CONFIG_AM335X_TIMERS',
+	'CONFIG_ASID',
+	'CONFIG_ASID_FIFO',
+	'CONFIG_AT_KBD',
+	'CONFIG_BCM2835_MAILBOX',
+	'CONFIG_DSRLNIN',
+	'CONFIG_DSRLNOUT',
+	'CONFIG_EGA',
+	'CONFIG_FB',
+	'CONFIG_GICV2',
+	'CONFIG_I8042',
+	'CONFIG_I8259',
+	'CONFIG_IOMAP_BITMAP',
+	'CONFIG_IOMAP_DUMMY',
+	'CONFIG_KCONSOLE',
+	'CONFIG_MAC_KBD',
+	'CONFIG_MULTIBOOT',
+	'CONFIG_NS16550',
+	'CONFIG_OFW_PCI',
+	'CONFIG_OFW_TREE',
+	'CONFIG_OMAP_UART',
+	'CONFIG_PAGE_HT',
+	'CONFIG_PAGE_PT',
+	'CONFIG_PC_KBD',
+	'CONFIG_PL011_UART',
+	'CONFIG_PL050',
+	'CONFIG_S3C24XX_IRQC',
+	'CONFIG_S3C24XX_UART',
+	'CONFIG_SMP',
+	'CONFIG_SOFTINT',
+	'CONFIG_SRLN',
+	'CONFIG_SUN_KBD',
+	'CONFIG_SYMTAB',
+	'CONFIG_TEST',
+	'CONFIG_TRACE',
+	'CONFIG_TSB',
+	'CONFIG_UDEBUG',
+	'CONFIG_VIA_CUDA',
+	'MACHINE',
+	'MEMORY_MODEL',
+
+	'UARCH',
+	'KARCH',
+	'BARCH',
+	'GRUB_ARCH',
+	'UIMAGE_OS',
+	'CONFIG_COMPRESSED_INIT',
+]
+
+foreach _varname : _config_variables
+	_c = run_command(grep, '^' + _varname + '\\b', _config_file)
+	if _c.returncode() != 0
+		# TODO: Output negative/inapplicable variables too in config, so that we can check for typos here.
+		#warning('Missing configuration variable ' + _varname + ' in Makefile.config')
+		set_variable(_varname, false)
+		continue
+	endif
+
+	_val = _c.stdout().split('\n')[0].strip().split('=')[1].strip()
+	if _val == 'y'
+		_val = true
+	elif _val == 'n'
+		_val = false
+	endif
+
+	set_variable(_varname, _val)
+	if debug_options
+		message([ _varname, get_variable(_varname) ])
+	endif
+endforeach
+
+# Also read version information.
+_version_variables = [
+	'COPYRIGHT',
+	'NAME',
+	'PATCHLEVEL',
+	'SUBLEVEL',
+	'VERSION',
+]
+
+foreach _varname : _version_variables
+	_line = run_command(grep, '^' + _varname + '\\b', _version_file, check: true).stdout().strip()
+	_val = _line.split('=')[1].strip()
+
+	_realvarname = 'HELENOS_' + _varname
+	set_variable(_realvarname, _val)
+	if debug_options
+		message([ _realvarname, get_variable(_realvarname) ])
+	endif
+endforeach
+
+HELENOS_RELEASE = HELENOS_VERSION + '.' + HELENOS_PATCHLEVEL + '.' + HELENOS_SUBLEVEL
Index: meson/part/tools/meson.build
===================================================================
--- meson/part/tools/meson.build	(revision 200b2113fc4b438d75d53ae122f1828de1728eb8)
+++ meson/part/tools/meson.build	(revision 200b2113fc4b438d75d53ae122f1828de1728eb8)
@@ -0,0 +1,79 @@
+# Find all the programs and tools used by meson.
+# Whenever you use an external command anywhere,
+# first find and assign it to a variable here. Don't use its name directly.
+# This way it's obvious what programs are used, and for some we can use
+# the same argument list everywhere.
+
+_tools_dir = meson.source_root() / 'tools'
+
+cc = meson.get_compiler('c')
+basename = find_program('basename')
+cp = find_program('cp')
+dirname = find_program('dirname')
+find = find_program('find')
+grep = find_program('grep')
+mkarray = find_program(_tools_dir / 'mkarray_for_meson.sh')
+mkext4 = find_program(_tools_dir / 'mkext4.py')
+mkfat = find_program(_tools_dir / 'mkfat.py')
+mkuimage = find_program(_tools_dir / 'mkuimage.py')
+config_py = find_program(_tools_dir / 'config.py')
+objcopy = find_program('objcopy')
+objdump = find_program('objdump')
+sed = find_program('sed')
+unzip = find_program('unzip')
+which = find_program('which')
+
+sh = [ find_program('sh'), '-u', '-e' ]
+if debug_shell_scripts
+	sh += '-x'
+endif
+
+genisoimage = find_program('genisoimage', required: false)
+
+if not genisoimage.found()
+	genisoimage = find_program('mkisofs', required: false)
+endif
+
+if not genisoimage.found()
+	xorriso = find_program('xorriso', required: false)
+
+	if xorriso.found()
+		genisoimage = [ xorriso, '-as', 'genisoimage' ]
+	else
+		error('Need genisoimage, mkisofs or xorriso.')
+	endif
+endif
+
+
+autocheck = generator(find_program(_tools_dir / 'autocheck.awk'),
+	arguments: [ '@INPUT@' ],
+	output: '@PLAINNAME@.check.c',
+	capture: true,
+)
+
+# TODO: bug in Meson
+#gzip = generator(find_program('gzip'),
+#	arguments: [ '--no-name', '-9', '--stdout', '@INPUT@' ],
+#	output: '@PLAINNAME@.gz',
+#	capture: true,
+#)
+
+gzip = [ find_program('gzip'), '--no-name', '-9', '--stdout', '@INPUT@' ]
+
+# Tar's arguments make sure that the archive is reproducible.
+tar = [
+	find_program('tar'),
+	'-c',
+	'-f', '@OUTPUT@',
+	'--mtime=1970-01-01 00:00:00Z',
+	'--group=0',
+	'--owner=0',
+	'--numeric-owner',
+	'--mode=go=rX,u+rw,a-s',
+	'--no-acls',
+	'--no-selinux',
+	'--no-xattrs',
+	'--format=ustar',
+	'--transform', 's:@OUTDIR@/::',
+	'@INPUT@',
+]
