Skip to content

Commit

Permalink
refs #27: Import googletest as submodule and merge with our codebase.
Browse files Browse the repository at this point in the history
 * Since googletest v1.3, it provides a "fusing" script that combines
   all gtest source files into a single .cc file.

 * To update to latest version of gtest, first run git submodule update
   and then run cd 3rdparty && 3rdparty/update-googletest.sh.
  • Loading branch information
achimnol committed Jan 23, 2016
1 parent 07b67b8 commit 45aa094
Show file tree
Hide file tree
Showing 9 changed files with 31,655 additions and 9 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
[submodule "3rdparty/click-parser"]
path = 3rdparty/click-parser
url = https://github.com/leeopop/click-parser.git
[submodule "3rdparty/googletest"]
path = 3rdparty/googletest
url = https://github.com/google/googletest
1 change: 1 addition & 0 deletions 3rdparty/googletest
Submodule googletest added at 82b11b
13 changes: 13 additions & 0 deletions 3rdparty/update-googletest.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
echo 'Fusing and updating gtest sources/headers for NBA...'
echo 'NOTE: You need to run "git pull" in 3rdparty/googletest manually first.'
TMPDIR=/tmp/nba-gtest-update
NBADIR=..
mkdir -p $TMPDIR
python2 googletest/googletest/scripts/fuse_gtest_files.py $TMPDIR
cp googletest/googletest/src/gtest_main.cc $TMPDIR/gtest
# Replace relative include path to absolute path
sed -i 's/^#include "gtest\/gtest.h"/#include <gtest\/gtest.h>/' $TMPDIR/gtest/*.cc
cp $TMPDIR/gtest/gtest-all.cc $NBADIR/src/lib/gtest
cp $TMPDIR/gtest/gtest_main.cc $NBADIR/src/lib/gtest
cp $TMPDIR/gtest/gtest.h $NBADIR/include/gtest
rm -rf $TMPDIR
22 changes: 14 additions & 8 deletions Snakefile
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ ELEMENT_HEADER_FILES = [s for s in compilelib.find_all(['elements'], r'^.+\.(h|h
# List of object files
OBJ_DIR = 'build'
OBJ_FILES = [joinpath(OBJ_DIR, o) for o in map(lambda s: re.sub(r'^(.+)\.(c|cc|cpp|cu)$', r'\1.o', s), SOURCE_FILES)]
GTEST_MAIN_OBJ = 'build/src/lib/gtest/gtest_main.o'
GTEST_FUSED_OBJ = 'build/src/lib/gtest/gtest-all.o'
OBJ_FILES.remove(GTEST_MAIN_OBJ)
OBJ_FILES.remove(GTEST_FUSED_OBJ)

# Common configurations
CXXSTD = version()
Expand Down Expand Up @@ -232,31 +236,33 @@ rule clean:
shell: _clean_cmds

_test_cases, = glob_wildcards('tests/test_{case}.cc')
TEST_LIBS = '-lgtest_main -lgtest'
MAINLESS_OBJ_FILES = OBJ_FILES.copy()
MAINLESS_OBJ_FILES.remove('build/src/main.o')
TEST_OBJ_FILES = OBJ_FILES.copy()
TEST_OBJ_FILES.append(GTEST_MAIN_OBJ)
TEST_OBJ_FILES.append(GTEST_FUSED_OBJ)
TEST_OBJ_FILES.remove('build/src/main.o')

rule cleantest:
shell: 'rm -rf build/tests tests/test_all'
shell: 'rm -rf build/tests tests/test_all ' \
+ ' '.join(joinpath('tests', 'test_' + f.replace('.cc', '')) for f in _test_cases)

rule test: # build only individual tests
input: expand('tests/test_{case}', case=_test_cases)

rule testall: # build a unified test suite
input:
testobjs=expand(joinpath(OBJ_DIR, 'tests/test_{case}.o'), case=_test_cases),
objs=MAINLESS_OBJ_FILES,
objs=TEST_OBJ_FILES,
libs=[lib.target for lib in THIRD_PARTY_LIBS]
output: 'tests/test_all'
shell: '{CXX} {CXXFLAGS} -o {output} {input.testobjs} -Wl,--whole-archive {input.objs} -Wl,--no-whole-archive {TEST_LIBS} {LIBS}'
shell: '{CXX} {CXXFLAGS} -o {output} {input.testobjs} -Wl,--whole-archive {input.objs} -Wl,--no-whole-archive {LIBS}'

for case in _test_cases:
includes = [f for f in compilelib.get_includes(fmt('tests/test_{case}.cc'), 'include')]
requires = [joinpath(OBJ_DIR, f) for f in compilelib.get_requires(fmt('tests/test_{case}.cc'), 'src')]
rule: # for individual tests
input: fmt('tests/test_{case}.cc'), includes, req=requires
input: fmt('tests/test_{case}.cc'), includes, GTEST_FUSED_OBJ, GTEST_MAIN_OBJ, req=requires
output: fmt('tests/test_{case}')
shell: '{CXX} {CXXFLAGS} -o {output} {input[0]} {input.req} {TEST_LIBS} {LIBS}'
shell: '{CXX} {CXXFLAGS} -o {output} {input[0]} {input.req} {GTEST_FUSED_OBJ} {GTEST_MAIN_OBJ} {LIBS}'
rule: # for unified test suite
input: fmt('tests/test_{case}.cc'), includes
output: joinpath(OBJ_DIR, fmt('tests/test_{case}.o'))
Expand Down
Loading

0 comments on commit 45aa094

Please sign in to comment.