nobuild.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. #include <assert.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <stdarg.h>
  5. #include <string.h>
  6. #include <errno.h>
  7. #ifdef _WIN32
  8. # define WIN32_LEAN_AND_MEAN
  9. # include "windows.h"
  10. # include <process.h>
  11. #else
  12. # include <sys/stat.h>
  13. # include <sys/types.h>
  14. # include <sys/wait.h>
  15. # include <unistd.h>
  16. #endif // _WIN32
  17. // TODO(#1): no way to disable echo in nobuild scripts
  18. // TODO(#2): no way to ignore fails
  19. #ifdef _WIN32
  20. struct dirent
  21. {
  22. char d_name[MAX_PATH+1];
  23. };
  24. typedef struct {
  25. HANDLE hFind;
  26. WIN32_FIND_DATA data;
  27. struct dirent *dirent;
  28. } DIR;
  29. DIR *opendir(const char *dirpath)
  30. {
  31. assert(dirpath);
  32. char buffer[MAX_PATH];
  33. snprintf(buffer, MAX_PATH, "%s\\*", dirpath);
  34. DIR *dir = (DIR*)calloc(1, sizeof(DIR));
  35. dir->hFind = FindFirstFile(buffer, &dir->data);
  36. if (dir->hFind == INVALID_HANDLE_VALUE) {
  37. goto fail;
  38. }
  39. return dir;
  40. fail:
  41. if (dir) {
  42. free(dir);
  43. }
  44. return NULL;
  45. }
  46. struct dirent *readdir(DIR *dirp)
  47. {
  48. assert(dirp);
  49. if (dirp->dirent == NULL) {
  50. dirp->dirent = (struct dirent*)calloc(1, sizeof(struct dirent));
  51. } else {
  52. if(!FindNextFile(dirp->hFind, &dirp->data)) {
  53. return NULL;
  54. }
  55. }
  56. memset(dirp->dirent->d_name, 0, sizeof(dirp->dirent->d_name));
  57. strncpy(
  58. dirp->dirent->d_name,
  59. dirp->data.cFileName,
  60. sizeof(dirp->dirent->d_name) - 1);
  61. return dirp->dirent;
  62. }
  63. void closedir(DIR *dirp)
  64. {
  65. assert(dirp);
  66. FindClose(dirp->hFind);
  67. if (dirp->dirent) {
  68. free(dirp->dirent);
  69. }
  70. free(dirp);
  71. }
  72. #else
  73. # include <dirent.h>
  74. #endif // _WIN32
  75. #ifdef _WIN32
  76. # define PATH_SEP "\\"
  77. #else
  78. # define PATH_SEP "/"
  79. #endif // _WIN32
  80. #define PATH_SEP_LEN (sizeof(PATH_SEP) - 1)
  81. #define FOREACH_VARGS(param, arg, args, body) \
  82. do { \
  83. va_start(args, param); \
  84. for (const char *arg = va_arg(args, const char *); \
  85. arg != NULL; \
  86. arg = va_arg(args, const char *)) \
  87. { \
  88. body; \
  89. } \
  90. va_end(args); \
  91. } while(0)
  92. #define FOREACH_ARRAY(type, item, items, body) \
  93. do { \
  94. for (size_t i = 0; \
  95. i < sizeof(items) / sizeof((items)[0]); \
  96. ++i) \
  97. { \
  98. type item = items[i]; \
  99. body; \
  100. } \
  101. } while(0)
  102. #define FOREACH_FILE_IN_DIR(file, dirpath, body) \
  103. do { \
  104. struct dirent *dp = NULL; \
  105. DIR *dir = opendir(dirpath); \
  106. while ((dp = readdir(dir))) { \
  107. const char *file = dp->d_name; \
  108. body; \
  109. } \
  110. closedir(dir); \
  111. } while(0)
  112. const char *concat_sep_impl(const char *sep, ...)
  113. {
  114. const size_t sep_len = strlen(sep);
  115. size_t length = 0;
  116. size_t seps_count = 0;
  117. va_list args;
  118. FOREACH_VARGS(sep, arg, args, {
  119. length += strlen(arg);
  120. seps_count += 1;
  121. });
  122. assert(length > 0);
  123. seps_count -= 1;
  124. char *result = malloc(length + seps_count * sep_len + 1);
  125. length = 0;
  126. FOREACH_VARGS(sep, arg, args, {
  127. size_t n = strlen(arg);
  128. memcpy(result + length, arg, n);
  129. length += n;
  130. if (seps_count > 0) {
  131. memcpy(result + length, sep, sep_len);
  132. length += sep_len;
  133. seps_count -= 1;
  134. }
  135. });
  136. result[length] = '\0';
  137. return result;
  138. }
  139. #define CONCAT_SEP(sep, ...) concat_sep_impl(sep, __VA_ARGS__, NULL)
  140. #define PATH(...) CONCAT_SEP(PATH_SEP, __VA_ARGS__)
  141. void mkdirs_impl(int ignore, ...)
  142. {
  143. size_t length = 0;
  144. size_t seps_count = 0;
  145. va_list args;
  146. FOREACH_VARGS(ignore, arg, args, {
  147. length += strlen(arg);
  148. seps_count += 1;
  149. });
  150. assert(length > 0);
  151. seps_count -= 1;
  152. char *result = malloc(length + seps_count * PATH_SEP_LEN + 1);
  153. length = 0;
  154. FOREACH_VARGS(ignore, arg, args, {
  155. size_t n = strlen(arg);
  156. memcpy(result + length, arg, n);
  157. length += n;
  158. if (seps_count > 0) {
  159. memcpy(result + length, PATH_SEP, PATH_SEP_LEN);
  160. length += PATH_SEP_LEN;
  161. seps_count -= 1;
  162. }
  163. result[length] = '\0';
  164. printf("[INFO] mkdir %s\n", result);
  165. if (mkdir(result, 0755) < 0) {
  166. if (errno == EEXIST) {
  167. fprintf(stderr, "[WARN] directory %s already exists\n",
  168. result);
  169. } else {
  170. fprintf(stderr, "[ERROR] could not create directory %s: %s\n",
  171. result, strerror(errno));
  172. exit(1);
  173. }
  174. }
  175. });
  176. }
  177. #define MKDIRS(...) mkdirs_impl(69, __VA_ARGS__, NULL)
  178. // TODO: there is no way to remove a folder
  179. // TODO: there is no way to remove a file
  180. const char *concat_impl(int ignore, ...)
  181. {
  182. size_t length = 0;
  183. va_list args;
  184. FOREACH_VARGS(ignore, arg, args, {
  185. length += strlen(arg);
  186. });
  187. char *result = malloc(length + 1);
  188. length = 0;
  189. FOREACH_VARGS(ignore, arg, args, {
  190. size_t n = strlen(arg);
  191. memcpy(result + length, arg, n);
  192. length += n;
  193. });
  194. result[length] = '\0';
  195. return result;
  196. }
  197. #define CONCAT(...) concat_impl(69, __VA_ARGS__, NULL)
  198. void nobuild_exec(const char **argv)
  199. {
  200. #ifdef _WIN32
  201. intptr_t status = _spawnvp(_P_WAIT, argv[0], (char * const*) argv);
  202. if (status < 0) {
  203. fprintf(stderr, "[ERROR] could not start child process: %s\n",
  204. strerror(errno));
  205. exit(1);
  206. }
  207. if (status > 0) {
  208. fprintf(stderr, "[ERROR] command exited with exit code %d\n",
  209. status);
  210. exit(1);
  211. }
  212. #else
  213. pid_t cpid = fork();
  214. if (cpid == -1) {
  215. fprintf(stderr, "[ERROR] could not fork a child process: %s\n",
  216. strerror(errno));
  217. exit(1);
  218. }
  219. if (cpid == 0) {
  220. if (execvp(argv[0], (char * const*) argv) < 0) {
  221. fprintf(stderr, "[ERROR] could not execute child process: %s\n",
  222. strerror(errno));
  223. exit(1);
  224. }
  225. } else {
  226. for (;;) {
  227. int wstatus = 0;
  228. wait(&wstatus);
  229. if (WIFEXITED(wstatus)) {
  230. int exit_status = WEXITSTATUS(wstatus);
  231. if (exit_status != 0) {
  232. fprintf(stderr, "[ERROR] command exited with exit code %d\n", exit_status);
  233. exit(-1);
  234. }
  235. break;
  236. }
  237. if (WIFSIGNALED(wstatus)) {
  238. fprintf(stderr, "[ERROR] command process was terminated by signal %d\n",
  239. WTERMSIG(wstatus));
  240. exit(-1);
  241. }
  242. }
  243. }
  244. #endif // _WIN32
  245. }
  246. void cmd_impl(int ignore, ...)
  247. {
  248. size_t argc = 0;
  249. va_list args;
  250. FOREACH_VARGS(ignore, arg, args, {
  251. argc += 1;
  252. });
  253. const char **argv = malloc(sizeof(const char*) * (argc + 1));
  254. argc = 0;
  255. FOREACH_VARGS(ignore, arg, args, {
  256. argv[argc++] = arg;
  257. });
  258. argv[argc] = NULL;
  259. assert(argc >= 1);
  260. nobuild_exec(argv);
  261. }
  262. // TODO: there is no way to redirect the output of CMD to a file
  263. #define CMD(...) \
  264. do { \
  265. printf("[INFO] %s\n", CONCAT_SEP(" ", __VA_ARGS__)); \
  266. cmd_impl(69, __VA_ARGS__, NULL); \
  267. } while(0)
  268. const char *remove_ext(const char *path)
  269. {
  270. size_t n = strlen(path);
  271. while (n > 0 && path[n - 1] != '.') {
  272. n -= 1;
  273. }
  274. if (n > 0) {
  275. char *result = malloc(n);
  276. memcpy(result, path, n);
  277. result[n - 1] = '\0';
  278. return result;
  279. } else {
  280. return path;
  281. }
  282. }
  283. char *shift(int *argc, char ***argv)
  284. {
  285. assert(*argc > 0);
  286. char *result = **argv;
  287. *argv += 1;
  288. *argc -= 1;
  289. return result;
  290. }