From a478c44b69296185c7affce1d25ffd378fff83a4 Mon Sep 17 00:00:00 2001 From: Zach Laine Date: Fri, 12 May 2017 20:26:50 -0500 Subject: [PATCH] Move the test library sources to src/, and compile a real lib for the test and perf programs to use. --- CMakeLists.txt | 68 ++++++++++++++++++++++++++++-- perf/CMakeLists.txt | 14 +----- {test => src}/basic_structures.cpp | 0 {test => src}/block_style.cpp | 0 {test => src}/characters.cpp | 0 {test => src}/flow_style.cpp | 0 {test => src}/stream.cpp | 0 test/CMakeLists.txt | 36 +--------------- 8 files changed, 68 insertions(+), 50 deletions(-) rename {test => src}/basic_structures.cpp (100%) rename {test => src}/block_style.cpp (100%) rename {test => src}/characters.cpp (100%) rename {test => src}/flow_style.cpp (100%) rename {test => src}/stream.cpp (100%) diff --git a/CMakeLists.txt b/CMakeLists.txt index b5369cf1..e21fced9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -29,9 +29,71 @@ if (PYTHONINTERP_FOUND) message("-- Found Python ${PYTHON_VERSION_STRING} (${PYTHON_EXECUTABLE})") endif () -add_library(yaml INTERFACE) -target_include_directories(yaml INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}) -target_link_libraries(yaml INTERFACE boost) + +################################################## +# Sanitizers +################################################## +set(USE_ASAN false CACHE BOOL "Set to true to enable -fsanitize=address when building tests.") +set(USE_UBSAN false CACHE BOOL "Set to true to enable -fsanitize=undefined when building tests.") +if (USE_ASAN AND USE_UBSAN) + message(FATAL_ERROR "USE_ASAN and USE_UBSAN must not be enabled at the same time") +elseif (USE_ASAN) + set(link_flags -fsanitize=address) +elseif (USE_UBSAN) + set(link_flags -fsanitize=undefined) +endif() + + +# TODO: Turn this on if a static lib is desired; there's no need for it now. +if (false) +################################################## +# Static lib +################################################## +add_library( + yaml-static + STATIC + src/characters.cpp + src/basic_structures.cpp + src/flow_style.cpp + src/block_style.cpp + src/stream.cpp +) +target_compile_options(yaml-static PUBLIC ${cxx_defs}) +target_include_directories(yaml-static PUBLIC ${CMAKE_SOURCE_DIR}) +target_link_libraries(yaml-static PUBLIC boost) +target_link_libraries(yaml-static ${link_flags}) +if (USE_ASAN OR USE_UBSAN) + target_compile_options(yaml-static PUBLIC ${link_flags}) +endif() +if (clang_on_linux) + target_link_libraries(yaml-static c++) +endif () +endif () + + +################################################## +# Dynamic lib +################################################## +add_library( + yaml + SHARED + src/characters.cpp + src/basic_structures.cpp + src/flow_style.cpp + src/block_style.cpp + src/stream.cpp +) +target_compile_options(yaml PUBLIC ${cxx_defs}) +target_include_directories(yaml PUBLIC ${CMAKE_SOURCE_DIR}) +target_link_libraries(yaml PUBLIC boost) +target_link_libraries(yaml ${link_flags}) +if (USE_ASAN OR USE_UBSAN) + target_compile_options(yam PUBLIC ${link_flags}) +endif() +if (clang_on_linux) + target_link_libraries(yaml c++) +endif () + add_subdirectory(test) add_subdirectory(perf) diff --git a/perf/CMakeLists.txt b/perf/CMakeLists.txt index 501ab768..57f01591 100644 --- a/perf/CMakeLists.txt +++ b/perf/CMakeLists.txt @@ -1,17 +1,5 @@ -add_definitions(${cxx_defs}) - -# TODO: Add library to top-level CMakeLists.txt, and reuse it here and in test/. - macro(add_perf_executable name) - add_executable( - ${name} - ${name}.cpp - ${CMAKE_SOURCE_DIR}/test/characters.cpp - ${CMAKE_SOURCE_DIR}/test/basic_structures.cpp - ${CMAKE_SOURCE_DIR}/test/flow_style.cpp - ${CMAKE_SOURCE_DIR}/test/block_style.cpp - ${CMAKE_SOURCE_DIR}/test/stream.cpp - ) + add_executable(${name} ${name}.cpp) target_link_libraries(${name} yaml benchmark ${link_flags}) if (clang_on_linux) target_link_libraries(${name} c++) diff --git a/test/basic_structures.cpp b/src/basic_structures.cpp similarity index 100% rename from test/basic_structures.cpp rename to src/basic_structures.cpp diff --git a/test/block_style.cpp b/src/block_style.cpp similarity index 100% rename from test/block_style.cpp rename to src/block_style.cpp diff --git a/test/characters.cpp b/src/characters.cpp similarity index 100% rename from test/characters.cpp rename to src/characters.cpp diff --git a/test/flow_style.cpp b/src/flow_style.cpp similarity index 100% rename from test/flow_style.cpp rename to src/flow_style.cpp diff --git a/test/stream.cpp b/src/stream.cpp similarity index 100% rename from test/stream.cpp rename to src/stream.cpp diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index a9ae6cc8..2d1c092a 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -6,20 +6,8 @@ enable_testing() add_custom_target(check COMMAND ${CMAKE_CTEST_COMMAND} -C ${CMAKE_CFG_INTDIR}) -set(USE_ASAN false CACHE BOOL "Set to true to enable -fsanitize=address when building tests.") -set(USE_UBSAN false CACHE BOOL "Set to true to enable -fsanitize=undefined when building tests.") -if (USE_ASAN AND USE_UBSAN) - message(FATAL_ERROR "USE_ASAN and USE_UBSAN must not be enabled at the same time") -elseif (USE_ASAN) - set(link_flags -fsanitize=address) -elseif (USE_UBSAN) - set(link_flags -fsanitize=undefined) -endif() - set(BUILD_HEADER_ONLY_TEST false CACHE BOOL "Set to true to build a test app with -DYAML_HEADER_ONLY=1.") -add_definitions(${cxx_defs}) - ################################################## # Header-only build test @@ -36,26 +24,6 @@ if (clang_on_linux) endif () endif () -################################################## -# Static test lib -################################################## -add_library( - yaml_test_lib - STATIC - characters.cpp - basic_structures.cpp - flow_style.cpp - block_style.cpp - stream.cpp -) -target_link_libraries(yaml_test_lib yaml gtest ${link_flags}) -if (USE_ASAN OR USE_UBSAN) - target_compile_options(yaml_test_lib PUBLIC ${link_flags}) -endif() -if (clang_on_linux) - target_link_libraries(yaml_test_lib c++) -endif () - ################################################## # Parse-only test executable ################################################## @@ -63,7 +31,7 @@ add_executable( parse_test parse_yaml_test.cpp ) -target_link_libraries(parse_test yaml yaml_test_lib gtest ${link_flags}) +target_link_libraries(parse_test yaml gtest ${link_flags}) if (clang_on_linux) target_link_libraries(parse_test c++) endif () @@ -75,7 +43,7 @@ add_executable( parse_compare_test parse_yaml_compare_test.cpp ) -target_link_libraries(parse_compare_test yaml yaml_test_lib gtest ${link_flags}) +target_link_libraries(parse_compare_test yaml gtest ${link_flags}) if (clang_on_linux) target_link_libraries(parse_compare_test c++) endif ()