Skip to content

Added files for CMake, CI and coverage #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,4 @@
bin
obj
lib
build
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "UnitTest-cpp"]
path = UnitTest-cpp
url = https://github.com/liyier90/UnitTest-cpp
36 changes: 36 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
language: cpp

os: linux
dist: bionic
compiler: gcc
sudo: true
matrix:
include:
- env: BUILD=Debug COVERAGE=OFF
- env: BUILD=Release COVERAGE=OFF
- env: BUILD=Debug COVERAGE=ON

before_script:
- sudo apt-get install -yq lcov

script:
- git submodule update --init --recursive
- cd UnitTest-cpp
- mkdir -p lib/${BUILD}
- cd lib/${BUILD}
- cmake ../.. -DCMAKE_BUILD_TYPE=${BUILD}
- make
- cd ../../..
- mkdir -p build
- cd build
- cmake .. -DCMAKE_BUILD_TYPE=${BUILD} -DUnfit_ENABLE_TESTING=ON -DUnfit_COVERAGE=${COVERAGE}
- make
- if [ $COVERAGE == "ON" ];
then make coverage;
else ./UnfitTest;
fi

after_success:
- if [ $COVERAGE == "ON" ]; then
bash <(curl -s https://codecov.io/bash) -f coverage.info.cleaned -t "30ccfca2-015e-4c4e-913f-3cae31ea6673" || echo "Codecov did not collect coverage reports";
fi
114 changes: 114 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
cmake_minimum_required(VERSION 3.10)

set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH}
${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules)
include(EnsureOutOfSourceBuild)
ensure_out_of_source_build()
project(Unfit)

#######################################################################
# Set up compiler and build type
#######################################################################
include(UnfitCompilerFlags)
option(Unfit_ENABLE_TESTING "Enable Unfit Testing" OFF)
option(Unfit_COVERAGE "Build Unfit with coverage information" OFF)

if(Unfit_COVERAGE)
set(Unfit_ENABLE_TESTING ON)
set(CMAKE_BUILD_TYPE "Debug")
endif()

#######################################################################
# Find libraries
#######################################################################
set(Unfit_INCLUDES include examples)
set(Unfit_LINK_LIBRARIES "")

# Check prereqs
if(Unfit_COVERAGE)
find_program(GCOV_PATH gcov)
find_program(LCOV_PATH lcov)
find_program(GENHTML_PATH genhtml)
find_program(GCOVR_PATH gcovr PATHS ${CMAKE_SOURCE_DIR}/unittests)

if(NOT GCOV_PATH)
message(FATAL_ERROR "gcov not found! Aborting...")
endif()

if(NOT LCOV_PATH)
message(FATAL_ERROR "lcov not found! Aborting...")
endif()

if(NOT GENHTML_PATH)
message(FATAL_ERROR "genhtml not found! Aborting...")
endif()
endif()

if(Unfit_ENABLE_TESTING)
list(APPEND Unfit_INCLUDES
"${Unfit_SOURCE_DIR}/UnitTest-cpp"
"${Unfit_SOURCE_DIR}/UnitTest-cpp/UnitTest++/src")
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
list(APPEND Unfit_LINK_LIBRARIES
"${Unfit_SOURCE_DIR}/UnitTest-cpp/lib/Debug/libUnitTest++.a")
else()
list(APPEND Unfit_LINK_LIBRARIES
"${Unfit_SOURCE_DIR}/UnitTest-cpp/lib/Release/libUnitTest++.a")
endif()
endif()

#######################################################################
# Find pthread
#######################################################################
set(THREADS_PREFER_PTHREAD_FLAG_ON)
find_package(Threads REQUIRED)
list(APPEND Unfit_LINK_LIBRARIES Threads::Threads)

#######################################################################
# Configure build
#######################################################################
file(GLOB SOURCES "src/*.cpp")
if(Unfit_ENABLE_TESTING)
file(GLOB TEST_SOURCES "examples/*.cpp" "unittests/*.cpp")
file(COPY "${Unfit_SOURCE_DIR}/examples/data"
DESTINATION "${Unfit_BINARY_DIR}/examples")
file(COPY "${Unfit_SOURCE_DIR}/unittests/data"
DESTINATION "${Unfit_BINARY_DIR}/unittests")

set(target_name ${PROJECT_NAME}Test)
list(FILTER SOURCES EXCLUDE REGEX "main.cpp$")
add_executable(${target_name} ${SOURCES} ${TEST_SOURCES})
target_include_directories(${target_name} PUBLIC ${Unfit_INCLUDES})
target_link_libraries(${target_name} ${Unfit_LINK_LIBRARIES})
else()
set(target_name ${PROJECT_NAME}Example)
list(FILTER SOURCES EXCLUDE REGEX ".*-test.cpp$")
add_executable(${target_name} ${SOURCES} "examples/ParabolicModel.hpp")
target_include_directories(${target_name} PUBLIC include PUBLIC examples)
target_link_libraries(${target_name} ${Unfit_LINK_LIBRARIES})
endif()

if(Unfit_COVERAGE)
# Run test with low priority (+15)
set(NICE_COMMAND nice)
set(NICENESS -15)
set(_output_name coverage)
add_custom_target(coverage
# Cleanup lcov
${LCOV_PATH} --directory . --zerocounters

# Run tests
COMMAND ${NICE_COMMAND} ${NICENESS} "./${target_name}"

# Capture lcov counters
COMMAND ${LCOV_PATH} --directory . --capture --output-file ${_output_name}.info
COMMAND ${LCOV_PATH} --remove ${_output_name}.info /usr/* */UnitTest-cpp/* */unittests/* */examples/Test*.cpp --output-file ${_output_name}.info.cleaned
set(_page_title "\"Unfit Coverage Results\"")
COMMAND ${GENHTML_PATH} --title "${_page_title}" --no-function-coverage -o ${_output_name} ${_output_name}.info.cleaned
COMMAND ${CMAKE_COMMAND} -E remove ${_output_name}.info
COMMAND ${LCOV_PATH} --list ${_output_name}.info.cleaned

WORKING_DIRECTORY ${Chaste_BINARY_DIR}
COMMENT "Resetting code coverage counters to zero.\nProcessing code coverage counters and generating report."
VERBATIM)
endif()
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# Unfit
Unfit<br>
[![Build Status](https://travis-ci.org/liyier90/Unfit.svg?branch=master)](https://travis-ci.org/liyier90/Unfit)
[![codecov](https://codecov.io/gh/liyier90/Unfit/branch/master/graph/badge.svg)](https://codecov.io/gh/liyier90/Unfit)
==========
Data fitting and optimization software

### Getting Started (User Version)
Expand Down
1 change: 1 addition & 0 deletions UnitTest-cpp
Submodule UnitTest-cpp added at d246dd
20 changes: 20 additions & 0 deletions cmake/Modules/EnsureOutOfSourceBuild.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
macro(ensure_out_of_source_build)
get_filename_component(srcdir "${CMAKE_SOURCE_DIR}" REALPATH)
get_filename_component(bindir "${CMAKE_BINARY_DIR}" REALPATH)

if("${srcdir}" STREQUAL "${bindir}")
message("****************************************************************")
message("* Please build outside the source directory. For example: *")
message("* mkdir ~/tmp/build")
message("* cd ~/tmp/build")
message("* cmake ${CMAKE_SOURCE_DIR}")
message("* *")
message("* Please remove the following files:")
message("* ${CMAKE_SOURCE_DIR}/CMakeCache.txt")
message("* ${CMAKE_SOURCE_DIR}/CMakeFiles/*")
message("* *")
message("* before the next configuration attempt to avoid this message. *")
message("****************************************************************")
message(FATAL_ERROR "Quitting configuration")
endif()
endmacro(ensure_out_of_source_build)
17 changes: 17 additions & 0 deletions cmake/Modules/UnfitCompilerFlags.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -std=c++11")

if(CMAKE_BUILD_TYPE STREQUAL "Debug")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g")
endif()
if(CMAKE_BUILD_TYPE STREQUAL "Release")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O2 -s")
endif()

if(Unfit_COVERAGE)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} --coverage")
endif()

string(CONCAT CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Weffc++ -Wextra "
"-pedantic -Winit-self -Wmissing-declarations -Wmissing-include-dirs "
"-Wundef -Wredundant-decls -Wfloat-equal -Wmain -Wunreachable-code "
"-Wshadow -Wcast-align -Wswitch-enum")