Fix makectx

This commit is contained in:
Seeky 2022-07-05 14:32:08 +01:00
parent 0289e125e5
commit bcdc013aec
4 changed files with 73 additions and 57 deletions

View File

@ -8,7 +8,7 @@ import json
import os
from subprocess import PIPE, Popen
from sys import executable as PYTHON, platform
from typing import Tuple, Union
from typing import List, Tuple, Union
#############
# Functions #
@ -52,6 +52,22 @@ def get_containing_slice(addr: int) -> Tuple[Binary, SourceDesc]:
else:
return (Binary.DOL, containing)
def find_headers(dirname: str, base=None) -> List[str]:
"""Returns a list of all headers in a folder recursively"""
if base is None:
base = dirname
ret = []
for name in os.listdir(dirname):
path = dirname + '/' + name
if os.path.isdir(path):
ret.extend(find_headers(path, base))
elif name.endswith('.h'):
ret.append(path[len(base)+1:])
return ret
################
# Project dirs #
################
@ -68,6 +84,9 @@ INCDIR = "include"
# Build artifacts directory
BUILDDIR = "build"
# Build include directory
BUILD_INCDIR = f"{BUILDDIR}/include"
# Output binaries directory
OUTDIR = "out"
@ -86,6 +105,7 @@ CONFIG = "config"
# ppcdis
PPCDIS = "tools/ppcdis"
PPCDIS_INCDIR = f"{PPCDIS}/include"
ANALYSER = f"{PYTHON} {PPCDIS}/analyser.py"
DISASSEMBLER = f"{PYTHON} {PPCDIS}/disassembler.py"
ORDERSTRINGS = f"{PYTHON} {PPCDIS}/orderstrings.py"

View File

@ -158,14 +158,14 @@ n.rule(
n.rule(
"as",
command = f"$as $asflags -I {c.INCDIR} -I $ppcdis/include -c $in -o $out -I orig",
command = f"$as $asflags -I {c.INCDIR} -I {c.PPCDIS_INCDIR} -c $in -o $out -I orig",
description = "AS $in"
)
# Due to CW dumbness with .d output location, $outstem must be defined without the .o
n.rule(
"cc",
command = f"$cc $cflags -I- -i {c.INCDIR} -i $ppcdis/include -i $builddir/include -MD -gccdep -c $in -o $out",
command = f"$cc $cflags -I- -i {c.INCDIR} -i {c.PPCDIS_INCDIR} -i {c.BUILD_INCDIR} -MD -gccdep -c $in -o $out",
description = "CC $in",
deps = "gcc",
depfile = "$outstem.d"
@ -173,7 +173,7 @@ n.rule(
n.rule(
"ccs",
command = f"$cc $cflags -I- -i {c.INCDIR} -i $builddir/include -MD -gccdep -c $in -o $out -S",
command = f"$cc $cflags -I- -i {c.INCDIR} -i {c.BUILD_INCDIR} -MD -gccdep -c $in -o $out -S",
description = "CC -S $in",
deps = "gcc",
depfile = "$outstem.d"
@ -233,7 +233,7 @@ class AsmInclude(GeneratedInclude):
def __init__(self, source_name: str, match: str):
self.addr = match
super().__init__(source_name, f"$builddir/include/asm/{self.addr}.s")
super().__init__(source_name, f"{c.BUILD_INCDIR}/asm/{self.addr}.s")
def build(self, ctx: SourceContext):
n.build(
@ -255,7 +255,7 @@ class JumptableInclude(GeneratedInclude):
def __init__(self, source_name: str, match: str):
self.addr = match
super().__init__(source_name, f"$builddir/include/jumptable/{self.addr}.inc")
super().__init__(source_name, f"{c.BUILD_INCDIR}/jumptable/{self.addr}.inc")
def build(self, ctx: SourceContext):
n.build(
@ -277,7 +277,7 @@ class StringInclude(GeneratedInclude):
def __init__(self, source_name: str, match: Tuple[str]):
self.start, self.end = match
super().__init__(source_name, f"$builddir/include/orderstrings/{self.start}_{self.end}.inc")
super().__init__(source_name, f"{c.BUILD_INCDIR}/orderstrings/{self.start}_{self.end}.inc")
def build(self, ctx: SourceContext):
n.build(
@ -299,7 +299,7 @@ class FloatInclude(GeneratedInclude):
def __init__(self, source_name: str, match: Tuple[str]):
folder, manual, self.start, self.end = match
self.manual = manual != ''
super().__init__(source_name, f"$builddir/include/{folder}/{self.start}_{self.end}.inc")
super().__init__(source_name, f"{c.BUILD_INCDIR}/{folder}/{self.start}_{self.end}.inc")
def build(self, ctx: SourceContext):
sda = "--sda " if ctx.sdata2_threshold >= 4 else ""
@ -322,7 +322,7 @@ class DoubleInclude(GeneratedInclude):
def __init__(self, source_name: str, match: Tuple[str]):
self.start, self.end = match
super().__init__(source_name, f"$builddir/include/orderdoubles/{self.start}_{self.end}.inc")
super().__init__(source_name, f"{c.BUILD_INCDIR}/orderdoubles/{self.start}_{self.end}.inc")
def build(self, ctx: SourceContext):
n.build(

View File

@ -2,39 +2,47 @@
Creates a context of all headers
"""
from os import listdir
from os.path import isdir
import os.path
from typing import List
import common as c
gIncluded = set()
def process_header(path, base):
def resolve_path(path: str, bases: List[str]) -> str:
ret = None
for base in bases:
full = f"{base}/{path}"
if os.path.exists(full):
assert ret is None, f"Name conflict: {full} {ret}"
ret = full
assert ret is not None, f"Header {path} not found"
return ret
def process_header(path: str, bases: List[str]):
global gIncluded
if path in gIncluded:
return ""
ret = ""
with open(path) as f:
for line in f:
if line.startswith("#include"):
ret += process_header(base + '/' + line[line.find('<')+1:line.find('>')], base)
elif not line.startswith("#pragma once"):
ret += line
gIncluded.add(path)
full = resolve_path(path, bases)
ret = ""
with open(full) as f:
for line in f:
if line.startswith("#include"):
ret += process_header(line[line.find('<')+1:line.find('>')], bases)
elif not line.startswith("#pragma once"):
ret += line
return ret
def concat_headers(dirname, base=None):
if base is None:
base = dirname
def makectx(bases: List[str]):
ret = ""
for name in listdir(dirname):
path = dirname + '/' + name
if isdir(path):
ret += concat_headers(path, base)
elif name.endswith('.h'):
ret += (process_header(path, base))
for base in bases:
for header in c.find_headers(base):
ret += process_header(header, bases)
return ret
print(concat_headers("include"))
print(makectx([c.INCDIR, c.PPCDIS_INCDIR]))

View File

@ -2,46 +2,34 @@
Creates a context of all headers with the preprocessor ran
"""
from os import listdir, unlink
from os.path import isdir
from os import unlink
from tempfile import NamedTemporaryFile
from typing import List
import common as c
def find_headers(dirname: str, base=None) -> List[str]:
"""Returns a list of all headers in a folder recursively"""
if base is None:
base = dirname
ret = []
for name in listdir(dirname):
path = dirname + '/' + name
if isdir(path):
ret.extend(find_headers(path, base))
elif name.endswith('.h'):
ret.append(path[len(base)+1:])
return ret
def make_includes(dirname: str) -> str:
def make_includes(dirnames: List[str]) -> str:
"""Returns a chain of #includes for all headers in a folder"""
return '\n'.join([
f"#include <{header}>"
for header in find_headers(dirname)
])
return '\n'.join(
'\n'.join(
f"#include <{header}>"
for header in c.find_headers(dirname)
)
for dirname in dirnames
)
# Find all headers
includes = make_includes("include")
includes = make_includes([c.INCDIR, c.PPCDIS_INCDIR])
# Run mwcc preprocessor
with NamedTemporaryFile(suffix=".c", delete=False) as tmp:
try:
tmp.write(includes.encode())
tmp.close()
out = c.get_cmd_stdout(f"{c.CC} -I- -i include -DM2C -stderr -E {tmp.name}")
out = c.get_cmd_stdout(
f"{c.CC} -I- -i {c.INCDIR} -i {c.PPCDIS}/include -DM2C -stderr -E {tmp.name}"
)
finally:
unlink(tmp.name)