Skip to content

Commit 57ee08a

Browse files
committed
Review amendments
1 parent ade1dc9 commit 57ee08a

File tree

5 files changed

+82
-49
lines changed

5 files changed

+82
-49
lines changed

CMakeLists.txt

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,4 @@ add_custom_target(models-library ALL
112112
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/benchmarks/LIBRARIES/models/model
113113
)
114114

115-
add_custom_target(env-model-generator ALL
116-
COMMAND yarn install
117-
COMMAND yarn run build
118-
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/env-model-generator
119-
)
120-
121-
115+
add_subdirectory(env-model-generator)

driver/mkbench.py

Lines changed: 46 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import os
22
import re
3-
import subprocess
3+
import sys
44

55
import analyser
66
import filecmp
@@ -10,8 +10,6 @@
1010
import utility
1111
import subprocess
1212

13-
import xml.dom.minidom
14-
1513
class CollectedJavaBinaries:
1614
def __init__(self):
1715
self.class_files = []
@@ -88,16 +86,16 @@ def run_di_model_generation(configuration, cmdline):
8886
# If we don't have anything vaguely looking like a spring library being used, just skip generation.
8987
if not next((lib for lib in cmdline.libraries if re.search("spring-[a-zA-Z]*-[0-9]*\.[0-9]*", lib)), None) \
9088
or cmdline.skip_di_generation:
91-
print("No Spring Framework libraries passed in, skipping DI generation.")
92-
return
89+
print("No Spring Framework libraries passed in, cannot generate any DI-related code.", file=sys.stderr)
90+
return None
9391

9492
di_output_file_path = configuration["diConfigurationPath"]
9593
detected_entry_points_path = configuration["detectedEntryPointsPath"]
96-
di_starting_script = os.path.abspath(os.path.join(os.path.dirname(os.path.realpath(__file__)), "..", "env-model-generator", "built", "env-model-generator.js"))
94+
di_starting_script = os.path.abspath(os.path.join(os.path.dirname(os.path.realpath(__file__)), "..", "env-model-generator", "env-model-generator"))
9795

9896
# Right now we're only using our metadata generated files, but this should also include XML.
9997
spring_files = []
100-
if os.path.exists(di_output_file_path) and os.path.isfile(di_output_file_path):
98+
if os.path.isfile(di_output_file_path):
10199
spring_files.append(di_output_file_path)
102100

103101
# Clear down any existing files.
@@ -106,18 +104,25 @@ def run_di_model_generation(configuration, cmdline):
106104
os.rmdir(generated_source_file_path)
107105
os.mkdir(generated_source_file_path)
108106

109-
di_generation_commandline = ["node", di_starting_script, "".join(spring_files[:1]), "--input-file", " ".join(spring_files[1:]), "--output-path", generated_source_file_path, "--entry-points-input-file", detected_entry_points_path]
107+
primary_input_file = "".join(spring_files[:1])
108+
if not os.path.isfile(primary_input_file):
109+
print("No files available to generate DI overlays.")
110+
return None
110111

111-
print("Running commandline: " + " ".join(di_generation_commandline))
112-
di_generation_result = subprocess.run(di_generation_commandline, stderr=subprocess.STDOUT)
113-
if di_generation_result.returncode != 0:
114-
print("DI generation failed.")
115-
return
112+
di_generation_commandline = [di_starting_script, primary_input_file, "--output-path", generated_source_file_path, "--entry-points-input-file", detected_entry_points_path]
113+
114+
additional_spring_files = spring_files[1:]
115+
if additional_spring_files:
116+
di_generation_commandline.extend(["--input-file"] + additional_spring_files)
117+
118+
di_generation_result = utility.call(di_generation_commandline, "DI model generator", cmdline.verbosity, stderr=subprocess.STDOUT)
119+
if di_generation_result != 0:
120+
return None
116121

117122
# Quick check to see if we have any output files.
118123
if not next((file for root, dirs, files in os.walk(generated_source_file_path) for file in files), None):
119124
print("No Java source files emitted by DI generation.")
120-
return
125+
return None
121126

122127
# Clear down yet more existing files.
123128
java_binaries_path = os.path.join(cmdline.common_dir, "GENERATED_BINARIES")
@@ -130,29 +135,35 @@ def run_di_model_generation(configuration, cmdline):
130135
class_paths = cmdline.libraries + [os.path.join(cmdline.common_dir, "collected_classes.jar")]
131136

132137
generated_jar_path = os.path.join(cmdline.common_dir, "DI-models.jar")
133-
ant_build_file = ('<project default="compile">'
134-
'<target name="compile">'
135-
'<javac includeantruntime="true" srcdir="' + generated_source_file_path + '" destdir="' + java_binaries_path + '">'
136-
+ '<classpath>' + "".join(["<pathelement location=\"" + path + "\"/>" for path in class_paths]) + '</classpath>' +
137-
'</javac>'
138-
'<jar destfile="' + generated_jar_path + '" basedir="' + java_binaries_path + '"/>'
139-
'</target>'
140-
'</project>')
141-
142-
# Attempt to prettify output a little.
143-
ant_build_file = xml.dom.minidom.parseString(ant_build_file).toprettyxml()
138+
139+
ant_format_arguments = {
140+
"generated_source_file_path": generated_source_file_path,
141+
"class_paths": os.linesep.join([" <pathelement location=\"" + path + "\"/>" for path in class_paths]),
142+
"generated_jar_path": generated_jar_path,
143+
"java_binaries_path": java_binaries_path
144+
}
145+
146+
ant_build_statement = """
147+
<project default="compile">
148+
<target name="compile">
149+
<javac includeantruntime="true" srcdir="{generated_source_file_path}" destdir="{java_binaries_path}">
150+
<classpath>
151+
{class_paths}
152+
</classpath>
153+
</javac>
154+
<jar destfile="{generated_jar_path}" basedir="{java_binaries_path}"/>
155+
</target>
156+
</project>
157+
""".format(**ant_format_arguments)
144158

145159
ant_build_path = os.path.join(generated_source_file_path, "build.xml")
146160
with open(ant_build_path, 'x') as ant_xml_file:
147-
ant_xml_file.write(ant_build_file)
148-
149-
javac_commandline = ["ant", "-file", ant_build_path, "-lib", os.pathsep.join(class_paths)]
161+
ant_xml_file.write(ant_build_statement)
150162

151-
print("Running commandline: " + " ".join(javac_commandline))
152-
javac_result = subprocess.run(javac_commandline, stderr=subprocess.STDOUT)
153-
if javac_result.returncode != 0:
163+
javac_result = utility.call(["ant", "-file", ant_build_path], "Java Compiler for DI model overlays", cmdline.verbosity, stderr=subprocess.STDOUT)
164+
if javac_result != 0:
154165
print("Java compilation of DI-generated source failed.")
155-
return
166+
return None
156167

157168
return generated_jar_path
158169

@@ -259,9 +270,11 @@ def collect_java_binaries(cmdline):
259270
# If DI generation has run we want to target its output jar as our starting point.
260271
target_binary = java_binaries.jar_file
261272
if di_output_path:
262-
class_paths.append(java_binaries.jar_file)
273+
class_paths.append(target_binary)
263274
class_paths.append(di_output_path) # Appended because we need overlay classes too.
264275
target_binary = di_output_path
276+
else:
277+
print("DI generation failed, continuing without synthetic entry points.", file=sys.stderr)
265278

266279
with open(entry_points_file) as ep_config_file:
267280
ep_config = json.load(ep_config_file)

driver/run.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ def create_parser():
196196
parser.add_argument("--run-scan", type=str, const='', nargs="?",
197197
help="Flag for whether a security scan should be run. Target will be the results folder.")
198198
parser.add_argument("--skip-di-generation", action='store_true',
199-
help="Turns off DI overlay generation. This overrides the automatic detection of this feature.")
199+
help="Turns off DI overlay generation. This overrides the automatic detection of DI usage.")
200200

201201
return parser
202202

@@ -434,14 +434,21 @@ def __main():
434434
analyser.get_missing_binary_error_message())
435435
return
436436

437-
# If we've been passed a path with * on the end, unpack the .jar files in
438-
# that directory (and children).
439-
detected_jar_files = [os.path.join(root, file)
440-
for path in cmdline.libraries if not os.path.isdir(path) and path.endswith("*") and os.path.isdir(path.strip('*'))
441-
for root, directories, files in os.walk(path.strip('*'))
442-
for file in files if re.search(".*\.jar$", file)]
437+
def extract_wildcard_paths(path):
438+
"""
439+
If our path is a directory and has a trailing * that isn't related
440+
to any concrete item, recursively find all .jar files in that folder
441+
and its children.
442+
"""
443+
if not os.path.exists(path) and path.endswith("*") and os.path.isdir(path.strip('*')):
444+
return [os.path.join(root, file)
445+
for root, directories, files in os.walk(path.strip('*'))
446+
for file in files if re.search(".*\.jar$", file)]
447+
else:
448+
return [path]
443449

444-
cmdline.libraries = detected_jar_files + [path for path in cmdline.libraries if not (not os.path.isdir(path) and path.endswith("*"))]
450+
# Transform incoming libraries.
451+
cmdline.libraries = [path for library_path in cmdline.libraries for path in extract_wildcard_paths(library_path)]
445452

446453
common_libraries = _get_common_libraries(cmdline.models_library_location)
447454

env-model-generator/CMakeLists.txt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
project(SECURITY_ANALYSER)
2+
3+
file(GLOB_RECURSE typescript_files "${CMAKE_CURRENT_SOURCE_DIR}/src/*.ts")
4+
5+
string(REGEX REPLACE "\\.ts($|;)" ".js\\1" javascript_files "${typescript_files}")
6+
string(REPLACE "/src/" "/built/" javascript_files "${javascript_files}")
7+
8+
add_custom_target(env-model-generator ALL
9+
DEPENDS ${javascript_files}
10+
)
11+
12+
add_custom_command(
13+
COMMENT "Recompiling env-model-generator."
14+
OUTPUT ${javascript_files}
15+
DEPENDS ${typescript_files};package.json
16+
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
17+
COMMAND yarn install
18+
COMMAND yarn run build
19+
)

src/java-class-info/entry_point.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ jsont entry_pointt::to_json()
6161
method_obj[json_namest::name] = json_stringt(method.name);
6262
method_obj[json_namest::signature] = json_stringt(method.signature);
6363
method_obj[json_namest::synthetic_method_name] = json_stringt(
64-
boost::replace_all_copy(class_name, ".", "") + "_" + method.name);
64+
boost::replace_all_copy(class_name, ".", "_") + "_" + method.name);
6565

6666
return json_entry_point;
6767
}

0 commit comments

Comments
 (0)