Add dependency file generation to atdna

This commit is contained in:
Jack Andersen 2017-08-19 19:22:25 -10:00
parent b1f73f0c33
commit 38985326f3
3 changed files with 69 additions and 10 deletions

View File

@ -152,10 +152,16 @@ endif()
# Super handy macro for adding atdna target
macro(atdna out)
# Ninja wants invocations in root binary dir for DEPFILE application
file(RELATIVE_PATH out_rel ${CMAKE_BINARY_DIR} "${CMAKE_CURRENT_BINARY_DIR}/${out}")
# Make input files source-relative
set(ins "")
set(ins_impdeps "")
foreach(arg ${ARGN})
list(APPEND ins ${CMAKE_CURRENT_SOURCE_DIR}/${arg})
list(APPEND ins_impdeps CXX)
list(APPEND ins_impdeps ${CMAKE_CURRENT_SOURCE_DIR}/${arg})
endforeach()
# Get local include directories for atdna
@ -194,8 +200,22 @@ macro(atdna out)
endif()
# Make target
add_custom_command(OUTPUT ${out} COMMAND $<TARGET_FILE:atdna>
ARGS ${extraargs} -o ${out} ${cdefcli} ${inccli} "-I${ATHENA_INCLUDE_DIR}"
-isystem "${CLANG_INCLUDE_DIR}" ${ins}
DEPENDS ${ins} COMMENT "Generating DNA ${out}")
if(${CMAKE_GENERATOR} STREQUAL "Ninja")
# Use Ninja's DEPFILE parser in cooperation with atdna
add_custom_command(OUTPUT ${out} COMMAND $<TARGET_FILE:atdna>
ARGS ${extraargs} -o ${out_rel} -MD -MT ${out_rel} -MF ${out_rel}.d ${cdefcli} ${inccli}
"-I${ATHENA_INCLUDE_DIR}" -isystem "${CLANG_INCLUDE_DIR}" ${ins}
DEPENDS atdna ${ins} IMPLICIT_DEPENDS ${ins_impdeps}
DEPFILE "${CMAKE_CURRENT_BINARY_DIR}/${out}.d"
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
COMMENT "Generating DNA ${out_rel}")
else()
# Use CMake's built-in dependency scanner for makefile targets
add_custom_command(OUTPUT ${out} COMMAND $<TARGET_FILE:atdna>
ARGS ${extraargs} -o ${out_rel} ${cdefcli} ${inccli}
"-I${ATHENA_INCLUDE_DIR}" -isystem "${CLANG_INCLUDE_DIR}" ${ins}
DEPENDS atdna ${ins} IMPLICIT_DEPENDS ${ins_impdeps}
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
COMMENT "Generating DNA ${out_rel}")
endif()
endmacro()

View File

@ -13,10 +13,16 @@ find_package(Athena REQUIRED)
# Super handy macro for adding atdna target
macro(atdna out)
# Ninja wants invocations in root binary dir for DEPFILE application
file(RELATIVE_PATH out_rel ${CMAKE_BINARY_DIR} "${CMAKE_CURRENT_BINARY_DIR}/${out}")
# Make input files source-relative
set(ins "")
set(ins_impdeps "")
foreach(arg ${ARGN})
list(APPEND ins ${CMAKE_CURRENT_SOURCE_DIR}/${arg})
list(APPEND ins_impdeps CXX)
list(APPEND ins_impdeps ${CMAKE_CURRENT_SOURCE_DIR}/${arg})
endforeach()
# Get local include directories for atdna
@ -50,9 +56,23 @@ macro(atdna out)
endif()
# Make target
add_custom_command(OUTPUT ${out} COMMAND $<TARGET_FILE:atdna>
ARGS ${extraargs} -o ${out} ${cdefcli} ${inccli} "-I${ATHENA_INCLUDE_DIR}"
-isystem "@CONF_CLANG_INCLUDE_DIR@" ${ins}
DEPENDS ${ins} COMMENT "Generating DNA ${out}")
if(${CMAKE_GENERATOR} STREQUAL "Ninja")
# Use Ninja's DEPFILE parser in cooperation with atdna
add_custom_command(OUTPUT ${out} COMMAND $<TARGET_FILE:atdna>
ARGS ${extraargs} -o ${out_rel} -MD -MT ${out_rel} -MF ${out_rel}.d ${cdefcli} ${inccli}
"-I${ATHENA_INCLUDE_DIR}" -isystem "${CLANG_INCLUDE_DIR}" ${ins}
DEPENDS atdna ${ins} IMPLICIT_DEPENDS ${ins_impdeps}
DEPFILE "${CMAKE_CURRENT_BINARY_DIR}/${out}.d"
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
COMMENT "Generating DNA ${out_rel}")
else()
# Use CMake's built-in dependency scanner for makefile targets
add_custom_command(OUTPUT ${out} COMMAND $<TARGET_FILE:atdna>
ARGS ${extraargs} -o ${out_rel} ${cdefcli} ${inccli}
"-I${ATHENA_INCLUDE_DIR}" -isystem "${CLANG_INCLUDE_DIR}" ${ins}
DEPENDS atdna ${ins} IMPLICIT_DEPENDS ${ins_impdeps}
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
COMMENT "Generating DNA ${out_rel}")
endif()
endmacro()

View File

@ -4,6 +4,7 @@
#include "clang/AST/RecursiveASTVisitor.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Frontend/FrontendAction.h"
#include "clang/Frontend/Utils.h"
#include "clang/Tooling/Tooling.h"
#include "clang/Lex/Preprocessor.h"
#include "clang/Sema/Sema.h"
@ -61,6 +62,14 @@ static llvm::cl::list<std::string> SystemIncludeSearchPaths("isystem",
static llvm::cl::opt<std::string> StandardCXXLib("stdlib",
llvm::cl::desc("Standard C++ library"));
static llvm::cl::opt<bool> DepFile("MD", llvm::cl::desc("Make Dependency file"));
static llvm::cl::opt<std::string> DepFileOut("MF",
llvm::cl::desc("Dependency file out path"));
static llvm::cl::list<std::string> DepFileTargets("MT",
llvm::cl::desc("Dependency file targets"));
static llvm::cl::list<std::string> SystemIncRoot("isysroot",
llvm::cl::desc("System include root"));
@ -2372,11 +2381,20 @@ class ATDNAAction : public clang::ASTFrontendAction
return {};
}
std::unique_ptr<clang::DependencyFileGenerator> TheDependencyFileGenerator;
public:
explicit ATDNAAction() {}
explicit ATDNAAction() = default;
std::unique_ptr<clang::ASTConsumer> CreateASTConsumer(clang::CompilerInstance& compiler,
llvm::StringRef /*filename*/)
{
clang::DependencyOutputOptions DepOpts;
DepOpts.OutputFile = DepFileOut;
DepOpts.Targets = DepFileTargets;
if (!DepOpts.OutputFile.empty())
TheDependencyFileGenerator.reset(
clang::DependencyFileGenerator::CreateAndAttachToPreprocessor(compiler.getPreprocessor(), DepOpts));
std::unique_ptr<StreamOut> fileout;
StreamOut* fileoutOld;
if (OutputFilename.size())
@ -2406,7 +2424,8 @@ int main(int argc, const char** argv)
args.push_back(argv[a]);
llvm::IntrusiveRefCntPtr<clang::FileManager> fman(new clang::FileManager(clang::FileSystemOptions()));
clang::tooling::ToolInvocation TI(args, new ATDNAAction, fman.get());
ATDNAAction* action = new ATDNAAction();
clang::tooling::ToolInvocation TI(args, action, fman.get());
if (!TI.run())
return 1;