Skip to content

Commit 2a36571

Browse files
authored
Dont require __main__ for an addon (danmar#3363)
1 parent d86ff32 commit 2a36571

File tree

7 files changed

+92
-35
lines changed

7 files changed

+92
-35
lines changed

CMakeLists.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,16 @@ if (BUILD_TESTS)
3333
enable_testing()
3434
endif()
3535

36+
add_custom_target(copy_cfg ALL
37+
${CMAKE_COMMAND} -E copy_directory "${PROJECT_SOURCE_DIR}/cfg"
38+
"${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${CMAKE_CFG_INTDIR}/cfg"
39+
COMMENT "Copying cfg files to ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${CMAKE_CFG_INTDIR}")
40+
41+
add_custom_target(copy_addons ALL
42+
${CMAKE_COMMAND} -E copy_directory "${PROJECT_SOURCE_DIR}/addons"
43+
"${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${CMAKE_CFG_INTDIR}/addons"
44+
COMMENT "Copying addons files to ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${CMAKE_CFG_INTDIR}")
45+
3646
if(USE_BUNDLED_TINYXML2)
3747
message(STATUS "Using bundled version of tinyxml2")
3848
add_subdirectory(externals/tinyxml2)

addons/cppcheck.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
2+
import cppcheckdata, sys, os
3+
4+
__checkers__ = []
5+
6+
def checker(f):
7+
__checkers__.append(f)
8+
return f
9+
10+
11+
__errorid__ = ''
12+
__addon_name__ = ''
13+
def reportError(location, severity, message, errorId=None):
14+
cppcheckdata.reportError(location, severity, message, __addon_name__, errorId or __errorid__)
15+
16+
def runcheckers():
17+
# If there are no checkers then dont run
18+
if len(__checkers__) == 0:
19+
return
20+
global __addon_name__
21+
global __errorid__
22+
addon = sys.argv[0]
23+
parser = cppcheckdata.ArgumentParser()
24+
args = parser.parse_args()
25+
26+
__addon_name__ = os.path.splitext(os.path.basename(addon))[0]
27+
28+
for dumpfile in args.dumpfile:
29+
if not args.quiet:
30+
print('Checking %s...' % dumpfile)
31+
32+
data = cppcheckdata.CppcheckData(dumpfile)
33+
34+
for cfg in data.iterconfigurations():
35+
if not args.quiet:
36+
print('Checking %s, config %s...' % (dumpfile, cfg.name))
37+
for c in __checkers__:
38+
__errorid__ = c.__name__
39+
c(cfg, data)

addons/findcasts.py

Lines changed: 21 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,40 +3,31 @@
33
# Locate casts in the code
44
#
55

6-
import cppcheckdata
6+
import cppcheck
77
import sys
88

9-
for arg in sys.argv[1:]:
10-
if arg.startswith('-'):
11-
continue
9+
@cppcheck.checker
10+
def cast(cfg, data):
11+
for token in cfg.tokenlist:
12+
if token.str != '(' or not token.astOperand1 or token.astOperand2:
13+
continue
1214

13-
print('Checking %s...' % arg)
14-
data = cppcheckdata.CppcheckData(arg)
15+
# Is it a lambda?
16+
if token.astOperand1.str == '{':
17+
continue
1518

16-
for cfg in data.iterconfigurations():
17-
print('Checking %s, config %s...' % (arg, cfg.name))
18-
for token in cfg.tokenlist:
19-
if token.str != '(' or not token.astOperand1 or token.astOperand2:
20-
continue
19+
# we probably have a cast.. if there is something inside the parentheses
20+
# there is a cast. Otherwise this is a function call.
21+
typetok = token.next
22+
if not typetok.isName:
23+
continue
2124

22-
# Is it a lambda?
23-
if token.astOperand1.str == '{':
24-
continue
25+
# cast number => skip output
26+
if token.astOperand1.isNumber:
27+
continue
2528

26-
# we probably have a cast.. if there is something inside the parentheses
27-
# there is a cast. Otherwise this is a function call.
28-
typetok = token.next
29-
if not typetok.isName:
30-
continue
29+
# void cast => often used to suppress compiler warnings
30+
if typetok.str == 'void':
31+
continue
3132

32-
# cast number => skip output
33-
if token.astOperand1.isNumber:
34-
continue
35-
36-
# void cast => often used to suppress compiler warnings
37-
if typetok.str == 'void':
38-
continue
39-
40-
cppcheckdata.reportError(token, 'information', 'found a cast', 'findcasts', 'cast')
41-
42-
sys.exit(cppcheckdata.EXIT_CODE)
33+
cppcheck.reportError(token, 'information', 'found a cast')

addons/runaddon.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import cppcheckdata, cppcheck, runpy, sys, os
2+
3+
if __name__ == '__main__':
4+
addon = sys.argv[1]
5+
__addon_name__ = os.path.splitext(os.path.basename(addon))[0]
6+
sys.argv.pop(0)
7+
8+
runpy.run_path(addon, run_name='__main__')
9+
10+
# Run registered checkers
11+
cppcheck.runcheckers()
12+
sys.exit(cppcheckdata.EXIT_CODE)

cli/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ if(tinyxml2_FOUND AND NOT USE_BUNDLED_TINYXML2)
3434
target_link_libraries(cppcheck tinyxml2)
3535
endif()
3636

37+
add_dependencies(cppcheck copy_cfg)
38+
add_dependencies(cppcheck copy_addons)
39+
3740
install(TARGETS cppcheck
3841
RUNTIME DESTINATION ${CMAKE_INSTALL_FULL_BINDIR}
3942
COMPONENT applications)

lib/cppcheck.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include "version.h"
3535

3636
#include "exprengine.h"
37+
#include <string>
3738

3839
#define PICOJSON_USE_INT64
3940
#include <picojson.h>
@@ -71,6 +72,7 @@ namespace {
7172
std::string args;
7273
std::string python;
7374
bool ctu = false;
75+
std::string runScript{};
7476

7577
static std::string getFullPath(const std::string &fileName, const std::string &exename) {
7678
if (Path::fileExists(fileName))
@@ -153,6 +155,8 @@ namespace {
153155
pos2 = std::string::npos;
154156
name = scriptFile.substr(pos1, pos2 - pos1);
155157

158+
runScript = getFullPath("runaddon.py", exename);
159+
156160
return "";
157161
}
158162

@@ -291,7 +295,8 @@ static std::string executeAddon(const AddonInfo &addonInfo,
291295
}
292296

293297
const std::string fileArg = (endsWith(file, FILELIST, sizeof(FILELIST)-1) ? " --file-list " : " ") + cmdFileName(file);
294-
const std::string args = cmdFileName(addonInfo.scriptFile) + " --cli" + addonInfo.args + fileArg;
298+
const std::string args =
299+
cmdFileName(addonInfo.runScript) + " " + cmdFileName(addonInfo.scriptFile) + " --cli" + addonInfo.args + fileArg;
295300

296301
std::string result;
297302
if (!executeCommand(pythonExe, split(args), redirect, &result))

test/CMakeLists.txt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,8 @@ if (BUILD_TESTS)
3434
target_precompile_headers(testrunner PRIVATE precompiled.h)
3535
endif()
3636

37-
add_custom_target(copy_cfg ALL
38-
${CMAKE_COMMAND} -E copy_directory "${PROJECT_SOURCE_DIR}/cfg"
39-
"${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${CMAKE_CFG_INTDIR}/cfg"
40-
COMMENT "Copying cfg files to ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${CMAKE_CFG_INTDIR}")
4137
add_dependencies(testrunner copy_cfg)
38+
add_dependencies(testrunner copy_addons)
4239

4340
if (LIBXML2_XMLLINT_EXECUTABLE)
4441
# TODO: get rid of the copy

0 commit comments

Comments
 (0)