diff options
author | Matthew Woehlke <matthew.woehlke@kitware.com> | 2022-09-22 17:43:14 (GMT) |
---|---|---|
committer | Matthew Woehlke <matthew.woehlke@kitware.com> | 2022-09-22 18:56:50 (GMT) |
commit | 611d80179006ba6670273fcfcad46b0bb64a36c2 (patch) | |
tree | 1975420c55f72ce6e9beb22c80aeae18398ded6b /Source/cmCoreTryCompile.cxx | |
parent | a04eaf6742d98dd415ee939eae8c955c8afd1ce6 (diff) | |
download | CMake-611d80179006ba6670273fcfcad46b0bb64a36c2.zip CMake-611d80179006ba6670273fcfcad46b0bb64a36c2.tar.gz CMake-611d80179006ba6670273fcfcad46b0bb64a36c2.tar.bz2 |
try_compile: Add SOURCE_FROM_FILE
Add ability to copy try_compile (and try_run) source files from
arbitrary locations into the operation directory. This is included for
the sake of completion and consolidation, although use cases which
actually require this may be rare.
Diffstat (limited to 'Source/cmCoreTryCompile.cxx')
-rw-r--r-- | Source/cmCoreTryCompile.cxx | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/Source/cmCoreTryCompile.cxx b/Source/cmCoreTryCompile.cxx index 0ef32c0..839cb7b 100644 --- a/Source/cmCoreTryCompile.cxx +++ b/Source/cmCoreTryCompile.cxx @@ -174,6 +174,7 @@ auto const TryCompileBaseNewSourcesArgParser = cmArgumentParser<Arguments>{ TryCompileBaseSourcesArgParser } .Bind("SOURCE_FROM_ARG"_s, &Arguments::SourceFromArg) .Bind("SOURCE_FROM_VAR"_s, &Arguments::SourceFromVar) + .Bind("SOURCE_FROM_FILE"_s, &Arguments::SourceFromFile) /* keep semicolon on own line */; auto const TryCompileBaseProjectArgParser = @@ -416,6 +417,12 @@ bool cmCoreTryCompile::TryCompileCode(Arguments& arguments, "SOURCE_FROM_VAR requires exactly two arguments"); return false; } + if (arguments.SourceFromFile && arguments.SourceFromFile->size() % 2) { + this->Makefile->IssueMessage( + MessageType::FATAL_ERROR, + "SOURCE_FROM_FILE requires exactly two arguments"); + return false; + } } else { // only valid for srcfile signatures if (!arguments.LangProps.empty()) { @@ -497,6 +504,31 @@ bool cmCoreTryCompile::TryCompileCode(Arguments& arguments, sources.emplace_back(std::move(out)); } } + if (arguments.SourceFromFile) { + auto const k = arguments.SourceFromFile->size(); + for (auto i = decltype(k){ 0 }; i < k; i += 2) { + const auto& dst = (*arguments.SourceFromFile)[i + 0]; + const auto& src = (*arguments.SourceFromFile)[i + 1]; + + if (!cmSystemTools::GetFilenamePath(dst).empty()) { + const auto& msg = + cmStrCat("SOURCE_FROM_FILE given invalid filename \"", dst, "\""); + this->Makefile->IssueMessage(MessageType::FATAL_ERROR, msg); + return false; + } + + auto dstPath = cmStrCat(this->BinaryDirectory, "/", dst); + auto const result = cmSystemTools::CopyFileAlways(src, dstPath); + if (!result.IsSuccess()) { + const auto& msg = cmStrCat("SOURCE_FROM_FILE failed to copy \"", src, + "\": ", result.GetString()); + this->Makefile->IssueMessage(MessageType::FATAL_ERROR, msg); + return false; + } + + sources.emplace_back(std::move(dstPath)); + } + } // TODO: ensure sources is not empty // Detect languages to enable. |