Skip to content

Commit

Permalink
Add testonly flag to GN
Browse files Browse the repository at this point in the history
This also reworks visibility so both it and the testonly flag are checked when
a target is marked resolved, rather than when it is written out. The previous
code would not check visibility when doing non-"gen" commands like "check",
which is counterintuitive.

This change required OnResolved to be able to report a failure, which required
updating many callers. But it makes visibility mush easier to test, so I
added some additional visibility tests.

BUG=357779
R=hclam@chromium.org

Review URL: https://codereview.chromium.org/524623005

Cr-Commit-Position: refs/heads/master@{#292976}
  • Loading branch information
Brett Wilson committed Sep 2, 2014
1 parent d2787a3 commit 85423a0
Show file tree
Hide file tree
Showing 18 changed files with 348 additions and 92 deletions.
4 changes: 3 additions & 1 deletion tools/gn/builder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,9 @@ bool Builder::ResolveItem(BuilderRecord* record, Err* err) {
}

record->set_resolved(true);
record->item()->OnResolved();

if (!record->item()->OnResolved(err))
return false;
if (!resolved_callback_.is_null())
resolved_callback_.Run(record);

Expand Down
27 changes: 4 additions & 23 deletions tools/gn/command_gen.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,9 @@ const char kSwitchQuiet[] = "q";

const char kSwitchCheck[] = "check";

void BackgroundDoWrite(const Target* target,
const std::vector<const Item*>& deps_for_visibility) {
// Validate visibility.
Err err;
for (size_t i = 0; i < deps_for_visibility.size(); i++) {
if (!Visibility::CheckItemVisibility(target, deps_for_visibility[i],
&err)) {
g_scheduler->FailWithError(err);
break; // Don't return early since we need DecrementWorkCount below.
}
}

if (!err.has_error())
NinjaTargetWriter::RunAndWriteFile(target);
// Called on worker thread to write the ninja file.
void BackgroundDoWrite(const Target* target) {
NinjaTargetWriter::RunAndWriteFile(target);
g_scheduler->DecrementWorkCount();
}

Expand All @@ -51,16 +40,8 @@ void ItemResolvedCallback(base::subtle::Atomic32* write_counter,
const Item* item = record->item();
const Target* target = item->AsTarget();
if (target) {
// Collect all dependencies.
std::vector<const Item*> deps;
for (BuilderRecord::BuilderRecordSet::const_iterator iter =
record->all_deps().begin();
iter != record->all_deps().end();
++iter)
deps.push_back((*iter)->item());

g_scheduler->IncrementWorkCount();
g_scheduler->ScheduleWork(base::Bind(&BackgroundDoWrite, target, deps));
g_scheduler->ScheduleWork(base::Bind(&BackgroundDoWrite, target));
}
}

Expand Down
7 changes: 4 additions & 3 deletions tools/gn/config_values_extractors_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ struct IncludeWriter {

TEST(ConfigValuesExtractors, IncludeOrdering) {
TestWithScope setup;
Err err;

// Construct a chain of dependencies: target -> dep1 -> dep2
// Add representative values: cflags (opaque, always copied) and include_dirs
Expand Down Expand Up @@ -100,9 +101,9 @@ TEST(ConfigValuesExtractors, IncludeOrdering) {
SourceDir("//target/"));

// Mark targets resolved. This should push dependent configs.
dep2.OnResolved();
dep1.OnResolved();
target.OnResolved();
ASSERT_TRUE(dep2.OnResolved(&err));
ASSERT_TRUE(dep1.OnResolved(&err));
ASSERT_TRUE(target.OnResolved(&err));

// Verify cflags by serializing.
std::ostringstream flag_out;
Expand Down
4 changes: 4 additions & 0 deletions tools/gn/item.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,7 @@ std::string Item::GetItemTypeName() const {
NOTREACHED();
return "this thing that I have no idea what it is";
}

bool Item::OnResolved(Err* err) {
return true;
}
5 changes: 3 additions & 2 deletions tools/gn/item.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,9 @@ class Item {
std::string GetItemTypeName() const;

// Called when this item is resolved, meaning it and all of its dependents
// have no unresolved deps.
virtual void OnResolved() {}
// have no unresolved deps. Returns true on success. Sets the error and
// returns false on failure.
virtual bool OnResolved(Err* err);

private:
const Settings* settings_;
Expand Down
6 changes: 5 additions & 1 deletion tools/gn/label_ptr.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@ struct LabelPtrPair {

Label label;
const T* ptr; // May be NULL.
const ParseNode* origin; // May be NULL.

// The origin of this dependency. This will be null for internally generated
// dependencies. This happens when a group is automatically expanded and that
// group's members are added to the target that depends on that group.
const ParseNode* origin;
};

typedef LabelPtrPair<Config> LabelConfigPair;
Expand Down
25 changes: 17 additions & 8 deletions tools/gn/ninja_action_target_writer_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

TEST(NinjaActionTargetWriter, WriteOutputFilesForBuildLine) {
TestWithScope setup;
Err err;

setup.build_settings()->SetBuildDir(SourceDir("//out/Debug/"));

Target target(setup.settings(), Label(SourceDir("//foo/"), "bar"));
Expand All @@ -22,7 +24,7 @@ TEST(NinjaActionTargetWriter, WriteOutputFilesForBuildLine) {
"//out/Debug/gen/{{source_name_part}}.cc");

target.SetToolchain(setup.toolchain());
target.OnResolved();
ASSERT_TRUE(target.OnResolved(&err));

std::ostringstream out;
NinjaActionTargetWriter writer(&target, out);
Expand All @@ -37,6 +39,8 @@ TEST(NinjaActionTargetWriter, WriteOutputFilesForBuildLine) {
// Tests an action with no sources.
TEST(NinjaActionTargetWriter, ActionNoSources) {
TestWithScope setup;
Err err;

setup.build_settings()->SetBuildDir(SourceDir("//out/Debug/"));
Target target(setup.settings(), Label(SourceDir("//foo/"), "bar"));
target.set_output_type(Target::ACTION);
Expand All @@ -48,7 +52,7 @@ TEST(NinjaActionTargetWriter, ActionNoSources) {
SubstitutionList::MakeForTest("//out/Debug/foo.out");

target.SetToolchain(setup.toolchain());
target.OnResolved();
ASSERT_TRUE(target.OnResolved(&err));

setup.settings()->set_target_os(Settings::LINUX);
setup.build_settings()->set_python_path(base::FilePath(FILE_PATH_LITERAL(
Expand Down Expand Up @@ -76,6 +80,8 @@ TEST(NinjaActionTargetWriter, ActionNoSources) {
// both sources and inputs (ACTION_FOREACH treats the sources differently).
TEST(NinjaActionTargetWriter, ActionWithSources) {
TestWithScope setup;
Err err;

setup.build_settings()->SetBuildDir(SourceDir("//out/Debug/"));
Target target(setup.settings(), Label(SourceDir("//foo/"), "bar"));
target.set_output_type(Target::ACTION);
Expand All @@ -89,7 +95,7 @@ TEST(NinjaActionTargetWriter, ActionWithSources) {
SubstitutionList::MakeForTest("//out/Debug/foo.out");

target.SetToolchain(setup.toolchain());
target.OnResolved();
ASSERT_TRUE(target.OnResolved(&err));

// Posix.
{
Expand Down Expand Up @@ -146,6 +152,8 @@ TEST(NinjaActionTargetWriter, ActionWithSources) {

TEST(NinjaActionTargetWriter, ForEach) {
TestWithScope setup;
Err err;

setup.build_settings()->SetBuildDir(SourceDir("//out/Debug/"));

// Some dependencies that the action can depend on. Use actions for these
Expand All @@ -155,12 +163,12 @@ TEST(NinjaActionTargetWriter, ForEach) {
Target dep(setup.settings(), Label(SourceDir("//foo/"), "dep"));
dep.set_output_type(Target::ACTION);
dep.SetToolchain(setup.toolchain());
dep.OnResolved();
ASSERT_TRUE(dep.OnResolved(&err));

Target datadep(setup.settings(), Label(SourceDir("//foo/"), "datadep"));
datadep.set_output_type(Target::ACTION);
datadep.SetToolchain(setup.toolchain());
datadep.OnResolved();
ASSERT_TRUE(datadep.OnResolved(&err));

Target target(setup.settings(), Label(SourceDir("//foo/"), "bar"));
target.set_output_type(Target::ACTION_FOREACH);
Expand All @@ -182,7 +190,7 @@ TEST(NinjaActionTargetWriter, ForEach) {
target.inputs().push_back(SourceFile("//foo/included.txt"));

target.SetToolchain(setup.toolchain());
target.OnResolved();
ASSERT_TRUE(target.OnResolved(&err));

// Posix.
{
Expand Down Expand Up @@ -268,6 +276,8 @@ TEST(NinjaActionTargetWriter, ForEach) {

TEST(NinjaActionTargetWriter, ForEachWithDepfile) {
TestWithScope setup;
Err err;

setup.build_settings()->SetBuildDir(SourceDir("//out/Debug/"));
Target target(setup.settings(), Label(SourceDir("//foo/"), "bar"));
target.set_output_type(Target::ACTION_FOREACH);
Expand All @@ -278,10 +288,9 @@ TEST(NinjaActionTargetWriter, ForEachWithDepfile) {
target.action_values().set_script(SourceFile("//foo/script.py"));

target.SetToolchain(setup.toolchain());
target.OnResolved();
ASSERT_TRUE(target.OnResolved(&err));

SubstitutionPattern depfile;
Err err;
ASSERT_TRUE(
depfile.Parse("//out/Debug/gen/{{source_name_part}}.d", NULL, &err));
target.action_values().set_depfile(depfile);
Expand Down
18 changes: 12 additions & 6 deletions tools/gn/ninja_binary_target_writer_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

TEST(NinjaBinaryTargetWriter, SourceSet) {
TestWithScope setup;
Err err;

setup.build_settings()->SetBuildDir(SourceDir("//out/Debug/"));
setup.settings()->set_target_os(Settings::WIN);

Expand All @@ -23,7 +25,7 @@ TEST(NinjaBinaryTargetWriter, SourceSet) {
target.sources().push_back(SourceFile("//foo/input3.o"));
target.sources().push_back(SourceFile("//foo/input4.obj"));
target.SetToolchain(setup.toolchain());
target.OnResolved();
ASSERT_TRUE(target.OnResolved(&err));

// Source set itself.
{
Expand Down Expand Up @@ -57,7 +59,7 @@ TEST(NinjaBinaryTargetWriter, SourceSet) {
shlib_target.set_output_type(Target::SHARED_LIBRARY);
shlib_target.deps().push_back(LabelTargetPair(&target));
shlib_target.SetToolchain(setup.toolchain());
shlib_target.OnResolved();
ASSERT_TRUE(shlib_target.OnResolved(&err));

{
std::ostringstream out;
Expand Down Expand Up @@ -94,7 +96,7 @@ TEST(NinjaBinaryTargetWriter, SourceSet) {
stlib_target.set_output_type(Target::STATIC_LIBRARY);
stlib_target.deps().push_back(LabelTargetPair(&target));
stlib_target.SetToolchain(setup.toolchain());
stlib_target.OnResolved();
ASSERT_TRUE(stlib_target.OnResolved(&err));

{
std::ostringstream out;
Expand Down Expand Up @@ -129,14 +131,16 @@ TEST(NinjaBinaryTargetWriter, SourceSet) {
// are applied.
TEST(NinjaBinaryTargetWriter, ProductExtensionAndInputDeps) {
TestWithScope setup;
Err err;

setup.build_settings()->SetBuildDir(SourceDir("//out/Debug/"));
setup.settings()->set_target_os(Settings::LINUX);

// An action for our library to depend on.
Target action(setup.settings(), Label(SourceDir("//foo/"), "action"));
action.set_output_type(Target::ACTION_FOREACH);
action.SetToolchain(setup.toolchain());
action.OnResolved();
ASSERT_TRUE(action.OnResolved(&err));

// A shared library w/ the product_extension set to a custom value.
Target target(setup.settings(), Label(SourceDir("//foo/"), "shlib"));
Expand All @@ -146,7 +150,7 @@ TEST(NinjaBinaryTargetWriter, ProductExtensionAndInputDeps) {
target.sources().push_back(SourceFile("//foo/input2.cc"));
target.deps().push_back(LabelTargetPair(&action));
target.SetToolchain(setup.toolchain());
target.OnResolved();
ASSERT_TRUE(target.OnResolved(&err));

std::ostringstream out;
NinjaBinaryTargetWriter writer(&target, out);
Expand Down Expand Up @@ -185,6 +189,8 @@ TEST(NinjaBinaryTargetWriter, ProductExtensionAndInputDeps) {

TEST(NinjaBinaryTargetWriter, EmptyProductExtension) {
TestWithScope setup;
Err err;

setup.build_settings()->SetBuildDir(SourceDir("//out/Debug/"));
setup.settings()->set_target_os(Settings::LINUX);

Expand All @@ -197,7 +203,7 @@ TEST(NinjaBinaryTargetWriter, EmptyProductExtension) {
target.sources().push_back(SourceFile("//foo/input2.cc"));

target.SetToolchain(setup.toolchain());
target.OnResolved();
ASSERT_TRUE(target.OnResolved(&err));

std::ostringstream out;
NinjaBinaryTargetWriter writer(&target, out);
Expand Down
6 changes: 4 additions & 2 deletions tools/gn/ninja_copy_target_writer_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// Tests mutliple files with an output pattern and no toolchain dependency.
TEST(NinjaCopyTargetWriter, Run) {
TestWithScope setup;
Err err;

setup.settings()->set_target_os(Settings::LINUX);
setup.build_settings()->SetBuildDir(SourceDir("//out/Debug/"));
Expand All @@ -26,7 +27,7 @@ TEST(NinjaCopyTargetWriter, Run) {
SubstitutionList::MakeForTest("//out/Debug/{{source_name_part}}.out");

target.SetToolchain(setup.toolchain());
target.OnResolved();
ASSERT_TRUE(target.OnResolved(&err));

std::ostringstream out;
NinjaCopyTargetWriter writer(&target, out);
Expand All @@ -44,6 +45,7 @@ TEST(NinjaCopyTargetWriter, Run) {
// Tests a single file with no output pattern.
TEST(NinjaCopyTargetWriter, ToolchainDeps) {
TestWithScope setup;
Err err;

setup.settings()->set_target_os(Settings::LINUX);
setup.build_settings()->SetBuildDir(SourceDir("//out/Debug/"));
Expand All @@ -56,7 +58,7 @@ TEST(NinjaCopyTargetWriter, ToolchainDeps) {
SubstitutionList::MakeForTest("//out/Debug/output.out");

target.SetToolchain(setup.toolchain());
target.OnResolved();
ASSERT_TRUE(target.OnResolved(&err));

std::ostringstream out;
NinjaCopyTargetWriter writer(&target, out);
Expand Down
12 changes: 7 additions & 5 deletions tools/gn/ninja_target_writer_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class TestingNinjaTargetWriter : public NinjaTargetWriter {

TEST(NinjaTargetWriter, WriteInputDepsStampAndGetDep) {
TestWithScope setup;
Err err;

// Make a base target that's a hard dep (action).
Target base_target(setup.settings(), Label(SourceDir("//foo/"), "base"));
Expand All @@ -57,9 +58,9 @@ TEST(NinjaTargetWriter, WriteInputDepsStampAndGetDep) {
action.sources().push_back(SourceFile("//foo/action_source.txt"));
action.deps().push_back(LabelTargetPair(&target));

base_target.OnResolved();
target.OnResolved();
action.OnResolved();
ASSERT_TRUE(base_target.OnResolved(&err));
ASSERT_TRUE(target.OnResolved(&err));
ASSERT_TRUE(action.OnResolved(&err));

// Input deps for the base (should be only the script itself).
{
Expand Down Expand Up @@ -105,6 +106,7 @@ TEST(NinjaTargetWriter, WriteInputDepsStampAndGetDep) {
// Tests WriteInputDepsStampAndGetDep when toolchain deps are present.
TEST(NinjaTargetWriter, WriteInputDepsStampAndGetDepWithToolchainDeps) {
TestWithScope setup;
Err err;

// Toolchain dependency. Here we make a target in the same toolchain for
// simplicity, but in real life (using the Builder) this would be rejected
Expand All @@ -114,14 +116,14 @@ TEST(NinjaTargetWriter, WriteInputDepsStampAndGetDepWithToolchainDeps) {
Label(SourceDir("//foo/"), "setup"));
toolchain_dep_target.set_output_type(Target::ACTION);
toolchain_dep_target.SetToolchain(setup.toolchain());
toolchain_dep_target.OnResolved();
ASSERT_TRUE(toolchain_dep_target.OnResolved(&err));
setup.toolchain()->deps().push_back(LabelTargetPair(&toolchain_dep_target));

// Make a binary target
Target target(setup.settings(), Label(SourceDir("//foo/"), "target"));
target.set_output_type(Target::EXECUTABLE);
target.SetToolchain(setup.toolchain());
target.OnResolved();
ASSERT_TRUE(target.OnResolved(&err));

std::ostringstream stream;
TestingNinjaTargetWriter writer(&target, setup.toolchain(), stream);
Expand Down
Loading

0 comments on commit 85423a0

Please sign in to comment.