Skip to content
Advertisement

How does this Makefile work?

This is the makefile:

GOOGLE_APPLICATION_CREDENTIALS=/home/roman/Documents/Google_Cloud/SpeechRec.json
GOOGLEAPIS_GENS_PATH=/home/roman/Downloads/GoogleAPIs/googleapis/gens

GOOGLEAPIS_GENS_PATH ?= $(HOME)/gitrepos/googleapis/gens
GOOGLEAPIS_API_CCS = $(shell find $(GOOGLEAPIS_GENS_PATH)/google/api 
    -name '*.pb.cc')
GOOGLEAPIS_RPC_CCS = $(shell find $(GOOGLEAPIS_GENS_PATH)/google/rpc 
    -name '*.pb.cc')
GOOGLEAPIS_SPEECH_CCS = $(shell find 
    $(GOOGLEAPIS_GENS_PATH)/google/cloud/speech -name '*.pb.cc')
GOOGLEAPIS_LONGRUNNING_CCS = $(shell find 
    $(GOOGLEAPIS_GENS_PATH)/google/longrunning -name '*.pb.cc')
GOOGLEAPIS_CCS = $(GOOGLEAPIS_API_CCS) $(GOOGLEAPIS_RPC_CCS) 
    $(GOOGLEAPIS_LONGRUNNING_CCS) $(GOOGLEAPIS_SPEECH_CCS)

HOST_SYSTEM = $(shell uname | cut -f 1 -d_)
SYSTEM ?= $(HOST_SYSTEM)
CXX = g++
CPPFLAGS += -I/usr/local/include -pthread -I$(GOOGLEAPIS_GENS_PATH) -g -O0
CXXFLAGS += -std=c++11 -g -O0
ifeq ($(SYSTEM),Darwin)
LDFLAGS += -L/usr/local/lib `pkg-config --libs grpc++ grpc`       
           -lgrpc++_reflection                                    
           -lprotobuf -lpthread -ldl
else
LDFLAGS += -L/usr/local/lib `pkg-config --libs grpc++ grpc`       
           -Wl,--no-as-needed -lgrpc++_reflection -Wl,--as-needed 
           -lprotobuf -lpthread -ldl
endif

.PHONY: all

#all: transcribe async_transcribe streaming_transcribe streaming_transcribe_singlethread
all: streaming_transcribe

googleapis.ar: $(GOOGLEAPIS_CCS:.cc=.o)
    ar r $@ $?


streaming_transcribe: streaming_transcribe.o parse_arguments.o googleapis.ar
    $(CXX) $^ $(LDFLAGS) -o $@

It produces log result:

23:20:19 **** Build of configuration Build (GNU) for project GoogleSpeechApi ****
make all 
g++ -std=c++11 -g -O0 -I/usr/local/include -pthread -I/home/roman/Downloads/GoogleAPIs/googleapis/gens -g -O0  -c -o streaming_transcribe.o streaming_transcribe.cc
g++ -std=c++11 -g -O0 -I/usr/local/include -pthread -I/home/roman/Downloads/GoogleAPIs/googleapis/gens -g -O0  -c -o parse_arguments.o parse_arguments.cc
g++ -std=c++11 -g -O0 -I/usr/local/include -pthread -I/home/roman/Downloads/GoogleAPIs/googleapis/gens -g -O0  -c -o /home/roman/Downloads/GoogleAPIs/googleapis/gens/google/api/log.grpc.pb.o /home/roman/Downloads/GoogleAPIs/googleapis/gens/google/api/log.grpc.pb.cc
g++ -std=c++11 -g -O0 -I/usr/local/include -pthread -I/home/roman/Downloads/GoogleAPIs/googleapis/gens -g -O0  -c -o /home/roman/Downloads/GoogleAPIs/googleapis/gens/google/api/experimental/authorization_config.pb.o /home/roman/Downloads/GoogleAPIs/googleapis/gens/google/api/experimental/authorization_config.pb.cc
g++ -std=c++11 -g -O0 -I/usr/local/include -pthread -I/home/roman/Downloads/GoogleAPIs/googleapis/gens -g -O0  -c -o /home/roman/Downloads/GoogleAPIs/googleapis/gens/google/api/experimental/authorization_config.grpc.pb.o /home/roman/Downloads/GoogleAPIs/googleapis/gens/google/api/experimental/authorization_config.grpc.pb.cc
g++ -std=c++11 -g -O0 -I/usr/local/include -pthread -I/home/roman/Downloads/GoogleAPIs/googleapis/gens -g -O0  -c -o /home/roman/Downloads/GoogleAPIs/googleapis/gens/google/api/experimental/experimental.grpc.pb.o /home/roman/Downloads/GoogleAPIs/googleapis/gens/google/api/experimental/experimental.grpc.pb.cc
g++ -std=c++11 -g -O0 -I/usr/local/include -pthread -I/home/roman/Downloads/GoogleAPIs/googleapis/gens -g -O0  -c -o /home/roman/Downloads/GoogleAPIs/googleapis/gens/google/api/experimental/experimental.pb.o /home/roman/Downloads/GoogleAPIs/googleapis/gens/google/api/experimental/experimental.pb.cc
g++ -std=c++11 -g -O0 -I/usr/local/include -pthread -I/home/roman/Downloads/GoogleAPIs/googleapis/gens -g -O0  -c -o /home/roman/Downloads/GoogleAPIs/googleapis/gens/google/api/servicemanagement/v1/resources.grpc.pb.o /home/roman/Downloads/GoogleAPIs/googleapis/gens/google/api/servicemanagement/v1/resources.grpc.pb.cc
g++ -std=c++11 -g -O0 -I/usr/local/include -pthread -I/home/roman/Downloads/GoogleAPIs/googleapis/gens -g -O0  -c -o /home/roman/Downloads/GoogleAPIs/googleapis/gens/google/api/servicemanagement/v1/servicemanager.grpc.pb.o /home/roman/Downloads/GoogleAPIs/googleapis/gens/google/api/servicemanagement/v1/servicemanager.grpc.pb.cc
g++ -std=c++11 -g -O0 -I/usr/local/include -pthread -I/home/roman/Downloads/GoogleAPIs/googleapis/gens -g -O0  -c -o /home/roman/Downloads/GoogleAPIs/googleapis/gens/google/api/servicemanagement/v1/servicemanager.pb.o /home/roman/Downloads/GoogleAPIs/googleapis/gens/google/api/servicemanagement/v1/servicemanager.pb.cc
g++ -std=c++11 -g -O0 -I/usr/local/include -pthread -I/home/roman/Downloads/GoogleAPIs/googleapis/gens -g -O0  -c -o /home/roman/Downloads/GoogleAPIs/googleapis/gens/google/api/servicemanagement/v1/resources.pb.o /home/roman/Downloads/GoogleAPIs/googleapis/gens/google/api/servicemanagement/v1/resources.pb.cc
g++ -std=c++11 -g -O0 -I/usr/local/include -pthread -I/home/roman/Downloads/GoogleAPIs/googleapis/gens -g -O0  -c -o /home/roman/Downloads/GoogleAPIs/googleapis/gens/google/api/httpbody.grpc.pb.o /home/roman/Downloads/GoogleAPIs/googleapis/gens/google/api/httpbody.grpc.pb.cc

ar r googleapis.ar /home/roman/Downloads/GoogleAPIs/googleapis/gens/google/api/log.grpc.pb.o /home/roman/Downloads/GoogleAPIs/googleapis/gens/google/api/experimental/authorization_config.pb.o /home/roman/Downloads/GoogleAPIs/googleapis/gens/google/api/experimental/authorization_config.grpc.pb.o /home/roman/Downloads/GoogleAPIs/googleapis/gens/google/api/experimental/experimental.grpc.pb.o /home/roman/Downloads/GoogleAPIs/googleapis/gens/google/api/experimental/experimental.pb.o /home/roman/Downloads/GoogleAPIs/googleapis/gens/google/api/servicemanagement/v1/resources.grpc.pb.o /home/roman/Downloads/GoogleAPIs/googleapis/gens/google/api/servicemanagement/v1/servicemanager.grpc.pb.o /home/roman/Downloads/GoogleAPIs/googleapis/gens/google/api/servicemanagement/v1/servicemanager.pb.o /home/roman/Downloads/GoogleAPIs/googleapis/gens/google/api/servicemanagement/v1/resources.pb.o /home/roman/Downloads/GoogleAPIs/googleapis/gens/google/api/httpbody.grpc.pb.o /home/roman/Downloads/GoogleAPIs/googleapis/gens/google/api/label.pb.o /home/roman/Downloads/GoogleAPIs/googleapis/gens/google/api/billing.grpc.pb.o /home/roman/Downloads/GoogleAPIs/googleapis/gens/google/api/servicecontrol/v1/quota_controller.pb.o /home/roman/Downloads/GoogleAPIs/googleapis/gens/google/api/servicecontrol/v1/service_controller.pb.o /home/roman/Downloads/GoogleAPIs/googleapis/gens/google/api/servicecontrol/v1/log_entry.pb.o /home/roman/Downloads/GoogleAPIs/googleapis/gens/google/api/servicecontrol/v1/metric_value.grpc.pb.o /home/roman/Downloads/GoogleAPIs/googleapis/gens/google/api/servicecontrol/v1/operation.pb.o /home/roman/Downloads/GoogleAPIs/googleapis/gens/google/api/servicecontrol/v1/operation.grpc.pb.o /home/roman/Downloads/GoogleAPIs/googleapis/gens/google/api/servicecontrol/v1/check_error.grpc.pb.o /home/roman/Downloads/GoogleAPIs/googleapis/gens/google/api/servicecontrol/v1/quota_controller.grpc.pb.o /home/roman/Downloads/GoogleAPIs/googleapis/gens/google/api/servicecontrol/v1/log_entry.grpc.pb.o /home/roman/Downloads/GoogleAPIs/googleapis/gens/google/api/servicecontrol/v1/check_error.pb.o /home/roman/Downloads/GoogleAPIs/googleapis/gens/google/api/servicecontrol/v1/metric_value.pb.o /home/roman/Downloads/GoogleAPIs/googleapis/gens/google/api/servicecontrol/v1/distribution.pb.o /home/roman/Downloads/GoogleAPIs/googleapis/gens/google/api/servicecontrol/v1/distribution.grpc.pb.o /home/roman/Downloads/GoogleAPIs/googleapis/gens/google/api/servicecontrol/v1/service_controller.grpc.pb.o /home/roman/Downloads/GoogleAPIs/googleapis/gens/google/api/httpbody.pb.o /home/roman/Downloads/GoogleAPIs/googleapis/gens/google/api/consumer.pb.o /home/roman/Downloads/GoogleAPIs/googleapis/gens/google/api/billing.pb.o /home/roman/Downloads/GoogleAPIs/googleapis/gens/google/api/config_change.pb.o /home/roman/Downloads/GoogleAPIs/googleapis/gens/google/api/monitored_resource.grpc.pb.o /home/roman/Downloads/GoogleAPIs/googleapis/gens/google/api/http.grpc.pb.o /home/roman/Downloads/GoogleAPIs/googleapis/gens/google/api/backend.pb.o /home/roman/Downloads/GoogleAPIs/googleapis/gens/google/api/documentation.grpc.pb.o /home/roman/Downloads/GoogleAPIs/googleapis/gens/google/api/metric.grpc.pb.o /home/roman/Downloads/GoogleAPIs/googleapis/gens/google/api/system_parameter.grpc.pb.o /home/roman/Downloads/GoogleAPIs/googleapis/gens/google/api/consumer.grpc.pb.o /home/roman/Downloads/GoogleAPIs/googleapis/gens/google/api/control.pb.o /home/roman/Downloads/GoogleAPIs/googleapis/gens/google/api/documentation.pb.o /home/roman/Downloads/GoogleAPIs/googleapis/gens/google/api/monitored_resource.pb.o /home/roman/Downloads/GoogleAPIs/googleapis/gens/google/api/logging.grpc.pb.o /home/roman/Downloads/GoogleAPIs/googleapis/gens/google/api/http.pb.o /home/roman/Downloads/GoogleAPIs/googleapis/gens/google/api/backend.grpc.pb.o /home/roman/Downloads/GoogleAPIs/googleapis/gens/google/api/annotations.pb.o /home/roman/Downloads/GoogleAPIs/googleapis/gens/google/api/auth.grpc.pb.o /home/roman/Downloads/GoogleAPIs/googleapis/gens/google/api/usage.pb.o /home/roman/Downloads/GoogleAPIs/googleapis/gens/google/api/metric.pb.o /home/roman/Downloads/GoogleAPIs/googleapis/gens/google/api/service.grpc.pb.o /home/roman/Downloads/GoogleAPIs/googleapis/gens/google/api/usage.grpc.pb.o /home/roman/Downloads/GoogleAPIs/googleapis/gens/google/api/log.pb.o /home/roman/Downloads/GoogleAPIs/googleapis/gens/google/api/service.pb.o /home/roman/Downloads/GoogleAPIs/googleapis/gens/google/api/auth.pb.o /home/roman/Downloads/GoogleAPIs/googleapis/gens/google/api/source_info.grpc.pb.o /home/roman/Downloads/GoogleAPIs/googleapis/gens/google/api/endpoint.grpc.pb.o /home/roman/Downloads/GoogleAPIs/googleapis/gens/google/api/endpoint.pb.o /home/roman/Downloads/GoogleAPIs/googleapis/gens/google/api/quota.grpc.pb.o /home/roman/Downloads/GoogleAPIs/googleapis/gens/google/api/system_parameter.pb.o /home/roman/Downloads/GoogleAPIs/googleapis/gens/google/api/annotations.grpc.pb.o /home/roman/Downloads/GoogleAPIs/googleapis/gens/google/api/context.grpc.pb.o /home/roman/Downloads/GoogleAPIs/googleapis/gens/google/api/logging.pb.o /home/roman/Downloads/GoogleAPIs/googleapis/gens/google/api/distribution.pb.o /home/roman/Downloads/GoogleAPIs/googleapis/gens/google/api/config_change.grpc.pb.o /home/roman/Downloads/GoogleAPIs/googleapis/gens/google/api/source_info.pb.o /home/roman/Downloads/GoogleAPIs/googleapis/gens/google/api/distribution.grpc.pb.o /home/roman/Downloads/GoogleAPIs/googleapis/gens/google/api/monitoring.pb.o /home/roman/Downloads/GoogleAPIs/googleapis/gens/google/api/control.grpc.pb.o /home/roman/Downloads/GoogleAPIs/googleapis/gens/google/api/quota.pb.o /home/roman/Downloads/GoogleAPIs/googleapis/gens/google/api/monitoring.grpc.pb.o /home/roman/Downloads/GoogleAPIs/googleapis/gens/google/api/label.grpc.pb.o /home/roman/Downloads/GoogleAPIs/googleapis/gens/google/api/context.pb.o /home/roman/Downloads/GoogleAPIs/googleapis/gens/google/rpc/error_details.pb.o /home/roman/Downloads/GoogleAPIs/googleapis/gens/google/rpc/status.pb.o /home/roman/Downloads/GoogleAPIs/googleapis/gens/google/rpc/code.grpc.pb.o /home/roman/Downloads/GoogleAPIs/googleapis/gens/google/rpc/code.pb.o /home/roman/Downloads/GoogleAPIs/googleapis/gens/google/rpc/status.grpc.pb.o /home/roman/Downloads/GoogleAPIs/googleapis/gens/google/rpc/error_details.grpc.pb.o /home/roman/Downloads/GoogleAPIs/googleapis/gens/google/longrunning/operations.grpc.pb.o /home/roman/Downloads/GoogleAPIs/googleapis/gens/google/longrunning/operations.pb.o /home/roman/Downloads/GoogleAPIs/googleapis/gens/google/cloud/speech/v1beta1/cloud_speech.pb.o /home/roman/Downloads/GoogleAPIs/googleapis/gens/google/cloud/speech/v1beta1/cloud_speech.grpc.pb.o /home/roman/Downloads/GoogleAPIs/googleapis/gens/google/cloud/speech/v1/cloud_speech.pb.o /home/roman/Downloads/GoogleAPIs/googleapis/gens/google/cloud/speech/v1/cloud_speech.grpc.pb.o /home/roman/Downloads/GoogleAPIs/googleapis/gens/google/cloud/speech/v1_1beta1/cloud_speech.pb.o /home/roman/Downloads/GoogleAPIs/googleapis/gens/google/cloud/speech/v1_1beta1/cloud_speech.grpc.pb.o
ar: creating googleapis.ar
g++ streaming_transcribe.o parse_arguments.o googleapis.ar -L/usr/local/lib `pkg-config --libs grpc++ grpc` -Wl,--no-as-needed -lgrpc++_reflection -Wl,--as-needed -lprotobuf -lpthread -ldl -o streaming_transcribe

23:23:26 Build Finished (took 3m:7s.68ms)

I can only understand first two lines in the log. How all this files:

/home/roman/Downloads/GoogleAPIs/googleapis/gens/google

files get to compiling? They are contained in GOOGLEAPIS_CCS variable and it is only used in makefile in

ar r $@ $?

string. Can you explain step by step how this actually work?

Advertisement

Answer

The following rule makes a static library that depends on the object files that will result from compiling all of the discovered source files:

googleapis.ar: $(GOOGLEAPIS_CCS:.cc=.o)
    ar r $@ $?

The way this works is that $(GOOGLEAPIS_CCS:.cc=.o) means “take the variable GOOGLEAPIS_CCS, and within it, replace every occurance of .cc with .o“. That is, convert the discovered lists of sources into a list of the object files that will be generated from them. Because this is on the prerequisite line, make will try to find a rule for how to produce these objects.

There are no explicit rules in the Makefile for building the .o files, so make will use one of the built-in implicitly defined pattern rules. Specifically:

%.o: %.cc
    $(CXX) $(CXXFLAGS) $(CPPFLAGS) -c -o $@ $^

So that causes all of the compilation you see in your output. Once these are built, the rule we were looking at above archives them into a static library. That static library is one of the prerequisites for the main build target, which is what triggers all of the above to be evaluated.

It may be obvious, but for completeness, all of the initial variable declartions use $(shell find ...). This literally runs the find command via the shell to recursively traverse the specified directory, and list the files that match the given pattern (e.g., *.cc). The result is that these variables are defined as the list of matching filenames.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement