diff options
48 files changed, 783 insertions, 576 deletions
diff --git a/.gitlab/ci/ctest_test.cmake b/.gitlab/ci/ctest_test.cmake index 569139d..08ef18f 100644 --- a/.gitlab/ci/ctest_test.cmake +++ b/.gitlab/ci/ctest_test.cmake @@ -10,6 +10,11 @@ ctest_start(APPEND) include(ProcessorCount) ProcessorCount(nproc) +if (NOT "$ENV{CTEST_MAX_PARALLELISM}" STREQUAL "") + if (nproc GREATER "$ENV{CTEST_MAX_PARALLELISM}") + set(nproc "$ENV{CTEST_MAX_PARALLELISM}") + endif () +endif () include("${CMAKE_CURRENT_LIST_DIR}/ctest_exclusions.cmake") ctest_test( diff --git a/.gitlab/os-macos.yml b/.gitlab/os-macos.yml index 96ec264..47e81d1 100644 --- a/.gitlab/os-macos.yml +++ b/.gitlab/os-macos.yml @@ -8,6 +8,8 @@ # TODO: Factor this out so that each job selects the Xcode version to # use so that different versions can be tested in a single pipeline. DEVELOPER_DIR: "/Applications/Xcode-11.7.app/Contents/Developer" + # Avoid conflicting with other projects running on the same machine. + SCCACHE_SERVER_PORT: 4227 ### Build and test diff --git a/.gitlab/os-windows.yml b/.gitlab/os-windows.yml index 63b8758..61a2018 100644 --- a/.gitlab/os-windows.yml +++ b/.gitlab/os-windows.yml @@ -5,6 +5,8 @@ .windows: variables: GIT_CLONE_PATH: "$CI_BUILDS_DIR\\cmake ci ext\\$CI_CONCURRENT_ID" + # Avoid conflicting with other projects running on the same machine. + SCCACHE_SERVER_PORT: 4227 ### Build and test diff --git a/Help/guide/importing-exporting/index.rst b/Help/guide/importing-exporting/index.rst index b0cfb71..2e6e06d 100644 --- a/Help/guide/importing-exporting/index.rst +++ b/Help/guide/importing-exporting/index.rst @@ -1,10 +1,13 @@ -Importing and Exporting Targets -******************************* +Importing and Exporting Guide +***************************** .. only:: html .. contents:: +Introduction +============ + In this guide, we will present the concept of :prop_tgt:`IMPORTED` targets and demonstrate how to import existing executable or library files from disk into a CMake project. We will then show how CMake supports exporting targets diff --git a/Help/guide/tutorial/index.rst b/Help/guide/tutorial/index.rst index 665d123..4b09e53 100644 --- a/Help/guide/tutorial/index.rst +++ b/Help/guide/tutorial/index.rst @@ -5,6 +5,9 @@ CMake Tutorial .. contents:: +Introduction +============ + The CMake tutorial provides a step-by-step guide that covers common build system issues that CMake helps address. Seeing how various topics all work together in an example project can be very helpful. The tutorial diff --git a/Help/manual/VS-Choose-Arch.png b/Help/guide/user-interaction/VS-Choose-Arch.png Binary files differindex 816b0f4..816b0f4 100644 --- a/Help/manual/VS-Choose-Arch.png +++ b/Help/guide/user-interaction/VS-Choose-Arch.png diff --git a/Help/guide/user-interaction/index.rst b/Help/guide/user-interaction/index.rst index c724b6f..2d8ed29 100644 --- a/Help/guide/user-interaction/index.rst +++ b/Help/guide/user-interaction/index.rst @@ -85,7 +85,7 @@ The source and binary directories must first be populated. It is always advised to use different directories for the source and the build. -.. image:: /guide/user-interaction/GUI-Source-Binary.png +.. image:: GUI-Source-Binary.png :alt: Choosing source and binary directories Generating a Buildsystem @@ -246,19 +246,19 @@ Choosing a generator in cmake-gui The "Configure" button triggers a new dialog to select the CMake generator to use. -.. image:: /guide/user-interaction/GUI-Configure-Dialog.png +.. image:: GUI-Configure-Dialog.png :alt: Configuring a generator All generators available on the command line are also available in :manual:`cmake-gui(1)`. -.. image:: /guide/user-interaction/GUI-Choose-Generator.png +.. image:: GUI-Choose-Generator.png :alt: Choosing a generator When choosing a Visual Studio generator, further options are available to set an architecture to generate for. -.. image:: /manual/VS-Choose-Arch.png +.. image:: VS-Choose-Arch.png :alt: Choosing an architecture for Visual Studio generators .. _`Setting Build Variables`: @@ -359,7 +359,7 @@ Variables may be set in the cmake-gui using the "Add Entry" button. This triggers a new dialog to set the value of the variable. -.. image:: /guide/user-interaction/GUI-Add-Entry.png +.. image:: GUI-Add-Entry.png :alt: Editing a cache entry The main view of the :manual:`cmake-gui(1)` user interface diff --git a/Help/guide/using-dependencies/index.rst b/Help/guide/using-dependencies/index.rst index 6fdcc55..f4d7845 100644 --- a/Help/guide/using-dependencies/index.rst +++ b/Help/guide/using-dependencies/index.rst @@ -31,7 +31,7 @@ See the :guide:`User Interaction Guide` for more about setting cache entries. Libraries providing Config-file packages ----------------------------------------- +======================================== The most convenient way for a third-party to provide library binaries for use with CMake is to provide @@ -115,7 +115,7 @@ file, such as ``/opt/somepackage/lib/cmake/SomePackage/`` in the above example. Imported Targets from Packages ------------------------------- +============================== A third-party package which provides config-file packages may also provide :ref:`Imported targets`. These will be diff --git a/Help/manual/cmake-modules.7.rst b/Help/manual/cmake-modules.7.rst index d364198..14af149 100644 --- a/Help/manual/cmake-modules.7.rst +++ b/Help/manual/cmake-modules.7.rst @@ -45,6 +45,7 @@ These modules are loaded using the :command:`include` command. /module/CheckOBJCXXSourceRuns /module/CheckPIESupported /module/CheckPrototypeDefinition + /module/CheckCompilerFlag /module/CheckSourceCompiles /module/CheckSourceRuns /module/CheckStructHasMember diff --git a/Help/module/CheckCompilerFlag.rst b/Help/module/CheckCompilerFlag.rst new file mode 100644 index 0000000..bcf19a8 --- /dev/null +++ b/Help/module/CheckCompilerFlag.rst @@ -0,0 +1 @@ +.. cmake-module:: ../../Modules/CheckCompilerFlag.cmake diff --git a/Help/release/dev/check-source-modules.rst b/Help/release/dev/check-source-modules.rst index 9a7785a..e6cccbe 100644 --- a/Help/release/dev/check-source-modules.rst +++ b/Help/release/dev/check-source-modules.rst @@ -8,3 +8,7 @@ check-source-modules * The :module:`CheckSourceRuns` module has been added to generalize :module:`CheckCSourceRuns` and :module:`CheckCXXSourceRuns` to more languages. + +* The :module:`CheckCompilerFlag` module has been added to + generalize :module:`CheckCCompilerFlag` and + :module:`CheckCXXCompilerFlag` to more languages. diff --git a/Modules/CheckCCompilerFlag.cmake b/Modules/CheckCCompilerFlag.cmake index 1452b51..f835f29 100644 --- a/Modules/CheckCCompilerFlag.cmake +++ b/Modules/CheckCCompilerFlag.cmake @@ -34,24 +34,8 @@ effect or even a specific one is beyond the scope of this module. include_guard(GLOBAL) include(CheckCSourceCompiles) -include(CMakeCheckCompilerFlagCommonPatterns) - -function(check_c_compiler_flag _flag _var) - set(CMAKE_REQUIRED_DEFINITIONS "${_flag}") - - # Normalize locale during test compilation. - set(_locale_vars LC_ALL LC_MESSAGES LANG) - foreach(v IN LISTS _locale_vars) - set(_locale_vars_saved_${v} "$ENV{${v}}") - set(ENV{${v}} C) - endforeach() - check_compiler_flag_common_patterns(_common_patterns) - check_c_source_compiles("int main(void) { return 0; }" ${_var} - # Some compilers do not fail with a bad flag - FAIL_REGEX "command line option .* is valid for .* but not for C" # GNU - ${_common_patterns} - ) - foreach(v IN LISTS _locale_vars) - set(ENV{${v}} ${_locale_vars_saved_${v}}) - endforeach() -endfunction() +include(CheckCompilerFlag) + +macro (CHECK_C_COMPILER_FLAG _FLAG _RESULT) + check_compiler_flag(C "${_FLAG}" ${_RESULT}) +endmacro () diff --git a/Modules/CheckCXXCompilerFlag.cmake b/Modules/CheckCXXCompilerFlag.cmake index 544e9ac..ce49ae3 100644 --- a/Modules/CheckCXXCompilerFlag.cmake +++ b/Modules/CheckCXXCompilerFlag.cmake @@ -34,24 +34,8 @@ effect or even a specific one is beyond the scope of this module. include_guard(GLOBAL) include(CheckCXXSourceCompiles) -include(CMakeCheckCompilerFlagCommonPatterns) - -function(check_cxx_compiler_flag _flag _var) - set(CMAKE_REQUIRED_DEFINITIONS "${_flag}") - - # Normalize locale during test compilation. - set(_locale_vars LC_ALL LC_MESSAGES LANG) - foreach(v IN LISTS _locale_vars) - set(_locale_vars_saved_${v} "$ENV{${v}}") - set(ENV{${v}} C) - endforeach() - check_compiler_flag_common_patterns(_common_patterns) - check_cxx_source_compiles("int main() { return 0; }" ${_var} - # Some compilers do not fail with a bad flag - FAIL_REGEX "command line option .* is valid for .* but not for C\\\\+\\\\+" # GNU - ${_common_patterns} - ) - foreach(v IN LISTS _locale_vars) - set(ENV{${v}} ${_locale_vars_saved_${v}}) - endforeach() -endfunction() +include(CheckCompilerFlag) + +macro (CHECK_CXX_COMPILER_FLAG _FLAG _RESULT) + check_compiler_flag(CXX "${_FLAG}" ${_RESULT}) +endmacro () diff --git a/Modules/CheckCompilerFlag.cmake b/Modules/CheckCompilerFlag.cmake new file mode 100644 index 0000000..9223009 --- /dev/null +++ b/Modules/CheckCompilerFlag.cmake @@ -0,0 +1,105 @@ +# Distributed under the OSI-approved BSD 3-Clause License. See accompanying +# file Copyright.txt or https://cmake.org/licensing for details. + +#[=======================================================================[.rst: +CheckCompilerFlag +--------------------- + +.. versionadded:: 3.19 + +Check whether the compiler supports a given flag. + +.. command:: check_compiler_flag + + .. code-block:: cmake + + check_compiler_flag(<lang> <flag> <var>) + +Check that the ``<flag>`` is accepted by the compiler without a diagnostic. +Stores the result in an internal cache entry named ``<var>``. + +This command temporarily sets the ``CMAKE_REQUIRED_DEFINITIONS`` variable +and calls the ``check_source_compiles(<LANG>`` function from the +:module:`CheckSourceCompiles` module. See documentation of that +module for a listing of variables that can otherwise modify the build. + +A positive result from this check indicates only that the compiler did not +issue a diagnostic message when given the flag. Whether the flag has any +effect or even a specific one is beyond the scope of this module. + +.. note:: + Since the :command:`try_compile` command forwards flags from variables + like :variable:`CMAKE_<LANG>_FLAGS <CMAKE_<LANG>_FLAGS>`, unknown flags + in such variables may cause a false negative for this check. +#]=======================================================================] + +include_guard(GLOBAL) +include(CheckSourceCompiles) +include(CMakeCheckCompilerFlagCommonPatterns) + +cmake_policy(PUSH) +cmake_policy(SET CMP0054 NEW) # if() quoted variables not dereferenced +cmake_policy(SET CMP0057 NEW) # if() supports IN_LIST + +function(CHECK_COMPILER_FLAG _lang _flag _var) + + if(_lang STREQUAL C) + set(_lang_src "int main(void) { return 0; }") + set(_lang_fail_regex FAIL_REGEX "command line option .* is valid for .* but not for C") + elseif(_lang STREQUAL CXX) + set(_lang_src "int main() { return 0; }") + set(_lang_fail_regex FAIL_REGEX "command line option .* is valid for .* but not for C\\+\\+") + elseif(_lang STREQUAL Fortran) + set(_lang_src " program test\n stop\n end program") + set(_lang_fail_regex FAIL_REGEX "command line option .* is valid for .* but not for Fortran") + elseif(_lang STREQUAL OBJC) + set(_lang_src [=[ +#ifndef __OBJC__ +# error "Not an Objective-C compiler" +#endif +int main(void) { return 0; }]=]) + set(_lang_fail_regex FAIL_REGEX "command line option .* is valid for .* but not for Objective-C" # GNU + FAIL_REGEX "argument unused during compilation: .*") # Clang + elseif(_lang STREQUAL OBJCXX) + set(_lang_src [=[ +#ifndef __OBJC__ +# error "Not an Objective-C++ compiler" +#endif +int main(void) { return 0; }]=]) + set(_lang_fail_regex FAIL_REGEX "command line option .* is valid for .* but not for Objective-C\\+\\+" # GNU + FAIL_REGEX "argument unused during compilation: .*") # Clang + else() + message (SEND_ERROR "check_compiler_flag: ${_lang}: unknown language.") + return() + endif() + + get_property (_supported_languages GLOBAL PROPERTY ENABLED_LANGUAGES) + if (NOT _lang IN_LIST _supported_languages) + message (SEND_ERROR "check_compiler_flag: ${_lang}: needs to be enabled before use.") + return() + endif() + + set(CMAKE_REQUIRED_DEFINITIONS ${_flag}) + + # Normalize locale during test compilation. + set(_locale_vars LC_ALL LC_MESSAGES LANG) + foreach(v IN LISTS _locale_vars) + set(_locale_vars_saved_${v} "$ENV{${v}}") + set(ENV{${v}} C) + endforeach() + + check_compiler_flag_common_patterns(_common_patterns) + check_source_compiles(${_lang} + "${_lang_src}" + ${_var} + ${_lang_fail_regex} + ${_common_patterns} + ) + + foreach(v IN LISTS _locale_vars) + set(ENV{${v}} ${_locale_vars_saved_${v}}) + endforeach() + set(${_var} "${${_var}}" PARENT_SCOPE) +endfunction () + +cmake_policy(POP) diff --git a/Modules/CheckFortranCompilerFlag.cmake b/Modules/CheckFortranCompilerFlag.cmake index b8fac97..0f5cf9a 100644 --- a/Modules/CheckFortranCompilerFlag.cmake +++ b/Modules/CheckFortranCompilerFlag.cmake @@ -36,30 +36,8 @@ effect or even a specific one is beyond the scope of this module. include_guard(GLOBAL) include(CheckFortranSourceCompiles) -include(CMakeCheckCompilerFlagCommonPatterns) +include(CheckCompilerFlag) -macro (CHECK_Fortran_COMPILER_FLAG _FLAG _RESULT) - set(SAFE_CMAKE_REQUIRED_DEFINITIONS "${CMAKE_REQUIRED_DEFINITIONS}") - set(CMAKE_REQUIRED_DEFINITIONS "${_FLAG}") - - # Normalize locale during test compilation. - set(_CheckFortranCompilerFlag_LOCALE_VARS LC_ALL LC_MESSAGES LANG) - foreach(v ${_CheckFortranCompilerFlag_LOCALE_VARS}) - set(_CheckFortranCompilerFlag_SAVED_${v} "$ENV{${v}}") - set(ENV{${v}} C) - endforeach() - CHECK_COMPILER_FLAG_COMMON_PATTERNS(_CheckFortranCompilerFlag_COMMON_PATTERNS) - CHECK_Fortran_SOURCE_COMPILES(" program test\n stop\n end program" ${_RESULT} - # Some compilers do not fail with a bad flag - FAIL_REGEX "command line option .* is valid for .* but not for Fortran" # GNU - ${_CheckFortranCompilerFlag_COMMON_PATTERNS} - ) - foreach(v ${_CheckFortranCompilerFlag_LOCALE_VARS}) - set(ENV{${v}} ${_CheckFortranCompilerFlag_SAVED_${v}}) - unset(_CheckFortranCompilerFlag_SAVED_${v}) - endforeach() - unset(_CheckFortranCompilerFlag_LOCALE_VARS) - unset(_CheckFortranCompilerFlag_COMMON_PATTERNS) - - set (CMAKE_REQUIRED_DEFINITIONS "${SAFE_CMAKE_REQUIRED_DEFINITIONS}") +macro (CHECK_FORTRAN_COMPILER_FLAG _FLAG _RESULT) + check_compiler_flag(Fortran "${_FLAG}" ${_RESULT}) endmacro () diff --git a/Modules/CheckOBJCCompilerFlag.cmake b/Modules/CheckOBJCCompilerFlag.cmake index a98f429..df9d724 100644 --- a/Modules/CheckOBJCCompilerFlag.cmake +++ b/Modules/CheckOBJCCompilerFlag.cmake @@ -36,31 +36,8 @@ effect or even a specific one is beyond the scope of this module. include_guard(GLOBAL) include(CheckOBJCSourceCompiles) -include(CMakeCheckCompilerFlagCommonPatterns) +include(CheckCompilerFlag) macro (CHECK_OBJC_COMPILER_FLAG _FLAG _RESULT) - set(SAFE_CMAKE_REQUIRED_DEFINITIONS "${CMAKE_REQUIRED_DEFINITIONS}") - set(CMAKE_REQUIRED_DEFINITIONS "${_FLAG}") - - # Normalize locale during test compilation. - set(_CheckOBJCCompilerFlag_LOCALE_VARS LC_ALL LC_MESSAGES LANG) - foreach(v ${_CheckOBJCCompilerFlag_LOCALE_VARS}) - set(_CheckOBJCCompilerFlag_SAVED_${v} "$ENV{${v}}") - set(ENV{${v}} OBJC) - endforeach() - CHECK_COMPILER_FLAG_COMMON_PATTERNS(_CheckOBJCCompilerFlag_COMMON_PATTERNS) - CHECK_OBJC_SOURCE_COMPILES("#ifndef __OBJC__\n# error \"Not an Objective-C compiler\"\n#endif\nint main(void) { return 0; }" ${_RESULT} - # Some compilers do not fail with a bad flag - FAIL_REGEX "command line option .* is valid for .* but not for Objective-C" # GNU - FAIL_REGEX "argument unused during compilation: .*" # Clang - ${_CheckOBJCCompilerFlag_COMMON_PATTERNS} - ) - foreach(v ${_CheckOBJCCompilerFlag_LOCALE_VARS}) - set(ENV{${v}} ${_CheckOBJCCompilerFlag_SAVED_${v}}) - unset(_CheckOBJCCompilerFlag_SAVED_${v}) - endforeach() - unset(_CheckOBJCCompilerFlag_LOCALE_VARS) - unset(_CheckOBJCCompilerFlag_COMMON_PATTERNS) - - set (CMAKE_REQUIRED_DEFINITIONS "${SAFE_CMAKE_REQUIRED_DEFINITIONS}") + check_compiler_flag(OBJC "${_FLAG}" ${_RESULT}) endmacro () diff --git a/Modules/CheckOBJCXXCompilerFlag.cmake b/Modules/CheckOBJCXXCompilerFlag.cmake index 7944ab0..6e01bcc 100644 --- a/Modules/CheckOBJCXXCompilerFlag.cmake +++ b/Modules/CheckOBJCXXCompilerFlag.cmake @@ -36,31 +36,8 @@ effect or even a specific one is beyond the scope of this module. include_guard(GLOBAL) include(CheckOBJCXXSourceCompiles) -include(CMakeCheckCompilerFlagCommonPatterns) +include(CheckCompilerFlag) macro (CHECK_OBJCXX_COMPILER_FLAG _FLAG _RESULT) - set(SAFE_CMAKE_REQUIRED_DEFINITIONS "${CMAKE_REQUIRED_DEFINITIONS}") - set(CMAKE_REQUIRED_DEFINITIONS "${_FLAG}") - - # Normalize locale during test compilation. - set(_CheckOBJCXXCompilerFlag_LOCALE_VARS LC_ALL LC_MESSAGES LANG) - foreach(v ${_CheckOBJCXXCompilerFlag_LOCALE_VARS}) - set(_CheckOBJCXXCompilerFlag_SAVED_${v} "$ENV{${v}}") - set(ENV{${v}} OBJCXX) - endforeach() - CHECK_COMPILER_FLAG_COMMON_PATTERNS(_CheckOBJCXXCompilerFlag_COMMON_PATTERNS) - CHECK_OBJCXX_SOURCE_COMPILES("#ifndef __OBJC__\n# error \"Not an Objective-C++ compiler\"\n#endif\nint main(void) { return 0; }" ${_RESULT} - # Some compilers do not fail with a bad flag - FAIL_REGEX "command line option .* is valid for .* but not for Objective-C\\\\+\\\\+" # GNU - FAIL_REGEX "argument unused during compilation: .*" # Clang - ${_CheckOBJCXXCompilerFlag_COMMON_PATTERNS} - ) - foreach(v ${_CheckOBJCXXCompilerFlag_LOCALE_VARS}) - set(ENV{${v}} ${_CheckOBJCXXCompilerFlag_SAVED_${v}}) - unset(_CheckOBJCXXCompilerFlag_SAVED_${v}) - endforeach() - unset(_CheckOBJCXXCompilerFlag_LOCALE_VARS) - unset(_CheckOBJCXXCompilerFlag_COMMON_PATTERNS) - - set (CMAKE_REQUIRED_DEFINITIONS "${SAFE_CMAKE_REQUIRED_DEFINITIONS}") + check_compiler_flag(OBJCXX "${_FLAG}" ${_RESULT}) endmacro () diff --git a/Source/CMakeLists.txt b/Source/CMakeLists.txt index 1be424a..d0d9459 100644 --- a/Source/CMakeLists.txt +++ b/Source/CMakeLists.txt @@ -946,6 +946,7 @@ set(CTEST_SRCS cmCTest.cxx CTest/cmCTestResourceAllocator.cxx CTest/cmCTestResourceSpec.cxx CTest/cmCTestLaunch.cxx + CTest/cmCTestLaunchReporter.cxx CTest/cmCTestMemCheckCommand.cxx CTest/cmCTestMemCheckHandler.cxx CTest/cmCTestMultiProcessHandler.cxx diff --git a/Source/CMakeVersion.cmake b/Source/CMakeVersion.cmake index 334d946..818b1bd 100644 --- a/Source/CMakeVersion.cmake +++ b/Source/CMakeVersion.cmake @@ -1,7 +1,7 @@ # CMake version number components. set(CMake_VERSION_MAJOR 3) set(CMake_VERSION_MINOR 18) -set(CMake_VERSION_PATCH 20200928) +set(CMake_VERSION_PATCH 20200929) #set(CMake_VERSION_RC 0) set(CMake_VERSION_IS_DIRTY 0) diff --git a/Source/CTest/cmCTestBuildHandler.cxx b/Source/CTest/cmCTestBuildHandler.cxx index 9997548..bb700eba 100644 --- a/Source/CTest/cmCTestBuildHandler.cxx +++ b/Source/CTest/cmCTestBuildHandler.cxx @@ -14,6 +14,7 @@ #include "cmsys/Process.h" #include "cmCTest.h" +#include "cmCTestLaunchReporter.h" #include "cmDuration.h" #include "cmFileTimeCache.h" #include "cmGeneratedFileStream.h" @@ -887,15 +888,28 @@ int cmCTestBuildHandler::RunMakeCommand(const std::string& command, if (*retVal) { // If there was an error running command, report that on the // dashboard. - cmCTestBuildErrorWarning errorwarning; - errorwarning.LogLine = 1; - errorwarning.Text = cmStrCat( - "*** WARNING non-zero return value in ctest from: ", argv[0]); - errorwarning.PreContext.clear(); - errorwarning.PostContext.clear(); - errorwarning.Error = false; - this->ErrorsAndWarnings.push_back(std::move(errorwarning)); - this->TotalWarnings++; + if (this->UseCTestLaunch) { + cmCTestLaunchReporter reporter; + reporter.RealArgs = args; + reporter.ComputeFileNames(); + reporter.ExitCode = *retVal; + reporter.Process = cp; + // Use temporary BuildLog file to populate this error for CDash. + ofs.flush(); + reporter.LogOut = this->LogFileNames["Build"]; + reporter.LogOut += ".tmp"; + reporter.WriteXML(); + } else { + cmCTestBuildErrorWarning errorwarning; + errorwarning.LogLine = 1; + errorwarning.Text = cmStrCat( + "*** WARNING non-zero return value in ctest from: ", argv[0]); + errorwarning.PreContext.clear(); + errorwarning.PostContext.clear(); + errorwarning.Error = false; + this->ErrorsAndWarnings.push_back(std::move(errorwarning)); + this->TotalWarnings++; + } } } } else if (result == cmsysProcess_State_Exception) { diff --git a/Source/CTest/cmCTestGenericHandler.cxx b/Source/CTest/cmCTestGenericHandler.cxx index a71f550..91818bb 100644 --- a/Source/CTest/cmCTestGenericHandler.cxx +++ b/Source/CTest/cmCTestGenericHandler.cxx @@ -6,6 +6,7 @@ #include <utility> #include "cmCTest.h" +#include "cmStringAlgorithms.h" #include "cmSystemTools.h" cmCTestGenericHandler::cmCTestGenericHandler() @@ -122,6 +123,8 @@ bool cmCTestGenericHandler::StartLogFile(const char* name, ostr << "_" << this->CTest->GetCurrentTag(); } ostr << ".log"; + this->LogFileNames[name] = + cmStrCat(this->CTest->GetBinaryDir(), "/Testing/Temporary/", ostr.str()); if (!this->CTest->OpenOutputFile("Temporary", ostr.str(), xofs)) { cmCTestLog(this->CTest, ERROR_MESSAGE, "Cannot create log file: " << ostr.str() << std::endl); diff --git a/Source/CTest/cmCTestGenericHandler.h b/Source/CTest/cmCTestGenericHandler.h index 591d9cd..89d7596 100644 --- a/Source/CTest/cmCTestGenericHandler.h +++ b/Source/CTest/cmCTestGenericHandler.h @@ -100,6 +100,7 @@ protected: cmCTest* CTest; t_StringToString Options; t_StringToString PersistentOptions; + t_StringToString LogFileNames; cmCTestCommand* Command; int SubmitIndex; diff --git a/Source/CTest/cmCTestLaunch.cxx b/Source/CTest/cmCTestLaunch.cxx index 647f5ff..b9ed033 100644 --- a/Source/CTest/cmCTestLaunch.cxx +++ b/Source/CTest/cmCTestLaunch.cxx @@ -2,7 +2,6 @@ file Copyright.txt or https://cmake.org/licensing for details. */ #include "cmCTestLaunch.h" -#include <cstdlib> #include <cstring> #include <iostream> @@ -10,8 +9,7 @@ #include "cmsys/Process.h" #include "cmsys/RegularExpression.hxx" -#include "cmCryptoHash.h" -#include "cmGeneratedFileStream.h" +#include "cmCTestLaunchReporter.h" #include "cmGlobalGenerator.h" #include "cmMakefile.h" #include "cmProcessOutput.h" @@ -19,7 +17,6 @@ #include "cmStateSnapshot.h" #include "cmStringAlgorithms.h" #include "cmSystemTools.h" -#include "cmXMLWriter.h" #include "cmake.h" #ifdef _WIN32 @@ -30,16 +27,14 @@ cmCTestLaunch::cmCTestLaunch(int argc, const char* const* argv) { - this->Passthru = true; this->Process = nullptr; - this->ExitCode = 1; - this->CWD = cmSystemTools::GetCurrentWorkingDirectory(); if (!this->ParseArguments(argc, argv)) { return; } - this->ComputeFileNames(); + this->Reporter.RealArgs = this->RealArgs; + this->Reporter.ComputeFileNames(); this->ScrapeRulesLoaded = false; this->HaveOut = false; @@ -50,10 +45,6 @@ cmCTestLaunch::cmCTestLaunch(int argc, const char* const* argv) cmCTestLaunch::~cmCTestLaunch() { cmsysProcess_Delete(this->Process); - if (!this->Passthru) { - cmSystemTools::RemoveFile(this->LogOut); - cmSystemTools::RemoveFile(this->LogErr); - } } bool cmCTestLaunch::ParseArguments(int argc, const char* const* argv) @@ -93,28 +84,28 @@ bool cmCTestLaunch::ParseArguments(int argc, const char* const* argv) } else if (strcmp(arg, "--filter-prefix") == 0) { doing = DoingFilterPrefix; } else if (doing == DoingOutput) { - this->OptionOutput = arg; + this->Reporter.OptionOutput = arg; doing = DoingNone; } else if (doing == DoingSource) { - this->OptionSource = arg; + this->Reporter.OptionSource = arg; doing = DoingNone; } else if (doing == DoingLanguage) { - this->OptionLanguage = arg; - if (this->OptionLanguage == "CXX") { - this->OptionLanguage = "C++"; + this->Reporter.OptionLanguage = arg; + if (this->Reporter.OptionLanguage == "CXX") { + this->Reporter.OptionLanguage = "C++"; } doing = DoingNone; } else if (doing == DoingTargetName) { - this->OptionTargetName = arg; + this->Reporter.OptionTargetName = arg; doing = DoingNone; } else if (doing == DoingTargetType) { - this->OptionTargetType = arg; + this->Reporter.OptionTargetType = arg; doing = DoingNone; } else if (doing == DoingBuildDir) { - this->OptionBuildDir = arg; + this->Reporter.OptionBuildDir = arg; doing = DoingNone; } else if (doing == DoingFilterPrefix) { - this->OptionFilterPrefix = arg; + this->Reporter.OptionFilterPrefix = arg; doing = DoingNone; } } @@ -150,42 +141,11 @@ void cmCTestLaunch::HandleRealArg(const char* arg) this->RealArgs.emplace_back(arg); } -void cmCTestLaunch::ComputeFileNames() -{ - // We just passthru the behavior of the real command unless the - // CTEST_LAUNCH_LOGS environment variable is set. - const char* d = getenv("CTEST_LAUNCH_LOGS"); - if (!(d && *d)) { - return; - } - this->Passthru = false; - - // The environment variable specifies the directory into which we - // generate build logs. - this->LogDir = d; - cmSystemTools::ConvertToUnixSlashes(this->LogDir); - this->LogDir += "/"; - - // We hash the input command working dir and command line to obtain - // a repeatable and (probably) unique name for log files. - cmCryptoHash md5(cmCryptoHash::AlgoMD5); - md5.Initialize(); - md5.Append(this->CWD); - for (std::string const& realArg : this->RealArgs) { - md5.Append(realArg); - } - this->LogHash = md5.FinalizeHex(); - - // We store stdout and stderr in temporary log files. - this->LogOut = cmStrCat(this->LogDir, "launch-", this->LogHash, "-out.txt"); - this->LogErr = cmStrCat(this->LogDir, "launch-", this->LogHash, "-err.txt"); -} - void cmCTestLaunch::RunChild() { // Ignore noopt make rules if (this->RealArgs.empty() || this->RealArgs[0] == ":") { - this->ExitCode = 0; + this->Reporter.ExitCode = 0; return; } @@ -195,14 +155,14 @@ void cmCTestLaunch::RunChild() cmsys::ofstream fout; cmsys::ofstream ferr; - if (this->Passthru) { + if (this->Reporter.Passthru) { // In passthru mode we just share the output pipes. cmsysProcess_SetPipeShared(cp, cmsysProcess_Pipe_STDOUT, 1); cmsysProcess_SetPipeShared(cp, cmsysProcess_Pipe_STDERR, 1); } else { // In full mode we record the child output pipes to log files. - fout.open(this->LogOut.c_str(), std::ios::out | std::ios::binary); - ferr.open(this->LogErr.c_str(), std::ios::out | std::ios::binary); + fout.open(this->Reporter.LogOut.c_str(), std::ios::out | std::ios::binary); + ferr.open(this->Reporter.LogErr.c_str(), std::ios::out | std::ios::binary); } #ifdef _WIN32 @@ -216,7 +176,7 @@ void cmCTestLaunch::RunChild() cmsysProcess_Execute(cp); // Record child stdout and stderr if necessary. - if (!this->Passthru) { + if (!this->Reporter.Passthru) { char* data = nullptr; int length = 0; cmProcessOutput processOutput; @@ -248,7 +208,7 @@ void cmCTestLaunch::RunChild() // Wait for the real command to finish. cmsysProcess_WaitForExit(cp, nullptr); - this->ExitCode = cmsysProcess_GetExitValue(cp); + this->Reporter.ExitCode = cmsysProcess_GetExitValue(cp); } int cmCTestLaunch::Run() @@ -261,255 +221,31 @@ int cmCTestLaunch::Run() this->RunChild(); if (this->CheckResults()) { - return this->ExitCode; + return this->Reporter.ExitCode; } this->LoadConfig(); - this->WriteXML(); - - return this->ExitCode; -} - -void cmCTestLaunch::LoadLabels() -{ - if (this->OptionBuildDir.empty() || this->OptionTargetName.empty()) { - return; - } - - // Labels are listed in per-target files. - std::string fname = cmStrCat(this->OptionBuildDir, "/CMakeFiles/", - this->OptionTargetName, ".dir/Labels.txt"); - - // We are interested in per-target labels for this source file. - std::string source = this->OptionSource; - cmSystemTools::ConvertToUnixSlashes(source); - - // Load the labels file. - cmsys::ifstream fin(fname.c_str(), std::ios::in | std::ios::binary); - if (!fin) { - return; - } - bool inTarget = true; - bool inSource = false; - std::string line; - while (cmSystemTools::GetLineFromStream(fin, line)) { - if (line.empty() || line[0] == '#') { - // Ignore blank and comment lines. - continue; - } - if (line[0] == ' ') { - // Label lines appear indented by one space. - if (inTarget || inSource) { - this->Labels.insert(line.substr(1)); - } - } else if (!this->OptionSource.empty() && !inSource) { - // Non-indented lines specify a source file name. The first one - // is the end of the target-wide labels. Use labels following a - // matching source. - inTarget = false; - inSource = this->SourceMatches(line, source); - } else { - return; - } - } -} - -bool cmCTestLaunch::SourceMatches(std::string const& lhs, - std::string const& rhs) -{ - // TODO: Case sensitivity, UseRelativePaths, etc. Note that both - // paths in the comparison get generated by CMake. This is done for - // every source in the target, so it should be efficient (cannot use - // cmSystemTools::IsSameFile). - return lhs == rhs; -} - -bool cmCTestLaunch::IsError() const -{ - return this->ExitCode != 0; -} - -void cmCTestLaunch::WriteXML() -{ - // Name the xml file. - std::string logXML = - cmStrCat(this->LogDir, this->IsError() ? "error-" : "warning-", - this->LogHash, ".xml"); - - // Use cmGeneratedFileStream to atomically create the report file. - cmGeneratedFileStream fxml(logXML); - cmXMLWriter xml(fxml, 2); - cmXMLElement e2(xml, "Failure"); - e2.Attribute("type", this->IsError() ? "Error" : "Warning"); - this->WriteXMLAction(e2); - this->WriteXMLCommand(e2); - this->WriteXMLResult(e2); - this->WriteXMLLabels(e2); -} - -void cmCTestLaunch::WriteXMLAction(cmXMLElement& e2) -{ - e2.Comment("Meta-information about the build action"); - cmXMLElement e3(e2, "Action"); - - // TargetName - if (!this->OptionTargetName.empty()) { - e3.Element("TargetName", this->OptionTargetName); - } - - // Language - if (!this->OptionLanguage.empty()) { - e3.Element("Language", this->OptionLanguage); - } - - // SourceFile - if (!this->OptionSource.empty()) { - std::string source = this->OptionSource; - cmSystemTools::ConvertToUnixSlashes(source); - - // If file is in source tree use its relative location. - if (cmSystemTools::FileIsFullPath(this->SourceDir) && - cmSystemTools::FileIsFullPath(source) && - cmSystemTools::IsSubDirectory(source, this->SourceDir)) { - source = cmSystemTools::RelativePath(this->SourceDir, source); - } - - e3.Element("SourceFile", source); - } - - // OutputFile - if (!this->OptionOutput.empty()) { - e3.Element("OutputFile", this->OptionOutput); - } - - // OutputType - const char* outputType = nullptr; - if (!this->OptionTargetType.empty()) { - if (this->OptionTargetType == "EXECUTABLE") { - outputType = "executable"; - } else if (this->OptionTargetType == "SHARED_LIBRARY") { - outputType = "shared library"; - } else if (this->OptionTargetType == "MODULE_LIBRARY") { - outputType = "module library"; - } else if (this->OptionTargetType == "STATIC_LIBRARY") { - outputType = "static library"; - } - } else if (!this->OptionSource.empty()) { - outputType = "object file"; - } - if (outputType) { - e3.Element("OutputType", outputType); - } -} - -void cmCTestLaunch::WriteXMLCommand(cmXMLElement& e2) -{ - e2.Comment("Details of command"); - cmXMLElement e3(e2, "Command"); - if (!this->CWD.empty()) { - e3.Element("WorkingDirectory", this->CWD); - } - for (std::string const& realArg : this->RealArgs) { - e3.Element("Argument", realArg); - } -} - -void cmCTestLaunch::WriteXMLResult(cmXMLElement& e2) -{ - e2.Comment("Result of command"); - cmXMLElement e3(e2, "Result"); - - // StdOut - this->DumpFileToXML(e3, "StdOut", this->LogOut); - - // StdErr - this->DumpFileToXML(e3, "StdErr", this->LogErr); - - // ExitCondition - cmXMLElement e4(e3, "ExitCondition"); - cmsysProcess* cp = this->Process; - switch (cmsysProcess_GetState(cp)) { - case cmsysProcess_State_Starting: - e4.Content("No process has been executed"); - break; - case cmsysProcess_State_Executing: - e4.Content("The process is still executing"); - break; - case cmsysProcess_State_Disowned: - e4.Content("Disowned"); - break; - case cmsysProcess_State_Killed: - e4.Content("Killed by parent"); - break; - - case cmsysProcess_State_Expired: - e4.Content("Killed when timeout expired"); - break; - case cmsysProcess_State_Exited: - e4.Content(this->ExitCode); - break; - case cmsysProcess_State_Exception: - e4.Content("Terminated abnormally: "); - e4.Content(cmsysProcess_GetExceptionString(cp)); - break; - case cmsysProcess_State_Error: - e4.Content("Error administrating child process: "); - e4.Content(cmsysProcess_GetErrorString(cp)); - break; - } -} + this->Reporter.Process = this->Process; + this->Reporter.WriteXML(); -void cmCTestLaunch::WriteXMLLabels(cmXMLElement& e2) -{ - this->LoadLabels(); - if (!this->Labels.empty()) { - e2.Comment("Interested parties"); - cmXMLElement e3(e2, "Labels"); - for (std::string const& label : this->Labels) { - e3.Element("Label", label); - } - } -} - -void cmCTestLaunch::DumpFileToXML(cmXMLElement& e3, const char* tag, - std::string const& fname) -{ - cmsys::ifstream fin(fname.c_str(), std::ios::in | std::ios::binary); - - std::string line; - const char* sep = ""; - - cmXMLElement e4(e3, tag); - while (cmSystemTools::GetLineFromStream(fin, line)) { - if (MatchesFilterPrefix(line)) { - continue; - } - if (this->Match(line, this->RegexWarningSuppress)) { - line = cmStrCat("[CTest: warning suppressed] ", line); - } else if (this->Match(line, this->RegexWarning)) { - line = cmStrCat("[CTest: warning matched] ", line); - } - e4.Content(sep); - e4.Content(line); - sep = "\n"; - } + return this->Reporter.ExitCode; } bool cmCTestLaunch::CheckResults() { // Skip XML in passthru mode. - if (this->Passthru) { + if (this->Reporter.Passthru) { return true; } // We always report failure for error conditions. - if (this->IsError()) { + if (this->Reporter.IsError()) { return false; } // Scrape the output logs to look for warnings. - if ((this->HaveErr && this->ScrapeLog(this->LogErr)) || - (this->HaveOut && this->ScrapeLog(this->LogOut))) { + if ((this->HaveErr && this->ScrapeLog(this->Reporter.LogErr)) || + (this->HaveOut && this->ScrapeLog(this->Reporter.LogOut))) { return false; } return true; @@ -522,22 +258,17 @@ void cmCTestLaunch::LoadScrapeRules() } this->ScrapeRulesLoaded = true; - // Common compiler warning formats. These are much simpler than the - // full log-scraping expressions because we do not need to extract - // file and line information. - this->RegexWarning.emplace_back("(^|[ :])[Ww][Aa][Rr][Nn][Ii][Nn][Gg]"); - this->RegexWarning.emplace_back("(^|[ :])[Rr][Ee][Mm][Aa][Rr][Kk]"); - this->RegexWarning.emplace_back("(^|[ :])[Nn][Oo][Tt][Ee]"); - // Load custom match rules given to us by CTest. - this->LoadScrapeRules("Warning", this->RegexWarning); - this->LoadScrapeRules("WarningSuppress", this->RegexWarningSuppress); + this->LoadScrapeRules("Warning", this->Reporter.RegexWarning); + this->LoadScrapeRules("WarningSuppress", + this->Reporter.RegexWarningSuppress); } void cmCTestLaunch::LoadScrapeRules( const char* purpose, std::vector<cmsys::RegularExpression>& regexps) { - std::string fname = cmStrCat(this->LogDir, "Custom", purpose, ".txt"); + std::string fname = + cmStrCat(this->Reporter.LogDir, "Custom", purpose, ".txt"); cmsys::ifstream fin(fname.c_str(), std::ios::in | std::ios::binary); std::string line; cmsys::RegularExpression rex; @@ -557,35 +288,18 @@ bool cmCTestLaunch::ScrapeLog(std::string const& fname) cmsys::ifstream fin(fname.c_str(), std::ios::in | std::ios::binary); std::string line; while (cmSystemTools::GetLineFromStream(fin, line)) { - if (MatchesFilterPrefix(line)) { + if (this->Reporter.MatchesFilterPrefix(line)) { continue; } - if (this->Match(line, this->RegexWarning) && - !this->Match(line, this->RegexWarningSuppress)) { + if (this->Reporter.Match(line, this->Reporter.RegexWarning) && + !this->Reporter.Match(line, this->Reporter.RegexWarningSuppress)) { return true; } } return false; } -bool cmCTestLaunch::Match(std::string const& line, - std::vector<cmsys::RegularExpression>& regexps) -{ - for (cmsys::RegularExpression& r : regexps) { - if (r.find(line)) { - return true; - } - } - return false; -} - -bool cmCTestLaunch::MatchesFilterPrefix(std::string const& line) const -{ - return !this->OptionFilterPrefix.empty() && - cmHasPrefix(line, this->OptionFilterPrefix); -} - int cmCTestLaunch::Main(int argc, const char* const argv[]) { if (argc == 2) { @@ -605,9 +319,10 @@ void cmCTestLaunch::LoadConfig() cm.GetCurrentSnapshot().SetDefaultDefinitions(); cmGlobalGenerator gg(&cm); cmMakefile mf(&gg, cm.GetCurrentSnapshot()); - std::string fname = cmStrCat(this->LogDir, "CTestLaunchConfig.cmake"); + std::string fname = + cmStrCat(this->Reporter.LogDir, "CTestLaunchConfig.cmake"); if (cmSystemTools::FileExists(fname) && mf.ReadListFile(fname)) { - this->SourceDir = mf.GetSafeDefinition("CTEST_SOURCE_DIRECTORY"); - cmSystemTools::ConvertToUnixSlashes(this->SourceDir); + this->Reporter.SourceDir = mf.GetSafeDefinition("CTEST_SOURCE_DIRECTORY"); + cmSystemTools::ConvertToUnixSlashes(this->Reporter.SourceDir); } } diff --git a/Source/CTest/cmCTestLaunch.h b/Source/CTest/cmCTestLaunch.h index 33ff82c..d18f66d 100644 --- a/Source/CTest/cmCTestLaunch.h +++ b/Source/CTest/cmCTestLaunch.h @@ -4,13 +4,14 @@ #include "cmConfigure.h" // IWYU pragma: keep -#include <set> #include <string> #include <vector> -#include "cmsys/RegularExpression.hxx" +#include "cmCTestLaunchReporter.h" -class cmXMLElement; +namespace cmsys { +class RegularExpression; +} /** \class cmCTestLaunch * \brief Launcher for make rules to report results for ctest @@ -35,70 +36,36 @@ private: int Run(); void RunChild(); - // Methods to check the result of the real command. - bool IsError() const; + // Method to check the result of the real command. bool CheckResults(); - // Launcher options specified before the real command. - std::string OptionOutput; - std::string OptionSource; - std::string OptionLanguage; - std::string OptionTargetName; - std::string OptionTargetType; - std::string OptionBuildDir; - std::string OptionFilterPrefix; + // Parse out launcher-specific options specified before the real command. bool ParseArguments(int argc, const char* const* argv); // The real command line appearing after launcher arguments. int RealArgC; const char* const* RealArgV; - std::string CWD; // The real command line after response file expansion. std::vector<std::string> RealArgs; void HandleRealArg(const char* arg); - // A hash of the real command line is unique and unlikely to collide. - std::string LogHash; - void ComputeFileNames(); - - bool Passthru; struct cmsysProcess_s* Process; - int ExitCode; - // Temporary log files for stdout and stderr of real command. - std::string LogDir; - std::string LogOut; - std::string LogErr; + // Whether or not any data have been written to stdout or stderr. bool HaveOut; bool HaveErr; - // Labels associated with the build rule. - std::set<std::string> Labels; - void LoadLabels(); - bool SourceMatches(std::string const& lhs, std::string const& rhs); - - // Regular expressions to match warnings and their exceptions. + // Load custom rules to match warnings and their exceptions. bool ScrapeRulesLoaded; - std::vector<cmsys::RegularExpression> RegexWarning; - std::vector<cmsys::RegularExpression> RegexWarningSuppress; void LoadScrapeRules(); void LoadScrapeRules(const char* purpose, std::vector<cmsys::RegularExpression>& regexps); bool ScrapeLog(std::string const& fname); - bool Match(std::string const& line, - std::vector<cmsys::RegularExpression>& regexps); - bool MatchesFilterPrefix(std::string const& line) const; - - // Methods to generate the xml fragment. - void WriteXML(); - void WriteXMLAction(cmXMLElement&); - void WriteXMLCommand(cmXMLElement&); - void WriteXMLResult(cmXMLElement&); - void WriteXMLLabels(cmXMLElement&); - void DumpFileToXML(cmXMLElement&, const char* tag, std::string const& fname); + + // Helper class to generate the xml fragment. + cmCTestLaunchReporter Reporter; // Configuration void LoadConfig(); - std::string SourceDir; }; diff --git a/Source/CTest/cmCTestLaunchReporter.cxx b/Source/CTest/cmCTestLaunchReporter.cxx new file mode 100644 index 0000000..6ec7d0e --- /dev/null +++ b/Source/CTest/cmCTestLaunchReporter.cxx @@ -0,0 +1,316 @@ +/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying + file Copyright.txt or https://cmake.org/licensing for details. */ +#include "cmCTestLaunchReporter.h" + +#include "cmsys/FStream.hxx" +#include "cmsys/Process.h" +#include "cmsys/RegularExpression.hxx" + +#include "cmCryptoHash.h" +#include "cmGeneratedFileStream.h" +#include "cmStringAlgorithms.h" +#include "cmSystemTools.h" +#include "cmXMLWriter.h" + +#ifdef _WIN32 +# include <fcntl.h> // for _O_BINARY +# include <io.h> // for _setmode +# include <stdio.h> // for std{out,err} and fileno +#endif + +cmCTestLaunchReporter::cmCTestLaunchReporter() +{ + this->Passthru = true; + this->ExitCode = 1; + this->CWD = cmSystemTools::GetCurrentWorkingDirectory(); + + this->ComputeFileNames(); + + // Common compiler warning formats. These are much simpler than the + // full log-scraping expressions because we do not need to extract + // file and line information. + this->RegexWarning.emplace_back("(^|[ :])[Ww][Aa][Rr][Nn][Ii][Nn][Gg]"); + this->RegexWarning.emplace_back("(^|[ :])[Rr][Ee][Mm][Aa][Rr][Kk]"); + this->RegexWarning.emplace_back("(^|[ :])[Nn][Oo][Tt][Ee]"); +} + +cmCTestLaunchReporter::~cmCTestLaunchReporter() +{ + if (!this->Passthru) { + cmSystemTools::RemoveFile(this->LogOut); + cmSystemTools::RemoveFile(this->LogErr); + } +} + +void cmCTestLaunchReporter::ComputeFileNames() +{ + // We just passthru the behavior of the real command unless the + // CTEST_LAUNCH_LOGS environment variable is set. + std::string d; + if (!cmSystemTools::GetEnv("CTEST_LAUNCH_LOGS", d) || d.empty()) { + return; + } + this->Passthru = false; + + // The environment variable specifies the directory into which we + // generate build logs. + this->LogDir = d; + cmSystemTools::ConvertToUnixSlashes(this->LogDir); + this->LogDir += "/"; + + // We hash the input command working dir and command line to obtain + // a repeatable and (probably) unique name for log files. + cmCryptoHash md5(cmCryptoHash::AlgoMD5); + md5.Initialize(); + md5.Append(this->CWD); + for (std::string const& realArg : this->RealArgs) { + md5.Append(realArg); + } + this->LogHash = md5.FinalizeHex(); + + // We store stdout and stderr in temporary log files. + this->LogOut = cmStrCat(this->LogDir, "launch-", this->LogHash, "-out.txt"); + this->LogErr = cmStrCat(this->LogDir, "launch-", this->LogHash, "-err.txt"); +} + +void cmCTestLaunchReporter::LoadLabels() +{ + if (this->OptionBuildDir.empty() || this->OptionTargetName.empty()) { + return; + } + + // Labels are listed in per-target files. + std::string fname = cmStrCat(this->OptionBuildDir, "/CMakeFiles/", + this->OptionTargetName, ".dir/Labels.txt"); + + // We are interested in per-target labels for this source file. + std::string source = this->OptionSource; + cmSystemTools::ConvertToUnixSlashes(source); + + // Load the labels file. + cmsys::ifstream fin(fname.c_str(), std::ios::in | std::ios::binary); + if (!fin) { + return; + } + bool inTarget = true; + bool inSource = false; + std::string line; + while (cmSystemTools::GetLineFromStream(fin, line)) { + if (line.empty() || line[0] == '#') { + // Ignore blank and comment lines. + continue; + } + if (line[0] == ' ') { + // Label lines appear indented by one space. + if (inTarget || inSource) { + this->Labels.insert(line.substr(1)); + } + } else if (!this->OptionSource.empty() && !inSource) { + // Non-indented lines specify a source file name. The first one + // is the end of the target-wide labels. Use labels following a + // matching source. + inTarget = false; + inSource = this->SourceMatches(line, source); + } else { + return; + } + } +} + +bool cmCTestLaunchReporter::SourceMatches(std::string const& lhs, + std::string const& rhs) +{ + // TODO: Case sensitivity, UseRelativePaths, etc. Note that both + // paths in the comparison get generated by CMake. This is done for + // every source in the target, so it should be efficient (cannot use + // cmSystemTools::IsSameFile). + return lhs == rhs; +} + +bool cmCTestLaunchReporter::IsError() const +{ + return this->ExitCode != 0; +} + +void cmCTestLaunchReporter::WriteXML() +{ + // Name the xml file. + std::string logXML = + cmStrCat(this->LogDir, this->IsError() ? "error-" : "warning-", + this->LogHash, ".xml"); + + // Use cmGeneratedFileStream to atomically create the report file. + cmGeneratedFileStream fxml(logXML); + cmXMLWriter xml(fxml, 2); + cmXMLElement e2(xml, "Failure"); + e2.Attribute("type", this->IsError() ? "Error" : "Warning"); + this->WriteXMLAction(e2); + this->WriteXMLCommand(e2); + this->WriteXMLResult(e2); + this->WriteXMLLabels(e2); +} + +void cmCTestLaunchReporter::WriteXMLAction(cmXMLElement& e2) +{ + e2.Comment("Meta-information about the build action"); + cmXMLElement e3(e2, "Action"); + + // TargetName + if (!this->OptionTargetName.empty()) { + e3.Element("TargetName", this->OptionTargetName); + } + + // Language + if (!this->OptionLanguage.empty()) { + e3.Element("Language", this->OptionLanguage); + } + + // SourceFile + if (!this->OptionSource.empty()) { + std::string source = this->OptionSource; + cmSystemTools::ConvertToUnixSlashes(source); + + // If file is in source tree use its relative location. + if (cmSystemTools::FileIsFullPath(this->SourceDir) && + cmSystemTools::FileIsFullPath(source) && + cmSystemTools::IsSubDirectory(source, this->SourceDir)) { + source = cmSystemTools::RelativePath(this->SourceDir, source); + } + + e3.Element("SourceFile", source); + } + + // OutputFile + if (!this->OptionOutput.empty()) { + e3.Element("OutputFile", this->OptionOutput); + } + + // OutputType + const char* outputType = nullptr; + if (!this->OptionTargetType.empty()) { + if (this->OptionTargetType == "EXECUTABLE") { + outputType = "executable"; + } else if (this->OptionTargetType == "SHARED_LIBRARY") { + outputType = "shared library"; + } else if (this->OptionTargetType == "MODULE_LIBRARY") { + outputType = "module library"; + } else if (this->OptionTargetType == "STATIC_LIBRARY") { + outputType = "static library"; + } + } else if (!this->OptionSource.empty()) { + outputType = "object file"; + } + if (outputType) { + e3.Element("OutputType", outputType); + } +} + +void cmCTestLaunchReporter::WriteXMLCommand(cmXMLElement& e2) +{ + e2.Comment("Details of command"); + cmXMLElement e3(e2, "Command"); + if (!this->CWD.empty()) { + e3.Element("WorkingDirectory", this->CWD); + } + for (std::string const& realArg : this->RealArgs) { + e3.Element("Argument", realArg); + } +} + +void cmCTestLaunchReporter::WriteXMLResult(cmXMLElement& e2) +{ + e2.Comment("Result of command"); + cmXMLElement e3(e2, "Result"); + + // StdOut + this->DumpFileToXML(e3, "StdOut", this->LogOut); + + // StdErr + this->DumpFileToXML(e3, "StdErr", this->LogErr); + + // ExitCondition + cmXMLElement e4(e3, "ExitCondition"); + cmsysProcess* cp = this->Process; + switch (cmsysProcess_GetState(cp)) { + case cmsysProcess_State_Starting: + e4.Content("No process has been executed"); + break; + case cmsysProcess_State_Executing: + e4.Content("The process is still executing"); + break; + case cmsysProcess_State_Disowned: + e4.Content("Disowned"); + break; + case cmsysProcess_State_Killed: + e4.Content("Killed by parent"); + break; + + case cmsysProcess_State_Expired: + e4.Content("Killed when timeout expired"); + break; + case cmsysProcess_State_Exited: + e4.Content(this->ExitCode); + break; + case cmsysProcess_State_Exception: + e4.Content("Terminated abnormally: "); + e4.Content(cmsysProcess_GetExceptionString(cp)); + break; + case cmsysProcess_State_Error: + e4.Content("Error administrating child process: "); + e4.Content(cmsysProcess_GetErrorString(cp)); + break; + } +} + +void cmCTestLaunchReporter::WriteXMLLabels(cmXMLElement& e2) +{ + this->LoadLabels(); + if (!this->Labels.empty()) { + e2.Comment("Interested parties"); + cmXMLElement e3(e2, "Labels"); + for (std::string const& label : this->Labels) { + e3.Element("Label", label); + } + } +} + +void cmCTestLaunchReporter::DumpFileToXML(cmXMLElement& e3, const char* tag, + std::string const& fname) +{ + cmsys::ifstream fin(fname.c_str(), std::ios::in | std::ios::binary); + + std::string line; + const char* sep = ""; + + cmXMLElement e4(e3, tag); + while (cmSystemTools::GetLineFromStream(fin, line)) { + if (MatchesFilterPrefix(line)) { + continue; + } + if (this->Match(line, this->RegexWarningSuppress)) { + line = cmStrCat("[CTest: warning suppressed] ", line); + } else if (this->Match(line, this->RegexWarning)) { + line = cmStrCat("[CTest: warning matched] ", line); + } + e4.Content(sep); + e4.Content(line); + sep = "\n"; + } +} + +bool cmCTestLaunchReporter::Match( + std::string const& line, std::vector<cmsys::RegularExpression>& regexps) +{ + for (cmsys::RegularExpression& r : regexps) { + if (r.find(line)) { + return true; + } + } + return false; +} + +bool cmCTestLaunchReporter::MatchesFilterPrefix(std::string const& line) const +{ + return !this->OptionFilterPrefix.empty() && + cmHasPrefix(line, this->OptionFilterPrefix); +} diff --git a/Source/CTest/cmCTestLaunchReporter.h b/Source/CTest/cmCTestLaunchReporter.h new file mode 100644 index 0000000..675a878 --- /dev/null +++ b/Source/CTest/cmCTestLaunchReporter.h @@ -0,0 +1,81 @@ +/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying + file Copyright.txt or https://cmake.org/licensing for details. */ +#pragma once + +#include "cmConfigure.h" // IWYU pragma: keep + +#include <set> +#include <string> +#include <vector> + +#include "cmsys/RegularExpression.hxx" + +class cmXMLElement; + +/** \class cmCTestLaunchReporter + * \brief Generate CTest XML output for the 'ctest --launch' tool. + */ +class cmCTestLaunchReporter +{ +public: + // Initialize the launcher from its command line. + cmCTestLaunchReporter(); + ~cmCTestLaunchReporter(); + + cmCTestLaunchReporter(const cmCTestLaunchReporter&) = delete; + cmCTestLaunchReporter& operator=(const cmCTestLaunchReporter&) = delete; + + // Methods to check the result of the real command. + bool IsError() const; + + // Launcher options specified before the real command. + std::string OptionOutput; + std::string OptionSource; + std::string OptionLanguage; + std::string OptionTargetName; + std::string OptionTargetType; + std::string OptionBuildDir; + std::string OptionFilterPrefix; + + // The real command line appearing after launcher arguments. + std::string CWD; + + // The real command line after response file expansion. + std::vector<std::string> RealArgs; + + // A hash of the real command line is unique and unlikely to collide. + std::string LogHash; + void ComputeFileNames(); + + bool Passthru; + struct cmsysProcess_s* Process; + int ExitCode; + + // Temporary log files for stdout and stderr of real command. + std::string LogDir; + std::string LogOut; + std::string LogErr; + + // Labels associated with the build rule. + std::set<std::string> Labels; + void LoadLabels(); + bool SourceMatches(std::string const& lhs, std::string const& rhs); + + // Regular expressions to match warnings and their exceptions. + std::vector<cmsys::RegularExpression> RegexWarning; + std::vector<cmsys::RegularExpression> RegexWarningSuppress; + bool Match(std::string const& line, + std::vector<cmsys::RegularExpression>& regexps); + bool MatchesFilterPrefix(std::string const& line) const; + + // Methods to generate the xml fragment. + void WriteXML(); + void WriteXMLAction(cmXMLElement&); + void WriteXMLCommand(cmXMLElement&); + void WriteXMLResult(cmXMLElement&); + void WriteXMLLabels(cmXMLElement&); + void DumpFileToXML(cmXMLElement&, const char* tag, std::string const& fname); + + // Configuration + std::string SourceDir; +}; diff --git a/Source/cmExtraSublimeTextGenerator.cxx b/Source/cmExtraSublimeTextGenerator.cxx index 3e265a0..1c7e4b1 100644 --- a/Source/cmExtraSublimeTextGenerator.cxx +++ b/Source/cmExtraSublimeTextGenerator.cxx @@ -446,13 +446,13 @@ bool cmExtraSublimeTextGenerator::Open(const std::string& bindir, const std::string& projectName, bool dryRun) { - const char* sublExecutable = + cmProp sublExecutable = this->GlobalGenerator->GetCMakeInstance()->GetCacheDefinition( "CMAKE_SUBLIMETEXT_EXECUTABLE"); if (!sublExecutable) { return false; } - if (cmIsNOTFOUND(sublExecutable)) { + if (cmIsNOTFOUND(*sublExecutable)) { return false; } @@ -462,5 +462,5 @@ bool cmExtraSublimeTextGenerator::Open(const std::string& bindir, } return cmSystemTools::RunSingleCommand( - { sublExecutable, "--project", filename }); + { *sublExecutable, "--project", filename }); } diff --git a/Source/cmGeneratorTarget.cxx b/Source/cmGeneratorTarget.cxx index 8589ab1..859fa6a 100644 --- a/Source/cmGeneratorTarget.cxx +++ b/Source/cmGeneratorTarget.cxx @@ -516,9 +516,8 @@ std::string cmGeneratorTarget::GetFilePrefix( const std::string& config, cmStateEnums::ArtifactType artifact) const { if (this->IsImported()) { - const char* prefix = this->GetFilePrefixInternal(config, artifact); - - return prefix ? prefix : std::string(); + cmProp prefix = this->GetFilePrefixInternal(config, artifact); + return prefix ? *prefix : std::string(); } std::string prefix; @@ -531,9 +530,8 @@ std::string cmGeneratorTarget::GetFileSuffix( const std::string& config, cmStateEnums::ArtifactType artifact) const { if (this->IsImported()) { - const char* suffix = this->GetFileSuffixInternal(config, artifact); - - return suffix ? suffix : std::string(); + cmProp suffix = this->GetFileSuffixInternal(config, artifact); + return suffix ? *suffix : std::string(); } std::string prefix; @@ -587,7 +585,7 @@ std::string cmGeneratorTarget::GetFrameworkMultiConfigPostfix( return postfix ? *postfix : std::string(); } -const char* cmGeneratorTarget::GetFilePrefixInternal( +cmProp cmGeneratorTarget::GetFilePrefixInternal( std::string const& config, cmStateEnums::ArtifactType artifact, const std::string& language) const { @@ -623,8 +621,8 @@ const char* cmGeneratorTarget::GetFilePrefixInternal( if (!targetPrefix) { const char* prefixVar = this->Target->GetPrefixVariableInternal(artifact); - if (!language.empty() && prefixVar && *prefixVar) { - std::string langPrefix = prefixVar + std::string("_") + language; + if (!language.empty() && cmNonempty(prefixVar)) { + std::string langPrefix = cmStrCat(prefixVar, "_", language); targetPrefix = this->Makefile->GetDefinition(langPrefix); } @@ -635,9 +633,10 @@ const char* cmGeneratorTarget::GetFilePrefixInternal( } } - return targetPrefix ? targetPrefix->c_str() : nullptr; + return targetPrefix; } -const char* cmGeneratorTarget::GetFileSuffixInternal( + +cmProp cmGeneratorTarget::GetFileSuffixInternal( std::string const& config, cmStateEnums::ArtifactType artifact, const std::string& language) const { @@ -673,8 +672,8 @@ const char* cmGeneratorTarget::GetFileSuffixInternal( if (!targetSuffix) { const char* suffixVar = this->Target->GetSuffixVariableInternal(artifact); - if (!language.empty() && suffixVar && *suffixVar) { - std::string langSuffix = suffixVar + std::string("_") + language; + if (!language.empty() && cmNonempty(suffixVar)) { + std::string langSuffix = cmStrCat(suffixVar, "_", language); targetSuffix = this->Makefile->GetDefinition(langSuffix); } @@ -685,7 +684,7 @@ const char* cmGeneratorTarget::GetFileSuffixInternal( } } - return targetSuffix ? targetSuffix->c_str() : nullptr; + return targetSuffix; } void cmGeneratorTarget::ClearSourcesCache() @@ -1195,8 +1194,8 @@ bool cmGeneratorTarget::IsSystemIncludeDirectory( config_upper = cmSystemTools::UpperCase(config); } - using IncludeCacheType = std::map<std::string, std::vector<std::string>>; - auto iter = this->SystemIncludesCache.find(config_upper); + std::string key = cmStrCat(config_upper, "/", language); + auto iter = this->SystemIncludesCache.find(key); if (iter == this->SystemIncludesCache.end()) { cmGeneratorExpressionDAGChecker dagChecker( @@ -1224,8 +1223,7 @@ bool cmGeneratorTarget::IsSystemIncludeDirectory( std::sort(result.begin(), result.end()); result.erase(std::unique(result.begin(), result.end()), result.end()); - IncludeCacheType::value_type entry(config_upper, result); - iter = this->SystemIncludesCache.insert(entry).first; + iter = this->SystemIncludesCache.emplace(key, result).first; } return std::binary_search(iter->second.begin(), iter->second.end(), dir); @@ -1248,8 +1246,7 @@ bool cmGeneratorTarget::MaybeHaveInterfaceProperty( bool& maybeInterfaceProp = i->second; // If this target itself has a non-empty property value, we are done. - cmProp p = this->GetProperty(prop); - maybeInterfaceProp = cmNonempty(p); + maybeInterfaceProp = cmNonempty(this->GetProperty(prop)); // Otherwise, recurse to interface dependencies. if (!maybeInterfaceProp) { @@ -4943,8 +4940,8 @@ void cmGeneratorTarget::GetFullNameInternal( // retrieve prefix and suffix std::string ll = this->GetLinkerLanguage(config); - const char* targetPrefix = this->GetFilePrefixInternal(config, artifact, ll); - const char* targetSuffix = this->GetFileSuffixInternal(config, artifact, ll); + cmProp targetPrefix = this->GetFilePrefixInternal(config, artifact, ll); + cmProp targetSuffix = this->GetFileSuffixInternal(config, artifact, ll); // The implib option is only allowed for shared libraries, module // libraries, and executables. @@ -4962,18 +4959,18 @@ void cmGeneratorTarget::GetFullNameInternal( if (this->IsFrameworkOnApple()) { fw_prefix = cmStrCat(this->GetFrameworkDirectory(config, ContentLevel), '/'); - targetPrefix = fw_prefix.c_str(); + targetPrefix = &fw_prefix; targetSuffix = nullptr; } if (this->IsCFBundleOnApple()) { fw_prefix = cmStrCat(this->GetCFBundleDirectory(config, FullLevel), '/'); - targetPrefix = fw_prefix.c_str(); + targetPrefix = &fw_prefix; targetSuffix = nullptr; } // Begin the final name with the prefix. - outPrefix = targetPrefix ? targetPrefix : ""; + outPrefix = targetPrefix ? *targetPrefix : ""; // Append the target name or property-specified name. outBase += this->GetOutputName(config, artifact); @@ -4984,7 +4981,7 @@ void cmGeneratorTarget::GetFullNameInternal( // EXECUTABLE_SUFFIX attribute. if (this->IsFrameworkOnApple() && GetGlobalGenerator()->GetName() == "Xcode") { - targetSuffix = configPostfix.c_str(); + targetSuffix = &configPostfix; } else { outBase += configPostfix; } @@ -5000,7 +4997,7 @@ void cmGeneratorTarget::GetFullNameInternal( } // Append the suffix. - outSuffix = targetSuffix ? targetSuffix : ""; + outSuffix = targetSuffix ? *targetSuffix : ""; } std::string cmGeneratorTarget::GetLinkerLanguage( diff --git a/Source/cmGeneratorTarget.h b/Source/cmGeneratorTarget.h index 8e0def7..e5fa892 100644 --- a/Source/cmGeneratorTarget.h +++ b/Source/cmGeneratorTarget.h @@ -849,6 +849,8 @@ private: mutable std::set<std::string> VisitedConfigsForObjects; mutable std::map<cmSourceFile const*, std::string> Objects; std::set<cmSourceFile const*> ExplicitObjectName; + + // "config/language" is the key mutable std::map<std::string, std::vector<std::string>> SystemIncludesCache; mutable std::string ExportMacro; @@ -861,12 +863,12 @@ private: bool NeedImportLibraryName(std::string const& config) const; - const char* GetFilePrefixInternal(std::string const& config, - cmStateEnums::ArtifactType artifact, - const std::string& language = "") const; - const char* GetFileSuffixInternal(std::string const& config, - cmStateEnums::ArtifactType artifact, - const std::string& language = "") const; + cmProp GetFilePrefixInternal(std::string const& config, + cmStateEnums::ArtifactType artifact, + const std::string& language = "") const; + cmProp GetFileSuffixInternal(std::string const& config, + cmStateEnums::ArtifactType artifact, + const std::string& language = "") const; std::string GetFullNameInternal(const std::string& config, cmStateEnums::ArtifactType artifact) const; diff --git a/Source/cmGlobalGenerator.cxx b/Source/cmGlobalGenerator.cxx index 86b01bc..46030e0 100644 --- a/Source/cmGlobalGenerator.cxx +++ b/Source/cmGlobalGenerator.cxx @@ -197,12 +197,12 @@ std::string cmGlobalGenerator::SelectMakeProgram( { std::string makeProgram = inMakeProgram; if (cmIsOff(makeProgram)) { - const char* makeProgramCSTR = + cmProp makeProgramCSTR = this->CMakeInstance->GetCacheDefinition("CMAKE_MAKE_PROGRAM"); if (cmIsOff(makeProgramCSTR)) { makeProgram = makeDefault; } else { - makeProgram = makeProgramCSTR; + makeProgram = *makeProgramCSTR; } if (cmIsOff(makeProgram) && !makeProgram.empty()) { makeProgram = "CMAKE_MAKE_PROGRAM-NOTFOUND"; @@ -2153,10 +2153,11 @@ void cmGlobalGenerator::EnableLanguagesFromGenerator(cmGlobalGenerator* gen, { this->SetConfiguredFilesPath(gen); this->TryCompileOuterMakefile = mf; - const char* make = + cmProp make = gen->GetCMakeInstance()->GetCacheDefinition("CMAKE_MAKE_PROGRAM"); - this->GetCMakeInstance()->AddCacheEntry( - "CMAKE_MAKE_PROGRAM", make, "make program", cmStateEnums::FILEPATH); + this->GetCMakeInstance()->AddCacheEntry("CMAKE_MAKE_PROGRAM", cmToCStr(make), + "make program", + cmStateEnums::FILEPATH); // copy the enabled languages this->GetCMakeInstance()->GetState()->SetEnabledLanguages( gen->GetCMakeInstance()->GetState()->GetEnabledLanguages()); diff --git a/Source/cmGlobalGhsMultiGenerator.cxx b/Source/cmGlobalGhsMultiGenerator.cxx index cbaf0ab..c08c9cf 100644 --- a/Source/cmGlobalGhsMultiGenerator.cxx +++ b/Source/cmGlobalGhsMultiGenerator.cxx @@ -3,7 +3,6 @@ #include "cmGlobalGhsMultiGenerator.h" #include <algorithm> -#include <cstring> #include <map> #include <ostream> #include <utility> @@ -335,23 +334,23 @@ void cmGlobalGhsMultiGenerator::WriteTopLevelProject(std::ostream& fout, fout << "# Top Level Project File\n"; // Specify BSP option if supplied by user - const char* bspName = + cmProp bspName = this->GetCMakeInstance()->GetCacheDefinition("GHS_BSP_NAME"); if (!cmIsOff(bspName)) { - fout << " -bsp " << bspName << '\n'; + fout << " -bsp " << *bspName << '\n'; } // Specify OS DIR if supplied by user // -- not all platforms require this entry in the project file if (!cmIsOff(this->OsDir)) { - const char* osDirOption = + cmProp osDirOption = this->GetCMakeInstance()->GetCacheDefinition("GHS_OS_DIR_OPTION"); std::replace(this->OsDir.begin(), this->OsDir.end(), '\\', '/'); fout << " "; if (cmIsOff(osDirOption)) { fout << ""; } else { - fout << osDirOption; + fout << *osDirOption; } fout << "\"" << this->OsDir << "\"\n"; } @@ -565,9 +564,9 @@ cmGlobalGhsMultiGenerator::GenerateBuildCommand( { GeneratedMakeCommand makeCommand = {}; std::string gbuild; - if (const char* gbuildCached = + if (cmProp gbuildCached = this->CMakeInstance->GetCacheDefinition("CMAKE_MAKE_PROGRAM")) { - gbuild = gbuildCached; + gbuild = *gbuildCached; } makeCommand.Add(this->SelectMakeProgram(makeProgram, gbuild)); @@ -618,11 +617,10 @@ void cmGlobalGhsMultiGenerator::WriteMacros(std::ostream& fout, cmLocalGenerator* root) { fout << "macro PROJ_NAME=" << root->GetProjectName() << '\n'; - char const* ghsGpjMacros = + cmProp ghsGpjMacros = this->GetCMakeInstance()->GetCacheDefinition("GHS_GPJ_MACROS"); - if (nullptr != ghsGpjMacros) { - std::vector<std::string> expandedList = - cmExpandedList(std::string(ghsGpjMacros)); + if (ghsGpjMacros) { + std::vector<std::string> expandedList = cmExpandedList(*ghsGpjMacros); for (std::string const& arg : expandedList) { fout << "macro " << arg << '\n'; } @@ -634,17 +632,17 @@ void cmGlobalGhsMultiGenerator::WriteHighLevelDirectives( { /* set primary target */ std::string tgt; - const char* t = + cmProp t = this->GetCMakeInstance()->GetCacheDefinition("GHS_PRIMARY_TARGET"); if (cmNonempty(t)) { - tgt = t; + tgt = *t; this->GetCMakeInstance()->MarkCliAsUsed("GHS_PRIMARY_TARGET"); } else { - const char* a = + cmProp a = this->GetCMakeInstance()->GetCacheDefinition("CMAKE_GENERATOR_PLATFORM"); - const char* p = + cmProp p = this->GetCMakeInstance()->GetCacheDefinition("GHS_TARGET_PLATFORM"); - tgt = cmStrCat((a ? a : ""), '_', (p ? p : ""), ".tgt"); + tgt = cmStrCat((a ? *a : ""), '_', (p ? *p : ""), ".tgt"); } /* clang-format off */ @@ -655,11 +653,11 @@ void cmGlobalGhsMultiGenerator::WriteHighLevelDirectives( << "/CMakeFiles/custom_target.bod" << '\n'; /* clang-format on */ - char const* const customization = + cmProp const customization = this->GetCMakeInstance()->GetCacheDefinition("GHS_CUSTOMIZATION"); - if (nullptr != customization && strlen(customization) > 0) { + if (cmNonempty(customization)) { fout << "customization=" - << cmGlobalGhsMultiGenerator::TrimQuotes(customization) << '\n'; + << cmGlobalGhsMultiGenerator::TrimQuotes(*customization) << '\n'; this->GetCMakeInstance()->MarkCliAsUsed("GHS_CUSTOMIZATION"); } } diff --git a/Source/cmGlobalUnixMakefileGenerator3.cxx b/Source/cmGlobalUnixMakefileGenerator3.cxx index 37a77fa..2c934e1 100644 --- a/Source/cmGlobalUnixMakefileGenerator3.cxx +++ b/Source/cmGlobalUnixMakefileGenerator3.cxx @@ -104,8 +104,8 @@ std::string cmGlobalUnixMakefileGenerator3::GetEditCacheCommand() const cmStateEnums::INTERNAL); } } - const char* edit_cmd = cm->GetCacheDefinition("CMAKE_EDIT_COMMAND"); - return edit_cmd ? edit_cmd : ""; + cmProp edit_cmd = cm->GetCacheDefinition("CMAKE_EDIT_COMMAND"); + return edit_cmd ? *edit_cmd : std::string(); } void cmGlobalUnixMakefileGenerator3::ComputeTargetObjectDirectory( diff --git a/Source/cmGlobalVisualStudio7Generator.cxx b/Source/cmGlobalVisualStudio7Generator.cxx index 84cfaeb..6267205 100644 --- a/Source/cmGlobalVisualStudio7Generator.cxx +++ b/Source/cmGlobalVisualStudio7Generator.cxx @@ -625,9 +625,9 @@ std::string cmGlobalVisualStudio7Generator::WriteUtilityDepend( std::string cmGlobalVisualStudio7Generator::GetGUID(std::string const& name) { std::string const& guidStoreName = name + "_GUID_CMAKE"; - if (const char* storedGUID = + if (cmProp storedGUID = this->CMakeInstance->GetCacheDefinition(guidStoreName)) { - return std::string(storedGUID); + return *storedGUID; } // Compute a GUID that is deterministic but unique to the build tree. std::string input = diff --git a/Source/cmGlobalXCodeGenerator.cxx b/Source/cmGlobalXCodeGenerator.cxx index b57bdbc..b78f0a0 100644 --- a/Source/cmGlobalXCodeGenerator.cxx +++ b/Source/cmGlobalXCodeGenerator.cxx @@ -3140,11 +3140,10 @@ std::string cmGlobalXCodeGenerator::GetOrCreateId(const std::string& name, const std::string& id) { std::string guidStoreName = cmStrCat(name, "_GUID_CMAKE"); - const char* storedGUID = - this->CMakeInstance->GetCacheDefinition(guidStoreName); + cmProp storedGUID = this->CMakeInstance->GetCacheDefinition(guidStoreName); if (storedGUID) { - return storedGUID; + return *storedGUID; } this->CMakeInstance->AddCacheEntry(guidStoreName, id.c_str(), diff --git a/Source/cmake.cxx b/Source/cmake.cxx index bca938e..014a707 100644 --- a/Source/cmake.cxx +++ b/Source/cmake.cxx @@ -2000,10 +2000,9 @@ std::string cmake::StripExtension(const std::string& file) const return file; } -const char* cmake::GetCacheDefinition(const std::string& name) const +cmProp cmake::GetCacheDefinition(const std::string& name) const { - cmProp p = this->State->GetInitializedCacheValue(name); - return p ? p->c_str() : nullptr; + return this->State->GetInitializedCacheValue(name); } void cmake::AddScriptingCommands() diff --git a/Source/cmake.h b/Source/cmake.h index 44c35c2..63635cb 100644 --- a/Source/cmake.h +++ b/Source/cmake.h @@ -299,7 +299,7 @@ public: /** * Given a variable name, return its value (as a string). */ - const char* GetCacheDefinition(const std::string&) const; + cmProp GetCacheDefinition(const std::string&) const; //! Add an entry into the cache void AddCacheEntry(const std::string& key, const char* value, const char* helpString, int type); diff --git a/Templates/MSBuild/FlagTables/v142_CL.json b/Templates/MSBuild/FlagTables/v142_CL.json index 95b9d14..7c2d291 100644 --- a/Templates/MSBuild/FlagTables/v142_CL.json +++ b/Templates/MSBuild/FlagTables/v142_CL.json @@ -455,6 +455,20 @@ "flags": [] }, { + "name": "LanguageStandard_C", + "switch": "std:c11", + "comment": "ISO C11 Standard", + "value": "stdc11", + "flags": [] + }, + { + "name": "LanguageStandard_C", + "switch": "std:c17", + "comment": "ISO C17 (2018) Standard", + "value": "stdc17", + "flags": [] + }, + { "name": "PrecompiledHeader", "switch": "Yc", "comment": "Create", diff --git a/Tests/IncludeDirectories/CMakeLists.txt b/Tests/IncludeDirectories/CMakeLists.txt index eb08676..1f5b664 100644 --- a/Tests/IncludeDirectories/CMakeLists.txt +++ b/Tests/IncludeDirectories/CMakeLists.txt @@ -17,6 +17,7 @@ if (((CMAKE_C_COMPILER_ID STREQUAL GNU AND CMAKE_C_COMPILER_VERSION VERSION_GREA endif() if (run_sys_includes_test) add_subdirectory(SystemIncludeDirectories) + add_subdirectory(SystemIncludeDirectoriesPerLang) endif() endif() diff --git a/Tests/IncludeDirectories/SystemIncludeDirectoriesPerLang/CMakeLists.txt b/Tests/IncludeDirectories/SystemIncludeDirectoriesPerLang/CMakeLists.txt new file mode 100644 index 0000000..70dfa01 --- /dev/null +++ b/Tests/IncludeDirectories/SystemIncludeDirectoriesPerLang/CMakeLists.txt @@ -0,0 +1,23 @@ +cmake_minimum_required(VERSION 3.17 FATAL_ERROR) + +project(SystemIncludeDirectoriesPerLang) + +add_library(c_interface INTERFACE) +set_target_properties(c_interface PROPERTIES + INTERFACE_INCLUDE_DIRECTORIES "$<$<COMPILE_LANGUAGE:C>:${CMAKE_CURRENT_SOURCE_DIR}>" + INTERFACE_SYSTEM_INCLUDE_DIRECTORIES "$<$<COMPILE_LANGUAGE:C>:${CMAKE_CURRENT_SOURCE_DIR}>" +) +target_compile_options(c_interface INTERFACE "$<$<COMPILE_LANG_AND_ID:C,GNU,Clang>:-Werror=unused-variable>") + +add_library(cxx_interface INTERFACE) +set_target_properties(cxx_interface PROPERTIES + INTERFACE_INCLUDE_DIRECTORIES "$<$<COMPILE_LANGUAGE:CXX>:${CMAKE_CURRENT_SOURCE_DIR}/cxx_system_include>" + INTERFACE_SYSTEM_INCLUDE_DIRECTORIES "$<$<COMPILE_LANGUAGE:CXX>:${CMAKE_CURRENT_SOURCE_DIR}/cxx_system_include>" +) +target_compile_options(cxx_interface INTERFACE "$<$<COMPILE_LANG_AND_ID:CXX,GNU,Clang>:-Werror=unused-variable>") + +# The C header must come before the C++ header for this test to smoke out the +# failure. The order of sources is how CMake determines the include cache +# and we need it to cache on the 'bad' language first +add_executable(consume_multi_lang_includes main.c smoke_out_includes.cxx) +target_link_libraries(consume_multi_lang_includes PRIVATE c_interface cxx_interface) diff --git a/Tests/IncludeDirectories/SystemIncludeDirectoriesPerLang/cxx_system_include/header.h b/Tests/IncludeDirectories/SystemIncludeDirectoriesPerLang/cxx_system_include/header.h new file mode 100644 index 0000000..8dcd226 --- /dev/null +++ b/Tests/IncludeDirectories/SystemIncludeDirectoriesPerLang/cxx_system_include/header.h @@ -0,0 +1,10 @@ + +// Generate a warning in here + +int function_that_generates_warning(int x) +{ + int y = x; + int z = 2; + y -= x; + return y; +} diff --git a/Tests/IncludeDirectories/SystemIncludeDirectoriesPerLang/main.c b/Tests/IncludeDirectories/SystemIncludeDirectoriesPerLang/main.c new file mode 100644 index 0000000..f8b643a --- /dev/null +++ b/Tests/IncludeDirectories/SystemIncludeDirectoriesPerLang/main.c @@ -0,0 +1,4 @@ +int main() +{ + return 0; +} diff --git a/Tests/IncludeDirectories/SystemIncludeDirectoriesPerLang/smoke_out_includes.cxx b/Tests/IncludeDirectories/SystemIncludeDirectoriesPerLang/smoke_out_includes.cxx new file mode 100644 index 0000000..dbfc557 --- /dev/null +++ b/Tests/IncludeDirectories/SystemIncludeDirectoriesPerLang/smoke_out_includes.cxx @@ -0,0 +1,7 @@ + +#include <header.h> + +int empty_func() +{ + return function_that_generates_warning(4); +} diff --git a/Tests/RunCMake/CMakeLists.txt b/Tests/RunCMake/CMakeLists.txt index c97a959..c70eb75 100644 --- a/Tests/RunCMake/CMakeLists.txt +++ b/Tests/RunCMake/CMakeLists.txt @@ -311,7 +311,11 @@ add_RunCMake_test(cmake_parse_arguments) add_RunCMake_test(cmake_path) add_RunCMake_test(continue) add_executable(color_warning color_warning.c) -add_RunCMake_test(ctest_build -DCOLOR_WARNING=$<TARGET_FILE:color_warning>) +add_executable(fake_build_command fake_build_command.c) +add_RunCMake_test(ctest_build + -DCOLOR_WARNING=$<TARGET_FILE:color_warning> + -DFAKE_BUILD_COMMAND_EXE=$<TARGET_FILE:fake_build_command> +) add_RunCMake_test(ctest_cmake_error) add_RunCMake_test(ctest_configure) if(COVERAGE_COMMAND) diff --git a/Tests/RunCMake/ctest_build/BuildCommandFailure-check.cmake b/Tests/RunCMake/ctest_build/BuildCommandFailure-check.cmake new file mode 100644 index 0000000..feac3ce --- /dev/null +++ b/Tests/RunCMake/ctest_build/BuildCommandFailure-check.cmake @@ -0,0 +1,12 @@ +file(GLOB build_xml_file "${RunCMake_TEST_BINARY_DIR}/Testing/*/Build.xml") +if(build_xml_file) + file(READ "${build_xml_file}" build_xml LIMIT 4096) + if(NOT build_xml MATCHES [[this command failed]]) + string(REPLACE "\n" "\n " build_xml " ${build_xml}") + set(RunCMake_TEST_FAILED + "Build.xml does not have expected error message:\n${build_xml}" + ) + endif() +else() + set(RunCMake_TEST_FAILED "Build.xml not found") +endif() diff --git a/Tests/RunCMake/ctest_build/BuildCommandFailure-result.txt b/Tests/RunCMake/ctest_build/BuildCommandFailure-result.txt new file mode 100644 index 0000000..b57e2de --- /dev/null +++ b/Tests/RunCMake/ctest_build/BuildCommandFailure-result.txt @@ -0,0 +1 @@ +(-1|255) diff --git a/Tests/RunCMake/ctest_build/BuildCommandFailure-stderr.txt b/Tests/RunCMake/ctest_build/BuildCommandFailure-stderr.txt new file mode 100644 index 0000000..bbe9410 --- /dev/null +++ b/Tests/RunCMake/ctest_build/BuildCommandFailure-stderr.txt @@ -0,0 +1 @@ +^Error\(s\) when building project diff --git a/Tests/RunCMake/ctest_build/RunCMakeTest.cmake b/Tests/RunCMake/ctest_build/RunCMakeTest.cmake index b2e562a..072fbac 100644 --- a/Tests/RunCMake/ctest_build/RunCMakeTest.cmake +++ b/Tests/RunCMake/ctest_build/RunCMakeTest.cmake @@ -48,8 +48,12 @@ function(run_BuildChangeId) endfunction() run_BuildChangeId() -set(RunCMake_USE_LAUNCHERS FALSE) set(RunCMake_USE_CUSTOM_BUILD_COMMAND TRUE) +set(RunCMake_BUILD_COMMAND "${FAKE_BUILD_COMMAND_EXE}") +run_ctest(BuildCommandFailure) +unset(RunCMake_BUILD_COMMAND) + +set(RunCMake_USE_LAUNCHERS FALSE) set(RunCMake_BUILD_COMMAND "${COLOR_WARNING}") run_ctest(IgnoreColor) unset(RunCMake_BUILD_COMMAND) diff --git a/Tests/RunCMake/fake_build_command.c b/Tests/RunCMake/fake_build_command.c new file mode 100644 index 0000000..d87335b --- /dev/null +++ b/Tests/RunCMake/fake_build_command.c @@ -0,0 +1,6 @@ +#include <stdio.h> +int main(void) +{ + printf("this command failed\n"); + return 1; +} |