Index: Makefile
===================================================================
--- Makefile	(revision 14d789c5271b9fb5414610b8824d027be16e3478)
+++ Makefile	(revision 2660ee33905b12512a15891b19f47af71a4770f1)
@@ -48,5 +48,5 @@
 ERRNO_INPUT = abi/include/abi/errno.in
 
-.PHONY: all precheck cscope cscope_parts autotool config_auto config_default config distclean clean check releasefile release common boot kernel uspace
+.PHONY: all precheck cscope cscope_parts autotool config_auto config_default config distclean clean check releasefile release common boot kernel uspace export-posix
 
 all: kernel uspace
@@ -60,4 +60,11 @@
 uspace: common
 	$(MAKE) -r -C uspace PRECHECK=$(PRECHECK)
+
+export-posix: common
+ifndef EXPORT_DIR
+	@echo ERROR: Variable EXPORT_DIR is not defined. && false
+else
+	$(MAKE) -r -C uspace export EXPORT_DIR=$(abspath $(EXPORT_DIR))
+endif
 
 precheck: clean
Index: tools/autotool.py
===================================================================
--- tools/autotool.py	(revision 14d789c5271b9fb5414610b8824d027be16e3478)
+++ tools/autotool.py	(revision 2660ee33905b12512a15891b19f47af71a4770f1)
@@ -615,4 +615,5 @@
 				path = "%s/%s/bin" % (cross_prefix, platform)
 		
+		common['TARGET'] = target
 		prefix = "%s-" % target
 		
Index: uspace/Makefile
===================================================================
--- uspace/Makefile	(revision 14d789c5271b9fb5414610b8824d027be16e3478)
+++ uspace/Makefile	(revision 2660ee33905b12512a15891b19f47af71a4770f1)
@@ -255,5 +255,5 @@
 CLEANS := $(addsuffix .clean,$(DIRS) $(LIBS) $(BASE_LIBS))
 
-.PHONY: all $(BASE_BUILDS) $(BUILDS) $(BUILDS_TESTS) $(CLEANS) clean
+.PHONY: all $(BASE_BUILDS) $(BUILDS) $(BUILDS_TESTS) $(CLEANS) clean export
 
 all: $(BUILDS) $(BUILDS_TESTS)
@@ -261,4 +261,7 @@
 $(BUILDS_TESTS): $(BASE_BUILDS) $(BUILDS)
 	$(MAKE) -r -C $(basename $@) all-test PRECHECK=$(PRECHECK)
+
+export: lib/posix.build lib/math.build lib/clui.build
+	$(MAKE) -r -C lib/posix export EXPORT_DIR=$(EXPORT_DIR)
 
 clean: $(CLEANS)
Index: uspace/Makefile.common
===================================================================
--- uspace/Makefile.common	(revision 14d789c5271b9fb5414610b8824d027be16e3478)
+++ uspace/Makefile.common	(revision 2660ee33905b12512a15891b19f47af71a4770f1)
@@ -174,29 +174,55 @@
 endif
 
-COMMON_CFLAGS = $(INCLUDES_FLAGS) -O$(OPTIMIZATION) -imacros $(CONFIG_HEADER) \
-	-ffreestanding -nostdlib -nostdinc -fexec-charset=UTF-8 \
-	-finput-charset=UTF-8 -D__$(ENDIANESS)__ -D_HELENOS_SOURCE -fno-common \
+# Flags that are not necessary, and can be overriden, but are used by default.
+DEFAULT_CFLAGS = \
+	-O$(OPTIMIZATION) \
+	-ffunction-sections \
+	-pipe \
+	-Wall \
+	-Wextra \
+	-Wno-unused-parameter \
+	-Wmissing-prototypes \
+	-Wwrite-strings \
+	-Werror-implicit-function-declaration
+
+ifeq ($(CONFIG_DEBUG),y)
+	DEFAULT_CFLAGS += -Werror
+endif
+
+ifeq ($(COMPILER),clang)
+	DEFAULT_CFLAGS += \
+		-Wno-missing-field-initializers \
+		-Wno-typedef-redefinition \
+		-Wno-unused-command-line-argument
+else
+	DEFAULT_CFLAGS += \
+		-Wno-clobbered
+endif
+
+ifeq ($(CONFIG_LINE_DEBUG),y)
+	DEFAULT_CFLAGS += -ggdb
+endif
+
+# Flags that should always be used, even for third-party software.
+COMMON_CFLAGS = \
+	-ffreestanding \
+	-nostdlib \
+	-nostdinc \
+	-D__$(ENDIANESS)__
+
+# Flags that are always used for HelenOS code, but not for third-party.
+HELENOS_CFLAGS = \
+	-std=gnu99 \
+	$(INCLUDES_FLAGS) \
+	-imacros $(CONFIG_HEADER) \
+	-D_HELENOS_SOURCE \
+	-fexec-charset=UTF-8 \
+	-finput-charset=UTF-8 \
+	-fno-common \
 	-fdebug-prefix-map=$(realpath $(ROOT_PATH))=.
 
-GCC_CFLAGS = -ffunction-sections -Wall -Wextra -Wno-clobbered \
-	-Wno-unused-parameter -Wmissing-prototypes -std=gnu99 \
-	-Werror-implicit-function-declaration \
-	-Wwrite-strings -pipe
-
-# -Wno-missing-prototypes is there because it warns about main().
-# This should be fixed elsewhere.
-CLANG_CFLAGS = -Wall -Wextra -Wno-unused-parameter -Wno-missing-field-initializers -Wno-typedef-redefinition \
-	-Wno-missing-prototypes -Wno-unused-command-line-argument \
-	-std=gnu99 -Werror-implicit-function-declaration -Wwrite-strings \
-	-pipe -fno-stack-protector -fno-PIC
-
-ifeq ($(CONFIG_DEBUG),y)
-	COMMON_CFLAGS += -Werror
-endif
-
-ifeq ($(CONFIG_LINE_DEBUG),y)
-	GCC_CFLAGS += -ggdb
-	CLANG_CFLAGS += -g
-endif
+# TODO: Use a different name.
+# CFLAGS variable is traditionally used for overridable flags.
+CFLAGS = $(COMMON_CFLAGS) $(HELENOS_CFLAGS) $(DEFAULT_CFLAGS)
 
 ## Setup platform configuration
@@ -216,10 +242,4 @@
 endif
 
-ifeq ($(COMPILER),clang)
-	CFLAGS += $(COMMON_CFLAGS) $(CLANG_CFLAGS)
-else
-	CFLAGS += $(COMMON_CFLAGS) $(GCC_CFLAGS)
-endif
-
 ifeq ($(CONFIG_STRIP_BINARIES),y)
 	LFLAGS += --strip-all
@@ -246,6 +266,5 @@
 all-test: $(TEST_OUTPUTS)
 
-clean:
-	rm -f $(JOB) $(OUTPUTS) $(EXTRA_CLEAN) tag deps.mk
+clean: fasterclean
 	find . -name '*.o' -follow -exec rm \{\} \;
 	find . -name '*.lo' -follow -exec rm \{\} \;
@@ -253,5 +272,5 @@
 
 fasterclean:
-	rm -f $(JOB) $(OUTPUTS) $(EXTRA_CLEAN) tag deps.mk
+	rm -rf $(JOB) $(OUTPUTS) $(EXTRA_CLEAN) tag deps.mk
 
 depend: $(PRE_DEPEND)
Index: uspace/lib/c/arch/amd64/Makefile.common
===================================================================
--- uspace/lib/c/arch/amd64/Makefile.common	(revision 14d789c5271b9fb5414610b8824d027be16e3478)
+++ uspace/lib/c/arch/amd64/Makefile.common	(revision 2660ee33905b12512a15891b19f47af71a4770f1)
@@ -31,5 +31,8 @@
 # XXX: clang doesn't support this flag, but the optimization is OS-specific,
 #      so it isn't used for amd64-unknown-elf target.
-GCC_CFLAGS += -mno-tls-direct-seg-refs
+
+ifneq ($(COMPILER),clang)
+	COMMON_CFLAGS += -mno-tls-direct-seg-refs
+endif
 
 LFLAGS += --gc-sections
Index: uspace/lib/posix/Makefile
===================================================================
--- uspace/lib/posix/Makefile	(revision 14d789c5271b9fb5414610b8824d027be16e3478)
+++ uspace/lib/posix/Makefile	(revision 2660ee33905b12512a15891b19f47af71a4770f1)
@@ -45,4 +45,18 @@
 	$(LIBSOFTINT_PREFIX)/libsoftint.a
 
+SPECS = gcc.specs
+LIBC_LINKER_SCRIPT = $(LIBC_PREFIX)/arch/$(UARCH)/_link.ld
+LIBC_STARTUP_FILE = $(shell sed -n -e 's/^.*STARTUP(\(.*\)).*$$/\1/p' $(LIBC_LINKER_SCRIPT))
+EXPORT_LINKER_SCRIPT = link.ld
+EXPORT_STARTUP_FILE = crt0.o
+
+EXPORT_FILES = \
+	../math/libmath.a \
+	../clui/libclui.a \
+	$(MERGED_C_LIBRARY) \
+	$(EXPORT_STARTUP_FILE) \
+	$(EXPORT_LINKER_SCRIPT) \
+	$(SPECS)
+
 REDEFS_HIDE_LIBC = redefs-hide-libc-symbols.list
 
@@ -53,5 +67,5 @@
 	libc.o
 
-EXTRA_OUTPUT = $(FIXED_C_LIBRARY) $(FIXED_POSIX_LIBRARY) $(MERGED_C_LIBRARY)
+EXTRA_OUTPUT = $(FIXED_C_LIBRARY) $(FIXED_POSIX_LIBRARY) $(MERGED_C_LIBRARY) $(SPECS) $(EXPORT_LINKER_SCRIPT) $(EXPORT_STARTUP_FILE)
 
 SOURCES = \
@@ -83,8 +97,80 @@
 	test/scanf.c
 
+EXPORT_CPPFLAGS = \
+	-specs $$(HELENOS_EXPORT_ROOT)/lib/gcc.specs \
+	-isystem $$(HELENOS_EXPORT_ROOT)/include
+
+EXPORT_LDFLAGS = \
+	-L$$(HELENOS_EXPORT_ROOT)/lib \
+	--whole-archive -lc -lmath --no-whole-archive \
+	-T link.ld
+
+EXPORT_CFLAGS = \
+	$(EXPORT_CPPFLAGS) \
+	$(addprefix -Xlinker , $(EXPORT_LDFLAGS))
+
 include $(USPACE_PREFIX)/Makefile.common
 
-$(INCLUDE_LIBC): ../c/include
-	ln -s -f -n ../$^ $@
+$(SPECS): $(CONFIG_MAKEFILE)
+	echo '*self_spec:' > $@.new
+	echo '+ $(COMMON_CFLAGS)' >> $@.new
+	echo >> $@.new
+	echo '*lib:' >> $@.new
+	echo '--whole-archive -lc -lmath --no-whole-archive' >> $@.new
+	echo >> $@.new
+	mv $@.new $@
+
+$(EXPORT_LINKER_SCRIPT): $(LIBC_LINKER_SCRIPT)
+	sed 's/STARTUP(.*)/STARTUP(crt0.o)/' $< > $@
+
+$(EXPORT_STARTUP_FILE): $(LIBC_STARTUP_FILE)
+	cp $< $@
+
+$(INCLUDE_LIBC): $(shell find ../c/include -name '*.h')
+	cp -r -L --remove-destination -T ../c/include $@
+	find ../c/include -type f -and -not -name '*.h' -delete
+
+export: $(EXPORT_DIR)/config.mk $(EXPORT_DIR)/config.rc
+
+$(EXPORT_DIR)/config.mk: export-libs export-includes
+	echo '# Generated file, do not modify.' >> $@.new
+	echo '# Do not forget to set HELENOS_EXPORT_ROOT.' >> $@.new
+	echo 'HELENOS_CROSS_PATH="$(shell dirname $(CC))"' >> $@.new
+	echo 'HELENOS_TARGET="$(TARGET)"' >> $@.new
+	echo 'HELENOS_CPPFLAGS="$(EXPORT_CPPFLAGS)"' >> $@.new
+	echo 'HELENOS_CFLAGS="$(EXPORT_CFLAGS)"' >> $@.new
+	echo 'HELENOS_LDFLAGS="$(EXPORT_LDFLAGS)"' >> $@.new
+	mv $@.new $@
+
+$(EXPORT_DIR)/config.rc: $(EXPORT_DIR)/config.mk
+	sed 's:$$(HELENOS_EXPORT_ROOT):$$HELENOS_EXPORT_ROOT:g' < $< >$@
+
+export-libs: $(EXPORT_FILES) export-includes
+	mkdir -p $(EXPORT_DIR)/lib
+	cp -L $(EXPORT_FILES) $(EXPORT_DIR)/lib
+
+export-includes: $(INCLUDE_LIBC) $(shell find ./include ../c/arch/$(UARCH)/include $(ROOT_PATH)/abi/include -name '*.h')
+	mkdir -p $(EXPORT_DIR)/include
+	rm -rf $(EXPORT_DIR)/include.new
+	cp -r -L -T ./include/posix $(EXPORT_DIR)/include.new
+	cp -r -L -T ./include/libc $(EXPORT_DIR)/include.new/libc
+	cp -r -L ../c/arch/$(UARCH)/include/* $(EXPORT_DIR)/include.new/libc
+	cp -r -L $(ROOT_PATH)/abi/include/* $(EXPORT_DIR)/include.new
+	mkdir -p $(EXPORT_DIR)/include.new/libclui
+	cp -L ../clui/tinput.h $(EXPORT_DIR)/include.new/libclui
+	
+	find "$(EXPORT_DIR)/include.new/libc" "$(EXPORT_DIR)/include.new/libclui" -name '*.h' -exec sed \
+		-e 's:#include <:#include <libc/:' \
+		-e 's:#include <libc/abi/:#include <abi/:' \
+		-e 's:#include <libc/_bits/:#include <_bits/:' \
+		-e 's:#include <libc/libc/:#include <libc/:' \
+		-i {} \;
+	find "$(EXPORT_DIR)/include.new" -name '*.h' -exec sed \
+		-e 's:#include "posix/:#include ":' \
+		-e 's:#include <posix/:#include <:' \
+		-i {} \;
+	
+	rm -rf $(EXPORT_DIR)/include
+	mv $(EXPORT_DIR)/include.new $(EXPORT_DIR)/include
 
 $(FIXED_C_LIBRARY): $(LIBC_FILE) $(REDEFS_HIDE_LIBC)
@@ -97,3 +183,2 @@
 $(REDEFS_HIDE_LIBC): $(SOURCES)
 	sed -n -e 's/_HIDE_LIBC_SYMBOL(\(.*\))/\1 __helenos_libc_\1/p' $(SOURCES) >$@
-
