nobuild.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  1. // Copyright 2021 Alexey Kutepov <[email protected]>
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining
  4. // a copy of this software and associated documentation files (the
  5. // "Software"), to deal in the Software without restriction, including
  6. // without limitation the rights to use, copy, modify, merge, publish,
  7. // distribute, sublicense, and/or sell copies of the Software, and to
  8. // permit persons to whom the Software is furnished to do so, subject to
  9. // the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be
  12. // included in all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  16. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  18. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  19. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  20. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. //
  22. // ============================================================
  23. //
  24. // nobuild — 0.1.0 — Header only library for writing build recipes in C.
  25. //
  26. // https://github.com/tsoding/nobuild
  27. //
  28. // ============================================================
  29. #ifndef NOBUILD_H_
  30. #define NOBUILD_H_
  31. #include <assert.h>
  32. #include <stdio.h>
  33. #include <stdlib.h>
  34. #include <stdarg.h>
  35. #include <string.h>
  36. #include <errno.h>
  37. #ifdef _WIN32
  38. # define WIN32_LEAN_AND_MEAN
  39. # include "windows.h"
  40. # include <process.h>
  41. // minirent.h HEADER BEGIN ////////////////////////////////////////
  42. // Copyright 2021 Alexey Kutepov <[email protected]>
  43. //
  44. // Permission is hereby granted, free of charge, to any person obtaining
  45. // a copy of this software and associated documentation files (the
  46. // "Software"), to deal in the Software without restriction, including
  47. // without limitation the rights to use, copy, modify, merge, publish,
  48. // distribute, sublicense, and/or sell copies of the Software, and to
  49. // permit persons to whom the Software is furnished to do so, subject to
  50. // the following conditions:
  51. //
  52. // The above copyright notice and this permission notice shall be
  53. // included in all copies or substantial portions of the Software.
  54. //
  55. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  56. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  57. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  58. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  59. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  60. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  61. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  62. //
  63. // ============================================================
  64. //
  65. // minirent — 0.0.1 — A subset of dirent interface for Windows.
  66. //
  67. // https://github.com/tsoding/minirent
  68. //
  69. // ============================================================
  70. //
  71. // ChangeLog (https://semver.org/ is implied)
  72. //
  73. // 0.0.1 First Official Release
  74. #ifndef MINIRENT_H_
  75. #define MINIRENT_H_
  76. #define WIN32_LEAN_AND_MEAN
  77. #include "windows.h"
  78. struct dirent
  79. {
  80. char d_name[MAX_PATH+1];
  81. };
  82. typedef struct DIR DIR;
  83. DIR *opendir(const char *dirpath);
  84. struct dirent *readdir(DIR *dirp);
  85. int closedir(DIR *dirp);
  86. #endif // MINIRENT_H_
  87. // minirent.h HEADER END ////////////////////////////////////////
  88. #else
  89. # include <sys/stat.h>
  90. # include <sys/types.h>
  91. # include <sys/wait.h>
  92. # include <unistd.h>
  93. # include <dirent.h>
  94. #endif // _WIN32
  95. // TODO(#1): no way to disable echo in nobuild scripts
  96. // TODO(#2): no way to ignore fails
  97. #ifdef _WIN32
  98. # define PATH_SEP "\\"
  99. #else
  100. # define PATH_SEP "/"
  101. #endif // _WIN32
  102. #define PATH_SEP_LEN (sizeof(PATH_SEP) - 1)
  103. #define FOREACH_VARGS(param, arg, args, body) \
  104. do { \
  105. va_start(args, param); \
  106. for (const char *arg = va_arg(args, const char *); \
  107. arg != NULL; \
  108. arg = va_arg(args, const char *)) \
  109. { \
  110. body; \
  111. } \
  112. va_end(args); \
  113. } while(0)
  114. #define FOREACH_ARRAY(type, item, items, body) \
  115. do { \
  116. for (size_t i = 0; \
  117. i < sizeof(items) / sizeof((items)[0]); \
  118. ++i) \
  119. { \
  120. type item = items[i]; \
  121. body; \
  122. } \
  123. } while(0)
  124. #define FOREACH_FILE_IN_DIR(file, dirpath, body) \
  125. do { \
  126. struct dirent *dp = NULL; \
  127. DIR *dir = opendir(dirpath); \
  128. while ((dp = readdir(dir))) { \
  129. const char *file = dp->d_name; \
  130. body; \
  131. } \
  132. closedir(dir); \
  133. } while(0)
  134. // TODO(#5): there is no way to redirect the output of CMD to a file
  135. #define CMD(...) \
  136. do { \
  137. INFO(JOIN(" ", __VA_ARGS__)); \
  138. cmd_impl(69, __VA_ARGS__, NULL); \
  139. } while(0)
  140. const char *concat_impl(int ignore, ...);
  141. const char *concat_sep_impl(const char *sep, ...);
  142. const char *build__join(const char *sep, ...);
  143. void mkdirs_impl(int ignore, ...);
  144. void cmd_impl(int ignore, ...);
  145. void nobuild_exec(const char **argv);
  146. const char *remove_ext(const char *path);
  147. char *shift(int *argc, char ***argv);
  148. #define CONCAT(...) concat_impl(69, __VA_ARGS__, NULL)
  149. #define CONCAT_SEP(sep, ...) build__deprecated_concat_sep(sep, __VA_ARGS__, NULL)
  150. #define JOIN(sep, ...) build__join(sep, __VA_ARGS__, NULL)
  151. #define PATH(...) JOIN(PATH_SEP, __VA_ARGS__)
  152. #define MKDIRS(...) mkdirs_impl(69, __VA_ARGS__, NULL)
  153. #define NOEXT(path) nobuild__remove_ext(path)
  154. void nobuild_log(FILE *stream, const char *tag, const char *fmt, ...);
  155. void nobuild_vlog(FILE *stream, const char *tag, const char *fmt, va_list args);
  156. void INFO(const char *fmt, ...);
  157. void WARN(const char *fmt, ...);
  158. void ERRO(const char *fmt, ...);
  159. #endif // NOBUILD_H_
  160. #ifdef NOBUILD_IMPLEMENTATION
  161. #ifdef _WIN32
  162. // minirent.h IMPLEMENTATION BEGIN ////////////////////////////////////////
  163. struct DIR
  164. {
  165. HANDLE hFind;
  166. WIN32_FIND_DATA data;
  167. struct dirent *dirent;
  168. };
  169. DIR *opendir(const char *dirpath)
  170. {
  171. assert(dirpath);
  172. char buffer[MAX_PATH];
  173. snprintf(buffer, MAX_PATH, "%s\\*", dirpath);
  174. DIR *dir = (DIR*)calloc(1, sizeof(DIR));
  175. dir->hFind = FindFirstFile(buffer, &dir->data);
  176. if (dir->hFind == INVALID_HANDLE_VALUE) {
  177. errno = ENOSYS;
  178. goto fail;
  179. }
  180. return dir;
  181. fail:
  182. if (dir) {
  183. free(dir);
  184. }
  185. return NULL;
  186. }
  187. struct dirent *readdir(DIR *dirp)
  188. {
  189. assert(dirp);
  190. if (dirp->dirent == NULL) {
  191. dirp->dirent = (struct dirent*)calloc(1, sizeof(struct dirent));
  192. } else {
  193. if(!FindNextFile(dirp->hFind, &dirp->data)) {
  194. if (GetLastError() != ERROR_NO_MORE_FILES) {
  195. errno = ENOSYS;
  196. }
  197. return NULL;
  198. }
  199. }
  200. memset(dirp->dirent->d_name, 0, sizeof(dirp->dirent->d_name));
  201. strncpy(
  202. dirp->dirent->d_name,
  203. dirp->data.cFileName,
  204. sizeof(dirp->dirent->d_name) - 1);
  205. return dirp->dirent;
  206. }
  207. int closedir(DIR *dirp)
  208. {
  209. assert(dirp);
  210. if(!FindClose(dirp->hFind)) {
  211. errno = ENOSYS;
  212. return -1;
  213. }
  214. if (dirp->dirent) {
  215. free(dirp->dirent);
  216. }
  217. free(dirp);
  218. return 0;
  219. }
  220. // minirent.h IMPLEMENTATION END ////////////////////////////////////////
  221. #endif // _WIN32
  222. const char *build__join(const char *sep, ...)
  223. {
  224. const size_t sep_len = strlen(sep);
  225. size_t length = 0;
  226. size_t seps_count = 0;
  227. va_list args;
  228. FOREACH_VARGS(sep, arg, args, {
  229. length += strlen(arg);
  230. seps_count += 1;
  231. });
  232. assert(length > 0);
  233. seps_count -= 1;
  234. char *result = malloc(length + seps_count * sep_len + 1);
  235. length = 0;
  236. FOREACH_VARGS(sep, arg, args, {
  237. size_t n = strlen(arg);
  238. memcpy(result + length, arg, n);
  239. length += n;
  240. if (seps_count > 0) {
  241. memcpy(result + length, sep, sep_len);
  242. length += sep_len;
  243. seps_count -= 1;
  244. }
  245. });
  246. result[length] = '\0';
  247. return result;
  248. }
  249. const char *build__deprecated_concat_sep(const char *sep, ...)
  250. {
  251. WARN("DEPRECATED: Please don't use `CONCAT_SEP(sep, ...)`. Please use JOIN(sep, ...) instead. `CONCAT_SEP(sep, ...)` will be removed in the next major release.");
  252. const size_t sep_len = strlen(sep);
  253. size_t length = 0;
  254. size_t seps_count = 0;
  255. va_list args;
  256. FOREACH_VARGS(sep, arg, args, {
  257. length += strlen(arg);
  258. seps_count += 1;
  259. });
  260. assert(length > 0);
  261. seps_count -= 1;
  262. char *result = malloc(length + seps_count * sep_len + 1);
  263. length = 0;
  264. FOREACH_VARGS(sep, arg, args, {
  265. size_t n = strlen(arg);
  266. memcpy(result + length, arg, n);
  267. length += n;
  268. if (seps_count > 0) {
  269. memcpy(result + length, sep, sep_len);
  270. length += sep_len;
  271. seps_count -= 1;
  272. }
  273. });
  274. result[length] = '\0';
  275. return result;
  276. }
  277. const char *concat_sep_impl(const char *sep, ...)
  278. {
  279. WARN("DEPRECATED: Please don't use `concat_sep_impl(sep, ...)`. Please use JOIN(sep, ...) instead. `concat_sep_impl(sep, ...)` will be removed in the next major release.");
  280. const size_t sep_len = strlen(sep);
  281. size_t length = 0;
  282. size_t seps_count = 0;
  283. va_list args;
  284. FOREACH_VARGS(sep, arg, args, {
  285. length += strlen(arg);
  286. seps_count += 1;
  287. });
  288. assert(length > 0);
  289. seps_count -= 1;
  290. char *result = malloc(length + seps_count * sep_len + 1);
  291. length = 0;
  292. FOREACH_VARGS(sep, arg, args, {
  293. size_t n = strlen(arg);
  294. memcpy(result + length, arg, n);
  295. length += n;
  296. if (seps_count > 0) {
  297. memcpy(result + length, sep, sep_len);
  298. length += sep_len;
  299. seps_count -= 1;
  300. }
  301. });
  302. result[length] = '\0';
  303. return result;
  304. }
  305. void mkdirs_impl(int ignore, ...)
  306. {
  307. size_t length = 0;
  308. size_t seps_count = 0;
  309. va_list args;
  310. FOREACH_VARGS(ignore, arg, args, {
  311. length += strlen(arg);
  312. seps_count += 1;
  313. });
  314. assert(length > 0);
  315. seps_count -= 1;
  316. char *result = malloc(length + seps_count * PATH_SEP_LEN + 1);
  317. length = 0;
  318. FOREACH_VARGS(ignore, arg, args, {
  319. size_t n = strlen(arg);
  320. memcpy(result + length, arg, n);
  321. length += n;
  322. if (seps_count > 0) {
  323. memcpy(result + length, PATH_SEP, PATH_SEP_LEN);
  324. length += PATH_SEP_LEN;
  325. seps_count -= 1;
  326. }
  327. result[length] = '\0';
  328. INFO("mkdir %s", result);
  329. if (mkdir(result, 0755) < 0) {
  330. if (errno == EEXIST) {
  331. WARN("directory %s already exists", result);
  332. } else {
  333. ERRO("could not create directory %s: %s", result, strerror(errno));
  334. exit(1);
  335. }
  336. }
  337. });
  338. }
  339. // TODO(#3): there is no way to remove a folder
  340. // TODO(#4): there is no way to remove a file
  341. const char *concat_impl(int ignore, ...)
  342. {
  343. size_t length = 0;
  344. va_list args;
  345. FOREACH_VARGS(ignore, arg, args, {
  346. length += strlen(arg);
  347. });
  348. char *result = malloc(length + 1);
  349. length = 0;
  350. FOREACH_VARGS(ignore, arg, args, {
  351. size_t n = strlen(arg);
  352. memcpy(result + length, arg, n);
  353. length += n;
  354. });
  355. result[length] = '\0';
  356. return result;
  357. }
  358. void nobuild_exec(const char **argv)
  359. {
  360. #ifdef _WIN32
  361. intptr_t status = _spawnvp(_P_WAIT, argv[0], (char * const*) argv);
  362. if (status < 0) {
  363. ERRO("could not start child process: %s", strerror(errno));
  364. exit(1);
  365. }
  366. if (status > 0) {
  367. ERRO("command exited with exit code %d", status);
  368. exit(1);
  369. }
  370. #else
  371. pid_t cpid = fork();
  372. if (cpid == -1) {
  373. ERRO("could not fork a child process: %s", strerror(errno));
  374. exit(1);
  375. }
  376. if (cpid == 0) {
  377. if (execvp(argv[0], (char * const*) argv) < 0) {
  378. ERRO("could not execute child process: %s", strerror(errno));
  379. exit(1);
  380. }
  381. } else {
  382. for (;;) {
  383. int wstatus = 0;
  384. wait(&wstatus);
  385. if (WIFEXITED(wstatus)) {
  386. int exit_status = WEXITSTATUS(wstatus);
  387. if (exit_status != 0) {
  388. ERRO("command exited with exit code %d", exit_status);
  389. exit(1);
  390. }
  391. break;
  392. }
  393. if (WIFSIGNALED(wstatus)) {
  394. ERRO("command process was terminated by signal %d", WTERMSIG(wstatus));
  395. exit(1);
  396. }
  397. }
  398. }
  399. #endif // _WIN32
  400. }
  401. void cmd_impl(int ignore, ...)
  402. {
  403. size_t argc = 0;
  404. va_list args;
  405. FOREACH_VARGS(ignore, arg, args, {
  406. argc += 1;
  407. });
  408. const char **argv = malloc(sizeof(const char*) * (argc + 1));
  409. argc = 0;
  410. FOREACH_VARGS(ignore, arg, args, {
  411. argv[argc++] = arg;
  412. });
  413. argv[argc] = NULL;
  414. assert(argc >= 1);
  415. nobuild_exec(argv);
  416. }
  417. const char *nobuild__remove_ext(const char *path)
  418. {
  419. size_t n = strlen(path);
  420. while (n > 0 && path[n - 1] != '.') {
  421. n -= 1;
  422. }
  423. if (n > 0) {
  424. char *result = malloc(n);
  425. memcpy(result, path, n);
  426. result[n - 1] = '\0';
  427. return result;
  428. } else {
  429. return path;
  430. }
  431. }
  432. const char *remove_ext(const char *path)
  433. {
  434. WARN("DEPRECATED: Please use `NOEXT(path)` instead of `remove_ext(path)`. `remove_ext(path)` will be removed in the next major release.");
  435. nobuild__remove_ext(path);
  436. }
  437. char *shift(int *argc, char ***argv)
  438. {
  439. assert(*argc > 0);
  440. char *result = **argv;
  441. *argv += 1;
  442. *argc -= 1;
  443. return result;
  444. }
  445. void nobuild_log(FILE *stream, const char *tag, const char *fmt, ...)
  446. {
  447. va_list args;
  448. va_start(args, fmt);
  449. nobuild_vlog(stream, tag, fmt, args);
  450. va_end(args);
  451. }
  452. void nobuild_vlog(FILE *stream, const char *tag, const char *fmt, va_list args)
  453. {
  454. fprintf(stream, "[%s] ", tag);
  455. vfprintf(stream, fmt, args);
  456. fprintf(stream, "\n");
  457. }
  458. void INFO(const char *fmt, ...)
  459. {
  460. va_list args;
  461. va_start(args, fmt);
  462. nobuild_vlog(stdout, "INFO", fmt, args);
  463. va_end(args);
  464. }
  465. void WARN(const char *fmt, ...)
  466. {
  467. va_list args;
  468. va_start(args, fmt);
  469. nobuild_vlog(stderr, "WARN", fmt, args);
  470. va_end(args);
  471. }
  472. void ERRO(const char *fmt, ...)
  473. {
  474. va_list args;
  475. va_start(args, fmt);
  476. nobuild_vlog(stderr, "ERRO", fmt, args);
  477. va_end(args);
  478. }
  479. #endif // NOBUILD_IMPLEMENTATION