From 1bec8463dd4c5f62cc12c194f03956162ce9356b Mon Sep 17 00:00:00 2001 From: Alex Reinking Date: Tue, 14 Feb 2023 15:45:49 -0800 Subject: [PATCH] Add depfile support to CMake rules (#329) --- src/exo/cmake/AddExoLibrary.cmake | 6 ++++-- src/exo/main.py | 22 +++++++++++++++++++++- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/src/exo/cmake/AddExoLibrary.cmake b/src/exo/cmake/AddExoLibrary.cmake index cbf7f549c..9829daee6 100644 --- a/src/exo/cmake/AddExoLibrary.cmake +++ b/src/exo/cmake/AddExoLibrary.cmake @@ -10,7 +10,7 @@ function(add_exo_library) list(APPEND source_files "${src}") endforeach () - set(intdir "${CMAKE_CURRENT_BINARY_DIR}/${ARG_NAME}.exo") + set(intdir "${ARG_NAME}.exo") set(files "${intdir}/${ARG_NAME}.c" "${intdir}/${ARG_NAME}.h") list(TRANSFORM ARG_PYTHONPATH PREPEND "--modify;PYTHONPATH=path_list_append:") @@ -21,10 +21,12 @@ function(add_exo_library) $ -o "${intdir}" --stem "${ARG_NAME}" ${source_files} DEPENDS ${source_files} + DEPFILE "${intdir}/${ARG_NAME}.d" VERBATIM ) + list(TRANSFORM files PREPEND "${CMAKE_CURRENT_BINARY_DIR}/") add_library(${ARG_NAME} ${files}) add_library(${PROJECT_NAME}::${ARG_NAME} ALIAS ${ARG_NAME}) - target_include_directories(${ARG_NAME} PUBLIC "$") + target_include_directories(${ARG_NAME} PUBLIC "$") endfunction() diff --git a/src/exo/main.py b/src/exo/main.py index 583045a10..4e50b1432 100644 --- a/src/exo/main.py +++ b/src/exo/main.py @@ -2,6 +2,7 @@ import importlib import importlib.machinery import importlib.util +import inspect import sys from pathlib import Path @@ -32,7 +33,6 @@ def main(): outdir = Path(args.outdir) outdir.mkdir(parents=True, exist_ok=True) - outdir = outdir.resolve(strict=True) library = [ proc @@ -41,6 +41,26 @@ def main(): ] exo.compile_procs(library, outdir, f"{args.stem}.c", f"{args.stem}.h") + write_depfile(outdir, args.stem) + + +def write_depfile(outdir, stem): + modules = set() + for mod in sys.modules.values(): + try: + modules.add(inspect.getfile(mod)) + except TypeError: + pass # this is the case for built-in modules + + c_file = outdir / f"{stem}.c" + h_file = outdir / f"{stem}.h" + depfile = outdir / f"{stem}.d" + + sep = " \\\n " + deps = sep.join(sorted(modules)) + contents = f"{c_file} {h_file} : {deps}" + + depfile.write_text(contents) def get_procs_from_module(user_module):