Skip to content

Commit 1f2288b

Browse files
committed
Improve backwards compatibility with GNU Make
1 parent fabf7f9 commit 1f2288b

File tree

18 files changed

+412
-96
lines changed

18 files changed

+412
-96
lines changed

Makefile

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -189,13 +189,13 @@ include examples/package/build.mk
189189
#-φ-examples/package/new.sh
190190
include test/test.mk
191191

192-
OBJS = $(foreach x,$(PKGS),$($(x)_OBJS))
193-
SRCS = $(foreach x,$(PKGS),$($(x)_SRCS))
194-
HDRS = $(foreach x,$(PKGS),$($(x)_HDRS))
195-
INCS = $(foreach x,$(PKGS),$($(x)_INCS))
196-
BINS = $(foreach x,$(PKGS),$($(x)_BINS))
197-
TESTS = $(foreach x,$(PKGS),$($(x)_TESTS))
198-
CHECKS = $(foreach x,$(PKGS),$($(x)_CHECKS))
192+
OBJS = $(foreach x,$(PKGS),$($(x)_OBJS))
193+
SRCS := $(foreach x,$(PKGS),$($(x)_SRCS))
194+
HDRS := $(foreach x,$(PKGS),$($(x)_HDRS))
195+
INCS = $(foreach x,$(PKGS),$($(x)_INCS))
196+
BINS = $(foreach x,$(PKGS),$($(x)_BINS))
197+
TESTS = $(foreach x,$(PKGS),$($(x)_TESTS))
198+
CHECKS = $(foreach x,$(PKGS),$($(x)_CHECKS))
199199

200200
bins: $(BINS)
201201
check: $(CHECKS)
@@ -206,11 +206,17 @@ tags: TAGS HTAGS
206206
o/$(MODE)/.x:
207207
@mkdir -p $(@D) && touch $@
208208

209+
ifneq ($(findstring 4.,,$(MAKE_VERSION)),$(MAKE_VERSION))
209210
o/$(MODE)/srcs.txt: o/$(MODE)/.x $(MAKEFILES) $(call uniq,$(foreach x,$(SRCS),$(dir $(x))))
210211
$(file >$@) $(foreach x,$(SRCS),$(file >>$@,$(x)))
211-
212212
o/$(MODE)/hdrs.txt: o/$(MODE)/.x $(MAKEFILES) $(call uniq,$(foreach x,$(HDRS) $(INCS),$(dir $(x))))
213213
$(file >$@) $(foreach x,$(HDRS) $(INCS),$(file >>$@,$(x)))
214+
else
215+
o/$(MODE)/srcs.txt: o/$(MODE)/.x $(MAKEFILES) $(call uniq,$(foreach x,$(SRCS),$(dir $(x))))
216+
$(MAKE) MODE=rel -j8 -pn bopit 2>/dev/null | sed -ne '/^SRCS/ {s/.*:= //;s/ */\n/g;p;q}' >$@
217+
o/$(MODE)/hdrs.txt: o/$(MODE)/.x $(MAKEFILES) $(call uniq,$(foreach x,$(HDRS) $(INCS),$(dir $(x))))
218+
$(MAKE) MODE=rel -j8 -pn bopit 2>/dev/null | sed -ne '/^HDRS/ {s/.*:= //;s/ */\n/g;p;q}' >$@
219+
endif
214220

215221
o/$(MODE)/depend: o/$(MODE)/.x o/$(MODE)/srcs.txt o/$(MODE)/hdrs.txt $(SRCS) $(HDRS) $(INCS)
216222
@$(COMPILE) -AMKDEPS $(MKDEPS) -o $@ -r o/$(MODE)/ o/$(MODE)/srcs.txt o/$(MODE)/hdrs.txt

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,4 @@ find o -name \*.com | xargs ls -rShal | less
7373
| FreeBSD | 12 | 2018 |
7474
| OpenBSD | 6.4 | 2018 |
7575
| NetBSD | 9.1 | 2020 |
76+
| GNU Make | 3.80 | 2010 |

libc/stdio/getdelim.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@
3333
* allocated automatically, also NUL-terminated is guaranteed
3434
* @param n is the capacity of s (in/out)
3535
* @param delim is the stop char (and NUL is implicitly too)
36-
* @return number of bytes read, including delim, excluding NUL, or -1
37-
* w/ errno on EOF or error; see ferror() and feof()
36+
* @return number of bytes read >0, including delim, excluding NUL,
37+
* or -1 w/ errno on EOF or error; see ferror() and feof()
3838
* @note this function can't punt EINTR to caller
39-
* @see getline(), gettok_r()
39+
* @see getline(), chomp(), gettok_r()
4040
*/
4141
ssize_t getdelim(char **s, size_t *n, int delim, FILE *f) {
4242
char *p;

libc/str/strchr.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,14 @@
1717
│ PERFORMANCE OF THIS SOFTWARE. │
1818
╚─────────────────────────────────────────────────────────────────────────────*/
1919
#include "libc/assert.h"
20+
#include "libc/bits/bits.h"
2021
#include "libc/str/str.h"
2122

22-
noasan static const unsigned char *strchr_x64(const unsigned char *p,
23-
uint64_t c) {
23+
noasan static const char *strchr_x64(const char *p, uint64_t c) {
2424
unsigned a, b;
2525
uint64_t w, x, y;
2626
for (c *= 0x0101010101010101;; p += 8) {
27-
w = (uint64_t)p[7] << 070 | (uint64_t)p[6] << 060 | (uint64_t)p[5] << 050 |
28-
(uint64_t)p[4] << 040 | (uint64_t)p[3] << 030 | (uint64_t)p[2] << 020 |
29-
(uint64_t)p[1] << 010 | (uint64_t)p[0] << 000;
27+
w = READ64LE(p);
3028
if ((x = ~(w ^ c) & ((w ^ c) - 0x0101010101010101) & 0x8080808080808080) |
3129
(y = ~w & (w - 0x0101010101010101) & 0x8080808080808080)) {
3230
if (x) {
@@ -63,7 +61,7 @@ char *strchr(const char *s, int c) {
6361
if ((*s & 0xff) == c) return s;
6462
if (!*s) return NULL;
6563
}
66-
r = (char *)strchr_x64((const unsigned char *)s, c);
64+
r = strchr_x64(s, c);
6765
assert(!r || *r || !c);
6866
return r;
6967
}

libc/str/strstr.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
1717
│ PERFORMANCE OF THIS SOFTWARE. │
1818
╚─────────────────────────────────────────────────────────────────────────────*/
19+
#include "libc/bits/safemacros.internal.h"
1920
#include "libc/str/str.h"
2021

2122
/**
@@ -29,6 +30,8 @@
2930
*/
3031
char *strstr(const char *haystack, const char *needle) {
3132
size_t i;
33+
if (!*needle) return haystack;
34+
haystack = firstnonnull(strchr(haystack, *needle), haystack);
3235
for (;;) {
3336
for (i = 0;;) {
3437
if (!needle[i]) return (/*unconst*/ char *)haystack;

libc/str/tprecode16to8.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ static noasan axdx_t tprecode16to8_sse2(char *dst, size_t dstsize,
4949
/**
5050
* Transcodes UTF-16 to UTF-8.
5151
*
52+
* This is a low-level function intended for the core runtime. Use
53+
* utf16toutf8() for a much better API that uses malloc().
54+
*
5255
* @param dst is output buffer
5356
* @param dstsize is bytes in dst
5457
* @param src is NUL-terminated UTF-16 input string

libc/str/tprecode8to16.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ static inline noasan axdx_t tprecode8to16_sse2(char16_t *dst, size_t dstsize,
4646
/**
4747
* Transcodes UTF-8 to UTF-16.
4848
*
49+
* This is a low-level function intended for the core runtime. Use
50+
* utf8toutf16() for a much better API that uses malloc().
51+
*
4952
* @param dst is output buffer
5053
* @param dstsize is shorts in dst
5154
* @param src is NUL-terminated UTF-8 input string

libc/x/utf16toutf8.c

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
2+
│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│
3+
╞══════════════════════════════════════════════════════════════════════════════╡
4+
│ Copyright 2021 Justine Alexandra Roberts Tunney │
5+
│ │
6+
│ Permission to use, copy, modify, and/or distribute this software for │
7+
│ any purpose with or without fee is hereby granted, provided that the │
8+
│ above copyright notice and this permission notice appear in all copies. │
9+
│ │
10+
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
11+
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
12+
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
13+
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
14+
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
15+
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
16+
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
17+
│ PERFORMANCE OF THIS SOFTWARE. │
18+
╚─────────────────────────────────────────────────────────────────────────────*/
19+
#include "libc/bits/bits.h"
20+
#include "libc/intrin/packsswb.h"
21+
#include "libc/intrin/pandn.h"
22+
#include "libc/intrin/pcmpgtb.h"
23+
#include "libc/intrin/pcmpgtw.h"
24+
#include "libc/intrin/pmovmskb.h"
25+
#include "libc/intrin/punpckhbw.h"
26+
#include "libc/intrin/punpcklbw.h"
27+
#include "libc/mem/mem.h"
28+
#include "libc/nexgen32e/bsr.h"
29+
#include "libc/str/str.h"
30+
#include "libc/str/thompike.h"
31+
#include "libc/str/tpenc.h"
32+
#include "libc/str/utf16.h"
33+
#include "libc/x/x.h"
34+
35+
static const int16_t kDel16[8] = {127, 127, 127, 127, 127, 127, 127, 127};
36+
37+
/**
38+
* Transcodes UTF-16 to UTF-8.
39+
*
40+
* @param p is input value
41+
* @param n if -1 implies strlen
42+
* @param z if non-NULL receives output length
43+
*/
44+
char *utf16toutf8(const char16_t *p, size_t n, size_t *z) {
45+
char *r, *q;
46+
wint_t x, y;
47+
unsigned m, j, w;
48+
const char16_t *e;
49+
int16_t v1[8], v2[8], v3[8], vz[8];
50+
if (z) *z = 0;
51+
if (n == -1) n = p ? strlen16(p) : 0;
52+
if ((q = r = malloc(n * 4 + 8 + 1))) {
53+
for (e = p + n; p < e;) {
54+
if (p + 8 < e) { /* 17x ascii */
55+
memset(vz, 0, 16);
56+
do {
57+
memcpy(v1, p, 16);
58+
pcmpgtw(v2, v1, vz);
59+
pcmpgtw(v3, v1, kDel16);
60+
pandn((void *)v2, (void *)v3, (void *)v2);
61+
if (pmovmskb((void *)v2) != 0xFFFF) break;
62+
packsswb((void *)v1, v1, v1);
63+
memcpy(q, v1, 8);
64+
p += 8;
65+
q += 8;
66+
} while (p + 8 < e);
67+
}
68+
x = *p++ & 0xffff;
69+
if (!IsUcs2(x)) {
70+
if (p < e) {
71+
y = *p++ & 0xffff;
72+
x = MergeUtf16(x, y);
73+
} else {
74+
x = 0xFFFD;
75+
}
76+
}
77+
if (x < 0200) {
78+
*q++ = x;
79+
} else {
80+
w = tpenc(x);
81+
WRITE64LE(q, w);
82+
q += bsr(w) >> 3;
83+
q += 1;
84+
}
85+
}
86+
if (z) *z = q - r;
87+
*q++ = '\0';
88+
if ((q = realloc(r, (q - r) * 1))) r = q;
89+
}
90+
return r;
91+
}

libc/x/utf8toutf16.c

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
2+
│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│
3+
╞══════════════════════════════════════════════════════════════════════════════╡
4+
│ Copyright 2021 Justine Alexandra Roberts Tunney │
5+
│ │
6+
│ Permission to use, copy, modify, and/or distribute this software for │
7+
│ any purpose with or without fee is hereby granted, provided that the │
8+
│ above copyright notice and this permission notice appear in all copies. │
9+
│ │
10+
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
11+
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
12+
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
13+
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
14+
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
15+
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
16+
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
17+
│ PERFORMANCE OF THIS SOFTWARE. │
18+
╚─────────────────────────────────────────────────────────────────────────────*/
19+
#include "libc/intrin/pcmpgtb.h"
20+
#include "libc/intrin/pmovmskb.h"
21+
#include "libc/intrin/punpckhbw.h"
22+
#include "libc/intrin/punpcklbw.h"
23+
#include "libc/mem/mem.h"
24+
#include "libc/str/str.h"
25+
#include "libc/str/thompike.h"
26+
#include "libc/str/utf16.h"
27+
#include "libc/x/x.h"
28+
29+
/**
30+
* Transcodes UTF-8 to UTF-16.
31+
*
32+
* @param p is input value
33+
* @param n if -1 implies strlen
34+
* @param z if non-NULL receives output length
35+
*/
36+
char16_t *utf8toutf16(const char *p, size_t n, size_t *z) {
37+
size_t i;
38+
wint_t x, a, b;
39+
char16_t *r, *q;
40+
unsigned m, j, w;
41+
uint8_t v1[16], v2[16], vz[16];
42+
if (z) *z = 0;
43+
if (n == -1) n = p ? strlen(p) : 0;
44+
if ((q = r = malloc(n * sizeof(char16_t) * 2 + sizeof(char16_t)))) {
45+
for (i = 0; i < n;) {
46+
if (i + 16 < n) { /* 34x ascii */
47+
memset(vz, 0, 16);
48+
do {
49+
memcpy(v1, p + i, 16);
50+
pcmpgtb((int8_t *)v2, (int8_t *)v1, (int8_t *)vz);
51+
if (pmovmskb(v2) != 0xFFFF) break;
52+
punpcklbw(v2, v1, vz);
53+
punpckhbw(v1, v1, vz);
54+
memcpy(q + 0, v2, 16);
55+
memcpy(q + 8, v1, 16);
56+
i += 16;
57+
q += 16;
58+
} while (i + 16 < n);
59+
}
60+
x = p[i++] & 0xff;
61+
if (x >= 0300) {
62+
a = ThomPikeByte(x);
63+
m = ThomPikeLen(x) - 1;
64+
if (i + m <= n) {
65+
for (j = 0;;) {
66+
b = p[i + j] & 0xff;
67+
if (!ThomPikeCont(b)) break;
68+
a = ThomPikeMerge(a, b);
69+
if (++j == m) {
70+
x = a;
71+
i += j;
72+
break;
73+
}
74+
}
75+
}
76+
}
77+
w = EncodeUtf16(x);
78+
*q++ = w;
79+
if ((w >>= 16)) *q++ = w;
80+
}
81+
if (z) *z = q - r;
82+
*q++ = '\0';
83+
if ((q = realloc(r, (q - r) * sizeof(char16_t)))) r = q;
84+
}
85+
return r;
86+
}

libc/x/x.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ char *xstrmul(const char *, size_t) paramsnonnull((1)) _XMAL;
5151
char *xinet_ntop(int, const void *) _XPNN _XMAL;
5252
void *xunbinga(size_t, const char16_t *) attributeallocalign((1)) _XMAL _XRET;
5353
void *xunbing(const char16_t *) _XMAL _XRET;
54+
char16_t *utf8toutf16(const char *, size_t, size_t *) nodiscard;
55+
char *utf16toutf8(const char16_t *, size_t, size_t *) nodiscard;
5456

5557
/*───────────────────────────────────────────────────────────────────────────│─╗
5658
│ cosmopolitan § eXtended apis » files ─╬─│┼

0 commit comments

Comments
 (0)