1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
|
#!/usr/bin/env python3
"""
Writes out the source and include files needed for AutoTools.
This script will update the collected_files.md file.
"""
import os
from typing import Iterable, Sequence, Tuple
import re
BANNER = "# This file was automatically generated by scripts/update_sources.py"
VENDOR_SOURCES = ("src/vendor/stb.c",)
VENDOR_SOURCES_AUTOMAKE = (
"src/vendor/glad.c",
"src/vendor/lodepng.c",
"src/vendor/stb.c",
"src/vendor/utf8proc/utf8proc.c",
)
def get_sources(
sources: bool = False,
includes: bool = False,
directory: str = "src/libtcod",
vendor_sources: Tuple[str, ...] = VENDOR_SOURCES,
) -> Iterable[Tuple[str, Sequence[str]]]:
"""Iterate over sources and headers with sub-folders grouped together."""
re_inclusion = []
if sources:
re_inclusion.append("c|cpp")
if includes:
re_inclusion.append("h|hpp")
re_valid = re.compile(r".*\.(%s)$" % ("|".join(re_inclusion),))
for curpath, dirs, files in os.walk(directory):
# Ignore hidden directories.
dirs[:] = [dir for dir in dirs if not dir.startswith(".")]
files = [
os.path.join(curpath, f).replace("\\", "/")
for f in files
if re_valid.match(f)
]
group = os.path.relpath(curpath, "src").replace("\\", "/")
yield group, files
if sources:
yield "vendor", vendor_sources
def all_sources(
sources: bool = True,
includes: bool = False,
vendor_sources: Tuple[str, ...] = VENDOR_SOURCES,
) -> Iterable[str]:
"""Iterate over all sources needed to compile libtcod."""
for _, sources_ in get_sources(
sources=sources, includes=includes, vendor_sources=vendor_sources
):
yield from sources_
def generate_am() -> str:
"""Returns an AutoMake script.
This might be run on Windows, so it must return Unix file separators.
"""
out = f"{BANNER}\n"
for group, files in get_sources(sources=False, includes=True):
include_name = group.replace("/", "_")
files = ["../../" + f for f in files]
out += f"\n{include_name}_includedir = $(includedir)/{group}"
out += f"\n{include_name}_include_HEADERS = \\"
out += "\n\t" + " \\\n\t".join(files)
out += "\n"
out += "\nlibtcod_la_SOURCES = \\"
out += "\n\t" + " \\\n\t".join(
"../../" + f for f in all_sources(vendor_sources=VENDOR_SOURCES_AUTOMAKE)
)
out += "\n"
return out
def generate_cmake() -> str:
"""Returns a CMake script with libtcod's sources."""
out = f"{BANNER}"
out += "\ntarget_sources(${PROJECT_NAME} PRIVATE\n "
out += "\n ".join(
os.path.relpath(f, "src").replace("\\", "/") for f in all_sources(includes=True)
)
out += "\n)"
for group, files in get_sources(sources=False, includes=True, directory="src/"):
if group.startswith("vendor"):
continue
group = group.replace("\\", "/")
out += "\ninstall(FILES\n "
out += "\n ".join(
os.path.relpath(f, "src").replace("\\", "/") for f in files
)
out += "\n DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/%s" % group
out += "\n COMPONENT IncludeFiles"
out += "\n)"
for group, files in get_sources(sources=True, includes=True):
group = group.replace("/", r"\\")
out += f"\nsource_group({group} FILES\n "
out += "\n ".join(
os.path.relpath(f, "src").replace("\\", "/") for f in files
)
out += "\n)"
out += "\n"
return out
def main() -> None:
# Change to project root directory, using this file as a reference.
os.chdir(os.path.join(os.path.split(__file__)[0], ".."))
with open("buildsys/autotools/sources.am", "w") as file:
file.write(generate_am())
with open("src/sources.cmake", "w") as file:
file.write(generate_cmake())
if __name__ == "__main__":
main()
|