@@ -65,6 +65,7 @@ const char *usage =
65
65
;
66
66
67
67
#include < sys/types.h>
68
+ #include < sys/param.h>
68
69
#include < sys/uio.h>
69
70
#include < sys/stat.h>
70
71
#include < stdio.h>
@@ -423,8 +424,8 @@ ssize_t pread_all(int fd, void *buf, size_t count, off_t offset)
423
424
424
425
template <typename T>
425
426
int parse_macho (int fd, uint32_t offset, uint32_t size,
426
- void (^dylibVisitor)(std::filesystem::path path),
427
- void (^uuidVisitor)(uuid_t uuid))
427
+ void (^dylibVisitor)(std::filesystem::path const & path),
428
+ void (^uuidVisitor)(uuid_t const uuid))
428
429
{
429
430
ssize_t readed;
430
431
@@ -473,9 +474,7 @@ void (^dylibVisitor)(std::filesystem::path path),
473
474
else if (uuidVisitor && cmd->cmd () == LC_UUID) {
474
475
macho_uuid_command<T>* uuid_cmd = (macho_uuid_command<T>*)cmd;
475
476
if (uuid_cmd->cmdsize () < sizeof (uuid_command)) continue ;
476
- uuid_t uuid;
477
- memcpy (uuid, uuid_cmd->uuid (), 16 );
478
- uuidVisitor (uuid);
477
+ uuidVisitor (uuid_cmd->uuid ());
479
478
}
480
479
}
481
480
}
@@ -486,8 +485,8 @@ void (^dylibVisitor)(std::filesystem::path path),
486
485
487
486
488
487
int parse_macho (int fd, uint32_t offset, uint32_t size,
489
- void (^dylibVisitor)(std::filesystem::path path),
490
- void (^uuidVisitor)(uuid_t uuid))
488
+ void (^dylibVisitor)(std::filesystem::path const & path),
489
+ void (^uuidVisitor)(uuid_t const uuid))
491
490
{
492
491
uint32_t magic;
493
492
if (size < sizeof (magic)) return log_vv (" file is too small" );
@@ -514,8 +513,8 @@ int parse_macho(int fd, uint32_t offset, uint32_t size,
514
513
515
514
516
515
int parse_fat (int fd, off_t fsize, char *buffer, size_t size,
517
- void (^dylibVisitor)(std::filesystem::path path),
518
- void (^uuidVisitor)(uuid_t uuid))
516
+ void (^dylibVisitor)(std::filesystem::path const & path),
517
+ void (^uuidVisitor)(uuid_t const uuid))
519
518
{
520
519
uint32_t magic;
521
520
@@ -608,8 +607,8 @@ int parse_fat(int fd, off_t fsize, char *buffer, size_t size,
608
607
}
609
608
610
609
611
- void process (std::filesystem::path path, void (^dylibVisitor)(std::filesystem::path),
612
- void (^uuidVisitor)(uuid_t ))
610
+ void process (std::filesystem::path const & path, void (^dylibVisitor)(std::filesystem::path const & ),
611
+ void (^uuidVisitor)(uuid_t const ))
613
612
{
614
613
log_vv (" Scanning %s..." , path.c_str ());
615
614
@@ -644,12 +643,10 @@ bool operator <= (const struct timespec &lhs, const struct timespec &rhs)
644
643
645
644
// This executable's own path.
646
645
std::filesystem::path self_executable = []() -> std::filesystem::path {
647
- uint32_t len = 0 ;
648
- _NSGetExecutablePath (NULL , &len);
649
- std::vector<char > buffer;
650
- buffer.reserve (len);
651
- _NSGetExecutablePath (buffer.data (), &len);
652
- return buffer.data ();
646
+ char path[MAXPATHLEN] = {0 };
647
+ uint32_t len = sizeof (path);
648
+ _NSGetExecutablePath (path, &len);
649
+ return std::filesystem::path (path);
653
650
}();
654
651
655
652
@@ -695,38 +692,36 @@ int xcrunToolCommand(std::vector<std::string> commandAndArguments, XcrunToolBloc
695
692
const char *launchPath = " /usr/bin/xcrun" ;
696
693
697
694
// Tell xcrun to search our toolchain first.
698
- const char *arguments[commandAndArguments.size () + 5 ];
699
- arguments[0 ] = launchPath;
700
- arguments[1 ] = " --toolchain" ;
701
- arguments[2 ] = self_toolchain.c_str ();
702
- int argsCount = 3 ;
695
+ std::vector<const char *> arguments;
696
+ arguments.push_back (launchPath);
697
+ arguments.push_back (" --toolchain" );
698
+ arguments.push_back (self_toolchain.c_str ());
703
699
704
700
// Tell xcrun to print its command if we are very verbose.
705
701
if (Verbose > 1 ) {
706
- arguments[3 ] = " --log" ;
707
- argsCount += 1 ;
702
+ arguments.push_back (" --log" );
708
703
}
709
704
710
705
for (auto const &string : commandAndArguments) {
711
- arguments[argsCount++] = string.c_str ();
706
+ arguments. push_back ( string.c_str () );
712
707
}
713
- arguments[argsCount] = NULL ;
708
+ arguments. push_back ( NULL ) ;
714
709
715
710
int outPipe[2 ];
716
711
int errPipe[2 ];
717
712
718
713
pipe (outPipe);
719
714
pipe (errPipe);
720
715
721
- log_v (" %s" , arguments[ 0 ] );
716
+ log_v (" %s" , launchPath );
722
717
723
718
int childPid = fork ();
724
719
725
720
if (childPid == 0 ) {
726
721
dup2 (outPipe[1 ], STDOUT_FILENO);
727
722
dup2 (errPipe[1 ], STDERR_FILENO);
728
723
729
- execv (launchPath, (char *const *)arguments);
724
+ execv (launchPath, (char *const *)arguments. data () );
730
725
}
731
726
732
727
// Read stdout and stderr in parallel, then wait for the task
@@ -791,37 +786,36 @@ copyFile(std::filesystem::path src, std::filesystem::path dst, bool stripBitcode
791
786
}
792
787
}
793
788
794
- std::string uuidString (uuid_t uuid) {
789
+ std::string uuidString (uuid_t const uuid) {
795
790
char buffer[37 ];
796
791
uuid_unparse (uuid, buffer);
797
792
return buffer;
798
793
}
799
794
800
795
void copyLibraries (std::filesystem::path src_dir, std::filesystem::path dst_dir,
801
796
std::unordered_map<std::string,
802
- std::unordered_set<std::string>> libs,
797
+ std::unordered_set<std::string>> const & libs,
803
798
bool stripBitcode)
804
799
{
805
800
mkpath_np (dst_dir.c_str (), S_IRWXU | S_IRWXG | S_IRWXO);
806
801
807
- for (auto const &pair : libs) {
808
- std::filesystem::path src = src_dir/pair. first ;
809
- std::filesystem::path dst = dst_dir/pair. first ;
802
+ for (auto const &[lib, srcUUIDs] : libs) {
803
+ std::filesystem::path src = src_dir/lib ;
804
+ std::filesystem::path dst = dst_dir/lib ;
810
805
811
806
// Compare UUIDs of src and dst and don't copy if they're the same.
812
807
// Do not use mod times for this task: the dst copy gets code-signed
813
808
// and bitcode-stripped so it can look newer than it really is.
814
- auto const &srcUUIDs = pair.second ;
815
809
__block std::unordered_set<std::string> dstUUIDs;
816
- process (dst, NULL , ^(uuid_t uuid) {
810
+ process (dst, NULL , ^(uuid_t const uuid) {
817
811
dstUUIDs.insert (uuidString (uuid));
818
812
});
819
813
820
814
std::string srcUUIDsString;
821
815
srcUUIDsString.reserve (37 * srcUUIDs.size ());
822
816
823
817
for (auto const &uuidString : srcUUIDs) {
824
- srcUUIDsString.append (uuidString + " " );
818
+ srcUUIDsString.append (uuidString + std::string ( " " ) );
825
819
}
826
820
827
821
std::string dstUUIDsString;
@@ -836,14 +830,14 @@ void copyLibraries(std::filesystem::path src_dir, std::filesystem::path dst_dir,
836
830
837
831
if (srcUUIDs == dstUUIDs) {
838
832
log_v (" %s is up to date at %s" ,
839
- pair. first .c_str (), dst.c_str ());
833
+ lib .c_str (), dst.c_str ());
840
834
continue ;
841
835
}
842
836
843
837
// Perform the copy.
844
838
845
839
log_v (" Copying %s from %s to %s" ,
846
- pair. first . c_str (),
840
+ lib. c_str (),
847
841
src_dir.c_str (),
848
842
dst_dir.c_str ());
849
843
@@ -961,14 +955,13 @@ int main(int argc, const char *argv[])
961
955
// Neither src_dir nor platform is set. Die.
962
956
fail_usage (" At least one of --source-libraries and --platform "
963
957
" must be set." );
964
- }
965
- else if (src_dir.empty ()) {
958
+ } else if (src_dir.empty ()) {
966
959
// platform is set but src_dir is not.
967
960
// Use platform to set src_dir relative to us.
968
961
src_dir = self_executable.parent_path ().parent_path ()/
969
962
" lib" /" swift-5.0" /platform;
970
963
} else if (platform.empty ()) {
971
- // src_dir is set but platform is not.
964
+ // src_dir is set but platform is not.
972
965
// Pick platform from src_dir's name.
973
966
platform = src_dir.filename ();
974
967
}
@@ -1008,8 +1001,8 @@ int main(int argc, const char *argv[])
1008
1001
__block std::unordered_map<std::string,
1009
1002
std::unordered_set<std::string>> swiftLibs;
1010
1003
for (auto const &path : executables) {
1011
- process (path,
1012
- ^(std::filesystem::path linkedLib) {
1004
+ process (path,
1005
+ ^(std::filesystem::path const & linkedLib) {
1013
1006
auto const linkedSrc = src_dir/linkedLib;
1014
1007
if (std::filesystem::exists (linkedSrc)) {
1015
1008
swiftLibs[linkedLib] = std::unordered_set<std::string>();
@@ -1022,15 +1015,15 @@ int main(int argc, const char *argv[])
1022
1015
// Also collect the Swift libraries' UUIDs.
1023
1016
__block std::vector<std::string> worklist;
1024
1017
worklist.reserve (swiftLibs.size ());
1025
- for (auto const &pair : swiftLibs) {
1026
- worklist.push_back (pair. first );
1018
+ for (auto const &[lib, _] : swiftLibs) {
1019
+ worklist.push_back (lib );
1027
1020
}
1028
1021
while (worklist.size ()) {
1029
1022
auto const &lib = worklist.back ();
1030
1023
worklist.pop_back ();
1031
1024
auto const path = src_dir/lib;
1032
- process (path,
1033
- ^(std::filesystem::path linkedLib) {
1025
+ process (path,
1026
+ ^(std::filesystem::path const & linkedLib) {
1034
1027
auto const linkedSrc = src_dir/linkedLib;
1035
1028
if (swiftLibs.count (linkedLib) == 0 &&
1036
1029
std::filesystem::exists (linkedSrc))
@@ -1039,7 +1032,7 @@ int main(int argc, const char *argv[])
1039
1032
worklist.push_back (linkedLib);
1040
1033
}
1041
1034
},
1042
- ^(uuid_t uuid) {
1035
+ ^(uuid_t const uuid) {
1043
1036
swiftLibs[lib].insert (uuidString (uuid));
1044
1037
});
1045
1038
}
@@ -1057,15 +1050,15 @@ int main(int argc, const char *argv[])
1057
1050
1058
1051
// Collect dependencies of --resource-library libs.
1059
1052
worklist.clear ();
1060
- for (auto const &pair : swiftLibsForResources) {
1061
- worklist.push_back (pair. first );
1053
+ for (auto const &[lib, _] : swiftLibsForResources) {
1054
+ worklist.push_back (lib );
1062
1055
}
1063
1056
while (worklist.size ()) {
1064
1057
auto const &lib = worklist.back ();
1065
1058
worklist.pop_back ();
1066
1059
auto const path = src_dir/lib;
1067
1060
process (path,
1068
- ^(std::filesystem::path linkedLib) {
1061
+ ^(std::filesystem::path const & linkedLib) {
1069
1062
auto const linkedSrc = src_dir/linkedLib;
1070
1063
if (swiftLibsForResources.count (linkedLib) == 0 &&
1071
1064
std::filesystem::exists (linkedSrc))
@@ -1074,7 +1067,7 @@ int main(int argc, const char *argv[])
1074
1067
worklist.push_back (linkedLib);
1075
1068
}
1076
1069
},
1077
- ^(uuid_t uuid) {
1070
+ ^(uuid_t const uuid) {
1078
1071
swiftLibsForResources[lib].insert (uuidString (uuid));
1079
1072
});
1080
1073
}
@@ -1117,7 +1110,7 @@ int main(int argc, const char *argv[])
1117
1110
__block bool signedOne = false ;
1118
1111
std::mutex signingLock;
1119
1112
1120
- for (auto const &pair : swiftLibs) {
1113
+ for (auto const &[lib, _] : swiftLibs) {
1121
1114
// Work around authentication UI problems
1122
1115
// by signing one synchronously and then signing the rest.
1123
1116
signingLock.lock ();
@@ -1128,13 +1121,11 @@ int main(int argc, const char *argv[])
1128
1121
// We are the first signer. Hold the lock until we finish.
1129
1122
}
1130
1123
1131
- auto const &lib = pair.first ;
1132
- auto const dst = dst_dir/lib;
1133
-
1134
1124
// Get the code signature, and copy the dylib to the side
1135
1125
// to preserve it in case it does not change. We can use
1136
1126
// this to avoid unnecessary copies during delta installs
1137
1127
// to devices.
1128
+ auto const dst = dst_dir/lib;
1138
1129
auto const oldSignatureData = query_code_signature (dst);
1139
1130
const char *tmpFilePath = 0 ;
1140
1131
if (!oldSignatureData.empty ()) {
0 commit comments