diff options
6 files changed, 96 insertions, 18 deletions
diff --git a/Tests/RunCMake/GenerateExportHeader/GEH-failures.cmake b/Tests/RunCMake/GenerateExportHeader/GEH-failures.cmake new file mode 100644 index 0000000..7182cdf --- /dev/null +++ b/Tests/RunCMake/GenerateExportHeader/GEH-failures.cmake @@ -0,0 +1,67 @@ +set(failure_test_executables + ${CMAKE_CURRENT_BINARY_DIR}/failure_test_targets) +file(WRITE ${failure_test_executables} "") + +# Check if we should do anything. If the compiler doesn't support hidden +# visibility, the failure tests won't fail, so just write an empty targets +# list and punt. +if(NOT WIN32 AND NOT CYGWIN AND NOT COMPILER_HAS_HIDDEN_VISIBILITY) + return() +endif() + +# Read the input source file +file(READ ${CMAKE_CURRENT_SOURCE_DIR}/exportheader_test.cpp content_post) +set(content_pre "") + +# Generate source files for failure test executables +set(counter 0) +while(1) + # Find first occurrence of link error marker in remaining content + string(REGEX MATCH "//([^;\n]+;) LINK ERROR( [(][^)]+[)])?\n(.*)" + match "${content_post}") + if(match STREQUAL "") + # No more matches + break() + endif() + + # Shift content buffers and extract failing statement + string(LENGTH "${content_post}" content_post_length) + string(LENGTH "${match}" matched_length) + math(EXPR shift_length "${content_post_length} - ${matched_length}") + + string(SUBSTRING "${content_post}" 0 ${shift_length} shift) + set(content_pre "${content_pre}${shift}") + set(content_post "${CMAKE_MATCH_3}") + set(content_active "//${CMAKE_MATCH_1} LINK ERROR${CMAKE_MATCH_2}") + set(statement "${CMAKE_MATCH_1}") + + # Check if potential error is conditional, and evaluate condition if so + string(REGEX REPLACE " [(]([^)]+)[)]" "\\1" condition "${CMAKE_MATCH_2}") + if(NOT condition STREQUAL "") + string(REGEX REPLACE " +" ";" condition "${condition}") + if(${condition}) + else() + message(STATUS "Not testing '${statement}'; " + "condition (${condition}) is FALSE") + set(content_pre "${content_pre}// link error removed\n") + continue() + endif() + endif() + + if(NOT skip) + message(STATUS "Creating failure test for '${statement}'") + math(EXPR counter "${counter} + 1") + + # Write new source file + set(out ${CMAKE_CURRENT_BINARY_DIR}/exportheader_failtest-${counter}.cpp) + file(WRITE ${out} "${content_pre}${statement}\n${content_post}") + + # Add executable for failure test + add_executable(GEH-fail-${counter} EXCLUDE_FROM_ALL ${out}) + target_link_libraries(GEH-fail-${counter} ${link_libraries}) + file(APPEND ${failure_test_executables} "GEH-fail-${counter}\n") + endif() + + # Add placeholder where failing statement was removed + set(content_pre "${content_pre}${content_active}\n") +endwhile() diff --git a/Tests/RunCMake/GenerateExportHeader/GEH-link-error-result.txt b/Tests/RunCMake/GenerateExportHeader/GEH-link-error-result.txt new file mode 100644 index 0000000..d197c91 --- /dev/null +++ b/Tests/RunCMake/GenerateExportHeader/GEH-link-error-result.txt @@ -0,0 +1 @@ +[^0] diff --git a/Tests/RunCMake/GenerateExportHeader/GEH-link-error-stderr.txt b/Tests/RunCMake/GenerateExportHeader/GEH-link-error-stderr.txt new file mode 100644 index 0000000..8d98f9d --- /dev/null +++ b/Tests/RunCMake/GenerateExportHeader/GEH-link-error-stderr.txt @@ -0,0 +1 @@ +.* diff --git a/Tests/RunCMake/GenerateExportHeader/GEH.cmake b/Tests/RunCMake/GenerateExportHeader/GEH.cmake index ee0871b..cddba29 100644 --- a/Tests/RunCMake/GenerateExportHeader/GEH.cmake +++ b/Tests/RunCMake/GenerateExportHeader/GEH.cmake @@ -123,3 +123,5 @@ target_compile_definitions(GenerateExportHeader "SRC_DIR=\"${CMAKE_CURRENT_SOURCE_DIR}/reference/${_platform}\"" "BIN_DIR=\"${CMAKE_CURRENT_BINARY_DIR}\"" ) + +include(${CMAKE_CURRENT_LIST_DIR}/GEH-failures.cmake) diff --git a/Tests/RunCMake/GenerateExportHeader/RunCMakeTest.cmake b/Tests/RunCMake/GenerateExportHeader/RunCMakeTest.cmake index e534c1f..9423ef5 100644 --- a/Tests/RunCMake/GenerateExportHeader/RunCMakeTest.cmake +++ b/Tests/RunCMake/GenerateExportHeader/RunCMakeTest.cmake @@ -12,6 +12,16 @@ function(run_GEH) run_cmake(GEH) run_cmake_command(GEH-build ${CMAKE_COMMAND} --build . --config Debug) run_cmake_command(GEH-run ${RunCMake_TEST_BINARY_DIR}/GenerateExportHeader) + + file(STRINGS "${RunCMake_TEST_BINARY_DIR}/failure_test_targets" + failure_test_targets) + + foreach(failure_test_target ${failure_test_targets}) + run_cmake_command(GEH-link-error ${CMAKE_COMMAND} + --build . + --config Debug + --target ${failure_test_target}) + endforeach() endfunction() run_GEH() diff --git a/Tests/RunCMake/GenerateExportHeader/exportheader_test.cpp b/Tests/RunCMake/GenerateExportHeader/exportheader_test.cpp index 26bea7e..3a4e7dd 100644 --- a/Tests/RunCMake/GenerateExportHeader/exportheader_test.cpp +++ b/Tests/RunCMake/GenerateExportHeader/exportheader_test.cpp @@ -3,14 +3,6 @@ #include "libstatic.h" -// #define BUILD_FAIL - -#ifndef BUILD_FAIL -#define DOES_NOT_BUILD(function) -#else -#define DOES_NOT_BUILD(function) function -#endif - #include <fstream> #include <iostream> #include <stdlib.h> @@ -69,32 +61,35 @@ int main() l.libshared_exported(); l.libshared_deprecated(); l.libshared_not_exported(); - - DOES_NOT_BUILD(l.libshared_excluded();) +#if defined(_WIN32) || defined(__CYGWIN__) + l.libshared_excluded(); +#else +// l.libshared_excluded(); LINK ERROR (NOT WIN32 AND NOT CYGWIN) +#endif } { LibsharedNotExported l; - DOES_NOT_BUILD(l.libshared();) + // l.libshared(); LINK ERROR l.libshared_exported(); l.libshared_deprecated(); - DOES_NOT_BUILD(l.libshared_not_exported();) - DOES_NOT_BUILD(l.libshared_excluded();) + // l.libshared_not_exported(); LINK ERROR + // l.libshared_excluded(); LINK ERROR } { LibsharedExcluded l; - DOES_NOT_BUILD(l.libshared();) + // l.libshared(); LINK ERROR l.libshared_exported(); l.libshared_deprecated(); - DOES_NOT_BUILD(l.libshared_not_exported();) - DOES_NOT_BUILD(l.libshared_excluded();) + // l.libshared_not_exported(); LINK ERROR + // l.libshared_excluded(); LINK ERROR } libshared_exported(); libshared_deprecated(); - DOES_NOT_BUILD(libshared_not_exported();) - DOES_NOT_BUILD(libshared_excluded();) + // libshared_not_exported(); LINK ERROR + // libshared_excluded(); LINK ERROR { Libstatic l; @@ -128,10 +123,12 @@ int main() libstatic_not_exported(); libstatic_excluded(); +#if defined(SRC_DIR) && defined(BIN_DIR) compare(SRC_DIR "/libshared_export.h", BIN_DIR "/libshared/libshared_export.h"); compare(SRC_DIR "/libstatic_export.h", BIN_DIR "/libstatic/libstatic_export.h"); +#endif return 0; } |