@@ -40,6 +40,78 @@ linker_mode = struct(
4040
4141artifact_category = _artifact_category
4242
43+ def _is_non_empty_list_or_select (value , attr ):
44+ if type (value ) == "list" :
45+ return len (value ) > 0
46+ elif type (value ) == "select" :
47+ return True
48+ else :
49+ fail ("Only select or list is valid for {} attr" .format (attr ))
50+
51+ def _gen_empty_def_file (ctx ):
52+ trivial_def_file = ctx .actions .declare_file (ctx .label .name + ".gen.empty.def" )
53+ ctx .actions .write (trivial_def_file , "" , False )
54+ return trivial_def_file
55+
56+ def _get_windows_def_file_for_linking (ctx , custom_def_file , generated_def_file , feature_configuration ):
57+ # 1. If a custom DEF file is specified in win_def_file attribute, use it.
58+ # 2. If a generated DEF file is available and should be used, use it.
59+ # 3. Otherwise, we use an empty DEF file to ensure the import library will be generated.
60+ if custom_def_file != None :
61+ return custom_def_file
62+ elif generated_def_file != None and _should_generate_def_file (ctx , feature_configuration ) == True :
63+ return generated_def_file
64+ else :
65+ return _gen_empty_def_file (ctx )
66+
67+ def _should_generate_def_file (ctx , feature_configuration ):
68+ windows_export_all_symbols_enabled = cc_common .is_enabled (feature_configuration = feature_configuration , feature_name = "windows_export_all_symbols" )
69+ no_windows_export_all_symbols_enabled = cc_common .is_enabled (feature_configuration = feature_configuration , feature_name = "no_windows_export_all_symbols" )
70+ return windows_export_all_symbols_enabled and (not no_windows_export_all_symbols_enabled ) and (ctx .attr .win_def_file == None )
71+
72+ def _generate_def_file (ctx , def_parser , object_files , dll_name ):
73+ def_file = ctx .actions .declare_file (ctx .label .name + ".gen.def" )
74+ args = ctx .actions .args ()
75+ args .add (def_file )
76+ args .add (dll_name )
77+ argv = ctx .actions .args ()
78+ argv .use_param_file ("@%s" , use_always = True )
79+ argv .set_param_file_format ("shell" )
80+ for object_file in object_files :
81+ argv .add (object_file .path )
82+
83+ ctx .actions .run (
84+ mnemonic = "DefParser" ,
85+ executable = def_parser ,
86+ toolchain = None ,
87+ arguments = [args , argv ],
88+ inputs = object_files ,
89+ outputs = [def_file ],
90+ use_default_shell_env = True ,
91+ )
92+ return def_file
93+
94+ def _stringify_linker_input (linker_input ):
95+ parts = []
96+ parts .append (str (linker_input .owner ))
97+ for library in linker_input .libraries :
98+ if library .static_library != None :
99+ parts .append (library .static_library .path )
100+ if library .pic_static_library != None :
101+ parts .append (library .pic_static_library .path )
102+ if library .dynamic_library != None :
103+ parts .append (library .dynamic_library .path )
104+ if library .interface_library != None :
105+ parts .append (library .interface_library .path )
106+
107+ for additional_input in linker_input .additional_inputs :
108+ parts .append (additional_input .path )
109+
110+ for linkstamp in linker_input .linkstamps :
111+ parts .append (linkstamp .file ().path )
112+
113+ return "" .join (parts )
114+
43115def _get_compilation_contexts_from_deps (deps ):
44116 compilation_contexts = []
45117 for dep in deps :
@@ -427,5 +499,9 @@ cc_helper = struct(
427499 get_cc_flags_make_variable = _get_cc_flags_make_variable ,
428500 get_compilation_contexts_from_deps = _get_compilation_contexts_from_deps ,
429501 system_include_dirs = _system_include_dirs ,
502+ stringify_linker_input = _stringify_linker_input ,
503+ generate_def_file = _generate_def_file ,
504+ get_windows_def_file_for_linking = _get_windows_def_file_for_linking ,
505+ is_non_empty_list_or_select = _is_non_empty_list_or_select ,
430506)
431507# LINT.ThenChange(https://github.com/bazelbuild/bazel/blob/master/src/main/starlark/builtins_bzl/common/cc/cc_helper.bzl:forked_exports)
0 commit comments