Dont require __main__ for an addon (#3363)

This commit is contained in:
Paul Fultz II 2021-08-12 13:17:51 -05:00 committed by GitHub
parent d86ff326e6
commit 2a3657154b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 92 additions and 35 deletions

View File

@ -33,6 +33,16 @@ if (BUILD_TESTS)
enable_testing() enable_testing()
endif() endif()
add_custom_target(copy_cfg ALL
${CMAKE_COMMAND} -E copy_directory "${PROJECT_SOURCE_DIR}/cfg"
"${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${CMAKE_CFG_INTDIR}/cfg"
COMMENT "Copying cfg files to ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${CMAKE_CFG_INTDIR}")
add_custom_target(copy_addons ALL
${CMAKE_COMMAND} -E copy_directory "${PROJECT_SOURCE_DIR}/addons"
"${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${CMAKE_CFG_INTDIR}/addons"
COMMENT "Copying addons files to ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${CMAKE_CFG_INTDIR}")
if(USE_BUNDLED_TINYXML2) if(USE_BUNDLED_TINYXML2)
message(STATUS "Using bundled version of tinyxml2") message(STATUS "Using bundled version of tinyxml2")
add_subdirectory(externals/tinyxml2) add_subdirectory(externals/tinyxml2)

39
addons/cppcheck.py Normal file
View File

@ -0,0 +1,39 @@
import cppcheckdata, sys, os
__checkers__ = []
def checker(f):
__checkers__.append(f)
return f
__errorid__ = ''
__addon_name__ = ''
def reportError(location, severity, message, errorId=None):
cppcheckdata.reportError(location, severity, message, __addon_name__, errorId or __errorid__)
def runcheckers():
# If there are no checkers then dont run
if len(__checkers__) == 0:
return
global __addon_name__
global __errorid__
addon = sys.argv[0]
parser = cppcheckdata.ArgumentParser()
args = parser.parse_args()
__addon_name__ = os.path.splitext(os.path.basename(addon))[0]
for dumpfile in args.dumpfile:
if not args.quiet:
print('Checking %s...' % dumpfile)
data = cppcheckdata.CppcheckData(dumpfile)
for cfg in data.iterconfigurations():
if not args.quiet:
print('Checking %s, config %s...' % (dumpfile, cfg.name))
for c in __checkers__:
__errorid__ = c.__name__
c(cfg, data)

View File

@ -3,18 +3,11 @@
# Locate casts in the code # Locate casts in the code
# #
import cppcheckdata import cppcheck
import sys import sys
for arg in sys.argv[1:]: @cppcheck.checker
if arg.startswith('-'): def cast(cfg, data):
continue
print('Checking %s...' % arg)
data = cppcheckdata.CppcheckData(arg)
for cfg in data.iterconfigurations():
print('Checking %s, config %s...' % (arg, cfg.name))
for token in cfg.tokenlist: for token in cfg.tokenlist:
if token.str != '(' or not token.astOperand1 or token.astOperand2: if token.str != '(' or not token.astOperand1 or token.astOperand2:
continue continue
@ -37,6 +30,4 @@ for arg in sys.argv[1:]:
if typetok.str == 'void': if typetok.str == 'void':
continue continue
cppcheckdata.reportError(token, 'information', 'found a cast', 'findcasts', 'cast') cppcheck.reportError(token, 'information', 'found a cast')
sys.exit(cppcheckdata.EXIT_CODE)

12
addons/runaddon.py Normal file
View File

@ -0,0 +1,12 @@
import cppcheckdata, cppcheck, runpy, sys, os
if __name__ == '__main__':
addon = sys.argv[1]
__addon_name__ = os.path.splitext(os.path.basename(addon))[0]
sys.argv.pop(0)
runpy.run_path(addon, run_name='__main__')
# Run registered checkers
cppcheck.runcheckers()
sys.exit(cppcheckdata.EXIT_CODE)

View File

@ -34,6 +34,9 @@ if(tinyxml2_FOUND AND NOT USE_BUNDLED_TINYXML2)
target_link_libraries(cppcheck tinyxml2) target_link_libraries(cppcheck tinyxml2)
endif() endif()
add_dependencies(cppcheck copy_cfg)
add_dependencies(cppcheck copy_addons)
install(TARGETS cppcheck install(TARGETS cppcheck
RUNTIME DESTINATION ${CMAKE_INSTALL_FULL_BINDIR} RUNTIME DESTINATION ${CMAKE_INSTALL_FULL_BINDIR}
COMPONENT applications) COMPONENT applications)

View File

@ -34,6 +34,7 @@
#include "version.h" #include "version.h"
#include "exprengine.h" #include "exprengine.h"
#include <string>
#define PICOJSON_USE_INT64 #define PICOJSON_USE_INT64
#include <picojson.h> #include <picojson.h>
@ -71,6 +72,7 @@ namespace {
std::string args; std::string args;
std::string python; std::string python;
bool ctu = false; bool ctu = false;
std::string runScript{};
static std::string getFullPath(const std::string &fileName, const std::string &exename) { static std::string getFullPath(const std::string &fileName, const std::string &exename) {
if (Path::fileExists(fileName)) if (Path::fileExists(fileName))
@ -153,6 +155,8 @@ namespace {
pos2 = std::string::npos; pos2 = std::string::npos;
name = scriptFile.substr(pos1, pos2 - pos1); name = scriptFile.substr(pos1, pos2 - pos1);
runScript = getFullPath("runaddon.py", exename);
return ""; return "";
} }
@ -291,7 +295,8 @@ static std::string executeAddon(const AddonInfo &addonInfo,
} }
const std::string fileArg = (endsWith(file, FILELIST, sizeof(FILELIST)-1) ? " --file-list " : " ") + cmdFileName(file); const std::string fileArg = (endsWith(file, FILELIST, sizeof(FILELIST)-1) ? " --file-list " : " ") + cmdFileName(file);
const std::string args = cmdFileName(addonInfo.scriptFile) + " --cli" + addonInfo.args + fileArg; const std::string args =
cmdFileName(addonInfo.runScript) + " " + cmdFileName(addonInfo.scriptFile) + " --cli" + addonInfo.args + fileArg;
std::string result; std::string result;
if (!executeCommand(pythonExe, split(args), redirect, &result)) if (!executeCommand(pythonExe, split(args), redirect, &result))

View File

@ -34,11 +34,8 @@ if (BUILD_TESTS)
target_precompile_headers(testrunner PRIVATE precompiled.h) target_precompile_headers(testrunner PRIVATE precompiled.h)
endif() endif()
add_custom_target(copy_cfg ALL
${CMAKE_COMMAND} -E copy_directory "${PROJECT_SOURCE_DIR}/cfg"
"${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${CMAKE_CFG_INTDIR}/cfg"
COMMENT "Copying cfg files to ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${CMAKE_CFG_INTDIR}")
add_dependencies(testrunner copy_cfg) add_dependencies(testrunner copy_cfg)
add_dependencies(testrunner copy_addons)
if (LIBXML2_XMLLINT_EXECUTABLE) if (LIBXML2_XMLLINT_EXECUTABLE)
# TODO: get rid of the copy # TODO: get rid of the copy