Skip to content

Commit ff96a71

Browse files
author
Kevin Pyle
committed
Avoid shell call for autogenerated-EXTRA_CFLAGS.txt
Instead of passing the contents of autogenerated-EXTRA_CFLAGS.txt on the gcc command line, read it in and use it to decide whether to run gcc or g++. This also produces a cleaner failure mode if autogenerated-EXTRA_CFLAGS.txt has an unexpected value.
1 parent 650ae20 commit ff96a71

File tree

3 files changed

+41
-8
lines changed

3 files changed

+41
-8
lines changed

Makefile

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,10 @@ CPPFLAGS+= -I$(GCCPLUGINS_DIR)/include -I$(GCCPLUGINS_DIR)/include/c-family -I.
116116
# Allow user to pick optimization, choose whether warnings are fatal,
117117
# and choose debugging information level.
118118
CFLAGS?=-O2 -Werror -g
119+
CXXFLAGS?=-O2 -Werror -g
119120
# Force these settings
120121
CFLAGS+= -fPIC -fno-strict-aliasing -Wall
122+
CXXFLAGS+= -fPIC -fno-strict-aliasing -Wall
121123
LIBS+= $(PYTHON_LIBS)
122124
ifneq "$(PLUGIN_PYTHONPATH)" ""
123125
CPPFLAGS+= -DPLUGIN_PYTHONPATH='"$(PLUGIN_PYTHONPATH)"'
@@ -136,23 +138,39 @@ plugin: autogenerated-config.h $(PLUGIN_DSO)
136138
#
137139
INVOCATION_ENV_VARS := LD_LIBRARY_PATH=gcc-c-api CC="$(TARGET_GCC)"
138140

141+
, := ,
142+
143+
define run-cc
144+
set -- $1; \
145+
read gcc_cxx < "autogenerated-EXTRA_CFLAGS.txt"; \
146+
if [ "$$gcc_cxx" = '-x c' ]; then \
147+
$(if $2,$2,$(CC) $(CFLAGS)) "$$@"; \
148+
elif [ "$$gcc_cxx" = '-x c++' ]; then \
149+
$(if $3,$3,$(CXX) $(CXXFLAGS)) "$$@"; \
150+
else \
151+
false; \
152+
fi
153+
endef
154+
139155
# When installing, both the plugin and libgcc-c-api.so will be installed to
140156
# $(GCCPLUGINS_DIR), so we give the plugin an RPATH of $(GCCPLUGINS_DIR)
141157
# so that it finds the libgcc-c-api.so there (to support the case of having
142158
# multiple GCCs installed)
143159
#
144160
$(PLUGIN_DSO): $(PLUGIN_OBJECT_FILES) $(LIBGCC_C_API_SO)
145-
$(CC) \
146-
$(CPPFLAGS) $(CFLAGS) $(LDFLAGS) \
161+
$(call run-cc, \
147162
-shared \
148163
$(PLUGIN_OBJECT_FILES) \
149-
-o $@ \
150164
$(LIBS) \
151-
-lgcc-c-api -Lgcc-c-api -Wl,-rpath=$(GCCPLUGINS_DIR)
165+
$(OUTPUT_OPTION) \
166+
-lgcc-c-api -Lgcc-c-api -Wl$(,)-rpath=$(GCCPLUGINS_DIR), \
167+
$(LINK.c), \
168+
$(LINK.cpp) \
169+
)
152170

153171
.PHONY: gcc-c-api/phony-stamp-gcc-api
154172
gcc-c-api/phony-stamp-gcc-api: autogenerated-EXTRA_CFLAGS.txt
155-
$(MAKE) -C gcc-c-api libgcc-c-api.so CC="$(CC)" TARGET_GCC="$(TARGET_GCC)"
173+
$(MAKE) -C gcc-c-api libgcc-c-api.so CC="$(CC)" CXX="$(CXX)" TARGET_GCC="$(TARGET_GCC)"
156174

157175
$(LIBGCC_C_API_SO): gcc-c-api/phony-stamp-gcc-api
158176

@@ -161,7 +179,7 @@ $(PLUGIN_OBJECT_GENERATED_FILES): CPPFLAGS+= $(if $(srcdir),-I$(srcdir))
161179
# This is the standard .c->.o recipe, but it needs to be stated
162180
# explicitly to support the case that $(srcdir) is not blank.
163181
$(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
164-
$(COMPILE.c) $(shell cat autogenerated-EXTRA_CFLAGS.txt) $(OUTPUT_OPTION) $<
182+
$(call run-cc,$< $(OUTPUT_OPTION),$(COMPILE.c),$(COMPILE.cpp))
165183

166184
# Use a long variable name to avoid unwanted matches. Add an explicit
167185
# -x c since gcc requires a language specifier when processing stdin.

gcc-c-api/Makefile

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,18 +80,32 @@ CPPFLAGS+= -I$(GCCPLUGINS_DIR)/include -I$(GCCPLUGINS_DIR)/include/c-family -I.
8080
# Allow user to pick optimization, choose whether warnings are fatal,
8181
# and choose debugging information level.
8282
CFLAGS?=-O2 -Werror -g
83+
CXXFLAGS?=-O2 -Werror -g
8384
# Force these settings
8485
CFLAGS+= -fPIC -fno-strict-aliasing -Wall
86+
CXXFLAGS+= -fPIC -fno-strict-aliasing -Wall
87+
88+
define run-cc
89+
set -- $1; \
90+
read gcc_cxx < "../autogenerated-EXTRA_CFLAGS.txt"; \
91+
if [ "$$gcc_cxx" = '-x c' ]; then \
92+
$(if $2,$2,$(CC) $(CFLAGS)) "$$@"; \
93+
elif [ "$$gcc_cxx" = '-x c++' ]; then \
94+
$(if $3,$3,$(CXX) $(CXXFLAGS)) "$$@"; \
95+
else \
96+
false; \
97+
fi
98+
endef
8599

86100
all: $(LIBGCC_C_API_SO)
87101

88102
$(LIBGCC_C_API_SO): $(OBJECT_FILES)
89-
$(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) -shared $^ -o $@ $(LIBS)
103+
$(call run-cc,-shared $^ $(OUTPUT_OPTION) $(LIBS),$(LINK.c),$(LINK.cpp))
90104

91105
# This is the standard .c->.o recipe, but it needs to be stated
92106
# explicitly to support the case that $(srcdir) is not blank.
93107
$(OBJECT_FILES): %.o: $(srcdir)%.c $(GENERATED_HEADERS) ../autogenerated-EXTRA_CFLAGS.txt
94-
$(COMPILE.c) $(shell cat ../autogenerated-EXTRA_CFLAGS.txt) $(OUTPUT_OPTION) $<
108+
$(call run-cc,$< $(OUTPUT_OPTION),$(COMPILE.c),$(COMPILE.cpp))
95109

96110
autogenerated-casts.c: $(SOURCE_XML) xmltypes.py generate-casts-c.py
97111
$(PYTHON) generate-casts-c.py $@

generate-config-h.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ def with_cplusplus():
8989
self.extra_compilation_args += ['-x', 'c++']
9090
def with_c():
9191
check.okmsg = 'C'
92+
self.extra_compilation_args += ['-x', 'c']
9293
auto_host_h = os.path.join(self.plugindir, 'include', 'auto-host.h')
9394
with open(auto_host_h, 'r') as f:
9495
content = f.read()

0 commit comments

Comments
 (0)