Makefile With an IF Statement - unix

I have a Makefile which creates build a programme called monitor:
fo/monitor: fo/monitor.c fo/inotify.c
(cd fo ; $(MAKE) monitor)
I have two types of system that I can run my Make on, and only wish to have have one installer.
So I would like to add an IF statement to this to check for a file, and if it exists, then to build the monitor.
fo/monitor:
if [ -f path/to/file/exists ]; \
then \
fo/monitor.c fo/inotify.c \
(cd fo ; $(MAKE) monitor) \
else \
echo "" >/dev/null \
fi \
The problem is, when I attempt to run the Makefile - it falls over becuase it does not like this code - can anyone point me in the right direction please?

The fo/monitor.c and fo/inotify.c have to be added to the targets dependencies, and not in the if statement. You can also use the -C option of make instead of using a subshell. And you do have to echo nothing in nothing.
This should be good:
fo/monitor: fo/monitor.c fo/inotify.c
if [ -f path/to/file/exists ]; then \
$(MAKE) -C fo monitor; \
fi

Another way is to depend on that target only if path/to/file/exists exists:
# add fo/monitor dependency only if path/to/file/exists exists
all : $(shell test -e path/to/file/exists && echo "fo/monitor")
fo/monitor: fo/monitor.c fo/inotify.c
${MAKE} -C ${#D}

Related

How can I avoid repetitive commands, such as cd in a target rule?

In my makefile I have many cd $(d) commands, one for each simple command of a target.
Is there any way to reduce the number of cd $(d) commands?
all:
cd $(d); command1
cd $(d); command2
cd $(d); command3
cd $(d); command4
cd $(d); command5
Use a shell script that CDs to $d first as make's ${SHELL}.
.PHONY: all
all: SHELL := ./cd-to-d
all: .SHELLFLAGS :=
all:
command1
command2
command3
command4
We use target specific variables so as not to disturb other commands in the makefile.
It gets a bit messy if we add the rule to create cd-to-d though.
cd-to-d: SHELL := ${SHELL}
cd-to-d: .SHELLFLAGS := ${.SHELLFLAGS}
cd-to-d:
echo '#!/bin/bash' >$#-tmp
echo 'cd "$d"' >>$#-tmp
echo 'exec "$$#"' >>$#-tmp
chmod a+x $#-tmp
mv $#-tmp $#
.PHONY: all
all: SHELL := ./cd-to-d
all: .SHELLFLAGS :=
all: cd-to-d
all:
command1
command2
command3
command4
Why all the $#-tmp noise? If we get an error while creating cd-to-d, we don't want to leave an old one lying around. You could use .DELETE_ON_ERROR: instead (I always do).
Why the SHELL := ${SHELL} noise? When make builds cd-to-d as a dependency of all, it will inherit all's target specific defines. We need to cancel those concerned with changing the recipe shell.
Yuk.
You could use line continuation and pass the shell just one line where commands are separated by semicolons:
all:
cd $(d); \
command1; \
command2; \
command3; \
command4; \
command5
Or even
all:
cd $(d); command1; command2; command3; command4; command5
Recursive make?
.PHONY: all
all: ; ${MAKE} -C $d all-in-d
PHONY: all-in-d
all-in-d:
command1
command2
command3
command4

Startup script generated by SBT packager doesn't work

I have an SBT project that uses a pure-Java plugin to create a Debian package. The plugin is defined in plugins.sbt as such:
libraryDependencies += "org.vafer" % "jdeb" % "1.5" artifacts (Artifact("jdeb", "jar", "jar"))
In the SBT console, I execute this command:
debian:packageBin
This generates a .deb file which I am attempting to install on an Ubuntu machine using dpkg -i <filename>
The package installs. The libraries and configuration etc. are placed in /usr/share/, which is what I expected. That folder also includes a bin folder with a script that directly starts the program. So far so good.
However, I want to start the program using service start <projectname>, which doesn't work. Same goes for /etc/init.d/<projectname> start. The output for the latter command is:
[ ok ] Starting undagrid-api-server (via systemctl): undagrid-api-server.service.
But the process isn't running when I check it with ps. Here's the contents of /etc/init.d/<projectname>
#! /bin/bash
### BEGIN INIT INFO
# Provides: <projectname>
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: <projectname>
### END INIT INFO
source /lib/init/vars.sh
source /lib/lsb/init-functions
# adding bashScriptEnvConfigLocation
[[ -f /etc/default/<projectname> ]] && . /etc/default/<projectname>
# $JAVA_OPTS used in $RUN_CMD wrapper
export JAVA_OPTS
PIDFILE=/var/run/<projectname>/running.pid
if [ -z "$DAEMON_USER" ]; then
DAEMON_USER=<projectname>
fi
if [ -z "$DAEMON_GROUP" ]; then
DAEMON_GROUP=<projectname>
fi
RUN_CMD="/usr/share/<projectname>/bin/<projectname>"
start_daemon() {
log_daemon_msg "Starting" "<projectname>"
[ -d "/var/run/<projectname>" ] || install -d -o "$DAEMON_USER" -g "$DAEMON_GROUP" -m755 "/var/run/<projectname>"
start-stop-daemon --background --chdir /usr/share/<projectname> --chuid "$DAEMON_USER" --make-pidfile --pidfile "$PIDFILE" --startas "$RUN_CMD" --start -- $RUN_OPTS
log_end_msg $?
}
stop_daemon() {
log_daemon_msg "Stopping" "<projectname>"
start-stop-daemon --stop --quiet --oknodo --pidfile "$PIDFILE" --retry=TERM/60/KILL/30
log_end_msg $?
rm -f "$PIDFILE"
}
case "$1" in
start)
start_daemon
exit $?
;;
stop)
stop_daemon
exit $?
;;
restart|force-reload)
stop_daemon
start_daemon
exit $?
;;
status)
status_of_proc -p "$PIDFILE" "$RUN_CMD" <projectname> && exit 0 || exit $?
;;
*)
log_daemon_msg "Usage: /etc/init.d/<projectname> {start|stop|restart|status}"
;;
esac
exit 0
With some simple debugging I have found out that if the command line argument is "start", the script never makes it past the second source statement: source /lib/lsb/init-functions
Systemctl gives me the following output:
<projectname>.service loaded active exited LSB: <projectname>
I have also run the script using bash -x but that produces quite a pile of output that I don't really know how to read.
Anybody know what's going on here? The entire point of using a packager and installing it as a package is to avoid headaches like these...

Holding state in zsh completions

I want to write zsh completions for a program with the following calling convention:
program [generaloptions] operation [operationoptions]
where operation is one of --install, --upgrade...
What I have so far, are the general options and the operation options. My code looks something like this:
local generaloptions; generaloptions=(...)
local installoptions; installoptions=(...)
local upgradeoptions; upgradeoptions=(...)
case "$state" in
(install)
_arguments -s \
"$installoptions[#]" \
&& ret=0
(upgrade)
_arguments -s \
"$upgradeoption[#]" \
&& ret=0
*)
_arguments -s \
"$generaloptions[#]" \
'--install[...]: :->install' \
'--upgrade[...]: :->upgrade' \
&& ret=0
The problem is, after I type the operation and the first operation option, the state gets reset to the *) case.
Example
$ program --install --installoption --<tab>
list of general options
How can I set the next state to be the same as the old? Which command has similar calling conventions, so I can look at the code of the completion for this command?
The main problem is that the operations start with a --, so it is harder to find them in the arguments. In git for example all subcommands are only a word without dashes. So git solves this problem something like this:
Find the first argument without dashes because this must be the subcommand
Dispatch based on the subcommand to the commandline arguments for that subcommand.
So git dispatches in every call to the completion function (this was what I meant with "holding the state").
The way I solved this problem was by looking through many completion functions and finding a command that had a similar calling convention. The command that I found the most useful is pacman. Here is what I extracted from that:
# This somehow disassembles the commandline options
args=( ${${${(M)words:#-*}#-}:#-*}
case $args in
*i)
_arguments -s \
${installoptions} \
'(-i[...]' \
&& ret=0
;;
*u)
_arguments -s \
${upgradeoption} \
'-u[...]' \
&& ret=0
;;
*)
case ${(M)words:#--*} in
*--install*)
_arguments -s \
${installoptions} \
'--install[...]' \
&& ret=0
;;
*--upgrade*)
_arguments -s \
${upgradeoption} \
'--upgrade[...]' \
&& ret=0
;;
*)
_arguments -s \
{generaloptions} \
&& ret=0
;;
esac
esac
I know, there is a lot of dublication, but I think you get the point. Also notice, I moved the --install and --upgrade options from the general case to the operation case. If you don't do that, you loose the argument if you want complete after --install or --upgrade

Get pid of application started by mvn exec plugin

I want to write .sh script that will kill process started by maven exec plugin. At the moment i'm trying to get PID by start time, but this approach do not guarantee that there is no some collision with other processes.
Please, could someone say whether there is an approach to get PID or to kill process started by mvn exec .
Thanks.
The best approach is to start the maven process using start-stop-daemon. I use a modified bash script from a tomcat init.d to start/stop java or maven programs as daemons. Here is a snippet. Please customize as needed:
#!/bin/sh
case "$1" in
start)
log_daemon_msg "Starting $DESC" "$NAME"
start-stop-daemon --start -b -u "$MAVEN_USER" -g "$MAVEN_GROUP" \
-c "$MAVEN_USER" -d "$MAVEN_HOME" -p "$MAVEN_PID" -m \
-x mvn -- exec:java -Dexec.mainClass="com.company.Application" -Dexec.classpathScope=runtime -Dexec.args="/home/user 192.168.1.1" 2>&1
status="$?"
set +a -e
else
log_progress_msg "(already running)"
log_end_msg 0
fi
;;
stop)
log_daemon_msg "Stopping $DESC" "$NAME"
set +e
if [ -f "$MAVEN_PID" ]; then
start-stop-daemon --stop --pidfile "$MAVEN_PID" \
--user "$MAVEN_USER" \
--retry=TERM/20/KILL/5 >/dev/null
if [ $? -eq 1 ]; then
log_progress_msg "$DESC is not running but pid file exists, cleaning up"
elif [ $? -eq 3 ]; then
PID="`cat $MAVEN_PID`"
log_failure_msg "Failed to stop $NAME (pid $PID)"
exit 1
fi
rm -f "$MAVEN_PID"
rm -rf "$JVM_TMP"
else
log_progress_msg "(not running)"
fi
log_end_msg 0
set -e
;;

Need Help w/ Annoying Makefile Errors -- g++: g++ and shell errors -- and Multi-Makefile Design Advice

I have a makefile:
#Nice, wonderful makefile written by Jason
CC=g++
CFLAGS=-c -Wall
BASE_DIR:=.
SOURCE_DIR:=$(BASE_DIR)/source
BUILD_DIR:=$(BASE_DIR)/build
TEST_DIR:=$(BASE_DIR)/build/tests
MAKEFILE_DIR:=$(BASE_DIR)/makefiles
DATA_DIR:=$(BASE_DIR)/data
DATA_DIR_TESTS:=$(DATA_DIR)/tests
MOLECULE_UT_SOURCES := $(SOURCE_DIR)/molecule_test/main.cc \
$(SOURCE_DIR)/molecule_manager.h \
$(SOURCE_DIR)/molecule_manager.cpp \
$(SOURCE_DIR)/molecule_manager_main.h \
$(SOURCE_DIR)/molecule_manager_main.cpp \
$(SOURCE_DIR)/molecule_reader.h \
$(SOURCE_DIR)/molecule_reader.cpp \
$(SOURCE_DIR)/molecule_reader_psf_pdb.h \
$(SOURCE_DIR)/molecule_reader_psf_pdb.cpp \
$(SOURCE_DIR)/parameter_manager_lj_molecule.h \
$(SOURCE_DIR)/parameter_manager_lj_molecule.cpp \
$(SOURCE_DIR)/parameter_manager.h \
$(SOURCE_DIR)/parameter_manager.cpp \
$(SOURCE_DIR)/parser.h \
$(SOURCE_DIR)/parser.cpp \
$(SOURCE_DIR)/common.h
MOLECULE_UT_DATA := \
$(DATA_DIR_TESTS)/molecule_test/par_oxalate_and_friends.inp \
$(DATA_DIR_TESTS)/molecule_test/dicarboxy-octane_4.pdb \
$(DATA_DIR_TESTS)/molecule_test/dicarboxy-octane_4.psf
PARAM_UT_SOURCES := $(SOURCE_DIR)/parameter_test/main.cc \
$(SOURCE_DIR)/parameter_manager_lj_molecule.h \
$(SOURCE_DIR)/parameter_manager_lj_molecule.cpp \
$(SOURCE_DIR)/parameter_manager.h \
$(SOURCE_DIR)/parameter_manager.cpp \
$(SOURCE_DIR)/parser.h \
$(SOURCE_DIR)/parser.cpp \
$(SOURCE_DIR)/common.h
PARAM_UT_DATA := $(DATA_DIR_TESTS)/molecule_test/par_oxalate_and_friends.inp
molecule_test : molecule_test_prepare_sources molecule_test_prepare_makefiles \
molecule_test_prepare_data_files
#$(shell cd $(TEST_DIR)/molecule_unit_test/; \
make ./bin/molecule_test)
molecule_test_prepare_sources: molecule_test_dir
#echo Copying sources...
#cp --preserve $(MOLECULE_UT_SOURCES) \
$(TEST_DIR)/molecule_unit_test/source
molecule_test_prepare_makefiles: $(MAKEFILE_DIR)/Makefile.molecule_test
#cp --preserve $(MAKEFILE_DIR)/Makefile.molecule_test \
$(TEST_DIR)/molecule_unit_test/Makefile
molecule_test_prepare_data_files:
cp --preserve $(MOLECULE_UT_DATA) $(TEST_DIR)/molecule_unit_test/bin/
molecule_test_dir:
#if test -d $(BUILD_DIR); then \
echo Build exists...; \
else \
echo Build directory does not exist, making build dir...; \
mkdir $(BUILD_DIR); \
fi
#if test -d $(TEST_DIR); then \
echo Tests exists...; \
else \
echo Tests directory does not exist, making tests dir...; \
mkdir $(TEST_DIR); \
fi
#if test -d $(TEST_DIR)/molecule_unit_test; then \
echo Molecule unit test directory exists...; \
else \
echo Molecule unit test directory does \
not exist, making build dir...; \
mkdir $(TEST_DIR)/molecule_unit_test; \
fi
#if test -d $(TEST_DIR)/molecule_unit_test/source; then \
echo Molecule unit test source directory exists...; \
else \
echo Molecule unit test source directory does \
not exist, making build dir...; \
mkdir $(TEST_DIR)/molecule_unit_test/source; \
fi
#if test -d $(TEST_DIR)/molecule_unit_test/obj; then \
echo Molecule unit test object directory exists...; \
else \
echo Molecule unit test object directory does \
not exist, making object dir...; \
mkdir $(TEST_DIR)/molecule_unit_test/obj; \
fi
#if test -d $(TEST_DIR)/molecule_unit_test/bin; then \
echo Molecule unit test executable directory exists...; \
else \
echo Molecule unit test executable directory does \
not exist, making executable dir...; \
mkdir $(TEST_DIR)/molecule_unit_test/bin; \
fi
param_test : param_test_prepare_sources param_test_prepare_makefiles \
param_test_prepare_data_files
#$(shell cd $(TEST_DIR)/param_unit_test/; \
make ./bin/param_test)
param_test_prepare_sources: param_test_dir
#echo Copying sources...
#cp --preserve $(PARAM_UT_SOURCES) $(TEST_DIR)/param_unit_test/source
param_test_prepare_makefiles: $(MAKEFILE_DIR)/Makefile.param_test
#cp --preserve $(MAKEFILE_DIR)/Makefile.param_test \
$(TEST_DIR)/param_unit_test/Makefile
param_test_prepare_data_files:
cp --preserve $(PARAM_UT_DATA) $(TEST_DIR)/param_unit_test/bin/
param_test_dir:
#if test -d $(BUILD_DIR); then \
echo Build exists...; \
else \
echo Build directory does not exist, making build dir...; \
mkdir $(BUILD_DIR); \
fi
#if test -d $(TEST_DIR); then \
echo Tests exists...; \
else \
echo Tests directory does not exist, making tests dir...; \
mkdir $(TEST_DIR); \
fi
#if test -d $(TEST_DIR)/param_unit_test; then \
echo Param unit test directory exists...; \
else \
echo Param unit test directory does \
not exist, making build dir...; \
mkdir $(TEST_DIR)/param_unit_test; \
fi
#if test -d $(TEST_DIR)/param_unit_test/source; then \
echo Param unit test source directory exists...; \
else \
echo Param unit test source directory does \
not exist, making build dir...; \
mkdir $(TEST_DIR)/param_unit_test/source; \
fi
#if test -d $(TEST_DIR)/param_unit_test/obj; then \
echo Param unit test object directory exists...; \
else \
echo Param unit test object directory does \
not exist, making object dir...; \
mkdir $(TEST_DIR)/param_unit_test/obj; \
fi
#if test -d $(TEST_DIR)/param_unit_test/bin; then \
echo Param unit test executable directory exists...; \
else \
echo Param unit test executable directory does \
not exist, making executable dir...; \
mkdir $(TEST_DIR)/param_unit_test/bin; \
fi
That calls a second makefile after it creates and populates the directory structure.
The second makefile is as follows:
#Nice, wonderful makefile written by Jason
CC=g++
CFLAGS=-c -Wall
SOURCE_DIR:=./source
OBJ_DIR:=./obj
EXE_DIR:=./bin
$(EXE_DIR)/molecule_test : $(OBJ_DIR)/main.o \
$(OBJ_DIR)/parameter_manager_lj_molecule.o \
$(OBJ_DIR)/parameter_manager.o $(OBJ_DIR)/parser.o \
$(OBJ_DIR)/molecule_manager.o $(OBJ_DIR)/molecule_manager_main.o \
$(OBJ_DIR)/molecule_reader.o \
$(OBJ_DIR)/molecule_reader_psf_pdb.o
#$(CC) $(OBJ_DIR)/main.o $(OBJ_DIR)/parameter_manager.o \
$(OBJ_DIR)/parser.o $(OBJ_DIR)/parameter_manager_lj_molecule.o \
$(OBJ_DIR)/molecule_manager.o $(OBJ_DIR)/molecule_manager_main.o \
$(OBJ_DIR)/molecule_reader.o \
$(OBJ_DIR)/molecule_reader_psf_pdb.o \
-o molecule_test
#mv molecule_test $(EXE_DIR)/
$(OBJ_DIR)/main.o: $(SOURCE_DIR)/parameter_manager.h \
$(SOURCE_DIR)/parameter_manager_lj_molecule.h \
$(SOURCE_DIR)/molecule_manager.h \
$(SOURCE_DIR)/molecule_manager_main.h \
$(SOURCE_DIR)/molecule_reader.h \
$(SOURCE_DIR)/molecule_reader_psf_pdb.h \
$(SOURCE_DIR)/common.h $(SOURCE_DIR)/main.cc
$(CC) $(CFLAGS) $(SOURCE_DIR)/main.cc
#mv main.o $(OBJ_DIR)/
$(OBJ_DIR)/molecule_reader.o: $(SOURCE_DIR)/parameter_manager.h \
$(SOURCE_DIR)/parameter_manager_lj_molecule.h \
$(SOURCE_DIR)/molecule_manager.h \
$(SOURCE_DIR)/molecule_manager_main.h \
$(SOURCE_DIR)/molecule_reader.h \
$(SOURCE_DIR)/common.h
$(CC) $(CFLAGS) $(SOURCE_DIR)/molecule_reader.cpp
#mv molecule_reader.o $(OBJ_DIR)/
$(OBJ_DIR)/molecule_reader_psf_pdb.o: $(SOURCE_DIR)/parameter_manager.h \
$(SOURCE_DIR)/parameter_manager_lj_molecule.h \
$(SOURCE_DIR)/molecule_manager.h \
$(SOURCE_DIR)/molecule_manager_main.h \
$(SOURCE_DIR)/molecule_reader.h \
$(SOURCE_DIR)/molecule_reader_psf_pdb.h \
$(SOURCE_DIR)/common.h
$(CC) $(CFLAGS) $(SOURCE_DIR)/molecule_reader_psf_pdb.cpp
#mv molecule_reader_psf_pdb.o $(OBJ_DIR)/
$(OBJ_DIR)/molecule_manager.o: $(SOURCE_DIR)/molecule_manager.h \
$(SOURCE_DIR)/common.h
$(CC) $(CFLAGS) $(SOURCE_DIR)/molecule_manager.cpp
#mv molecule_manager.o $(OBJ_DIR)/
$(OBJ_DIR)/molecule_manager_main.o: $(SOURCE_DIR)/molecule_manager.h \
$(SOURCE_DIR)/molecule_manager_main.h \
$(SOURCE_DIR)/common.h
$(CC) $(CFLAGS) $(SOURCE_DIR)/molecule_manager_main.cpp
#mv molecule_manager_main.o $(OBJ_DIR)/
$(OBJ_DIR)/parameter_manager_lj_molecule.o: $(SOURCE_DIR)/common.h \
$(SOURCE_DIR)/parameter_manager.h \
$(SOURCE_DIR)/parser.h
$(CC) $(CFLAGS) $(SOURCE_DIR)/parameter_manager_lj_molecule.cpp
#mv parameter_manager_lj_molecule.o $(OBJ_DIR)/
$(OBJ_DIR)/parameter_manager.o: $(SOURCE_DIR)/common.h
$(CC) $(CFLAGS) $(SOURCE_DIR)/parameter_manager.cpp
#mv parameter_manager.o $(OBJ_DIR)/
$(OBJ_DIR)/parser.o: $(SOURCE_DIR)/parser.h
#$(CC) $(CFLAGS) $(SOURCE_DIR)/parser.cpp
#mv parser.o $(OBJ_DIR)/
$(OBJ_DIR)/common.o: $(SOURCE_DIR)/common.h
$(CC) $(CFLAGS) $(SOURCE_DIR)/common.h
mv common.h.gch $(OBJ_DIR)/
I admit I'm somewhat of a novice at Makefiles. I would both like advice as to how to streamline these files (without too much "magic") and how to fix these two errors...
First I have to say everything is working, so to speak. When I build my target it creates all the directories right and generates the executable. And all my files get copied properly and get recompiled when I touch the files in my base-level source directory. So these aren't "real" errors so to speak, just annoying error text I want to get rid of...
The first error occurs when I run a build make molecule_test which requires it to do something. Whatever it needs to do gets done, but I also get:
g++: g++: No such file or directory
g++: g++: No such file or directory
g++: g++: No such file or directory
g++: g++: No such file or directory
g++: g++: No such file or directory
g++: g++: No such file or directory
make: *** [molecule_test] Error 1
..AGAIN the build succeeds, creating the executable properly
The second error I get occurs when there's nothing to be done...when that happens I get:
/bin/sh: -c: line 0: unexpected EOF while looking for matching ``'
/bin/sh: -c: line 1: syntax error: unexpected end of file
Please be gentle... I've read basic makefile tutorials, including the gnu makefile tutorial, but there seems to be a leap between creating a small program with a handful of local sources and a large program with the need for nested directories, data files, etc. I'm trying to make that leap... unfortunately I have no best practice makefiles from past code as I'm at a small research group at a university, not a corporate atmosphere.
My basic approach is to create a base directory with the following
[dir] source/
[dir] data/
[dir] makefiles/
[dir] build/ **gets created
Makefile
The top level makefile creates a subdirectory in the build directory, copies the needed sources (say for a particular test program, and needed data files, and a makefile to make all the sources. The top level makefile then calls the build-level makefile.
I'd be open to ideas on how to streamline this process, but would appreciate if we FIRST resolve the errors.
Thanks in advance!!!
P.S. I'm running on Centos 5.4, GNU Make 3.81, gcc version 4.1.2 20080704 (Red Hat 4.1.2-44) .... GNU Make and gcc are both 64-bit versions...
This fix seems to clear up both of your errors:
molecule_test : molecule_test_prepare_sources molecule_test_prepare_makefiles \
molecule_test_prepare_data_files
#cd $(TEST_DIR)/molecule_unit_test && $(MAKE) ./bin/molecule_test
The $(shell ...) command is for invoking a shell outside of a rule. There's no need to use it here, since this is a command in a rule-- it's already happening in a subshell. Also note that this uses $(MAKE) instead of make (the reasons are a little subtle, just think of it as a good habit).
You can do it even more concisely and quietly:
molecule_test : molecule_test_prepare_sources molecule_test_prepare_makefiles \
molecule_test_prepare_data_files
#$(MAKE) -s -C $(TEST_DIR)/molecule_unit_test ./bin/molecule_test
As for streamlining, there's a lot you can do. You can reduce the length of your second makefile by about half, and fix what appear to be a number of bugs, and with the first one you can do even better. It depends on how much "magic" you can tolerate. Here's a quick attempt at streamlining your second Makefile (since I don't have your files to test it with I can't promise it'll work without some touchups).
CC=g++
CFLAGS=-c -Wall
SOURCE_DIR:=./source
INCDIRS := -I$(SOURCE_DIR)
OBJ_DIR:=./obj
EXE_DIR:=./bin
VPATH = $(SOURCE_DIR)
$(EXE_DIR)/molecule_test : $(OBJ_DIR)/main.o \
$(OBJ_DIR)/parameter_manager_lj_molecule.o \
$(OBJ_DIR)/parameter_manager.o $(OBJ_DIR)/parser.o \
$(OBJ_DIR)/molecule_manager.o $(OBJ_DIR)/molecule_manager_main.o \
$(OBJ_DIR)/molecule_reader.o \
$(OBJ_DIR)/molecule_reader_psf_pdb.o
#$(CC) $^ -o $#
$(OBJ_DIR)/main.o $(OBJ_DIR)/molecule_reader.o \
$(OBJ_DIR)/molecule_reader_psf_pdb.o: \
molecule_manager.h \
molecule_manager_main.h \
parameter_manager.h \
parameter_manager_lj_molecule.h
$(OBJ_DIR)/main.o: main.cpp \
molecule_reader.h \
molecule_reader_psf_pdb.h common.h
$(CC) $(CFLAGS) $(INCDIRS) $< $#
$(OBJ_DIR)/molecule_reader_psf_pdb.o: molecule_reader.h
$(OBJ_DIR)/parameter_manager_lj_molecule.o: parser.h
%.o: %.cpp %.h common.h
$(CC) $(CFLAGS) $(INCDIRS) $< -o $#
For a start you can get rid of all the mv commands and use make's built-in variables, e.g.
$(OBJ_DIR)/parameter_manager.o: $(SOURCE_DIR)/parameter_manager.cpp $(SOURCE_DIR)/common.h
$(CC) $(CFLAGS) -o $# $<

Resources