Skip to content

Commit

Permalink
cmock-config accepts Google Test directory (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
hjagodzinski authored Aug 1, 2020
1 parent a34b42a commit 5a7cde1
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 36 deletions.
4 changes: 1 addition & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
language: c++

sudo: required
dist: bionic

before_script:
Expand All @@ -11,8 +10,7 @@ before_script:
- cd gtest
- cmake CMakeLists.txt
- make
- sudo make install
- cd ..
- make
- GTEST_DIR=./gtest make

script: make test
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
PREFIX=/usr/local

ifdef GTEST_DIR
GTEST_DIR:=$(shell readlink -f "$(GTEST_DIR)" | sed 's/ /\\ /')
endif

all: test/cmock_test

clean:
Expand Down
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,18 +158,18 @@ Secondly, you must pass the following options to a linker when building a test e
C Mock comes with *cmock-config* tool to hide all these details away from you. Run

```
cmock-config --cflags
cmock-config --cflags [path to Google Test]
```

and

```
cmock-config --libs [path to libgmock [path to libgtest]]
cmock-config --libs [path to Google Test]
```

to get the compilations and linker options, respectively.

Note: Since [it is not recommended to install a pre-compiled version of Google Test][4] many distributions don't provide pre-compiled Google Test anymore. You need to download and compile Google Test manually as described in [Google Test][1]. For the linker to find libgmock and libgtest you can pass the paths to them to the cmock-config script. If you omit the path to libgtest it defaults to "pathToLibgmock/libgtest".
Since [it is not recommended to install a pre-compiled version of Google Test][4] many distributions don't provide pre-compiled Google Test anymore. You need to download and compile Google Test manually as described in [Google Test][1]. Optional second command argument is a path to a directory contaning downloaded and built Google Test.

Let's say you built a code under test into *libfoo.so* and put a test code in *bar.cc*. To build your test executable you would run:

Expand Down Expand Up @@ -211,6 +211,12 @@ make
make test
```

Optionally you can provide a directory containing downloaded and built Google Test by setting _GTEST\_DIR_:

```
GTEST_DIR=/path/to/googletest make
```

Tests are quite simple and are a good source of usage examples.

References
Expand Down
91 changes: 63 additions & 28 deletions bin/cmock-config
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,75 @@

function print_usage
{
echo "cmock-config provides compilation and linker options required when using with cmock"
echo ""
echo "$0 OPTION [gmock [gtest]]"
echo "$0 OPTION [GOOGLE-TEST-DIRECTORY]"
echo "Provide compilation and linker options required by C Mock."
echo ""
echo "Options:"
echo -e "\t --cflags print compilation options"
echo -e "\t --libs print linker options"
echo -e "\t additional linker options:"
echo -e "\t [gmock] path to libgmock"
echo -e "\t [gtest] path to libgtest"
echo -e "\t defaults to [gmock]/gtest"
echo -e "\t-h, --help print help"
echo ""
echo " --cflags print compilation options"
echo " --libs print linker options"
echo " -h, --help print help"
echo ""
echo "GOOGLE-TEST-DIRECTORY is a directory contaning downloaded and built Google Test."
}

if [ $# -lt 1 -o $# -gt 3 ] ; then
print_usage 1>&2
exit 1
elif [ "$1" == --cflags -a $# -eq 1 ] ; then
DIR=`readlink -f $0`
DIR=`dirname $DIR`
DIR=`dirname $DIR`

echo "-I$DIR/include"
elif [ "$1" == --libs -a $# -le 3 ] ; then
if [ $# -eq 1 ] ; then
echo "-rdynamic -Wl,--no-as-needed -ldl"
elif [ $# -eq 2 ] ; then
echo "-rdynamic -Wl,--no-as-needed -ldl -L$2 -L$2/gtest"
function check_directory
{
DIR=$1

if [ -e "$DIR" ]; then
if [ ! -d "$DIR" ]; then
echo "'$DIR' is not a directory."
exit 1
fi
else
echo "-rdynamic -Wl,--no-as-needed -ldl -L$2 -L$3"
echo "'$DIR' does not exist."
exit 1
fi
elif [ "$1" == -h -o "$1" == --help ] ; then
print_usage
else
}

function find_directories
{
DIR=$(readlink -f "$1")
FILE_PATTERN=$2

find "$DIR" -regex $FILE_PATTERN -type f -exec dirname {} \; | sort | uniq
}

if [ $# -lt 1 -o $# -gt 2 ]; then
print_usage 1>&2
exit 1
fi

case $1 in
'--cflags')
CMOCK_DIR=$(dirname $(dirname $(readlink -f $0)))
OPTIONS="-I'$CMOCK_DIR/include'"

if [ $# -eq 2 ]; then
GTEST_DIR=$2
check_directory "$GTEST_DIR"
OPTIONS+=$(find_directories "$GTEST_DIR" '.*/\(gtest/gtest\.h\|gmock/gmock.h\)' | xargs -d '\n' dirname | xargs -d '\n' printf " -I'%s'")
fi

echo $OPTIONS
;;
'--libs')
OPTIONS="-rdynamic -Wl,--no-as-needed -ldl"

if [ $# -eq 2 ]; then
GTEST_DIR=$2
check_directory "$GTEST_DIR"
OPTIONS+=$(find_directories "$GTEST_DIR" '.*/\(libgtest\|libgmock\|libgmock_main\)\.a' | xargs -d '\n' printf " -L'%s'")
fi

echo $OPTIONS
;;
'-h'|'--help')
print_usage
;;
*)
print_usage 1>&2
exit 1
;;
esac
4 changes: 2 additions & 2 deletions test/Makefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
CMOCK=$(shell readlink -f ..)
CWD=$(shell pwd)

CFLAGS=$(shell $(CMOCK)/bin/cmock-config --cflags)
LDFLAGS=$(shell $(CMOCK)/bin/cmock-config --libs $(GMOCK) $(GTEST))
CFLAGS=$(shell $(CMOCK)/bin/cmock-config --cflags $(GTEST_DIR))
LDFLAGS=$(shell $(CMOCK)/bin/cmock-config --libs $(GTEST_DIR))

SRCS=$(shell ls *.cc)
OBJS=$(SRCS:.cc=.o)
Expand Down

0 comments on commit 5a7cde1

Please sign in to comment.