+}
+
+bool InputFiles::frameworkAlreadyLoaded(const char* path, const char* frameworkName)
+{
+ for (ld::File* file : _inputFiles) {
+ if ( strcmp(path, file->path()) == 0 )
+ return true;
+ }
+ for (ld::dylib::File* dylibx : _allDylibs) {
+ const char* fname = dylibx->frameworkName();
+ if ( fname == NULL )
+ continue;
+ if ( strcmp(frameworkName, fname) == 0 )
+ return true;
+ }
+ return false;
+}
+
+bool InputFiles::libraryAlreadyLoaded(const char* path)
+{
+ for (ld::File* file : _inputFiles) {
+ if ( strcmp(path, file->path()) == 0 )
+ return true;
+ }
+ for (ld::dylib::File* dylib : _allDylibs) {
+ if ( strcmp(path, dylib->path()) == 0 )
+ return true;
+ }
+ for (const LibraryInfo& libInfo : _searchLibraries) {
+ if ( strcmp(path, libInfo.archive()->path()) == 0 )
+ return true;
+ }
+
+ char realDylibPath[PATH_MAX];
+ if ( (realpath(path, realDylibPath) != NULL) && (strcmp(path, realDylibPath) != 0) ) {
+ return libraryAlreadyLoaded(realDylibPath);
+ }
+
+ return false;
+}
+
+
+void InputFiles::addLinkerOptionLibraries(ld::Internal& state, ld::File::AtomHandler& handler)
+{
+ if ( _options.outputKind() == Options::kObjectFile )
+ return;
+
+ while (! state.unprocessedLinkerOptionLibraries.empty() || ! state.unprocessedLinkerOptionFrameworks.empty()) {
+
+ // process frameworks specified in .o linker options
+ CStringSet newFrameworks = std::move(state.unprocessedLinkerOptionFrameworks);
+ state.unprocessedLinkerOptionFrameworks.clear();
+ for (const char* frameworkName : newFrameworks) {
+ if ( state.linkerOptionFrameworks.count(frameworkName) )
+ continue;
+ try {
+ Options::FileInfo info = _options.findFramework(frameworkName);
+ if ( ! this->frameworkAlreadyLoaded(info.path, frameworkName) ) {
+ _linkerOptionOrdinal = _linkerOptionOrdinal.nextLinkerOptionOrdinal();
+ info.ordinal = _linkerOptionOrdinal;
+ ld::File* reader = this->makeFile(info, true);
+ ld::dylib::File* dylibReader = dynamic_cast<ld::dylib::File*>(reader);
+ ld::archive::File* archiveReader = dynamic_cast<ld::archive::File*>(reader);
+ if ( dylibReader != NULL ) {
+ if ( ! dylibReader->installPathVersionSpecific() ) {
+ dylibReader->forEachAtom(handler);
+ dylibReader->setImplicitlyLinked();
+ dylibReader->setSpeculativelyLoaded();
+ this->addDylib(dylibReader, info);
+ }
+ }
+ else if ( archiveReader != NULL ) {
+ _searchLibraries.push_back(LibraryInfo(archiveReader));
+ _options.addDependency(Options::depArchive, archiveReader->path());
+ //<rdar://problem/17787306> -force_load_swift_libs
+ if (info.options.fForceLoad) {
+ archiveReader->forEachAtom(handler);
+ }
+ }
+ else {
+ throwf("framework linker option at %s is not a dylib and not an archive", info.path);
+ }
+ }
+ }
+ catch (const char* msg) {
+ warning("Auto-Linking %s", msg);
+ }
+ state.linkerOptionFrameworks.insert(frameworkName);
+ }
+
+ // process libraries specified in .o linker options
+ // fixme optimize with std::move?
+ CStringSet newLibraries = std::move(state.unprocessedLinkerOptionLibraries);
+ state.unprocessedLinkerOptionLibraries.clear();
+ for (const char* libName : newLibraries) {
+ if ( state.linkerOptionLibraries.count(libName) )
+ continue;
+ try {
+ Options::FileInfo info = _options.findLibrary(libName);
+ if ( ! this->libraryAlreadyLoaded(info.path) ) {
+ _linkerOptionOrdinal = _linkerOptionOrdinal.nextLinkerOptionOrdinal();
+ info.ordinal = _linkerOptionOrdinal;
+ //<rdar://problem/17787306> -force_load_swift_libs
+ info.options.fForceLoad = _options.forceLoadSwiftLibs() && (strncmp(libName, "swift", 5) == 0);
+ ld::File* reader = this->makeFile(info, true);
+ ld::dylib::File* dylibReader = dynamic_cast<ld::dylib::File*>(reader);
+ ld::archive::File* archiveReader = dynamic_cast<ld::archive::File*>(reader);
+ if ( dylibReader != NULL ) {
+ dylibReader->forEachAtom(handler);
+ dylibReader->setImplicitlyLinked();
+ dylibReader->setSpeculativelyLoaded();
+ this->addDylib(dylibReader, info);
+ }
+ else if ( archiveReader != NULL ) {
+ _searchLibraries.push_back(LibraryInfo(archiveReader));
+ _options.addDependency(Options::depArchive, archiveReader->path());
+ //<rdar://problem/17787306> -force_load_swift_libs
+ if (info.options.fForceLoad) {
+ archiveReader->forEachAtom(handler);
+ }
+ }
+ else {
+ throwf("linker option dylib at %s is not a dylib", info.path);
+ }
+ }
+ }
+ catch (const char* msg) {
+ warning("Auto-Linking %s", msg);
+ }
+ state.linkerOptionLibraries.insert(libName);
+ }
+ }
+}
+
+void InputFiles::createIndirectDylibs()
+{