Skip to content

Commit 5469202

Browse files
committed
Get monorepo fully building on Windows again
The mkdeps tool was failing, because it used a clever mmap() hack that's no longer supported. I've also removed tinymalloc.inc from build tooling because Windows doesn't like the way it uses overcommit memory. Sadly it means our build tool binaries will be larger. It's less of an issue, now that we are no longer putting build tool binaries in the git repository.
1 parent acd6c32 commit 5469202

File tree

15 files changed

+40
-93
lines changed

15 files changed

+40
-93
lines changed

libc/mem/tinymalloc.inc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "libc/assert.h"
1717
#include "libc/dce.h"
1818
#include "libc/errno.h"
19+
#include "libc/intrin/kprintf.h"
1920
#include "libc/mem/mem.h"
2021
#include "libc/stdalign.h"
2122
#include "libc/stdckdint.h"
@@ -50,6 +51,10 @@ static void tinymalloc_init(void) {
5051
heap.once = 1;
5152
}
5253

54+
__attribute__((__destructor__)) static void destroy(void) {
55+
kprintf("used = %'zu\n", heap.used);
56+
}
57+
5358
static inline int isheap(char *mem) {
5459
return heap.memory <= mem && mem < heap.memory + heap.used;
5560
}

tool/build/apelink.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,8 +260,6 @@ static Elf64_Xword notesize;
260260

261261
static char *r_off32_e_lfanew;
262262

263-
#include "libc/mem/tinymalloc.inc"
264-
265263
static wontreturn void Die(const char *thing, const char *reason) {
266264
tinyprint(2, thing, ": ", reason, "\n", NULL);
267265
exit(1);

tool/build/assimilate.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,6 @@
6767
#define FORMAT_MACHO 2
6868
#define FORMAT_PE 3
6969

70-
#include "libc/mem/tinymalloc.inc"
71-
7270
static int g_arch;
7371
static int g_format;
7472
static bool g_force;

tool/build/compile.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,8 +229,6 @@ const char *const kSafeEnv[] = {
229229
"TMPDIR", // needed by compiler
230230
};
231231

232-
#include "libc/mem/tinymalloc.inc"
233-
234232
void OnAlrm(int sig) {
235233
++gotalrm;
236234
}

tool/build/cp.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,6 @@ char linkbuf[PATH_MAX];
6969

7070
void Cp(char *, char *);
7171

72-
#include "libc/mem/tinymalloc.inc"
73-
7472
bool IsDirectory(const char *path) {
7573
int e;
7674
bool res;

tool/build/elf2pe.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,9 +159,6 @@ static const char *stubpath;
159159
static long FLAG_SizeOfStackCommit = 64 * 1024;
160160
static long FLAG_SizeOfStackReserve = 8 * 1024 * 1024;
161161

162-
#define TINYMALLOC_MAX_ALIGN MAX_ALIGN
163-
#include "libc/mem/tinymalloc.inc"
164-
165162
static wontreturn void Die(const char *thing, const char *reason) {
166163
tinyprint(2, thing, ": ", reason, "\n", NULL);
167164
exit(1);

tool/build/fixupobj.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,6 @@ static Elf64_Ehdr *elf;
6767
static const char *epath;
6868
static Elf64_Xword symcount;
6969

70-
#include "libc/mem/tinymalloc.inc"
71-
7270
static wontreturn void Die(const char *reason) {
7371
tinyprint(2, epath, ": ", reason, "\n", NULL);
7472
exit(1);

tool/build/gzip.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,6 @@ const char *prog;
7171
char databuf[32768];
7272
char pathbuf[PATH_MAX];
7373

74-
#include "libc/mem/tinymalloc.inc"
75-
7674
wontreturn void PrintUsage(int rc, FILE *f) {
7775
fputs("usage: ", f);
7876
fputs(prog, f);

tool/build/killall.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,6 @@ static const char *prog;
5151
static char16_t **filters;
5252
static uint32_t pids[10000];
5353

54-
#include "libc/mem/tinymalloc.inc"
55-
5654
static wontreturn void PrintUsage(int rc, FILE *f) {
5755
fprintf(f,
5856
"Usage: %s [-nshv] NAME...\n"

tool/build/mkdeps.c

Lines changed: 35 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@
2222
#include "libc/fmt/itoa.h"
2323
#include "libc/fmt/libgen.h"
2424
#include "libc/fmt/magnumstrs.internal.h"
25-
#include "libc/intrin/kprintf.h"
2625
#include "libc/limits.h"
2726
#include "libc/macros.h"
2827
#include "libc/mem/alg.h"
28+
#include "libc/mem/leaks.h"
2929
#include "libc/mem/mem.h"
3030
#include "libc/nexgen32e/crc32.h"
3131
#include "libc/runtime/runtime.h"
@@ -146,8 +146,6 @@ static const char *buildroot;
146146
static const char *genroot;
147147
static const char *outpath;
148148

149-
#include "libc/mem/tinymalloc.inc"
150-
151149
static inline bool IsBlank(int c) {
152150
return c == ' ' || c == '\t';
153151
}
@@ -345,9 +343,8 @@ static const char *FindIncludePath(const char *map, size_t mapsize,
345343

346344
// scan backwards for hash character
347345
for (;;) {
348-
if (q == map) {
346+
if (q == map)
349347
return 0;
350-
}
351348
if (IsBlank(q[-1])) {
352349
--q;
353350
continue;
@@ -414,17 +411,15 @@ static void LoadRelationships(int argc, char *argv[]) {
414411
static char srcdirbuf[PATH_MAX];
415412
const char *p, *pe, *src, *path, *pathend, *srcdir, *final;
416413
getargs_init(&ga, argv + optind);
417-
while ((src = getargs_next(&ga))) {
414+
while ((src = getargs_next(&ga)))
418415
CreateSourceId(src);
419-
}
420416
getargs_destroy(&ga);
421417
getargs_init(&ga, argv + optind);
422418
while ((src = getargs_next(&ga))) {
423419
is_assembly = endswith(src, ".s");
424420
srcid = GetSourceId(src);
425-
if (strlcpy(srcdirbuf, src, PATH_MAX) >= PATH_MAX) {
421+
if (strlcpy(srcdirbuf, src, PATH_MAX) >= PATH_MAX)
426422
DiePathTooLong(src);
427-
}
428423
srcdir = dirname(srcdirbuf);
429424
if ((fd = open(src, O_RDONLY)) == -1) {
430425
if (errno == ENOENT && ga.path) {
@@ -438,17 +433,14 @@ static void LoadRelationships(int argc, char *argv[]) {
438433
}
439434
DieSys(src);
440435
}
441-
if ((rc = lseek(fd, 0, SEEK_END)) == -1) {
436+
if ((rc = lseek(fd, 0, SEEK_END)) == -1)
442437
DieSys(src);
443-
}
444438
if ((size = rc)) {
445439
// repeatedly map to same fixed address so in order to weasel out
446440
// of incurring the additional overhead of all these munmap calls
447-
map = mmap((void *)0x311987030000, size, PROT_READ,
448-
MAP_SHARED | MAP_FIXED, fd, 0);
449-
if (map == MAP_FAILED) {
441+
map = mmap(0, size, PROT_READ, MAP_SHARED, fd, 0);
442+
if (map == MAP_FAILED)
450443
DieSys(src);
451-
}
452444
for (p = map, pe = map + size; p < pe; ++p) {
453445
if (!(p = memmem(p, pe - p, "include ", 8)))
454446
break;
@@ -477,12 +469,10 @@ static void LoadRelationships(int argc, char *argv[]) {
477469
dependency = -1;
478470
for (long i = 0; i < systempaths.n; ++i) {
479471
if (!(final =
480-
__join_paths(juf, PATH_MAX, systempaths.p[i], incpath))) {
472+
__join_paths(juf, PATH_MAX, systempaths.p[i], incpath)))
481473
DiePathTooLong(incpath);
482-
}
483-
if ((dependency = GetSourceId(final)) != -1) {
474+
if ((dependency = GetSourceId(final)) != -1)
484475
break;
485-
}
486476
}
487477
if (dependency != -1) {
488478
AppendEdge(&edges, dependency, srcid);
@@ -506,9 +496,8 @@ static void LoadRelationships(int argc, char *argv[]) {
506496
dependency = GetSourceId((final = incpath));
507497
// let foo/bar.c say `#include "hdr.h"`
508498
if (dependency == -1 && !strchr(final, '/')) {
509-
if (!(final = __join_paths(juf, PATH_MAX, srcdir, final))) {
499+
if (!(final = __join_paths(juf, PATH_MAX, srcdir, final)))
510500
DiePathTooLong(incpath);
511-
}
512501
dependency = GetSourceId(final);
513502
}
514503
if (dependency == -1) {
@@ -526,10 +515,11 @@ static void LoadRelationships(int argc, char *argv[]) {
526515
p = pathend + 1;
527516
}
528517
}
518+
if (munmap(map, size))
519+
DieSys(src);
529520
}
530-
if (close(fd)) {
521+
if (close(fd))
531522
DieSys(src);
532-
}
533523
}
534524
getargs_destroy(&ga);
535525
}
@@ -540,9 +530,8 @@ static wontreturn void ShowUsage(int rc, int fd) {
540530
}
541531

542532
static void AddPath(struct Paths *paths, const char *path) {
543-
if (paths->n == ARRAYLEN(paths->p)) {
533+
if (paths->n == ARRAYLEN(paths->p))
544534
Die("too many path arguments");
545-
}
546535
paths->p[paths->n++] = path;
547536
}
548537

@@ -557,21 +546,18 @@ static void GetOpts(int argc, char *argv[]) {
557546
AddPath(&systempaths, optarg);
558547
break;
559548
case 'o':
560-
if (outpath) {
549+
if (outpath)
561550
Die("multiple output paths specified");
562-
}
563551
outpath = optarg;
564552
break;
565553
case 'r':
566-
if (buildroot) {
554+
if (buildroot)
567555
Die("multiple build roots specified");
568-
}
569556
buildroot = optarg;
570557
break;
571558
case 'g':
572-
if (genroot) {
559+
if (genroot)
573560
Die("multiple generated roots specified");
574-
}
575561
genroot = optarg;
576562
break;
577563
case 'n':
@@ -582,31 +568,24 @@ static void GetOpts(int argc, char *argv[]) {
582568
ShowUsage(1, 2);
583569
}
584570
}
585-
if (optind == argc) {
571+
if (optind == argc)
586572
Die("missing input argument");
587-
}
588-
if (!genroot) {
573+
if (!genroot)
589574
genroot = "o/";
590-
}
591-
if (!endswith(genroot, "/")) {
575+
if (!endswith(genroot, "/"))
592576
Die("generated output path must end with slash");
593-
}
594-
if (!buildroot) {
577+
if (!buildroot)
595578
Die("need build output path");
596-
}
597-
if (!endswith(buildroot, "/")) {
579+
if (!endswith(buildroot, "/"))
598580
Die("build output path must end with slash");
599-
}
600-
if (!startswith(buildroot, genroot)) {
581+
if (!startswith(buildroot, genroot))
601582
Die("build output path must start with generated output path");
602-
}
603583
if (!systempaths.n && hermetic) {
604584
AddPath(&systempaths, "third_party/libcxx/include/");
605585
AddPath(&systempaths, "libc/isystem/");
606586
}
607-
if (systempaths.n && !hermetic) {
587+
if (systempaths.n && !hermetic)
608588
Die("system path can only be specified in hermetic mode");
609-
}
610589
long j = 0;
611590
for (long i = 0; i < systempaths.n; ++i) {
612591
size_t n;
@@ -619,21 +598,18 @@ static void GetOpts(int argc, char *argv[]) {
619598
DieSys(path);
620599
}
621600
}
622-
if ((n = strlen(path)) >= PATH_MAX) {
601+
if ((n = strlen(path)) >= PATH_MAX)
623602
DiePathTooLong(path);
624-
}
625-
if (!n || path[n - 1] != '/') {
603+
if (!n || path[n - 1] != '/')
626604
Die("system path must end with slash");
627-
}
628605
}
629606
systempaths.n = j;
630607
}
631608

632609
static const char *StripExt(char pathbuf[hasatleast PATH_MAX], const char *s) {
633610
static char *dot;
634-
if (strlcpy(pathbuf, s, PATH_MAX) >= PATH_MAX) {
611+
if (strlcpy(pathbuf, s, PATH_MAX) >= PATH_MAX)
635612
DiePathTooLong(s);
636-
}
637613
dot = strrchr(pathbuf, '.');
638614
if (dot)
639615
*dot = '\0';
@@ -661,13 +637,10 @@ static uint32_t GetFileExtension(const char *s) {
661637
static bool IsObjectSource(const char *name) {
662638
int i;
663639
uint32_t ext;
664-
if ((ext = GetFileExtension(name))) {
665-
for (i = 0; i < ARRAYLEN(kSourceExts); ++i) {
666-
if (ext == kSourceExts[i]) {
640+
if ((ext = GetFileExtension(name)))
641+
for (i = 0; i < ARRAYLEN(kSourceExts); ++i)
642+
if (ext == kSourceExts[i])
667643
return true;
668-
}
669-
}
670-
}
671644
return false;
672645
}
673646

@@ -736,22 +709,18 @@ int main(int argc, char *argv[]) {
736709
LoadRelationships(argc, argv);
737710
Crunch();
738711
makefile = Explore();
739-
if (outpath &&
740-
(fd = open(outpath, O_WRONLY | O_CREAT | O_TRUNC, 0644)) == -1) {
712+
if (outpath && (fd = open(outpath, O_WRONLY | O_CREAT | O_TRUNC, 0644)) == -1)
741713
DieSys(outpath);
742-
}
743714
n = appendz(makefile).i;
744-
for (i = 0; i < n; i += (size_t)rc) {
745-
if ((rc = write(fd, makefile + i, n - i)) == -1) {
715+
for (i = 0; i < n; i += (size_t)rc)
716+
if ((rc = write(fd, makefile + i, n - i)) == -1)
746717
DieSys(outpath);
747-
}
748-
}
749-
if (outpath && close(fd)) {
718+
if (outpath && close(fd))
750719
DieSys(outpath);
751-
}
752720
free(makefile);
753721
free(edges.p);
754722
free(sauces);
755723
free(names);
724+
CheckForMemoryLeaks();
756725
return 0;
757726
}

0 commit comments

Comments
 (0)