Add $(uniq token...) native function to Make

This reduces mono repo `make` latency from 336ms to 226ms.
This commit is contained in:
Justine Tunney 2023-11-30 20:58:36 -08:00
parent 14bf57180f
commit 6556dd2673
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
2 changed files with 46 additions and 3 deletions

View file

@ -16,7 +16,10 @@ LICENSE
LOCAL CHANGES
- Introduce $(uniq token...) native function
- .INTERNET variable to allow internet access
- Remove code that forces slow path if not using /bin/sh
TODO
- .PLEDGE variable which restricts system calls
- .UNVEIL variable which controls Landlock LSM
- .STRICT variable to disable implicit unveiling
@ -28,5 +31,3 @@ LOCAL CHANGES
- .NPROC variable which tunes fork() / clone() limit
- .NOFILE variable which tunes file descriptor limit
- .MAXCORE variable to set upper limit on core dumps
- Do automatic setup and teardown of TMPDIR per rule
- Remove code that forces slow path if not using /bin/sh

View file

@ -21,6 +21,8 @@ this program. If not, see <https://www.gnu.org/licenses/>. */
#include "job.h"
#include "os.h"
#include "commands.h"
#include "libc/mem/critbit0.h"
#include "libc/log/rop.internal.h"
#include "debug.h"
@ -1366,6 +1368,45 @@ func_intcmp (char *o, char **argv, const char *funcname UNUSED)
return o;
}
/*
chop argv[0] into words, and remove duplicates.
*/
static char *
func_uniq (char *o, char **argv, const char *funcname UNUSED)
{
char *p;
size_t len;
int mutated;
bool once = false;
const char *s = argv[0];
struct critbit0 t = {0};
while ((p = find_next_token (&s, &len)))
{
++s;
p[len] = 0;
RETURN_ON_ERROR ((mutated = critbit0_insert (&t, p)));
if (mutated)
{
if (once)
o = variable_buffer_output (o, " ", 1);
else
once = true;
o = variable_buffer_output (o, p, len);
}
}
critbit0_clear (&t);
return o;
OnError:
critbit0_clear (&t);
OSS (error, NILF, "%s: function failed: %s",
"uniq", strerror (errno));
exit (1);
}
/*
$(if condition,true-part[,false-part])
@ -2522,6 +2563,7 @@ static struct function_table_entry function_table_init[] =
FT_ENTRY ("value", 0, 1, 1, func_value),
FT_ENTRY ("eval", 0, 1, 1, func_eval),
FT_ENTRY ("file", 1, 2, 1, func_file),
FT_ENTRY ("uniq", 0, 1, 1, func_uniq),
#ifdef EXPERIMENTAL
FT_ENTRY ("eq", 2, 2, 1, func_eq),
FT_ENTRY ("not", 0, 1, 1, func_not),