clean up makefile and support recursive [[!inline]]
authorJacob Lifshay <programmerjake@gmail.com>
Tue, 11 Apr 2023 05:11:42 +0000 (22:11 -0700)
committerJacob Lifshay <programmerjake@gmail.com>
Tue, 11 Apr 2023 05:11:42 +0000 (22:11 -0700)
openpower/mdwn_inline.py
openpower/sv/rfc/Makefile

index 296bad33e505fd9ca78db33ba58a26e34bc0b003..6ff02492481b2fc7189f39721e1c8853e01ab62b 100755 (executable)
@@ -1,20 +1,38 @@
 #!/usr/bin/env python3
 import sys
 import os.path
+from io import StringIO
+
+deps_only = sys.argv[1] == '--deps'
+
+if deps_only:
+    del sys.argv[1]
 
 opened_files = []
+
 def open_tracked(name, mode='r'):
     opened_files.append(name)
-    return open(name, mode)
+    try:
+        return open(name, mode)
+    except FileNotFoundError:
+        if deps_only:
+            return StringIO("")
+        raise
 
 output_file = sys.argv[2]
+try:
+    os.remove(output_file)
+except FileNotFoundError:
+    pass
+temp_output_file = output_file + '.tmp'
 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:
+def body(o, print=print):
+    def recursive_inline(f, input_name, depth):
+        assert depth < 10, "probably found an [[!inline]]-loop"
         for line in f.readlines():
-            if sys.argv[1].endswith("comparison_table.tex") and \
+            if input_name.endswith("comparison_table.tex") and \
                 line.startswith("\begin{itemize}"):
                 o.write(line)
                 o.write("\\itemsep -0.3em\n")
@@ -26,29 +44,39 @@ with open(output_file, "w") as o:
             # assume first thing is pagename
             line = line.split('"')
             fname = line[1]
-            print("\t", fname)
+            print(f"\tdepth={depth}: {fname}")
             if fname.endswith(".py"):
                 if fname.startswith("gf_reference"):
                     with open_tracked(
                             wiki_path + "/../nmigen-gf/" + fname) as inc:
-                        o.write(inc.read())
+                        recursive_inline(inc, fname, depth + 1)
                 else:
                     with open_tracked(wiki_path + "/" + fname) as inc:
-                        o.write(inc.read())
+                        recursive_inline(inc, fname, depth + 1)
             else:
                 if fname.endswith(".mdwn"):
                     with open_tracked(wiki_path + "/" + fname) as inc:
-                        o.write(inc.read())
+                        recursive_inline(inc, fname, depth + 1)
                 elif fname == 'openpower/isatables/fields.text':
                     with open_tracked(
                             wiki_path + "/../openpower-isa/" + fname) as inc:
-                        o.write(inc.read())
+                        recursive_inline(inc, fname, depth + 1)
                 else:
                     with open_tracked(
                             wiki_path + "/" + fname + ".mdwn") as inc:
-                        o.write(inc.read())
+                        recursive_inline(inc, fname, depth + 1)
+
+    with open_tracked(sys.argv[1], "r") as f:
+        recursive_inline(f, sys.argv[1], 0)
 
-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")
+if deps_only:
+    with StringIO() as o:
+        body(o, print=lambda *_: None)
+    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")
+else:
+    with open(temp_output_file, "w") as o:
+        body(o)
+    os.rename(temp_output_file, output_file)
\ No newline at end of file
index 58191dca0fae58c797a0f4fefa9554b4f76fef14..acebf478d3d84c44a3ed41faedeb3ae2f1947db6 100644 (file)
@@ -6,13 +6,18 @@ main_sources = $(wildcard ls[0-9][0-9][0-9].mdwn)
 pdfs = $(patsubst %.mdwn,%.pdf,$(main_sources))
 deps = $(patsubst %,tex_out/%.d,$(main_sources))
 
-all: ls012_optable $(pdfs)
+all: $(pdfs)
 
-ls012_optable:
+ls012 = $(realpath ls012)
+
+$(ls012)/areas.mdwn $(ls012)/xo_cost.mdwn: ls012_optable.py ls012/optable.csv
        python3 ls012_optable.py
 
-# generate dependency files, also generates intermediate .mdwn files too
 tex_out/%.mdwn.d: %.mdwn ../../mdwn_inline.py
+       @mkdir -p $(dir $@)
+       @../../mdwn_inline.py --deps $< tex_out/$*.mdwn
+
+tex_out/%.mdwn: %.mdwn ../../mdwn_inline.py
        mkdir -p $(dir $@)
        ../../mdwn_inline.py $< tex_out/$*.mdwn
 
@@ -36,6 +41,6 @@ upload:
        ssh libre-soc.org 'cp opf_isa_wg/*.pdf /var/www/ftp.libre-riscv.org/opf_ext_rfc/'
 
 clean:
-       rm -fr *.pdf tex_out
+       rm -fr *.pdf tex_out ls012/areas.mdwn ls012/xo_cost.mdwn
 
 -include $(deps)