I'm having trouble establishing automatic dependency in my Makefile. The goal is to create a C++ library from a number of Protocol Buffer files, some of which might be in read-only directories. To get around the write permission, I want to generate the C++ files in the Makefile directory. The actual Makefile is created from Makefile.am. The following simplified script is supposed to do the trick for building automatic dependencies, but it doesn't:
mypbfiles = /usr/share/project/my_file.proto
MY_PB_FILES = $(notdir ${mypbfiles})
MY_SOURCE_FILES = ${MY_PB_FILES:.proto=.pb.cc}
lib_LTLIBRARIES = lib@PACKAGE_NAME@_protobuf.la
lib@PACKAGE_NAME@_protobuf_la_SOURCES = ${MY_SOURCE_FILES}
lib@PACKAGE_NAME@_protobuf_la_LDFLAGS = -version-info @MAJOR_VERSION@:@MINOR_VERSION@:0
$(MY_SOURCE_FILES): $(mypbfiles)
@for proto_file in $(mypbfiles); do \
gen_file=`echo ${proto_file} | perl -pe 's/\.proto/\.pb\.cc/; s|.*/||'`;\
echo "*** Building ${gen_file} from $${proto_file} ***"; \
protoc -I${mypbdir} --cpp_out=./ $${proto_file};\
done
LTLIBRARIES = $(lib_LTLIBRARIES)
all: Makefile $(LTLIBRARIES)
I then decided to add the following two lines to the script:
foo:
echo ${MY_SOURCE_FILES}
Running 'make foo' shows the following output:
echo my_file.pb.cc
my_file.pb.cc
So MY_SOURCE_FILES does contain the right value, but the dependency is not established. However, making the following change in the script fixes the problem:
lib@PACKAGE_NAME@_protobuf_la_SOURCES = my_file.pb.cc
You might ask why not make that change and be happy with it? The problem is that Makefile.am is fed to a program which handles 'lib@PACKAGE_NAME@_protobuf_xxx'. So I have to find a way to make things work! My suspicion is that using the 'notdir' function somehow doesn't expand the variable 'MY_PB_FILES' the way I intend it.
I am not all that versed in GNU Makefile, so any suggestion or help is greatly appreciated.