Commit 0e58870e authored by Yuke Liao's avatar Yuke Liao Committed by Commit Bot

[iOS] Refactoring XcodeWriter XCTest Files Search

The original XCTest files search code was in pretty bad shape, not only
ridiculously long, but also not extensible enough to accommodate
XCUITest.

This CL re-factors it to clean it up and makes it more general.

Bug: 709289
Change-Id: I14c0ad95af8365e3bbbf1db899560d5cac103b7a
Reviewed-on: https://chromium-review.googlesource.com/576990Reviewed-by: default avatarSylvain Defresne <sdefresne@chromium.org>
Commit-Queue: Yuke Liao <liaoyuke@chromium.org>
Cr-Commit-Position: refs/heads/master@{#487911}
parent a7558dd3
...@@ -154,27 +154,6 @@ const Target* FindXCTestApplicationTarget( ...@@ -154,27 +154,6 @@ const Target* FindXCTestApplicationTarget(
return nullptr; return nullptr;
} }
// TODO(crbug.com/741147) Remove this function and switch to use
// test_application_name once the bug is fixed and GN has rolled past it.
// Given XCTest module targets, find the corresponding application targets and
// the mappings between them.
void FindXCTestApplicationTargets(
const std::vector<const Target*>& xctest_module_targets,
const std::vector<const Target*>& targets,
std::vector<const Target*>* xctest_application_targets,
TargetToTarget* xctest_module_to_application_target) {
for (const Target* xctest_module_target : xctest_module_targets) {
xctest_application_targets->push_back(
FindXCTestApplicationTarget(xctest_module_target, targets));
xctest_module_to_application_target->insert(std::make_pair(
xctest_module_target, xctest_application_targets->back()));
}
DCHECK_EQ(xctest_module_targets.size(), xctest_application_targets->size());
DCHECK_EQ(xctest_module_targets.size(),
xctest_module_to_application_target->size());
}
// Adds |base_pbxtarget| as a dependency of |dependent_pbxtarget| in the // Adds |base_pbxtarget| as a dependency of |dependent_pbxtarget| in the
// generated Xcode project. // generated Xcode project.
void AddPBXTargetDependency(const PBXTarget* base_pbxtarget, void AddPBXTargetDependency(const PBXTarget* base_pbxtarget,
...@@ -212,8 +191,8 @@ void AddDependencyTargetForXCModuleTargets( ...@@ -212,8 +191,8 @@ void AddDependencyTargetForXCModuleTargets(
} }
// Searches the list of xctest files recursively under |target|. // Searches the list of xctest files recursively under |target|.
void SearchXCTestFiles(const Target* target, void SearchXCTestFilesForTarget(const Target* target,
TargetToFileList* xctest_files_per_target) { TargetToFileList* xctest_files_per_target) {
// Early return if already visited and processed. // Early return if already visited and processed.
if (xctest_files_per_target->find(target) != xctest_files_per_target->end()) if (xctest_files_per_target->find(target) != xctest_files_per_target->end())
return; return;
...@@ -227,7 +206,7 @@ void SearchXCTestFiles(const Target* target, ...@@ -227,7 +206,7 @@ void SearchXCTestFiles(const Target* target,
// Call recursively on public and private deps. // Call recursively on public and private deps.
for (const auto& t : target->public_deps()) { for (const auto& t : target->public_deps()) {
SearchXCTestFiles(t.ptr, xctest_files_per_target); SearchXCTestFilesForTarget(t.ptr, xctest_files_per_target);
const Target::FileList& deps_xctest_files = const Target::FileList& deps_xctest_files =
(*xctest_files_per_target)[t.ptr]; (*xctest_files_per_target)[t.ptr];
xctest_files.insert(xctest_files.end(), deps_xctest_files.begin(), xctest_files.insert(xctest_files.end(), deps_xctest_files.begin(),
...@@ -235,7 +214,7 @@ void SearchXCTestFiles(const Target* target, ...@@ -235,7 +214,7 @@ void SearchXCTestFiles(const Target* target,
} }
for (const auto& t : target->private_deps()) { for (const auto& t : target->private_deps()) {
SearchXCTestFiles(t.ptr, xctest_files_per_target); SearchXCTestFilesForTarget(t.ptr, xctest_files_per_target);
const Target::FileList& deps_xctest_files = const Target::FileList& deps_xctest_files =
(*xctest_files_per_target)[t.ptr]; (*xctest_files_per_target)[t.ptr];
xctest_files.insert(xctest_files.end(), deps_xctest_files.begin(), xctest_files.insert(xctest_files.end(), deps_xctest_files.begin(),
...@@ -250,22 +229,6 @@ void SearchXCTestFiles(const Target* target, ...@@ -250,22 +229,6 @@ void SearchXCTestFiles(const Target* target,
xctest_files_per_target->insert(std::make_pair(target, xctest_files)); xctest_files_per_target->insert(std::make_pair(target, xctest_files));
} }
// Finds the list of xctest files recursively under each of the application
// targets.
void FindXCTestFilesForApplicationTargets(
const std::vector<const Target*>& application_targets,
TargetToFileList* xctest_files_per_application_target) {
TargetToFileList xctest_files_per_target;
for (const Target* target : application_targets) {
DCHECK(IsApplicationTarget(target));
SearchXCTestFiles(target, &xctest_files_per_target);
xctest_files_per_application_target->insert(
std::make_pair(target, xctest_files_per_target[target]));
}
DCHECK_EQ(application_targets.size(),
xctest_files_per_application_target->size());
}
// Add all source files for indexing, both private and public. // Add all source files for indexing, both private and public.
void AddSourceFilesToProjectForIndexing( void AddSourceFilesToProjectForIndexing(
const std::vector<const Target*>& targets, const std::vector<const Target*>& targets,
...@@ -503,18 +466,6 @@ bool XcodeWriter::FilterTargets(const BuildSettings* build_settings, ...@@ -503,18 +466,6 @@ bool XcodeWriter::FilterTargets(const BuildSettings* build_settings,
return true; return true;
} }
// static
void XcodeWriter::FilterXCTestModuleTargets(
const std::vector<const Target*>& targets,
std::vector<const Target*>* xctest_module_targets) {
for (const Target* target : targets) {
if (!IsXCTestModuleTarget(target))
continue;
xctest_module_targets->push_back(target);
}
}
void XcodeWriter::CreateProductsProject( void XcodeWriter::CreateProductsProject(
const std::vector<const Target*>& targets, const std::vector<const Target*>& targets,
const std::vector<const Target*>& all_targets, const std::vector<const Target*>& all_targets,
...@@ -528,24 +479,6 @@ void XcodeWriter::CreateProductsProject( ...@@ -528,24 +479,6 @@ void XcodeWriter::CreateProductsProject(
std::unique_ptr<PBXProject> main_project( std::unique_ptr<PBXProject> main_project(
new PBXProject("products", config_name, source_path, attributes)); new PBXProject("products", config_name, source_path, attributes));
// Filter xctest module and application targets.
std::vector<const Target*> xctest_module_targets;
FilterXCTestModuleTargets(targets, &xctest_module_targets);
// There is a 1 on 1 mapping between |xctest_module_targets| and
// |xctest_application_targets|.
std::vector<const Target*> xctest_application_targets;
TargetToTarget xctest_module_to_application_target;
FindXCTestApplicationTargets(xctest_module_targets, targets,
&xctest_application_targets,
&xctest_module_to_application_target);
// Find list of xctest files recursively under them, and the files will be
// used for proper indexing and for discovery of tests function for XCTest.
TargetToFileList xctest_files_per_application_target;
FindXCTestFilesForApplicationTargets(xctest_application_targets,
&xctest_files_per_application_target);
std::vector<const Target*> bundle_targets; std::vector<const Target*> bundle_targets;
TargetToPBXTarget bundle_target_to_pbxtarget; TargetToPBXTarget bundle_target_to_pbxtarget;
...@@ -557,6 +490,11 @@ void XcodeWriter::CreateProductsProject( ...@@ -557,6 +490,11 @@ void XcodeWriter::CreateProductsProject(
main_project->AddAggregateTarget( main_project->AddAggregateTarget(
"All", GetBuildScript(root_target, ninja_extra_args, env.get())); "All", GetBuildScript(root_target, ninja_extra_args, env.get()));
// Needs to search for xctest files under the application targets, and this
// variable is used to store the results of visited targets, thus making the
// search more efficient.
TargetToFileList xctest_files_per_target;
for (const Target* target : targets) { for (const Target* target : targets) {
switch (target->output_type()) { switch (target->output_type()) {
case Target::EXECUTABLE: case Target::EXECUTABLE:
...@@ -621,12 +559,17 @@ void XcodeWriter::CreateProductsProject( ...@@ -621,12 +559,17 @@ void XcodeWriter::CreateProductsProject(
if (!IsXCTestModuleTarget(target)) if (!IsXCTestModuleTarget(target))
continue; continue;
// For XCTest, test files are compiled into the application bundle.
const Target* test_application_target =
FindXCTestApplicationTarget(target, targets);
SearchXCTestFilesForTarget(test_application_target,
&xctest_files_per_target);
const Target::FileList& xctest_file_list =
xctest_files_per_target[test_application_target];
// Add xctest files to the "Compiler Sources" of corresponding xctest // Add xctest files to the "Compiler Sources" of corresponding xctest
// native targets for proper indexing and for discovery of tests // native targets for proper indexing and for discovery of tests
// function for XCTest. // function for XCTest.
const Target::FileList& xctest_file_list =
xctest_files_per_application_target
[xctest_module_to_application_target[target]];
AddXCTestFilesToXCTestModuleTarget(xctest_file_list, native_target, AddXCTestFilesToXCTestModuleTarget(xctest_file_list, native_target,
main_project.get(), source_dir, main_project.get(), source_dir,
build_settings); build_settings);
......
...@@ -59,11 +59,6 @@ class XcodeWriter { ...@@ -59,11 +59,6 @@ class XcodeWriter {
std::vector<const Target*>* targets, std::vector<const Target*>* targets,
Err* err); Err* err);
// Filters list of targets to only return ones that are xctest module bundles.
static void FilterXCTestModuleTargets(
const std::vector<const Target*>& targets,
std::vector<const Target*>* xctest_module_targets);
// Generate the "products.xcodeproj" project that reference all products // Generate the "products.xcodeproj" project that reference all products
// (i.e. targets that have a build artefact usable from Xcode, mostly // (i.e. targets that have a build artefact usable from Xcode, mostly
// application bundles). // application bundles).
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment