Added ability to directly embed NED programs in NEDsim binary for runtime execution.
[screensavers] / hacks / NEDsim / ned_programs / Makefile
diff --git a/hacks/NEDsim/ned_programs/Makefile b/hacks/NEDsim/ned_programs/Makefile
new file mode 100644 (file)
index 0000000..716c5c6
--- /dev/null
@@ -0,0 +1,61 @@
+#############################################################
+# (c) 2021 Aaron Taylor <ataylor at subgeniuskitty dot com> #
+# See LICENSE.txt file for copyright and license details.   #
+#############################################################
+
+################################################################################
+# Configuration
+################################################################################
+
+# To keep the rules simple, since we are dealing with six different file
+# extensions, NED_EMBED is defined with no suffix, leaving the suffix to be
+# hardcoded in each step as appropriate and simplying the make target list.
+.SUFFIXES: .ned_asm
+NED_SRC != ls *.ned_asm
+NED_EMBED := $(NED_SRC:.ned_asm=)
+
+# This header file will contain C declarations for the binary blobs and a
+# variable which tracks their size. It should be included in any C source code
+# that will link against the object files containing the binary blobs.
+DECLARATION_HEADER_FILE = embedded_ned_program_declarations.h
+
+# Misc program names/paths.
+GNU_ASM     = as
+NED_ASM     = ned1asm
+NED_OBJDUMP = ned1objdump
+
+################################################################################
+# Targets
+################################################################################
+
+# Since nedasm and nedobjcopy do not overwrite existing files, include target
+# `clean` as a preq.
+all: clean $(NED_EMBED)
+    # Build an #include guard around the C declarations of the binary blobs.
+       @mv $(DECLARATION_HEADER_FILE) $(DECLARATION_HEADER_FILE).tempfile
+       @echo "/* Autogenerated code. Do not edit. */" >> $(DECLARATION_HEADER_FILE)
+       @echo "#ifndef NEDSIM_AUTOGEN_BLOB_DECLARATIONS" >> $(DECLARATION_HEADER_FILE)
+       @echo "#define NEDSIM_AUTOGEN_BLOB_DECLARATIONS" >> $(DECLARATION_HEADER_FILE)
+       @cat $(DECLARATION_HEADER_FILE).tempfile >> $(DECLARATION_HEADER_FILE)
+       @rm $(DECLARATION_HEADER_FILE).tempfile
+       @echo "#endif" >> $(DECLARATION_HEADER_FILE)
+
+.ned_asm:
+       # We assemble the NED source and then dump the executable code section of
+       # the resulting a.out file as a binary blob.
+       @$(NED_ASM) -i $*.ned_asm -o $*.out
+       @$(NED_OBJDUMP) -i $*.out -o $*.bin
+       # Using `sed`, prepare an x86 assembly file as a container for each NED
+       # binary blob. This includes building a C header file with relevant
+       # declarations for each binary blob.
+       @sed 's/INSERTNAME/$*/g' container.x86_asm_template > $*.x86_asm
+       @echo "extern const uint8_t $*[];" >> $(DECLARATION_HEADER_FILE)
+       @echo "extern const size_t $*_size;" >> $(DECLARATION_HEADER_FILE)
+       # Assemble the container, creating a linkable object file containing the
+       # NED binary blob.
+       @$(GNU_ASM) -o $*.o $*.x86_asm
+       @rm -f $*.bin $*.x86_asm $*.out
+
+clean:
+       @rm -f *.bin *.x86_asm *.out *.o $(DECLARATION_HEADER_FILE)*
+