| 4969 | } |
| 4970 | |
| 4971 | cmSourceFile* AddCustomCommand(cmLocalGenerator& lg, cmCommandOrigin origin, |
| 4972 | std::unique_ptr<cmCustomCommand> cc, |
| 4973 | bool replace) |
| 4974 | { |
| 4975 | cmMakefile* mf = lg.GetMakefile(); |
| 4976 | auto const& lfbt = cc->GetBacktrace(); |
| 4977 | auto const& outputs = cc->GetOutputs(); |
| 4978 | auto const& byproducts = cc->GetByproducts(); |
| 4979 | auto const& commandLines = cc->GetCommandLines(); |
| 4980 | |
| 4981 | // Choose a source file on which to store the custom command. |
| 4982 | cmSourceFile* file = nullptr; |
| 4983 | if (!commandLines.empty() && cc->HasMainDependency()) { |
| 4984 | auto const& main_dependency = cc->GetMainDependency(); |
| 4985 | // The main dependency was specified. Use it unless a different |
| 4986 | // custom command already used it. |
| 4987 | file = mf->GetSource(main_dependency); |
| 4988 | if (file && file->GetCustomCommand() && !replace) { |
| 4989 | // The main dependency already has a custom command. |
| 4990 | if (commandLines == file->GetCustomCommand()->GetCommandLines()) { |
| 4991 | // The existing custom command is identical. Silently ignore |
| 4992 | // the duplicate. |
| 4993 | return file; |
| 4994 | } |
| 4995 | // The existing custom command is different. We need to |
| 4996 | // generate a rule file for this new command. |
| 4997 | file = nullptr; |
| 4998 | } else if (!file) { |
| 4999 | file = mf->CreateSource(main_dependency); |
| 5000 | } |
| 5001 | } |
| 5002 | |
| 5003 | // Generate a rule file if the main dependency is not available. |
| 5004 | if (!file) { |
| 5005 | cmGlobalGenerator* gg = lg.GetGlobalGenerator(); |
| 5006 | |
| 5007 | // Construct a rule file associated with the first output produced. |
| 5008 | std::string outName = gg->GenerateRuleFile( |
| 5009 | ComputeCustomCommandRuleFileName(lg, lfbt, outputs[0])); |
| 5010 | |
| 5011 | // Check if the rule file already exists. |
| 5012 | file = mf->GetSource(outName, cmSourceFileLocationKind::Known); |
| 5013 | if (file && file->GetCustomCommand() && !replace) { |
| 5014 | // The rule file already exists. |
| 5015 | if (commandLines != file->GetCustomCommand()->GetCommandLines()) { |
| 5016 | lg.GetCMakeInstance()->IssueMessage( |
| 5017 | MessageType::FATAL_ERROR, |
| 5018 | cmStrCat("Attempt to add a custom rule to output\n ", outName, |
| 5019 | "\nwhich already has a custom rule."), |
| 5020 | lfbt); |
| 5021 | } |
| 5022 | return file; |
| 5023 | } |
| 5024 | |
| 5025 | // Create a cmSourceFile for the rule file. |
| 5026 | if (!file) { |
| 5027 | file = mf->CreateSource(outName, true, cmSourceFileLocationKind::Known); |
| 5028 | } |
no test coverage detected
searching dependent graphs…