switch to using mdwn_inline instead of special-cases in Makefile
authorJacob Lifshay <programmerjake@gmail.com>
Thu, 6 Apr 2023 03:09:18 +0000 (20:09 -0700)
committerJacob Lifshay <programmerjake@gmail.com>
Thu, 6 Apr 2023 03:11:51 +0000 (20:11 -0700)
also automatically tracks [[!include]] dependencies

openpower/mdwn_inline.py
openpower/sv/rfc/Makefile

index 53d30ac08f20e8af89f679c8303200dd1f706b94..296bad33e505fd9ca78db33ba58a26e34bc0b003 100755 (executable)
@@ -1,9 +1,18 @@
 #!/usr/bin/env python3
-
 import sys
+import os.path
+
+opened_files = []
+def open_tracked(name, mode='r'):
+    opened_files.append(name)
+    return open(name, mode)
 
-with open(sys.argv[2], "w") as o:
-    with open(sys.argv[1], "r") as f:
+output_file = sys.argv[2]
+file_path = os.path.abspath(__file__)
+openpower_path = os.path.split(file_path)[0]
+wiki_path = os.path.split(openpower_path)[0]
+with open(output_file, "w") as o:
+    with open_tracked(sys.argv[1], "r") as f:
         for line in f.readlines():
             if sys.argv[1].endswith("comparison_table.tex") and \
                 line.startswith("\begin{itemize}"):
@@ -13,25 +22,33 @@ with open(sys.argv[2], "w") as o:
             if not line.startswith("[[!inline"):
                 o.write(line)
                 continue
-            print (line.strip())
+            print(line.strip())
             # assume first thing is pagename
             line = line.split('"')
             fname = line[1]
-            print ("\t", fname)
+            print("\t", fname)
             if fname.endswith(".py"):
                 if fname.startswith("gf_reference"):
-                    with open("../../nmigen-gf/"+fname) as inc:
+                    with open_tracked(
+                            wiki_path + "/../nmigen-gf/" + fname) as inc:
                         o.write(inc.read())
                 else:
-                    with open("../%s" % fname) as inc:
+                    with open_tracked(wiki_path + "/" + fname) as inc:
                         o.write(inc.read())
             else:
                 if fname.endswith(".mdwn"):
-                    with open("../%s" % fname) as inc:
+                    with open_tracked(wiki_path + "/" + fname) as inc:
                         o.write(inc.read())
                 elif fname == 'openpower/isatables/fields.text':
-                    with open("../../openpower-isa/%s" % fname) as inc:
+                    with open_tracked(
+                            wiki_path + "/../openpower-isa/" + fname) as inc:
                         o.write(inc.read())
                 else:
-                    with open("../%s.mdwn" % fname) as inc:
+                    with open_tracked(
+                            wiki_path + "/" + fname + ".mdwn") as inc:
                         o.write(inc.read())
+
+deps_file = output_file + '.d'
+with open(deps_file, "w") as o:
+    deps = " ".join(opened_files)
+    o.write(f"{output_file} {deps_file}: {deps}\n")
index 16e39328d0aea0806ae4957e4a1bd58e5d2625f0..56480d2c7991111c3e522c2d62944ebf87a3513d 100644 (file)
@@ -1,15 +1,19 @@
 .PHONY: all clean upload
 
-all: $(patsubst %.mdwn,%.pdf,$(wildcard ls[0-9][0-9][0-9].mdwn))
+.SECONDARY:
 
-ls010.pdf: ls010.mdwn ../svp64.mdwn \
-       ../normal.mdwn \
-       ../ldst.mdwn \
-       ../branches.mdwn \
-       ../po9_encoding.mdwn \
-       ../cr_ops.mdwn
+main_sources = $(wildcard ls[0-9][0-9][0-9].mdwn)
+pdfs = $(patsubst %.mdwn,%.pdf,$(main_sources))
+deps = $(patsubst %,tex_out/%.d,$(main_sources))
 
-%.pdf: %.mdwn ../../pandoc_img.py
+all: $(pdfs)
+
+# generate dependency files, also generates intermediate .mdwn files too
+tex_out/%.mdwn.d: %.mdwn ../../mdwn_inline.py
+       mkdir -p $(dir $@)
+       ../../mdwn_inline.py $< tex_out/$*.mdwn
+
+%.pdf: tex_out/%.mdwn ../../pandoc_img.py
        pandoc \
                --filter ../../pandoc_img.py \
                -V margin-top=0.9in \
@@ -19,7 +23,7 @@ ls010.pdf: ls010.mdwn ../svp64.mdwn \
                -V fontsize=9pt \
                -V papersize=legal \
                -V linkcolor=blue \
-               -f markdown $(filter-out ../../pandoc_img.py,$^) \
+               -f markdown $< \
                -s --self-contained \
                --mathjax \
                -o $@
@@ -29,4 +33,6 @@ upload:
        ssh libre-soc.org 'cp opf_isa_wg/*.pdf /var/www/ftp.libre-riscv.org/opf_ext_rfc/'
 
 clean:
-       rm -fr *.pdf
+       rm -fr *.pdf tex_out
+
+-include $(deps)