Skip to content

Commit

Permalink
Merge pull request #255 from fnadeau/bugfix/cgreen-runner_nm_parsing
Browse files Browse the repository at this point in the history
Replace use of `nm` with calls to `libbfd`. This also fixes #251: cgreen-runner handle non-lib files
  • Loading branch information
thoni56 authored Dec 28, 2021
2 parents 9314651 + 873c5c9 commit a9fb016
Show file tree
Hide file tree
Showing 23 changed files with 1,120 additions and 566 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ addons:
- cmake
- lcov
- g++
- binutils-dev
- valgrind

before_install:
- if [[ $CC == gcc ]] ; then export CXX=g++ ; else export CXX=clang++ ; fi
Expand Down
10 changes: 9 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,16 @@ include(ConfigureChecks.cmake)
configure_file(config.h.cmake ${CMAKE_CURRENT_BINARY_DIR}/config.h)

# check subdirectories
add_subdirectory(src)
# Dependency on our own library so we can use CGREEN_LIBRARY in all subdirectories
if (CGREEN_WITH_STATIC_LIBRARY)
set(CGREEN_LIBRARY ${CGREEN_STATIC_LIBRARY})
else ()
set(CGREEN_LIBRARY ${CGREEN_SHARED_LIBRARY})
endif()

add_subdirectory(doc)
add_subdirectory(include)
add_subdirectory(src)
if (UNIX OR MSYS)
# reflective runner only supported on UNIX/binutils platforms
add_subdirectory(tools)
Expand All @@ -91,6 +98,7 @@ endif(UNIX OR MSYS)
if (CGREEN_WITH_UNIT_TESTS)
include(MacroAddUnitTest)
include(MacroAddTest)
include(MacroAddValgrindTest)
add_subdirectory(tests)
if (UNIX OR MSYS)
# reflective runner only supported on UNIX/binutils platforms
Expand Down
91 changes: 34 additions & 57 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,8 @@ ifndef VERBOSE
MAKEFLAGS += --no-print-directory
endif

# Removing ninja support as it does not build the same way and somehow
# trashes the output comparison tests with different paths and library names
#NINJA_EXISTS := $(shell command -v ninja 2>/dev/null)
ifdef NINJA_EXISTS
BUILDER = ninja
GENERATOR = -G Ninja
else
BUILDER = $(MAKE)
endif

all: build/Makefile
cd build; $(BUILDER)
.PHONY:all
all: build-it

.PHONY:debug
debug: build
Expand Down Expand Up @@ -100,30 +90,33 @@ DIFF_TOOL_ARGUMENTS = $(1)_tests \
../../tests \
$(1)_tests.expected

.PHONY: unit
unit: build-it
cd build ; \
$(LDPATH) tools/cgreen-runner -c `find tests -name $(PREFIX)cgreen_c_tests$(SUFFIX)` ; \
r=$$((r + $$?)) ; \
$(LDPATH) tools/cgreen-runner -c `find tests -name $(PREFIX)cgreen_cpp_tests$(SUFFIX)` ; \
r=$$((r + $$?)) ; \
$(LDPATH) tools/cgreen-runner -c `find tools/tests -name $(PREFIX)cgreen_runner_tests$(SUFFIX)` ; \
r=$$((r + $$?)) ; \
cd tests ; \
$(LDPATH) $(XML_DIFF_TOOL) $(call DIFF_TOOL_ARGUMENTS,xml_output) ; \
r=$$((r + $$?)) ; \
$(LDPATH) $(DIFF_TOOL) $(call DIFF_TOOL_ARGUMENTS,assertion_messages) ; \
r=$$((r + $$?)) ; \
$(LDPATH) $(DIFF_TOOL) $(call DIFF_TOOL_ARGUMENTS,mock_messages) ; \
r=$$((r + $$?)) ; \
$(LDPATH) $(DIFF_TOOL) $(call DIFF_TOOL_ARGUMENTS,constraint_messages) ; \
r=$$((r + $$?)) ; \
$(LDPATH) $(DIFF_TOOL) $(call DIFF_TOOL_ARGUMENTS,custom_constraint_messages) ; \
r=$$((r + $$?)) ; \
$(LDPATH) $(DIFF_TOOL) $(call DIFF_TOOL_ARGUMENTS,ignore_messages) ; \
r=$$((r + $$?)) ; \
$(LDPATH) CGREEN_PER_TEST_TIMEOUT=1 $(DIFF_TOOL) $(call DIFF_TOOL_ARGUMENTS,failure_messages) ; \
r=$$((r + $$?)) ; \
exit $$r
if [ -f build/tools/cgreen-runner ]; then \
cd build ; \
$(LDPATH) tools/cgreen-runner -c `find tests -name $(PREFIX)cgreen_c_tests$(SUFFIX)` ; \
r=$$((r + $$?)) ; \
$(LDPATH) tools/cgreen-runner -c `find tests -name $(PREFIX)cgreen_cpp_tests$(SUFFIX)` ; \
r=$$((r + $$?)) ; \
$(LDPATH) tools/cgreen-runner -c `find tools/tests -name $(PREFIX)cgreen_runner_tests$(SUFFIX)` ; \
r=$$((r + $$?)) ; \
cd tests ; \
$(LDPATH) $(XML_DIFF_TOOL) $(call DIFF_TOOL_ARGUMENTS,xml_output) ; \
r=$$((r + $$?)) ; \
$(LDPATH) $(DIFF_TOOL) $(call DIFF_TOOL_ARGUMENTS,assertion_messages) ; \
r=$$((r + $$?)) ; \
$(LDPATH) $(DIFF_TOOL) $(call DIFF_TOOL_ARGUMENTS,mock_messages) ; \
r=$$((r + $$?)) ; \
$(LDPATH) $(DIFF_TOOL) $(call DIFF_TOOL_ARGUMENTS,constraint_messages) ; \
r=$$((r + $$?)) ; \
$(LDPATH) $(DIFF_TOOL) $(call DIFF_TOOL_ARGUMENTS,custom_constraint_messages) ; \
r=$$((r + $$?)) ; \
$(LDPATH) $(DIFF_TOOL) $(call DIFF_TOOL_ARGUMENTS,ignore_messages) ; \
r=$$((r + $$?)) ; \
$(LDPATH) CGREEN_PER_TEST_TIMEOUT=1 $(DIFF_TOOL) $(call DIFF_TOOL_ARGUMENTS,failure_messages) ; \
r=$$((r + $$?)) ; \
exit $$r; \
fi

.PHONY: doc
doc: build
Expand All @@ -136,35 +129,19 @@ chunked: doc
asciidoctor-chunker build/doc/cgreen-guide-en.html -o docs
echo open $(PWD)/docs/index.html

.PHONY:valgrind
valgrind: build-it
@echo -n "Running all tests under Valgrind "
@> valgrind.log
@for lib in `ls build/tests/$(PREFIX)*_tests$(SUFFIX)` ; \
do \
echo -n "." ; \
LD_LIBRARY_PATH=build/src valgrind --leak-check=full build/tools/cgreen-runner $$lib >> valgrind.log 2>&1 ; \
done
@echo
grep --with-filename --line-number " lost: " valgrind.log | grep -v " 0 bytes" ; \
if [ $$? -eq 1 ] ; then echo "Nothing lost" ; fi



############# Internal

.PHONY: build-it
.PHONY:build-it
build-it: build/Makefile
$(BUILDER) -C build
$(MAKE) -C build

build:
mkdir build
mkdir -p build

build/Makefile: build
build/Makefile: | build
ifeq ($(OS),Darwin)
cd build; cmake -DCMAKE_OSX_ARCHITECTURES="arm64e;x86_64" $(GENERATOR) ..
cd build; cmake -DCMAKE_OSX_ARCHITECTURES="arm64e;x86_64" ..
else
cd build; cmake $(GENERATOR) ..
cd build; cmake ..
endif

.SILENT:
36 changes: 35 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,43 @@ You can also clone the repository or download the source zip from [GitHub](http:
You need the [CMake](http://www.cmake.org) build system.
Most standard C/C++ compilers should work. GCC definitely does.

Perl, diff, find and sed are quired to run unit-tests. Most distro will have
those already installed.

To build cgreen-runner, you need binutils development package. At runtime,
cgreen-runner only requires binutils library. Some distro do not distinguish the
two.

|Distro | Build | Run |
--- | --- | ---
|Arch|bintuils|bintuils|
|Cygwin|cygwin64-libbfd|cygwin64-libbfd|
|Debian|binutils-dev|libbinutils|
|Fedora|bintuils-devel|bintuils-devel|
|OpenSUSE|bintuils-devel|bintuils-devel|

In the root directory run ``make``. That will configure and build the
library and the `cgreen-runner`, both supporting both C and C++. See
also the documentation.
also the documentation. The later will only be build if build requirements are met.

Here are some example of how to build using docker for various distro:

```
# debian build (will not clean)
$ docker run --rm -v $PWD:/cgreen debian bash -c \
"apt update && apt -y install git cmake gcc g++ binutils-dev && cd /cgreen && make unit test"
# debian run (we cannot use make unit test since cmake will want to recompile)
$ docker run --rm -v $PWD:/cgreen debian bash -c \
'apt update && apt -y install libbinutils && cd /cgreen && ./build/tools/cgreen-runner $(find . -name *.so) rm -rf build'
# Fedora
$ docker run -t --rm -v $PWD:/cgreen fedora bash -c \
"/usr/bin/yum -y install cmake gcc g++ git diffutils findutils && cd /cgreen && make unit test && make clean"
# OpenSUSE
$ docker run --rm -v $PWD:/cgreen opensuse/tumbleweed bash -c \
'/usr/bin/zypper refresh && /usr/bin/zypper --non-interactive install git cmake gcc gcc-c++ git binutils-devel diffutils findutils && cd /cgreen && make unit test && make clean'
```

## Using It

Expand Down
87 changes: 87 additions & 0 deletions cmake/Modules/FindLibBfd.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
# - Try to find libbfd
# Once done this will define
#
# LIBBFD_FOUND - system has libbfd
# LIBBFD_INCLUDE_DIRS - the libbfd include directory
# LIBBFD_LIBRARIES - Link these to use libbfd
# LIBBFD_DEFINITIONS - Compiler switches required for using libbfd
#
# Based on:
#
# Copyright (c) 2008 Bernhard Walle <bernhard.walle@gmx.de>
#
# Redistribution and use is allowed according to the terms of the New
# BSD license.
# For details see the accompanying COPYING-CMAKE-SCRIPTS file.
#


if (LIBBFD_LIBRARIES AND LIBBFD_INCLUDE_DIRS)
set (LIBBFD_FIND_QUIETLY TRUE)
endif ()

find_path (LIBBFD_INCLUDE_DIRS
NAMES
bfd.h
dis-asm.h
PATHS
/usr/include
/usr/local/include
/opt/local/include
/opt/include
ENV CPATH)

# Ugly, yes ugly...
find_library (LIBBFD_BFD_LIBRARY
NAMES
bfd
PATHS
/usr/lib
/usr/lib64
/usr/local/lib
/usr/local/lib64
/usr/include
/opt/local/lib
/opt/usr/lib64
ENV LIBRARY_PATH
ENV LD_LIBRARY_PATH)

find_library (LIBIBERTY_LIBRARY
NAMES
iberty
PATHS
/usr/lib
/usr/lib64
/usr/local/lib
/usr/local/lib64
/usr/include
/opt/local/lib
/opt/usr/lib64
ENV LIBRARY_PATH
ENV LD_LIBRARY_PATH)

find_library (LIBZ_LIBRARY
NAMES
z
PATHS
/usr/lib
/usr/lib64
/usr/local/lib
/usr/local/lib64
/usr/include
/opt/local/lib
/opt/usr/lib64
ENV LIBRARY_PATH
ENV LD_LIBRARY_PATH)

include (FindPackageHandleStandardArgs)

# handle the QUIETLY and REQUIRED arguments and set LIBBFD_FOUND to TRUE if all listed variables are TRUE
FIND_PACKAGE_HANDLE_STANDARD_ARGS(LibBfd DEFAULT_MSG
LIBBFD_BFD_LIBRARY
LIBBFD_INCLUDE_DIRS
LIBIBERTY_LIBRARY
LIBZ_LIBRARY)

set(LIBBFD_LIBRARIES "${LIBBFD_BFD_LIBRARY}" "${LIBIBERTY_LIBRARY}" "${LIBZ_LIBRARY}")
mark_as_advanced(LIBBFD_INCLUDE_DIRS LIBBFD_LIBRARIES LIBBFD_BFD_LIBRARY LIBIBERTY_LIBRARY LIBZ_LIBRARY)
11 changes: 11 additions & 0 deletions cmake/Modules/FindValgrind.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
if (NOT Valgrind_FOUND)

find_program(Valgrind_EXECUTABLE valgrind)

include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(Valgrind DEFAULT_MSG Valgrind_EXECUTABLE)

set(Valgrind_FOUND ${Valgrind_FOUND} CACHE BOOL "Flag whether Valgrind package was found")
mark_as_advanced(Valgrind_FOUND Valgrind_EXECUTABLE)

endif ()
28 changes: 28 additions & 0 deletions cmake/Modules/MacroAddValgrindTest.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# - MACRO_ADD_VALGRIND_TEST(<args>)
#
# Calls add_test() with all the <args> but if on Win32 or Cygwin also adds the
# directory where the Cgreen library is generated to the path so that it will
# be used when running the test
#
# @thoni56/Thomas Nilefalk 2015-09-13

macro (macro_add_valgrind_test)
if (Valgrind_FOUND)
set(
libname
${CMAKE_FIND_LIBRARY_PREFIXES}${ARGN}${CMAKE_SHARED_LIBRARY_SUFFIX}
)
add_test(
NAME valgrind_${libname}
COMMAND sh -c "LD_LIBRARY_PATH=build/src valgrind --leak-check=full tools/cgreen-runner ${CMAKE_CURRENT_BINARY_DIR}/${libname} 2>1&"
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
)
set_tests_properties(
valgrind_${libname} PROPERTIES
FAIL_REGULAR_EXPRESSION "(definitely|indirectly|possible) lost: [1-9]"
)
if (CYGWIN OR WIN32)
set_tests_properties(${ARGV1} PROPERTIES ENVIRONMENT PATH=${PROJECT_BINARY_DIR}/src:$ENV{PATH})
endif ()
endif ()
endmacro(macro_add_valgrind_test)
Loading

0 comments on commit a9fb016

Please sign in to comment.