Skip to content

Commit

Permalink
Added t8114 - test tup.getvariantdir() in an output in Lua.
Browse files Browse the repository at this point in the history
This is the same as t8109, but now in Lua. The Lua parser now also uses
a node variable for the variant directory. Ideally both the Lua parser
and regular parser would use the same underlying functions for this.
  • Loading branch information
gittup committed Oct 25, 2023
1 parent 7da0d88 commit 41d1302
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 28 deletions.
15 changes: 4 additions & 11 deletions src/tup/luaparser.c
Original file line number Diff line number Diff line change
Expand Up @@ -334,20 +334,13 @@ static int tuplua_function_getcwd(lua_State *ls)
static int tuplua_function_getvariantdir(lua_State *ls)
{
struct tupfile *tf = top_tupfile();
struct estring e;

lua_settop(ls, 0);

estring_init(&e);
if(get_relative_dir(NULL, &e, tf->srctent->tnode.tupid, tf->curtent->tnode.tupid) < 0) {
fprintf(tf->f, "tup internal error: Unable to find relative directory from ID %lli -> %lli\n", tf->srctent->tnode.tupid, tf->curtent->tnode.tupid);
tup_db_print(tf->f, tf->srctent->tnode.tupid);
tup_db_print(tf->f, tf->curtent->tnode.tupid);
return luaL_error(ls, "Failed to get directory path length in getcwd.");
}

lua_pushlstring(ls, e.s, e.len);
free(e.s);
char value[32];
snprintf(value, 31, "%%%llit", tf->curtent->tnode.tupid);
value[31] = 0;
lua_pushlstring(ls, value, strlen(value));
return 1;
}

Expand Down
46 changes: 29 additions & 17 deletions src/tup/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -4062,7 +4062,10 @@ static char *tup_printf(struct tupfile *tf, const char *cmd, int cmd_len,
fprintf(tf->f, "tup error: Failed to run strtol on %%-flag with a number.\n");
return NULL;
}
if(num <= 0 || num >= 99) {
/* Bounds-check the %-flag number, except for node
* references (%Nt) which use an internal tup id.
*/
if((num <= 0 || num >= 99) && *endp != 't') {
fprintf(tf->f, "tup error: Expected number from 1-99 (base 10) for %%-flag, but got %i\n", num);
return NULL;
}
Expand All @@ -4079,27 +4082,36 @@ static char *tup_printf(struct tupfile *tf, const char *cmd, int cmd_len,
return NULL;
}
tmpnl = ooinput_nl;
} else if(*endp == 't') {
/* Skip node references here, they are
* resolved in eval(). Just store the %Nt
* string back in the output.
*/
estring_append(&e, "%%", 1);
estring_append(&e, next, endp-next + 1);
} else {
fprintf(tf->f, "tup error: Expected 'f', 'b', 'B', 'o', or 'i' after number in %%%i-flag, but got '%c'\n", num, *endp);
return NULL;
}
TAILQ_FOREACH(nle, &tmpnl->entries, list) {
if(nle->orderid == num) {
if(!first && estring_append(&e, " ", 1) < 0)
return NULL;
int err;
if(*endp == 'B') {
err = estring_append(&e, nle->base, nle->extlessbaselen);
} else if(*endp == 'b') {
err = estring_append(&e, nle->base, nle->baselen);
} else {
err = estring_append(&e, nle->path, nle->len);
if(tmpnl) {
TAILQ_FOREACH(nle, &tmpnl->entries, list) {
if(nle->orderid == num) {
if(!first && estring_append(&e, " ", 1) < 0)
return NULL;
int err;
if(*endp == 'B') {
err = estring_append(&e, nle->base, nle->extlessbaselen);
} else if(*endp == 'b') {
err = estring_append(&e, nle->base, nle->baselen);
} else {
err = estring_append(&e, nle->path, nle->len);
}
if(err < 0)
return NULL;
first = 0;
} else if(nle->orderid > num) {
break;
}
if(err < 0)
return NULL;
first = 0;
} else if(nle->orderid > num) {
break;
}
}

Expand Down
49 changes: 49 additions & 0 deletions test/t8114-tup-variantdir-output-lua.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#! /bin/sh -e
# tup - A file-based build system
#
# Copyright (C) 2023 Mike Shal <[email protected]>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

# Same as t8109 but in lua.
. ./tup.sh

cat > Tuprules.lua << HERE
LIBS_DIR = tup.getvariantdir() .. '/libs'
HERE

mkdir lib
cat > lib/Tupfile.lua << HERE
tup.foreach_rule('*.c', 'gcc -c %f -o %o', '%B.o')
tup.rule('*.o', 'ar crs %o %f', {LIBS_DIR .. '/libfoo.a', extra_outputs={LIBS_DIR .. '/<libs>'}})
HERE

mkdir sub
cat > sub/Tupfile.lua << HERE
tup.foreach_rule('*.c', 'gcc -c %f -o %o', '%B.o')
tup.rule({'*.o', extra_inputs={LIBS_DIR .. '/<libs>'}}, 'gcc %f -L' .. LIBS_DIR .. ' -lfoo -o %o', 'main.exe')
HERE

echo 'int foo(void) {return 7;}' > lib/foo.c
cat > sub/main.c << HERE
int foo(void);
int main(void) {return foo();}
HERE
update

mkdir build
touch build/tup.config
update

eotup

0 comments on commit 41d1302

Please sign in to comment.