From d59586630e9444f04ee12577e3e5977e93fc9b86 Mon Sep 17 00:00:00 2001 From: Jesse Beder Date: Mon, 24 Mar 2014 23:34:26 -0500 Subject: [PATCH 1/5] Fix clang compiler settings, and properly set up warnings so they don't interfere with gtest and gmock --- CMakeLists.txt | 33 +++++++++++++++++++++------------ test/CMakeLists.txt | 12 ++++++++++-- 2 files changed, 31 insertions(+), 14 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index cba9279..efa8090 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -68,6 +68,15 @@ else() add_definitions(-DYAML_CPP_NO_CONTRIB) endif() +set(all_sources + ${sources} + ${public_headers} + ${private_headers} + ${contrib_sources} + ${contrib_public_headers} + ${contrib_private_headers} +) + if(VERBOSE) message(STATUS "sources: ${sources}") message(STATUS "public_headers: ${public_headers}") @@ -84,6 +93,9 @@ include_directories(${YAML_CPP_SOURCE_DIR}/src) ### ### General compilation settings ### +set(yaml_c_flags ${CMAKE_C_FLAGS}) +set(yaml_cxx_flags ${CMAKE_CXX_FLAGS}) + if(BUILD_SHARED_LIBS) set(LABEL_SUFFIX "shared") else() @@ -110,8 +122,9 @@ if(WIN32) endif() endif() -# GCC specialities -if(CMAKE_COMPILER_IS_GNUCXX) +# GCC or Clang specialities +if("${CMAKE_CXX_COMPILER_ID}" MATCHES "GNU" OR + "${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang") ### General stuff if(WIN32) set(CMAKE_SHARED_LIBRARY_PREFIX "") # DLLs do not have a "lib" prefix @@ -137,7 +150,7 @@ if(CMAKE_COMPILER_IS_GNUCXX) set(GCC_EXTRA_OPTIONS "${GCC_EXTRA_OPTIONS} ${FLAG_TESTED}") endif() # - set(CMAKE_CXX_FLAGS "-Wall ${GCC_EXTRA_OPTIONS} -pedantic -Wno-long-long ${CMAKE_CXX_FLAGS}") + set(yaml_cxx_flags "-Wall ${GCC_EXTRA_OPTIONS} -pedantic -Wno-long-long ${yaml_cxx_flags}") ### Make specific if(${CMAKE_BUILD_TOOL} STREQUAL make OR ${CMAKE_BUILD_TOOL} STREQUAL gmake) @@ -175,7 +188,7 @@ if(MSVC) endif() # correct linker options - foreach(flag_var CMAKE_C_FLAGS CMAKE_CXX_FLAGS) + foreach(flag_var yaml_c_flags yaml_cxx_flags) foreach(config_name "" DEBUG RELEASE MINSIZEREL RELWITHDEBINFO) set(var_name "${flag_var}") if(NOT "${config_name}" STREQUAL "") @@ -201,7 +214,7 @@ if(MSVC) # /W3 = set warning level; see http://msdn.microsoft.com/en-us/library/thxezb7y.aspx # /wd4127 = disable warning C4127 "conditional expression is constant"; see http://msdn.microsoft.com/en-us/library/6t66728h.aspx # /wd4355 = disable warning C4355 "'this' : used in base member initializer list"; http://msdn.microsoft.com/en-us/library/3c594ae3.aspx - set(CMAKE_CXX_FLAGS "/W3 /wd4127 /wd4355 /D_SCL_SECURE_NO_WARNINGS ${CMAKE_CXX_FLAGS}") + set(yaml_cxx_flags "/W3 /wd4127 /wd4355 /D_SCL_SECURE_NO_WARNINGS ${yaml_cxx_flags}") endif() @@ -229,13 +242,9 @@ set(_INSTALL_DESTINATIONS ### ### Library ### -add_library(yaml-cpp - ${sources} - ${public_headers} - ${private_headers} - ${contrib_sources} - ${contrib_public_headers} - ${contrib_private_headers} +add_library(yaml-cpp ${all_sources}) +set_target_properties(yaml-cpp PROPERTIES + COMPILE_FLAGS "${yaml_c_flags} ${yaml_cxx_flags}" ) set_target_properties(yaml-cpp PROPERTIES diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 7108a65..9edf468 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -1,13 +1,18 @@ set(gtest_force_shared_crt ${MSVC_SHARED_RT} CACHE BOOL "Use shared (DLL) run-time lib even when Google Test built as a static lib.") add_subdirectory(gmock-1.7.0) -include_directories(gmock-1.7.0/gtest/include) -include_directories(gmock-1.7.0/include) +include_directories(SYSTEM gmock-1.7.0/gtest/include) +include_directories(SYSTEM gmock-1.7.0/include) if(WIN32 AND BUILD_SHARED_LIBS) add_definitions("-DGTEST_LINKED_AS_SHARED_LIBRARY") endif() +if("${CMAKE_CXX_COMPILER_ID}" MATCHES "GNU" OR + "${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang") + set(yaml_test_flags "-Wno-c99-extensions") +endif() + file(GLOB test_headers [a-z_]*.h) file(GLOB test_sources [a-z_]*.cpp integration/[a-z_]*.cpp) file(GLOB test_core_sources core/[a-z]*.cpp) @@ -20,6 +25,9 @@ add_executable(run-tests ${test_sources} ${test_headers} ) +set_target_properties(run-tests PROPERTIES + COMPILE_FLAGS "${yaml_c_flags} ${yaml_cxx_flags} ${yaml_test_flags}" +) target_link_libraries(run-tests yaml-cpp gtest gmock) #add_test(yaml-reader-test run-tests) From dc8d91ce4520ae6891a0f4ad702dd28d16bb12bb Mon Sep 17 00:00:00 2001 From: Jesse Beder Date: Mon, 24 Mar 2014 23:46:03 -0500 Subject: [PATCH 2/5] Add make format target to run clang-format --- CMakeLists.txt | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index efa8090..02a5bd4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -153,7 +153,7 @@ if("${CMAKE_CXX_COMPILER_ID}" MATCHES "GNU" OR set(yaml_cxx_flags "-Wall ${GCC_EXTRA_OPTIONS} -pedantic -Wno-long-long ${yaml_cxx_flags}") ### Make specific - if(${CMAKE_BUILD_TOOL} STREQUAL make OR ${CMAKE_BUILD_TOOL} STREQUAL gmake) + if(${CMAKE_BUILD_TOOL} MATCHES make OR ${CMAKE_BUILD_TOOL} MATCHES gmake) add_custom_target(debuggable $(MAKE) clean COMMAND ${CMAKE_COMMAND} -DCMAKE_BUILD_TYPE=Debug ${CMAKE_SOURCE_DIR} COMMENT "Adjusting settings for debug compilation" @@ -162,6 +162,10 @@ if("${CMAKE_CXX_COMPILER_ID}" MATCHES "GNU" OR COMMAND ${CMAKE_COMMAND} -DCMAKE_BUILD_TYPE=Release ${CMAKE_SOURCE_DIR} COMMENT "Adjusting settings for release compilation" VERBATIM) + add_custom_target(format + COMMAND clang-format --style=file -i ${all_sources} + COMMENT "Running clang-format" + VERBATIM) endif() endif() From a4a79835c9643303ed0fee44755ba8a7447f8274 Mon Sep 17 00:00:00 2001 From: Jesse Beder Date: Mon, 24 Mar 2014 23:47:46 -0500 Subject: [PATCH 3/5] Suppress gtest warnings on xcode --- test/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 9edf468..35b57a1 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -10,7 +10,7 @@ endif() if("${CMAKE_CXX_COMPILER_ID}" MATCHES "GNU" OR "${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang") - set(yaml_test_flags "-Wno-c99-extensions") + set(yaml_test_flags "-Wno-c99-extensions -Wno-variadic-macros -Wno-sign-compare") endif() file(GLOB test_headers [a-z_]*.h) From e0b293e7577728474b8f048ec8bd1f458201f745 Mon Sep 17 00:00:00 2001 From: Jesse Beder Date: Tue, 25 Mar 2014 00:02:16 -0500 Subject: [PATCH 4/5] Add test and util sources to make format --- CMakeLists.txt | 42 ++++++++++++++++++++++++++++++++++++------ test/CMakeLists.txt | 1 + util/CMakeLists.txt | 2 ++ 3 files changed, 39 insertions(+), 6 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 02a5bd4..b1e999c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -54,6 +54,29 @@ option(MSVC_STHREADED_RT "MSVC: Build with single-threaded static runtime libs ( ### ### Sources, headers, directories and libs ### + +# From http://www.cmake.org/pipermail/cmake/2010-March/035992.html: +# function to collect all the sources from sub-directories +# into a single list +function(add_sources) + get_property(is_defined GLOBAL PROPERTY SRCS_LIST DEFINED) + if(NOT is_defined) + define_property(GLOBAL PROPERTY SRCS_LIST + BRIEF_DOCS "List of source files" + FULL_DOCS "List of all source files in the entire project") + endif() + # make absolute paths + set(SRCS) + foreach(s IN LISTS ARGN) + if(NOT IS_ABSOLUTE "${s}") + get_filename_component(s "${s}" ABSOLUTE) + endif() + list(APPEND SRCS "${s}") + endforeach() + # append to global list + set_property(GLOBAL APPEND PROPERTY SRCS_LIST "${SRCS}") +endfunction(add_sources) + set(header_directory "include/yaml-cpp/") file(GLOB sources "src/[a-zA-Z]*.cpp") @@ -68,7 +91,7 @@ else() add_definitions(-DYAML_CPP_NO_CONTRIB) endif() -set(all_sources +set(library_sources ${sources} ${public_headers} ${private_headers} @@ -76,6 +99,7 @@ set(all_sources ${contrib_public_headers} ${contrib_private_headers} ) +add_sources(${library_sources}) if(VERBOSE) message(STATUS "sources: ${sources}") @@ -162,10 +186,6 @@ if("${CMAKE_CXX_COMPILER_ID}" MATCHES "GNU" OR COMMAND ${CMAKE_COMMAND} -DCMAKE_BUILD_TYPE=Release ${CMAKE_SOURCE_DIR} COMMENT "Adjusting settings for release compilation" VERBATIM) - add_custom_target(format - COMMAND clang-format --style=file -i ${all_sources} - COMMENT "Running clang-format" - VERBATIM) endif() endif() @@ -246,7 +266,7 @@ set(_INSTALL_DESTINATIONS ### ### Library ### -add_library(yaml-cpp ${all_sources}) +add_library(yaml-cpp ${library_sources}) set_target_properties(yaml-cpp PROPERTIES COMPILE_FLAGS "${yaml_c_flags} ${yaml_cxx_flags}" ) @@ -296,3 +316,13 @@ if(YAML_CPP_BUILD_TOOLS) add_subdirectory(test) add_subdirectory(util) endif() + +### Formatting +if(${CMAKE_BUILD_TOOL} MATCHES make OR ${CMAKE_BUILD_TOOL} MATCHES gmake) + get_property(all_sources GLOBAL PROPERTY SRCS_LIST) + message(STATUS ${all_sources}) + add_custom_target(format + COMMAND clang-format --style=file -i ${all_sources} + COMMENT "Running clang-format" + VERBATIM) +endif() \ No newline at end of file diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 35b57a1..6754fc9 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -18,6 +18,7 @@ file(GLOB test_sources [a-z_]*.cpp integration/[a-z_]*.cpp) file(GLOB test_core_sources core/[a-z]*.cpp) list(APPEND test_sources ${test_core_sources}) +add_sources(${test_sources} ${test_headers}) include_directories(${YAML_CPP_SOURCE_DIR}/test) diff --git a/util/CMakeLists.txt b/util/CMakeLists.txt index 2661c2e..7b35b79 100644 --- a/util/CMakeLists.txt +++ b/util/CMakeLists.txt @@ -1,5 +1,7 @@ +add_sources(sandbox.cpp) add_executable(sandbox sandbox.cpp) target_link_libraries(sandbox yaml-cpp) +add_sources(read.cpp) add_executable(read read.cpp) target_link_libraries(read yaml-cpp) From edf8ebe2467c2ff1cb2f20cc311c296ccbb00b17 Mon Sep 17 00:00:00 2001 From: Jesse Beder Date: Tue, 25 Mar 2014 00:04:04 -0500 Subject: [PATCH 5/5] Remove stray log message in cmake --- CMakeLists.txt | 1 - 1 file changed, 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index b1e999c..f5d9444 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -320,7 +320,6 @@ endif() ### Formatting if(${CMAKE_BUILD_TOOL} MATCHES make OR ${CMAKE_BUILD_TOOL} MATCHES gmake) get_property(all_sources GLOBAL PROPERTY SRCS_LIST) - message(STATUS ${all_sources}) add_custom_target(format COMMAND clang-format --style=file -i ${all_sources} COMMENT "Running clang-format"