Skip to content

Commit c33eb32

Browse files
author
Kevin Pyle
committed
Extract gcc version without running a test program
Change generation of print-gcc-version to preprocess, but not compile, a dummy source file containing the macro GCC_VERSION, then scan the resulting output to find the numeric value of the macro. This lets the compiler do the work of expanding the expression, but avoids running a test program. This is necessary for cross-compiler support, since cross-compiled test outputs cannot be run locally. Store the output in autogenerated-gcc-version, since it is not a runnable program.
1 parent 7990686 commit c33eb32

File tree

5 files changed

+15
-40
lines changed

5 files changed

+15
-40
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ gcc-c-api/gcc-tree.h
2727
gcc-c-api/gcc-type.h
2828
gcc-c-api/gcc-variable.h
2929
gcc-with-python.1.gz
30-
print-gcc-version
3130

3231
# Generated by Sphinx
3332
docs/_build

Makefile

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ PLUGIN_OBJECT_GENERATED_FILES:= $(patsubst %.c,%.o,$(PLUGIN_GENERATED_SOURCE_FIL
6262
PLUGIN_OBJECT_FILES:= $(PLUGIN_OBJECT_SOURCE_FILES) $(PLUGIN_OBJECT_GENERATED_FILES)
6363
GCCPLUGINS_DIR:= $(shell $(TARGET_GCC) --print-file-name=plugin)
6464

65-
GENERATOR_DEPS=cpybuilder.py wrapperbuilder.py print-gcc-version
65+
GENERATOR_DEPS=cpybuilder.py wrapperbuilder.py autogenerated-gcc-version
6666

6767
# The plugin supports both Python 2 and Python 3
6868
#
@@ -160,18 +160,20 @@ $(PLUGIN_OBJECT_GENERATED_FILES): CPPFLAGS+= $(if $(srcdir),-I$(srcdir))
160160
$(PLUGIN_OBJECT_SOURCE_FILES) $(PLUGIN_OBJECT_GENERATED_FILES): %.o: $(srcdir)%.c autogenerated-config.h $(srcdir)gcc-python.h $(LIBGCC_C_API_SO) autogenerated-EXTRA_CFLAGS.txt
161161
$(COMPILE.c) $(shell cat autogenerated-EXTRA_CFLAGS.txt) $(OUTPUT_OPTION) $<
162162

163-
print-gcc-version: print-gcc-version.c autogenerated-EXTRA_CFLAGS.txt
164-
$(CC) \
165-
$(CPPFLAGS) $(CFLAGS) \
166-
$(shell cat autogenerated-EXTRA_CFLAGS.txt) \
167-
-o $@ \
168-
$<
163+
# Use a long variable name to avoid unwanted matches. Add an explicit
164+
# -x c since gcc requires a language specifier when processing stdin.
165+
# C and C++ print the same version, so skip
166+
# autogenerated-EXTRA_CFLAGS.txt.
167+
autogenerated-gcc-version:
168+
echo gcc_version_major_minor= __GNUC__ __GNUC_MINOR__ | \
169+
$(TARGET_GCC) -x c -E - | \
170+
{ awk '/^gcc_version_major_minor=/ { print ($$2*1000)+$$3; exit 0; }' > "$@.part" && test -s "$@.part" && mv -f "$@.part" "$@"; } || { rc=$$?; $(RM) "$@.part"; exit $$rc; }
169171

170172
clean:
171173
$(RM) *.so *.o gcc-c-api/*.o autogenerated*
172174
$(RM) -r docs/_build
173175
$(RM) -f gcc-with-$(PLUGIN_NAME).1 gcc-with-$(PLUGIN_NAME).1.gz
174-
$(RM) -f print-gcc-version
176+
$(RM) -f autogenerated-gcc-version
175177
$(MAKE) -C gcc-c-api clean CC="$(CC)" TARGET_GCC="$(TARGET_GCC)"
176178
find tests -name "*.o" -delete
177179

@@ -283,7 +285,7 @@ demo: plugin
283285
json-examples: plugin
284286
$(INVOCATION_ENV_VARS) $(srcdir)./gcc-with-cpychecker -I/usr/include/python2.7 -c libcpychecker_html/test/example1/bug.c
285287

286-
test-suite: plugin print-gcc-version
288+
test-suite: plugin autogenerated-gcc-version
287289
$(INVOCATION_ENV_VARS) $(PYTHON) run-test-suite.py
288290

289291
show-ssa: plugin

gcc-python-plugin.spec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ BuildPlugin() {
145145
PYTHON=$PythonExe \
146146
PYTHON_CONFIG=$PythonConfig \
147147
PLUGIN_PYTHONPATH=%{gcc_plugins_dir}/$PluginName \
148-
plugin print-gcc-version
148+
plugin autogenerated-gcc-version
149149
popd
150150
}
151151

print-gcc-version.c

Lines changed: 0 additions & 26 deletions
This file was deleted.

testcpychecker.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@
2929
PLUGIN_NAME = os.environ.get('PLUGIN_NAME', 'python')
3030

3131
def get_gcc_version():
32-
p = Popen(['./print-gcc-version'],
33-
stdout=PIPE, stderr=PIPE)
34-
out, err = p.communicate()
32+
out = '0'
33+
with open('autogenerated-gcc-version', 'r') as f:
34+
out = f.read()
3535
return(int(out))
3636

3737
GCC_VERSION = get_gcc_version()

0 commit comments

Comments
 (0)