Skip to content

Commit

Permalink
Make rule available in output artifact and prepare function.
Browse files Browse the repository at this point in the history
  • Loading branch information
Dennis Tomas committed Aug 5, 2016
1 parent 2c916be commit 716cc90
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 1 deletion.
2 changes: 1 addition & 1 deletion doc/reference/items/rule.qdoc
Expand Up @@ -241,7 +241,7 @@
\li undefined
\li Script that prepares the commands to transform the inputs to outputs.
The code in this script is treated as a function with the signature
\c{function(project, product, inputs, outputs, input, output)}.
\c{function(project, product, rule, inputs, outputs, input, output)}.
The argument \c{input} is \c{undefined} if there's more than one input artifact for this
rule. Similarly, \c{output} is only defined if there's exactly one output artifact.
\row
Expand Down
9 changes: 9 additions & 0 deletions src/lib/corelib/buildgraph/buildgraph.cpp
Expand Up @@ -188,6 +188,15 @@ void setupScriptEngineForFile(ScriptEngine *engine, const ResolvedFileContextCon
JsExtensions::setupExtensions(fileContext->jsExtensions(), targetObject);
}

void setupScriptEngineForRule(ScriptEngine *engine, const RuleConstPtr &rule, QScriptValue targetObject) {
QScriptValue ruleScriptValue = engine->newObject();
const QVariantMap &propMap = rule->properties;
for (QVariantMap::ConstIterator it = propMap.constBegin(); it != propMap.constEnd(); ++it) {
ruleScriptValue.setProperty(it.key(), engine->toScriptValue(it.value()));
}
targetObject.setProperty(QLatin1String("rule"), ruleScriptValue);
}

void setupScriptEngineForProduct(ScriptEngine *engine, const ResolvedProductConstPtr &product,
const ResolvedModuleConstPtr &module, QScriptValue targetObject,
PrepareScriptObserver *observer)
Expand Down
2 changes: 2 additions & 0 deletions src/lib/corelib/buildgraph/buildgraph.h
Expand Up @@ -76,6 +76,8 @@ void setupScriptEngineForFile(ScriptEngine *engine, const ResolvedFileContextCon
void setupScriptEngineForProduct(ScriptEngine *engine, const ResolvedProductConstPtr &product,
const ResolvedModuleConstPtr &module, QScriptValue targetObject,
PrepareScriptObserver *observer = 0);
void setupScriptEngineForRule(ScriptEngine *engine, const RuleConstPtr &rule,
QScriptValue targetObject);
QString relativeArtifactFileName(const Artifact *artifact); // Debugging helpers

void doSanityChecks(const ResolvedProjectPtr &project, const Logger &logger);
Expand Down
1 change: 1 addition & 0 deletions src/lib/corelib/buildgraph/jscommandexecutor.cpp
Expand Up @@ -105,6 +105,7 @@ class JsCommandExecutorThreadObject : public QObject
setupScriptEngineForFile(scriptEngine, transformer->rule->prepareScript->fileContext, scope);
setupScriptEngineForProduct(scriptEngine, transformer->product(), transformer->rule->module, scope,
&observer);
setupScriptEngineForRule(scriptEngine, transformer->rule, scope);
transformer->setupInputs(scope);
transformer->setupOutputs(scriptEngine, scope);

Expand Down
2 changes: 2 additions & 0 deletions src/lib/corelib/buildgraph/rulesapplicator.cpp
Expand Up @@ -91,6 +91,7 @@ void RulesApplicator::applyRule(const RuleConstPtr &rule, const ArtifactSet &inp
PrepareScriptObserver observer(engine());
setupScriptEngineForFile(engine(), m_rule->prepareScript->fileContext, scope());
setupScriptEngineForProduct(engine(), m_product, m_rule->module, prepareScriptContext, &observer);
setupScriptEngineForRule(engine(), m_rule, prepareScriptContext);

if (m_rule->multiplex) { // apply the rule once for a set of inputs
doApply(inputArtifacts, prepareScriptContext);
Expand Down Expand Up @@ -170,6 +171,7 @@ void RulesApplicator::doApply(const ArtifactSet &inputArtifacts, QScriptValue &p
copyProperty(QLatin1String("input"), prepareScriptContext, scope());
copyProperty(QLatin1String("product"), prepareScriptContext, scope());
copyProperty(QLatin1String("project"), prepareScriptContext, scope());
copyProperty(QLatin1String("rule"), prepareScriptContext, scope());
if (m_rule->isDynamic()) {
outputArtifacts = runOutputArtifactsScript(inputArtifacts,
ScriptEngine::argumentList(m_rule->outputArtifactsScript->argumentNames,
Expand Down
2 changes: 2 additions & 0 deletions src/lib/corelib/language/language.cpp
Expand Up @@ -402,6 +402,7 @@ void Rule::load(PersistentPool &pool)
prepareScript = pool.idLoadS<ScriptFunction>();
outputArtifactsScript = pool.idLoadS<ScriptFunction>();
module = pool.idLoadS<ResolvedModule>();
properties = pool.loadVariantMap();
pool.stream()
>> inputs
>> outputFileTags
Expand All @@ -421,6 +422,7 @@ void Rule::store(PersistentPool &pool) const
pool.store(prepareScript);
pool.store(outputArtifactsScript);
pool.store(module);
pool.store(properties);
pool.stream()
<< inputs
<< outputFileTags
Expand Down
1 change: 1 addition & 0 deletions src/lib/corelib/language/language.h
Expand Up @@ -314,6 +314,7 @@ class Rule : public PersistentObject
bool multiplex;
QList<RuleArtifactPtr> artifacts; // unused, if outputFileTags/outputArtifactsScript is non-empty
bool alwaysRun;
QVariantMap properties;

// members that we don't need to save
int ruleGraphId;
Expand Down
1 change: 1 addition & 0 deletions src/lib/corelib/language/projectresolver.cpp
Expand Up @@ -728,6 +728,7 @@ void ProjectResolver::resolveRule(Item *item, ProjectContext *projectContext)
m_logger.printWarning(error);
return;
}
rule->properties = evaluateProperties(item);
if (m_productContext)
m_productContext->product->rules += rule;
else
Expand Down

0 comments on commit 716cc90

Please sign in to comment.