Unity: Add option to use relative paths for unity files

Add a target property that will be read on unity file generation to
attempt to use a relative path to the source file from the CMake Source
Directory or the generated unity file. Additionally add the CMake
Source Directory and the CMake Binary directory to the include path of
the generated unity files.

Closes: #26352
This commit is contained in:
James Moore 2024-10-15 09:43:14 -07:00
parent f521d20cb4
commit 84996a65aa
21 changed files with 387 additions and 31 deletions

View File

@ -415,6 +415,7 @@ Properties on Targets
/prop_tgt/UNITY_BUILD_CODE_AFTER_INCLUDE
/prop_tgt/UNITY_BUILD_CODE_BEFORE_INCLUDE
/prop_tgt/UNITY_BUILD_MODE
/prop_tgt/UNITY_BUILD_RELOCATABLE
/prop_tgt/UNITY_BUILD_UNIQUE_ID
/prop_tgt/VERIFY_INTERFACE_HEADER_SETS
/prop_tgt/VERSION

View File

@ -0,0 +1,40 @@
UNITY_BUILD_RELOCATABLE
-----------------------
.. versionadded:: 3.32
By default, the unity file generated when :prop_tgt:`UNITY_BUILD` is enabled
uses absolute paths to reference the original source files. This causes the
unity file to result in a different output depending on the location of the
source files.
When this property is set to true, the ``#include`` lines inside the generated
unity source files will attempt to use relative paths to the original source
files if possible in order to standardize the output of the unity file.
The unity file's path to the original source file will use the following
priority:
* relative path to the generated unity file if the source file exists
directly or in subfolder under the :variable:`CMAKE_BINARY_DIR`
* relative path to :variable:`CMAKE_SOURCE_DIR` if the source file exists
directly or in subfolder under the :variable:`CMAKE_SOURCE_DIR`
* absolute path to the source file.
This target property *does not* guarantee a consistent unity file across
different environments as the final priority is an absolute path.
Example usage:
.. code-block:: cmake
add_library(example_library
source1.cxx
source2.cxx
source3.cxx)
set_target_properties(example_library PROPERTIES
UNITY_BUILD True
UNITY_BUILD_RELOCATABLE TRUE)

View File

@ -3087,7 +3087,8 @@ inline void RegisterUnitySources(cmGeneratorTarget* target, cmSourceFile* sf,
cmLocalGenerator::UnitySource cmLocalGenerator::WriteUnitySource(
cmGeneratorTarget* target, std::vector<std::string> const& configs,
cmRange<std::vector<UnityBatchedSource>::const_iterator> sources,
cmValue beforeInclude, cmValue afterInclude, std::string filename) const
cmValue beforeInclude, cmValue afterInclude, std::string filename,
std::string const& unityFileDirectory, UnityPathMode pathMode) const
{
cmValue uniqueIdName = target->GetProperty("UNITY_BUILD_UNIQUE_ID");
cmGeneratedFileStream file(
@ -3110,7 +3111,8 @@ cmLocalGenerator::UnitySource cmLocalGenerator::WriteUnitySource(
}
RegisterUnitySources(target, ubs.Source, filename);
WriteUnitySourceInclude(file, cond, ubs.Source->ResolveFullPath(),
beforeInclude, afterInclude, uniqueIdName);
beforeInclude, afterInclude, uniqueIdName,
pathMode, unityFileDirectory);
}
return UnitySource(std::move(filename), perConfig);
@ -3119,28 +3121,35 @@ cmLocalGenerator::UnitySource cmLocalGenerator::WriteUnitySource(
void cmLocalGenerator::WriteUnitySourceInclude(
std::ostream& unity_file, cm::optional<std::string> const& cond,
std::string const& sf_full_path, cmValue beforeInclude, cmValue afterInclude,
cmValue uniqueIdName) const
cmValue uniqueIdName, UnityPathMode pathMode,
std::string const& unityFileDirectory) const
{
if (cond) {
unity_file << "#if " << *cond << "\n";
}
std::string pathToHash;
std::string relocatableIncludePath;
auto PathEqOrSubDir = [](std::string const& a, std::string const& b) {
return (cmSystemTools::ComparePath(a, b) ||
cmSystemTools::IsSubDirectory(a, b));
};
const auto path = cmSystemTools::GetFilenamePath(sf_full_path);
if (PathEqOrSubDir(path, this->GetBinaryDirectory())) {
relocatableIncludePath =
cmSystemTools::RelativePath(unityFileDirectory, sf_full_path);
pathToHash = "BLD_" +
cmSystemTools::RelativePath(this->GetBinaryDirectory(), sf_full_path);
} else if (PathEqOrSubDir(path, this->GetSourceDirectory())) {
relocatableIncludePath =
cmSystemTools::RelativePath(this->GetSourceDirectory(), sf_full_path);
pathToHash = "SRC_" + relocatableIncludePath;
} else {
relocatableIncludePath = sf_full_path;
pathToHash = "ABS_" + sf_full_path;
}
if (cmNonempty(uniqueIdName)) {
std::string pathToHash;
auto PathEqOrSubDir = [](std::string const& a, std::string const& b) {
return (cmSystemTools::ComparePath(a, b) ||
cmSystemTools::IsSubDirectory(a, b));
};
const auto path = cmSystemTools::GetFilenamePath(sf_full_path);
if (PathEqOrSubDir(path, this->GetBinaryDirectory())) {
pathToHash = "BLD_" +
cmSystemTools::RelativePath(this->GetBinaryDirectory(), sf_full_path);
} else if (PathEqOrSubDir(path, this->GetSourceDirectory())) {
pathToHash = "SRC_" +
cmSystemTools::RelativePath(this->GetSourceDirectory(), sf_full_path);
} else {
pathToHash = "ABS_" + sf_full_path;
}
cmCryptoHash hasher(cmCryptoHash::AlgoMD5);
unity_file << "/* " << pathToHash << " */\n"
<< "#undef " << *uniqueIdName << "\n"
@ -3156,7 +3165,11 @@ void cmLocalGenerator::WriteUnitySourceInclude(
unity_file
<< "/* NOLINTNEXTLINE(bugprone-suspicious-include,misc-include-cleaner) "
"*/\n";
unity_file << "#include \"" << sf_full_path << "\"\n";
if (pathMode == UnityPathMode::Relative) {
unity_file << "#include \"" << relocatableIncludePath << "\"\n";
} else {
unity_file << "#include \"" << sf_full_path << "\"\n";
}
if (afterInclude) {
unity_file << *afterInclude << "\n";
@ -3192,7 +3205,7 @@ cmLocalGenerator::AddUnityFilesModeAuto(
std::vector<std::string> const& configs,
std::vector<UnityBatchedSource> const& filtered_sources,
cmValue beforeInclude, cmValue afterInclude,
std::string const& filename_base, size_t batchSize)
std::string const& filename_base, UnityPathMode pathMode, size_t batchSize)
{
if (batchSize == 0) {
batchSize = filtered_sources.size();
@ -3210,7 +3223,7 @@ cmLocalGenerator::AddUnityFilesModeAuto(
auto const end = begin + chunk;
unity_files.emplace_back(this->WriteUnitySource(
target, configs, cmMakeRange(begin, end), beforeInclude, afterInclude,
std::move(filename)));
std::move(filename), filename_base, pathMode));
}
return unity_files;
}
@ -3221,7 +3234,7 @@ cmLocalGenerator::AddUnityFilesModeGroup(
std::vector<std::string> const& configs,
std::vector<UnityBatchedSource> const& filtered_sources,
cmValue beforeInclude, cmValue afterInclude,
std::string const& filename_base)
std::string const& filename_base, UnityPathMode pathMode)
{
std::vector<UnitySource> unity_files;
@ -3247,7 +3260,7 @@ cmLocalGenerator::AddUnityFilesModeGroup(
cmStrCat(filename_base, "unity_", name, unity_file_extension(lang));
unity_files.emplace_back(this->WriteUnitySource(
target, configs, cmMakeRange(item.second), beforeInclude, afterInclude,
std::move(filename)));
std::move(filename), filename_base, pathMode));
}
return unity_files;
@ -3305,6 +3318,9 @@ void cmLocalGenerator::AddUnityBuild(cmGeneratorTarget* target)
target->GetProperty("UNITY_BUILD_CODE_BEFORE_INCLUDE");
cmValue afterInclude = target->GetProperty("UNITY_BUILD_CODE_AFTER_INCLUDE");
cmValue unityMode = target->GetProperty("UNITY_BUILD_MODE");
UnityPathMode pathMode = target->GetPropertyAsBool("UNITY_BUILD_RELOCATABLE")
? UnityPathMode::Relative
: UnityPathMode::Absolute;
for (std::string lang : { "C", "CXX", "OBJC", "OBJCXX", "CUDA" }) {
std::vector<UnityBatchedSource> filtered_sources;
@ -3325,11 +3341,11 @@ void cmLocalGenerator::AddUnityBuild(cmGeneratorTarget* target)
if (!unityMode || *unityMode == "BATCH") {
unity_files = AddUnityFilesModeAuto(
target, lang, configs, filtered_sources, beforeInclude, afterInclude,
filename_base, unityBatchSize);
filename_base, pathMode, unityBatchSize);
} else if (unityMode && *unityMode == "GROUP") {
unity_files =
AddUnityFilesModeGroup(target, lang, configs, filtered_sources,
beforeInclude, afterInclude, filename_base);
unity_files = AddUnityFilesModeGroup(
target, lang, configs, filtered_sources, beforeInclude, afterInclude,
filename_base, pathMode);
} else {
// unity mode is set to an unsupported value
std::string e("Invalid UNITY_BUILD_MODE value of " + *unityMode +
@ -3348,6 +3364,11 @@ void cmLocalGenerator::AddUnityBuild(cmGeneratorTarget* target)
unity->SetProperty("COMPILE_DEFINITIONS",
"CMAKE_UNITY_CONFIG_$<UPPER_CASE:$<CONFIG>>");
}
if (pathMode == UnityPathMode::Relative) {
unity->AppendProperty("INCLUDE_DIRECTORIES",
this->GetSourceDirectory(), false);
}
}
}
}

View File

@ -699,28 +699,37 @@ private:
{
}
};
/** Whether to insert relative or absolute paths into unity files */
enum class UnityPathMode
{
Absolute,
Relative
};
UnitySource WriteUnitySource(
cmGeneratorTarget* target, std::vector<std::string> const& configs,
cmRange<std::vector<UnityBatchedSource>::const_iterator> sources,
cmValue beforeInclude, cmValue afterInclude, std::string filename) const;
cmValue beforeInclude, cmValue afterInclude, std::string filename,
std::string const& unityFileDirectory, UnityPathMode pathMode) const;
void WriteUnitySourceInclude(std::ostream& unity_file,
cm::optional<std::string> const& cond,
std::string const& sf_full_path,
cmValue beforeInclude, cmValue afterInclude,
cmValue uniqueIdName) const;
cmValue uniqueIdName, UnityPathMode pathMode,
std::string const& unityFileDirectory) const;
std::vector<UnitySource> AddUnityFilesModeAuto(
cmGeneratorTarget* target, std::string const& lang,
std::vector<std::string> const& configs,
std::vector<UnityBatchedSource> const& filtered_sources,
cmValue beforeInclude, cmValue afterInclude,
std::string const& filename_base, size_t batchSize);
std::string const& filename_base, UnityPathMode pathMode,
size_t batchSize);
std::vector<UnitySource> AddUnityFilesModeGroup(
cmGeneratorTarget* target, std::string const& lang,
std::vector<std::string> const& configs,
std::vector<UnityBatchedSource> const& filtered_sources,
cmValue beforeInclude, cmValue afterInclude,
std::string const& filename_base);
std::string const& filename_base, UnityPathMode pathMode);
};
namespace detail {

View File

@ -553,6 +553,7 @@ TargetProperty const StaticTargetProperties[] = {
{ "UNITY_BUILD_UNIQUE_ID"_s, IC::CanCompileSources },
{ "UNITY_BUILD_BATCH_SIZE"_s, "8"_s, IC::CanCompileSources },
{ "UNITY_BUILD_MODE"_s, "BATCH"_s, IC::CanCompileSources },
{ "UNITY_BUILD_RELOCATABLE"_s, IC::CanCompileSources },
{ "OPTIMIZE_DEPENDENCIES"_s, IC::CanCompileSources },
{ "VERIFY_INTERFACE_HEADER_SETS"_s },
// -- Android

View File

@ -8,10 +8,16 @@ function(run_build name)
endfunction()
run_cmake(unitybuild_c)
run_cmake(unitybuild_c_absolute_path)
run_cmake(unitybuild_c_relocatable_path)
run_cmake(unitybuild_c_batch)
run_cmake(unitybuild_c_group)
run_cmake(unitybuild_cxx)
run_cmake(unitybuild_cxx_absolute_path)
run_cmake(unitybuild_cxx_relocatable_path)
run_cmake(unitybuild_cxx_group)
run_cmake(unitybuild_c_and_cxx_absolute_path)
run_cmake(unitybuild_c_and_cxx_relocatable_path)
run_cmake(unitybuild_c_and_cxx)
run_cmake(unitybuild_c_and_cxx_group)
if(CMake_TEST_OBJC)
@ -37,6 +43,7 @@ run_cmake(unitybuild_invalid_mode)
run_build(unitybuild_anon_ns)
run_build(unitybuild_anon_ns_no_unity_build)
run_build(unitybuild_anon_ns_group_mode)
run_cmake(unitybuild_relocatable_locations)
function(run_per_config name)
set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/${name}-build)

View File

@ -0,0 +1,5 @@
int foo(int x)
{
(void)x;
return 0;
}

View File

@ -0,0 +1,17 @@
set(unitybuild_c "${RunCMake_TEST_BINARY_DIR}/CMakeFiles/tgt.dir/Unity/unity_0_c.c")
if(NOT EXISTS "${unitybuild_c}")
set(RunCMake_TEST_FAILED "Generated unity source file ${unitybuild_c} does not exist.")
return()
endif()
string(JOIN ".*" EXPECTED_UNITY_FILE_CONTENT
[[#include "([A-Za-z]:)?/[^"]*/Tests/RunCMake/UnityBuild/unitybuild_c_absolute_path-build/s1\.c"]]
[[#include "([A-Za-z]:)?/[^"]*/Tests/RunCMake/UnityBuild/unitybuild_c_absolute_path-build/s2\.c"]]
[[#include "([A-Za-z]:)?/[^"]*/Tests/RunCMake/UnityBuild/unitybuild_c_absolute_path-build/s3\.c"]]
)
file(STRINGS ${unitybuild_c} unitybuild_c_strings)
if(NOT unitybuild_c_strings MATCHES "${EXPECTED_UNITY_FILE_CONTENT}")
set(RunCMake_TEST_FAILED "Generated unity file ${unitybuild_c} doesn't contain absolute paths")
return()
endif()

View File

@ -0,0 +1,13 @@
project(unitybuild_c_absolute_path C)
set(srcs "")
foreach(s RANGE 1 3)
set(src "${CMAKE_CURRENT_BINARY_DIR}/s${s}.c")
file(WRITE "${src}" "int s${s}(void) { return 0; }\n")
list(APPEND srcs "${src}")
endforeach()
add_library(tgt SHARED ${srcs})
set_target_properties(tgt PROPERTIES UNITY_BUILD ON
UNITY_BUILD_RELOCATABLE FALSE)

View File

@ -0,0 +1,35 @@
set(unitybuild_c "${RunCMake_TEST_BINARY_DIR}/CMakeFiles/tgt.dir/Unity/unity_0_c.c")
if(NOT EXISTS "${unitybuild_c}")
set(RunCMake_TEST_FAILED "Generated unity source files ${unitybuild_c} does not exist.")
return()
endif()
string(JOIN ".*" EXPECTED_UNITY_FILE_CONTENT
[[#include "([A-Za-z]:)?/[^"]*/Tests/RunCMake/UnityBuild/unitybuild_c_and_cxx_absolute_path-build/s1\.c"]]
[[#include "([A-Za-z]:)?/[^"]*/Tests/RunCMake/UnityBuild/unitybuild_c_and_cxx_absolute_path-build/s2\.c"]]
[[#include "([A-Za-z]:)?/[^"]*/Tests/RunCMake/UnityBuild/unitybuild_c_and_cxx_absolute_path-build/s3\.c"]]
)
file(STRINGS ${unitybuild_c} unitybuild_c_strings)
if(NOT unitybuild_c_strings MATCHES "${EXPECTED_UNITY_FILE_CONTENT}")
set(RunCMake_TEST_FAILED "Generated unity file ${unitybuild_c} doesn't contain absolute paths")
return()
endif()
set(unitybuild_cxx "${RunCMake_TEST_BINARY_DIR}/CMakeFiles/tgt.dir/Unity/unity_0_cxx.cxx")
if(NOT EXISTS "${unitybuild_cxx}")
set(RunCMake_TEST_FAILED "Generated unity source files ${unitybuild_cxx} does not exist.")
return()
endif()
string(JOIN ".*" EXPECTED_UNITY_FILE_CONTENT_CXX
[[#include "([A-Za-z]:)?/[^"]*/Tests/RunCMake/UnityBuild/unitybuild_c_and_cxx_absolute_path-build/s1\.cxx"]]
[[#include "([A-Za-z]:)?/[^"]*/Tests/RunCMake/UnityBuild/unitybuild_c_and_cxx_absolute_path-build/s2\.cxx"]]
[[#include "([A-Za-z]:)?/[^"]*/Tests/RunCMake/UnityBuild/unitybuild_c_and_cxx_absolute_path-build/s3\.cxx"]]
)
file(STRINGS ${unitybuild_cxx} unitybuild_cxx_strings)
if(NOT unitybuild_cxx_strings MATCHES "${EXPECTED_UNITY_FILE_CONTENT_CXX}")
set(RunCMake_TEST_FAILED "Generated unity file ${unitybuild_cxx} doesn't contain absolute paths")
return()
endif()

View File

@ -0,0 +1,18 @@
project(unitybuild_c_and_cxx_absolute_path C CXX)
set(srcs "")
foreach(s RANGE 1 3)
set(src_c "${CMAKE_CURRENT_BINARY_DIR}/s${s}.c")
file(WRITE "${src_c}" "int s${s}(void) { return 0; }\n")
set(src_cxx "${CMAKE_CURRENT_BINARY_DIR}/s${s}.cxx")
file(WRITE "${src_cxx}" "int s${s}(void) { return 0; }\n")
list(APPEND srcs "${src_c}")
list(APPEND srcs "${src_cxx}")
endforeach()
add_library(tgt SHARED ${srcs})
set_target_properties(tgt PROPERTIES UNITY_BUILD ON
UNITY_BUILD_RELOCATABLE FALSE)

View File

@ -0,0 +1,35 @@
set(unitybuild_c "${RunCMake_TEST_BINARY_DIR}/CMakeFiles/tgt.dir/Unity/unity_0_c.c")
if(NOT EXISTS "${unitybuild_c}")
set(RunCMake_TEST_FAILED "Generated unity source files ${unitybuild_c} does not exist.")
return()
endif()
string(JOIN ".*" EXPECTED_UNITY_FILE_CONTENT
[[#include "\.\./\.\./\.\./s1\.c"]]
[[#include "\.\./\.\./\.\./s2\.c"]]
[[#include "\.\./\.\./\.\./s3\.c"]]
)
file(STRINGS ${unitybuild_c} unitybuild_c_strings)
if(NOT unitybuild_c_strings MATCHES "${EXPECTED_UNITY_FILE_CONTENT}")
set(RunCMake_TEST_FAILED "Generated unity file ${unitybuild_c} doesn't contain relative paths")
return()
endif()
set(unitybuild_cxx "${RunCMake_TEST_BINARY_DIR}/CMakeFiles/tgt.dir/Unity/unity_0_cxx.cxx")
if(NOT EXISTS "${unitybuild_cxx}")
set(RunCMake_TEST_FAILED "Generated unity source files ${unitybuild_cxx} does not exist.")
return()
endif()
string(JOIN ".*" EXPECTED_UNITY_FILE_CONTENT_CXX
[[#include "\.\./\.\./\.\./s1\.cxx"]]
[[#include "\.\./\.\./\.\./s2\.cxx"]]
[[#include "\.\./\.\./\.\./s3\.cxx"]]
)
file(STRINGS ${unitybuild_cxx} unitybuild_cxx_strings)
if(NOT unitybuild_cxx_strings MATCHES "${EXPECTED_UNITY_FILE_CONTENT_CXX}")
set(RunCMake_TEST_FAILED "Generated unity file ${unitybuild_cxx} doesn't contain relative paths")
return()
endif()

View File

@ -0,0 +1,18 @@
project(unitybuild_c_and_cxx_relocatable_path C CXX)
set(srcs "")
foreach(s RANGE 1 3)
set(src_c "${CMAKE_CURRENT_BINARY_DIR}/s${s}.c")
file(WRITE "${src_c}" "int s${s}(void) { return 0; }\n")
set(src_cxx "${CMAKE_CURRENT_BINARY_DIR}/s${s}.cxx")
file(WRITE "${src_cxx}" "int s${s}(void) { return 0; }\n")
list(APPEND srcs "${src_c}")
list(APPEND srcs "${src_cxx}")
endforeach()
add_library(tgt SHARED ${srcs})
set_target_properties(tgt PROPERTIES UNITY_BUILD ON
UNITY_BUILD_RELOCATABLE TRUE)

View File

@ -0,0 +1,17 @@
set(unitybuild_c "${RunCMake_TEST_BINARY_DIR}/CMakeFiles/tgt.dir/Unity/unity_0_c.c")
if(NOT EXISTS "${unitybuild_c}")
set(RunCMake_TEST_FAILED "Generated unity source file ${unitybuild_c} does not exist.")
return()
endif()
string(JOIN ".*" EXPECTED_UNITY_FILE_CONTENT
[[#include "\.\./\.\./\.\./s1\.c"]]
[[#include "\.\./\.\./\.\./s2\.c"]]
[[#include "\.\./\.\./\.\./s3\.c"]]
)
file(STRINGS ${unitybuild_c} unitybuild_c_strings)
if(NOT unitybuild_c_strings MATCHES "${EXPECTED_UNITY_FILE_CONTENT}")
set(RunCMake_TEST_FAILED "Generated unity file ${unitybuild_c} doesn't contain relative paths")
return()
endif()

View File

@ -0,0 +1,14 @@
project(unitybuild_c_relocatable_path C)
set(srcs "")
foreach(s RANGE 1 3)
set(src "${CMAKE_CURRENT_BINARY_DIR}/s${s}.c")
file(WRITE "${src}" "int s${s}(void) { return 0; }\n")
list(APPEND srcs "${src}")
endforeach()
add_library(tgt SHARED ${srcs})
set_target_properties(tgt PROPERTIES UNITY_BUILD ON
UNITY_BUILD_RELOCATABLE TRUE
UNITY_BUILD_UNIQUE_ID "anon")

View File

@ -0,0 +1,17 @@
set(unitybuild_cxx "${RunCMake_TEST_BINARY_DIR}/CMakeFiles/tgt.dir/Unity/unity_0_cxx.cxx")
if(NOT EXISTS "${unitybuild_cxx}")
set(RunCMake_TEST_FAILED "Generated unity source file ${unitybuild_cxx} does not exist.")
return()
endif()
string(JOIN ".*" EXPECTED_UNITY_FILE_CONTENT
[[#include "([A-Za-z]:)?/[^"]*/Tests/RunCMake/UnityBuild/unitybuild_cxx_absolute_path-build/s1\.cxx"]]
[[#include "([A-Za-z]:)?/[^"]*/Tests/RunCMake/UnityBuild/unitybuild_cxx_absolute_path-build/s2\.cxx"]]
[[#include "([A-Za-z]:)?/[^"]*/Tests/RunCMake/UnityBuild/unitybuild_cxx_absolute_path-build/s3\.cxx"]]
)
file(STRINGS ${unitybuild_cxx} unitybuild_cxx_strings)
if(NOT unitybuild_cxx_strings MATCHES "${EXPECTED_UNITY_FILE_CONTENT}")
set(RunCMake_TEST_FAILED "Generated unity file ${unitybuild_cxx} doesn't contain absolute paths")
return()
endif()

View File

@ -0,0 +1,13 @@
project(unitybuild_cxx_absolute_path CXX)
set(srcs "")
foreach(s RANGE 1 3)
set(src "${CMAKE_CURRENT_BINARY_DIR}/s${s}.cxx")
file(WRITE "${src}" "int s${s}(void) { return 0; }\n")
list(APPEND srcs "${src}")
endforeach()
add_library(tgt SHARED ${srcs})
set_target_properties(tgt PROPERTIES UNITY_BUILD ON
UNITY_BUILD_RELOCATABLE FALSE)

View File

@ -0,0 +1,17 @@
set(unitybuild_cxx "${RunCMake_TEST_BINARY_DIR}/CMakeFiles/tgt.dir/Unity/unity_0_cxx.cxx")
if(NOT EXISTS "${unitybuild_cxx}")
set(RunCMake_TEST_FAILED "Generated unity source file ${unitybuild_cxx} does not exist.")
return()
endif()
string(JOIN ".*" EXPECTED_UNITY_FILE_CONTENT
[[#include "\.\./\.\./\.\./s1\.cxx"]]
[[#include "\.\./\.\./\.\./s2\.cxx"]]
[[#include "\.\./\.\./\.\./s3\.cxx"]]
)
file(STRINGS ${unitybuild_cxx} unitybuild_cxx_strings)
if(NOT unitybuild_cxx_strings MATCHES "${EXPECTED_UNITY_FILE_CONTENT}")
set(RunCMake_TEST_FAILED "Generated unity file ${unitybuild_cxx} doesn't contain relative paths")
return()
endif()

View File

@ -0,0 +1,13 @@
project(unitybuild_cxx_relocatable_path CXX)
set(srcs "")
foreach(s RANGE 1 3)
set(src "${CMAKE_CURRENT_BINARY_DIR}/s${s}.cxx")
file(WRITE "${src}" "int s${s}(void) { return 0; }\n")
list(APPEND srcs "${src}")
endforeach()
add_library(tgt SHARED ${srcs})
set_target_properties(tgt PROPERTIES UNITY_BUILD ON
UNITY_BUILD_RELOCATABLE TRUE)

View File

@ -0,0 +1,22 @@
set(unitybuild_c "${RunCMake_TEST_BINARY_DIR}/CMakeFiles/tgt.dir/Unity/unity_0_c.c")
if(NOT EXISTS "${unitybuild_c}")
set(RunCMake_TEST_FAILED "Generated unity source file ${unitybuild_c} does not exist.")
return()
endif()
string(JOIN ".*" EXPECTED_UNITY_FILE_CONTENT
[[#include "\.\./\.\./\.\./s1\.c"]]
[[#include "\.\./\.\./\.\./s2\.c"]]
[[#include "\.\./\.\./\.\./s3\.c"]]
[[#include "\.\./\.\./\.\./subFolder/sub1\.c"]]
[[#include "\.\./\.\./\.\./subFolder/sub2\.c"]]
[[#include "\.\./\.\./\.\./subFolder/sub3\.c"]]
[[#include "f\.c"]]
[[#include "relocatable/foo\.c"]]
)
file(STRINGS ${unitybuild_c} unitybuild_c_strings)
if(NOT unitybuild_c_strings MATCHES "${EXPECTED_UNITY_FILE_CONTENT}")
set(RunCMake_TEST_FAILED "Generated unity file ${unitybuild_c} doesn't contain relative paths")
return()
endif()

View File

@ -0,0 +1,23 @@
project(unitybuild_relocatable_locations C)
# Binary path relative source file
set(srcs "")
foreach(s RANGE 1 3)
set(src "${CMAKE_CURRENT_BINARY_DIR}/s${s}.c")
file(WRITE "${src}" "int s${s}(void) { return 0; }\n")
list(APPEND srcs "${src}")
endforeach()
foreach(s RANGE 1 3)
set(src "${CMAKE_CURRENT_BINARY_DIR}/subFolder/sub${s}.c")
file(WRITE "${src}" "int sub${s}(void) { return 0; }\n")
list(APPEND srcs "${src}")
endforeach()
# Source path relative source file
list(APPEND srcs "${CMAKE_SOURCE_DIR}/f.c")
list(APPEND srcs "${CMAKE_SOURCE_DIR}/relocatable/foo.c")
add_library(tgt SHARED ${srcs})
set_target_properties(tgt PROPERTIES UNITY_BUILD ON
UNITY_BUILD_RELOCATABLE TRUE)