| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "tools/gn/substitution_writer.h" | 5 #include "tools/gn/substitution_writer.h" |
| 6 | 6 |
| 7 #include "tools/gn/build_settings.h" | 7 #include "tools/gn/build_settings.h" |
| 8 #include "tools/gn/escape.h" | 8 #include "tools/gn/escape.h" |
| 9 #include "tools/gn/filesystem_utils.h" | 9 #include "tools/gn/filesystem_utils.h" |
| 10 #include "tools/gn/output_file.h" | 10 #include "tools/gn/output_file.h" |
| (...skipping 16 matching lines...) Expand all Loading... |
| 27 else | 27 else |
| 28 dest->assign(dir); | 28 dest->assign(dir); |
| 29 | 29 |
| 30 if (dest->empty()) | 30 if (dest->empty()) |
| 31 dest->push_back('.'); | 31 dest->push_back('.'); |
| 32 } | 32 } |
| 33 | 33 |
| 34 } // namespace | 34 } // namespace |
| 35 | 35 |
| 36 const char kSourceExpansion_Help[] = | 36 const char kSourceExpansion_Help[] = |
| 37 "How Source Expansion Works\n" | 37 R"(How Source Expansion Works |
| 38 "\n" | 38 |
| 39 " Source expansion is used for the action_foreach and copy target types\n" | 39 Source expansion is used for the action_foreach and copy target types to map |
| 40 " to map source file names to output file names or arguments.\n" | 40 source file names to output file names or arguments. |
| 41 "\n" | 41 |
| 42 " To perform source expansion in the outputs, GN maps every entry in the\n" | 42 To perform source expansion in the outputs, GN maps every entry in the |
| 43 " sources to every entry in the outputs list, producing the cross\n" | 43 sources to every entry in the outputs list, producing the cross product of |
| 44 " product of all combinations, expanding placeholders (see below).\n" | 44 all combinations, expanding placeholders (see below). |
| 45 "\n" | 45 |
| 46 " Source expansion in the args works similarly, but performing the\n" | 46 Source expansion in the args works similarly, but performing the placeholder |
| 47 " placeholder substitution produces a different set of arguments for\n" | 47 substitution produces a different set of arguments for each invocation of the |
| 48 " each invocation of the script.\n" | 48 script. |
| 49 "\n" | 49 |
| 50 " If no placeholders are found, the outputs or args list will be treated\n" | 50 If no placeholders are found, the outputs or args list will be treated as a |
| 51 " as a static list of literal file names that do not depend on the\n" | 51 static list of literal file names that do not depend on the sources. |
| 52 " sources.\n" | 52 |
| 53 "\n" | 53 See "gn help copy" and "gn help action_foreach" for more on how this is |
| 54 " See \"gn help copy\" and \"gn help action_foreach\" for more on how\n" | 54 applied. |
| 55 " this is applied.\n" | 55 |
| 56 "\n" | 56 Placeholders |
| 57 "Placeholders\n" | 57 |
| 58 "\n" | 58 This section discusses only placeholders for actions. There are other |
| 59 " This section discusses only placeholders for actions. There are other\n" | 59 placeholders used in the definition of tools. See "gn help tool" for those. |
| 60 " placeholders used in the definition of tools. See \"gn help tool\" for\n" | 60 |
| 61 " those.\n" | 61 {{source}} |
| 62 "\n" | 62 The name of the source file including directory (*). This will generally |
| 63 " {{source}}\n" | 63 be used for specifying inputs to a script in the "args" variable. |
| 64 " The name of the source file including directory (*). This will\n" | 64 "//foo/bar/baz.txt" => "../../foo/bar/baz.txt" |
| 65 " generally be used for specifying inputs to a script in the\n" | 65 |
| 66 " \"args\" variable.\n" | 66 {{source_file_part}} |
| 67 " \"//foo/bar/baz.txt\" => \"../../foo/bar/baz.txt\"\n" | 67 The file part of the source including the extension. |
| 68 "\n" | 68 "//foo/bar/baz.txt" => "baz.txt" |
| 69 " {{source_file_part}}\n" | 69 |
| 70 " The file part of the source including the extension.\n" | 70 {{source_name_part}} |
| 71 " \"//foo/bar/baz.txt\" => \"baz.txt\"\n" | 71 The filename part of the source file with no directory or extension. This |
| 72 "\n" | 72 will generally be used for specifying a transformation from a source file |
| 73 " {{source_name_part}}\n" | 73 to a destination file with the same name but different extension. |
| 74 " The filename part of the source file with no directory or\n" | 74 "//foo/bar/baz.txt" => "baz" |
| 75 " extension. This will generally be used for specifying a\n" | 75 |
| 76 " transformation from a source file to a destination file with the\n" | 76 {{source_dir}} |
| 77 " same name but different extension.\n" | 77 The directory (*) containing the source file with no trailing slash. |
| 78 " \"//foo/bar/baz.txt\" => \"baz\"\n" | 78 "//foo/bar/baz.txt" => "../../foo/bar" |
| 79 "\n" | 79 |
| 80 " {{source_dir}}\n" | 80 {{source_root_relative_dir}} |
| 81 " The directory (*) containing the source file with no\n" | 81 The path to the source file's directory relative to the source root, with |
| 82 " trailing slash.\n" | 82 no leading "//" or trailing slashes. If the path is system-absolute, |
| 83 " \"//foo/bar/baz.txt\" => \"../../foo/bar\"\n" | 83 (beginning in a single slash) this will just return the path with no |
| 84 "\n" | 84 trailing slash. This value will always be the same, regardless of whether |
| 85 " {{source_root_relative_dir}}\n" | 85 it appears in the "outputs" or "args" section. |
| 86 " The path to the source file's directory relative to the source\n" | 86 "//foo/bar/baz.txt" => "foo/bar" |
| 87 " root, with no leading \"//\" or trailing slashes. If the path is\n" | 87 |
| 88 " system-absolute, (beginning in a single slash) this will just\n" | 88 {{source_gen_dir}} |
| 89 " return the path with no trailing slash. This value will always\n" | 89 The generated file directory (*) corresponding to the source file's path. |
| 90 " be the same, regardless of whether it appears in the \"outputs\"\n" | 90 This will be different than the target's generated file directory if the |
| 91 " or \"args\" section.\n" | 91 source file is in a different directory than the BUILD.gn file. |
| 92 " \"//foo/bar/baz.txt\" => \"foo/bar\"\n" | 92 "//foo/bar/baz.txt" => "gen/foo/bar" |
| 93 "\n" | 93 |
| 94 " {{source_gen_dir}}\n" | 94 {{source_out_dir}} |
| 95 " The generated file directory (*) corresponding to the source\n" | 95 The object file directory (*) corresponding to the source file's path, |
| 96 " file's path. This will be different than the target's generated\n" | 96 relative to the build directory. this us be different than the target's |
| 97 " file directory if the source file is in a different directory\n" | 97 out directory if the source file is in a different directory than the |
| 98 " than the BUILD.gn file.\n" | 98 build.gn file. |
| 99 " \"//foo/bar/baz.txt\" => \"gen/foo/bar\"\n" | 99 "//foo/bar/baz.txt" => "obj/foo/bar" |
| 100 "\n" | 100 |
| 101 " {{source_out_dir}}\n" | 101 (*) Note on directories |
| 102 " The object file directory (*) corresponding to the source file's\n" | 102 |
| 103 " path, relative to the build directory. this us be different than\n" | 103 Paths containing directories (except the source_root_relative_dir) will be |
| 104 " the target's out directory if the source file is in a different\n" | 104 different depending on what context the expansion is evaluated in. Generally |
| 105 " directory than the build.gn file.\n" | 105 it should "just work" but it means you can't concatenate strings containing |
| 106 " \"//foo/bar/baz.txt\" => \"obj/foo/bar\"\n" | 106 these values with reasonable results. |
| 107 "\n" | 107 |
| 108 "(*) Note on directories\n" | 108 Details: source expansions can be used in the "outputs" variable, the "args" |
| 109 "\n" | 109 variable, and in calls to "process_file_template". The "args" are passed to a |
| 110 " Paths containing directories (except the source_root_relative_dir)\n" | 110 script which is run from the build directory, so these directories will |
| 111 " will be different depending on what context the expansion is evaluated\n" | 111 relative to the build directory for the script to find. In the other cases, |
| 112 " in. Generally it should \"just work\" but it means you can't\n" | 112 the directories will be source- absolute (begin with a "//") because the |
| 113 " concatenate strings containing these values with reasonable results.\n" | 113 results of those expansions will be handled by GN internally. |
| 114 "\n" | 114 |
| 115 " Details: source expansions can be used in the \"outputs\" variable,\n" | 115 Examples |
| 116 " the \"args\" variable, and in calls to \"process_file_template\". The\n" | 116 |
| 117 " \"args\" are passed to a script which is run from the build directory,\n" | 117 Non-varying outputs: |
| 118 " so these directories will relative to the build directory for the\n" | 118 action("hardcoded_outputs") { |
| 119 " script to find. In the other cases, the directories will be source-\n" | 119 sources = [ "input1.idl", "input2.idl" ] |
| 120 " absolute (begin with a \"//\") because the results of those expansions\n" | 120 outputs = [ "$target_out_dir/output1.dat", |
| 121 " will be handled by GN internally.\n" | 121 "$target_out_dir/output2.dat" ] |
| 122 "\n" | 122 } |
| 123 "Examples\n" | 123 The outputs in this case will be the two literal files given. |
| 124 "\n" | 124 |
| 125 " Non-varying outputs:\n" | 125 Varying outputs: |
| 126 " action(\"hardcoded_outputs\") {\n" | 126 action_foreach("varying_outputs") { |
| 127 " sources = [ \"input1.idl\", \"input2.idl\" ]\n" | 127 sources = [ "input1.idl", "input2.idl" ] |
| 128 " outputs = [ \"$target_out_dir/output1.dat\",\n" | 128 outputs = [ "{{source_gen_dir}}/{{source_name_part}}.h", |
| 129 " \"$target_out_dir/output2.dat\" ]\n" | 129 "{{source_gen_dir}}/{{source_name_part}}.cc" ] |
| 130 " }\n" | 130 } |
| 131 " The outputs in this case will be the two literal files given.\n" | 131 Performing source expansion will result in the following output names: |
| 132 "\n" | 132 //out/Debug/obj/mydirectory/input1.h |
| 133 " Varying outputs:\n" | 133 //out/Debug/obj/mydirectory/input1.cc |
| 134 " action_foreach(\"varying_outputs\") {\n" | 134 //out/Debug/obj/mydirectory/input2.h |
| 135 " sources = [ \"input1.idl\", \"input2.idl\" ]\n" | 135 //out/Debug/obj/mydirectory/input2.cc |
| 136 " outputs = [ \"{{source_gen_dir}}/{{source_name_part}}.h\",\n" | 136 )"; |
| 137 " \"{{source_gen_dir}}/{{source_name_part}}.cc\" ]\n" | |
| 138 " }\n" | |
| 139 " Performing source expansion will result in the following output names:\n" | |
| 140 " //out/Debug/obj/mydirectory/input1.h\n" | |
| 141 " //out/Debug/obj/mydirectory/input1.cc\n" | |
| 142 " //out/Debug/obj/mydirectory/input2.h\n" | |
| 143 " //out/Debug/obj/mydirectory/input2.cc\n"; | |
| 144 | 137 |
| 145 // static | 138 // static |
| 146 void SubstitutionWriter::WriteWithNinjaVariables( | 139 void SubstitutionWriter::WriteWithNinjaVariables( |
| 147 const SubstitutionPattern& pattern, | 140 const SubstitutionPattern& pattern, |
| 148 const EscapeOptions& escape_options, | 141 const EscapeOptions& escape_options, |
| 149 std::ostream& out) { | 142 std::ostream& out) { |
| 150 // The result needs to be quoted as if it was one string, but the $ for | 143 // The result needs to be quoted as if it was one string, but the $ for |
| 151 // the inserted Ninja variables can't be escaped. So write to a buffer with | 144 // the inserted Ninja variables can't be escaped. So write to a buffer with |
| 152 // no quoting, and then quote the whole thing if necessary. | 145 // no quoting, and then quote the whole thing if necessary. |
| 153 EscapeOptions no_quoting(escape_options); | 146 EscapeOptions no_quoting(escape_options); |
| (...skipping 422 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 576 return tool->default_output_extension(); | 569 return tool->default_output_extension(); |
| 577 if (target->output_extension().empty()) | 570 if (target->output_extension().empty()) |
| 578 return std::string(); // Explicitly set to no extension. | 571 return std::string(); // Explicitly set to no extension. |
| 579 return std::string(".") + target->output_extension(); | 572 return std::string(".") + target->output_extension(); |
| 580 | 573 |
| 581 default: | 574 default: |
| 582 NOTREACHED(); | 575 NOTREACHED(); |
| 583 return std::string(); | 576 return std::string(); |
| 584 } | 577 } |
| 585 } | 578 } |
| OLD | NEW |