1# Copyright 2021 The Chromium Authors 2# Use of this source code is governed by a BSD-style license that can be 3# found in the LICENSE file. 4 5import("//build/toolchain/apple/toolchain.gni") 6 7# Defines a template for Swift source files. The default module_name 8# of the target is the entire target label (without the leading //) 9# with all "/" and ":" replaced with "_". 10# 11# Arguments 12# 13# generate_intents 14# (optional) boolean, if true, intents definition in the source code 15# will be parsed when generating the final application (if it enable 16# the extraction of intents metadata), defaults to false. 17# 18template("swift_source_set") { 19 _generate_intents = false 20 if (defined(invoker.generate_intents)) { 21 _generate_intents = invoker.generate_intents 22 } 23 24 _target_name = target_name 25 not_needed([ "_target_name" ]) 26 source_set(target_name) { 27 forward_variables_from(invoker, "*", TESTONLY_AND_VISIBILITY) 28 forward_variables_from(invoker, TESTONLY_AND_VISIBILITY) 29 if (!defined(module_name)) { 30 _target_label = get_label_info(":$_target_name", "label_no_toolchain") 31 32 # Strip the // from the beginning of the label. 33 _target_label = string_replace(_target_label, "//", "", 1) 34 module_name = 35 string_replace(string_replace(_target_label, "/", "_"), ":", "_") 36 } 37 38 # If generate_intents is true, write file $target_name.module_info.json 39 # with information about the module used by extract_metadata.py script. 40 if (_generate_intents) { 41 _output_path = "$target_out_dir/$target_name" 42 _swift_files = sources 43 _const_files = [] 44 if (swift_whole_module_optimization) { 45 _const_files += [ "$_output_path/$module_name.swiftconstvalues" ] 46 } else { 47 foreach(_source, sources) { 48 _const_files += [ "$_output_path/" + get_path_info(_source, "name") + 49 ".swiftconstvalues" ] 50 } 51 } 52 53 _module_info = { 54 module_name = module_name 55 swift_files = rebase_path(_swift_files, root_build_dir) 56 const_files = rebase_path(_const_files, root_build_dir) 57 } 58 59 # Write the information for the module using `write_file(...)`. 60 write_file("$_output_path.module_info.json", _module_info, "json") 61 } 62 } 63} 64 65set_defaults("swift_source_set") { 66 configs = default_compiler_configs 67} 68