build-unit-test-docker: add dependencies to package def

Add dependencies to the package definition for each package and remove
the manual "COPY" commands to create the staged-dependencies.
Eventually this will also be used to parallelize the docker build
process.

Signed-off-by: Patrick Williams <patrick@stwcx.xyz>
Change-Id: Ifb7e26fccea62ae9bc8d450a91d1fc4d9ef0db06
diff --git a/scripts/build-unit-test-docker b/scripts/build-unit-test-docker
index 7be128b..f8053d3 100755
--- a/scripts/build-unit-test-docker
+++ b/scripts/build-unit-test-docker
@@ -79,14 +79,39 @@
     # version from meta-openembedded/meta-oe/recipes-devtools/nlohmann-fifo/nlohmann-fifo_git.bb
     "nlohmann/fifo_map": {"rev": "0dfbf5dacbb15a32c43f912a7e66a54aae39d0f9"},
     "open-power/pdbg": {},
-    "openbmc/gpioplus": {},
-    "openbmc/phosphor-dbus-interfaces": {},
-    "openbmc/phosphor-logging": {},
-    "openbmc/phosphor-objmgr": {},
-    "openbmc/pldm": {},
+    "openbmc/gpioplus": {"depends": ["openbmc/stdplus"]},
+    "openbmc/phosphor-dbus-interfaces": {"depends": ["openbmc/sdbusplus"]},
+    "openbmc/phosphor-logging": {
+        "depends": [
+            "USCiLab/cereal",
+            "nlohmann/fifo_map",
+            "openbmc/phosphor-dbus-interfaces",
+            "openbmc/sdbusplus",
+            "openbmc/sdeventplus",
+        ]
+    },
+    "openbmc/phosphor-objmgr": {
+        "depends": [
+            "boost",
+            "leethomason/tinyxml2",
+            "openbmc/phosphor-logging",
+            "openbmc/sdbusplus",
+        ]
+    },
+    "openbmc/pldm": {
+        "depends": [
+            "CLIUtils/CLI11",
+            "boost",
+            "nlohmann/json",
+            "openbmc/phosphor-dbus-interfaces",
+            "openbmc/phosphor-logging",
+            "openbmc/sdbusplus",
+            "openbmc/sdeventplus",
+        ]
+    },
     "openbmc/sdbusplus": {},
-    "openbmc/sdeventplus": {},
-    "openbmc/stdplus": {},
+    "openbmc/sdeventplus": {"depends": ["Naios/function2", "openbmc/stdplus"]},
+    "openbmc/stdplus": {"depends": ["fmtlib/fmt", "martinmoene/span-lite"]},
 }
 
 
@@ -106,6 +131,24 @@
     return f"https://github.com/{pkg}/archive/{pkg_rev(pkg)}.tar.gz"
 
 
+def pkg_copycmds(pkg=None):
+    pkgs = []
+    if pkg:
+        if "depends" not in packages[pkg]:
+            return ""
+        pkgs = sorted(packages[pkg]["depends"])
+    else:
+        pkgs = sorted(packages.keys())
+
+    copy_cmds = ""
+    for p in pkgs:
+        copy_cmds += f"COPY --from={pkg_stagename(p)} {prefix} {prefix}\n"
+        # Workaround for upstream docker bug and multiple COPY cmds
+        # https://github.com/moby/moby/issues/37965
+        copy_cmds += "RUN true\n"
+    return copy_cmds
+
+
 # Look up the HEAD for missing a static rev.
 pkg_lookups = {}
 for pkg in packages.keys():
@@ -151,12 +194,7 @@
 # Build the commands needed to compose our final image
 # We must sort the packages, otherwise we might produce an unstable
 # docker file and rebuild the image unnecessarily
-copy_cmds = ""
-for pkg in sorted(packages.keys()):
-    copy_cmds += f"COPY --from={pkg_stagename(pkg)} {prefix} {prefix}\n"
-    # Workaround for upstream docker bug and multiple COPY cmds
-    # https://github.com/moby/moby/issues/37965
-    copy_cmds += "RUN true\n"
+copy_cmds = pkg_copycmds()
 
 # Special flags if setting up a deb mirror.
 mirror = ""
@@ -294,18 +332,21 @@
 RUN pip3 install protobuf
 
 FROM openbmc-base as {pkg_stagename('linux-test-project/lcov')}
+{pkg_copycmds('linux-test-project/lcov')}
 RUN curl -L {pkg_url('linux-test-project/lcov')} | tar -xz && \
 cd lcov-* && \
 make -j{proc_count} && \
 make install
 
 FROM openbmc-base as {pkg_stagename('Naios/function2')}
+{pkg_copycmds('Naios/function2')}
 RUN curl -L {pkg_url('Naios/function2')} | tar -xz && \
 cd function2-* && \
 mkdir {prefix}/include/function2 && \
 cp include/function2/function2.hpp {prefix}/include/function2/
 
 FROM openbmc-base as {pkg_stagename('google/googletest')}
+{pkg_copycmds('google/googletest')}
 RUN curl -L {pkg_url('google/googletest')} | tar -xz && \
 cd googletest-* && \
 mkdir build && \
@@ -315,6 +356,7 @@
 make install
 
 FROM openbmc-base as {pkg_stagename('catchorg/Catch2')}
+{pkg_copycmds('catchorg/Catch2')}
 RUN curl -L {pkg_url('catchorg/Catch2')} | tar -xz && \
 cd Catch2-* && \
 mkdir build && \
@@ -324,10 +366,12 @@
 make install
 
 FROM openbmc-base as {pkg_stagename('USCiLab/cereal')}
+{pkg_copycmds('USCiLab/cereal')}
 RUN curl -L {pkg_url('USCiLab/cereal')} | tar -xz && \
 cp -a cereal-*/include/cereal/ {prefix}/include/
 
 FROM openbmc-base as {pkg_stagename('CLIUtils/CLI11')}
+{pkg_copycmds('CLIUtils/CLI11')}
 RUN curl -L {pkg_url('CLIUtils/CLI11')} | tar -xz && \
 cd CLI11-* && \
 mkdir build && \
@@ -337,6 +381,7 @@
 make install
 
 FROM openbmc-base as {pkg_stagename('fmtlib/fmt')}
+{pkg_copycmds('fmtlib/fmt')}
 RUN curl -L {pkg_url('fmtlib/fmt')} | tar -xz && \
 cd fmt-* && \
 mkdir build && \
@@ -346,6 +391,7 @@
 make install
 
 FROM openbmc-base as {pkg_stagename('nlohmann/json')}
+{pkg_copycmds('nlohmann/json')}
 RUN curl -L {pkg_url('nlohmann/json')} | tar -xz && \
 cd json-* && \
 mkdir {prefix}/include/nlohmann && \
@@ -353,10 +399,12 @@
 ln -s {prefix}/include/nlohmann/json.hpp {prefix}/include/json.hpp
 
 FROM openbmc-base as {pkg_stagename('nlohmann/fifo_map')}
+{pkg_copycmds('nlohmann/fifo_map')}
 RUN curl -L {pkg_url('nlohmann/fifo_map')} | tar -xz && \
 cd fifo_map-*/src && cp fifo_map.hpp {prefix}/include/
 
 FROM openbmc-base as {pkg_stagename('martinmoene/span-lite')}
+{pkg_copycmds('martinmoene/span-lite')}
 RUN curl -L {pkg_url('martinmoene/span-lite')} | tar -xz && \
 cd span-lite-* && \
 mkdir build && \
@@ -366,18 +414,21 @@
 make install
 
 FROM openbmc-base as {pkg_stagename('openbmc/linux')}
+{pkg_copycmds('openbmc/linux')}
 RUN curl -L {pkg_url('openbmc/linux')} | tar -xz && \
 cd linux-* && \
 make -j{proc_count} defconfig && \
 make INSTALL_HDR_PATH=/usr/local headers_install
 
 FROM openbmc-base as {pkg_stagename('boost')}
+{pkg_copycmds('boost')}
 RUN curl -L {pkg_url('boost')} | tar -xj && \
 cd boost_*/ && \
 ./bootstrap.sh --prefix={prefix} --with-libraries=context,coroutine && \
 ./b2 && ./b2 install --prefix={prefix}
 
 FROM openbmc-base as {pkg_stagename('leethomason/tinyxml2')}
+{pkg_copycmds('leethomason/tinyxml2')}
 RUN curl -L {pkg_url('leethomason/tinyxml2')} | tar -xz && \
 cd tinyxml2-* && \
 mkdir build && \
@@ -387,6 +438,7 @@
 make install
 
 FROM openbmc-base as {pkg_stagename('CPPAlliance/url')}
+{pkg_copycmds('CPPAlliance/url')}
 RUN curl -L {pkg_url('CPPAlliance/url')} | tar -xz && \
 cd url-* && \
 mkdir buildir && \
@@ -396,6 +448,7 @@
 make install
 
 FROM openbmc-base as {pkg_stagename('tristanpenman/valijson')}
+{pkg_copycmds('tristanpenman/valijson')}
 RUN curl -L {pkg_url('tristanpenman/valijson')} | tar -xz && \
 cd valijson-* && \
 mkdir build && \
@@ -405,6 +458,7 @@
 make install
 
 FROM openbmc-base as {pkg_stagename('LibVNC/libvncserver')}
+{pkg_copycmds('LibVNC/libvncserver')}
 RUN curl -L {pkg_url('LibVNC/libvncserver')} | tar -xz && \
 cd libvncserver-* && \
 mkdir build && \
@@ -414,8 +468,7 @@
 make install
 
 FROM openbmc-base as {pkg_stagename('openbmc/stdplus')}
-COPY --from={pkg_stagename('fmtlib/fmt')} {prefix} {prefix}
-COPY --from={pkg_stagename('martinmoene/span-lite')} {prefix} {prefix}
+{pkg_copycmds('openbmc/stdplus')}
 RUN curl -L {pkg_url('openbmc/stdplus')} | tar -xz && \
 cd stdplus-* && \
 meson build {meson_flags} -Dtests=disabled -Dexamples=false && \
@@ -423,6 +476,7 @@
 ninja -C build install
 
 FROM openbmc-base as {pkg_stagename('openbmc/sdbusplus')}
+{pkg_copycmds('openbmc/sdbusplus')}
 RUN curl -L {pkg_url('openbmc/sdbusplus')} | tar -xz && \
 cd sdbusplus-* && \
 cd tools && ./setup.py install --root=/ --prefix={prefix} && \
@@ -431,8 +485,7 @@
 ninja -C build install
 
 FROM openbmc-base as {pkg_stagename('openbmc/sdeventplus')}
-COPY --from={pkg_stagename('Naios/function2')} {prefix} {prefix}
-COPY --from={pkg_stagename('openbmc/stdplus')} {prefix} {prefix}
+{pkg_copycmds('openbmc/sdeventplus')}
 RUN curl -L {pkg_url('openbmc/sdeventplus')} | tar -xz && \
 cd sdeventplus-* && \
 meson build {meson_flags} -Dtests=disabled -Dexamples=false && \
@@ -440,7 +493,7 @@
 ninja -C build install
 
 FROM openbmc-base as {pkg_stagename('openbmc/gpioplus')}
-COPY --from={pkg_stagename('openbmc/stdplus')} {prefix} {prefix}
+{pkg_copycmds('openbmc/gpioplus')}
 RUN curl -L {pkg_url('openbmc/gpioplus')} | tar -xz && \
 cd gpioplus-* && \
 meson build {meson_flags} -Dtests=disabled -Dexamples=false && \
@@ -448,7 +501,7 @@
 ninja -C build install
 
 FROM openbmc-base as {pkg_stagename('openbmc/phosphor-dbus-interfaces')}
-COPY --from={pkg_stagename('openbmc/sdbusplus')} {prefix} {prefix}
+{pkg_copycmds('openbmc/phosphor-dbus-interfaces')}
 RUN curl -L {pkg_url('openbmc/phosphor-dbus-interfaces')} | tar -xz && \
 cd phosphor-dbus-interfaces-* && \
 meson build {meson_flags} -Ddata_org_open_power=true -Ddata_com_ibm=true && \
@@ -456,11 +509,7 @@
 ninja -C build install
 
 FROM openbmc-base as {pkg_stagename('openbmc/phosphor-logging')}
-COPY --from={pkg_stagename('USCiLab/cereal')} {prefix} {prefix}
-COPY --from={pkg_stagename('openbmc/sdbusplus')} {prefix} {prefix}
-COPY --from={pkg_stagename('openbmc/sdeventplus')} {prefix} {prefix}
-COPY --from={pkg_stagename('openbmc/phosphor-dbus-interfaces')} {prefix} {prefix}
-COPY --from={pkg_stagename('nlohmann/fifo_map')} {prefix} {prefix}
+{pkg_copycmds('openbmc/phosphor-logging')}
 RUN curl -L {pkg_url('openbmc/phosphor-logging')} | tar -xz && \
 cd phosphor-logging-* && \
 ./bootstrap.sh && \
@@ -469,10 +518,7 @@
 make install
 
 FROM openbmc-base as {pkg_stagename('openbmc/phosphor-objmgr')}
-COPY --from={pkg_stagename('boost')} {prefix} {prefix}
-COPY --from={pkg_stagename('openbmc/sdbusplus')} {prefix} {prefix}
-COPY --from={pkg_stagename('leethomason/tinyxml2')} {prefix} {prefix}
-COPY --from={pkg_stagename('openbmc/phosphor-logging')} {prefix} {prefix}
+{pkg_copycmds('openbmc/phosphor-objmgr')}
 RUN curl -L {pkg_url('openbmc/phosphor-objmgr')} | tar -xz && \
 cd phosphor-objmgr-* && \
 ./bootstrap.sh && \
@@ -481,6 +527,7 @@
 make install
 
 FROM openbmc-base as {pkg_stagename('open-power/pdbg')}
+{pkg_copycmds('open-power/pdbg')}
 RUN curl -L {pkg_url('open-power/pdbg')} | tar -xz && \
 cd pdbg-* && \
 ./bootstrap.sh && \
@@ -489,13 +536,7 @@
 make install
 
 FROM openbmc-base as {pkg_stagename('openbmc/pldm')}
-COPY --from={pkg_stagename('openbmc/sdbusplus')} {prefix} {prefix}
-COPY --from={pkg_stagename('openbmc/sdeventplus')} {prefix} {prefix}
-COPY --from={pkg_stagename('boost')} {prefix} {prefix}
-COPY --from={pkg_stagename('openbmc/phosphor-dbus-interfaces')} {prefix} {prefix}
-COPY --from={pkg_stagename('openbmc/phosphor-logging')} {prefix} {prefix}
-COPY --from={pkg_stagename('nlohmann/json')} {prefix} {prefix}
-COPY --from={pkg_stagename('CLIUtils/CLI11')} {prefix} {prefix}
+{pkg_copycmds('openbmc/pldm')}
 RUN curl -L {pkg_url('openbmc/pldm')} | tar -xz && \
 cd pldm-* && \
 meson build {meson_flags} -Dlibpldm-only=enabled -Doem-ibm=enabled -Dtests=disabled && \