Commit 425f609fcc9c1f18618756e1cf3b29abc2c50860

Authored by Nodeduino
Committed by Moritz Wirger
1 parent 4f2efcab

add new functionality to CMakeLists

- cleanup and document CMakeLists
- add hueplusplus-config.cmake to make it easier to use hueplusplus by simply calling find_package
- remove GLOB in test/CMakeLists.txt and exchange with set
CMakeLists.txt
1 cmake_minimum_required(VERSION 2.8.3) 1 cmake_minimum_required(VERSION 2.8.3)
2 project(hueplusplus) 2 project(hueplusplus)
3 3
4 -# uninstall target 4 +# options to set
  5 +option(hueplusplus_TESTS "Build tests" OFF)
  6 +
  7 +# get the correct installation directory for add_library() to work
  8 +if(WIN32 AND NOT CYGWIN)
  9 + set(DEF_INSTALL_CMAKE_DIR cmake)
  10 +else()
  11 + set(DEF_INSTALL_CMAKE_DIR lib/cmake/hueplusplus)
  12 +endif()
  13 +set(INSTALL_CMAKE_DIR ${DEF_INSTALL_CMAKE_DIR} CACHE PATH "Installation directory for CMake files")
  14 +
  15 +
  16 +# target for uninstall
5 if(NOT TARGET uninstall) 17 if(NOT TARGET uninstall)
6 configure_file( 18 configure_file(
7 "${CMAKE_CURRENT_SOURCE_DIR}/cmake_uninstall.cmake.in" 19 "${CMAKE_CURRENT_SOURCE_DIR}/cmake_uninstall.cmake.in"
@@ -12,8 +24,7 @@ if(NOT TARGET uninstall) @@ -12,8 +24,7 @@ if(NOT TARGET uninstall)
12 COMMAND ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake) 24 COMMAND ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake)
13 endif() 25 endif()
14 26
15 -option(hueplusplus_TESTS "Build tests" OFF)  
16 - 27 +# if we are on a apple machine this is needed
17 if (1 AND APPLE) 28 if (1 AND APPLE)
18 set(CMAKE_MACOSX_RPATH 1) 29 set(CMAKE_MACOSX_RPATH 1)
19 endif() 30 endif()
hueplusplus/CMakeLists.txt
@@ -11,12 +11,14 @@ set(hueplusplus_SOURCES @@ -11,12 +11,14 @@ set(hueplusplus_SOURCES
11 ${CMAKE_CURRENT_SOURCE_DIR}/UPnP.cpp 11 ${CMAKE_CURRENT_SOURCE_DIR}/UPnP.cpp
12 ) 12 )
13 13
  14 +# on windows we want to compile the winHttpHandler
14 if(WIN32) 15 if(WIN32)
15 set(hueplusplus_SOURCES 16 set(hueplusplus_SOURCES
16 ${hueplusplus_SOURCES} 17 ${hueplusplus_SOURCES}
17 ${CMAKE_CURRENT_SOURCE_DIR}/winHttpHandler.cpp 18 ${CMAKE_CURRENT_SOURCE_DIR}/winHttpHandler.cpp
18 ) 19 )
19 endif() 20 endif()
  21 +# whereas on linux we want the linHttpHandler
20 if(UNIX) 22 if(UNIX)
21 set(hueplusplus_SOURCES 23 set(hueplusplus_SOURCES
22 ${hueplusplus_SOURCES} 24 ${hueplusplus_SOURCES}
@@ -24,16 +26,27 @@ if(UNIX) @@ -24,16 +26,27 @@ if(UNIX)
24 ) 26 )
25 endif() 27 endif()
26 28
  29 +# hueplusplus shared library
27 add_library(hueplusplusshared SHARED ${hueplusplus_SOURCES}) 30 add_library(hueplusplusshared SHARED ${hueplusplus_SOURCES})
28 set_property(TARGET hueplusplusshared PROPERTY CXX_STANDARD 14) 31 set_property(TARGET hueplusplusshared PROPERTY CXX_STANDARD 14)
29 set_property(TARGET hueplusplusshared PROPERTY CXX_EXTENSIONS OFF) 32 set_property(TARGET hueplusplusshared PROPERTY CXX_EXTENSIONS OFF)
30 33
  34 +# hueplusplus static library
31 add_library(hueplusplusstatic STATIC ${hueplusplus_SOURCES}) 35 add_library(hueplusplusstatic STATIC ${hueplusplus_SOURCES})
32 set_property(TARGET hueplusplusstatic PROPERTY CXX_STANDARD 14) 36 set_property(TARGET hueplusplusstatic PROPERTY CXX_STANDARD 14)
33 set_property(TARGET hueplusplusstatic PROPERTY CXX_EXTENSIONS OFF) 37 set_property(TARGET hueplusplusstatic PROPERTY CXX_EXTENSIONS OFF)
34 install(TARGETS hueplusplusstatic DESTINATION lib) 38 install(TARGETS hueplusplusstatic DESTINATION lib)
35 install(FILES ${hueplusplus_HEADERS} DESTINATION include) 39 install(FILES ${hueplusplus_HEADERS} DESTINATION include)
36 40
  41 +# Export the package for use from the build-tree
  42 +# (this registers the build-tree with a global CMake-registry)
  43 +export(PACKAGE hueplusplus)
  44 +# Create the hueplusplus-config.cmake
  45 +configure_file (hueplusplus-config.cmake.in "${PROJECT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/hueplusplus-config.cmake" @ONLY)
  46 +# Install hueplusplus-config.cmake
  47 +install(FILES "${PROJECT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/hueplusplus-config.cmake" DESTINATION "${INSTALL_CMAKE_DIR}" COMPONENT dev)
  48 +
  49 +# if the user decided to use tests add the subdirectory
37 if(hueplusplus_TESTS) 50 if(hueplusplus_TESTS)
38 set(HuePlusPlus_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}) 51 set(HuePlusPlus_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
39 add_subdirectory("test") 52 add_subdirectory("test")
hueplusplus/hueplusplus-config.cmake.in 0 → 100644
  1 +# -config file of the hueplusplus package
  2 +# It defines the following
  3 +# hueplusplus_FOUND - which indicates that the module was found
  4 +# hueplusplus_INCLUDE_DIR
  5 +
  6 +set(hueplusplus_FOUND TRUE)
  7 +set(hueplusplus_INCLUDE_DIR "@INSTALL_INCLUDE_DIR@")
hueplusplus/test/CMakeLists.txt
@@ -2,13 +2,15 @@ @@ -2,13 +2,15 @@
2 configure_file(CMakeLists.txt.in googletest-download/CMakeLists.txt) 2 configure_file(CMakeLists.txt.in googletest-download/CMakeLists.txt)
3 execute_process(COMMAND ${CMAKE_COMMAND} -G ${CMAKE_GENERATOR} . 3 execute_process(COMMAND ${CMAKE_COMMAND} -G ${CMAKE_GENERATOR} .
4 RESULT_VARIABLE result 4 RESULT_VARIABLE result
5 -WORKING_DIRECTORY "${CMAKE_BINARY_DIR}/hueplusplus/test/googletest-download" ) 5 + WORKING_DIRECTORY "${CMAKE_BINARY_DIR}/hueplusplus/test/googletest-download"
  6 +)
6 if(result) 7 if(result)
7 message(FATAL_ERROR "CMake step for googletest failed: ${result}") 8 message(FATAL_ERROR "CMake step for googletest failed: ${result}")
8 endif() 9 endif()
9 execute_process(COMMAND "${CMAKE_COMMAND}" --build . 10 execute_process(COMMAND "${CMAKE_COMMAND}" --build .
10 RESULT_VARIABLE result 11 RESULT_VARIABLE result
11 -WORKING_DIRECTORY "${CMAKE_BINARY_DIR}/hueplusplus/test/googletest-download" ) 12 + WORKING_DIRECTORY "${CMAKE_BINARY_DIR}/hueplusplus/test/googletest-download"
  13 +)
12 if(result) 14 if(result)
13 message(FATAL_ERROR "Build step for googletest failed: ${result}") 15 message(FATAL_ERROR "Build step for googletest failed: ${result}")
14 endif() 16 endif()
@@ -20,7 +22,8 @@ set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) @@ -20,7 +22,8 @@ set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
20 # Add googletest directly to our build. This defines 22 # Add googletest directly to our build. This defines
21 # the gtest and gtest_main targets. 23 # the gtest and gtest_main targets.
22 add_subdirectory(${CMAKE_BINARY_DIR}/googletest-src EXCLUDE_FROM_ALL 24 add_subdirectory(${CMAKE_BINARY_DIR}/googletest-src EXCLUDE_FROM_ALL
23 - ${CMAKE_BINARY_DIR}/googletest-build EXCLUDE_FROM_ALL) 25 + ${CMAKE_BINARY_DIR}/googletest-build EXCLUDE_FROM_ALL
  26 +)
24 27
25 # The gtest/gtest_main targets carry header search path 28 # The gtest/gtest_main targets carry header search path
26 # dependencies automatically when using CMake 2.8.11 or 29 # dependencies automatically when using CMake 2.8.11 or
@@ -29,9 +32,14 @@ if (CMAKE_VERSION VERSION_LESS 2.8.11) @@ -29,9 +32,14 @@ if (CMAKE_VERSION VERSION_LESS 2.8.11)
29 include_directories("${gtest_SOURCE_DIR}/include" EXCLUDE_FROM_ALL) 32 include_directories("${gtest_SOURCE_DIR}/include" EXCLUDE_FROM_ALL)
30 endif() 33 endif()
31 34
32 -# get all test sources. Maybe use something else than GLOB  
33 -file(GLOB TEST_SOURCES test_*) 35 +# define all test sources
  36 +set(TEST_SOURCES
  37 + ${CMAKE_CURRENT_SOURCE_DIR}/test_Main.cpp
  38 + ${CMAKE_CURRENT_SOURCE_DIR}/test_Hue.cpp
  39 + ${CMAKE_CURRENT_SOURCE_DIR}/test_UPnP.cpp
  40 +)
34 41
  42 +# test executable
35 add_executable(test_HuePlusPlus ${TEST_SOURCES} ${hueplusplus_SOURCES}) 43 add_executable(test_HuePlusPlus ${TEST_SOURCES} ${hueplusplus_SOURCES})
36 target_link_libraries(test_HuePlusPlus gtest gmock) 44 target_link_libraries(test_HuePlusPlus gtest gmock)
37 # prevent Main.cpp from defining main() 45 # prevent Main.cpp from defining main()
@@ -40,16 +48,15 @@ target_include_directories(test_HuePlusPlus PUBLIC ${GTest_INCLUDE_DIRS}) @@ -40,16 +48,15 @@ target_include_directories(test_HuePlusPlus PUBLIC ${GTest_INCLUDE_DIRS})
40 target_include_directories(test_HuePlusPlus PUBLIC ${HuePlusPlus_INCLUDE_DIR}) 48 target_include_directories(test_HuePlusPlus PUBLIC ${HuePlusPlus_INCLUDE_DIR})
41 set_property(TARGET test_HuePlusPlus PROPERTY CXX_STANDARD 14) 49 set_property(TARGET test_HuePlusPlus PROPERTY CXX_STANDARD 14)
42 set_property(TARGET test_HuePlusPlus PROPERTY CXX_EXTENSIONS OFF) 50 set_property(TARGET test_HuePlusPlus PROPERTY CXX_EXTENSIONS OFF)
  51 +# add custom target to make it simple to run the tests
43 add_custom_target("unittest" 52 add_custom_target("unittest"
44 -  
45 # make test_HuePlusPlus executable 53 # make test_HuePlusPlus executable
46 COMMAND "make" test_HuePlusPlus 54 COMMAND "make" test_HuePlusPlus
47 -  
48 # Run the executable 55 # Run the executable
49 COMMAND "${CMAKE_BINARY_DIR}/hueplusplus/test/test_HuePlusPlus" 56 COMMAND "${CMAKE_BINARY_DIR}/hueplusplus/test/test_HuePlusPlus"
50 ) 57 )
51 58
52 -# Check for prerequisites 59 +# Check for coverage test prerequisites
53 find_program( GCOV_PATH gcov ) 60 find_program( GCOV_PATH gcov )
54 find_program( LCOV_PATH lcov ) 61 find_program( LCOV_PATH lcov )
55 62
@@ -74,6 +81,7 @@ if(LCOV_PATH AND GCOV_PATH) @@ -74,6 +81,7 @@ if(LCOV_PATH AND GCOV_PATH)
74 # testcov_HuePlusPlus PROPERTIES 81 # testcov_HuePlusPlus PROPERTIES
75 # LINK_FLAGS "-O0 -g -Lgcov -fprofile-arcs -ftest-coverage" 82 # LINK_FLAGS "-O0 -g -Lgcov -fprofile-arcs -ftest-coverage"
76 #) 83 #)
  84 + # exclude some special files we do not want to profile
77 set(COVERAGE_EXCLUDES 85 set(COVERAGE_EXCLUDES
78 '/usr/*' # unix 86 '/usr/*' # unix
79 '*/build/*' 87 '*/build/*'