|
| 1 | +#include "swift/xcode-autobuilder/XcodeBuildRunner.h" |
| 2 | + |
| 3 | +#include <vector> |
| 4 | +#include <iostream> |
| 5 | +#include <spawn.h> |
| 6 | + |
| 7 | +static int waitpid_status(pid_t child) { |
| 8 | + int status; |
| 9 | + while (waitpid(child, &status, 0) == -1) { |
| 10 | + if (errno != EINTR) break; |
| 11 | + } |
| 12 | + return status; |
| 13 | +} |
| 14 | + |
| 15 | +extern char** environ; |
| 16 | + |
| 17 | +static bool exec(const std::vector<std::string>& argv) { |
| 18 | + const char** c_argv = (const char**)calloc(argv.size() + 1, sizeof(char*)); |
| 19 | + for (size_t i = 0; i < argv.size(); i++) { |
| 20 | + c_argv[i] = argv[i].c_str(); |
| 21 | + } |
| 22 | + c_argv[argv.size()] = nullptr; |
| 23 | + |
| 24 | + pid_t pid = 0; |
| 25 | + if (posix_spawn(&pid, argv.front().c_str(), nullptr, nullptr, (char* const*)c_argv, environ) != |
| 26 | + 0) { |
| 27 | + std::cerr << "[xcode autobuilder] posix_spawn failed: " << strerror(errno) << "\n"; |
| 28 | + free(c_argv); |
| 29 | + return false; |
| 30 | + } |
| 31 | + free(c_argv); |
| 32 | + int status = waitpid_status(pid); |
| 33 | + if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) { |
| 34 | + return false; |
| 35 | + } |
| 36 | + return true; |
| 37 | +} |
| 38 | + |
| 39 | +void buildTarget(Target& target, bool dryRun) { |
| 40 | + std::vector<std::string> argv({"/usr/bin/xcodebuild", "build"}); |
| 41 | + if (!target.workspace.empty()) { |
| 42 | + argv.push_back("-workspace"); |
| 43 | + argv.push_back(target.workspace); |
| 44 | + argv.push_back("-scheme"); |
| 45 | + } else { |
| 46 | + argv.push_back("-project"); |
| 47 | + argv.push_back(target.project); |
| 48 | + argv.push_back("-target"); |
| 49 | + } |
| 50 | + argv.push_back(target.name); |
| 51 | + argv.push_back("CODE_SIGNING_REQUIRED=NO"); |
| 52 | + argv.push_back("CODE_SIGNING_ALLOWED=NO"); |
| 53 | + |
| 54 | + if (dryRun) { |
| 55 | + for (auto& arg : argv) { |
| 56 | + std::cout << arg + " "; |
| 57 | + } |
| 58 | + std::cout << "\n"; |
| 59 | + } else { |
| 60 | + if (!exec(argv)) { |
| 61 | + std::cerr << "Build failed\n"; |
| 62 | + exit(1); |
| 63 | + } |
| 64 | + } |
| 65 | +} |
0 commit comments