Skip to content

Commit

Permalink
cmake improvements (#24)
Browse files Browse the repository at this point in the history
- adds an `INTERFACE` cmake library for easier consumption as a subproject
- Removes GNU dependencies
- Updates google test to 1.8.0 and substantially simplifies the google test build
- adds caching to our CI system to improve build speed
- Add options to avoid building the unit tests/documentation (this will be helpful for subdirectories)
  • Loading branch information
nholthaus authored Oct 7, 2016
1 parent 4de1dbc commit f20b07e
Show file tree
Hide file tree
Showing 15 changed files with 68 additions and 100 deletions.
14 changes: 11 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@ sudo: required
dist: trusty
language: cpp

cache:
ccache: true
directories:
- cmake-3.4.3-Linux-x86_64/bin
- cmake-3.4.3-Linux-x86_64/plugins
- cmake-3.4.3-Linux-x86_64/share

os:
- linux
compiler:
Expand Down Expand Up @@ -29,9 +36,10 @@ env:
install:
- sudo apt-get update -qq
- sudo apt-get install -y -qq lcov curl
- if [ "$CXX" = "g++" ]; then export CXX="g++-4.9" CC="gcc-4.9"; fi
- if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then wget --no-check-certificate http://cmake.org/files/v3.4/cmake-3.4.3-Linux-x86_64.tar.gz ;fi
- if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then tar -xzf cmake-3.4.3-Linux-x86_64.tar.gz ;fi
- if [[ "$CXX" = "g++" ]]; then export CXX="g++-4.9" CC="gcc-4.9"; fi
- pwd
- ls
- if [[ ! -f "cmake-3.4.3-Linux-x86_64/bin/cmake" ]]; then wget --no-check-certificate http://cmake.org/files/v3.4/cmake-3.4.3-Linux-x86_64.tar.gz && tar -xvf cmake-3.4.3-Linux-x86_64.tar.gz; fi

script:
- mkdir build && cd build
Expand Down
10 changes: 0 additions & 10 deletions 3rdParty/gnuWin/CMakeLists.txt

This file was deleted.

Binary file removed 3rdParty/gnuWin/applydiff.exe
Binary file not shown.
Binary file removed 3rdParty/gnuWin/diff.exe
Binary file not shown.
Binary file removed 3rdParty/gnuWin/libiconv2.dll
Binary file not shown.
Binary file removed 3rdParty/gnuWin/libintl3.dll
Binary file not shown.
Binary file removed 3rdParty/gnuWin/patch-man.pdf
Binary file not shown.
Binary file removed 3rdParty/gnuWin/regex2.dll
Binary file not shown.
Binary file removed 3rdParty/gnuWin/sed.exe
Binary file not shown.
42 changes: 21 additions & 21 deletions 3rdParty/gtest/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,18 @@

cmake_minimum_required(VERSION 3.0.0 FATAL_ERROR)
PROJECT(gtest)
SET(TARGET_NAME gtest)
STRING(TOUPPER ${TARGET_NAME} TARGET_NAME_UPPER)
STRING(TOUPPER ${PROJECT_NAME} PROJECT_NAME_UPPER)

SET(VERSION_${TARGET_NAME_UPPER} "?.?.?" CACHE STRING "${TARGET_NAME} version string")
SET(${TARGET_NAME_UPPER}_DIR ${CMAKE_CURRENT_BINARY_DIR}/src CACHE STRING "${TARGET_NAME} source directory")
SET(VERSION_${PROJECT_NAME_UPPER} "?.?.?" CACHE STRING "${PROJECT_NAME} version string")
SET(${PROJECT_NAME_UPPER}_DIR ${CMAKE_CURRENT_BINARY_DIR}/src CACHE STRING "${PROJECT_NAME} source directory")

include(ExternalProject)
ExternalProject_Add(
${TARGET_NAME}-ext
URL ${CMAKE_CURRENT_SOURCE_DIR}/gtest-${VERSION_GTEST}.zip
DOWNLOAD_DIR ${GTEST_DIR}
SOURCE_DIR ${GTEST_DIR}
PATCH_COMMAND ${PATCH} -p 0 -d ${GTEST_DIR} -i ${CMAKE_CURRENT_SOURCE_DIR}/patch.diff
${PROJECT_NAME}-ext
URL ${CMAKE_CURRENT_SOURCE_DIR}/${PROJECT_NAME}-${VERSION_${PROJECT_NAME_UPPER}}.zip
DOWNLOAD_DIR ${${PROJECT_NAME_UPPER}_DIR}
SOURCE_DIR ${${PROJECT_NAME_UPPER}_DIR}
CMAKE_ARGS -DBUILD_GMOCK=OFF -DBUILD_GTEST=ON -Dgtest_force_shared_crt=ON
INSTALL_COMMAND ""
BUILD_IN_SOURCE 1
)
Expand All @@ -24,23 +23,24 @@ ExternalProject_Add(
# ------------------------------------------------------------------------------------------------------------------------------

# Library
add_library(${TARGET_NAME} STATIC IMPORTED GLOBAL)
add_library(${PROJECT_NAME} STATIC IMPORTED GLOBAL)
IF(WIN32)
SET_TARGET_PROPERTIES(${TARGET_NAME} PROPERTIES
IMPORTED_LOCATION_DEBUG ${GTEST_DIR}/Debug/${CMAKE_FIND_LIBRARY_PREFIXES}gtest${CMAKE_FIND_LIBRARY_SUFFIXES}
IMPORTED_LOCATION_RELEASE ${GTEST_DIR}/Release/${CMAKE_FIND_LIBRARY_PREFIXES}gtest${CMAKE_FIND_LIBRARY_SUFFIXES}
IMPORTED_LOCATION_MINSIZEREL ${GTEST_DIR}/MinSizeRel/${CMAKE_FIND_LIBRARY_PREFIXES}gtest${CMAKE_FIND_LIBRARY_SUFFIXES}
IMPORTED_LOCATION_RELWITHDEBINFO ${GTEST_DIR}/RelWithDebInfo/${CMAKE_FIND_LIBRARY_PREFIXES}gtest${CMAKE_FIND_LIBRARY_SUFFIXES}
SET_TARGET_PROPERTIES(${PROJECT_NAME} PROPERTIES
IMPORTED_LOCATION_DEBUG ${${PROJECT_NAME_UPPER}_DIR}/googletest/Debug/${CMAKE_FIND_LIBRARY_PREFIXES}${PROJECT_NAME}${CMAKE_FIND_LIBRARY_SUFFIXES}
IMPORTED_LOCATION_RELEASE ${${PROJECT_NAME_UPPER}_DIR}/googletest/Release/${CMAKE_FIND_LIBRARY_PREFIXES}${PROJECT_NAME}${CMAKE_FIND_LIBRARY_SUFFIXES}
IMPORTED_LOCATION_MINSIZEREL ${${PROJECT_NAME_UPPER}_DIR}/googletest/MinSizeRel/${CMAKE_FIND_LIBRARY_PREFIXES}${PROJECT_NAME}${CMAKE_FIND_LIBRARY_SUFFIXES}
IMPORTED_LOCATION_RELWITHDEBINFO ${${PROJECT_NAME_UPPER}_DIR}/googletest/RelWithDebInfo/${CMAKE_FIND_LIBRARY_PREFIXES}${PROJECT_NAME}${CMAKE_FIND_LIBRARY_SUFFIXES}
)
ELSE(WIN32)
SET_TARGET_PROPERTIES(${TARGET_NAME} PROPERTIES
IMPORTED_LOCATION ${GTEST_DIR}/${CMAKE_FIND_LIBRARY_PREFIXES}gtest${CMAKE_STATIC_LIBRARY_SUFFIX}
SET_TARGET_PROPERTIES(${PROJECT_NAME} PROPERTIES
IMPORTED_LOCATION ${${PROJECT_NAME_UPPER}_DIR}/googletest/${CMAKE_FIND_LIBRARY_PREFIXES}${PROJECT_NAME}${CMAKE_STATIC_LIBRARY_SUFFIX}
INTERFACE_LINK_LIBRARIES -pthread
)
ENDIF(WIN32)
add_dependencies(${TARGET_NAME} ${TARGET_NAME}-ext)
add_dependencies(${PROJECT_NAME} ${PROJECT_NAME}-ext)

SET(${TARGET_NAME_UPPER}_INCLUDE_DIRS ${${TARGET_NAME_UPPER}_DIR}/include CACHE STRING "${TARGET_NAME} include directory")
SET(${TARGET_NAME_UPPER}_LIBRARIES ${TARGET_NAME} CACHE STRING "${TARGET_NAME} library location")
SET(INCLUDE_DIR ${${PROJECT_NAME_UPPER}_DIR}/googletest/include)
FILE(MAKE_DIRECTORY ${INCLUDE_DIR})
SET_TARGET_PROPERTIES(${PROJECT_NAME} PROPERTIES INTERFACE_INCLUDE_DIRECTORIES ${INCLUDE_DIR})

MARK_AS_ADVANCED(${TARGET_NAME_UPPER}_DIR ${TARGET_NAME_UPPER}_INCLUDE_DIRS ${TARGET_NAME_UPPER}_LIBRARIES)
MARK_AS_ADVANCED(${PROJECT_NAME_UPPER}_DIR)
Binary file removed 3rdParty/gtest/gtest-1.7.0.zip
Binary file not shown.
Binary file added 3rdParty/gtest/gtest-1.8.0.zip
Binary file not shown.
38 changes: 0 additions & 38 deletions 3rdParty/gtest/patch.diff

This file was deleted.

50 changes: 30 additions & 20 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
cmake_minimum_required(VERSION 3.2 FATAL_ERROR)

GET_FILENAME_COMPONENT(BRANCH ${CMAKE_CURRENT_SOURCE_DIR} NAME)
PROJECT(${BRANCH})
SET(TARGET_NAME ${BRANCH})
PROJECT(units)

OPTION(BUILD_TESTS "Build unit tests" ON)
OPTION(BUILD_DOCS "Build the documentation" OFF)

SET(CMAKE_CXX_STANDARD 14)
SET(CMAKE_CXX_STANDARD_REQUIRED TRUE)
Expand All @@ -11,22 +12,31 @@ if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
set( ENV{CL} /MP )
endif()

SET(VERSION_GTEST "1.7.0" CACHE STRING "Google Test framework version")

enable_testing ()

add_subdirectory(3rdParty/gnuWin)
add_subdirectory(3rdParty/gtest)

add_subdirectory(unitTests)
# header-only library target. To use this project as a subdirectory,
# add the following to your code:
#
# add_subdirectory(units) # or whatever you named the directory
# target_link_libraries(${PROJECT_NAME} units)
add_library(${PROJECT_NAME} INTERFACE)
target_include_directories(${PROJECT_NAME} INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/include)

# unit tests
if(BUILD_TESTS)
set(VERSION_GTEST "1.8.0" CACHE STRING "Google Test framework version")
enable_testing ()
add_subdirectory(3rdParty/gtest)
add_subdirectory(unitTests)
endif(BUILD_TESTS)

# add a target to generate API documentation with Doxygen
find_package(Doxygen)
if(DOXYGEN_FOUND)
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/docs/Doxyfile.in ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile @ONLY)
add_custom_target(doc ALL
${DOXYGEN_EXECUTABLE} ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/docs
COMMENT "Generating API documentation with Doxygen" VERBATIM
)
endif(DOXYGEN_FOUND)
if(BUILD_DOCS)
find_package(Doxygen)
if(DOXYGEN_FOUND)
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/docs/Doxyfile.in ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile @ONLY)
add_custom_target(doc ALL
${DOXYGEN_EXECUTABLE} ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/docs
COMMENT "Generating API documentation with Doxygen" VERBATIM
)
endif(DOXYGEN_FOUND)
endif(BUILD_DOCS)
14 changes: 6 additions & 8 deletions unitTests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
SET(TARGET_NAME unitLibTest)
PROJECT(${TARGET_NAME}) # it's a pun, GET IT!?!?
PROJECT(unitLibTest)

# use absolute paths to help coveralls
get_filename_component(MAIN main.cpp ABSOLUTE)
Expand All @@ -12,11 +11,10 @@ ${INC}

LIST(APPEND INCLUDE_DIRS
../include
${GTEST_INCLUDE_DIRS}
)

LIST(APPEND LIBRARIES
${GTEST_LIBRARIES}
gtest
)

# coveralls support
Expand All @@ -31,10 +29,10 @@ if (COVERALLS)
endif()

include_directories(${INCLUDE_DIRS})
ADD_EXECUTABLE(${TARGET_NAME} ${SOURCE_CODE})
ADD_TEST(NAME ${TARGET_NAME} COMMAND ${TARGET_NAME})
TARGET_LINK_LIBRARIES(${TARGET_NAME} ${LIBRARIES})
ADD_EXECUTABLE(${PROJECT_NAME} ${SOURCE_CODE})
ADD_TEST(NAME ${PROJECT_NAME} COMMAND ${PROJECT_NAME})
TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${LIBRARIES})

IF(WIN32)
set_target_properties(${TARGET_NAME} PROPERTIES LINK_FLAGS "/machine:x64")
set_target_properties(${PROJECT_NAME} PROPERTIES LINK_FLAGS "/machine:x64")
ENDIF(WIN32)

0 comments on commit f20b07e

Please sign in to comment.