OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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/functions.h" | 5 #include "tools/gn/functions.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <iostream> | 8 #include <iostream> |
9 #include <utility> | 9 #include <utility> |
10 | 10 |
11 #include "base/environment.h" | 11 #include "base/environment.h" |
12 #include "base/strings/string_util.h" | 12 #include "base/strings/string_util.h" |
13 #include "tools/gn/config.h" | 13 #include "tools/gn/config.h" |
14 #include "tools/gn/config_values_generator.h" | 14 #include "tools/gn/config_values_generator.h" |
15 #include "tools/gn/err.h" | 15 #include "tools/gn/err.h" |
16 #include "tools/gn/input_file.h" | 16 #include "tools/gn/input_file.h" |
| 17 #include "tools/gn/parse_node_value_adapter.h" |
17 #include "tools/gn/parse_tree.h" | 18 #include "tools/gn/parse_tree.h" |
18 #include "tools/gn/pool.h" | 19 #include "tools/gn/pool.h" |
19 #include "tools/gn/scheduler.h" | 20 #include "tools/gn/scheduler.h" |
20 #include "tools/gn/scope.h" | 21 #include "tools/gn/scope.h" |
21 #include "tools/gn/settings.h" | 22 #include "tools/gn/settings.h" |
22 #include "tools/gn/template.h" | 23 #include "tools/gn/template.h" |
23 #include "tools/gn/token.h" | 24 #include "tools/gn/token.h" |
24 #include "tools/gn/value.h" | 25 #include "tools/gn/value.h" |
25 #include "tools/gn/value_extractors.h" | 26 #include "tools/gn/value_extractors.h" |
26 #include "tools/gn/variables.h" | 27 #include "tools/gn/variables.h" |
(...skipping 777 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
804 const BuildSettings::PrintCallback& cb = | 805 const BuildSettings::PrintCallback& cb = |
805 scope->settings()->build_settings()->print_callback(); | 806 scope->settings()->build_settings()->print_callback(); |
806 if (cb.is_null()) | 807 if (cb.is_null()) |
807 printf("%s", output.c_str()); | 808 printf("%s", output.c_str()); |
808 else | 809 else |
809 cb.Run(output); | 810 cb.Run(output); |
810 | 811 |
811 return Value(); | 812 return Value(); |
812 } | 813 } |
813 | 814 |
| 815 // split_list ------------------------------------------------------------------ |
| 816 |
| 817 const char kSplitList[] = "split_list"; |
| 818 const char kSplitList_HelpShort[] = |
| 819 "split_list: Splits a list into N different sub-lists."; |
| 820 const char kSplitList_Help[] = |
| 821 "split_list: Splits a list into N different sub-lists.\n" |
| 822 "\n" |
| 823 " result = split_list(input, n)\n" |
| 824 "\n" |
| 825 " Given a list and a number N, splits the list into N sub-lists of\n" |
| 826 " approximately equal size. The return value is a list of the sub-lists.\n" |
| 827 " The result will always be a list of size N. If N is greater than the\n" |
| 828 " number of elements in the input, it will be padded with empty lists.\n" |
| 829 "\n" |
| 830 " The expected use is to divide source files into smaller uniform\n" |
| 831 " chunks.\n" |
| 832 "\n" |
| 833 "Example\n" |
| 834 "\n" |
| 835 " The code:\n" |
| 836 " mylist = [1, 2, 3, 4, 5, 6]\n" |
| 837 " print(split_list(mylist, 3))\n" |
| 838 "\n" |
| 839 " Will print:\n" |
| 840 " [[1, 2], [3, 4], [5, 6]\n"; |
| 841 Value RunSplitList(Scope* scope, |
| 842 const FunctionCallNode* function, |
| 843 const ListNode* args_list, |
| 844 Err* err) { |
| 845 const auto& args_vector = args_list->contents(); |
| 846 if (args_vector.size() != 2) { |
| 847 *err = Err(function, "Wrong number of arguments to split_list().", |
| 848 "Expecting exactly two."); |
| 849 return Value(); |
| 850 } |
| 851 |
| 852 ParseNodeValueAdapter list_adapter; |
| 853 if (!list_adapter.InitForType(scope, args_vector[0].get(), Value::LIST, err)) |
| 854 return Value(); |
| 855 const std::vector<Value>& input = list_adapter.get().list_value(); |
| 856 |
| 857 ParseNodeValueAdapter count_adapter; |
| 858 if (!count_adapter.InitForType(scope, args_vector[1].get(), Value::INTEGER, |
| 859 err)) |
| 860 return Value(); |
| 861 int64_t count = count_adapter.get().int_value(); |
| 862 if (count <= 0) { |
| 863 *err = Err(function, "Requested result size is not positive."); |
| 864 return Value(); |
| 865 } |
| 866 |
| 867 Value result(function, Value::LIST); |
| 868 result.list_value().resize(count); |
| 869 |
| 870 // Every result list gets at least this many items in it. |
| 871 int64_t min_items_per_list = static_cast<int64_t>(input.size()) / count; |
| 872 |
| 873 // This many result lists get an extra item which is the remainder from above. |
| 874 int64_t extra_items = static_cast<int64_t>(input.size()) % count; |
| 875 |
| 876 // Allocate all lists that have a remainder assigned to them (max items). |
| 877 int64_t max_items_per_list = min_items_per_list + 1; |
| 878 auto last_item_end = input.begin(); |
| 879 for (int64_t i = 0; i < extra_items; i++) { |
| 880 result.list_value()[i] = Value(function, Value::LIST); |
| 881 |
| 882 auto begin_add = last_item_end; |
| 883 last_item_end += max_items_per_list; |
| 884 result.list_value()[i].list_value().assign(begin_add, last_item_end); |
| 885 } |
| 886 |
| 887 // Allocate all smaller items that don't have a remainder. |
| 888 for (int64_t i = extra_items; i < count; i++) { |
| 889 result.list_value()[i] = Value(function, Value::LIST); |
| 890 |
| 891 auto begin_add = last_item_end; |
| 892 last_item_end += min_items_per_list; |
| 893 result.list_value()[i].list_value().assign(begin_add, last_item_end); |
| 894 } |
| 895 |
| 896 return result; |
| 897 } |
| 898 |
814 // ----------------------------------------------------------------------------- | 899 // ----------------------------------------------------------------------------- |
815 | 900 |
816 FunctionInfo::FunctionInfo() | 901 FunctionInfo::FunctionInfo() |
817 : self_evaluating_args_runner(nullptr), | 902 : self_evaluating_args_runner(nullptr), |
818 generic_block_runner(nullptr), | 903 generic_block_runner(nullptr), |
819 executed_block_runner(nullptr), | 904 executed_block_runner(nullptr), |
820 no_block_runner(nullptr), | 905 no_block_runner(nullptr), |
821 help_short(nullptr), | 906 help_short(nullptr), |
822 help(nullptr), | 907 help(nullptr), |
823 is_target(false) { | 908 is_target(false) { |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
916 INSERT_FUNCTION(GetTargetOutputs, false) | 1001 INSERT_FUNCTION(GetTargetOutputs, false) |
917 INSERT_FUNCTION(Import, false) | 1002 INSERT_FUNCTION(Import, false) |
918 INSERT_FUNCTION(Pool, false) | 1003 INSERT_FUNCTION(Pool, false) |
919 INSERT_FUNCTION(Print, false) | 1004 INSERT_FUNCTION(Print, false) |
920 INSERT_FUNCTION(ProcessFileTemplate, false) | 1005 INSERT_FUNCTION(ProcessFileTemplate, false) |
921 INSERT_FUNCTION(ReadFile, false) | 1006 INSERT_FUNCTION(ReadFile, false) |
922 INSERT_FUNCTION(RebasePath, false) | 1007 INSERT_FUNCTION(RebasePath, false) |
923 INSERT_FUNCTION(SetDefaults, false) | 1008 INSERT_FUNCTION(SetDefaults, false) |
924 INSERT_FUNCTION(SetDefaultToolchain, false) | 1009 INSERT_FUNCTION(SetDefaultToolchain, false) |
925 INSERT_FUNCTION(SetSourcesAssignmentFilter, false) | 1010 INSERT_FUNCTION(SetSourcesAssignmentFilter, false) |
| 1011 INSERT_FUNCTION(SplitList, false) |
926 INSERT_FUNCTION(Template, false) | 1012 INSERT_FUNCTION(Template, false) |
927 INSERT_FUNCTION(Tool, false) | 1013 INSERT_FUNCTION(Tool, false) |
928 INSERT_FUNCTION(Toolchain, false) | 1014 INSERT_FUNCTION(Toolchain, false) |
929 INSERT_FUNCTION(ToolchainArgs, false) | 1015 INSERT_FUNCTION(ToolchainArgs, false) |
930 INSERT_FUNCTION(WriteFile, false) | 1016 INSERT_FUNCTION(WriteFile, false) |
931 | 1017 |
932 #undef INSERT_FUNCTION | 1018 #undef INSERT_FUNCTION |
933 } | 1019 } |
934 }; | 1020 }; |
935 const FunctionInfoInitializer function_info; | 1021 const FunctionInfoInitializer function_info; |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1011 } | 1097 } |
1012 | 1098 |
1013 // Otherwise it's a no-block function. | 1099 // Otherwise it's a no-block function. |
1014 if (!VerifyNoBlockForFunctionCall(function, block, err)) | 1100 if (!VerifyNoBlockForFunctionCall(function, block, err)) |
1015 return Value(); | 1101 return Value(); |
1016 return found_function->second.no_block_runner(scope, function, | 1102 return found_function->second.no_block_runner(scope, function, |
1017 args.list_value(), err); | 1103 args.list_value(), err); |
1018 } | 1104 } |
1019 | 1105 |
1020 } // namespace functions | 1106 } // namespace functions |
OLD | NEW |