build: split app/test pimgen invocation

Run pimgen twice, once for the application with the real YAML
and once for the testcase with the example YAML so that the
testcase can run and build sucessfully when an alternate YAML
tree is provided.

Move extra interface rule generation from pimgen to a makefile
generating script.

Change-Id: Ib0c01bd2207007b815019232a466fa16889f2005
Signed-off-by: Brad Bishop <bradleyb@fuzziesquirrel.com>
diff --git a/.gitignore b/.gitignore
index 80bf698..b0baa5d 100644
--- a/.gitignore
+++ b/.gitignore
@@ -4,10 +4,8 @@
 *.sw*
 phosphor-inventory
 generated.cpp
-xyz.openbmc_project.Example.Iface1.cpp
-xyz.openbmc_project.Example.Iface2.cpp
-xyz/openbmc_project/Example/Iface1/server.hpp
-xyz/openbmc_project/Example/Iface2/server.hpp
+xyz.openbmc_project.Example.*.cpp
+xyz/openbmc_project/Example/
 Makefile
 Makefile.in
 configure
@@ -29,3 +27,5 @@
 ltmain.sh
 missing
 stamp-h1
+Makefile.extra
+extra_ifaces.cpp
diff --git a/Makefile.am b/Makefile.am
index fb4ecb2..1673362 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -1,34 +1,33 @@
-EXTRAIFACES_C=@EXTRAIFACES_C@
-EXTRAIFACES_LO=@EXTRAIFACES_LO@
-BUILT_SOURCES=generated.cpp
+BUILT_SOURCES=generated.cpp extra_ifaces.cpp
 
-CLEANFILES=$(BUILT_SOURCES) $(EXTRAIFACES_C) libextra.la
+CLEANFILES=$(BUILT_SOURCES)
 
 sbin_PROGRAMS = phosphor-inventory
+noinst_LTLIBRARIES = libmanagercommon.la libmanager.la
+
+extra_yamldir=$(YAML_PATH)/extra_interfaces.d
+
 phosphor_inventory_SOURCES = app.cpp
 phosphor_inventory_LDADD = libmanager.la
-phosphor_inventory_LDFLAGS = $(SYSTEMD_LIBS)
-phosphor_inventory_CFLAGS = $(SYSTEMD_CFLAGS)
+phosphor_inventory_LDFLAGS = $(SDBUSPLUS_LIBS)
+phosphor_inventory_CFLAGS = $(SDBUSPLUS_CFLAGS)
 
-noinst_LTLIBRARIES = libmanager.la
-libmanager_la_SOURCES = \
+libmanagercommon_la_SOURCES = \
 	xyz.openbmc_project.Inventory.Manager.cpp \
-	generated.cpp \
 	events.cpp \
 	manager.cpp
-libmanager_la_LIBADD = libextra.la $(SDBUSPLUS_LIBS)
+libmanagercommon_la_LIBADD = $(SDBUSPLUS_LIBS)
 
-# Automake does not allow autoconf substituted variables
-# in _SOURCES variables.  Work around this by providing
-# our own target.
-#
-# Typically the recommendation is conditional compilation
-# but here the generated source files are completely
-# arbitrary and not known making that approach difficult.
-libextra.la: $(EXTRAIFACES_LO)
-	$(AM_V_CXXLD)$(CXXLINK) $(EXTRAIFACES_LO)
+libmanager_la_SOURCES = \
+	extra_ifaces.cpp \
+	generated.cpp
+libmanager_la_LIBADD = libmanagercommon.la $(SDBUSPLUS_LIBS)
 
-$(EXTRAIFACES_C) generated.cpp:
-	$(AM_V_GEN)@PIMGEN@ -o $(top_builddir) generate-cpp
+clean-local: clean-extra
+
+generated.cpp:
+	$(AM_V_GEN)$(PYTHON) $(top_srcdir)/pimgen.py -d $(extra_yamldir)/.. -o $(builddir) generate-cpp
 
 SUBDIRS = . test
+
+-include Makefile.extra
diff --git a/Makefile.extra.in b/Makefile.extra.in
new file mode 100644
index 0000000..128a178
--- /dev/null
+++ b/Makefile.extra.in
@@ -0,0 +1 @@
+# Empty file so that 'configure' attempts to generate Makefile.extra.
diff --git a/configure.ac b/configure.ac
index 8101911..e9b30fe 100644
--- a/configure.ac
+++ b/configure.ac
@@ -12,6 +12,9 @@
 AC_PROG_INSTALL
 AC_PROG_MAKE_SET
 AC_PROG_AWK
+AC_CHECK_PROG([FIND], find, find)
+AC_CHECK_PROG([BASENAME], basename, basename)
+AC_CHECK_PROG([DIRNAME], dirname, dirname)
 AM_PATH_PYTHON([2.7],
     [AC_SUBST([PYTHON], [echo "$PYTHON"])],
     [AC_MSG_ERROR([Could not find python-2.7 installed...python-2.7 is required])])
@@ -53,6 +56,10 @@
     AC_SUBST([OESDK_TESTCASE_FLAGS], [$testcase_flags])
 )
 
+AC_PATH_PROG([SDBUSPLUSPLUS], [sdbus++])
+AS_IF([test "x$SDBUSPLUSPLUS" == "x"],
+    AC_MSG_ERROR([Cannot find sdbus++]))
+
 AC_ARG_VAR(BUSNAME, [The DBus busname to own.])
 AC_ARG_VAR(INVENTORY_ROOT, [The DBus inventory namespace root.])
 AC_ARG_VAR(IFACE, [The manager DBus interface.])
@@ -64,13 +71,13 @@
 AC_DEFINE_UNQUOTED([BUSNAME], ["$BUSNAME"], [The DBus busname to own.])
 AC_DEFINE_UNQUOTED([INVENTORY_ROOT], ["$INVENTORY_ROOT"], [The DBus inventory namespace root.])
 AC_DEFINE_UNQUOTED([IFACE], ["$IFACE"], [The manager DBus interface.])
-PIMGEN="$PYTHON $srcdir/pimgen.py -d $YAML_PATH"
-EXTRAIFACES_LO="`$PIMGEN list-interfaces|awk '{ for(i=1;i<=NF;i++){printf $i".lo ";}}'`"
-EXTRAIFACES_C="`$PIMGEN list-interfaces|awk '{ for(i=1;i<=NF;i++){printf $i".cpp ";}}'`"
-AC_SUBST(EXTRAIFACES_LO)
-AC_SUBST(EXTRAIFACES_C)
-AC_SUBST(PIMGEN)
 
 # Create configured output
+AC_CONFIG_FILES([Makefile.extra],
+    [${srcdir}/generate_makefile.sh $yaml > Makefile.extra],
+    [yaml=${YAML_PATH}/extra_interfaces.d])
+AC_CONFIG_FILES([test/Makefile.extra],
+    [${srcdir}/generate_makefile.sh $test_yaml > test/Makefile.extra],
+    [test_yaml=$srcdir/example/extra_interfaces.d])
 AC_CONFIG_FILES([Makefile test/Makefile])
 AC_OUTPUT
diff --git a/generate_makefile.sh b/generate_makefile.sh
new file mode 100755
index 0000000..17236a7
--- /dev/null
+++ b/generate_makefile.sh
@@ -0,0 +1,61 @@
+#!/bin/sh
+
+yaml_top=$1
+toplevel_dirs=xyz
+interfaces=
+
+if [ -d $yaml_top/$toplevel_dirs ]; then
+    cd $yaml_top
+    interfaces=`find $toplevel_dirs -name "*.interface.yaml"`
+fi
+
+for i in ${interfaces};
+do
+    iface_path=`dirname $i`/`basename $i .interface.yaml`
+    iface=`echo $iface_path | sed 's/\//./g'`
+    cat <<MAKEFILE
+${i%.interface.yaml}/server.cpp: \$(extra_yamldir)/${i} ${i%.interface.yaml}/server.hpp
+	@mkdir -p \`dirname \$@\`
+	\$(AM_V_GEN)\$(SDBUSPLUSPLUS) -r \$(extra_yamldir) interface server-cpp ${iface} > \$@
+
+${i%.interface.yaml}/server.hpp: \$(extra_yamldir)/${i}
+	@mkdir -p \`dirname \$@\`
+	\$(AM_V_GEN)\$(SDBUSPLUSPLUS) -r \$(extra_yamldir) interface server-header ${iface} > \$@
+
+MAKEFILE
+
+done
+
+echo "extra_ifaces_cpp_SOURCES = \\"
+for i in ${interfaces};
+do
+    echo "	${i%.interface.yaml}/server.cpp \\"
+done
+echo
+
+echo "extra_ifaces_hpp_SOURCES = \\"
+for i in ${interfaces};
+do
+    echo "	${i%.interface.yaml}/server.hpp \\"
+done
+echo
+
+echo "extra_ifaces.cpp: \$(extra_ifaces_cpp_SOURCES)"
+if [ "$interfaces" ]; then
+    echo "	\$(AM_V_GEN)cat \$^ > \$@"
+else
+    echo "	\$(AM_V_GEN)touch \$@"
+fi
+
+cat << MAKEFILE
+
+.PHONY: clean-extra
+clean-extra:
+	for i in \$(extra_ifaces_cpp_SOURCES) \\
+	         \$(extra_ifaces_hpp_SOURCES); \\
+	do \\
+	    test -e \$\$i && rm \$\$i ; \\
+	    test -d \`dirname \$\$i\` && rmdir -p \`dirname \$\$i\` ; \\
+	    true; \\
+	done
+MAKEFILE
diff --git a/pimgen.py b/pimgen.py
index ce43c46..4fd6fcc 100755
--- a/pimgen.py
+++ b/pimgen.py
@@ -431,61 +431,21 @@
 
     @staticmethod
     def load(args):
-        # Invoke sdbus++ to generate any extra interface bindings for
-        # extra interfaces that aren't defined externally.
-        yaml_files = []
-        extra_ifaces_dir = os.path.join(args.inputdir, 'extra_interfaces.d')
-        if os.path.exists(extra_ifaces_dir):
-            for directory, _, files in os.walk(extra_ifaces_dir):
-                if not files:
-                    continue
-
-                yaml_files += map(
-                    lambda f: os.path.relpath(
-                        os.path.join(directory, f),
-                        extra_ifaces_dir),
-                    filter(lambda f: f.endswith('.interface.yaml'), files))
-
-        genfiles = {
-            'server-cpp': lambda x: '%s.cpp' % (
-                x.replace(os.sep, '.')),
-            'server-header': lambda x: os.path.join(
-                os.path.join(
-                    *x.split('.')), 'server.hpp')
-        }
-
-        for i in yaml_files:
-            iface = i.replace('.interface.yaml', '').replace(os.sep, '.')
-            for process, f in genfiles.iteritems():
-
-                dest = os.path.join(args.outputdir, f(iface))
-                parent = os.path.dirname(dest)
-                if parent and not os.path.exists(parent):
-                    os.makedirs(parent)
-
-                with open(dest, 'w') as fd:
-                    subprocess.call([
-                        'sdbus++',
-                        '-r',
-                        extra_ifaces_dir,
-                        'interface',
-                        process,
-                        iface],
-                        stdout=fd)
-
         # Aggregate all the event YAML in the events.d directory
         # into a single list of events.
 
-        events_dir = os.path.join(args.inputdir, 'events.d')
-        yaml_files = filter(
-            lambda x: x.endswith('.yaml'),
-            os.listdir(events_dir))
-
         events = []
-        for x in yaml_files:
-            with open(os.path.join(events_dir, x), 'r') as fd:
-                for e in yaml.safe_load(fd.read()).get('events', {}):
-                    events.append(e)
+        events_dir = os.path.join(args.inputdir, 'events.d')
+
+        if os.path.exists(events_dir):
+            yaml_files = filter(
+                lambda x: x.endswith('.yaml'),
+                os.listdir(events_dir))
+
+            for x in yaml_files:
+                with open(os.path.join(events_dir, x), 'r') as fd:
+                    for e in yaml.safe_load(fd.read()).get('events', {}):
+                        events.append(e)
 
         return Everything(
             *events,
@@ -496,16 +456,17 @@
         '''Aggregate all the interface YAML in the interfaces.d
         directory into a single list of interfaces.'''
 
-        interfaces_dir = os.path.join(args.inputdir, 'interfaces.d')
-        yaml_files = filter(
-            lambda x: x.endswith('.yaml'),
-            os.listdir(interfaces_dir))
-
         interfaces = []
-        for x in yaml_files:
-            with open(os.path.join(interfaces_dir, x), 'r') as fd:
-                for i in yaml.safe_load(fd.read()):
-                    interfaces.append(i)
+        interfaces_dir = os.path.join(args.inputdir, 'interfaces.d')
+        if os.path.exists(interfaces_dir):
+            yaml_files = filter(
+                lambda x: x.endswith('.yaml'),
+                os.listdir(interfaces_dir))
+
+            for x in yaml_files:
+                with open(os.path.join(interfaces_dir, x), 'r') as fd:
+                    for i in yaml.safe_load(fd.read()):
+                        interfaces.append(i)
 
         return interfaces
 
@@ -516,9 +477,6 @@
             self.class_map[x['type']](**x) for x in a]
         super(Everything, self).__init__(**kw)
 
-    def list_interfaces(self, *a):
-        print ' '.join([str(i) for i in self.interfaces])
-
     def generate_cpp(self, loader):
         '''Render the template with the provided events and interfaces.'''
         with open(os.path.join(
@@ -537,7 +495,6 @@
     script_dir = os.path.dirname(os.path.realpath(__file__))
     valid_commands = {
         'generate-cpp': 'generate_cpp',
-        'list-interfaces': 'list_interfaces'
     }
 
     parser = argparse.ArgumentParser(
diff --git a/test/.gitignore b/test/.gitignore
index 65c2e9e..1520cd8 100644
--- a/test/.gitignore
+++ b/test/.gitignore
@@ -1 +1,2 @@
 phosphor-inventory-test
+xyz
diff --git a/test/Makefile.am b/test/Makefile.am
index 1f6470d..07f8453 100644
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -1,8 +1,27 @@
+BUILT_SOURCES=generated.cpp extra_ifaces.cpp
+CLEANFILES=$(BUILT_SOURCES)
+
+AM_CPPFLAGS = -iquote$(top_srcdir)
+
 check_PROGRAMS =
 noinst_PROGRAMS = phosphor-inventory-test
+noinst_LTLIBRARIES = libtest.la
 
-phosphor_inventory_test_SOURCES = \
-	test.cpp
-phosphor_inventory_test_LDFLAGS = $(SYSTEMD_LIBS) $(PTHREAD_CFLAGS)
-phosphor_inventory_test_CFLAGS = $(SYSTEMD_CFLAGS)
-phosphor_inventory_test_LDADD = ${top_builddir}/libmanager.la
+extra_yamldir=$(top_srcdir)/example/extra_interfaces.d
+
+phosphor_inventory_test_SOURCES = test.cpp
+phosphor_inventory_test_LDADD = libtest.la
+phosphor_inventory_test_LDFLAGS = $(SDBSUPLUS_LIBS) $(PTHREAD_CFLAGS)
+phosphor_inventory_test_CFLAGS = $(SDBUSPLUS_CFLAGS)
+
+libtest_la_SOURCES = \
+	extra_ifaces.cpp \
+	generated.cpp
+libtest_la_LIBADD = $(top_builddir)/libmanagercommon.la $(SDBUSPLUS_LIBS)
+
+clean-local: clean-extra
+
+generated.cpp:
+	$(AM_V_GEN)$(PYTHON) $(top_srcdir)/pimgen.py -d $(extra_yamldir)/.. -o $(builddir) generate-cpp
+
+-include Makefile.extra
diff --git a/test/Makefile.extra.in b/test/Makefile.extra.in
new file mode 100644
index 0000000..128a178
--- /dev/null
+++ b/test/Makefile.extra.in
@@ -0,0 +1 @@
+# Empty file so that 'configure' attempts to generate Makefile.extra.
diff --git a/test/test.cpp b/test/test.cpp
index 3f39fa9..6ffad51 100644
--- a/test/test.cpp
+++ b/test/test.cpp
@@ -13,15 +13,15 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-#include "../manager.hpp"
-#include "../config.h"
+#include "manager.hpp"
+#include "config.h"
 #include <cassert>
 #include <iostream>
 #include <algorithm>
 #include <thread>
 #include <chrono>
-#include <xyz/openbmc_project/Example/Iface1/server.hpp>
-#include <xyz/openbmc_project/Example/Iface2/server.hpp>
+#include "xyz/openbmc_project/Example/Iface1/server.hpp"
+#include "xyz/openbmc_project/Example/Iface2/server.hpp"
 
 using namespace std::literals::chrono_literals;
 using namespace std::literals::string_literals;