Skip to content

Commit ef76811

Browse files
committed
Add flags to build.py: --enable-profiling and --enable-line-tracing (cztomczak#424)
1 parent bf0e6e3 commit ef76811

File tree

2 files changed

+70
-16
lines changed

2 files changed

+70
-16
lines changed

tools/build.py

Lines changed: 52 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,14 @@
2222
[--hello-world]
2323
2424
Options:
25-
VERSION Version number eg. 50.0
26-
--no-run-examples Do not run examples after build, only unit tests
27-
--fast Fast mode
28-
--clean Clean C++ projects build files on Linux/Mac
29-
--kivy Run only Kivy example
30-
--hello-world Run only Hello World example
25+
VERSION Version number eg. 50.0
26+
--no-run-examples Do not run examples after build, only unit tests
27+
--fast Fast mode
28+
--clean Clean C++ projects build files on Linux/Mac
29+
--kivy Run only Kivy example
30+
--hello-world Run only Hello World example
31+
--enable-profiling Enable cProfile profiling
32+
--enable-line-tracing Enable cProfile line tracing
3133
"""
3234

3335
# --rebuild-cpp Force rebuild of .vcproj C++ projects (DISABLED)
@@ -82,6 +84,8 @@
8284
KIVY_FLAG = False
8385
HELLO_WORLD_FLAG = False
8486
REBUILD_CPP = False
87+
ENABLE_PROFILING = False
88+
ENABLE_LINE_TRACING = False
8589

8690
# First run
8791
FIRST_RUN = False
@@ -122,48 +126,72 @@ def command_line_args():
122126
REBUILD_CPP, VERSION, NO_RUN_EXAMPLES
123127

124128
VERSION = get_version_from_command_line_args(__file__)
129+
# Other scripts called by this script expect that version number
130+
# is available in sys.argv, so don't remove it like it's done
131+
# for all other args starting with "--".
125132
if not VERSION:
126133
print(__doc__)
127134
sys.exit(1)
128135

129136
print("[build.py] Parse command line arguments")
130137

131-
# --no-run-examples
132138
if "--no-run-examples" in sys.argv:
133139
NO_RUN_EXAMPLES = True
134140
print("[build.py] Running examples disabled (--no-run-examples)")
141+
sys.argv.remove("--no-run-examples")
135142

136-
# -- debug
137143
if "--debug" in sys.argv:
138144
DEBUG_FLAG = True
139145
print("[build.py] DEBUG mode On")
146+
sys.argv.remove("--debug")
140147

141-
# --fast
142148
if "--fast" in sys.argv:
143149
# Fast mode doesn't delete C++ .o .a files.
144150
# Fast mode also disables optimization flags in setup/setup.py .
145151
FAST_FLAG = True
146152
print("[build.py] FAST mode On")
153+
sys.argv.remove("--fast")
147154

148-
# --clean
149155
if "--clean" in sys.argv:
150156
CLEAN_FLAG = True
157+
sys.argv.remove("--clean")
151158

152-
# --kivy
153159
if "--kivy" in sys.argv:
154160
KIVY_FLAG = True
155161
print("[build.py] KIVY example")
162+
sys.argv.remove("--kivy")
156163

157-
# --kivy
158164
if "--hello-world" in sys.argv:
159165
HELLO_WORLD_FLAG = True
160166
print("[build.py] HELLO WORLD example")
167+
sys.argv.remove("--hello-world")
161168

162-
# --rebuild-cpp
163169
# Rebuild c++ projects
164170
if "--rebuild-cpp" in sys.argv:
165171
REBUILD_CPP = True
166172
print("[build.py] REBUILD_CPP mode enabled")
173+
sys.argv.remove("--rebuild-cpp")
174+
175+
global ENABLE_PROFILING
176+
if "--enable-profiling" in sys.argv:
177+
print("[build.py] cProfile profiling enabled")
178+
ENABLE_PROFILING = True
179+
sys.argv.remove("--enable-profiling")
180+
181+
global ENABLE_LINE_TRACING
182+
if "--enable-line-tracing" in sys.argv:
183+
print("[build.py] cProfile line tracing enabled")
184+
ENABLE_LINE_TRACING = True
185+
sys.argv.remove("--enable-line-tracing")
186+
187+
for arg in sys.argv:
188+
if arg.startswith("--"):
189+
print("ERROR: invalid arg {0}".format(arg))
190+
sys.exit(1)
191+
192+
if len(sys.argv) <= 1:
193+
print(__doc__)
194+
sys.exit(1)
167195

168196
print("[build.py] VERSION=%s" % VERSION)
169197

@@ -726,8 +754,18 @@ def build_cefpython_module():
726754

727755
os.chdir(BUILD_CEFPYTHON)
728756

757+
enable_profiling = ""
758+
if ENABLE_PROFILING:
759+
enable_profiling = "--enable-profiling"
760+
enable_line_tracing = ""
761+
if ENABLE_LINE_TRACING:
762+
enable_line_tracing = "--enable-line-tracing"
763+
729764
command = ("\"{python}\" {tools_dir}/cython_setup.py build_ext"
730-
.format(python=sys.executable, tools_dir=TOOLS_DIR))
765+
" {enable_profiling} {enable_line_tracing}"
766+
.format(python=sys.executable, tools_dir=TOOLS_DIR,
767+
enable_profiling=enable_profiling,
768+
enable_line_tracing=enable_line_tracing))
731769
if FAST_FLAG:
732770
command += " --fast"
733771
ret = subprocess.call(command, shell=True)

tools/cython_setup.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,10 @@ def generate_extern_c_macro_definition(self, code):
6565
generate_extern_c_macro_definition)
6666

6767

68-
# Constants
68+
# Command line args
6969
FAST_FLAG = False
70+
ENABLE_PROFILING = False
71+
ENABLE_LINE_TRACING = False
7072

7173
# Cython options. Stop on first error, otherwise hundreds
7274
# of errors appear in the console.
@@ -80,12 +82,24 @@ def main():
8082
print("[cython_setup.py] Cython version: %s" % Cython.__version__)
8183

8284
global FAST_FLAG
83-
if len(sys.argv) > 1 and "--fast" in sys.argv:
85+
if "--fast" in sys.argv:
8486
# Fast mode disables optimization flags
8587
print("[cython_setup.py] FAST mode enabled")
8688
FAST_FLAG = True
8789
sys.argv.remove("--fast")
8890

91+
global ENABLE_PROFILING
92+
if "--enable-profiling" in sys.argv:
93+
print("[cython_setup.py] cProfile profiling enabled")
94+
ENABLE_PROFILING = True
95+
sys.argv.remove("--enable-profiling")
96+
97+
global ENABLE_LINE_TRACING
98+
if "--enable-line-tracing" in sys.argv:
99+
print("[cython_setup.py] cProfile line tracing enabled")
100+
ENABLE_LINE_TRACING = True
101+
sys.argv.remove("--enable-line-tracing")
102+
89103
if len(sys.argv) <= 1:
90104
print(__doc__)
91105
sys.exit(1)
@@ -393,6 +407,8 @@ def get_ext_modules(options):
393407
# Any conversion to unicode must be explicit using .decode().
394408
"c_string_type": "bytes",
395409
"c_string_encoding": "utf-8",
410+
"profile": ENABLE_PROFILING,
411+
"linetrace": ENABLE_LINE_TRACING,
396412
},
397413

398414
language="c++",

0 commit comments

Comments
 (0)