@@ -68,18 +68,47 @@ def copy_file_action(ctx, src, dst, dir_path = None):
6868
6969 if dst .is_directory :
7070 fail ("dst must not be a TreeArtifact" )
71+
72+ args = ctx .actions .args ()
73+ args .add ("cp" )
74+
7175 if src .is_directory :
7276 if not dir_path :
7377 fail ("dir_path must be set if src is a TreeArtifact" )
74- src_path = "/" .join ([src .path , dir_path ])
78+
79+ # TODO(zbarsky): No path-mapping here, _copy_file_from_directory can support it though.
80+ # We can consider exposing it later.
81+ args .add ("/" .join ([src .path , dir_path ]))
7582 else :
76- src_path = src .path
83+ args .add (src )
84+
85+ args .add (dst )
86+ _run_copy_file_action (ctx , src , dst , args )
87+
88+ def _directory_path (src ):
89+ dir = src [DirectoryPathInfo ].directory
90+ dir_path = src [DirectoryPathInfo ].path
91+ if not dir_path :
92+ fail ("dir_path must be set if src is a TreeArtifact" )
93+
94+ return "/" .join ([dir .path , dir_path ])
7795
96+ def _copy_file_from_directory (ctx , src , dst ):
97+ if dst .is_directory :
98+ fail ("dst must not be a TreeArtifact" )
99+
100+ args = ctx .actions .args ()
101+ args .add ("cp" )
102+ args .add_all ([src ], map_each = _directory_path )
103+ args .add (dst )
104+ _run_copy_file_action (ctx , src [DirectoryPathInfo ].directory , dst , args )
105+
106+ def _run_copy_file_action (ctx , src , dst , args ):
78107 coreutils = ctx .toolchains [_COREUTILS_TOOLCHAIN ].coreutils_info
79108
80109 ctx .actions .run (
81110 executable = coreutils .bin ,
82- arguments = ["cp" , src_path , dst . path ],
111+ arguments = [args ],
83112 inputs = [src ],
84113 outputs = [dst ],
85114 mnemonic = "CopyFile" ,
@@ -100,12 +129,7 @@ def _copy_file_impl(ctx):
100129 is_executable = ctx .attr .is_executable ,
101130 )
102131 elif DirectoryPathInfo in ctx .attr .src :
103- copy_file_action (
104- ctx ,
105- ctx .attr .src [DirectoryPathInfo ].directory ,
106- ctx .outputs .out ,
107- dir_path = ctx .attr .src [DirectoryPathInfo ].path ,
108- )
132+ _copy_file_from_directory (ctx , ctx .attr .src , ctx .outputs .out )
109133 else :
110134 if len (ctx .files .src ) != 1 :
111135 fail ("src must be a single file or a target that provides a DirectoryPathInfo" )
0 commit comments