perf tools: Reorganize the Makefile feature tests

Moving the tests to a separate file, feature-tests.mak and using a try-cc
function similar to the try-run in Kbuild.

This also makes the output more quiet as we can stop using the INTERMEDIATE
target to remove the .perf.dev.null file needed for some gcc versions where
/dev/null can't be used as the output file name.

As the tests get shorter by uninlining the source code used to test for
features, we can more properly use identation.

The feature tests itself can be made more clear and reused, like when trying to
see what is needed to have bfd_demangle.

We also get a bit closer to reusing scripts/Kbuild.include, reducing the
distance from the kernel build system.

Tests performed:

[root@emilia perf]# make -j9 O=/tmp/perf
PERF_VERSION = 0.0.2.PERF
    GEN /tmp/perf/common-cmds.h
    * new build flags or prefix
    GEN perf-archive
    CC /tmp/perf/builtin-annotate.o
    CC /tmp/perf/bench/sched-messaging.o
    CC /tmp/perf/builtin-diff.o
<SNIP>
    CC /tmp/perf/scripts/python/Perf-Trace-Util/Context.o
    CC /tmp/perf/perf.o
    CC /tmp/perf/builtin-help.o
    AR /tmp/perf/libperf.a
    LINK /tmp/perf/perf
[root@emilia perf]#

If we uninstall, for instance newt-devel we get:

[root@emilia perf]# rpm -e newt-devel
[root@emilia perf]# make -j9 O=/tmp/perf
Makefile:564: newt not found, disables TUI support. Please install newt-devel or libnewt-dev
    * new build flags or prefix
    GEN perf-archive
    CC /tmp/perf/perf.o
    CC /tmp/perf/builtin-annotate.o
<SNIP>
    AR /tmp/perf/libperf.a
    LINK /tmp/perf/perf
[root@emilia perf]#

And then binutils-devel:

[root@emilia perf]# make -j9 O=/tmp/perf
Makefile:564: newt not found, disables TUI support. Please install newt-devel or libnewt-dev
Makefile:632: No bfd.h/libbfd found, install binutils-dev[el]/zlib-static to gain symbol demangling
    * new build flags or prefix
    GEN perf-archive
    CC /tmp/perf/perf.o
<SNIP>
    AR /tmp/perf/libperf.a
    LINK /tmp/perf/perf
[root@emilia perf]#

And then strictly required devel packages:

[root@emilia perf]# rpm -e elfutils-libelf-devel elfutils-devel
[root@emilia perf]# make -j9 O=/tmp/perf
Makefile:509: No libdw.h found or old libdw.h found or elfutils is older than 0.138, disables dwarf support. Please install new elfutils-devel/libdw-dev
Makefile:542: *** No libelf.h/libelf found, please install libelf-dev/elfutils-libelf-devel.  Stop.
[root@emilia perf]#

After installing everything back on:

[root@emilia perf]# yum install elfutils-devel binutils-devel newt-devel
<SNIP>
Installed:
  binutils-devel.x86_64 0:2.20.51.0.2-5.11.el6
  elfutils-devel.x86_64 0:0.147-1.el6
  elfutils-libelf-devel.x86_64 0:0.147-1.el6
  newt-devel.x86_64 0:0.52.11-1.el6

Complete!
[root@emilia perf]# make -j9
PERF_VERSION = 0.0.2.PERF
    GEN common-cmds.h
    * new build flags or prefix
    GEN perf-archive
    CC builtin-annotate.o
<SNIP>
    AR libperf.a
    LINK perf
[root@emilia perf]# make -j9
[root@emilia perf]#

Thanks to Sam for pointing me to try-run.

Cc: David S. Miller <davem@davemloft.net>
Cc: Frédéric Weisbecker <fweisbec@gmail.com>
Cc: Ingo Molnar <mingo@elte.hu>
Cc: Michal Marek <mmarek@suse.cz>
Cc: Mike Galbraith <efault@gmx.de>
Cc: Paul Mackerras <paulus@samba.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Sam Ravnborg <sam@ravnborg.org>
Cc: Stephane Eranian <eranian@google.com>
Cc: Tom Zanussi <tzanussi@gmail.com>
LKML-Reference: <new-submission>
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
diff --git a/tools/perf/feature-tests.mak b/tools/perf/feature-tests.mak
new file mode 100644
index 0000000..ddb68e6
--- /dev/null
+++ b/tools/perf/feature-tests.mak
@@ -0,0 +1,119 @@
+define SOURCE_HELLO
+#include <stdio.h>
+int main(void)
+{
+	return puts(\"hi\");
+}
+endef
+
+ifndef NO_DWARF
+define SOURCE_DWARF
+#include <dwarf.h>
+#include <libdw.h>
+#include <version.h>
+#ifndef _ELFUTILS_PREREQ
+#error
+#endif
+
+int main(void)
+{
+	Dwarf *dbg = dwarf_begin(0, DWARF_C_READ);
+	return (long)dbg;
+}
+endef
+endif
+
+define SOURCE_LIBELF
+#include <libelf.h>
+
+int main(void)
+{
+	Elf *elf = elf_begin(0, ELF_C_READ, 0);
+	return (long)elf;
+}
+endef
+
+define SOURCE_GLIBC
+#include <gnu/libc-version.h>
+
+int main(void)
+{
+	const char *version = gnu_get_libc_version();
+	return (long)version;
+}
+endef
+
+define SOURCE_ELF_MMAP
+#include <libelf.h>
+int main(void)
+{
+	Elf *elf = elf_begin(0, ELF_C_READ_MMAP, 0);
+	return (long)elf;
+}
+endef
+
+ifndef NO_NEWT
+define SOURCE_NEWT
+#include <newt.h>
+
+int main(void)
+{
+	newtInit();
+	newtCls();
+	return newtFinished();
+}
+endef
+endif
+
+ifndef NO_LIBPERL
+define SOURCE_PERL_EMBED
+#include <EXTERN.h>
+#include <perl.h>
+
+int main(void)
+{
+perl_alloc();
+return 0;
+}
+endef
+endif
+
+ifndef NO_LIBPYTHON
+define SOURCE_PYTHON_EMBED
+#include <Python.h>
+
+int main(void)
+{
+	Py_Initialize();
+	return 0;
+}
+endef
+endif
+
+define SOURCE_BFD
+#include <bfd.h>
+
+int main(void)
+{
+	bfd_demangle(0, 0, 0);
+	return 0;
+}
+endef
+
+define SOURCE_CPLUS_DEMANGLE
+extern char *cplus_demangle(const char *, int);
+
+int main(void)
+{
+	cplus_demangle(0, 0);
+	return 0;
+}
+endef
+
+# try-cc
+# Usage: option = $(call try-cc, source-to-build, cc-options)
+try-cc = $(shell sh -c						  \
+	'TMP="$(TMPOUT).$$$$";			 		  \
+	 echo "$(1)" |						  \
+	 $(CC) -x c - $(2) -o "$$TMP" > /dev/null 2>&1 && echo y; \
+	 rm -f "$$TMP"')