diff options
author | John Ankarstrom <john@ankarstrom.se> | 2021-07-06 21:15:06 +0200 |
---|---|---|
committer | John Ankarstrom <john@ankarstrom.se> | 2021-07-06 21:15:16 +0200 |
commit | dcb7758a74fb5ff4f8b6eecb6589223a408f3f9b (patch) | |
tree | a0a3e7a1d56656a90de64b96662a5a3786795df7 | |
parent | f6e8218a332933d1a6c34421c8a1638cd726c7b4 (diff) | |
download | build-dcb7758a74fb5ff4f8b6eecb6589223a408f3f9b.tar.gz |
Rewrite in C
-rw-r--r-- | Makefile | 1 | ||||
-rwxr-xr-x | build | 103 | ||||
-rw-r--r-- | build.c | 125 |
3 files changed, 126 insertions, 103 deletions
diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..5b08578 --- /dev/null +++ b/Makefile @@ -0,0 +1 @@ +build: build.c @@ -1,103 +0,0 @@ -#!/usr/bin/perl - -# build -- process and act on embedded build instructions - -use strict; -use warnings; - -# parse arguments - -my ($debug, $fork) = (0, 0); -my $usage = "usage: $0 [-dddf] file [...]\n"; - -while (@ARGV and ($_ = $ARGV[0]) =~ /^-/) { - shift @ARGV; - last if $_ eq '--'; - $debug++ for /(d)/g; - $fork++ if /f/; - die $usage if /[^-df]/; -} - -die $usage if not @ARGV; - -# find build line(s) in each file - -my $i; -for my $file (@ARGV) { - my ($cmd, $target, @deps); - open my $f, '<', $file or die "could not open $file: $!\n"; - $i = 1; - while (<$f>) { - if (not $cmd and m{ - ^ .* - (?: (?:build|generate) \s+ (?:it \s+)? with - | (?:built|generated) \s+ with - | the \s+ command - | run(?:ning)? - | issu(?:e|ing) - ) \s+ - (.+?) \s* (?: > \s* (.+?))? \.? \s* - (?:\*/)? \s* $ - }x) { - warn "$file build line: $_" if $debug > 2; - $cmd = $1; - $target = $2; - } - elsif (not @deps and m{ - ^ .* - (?: depends \s+ on - | dependent \s+ on - ) \s+ - (?: the \s+ files? \s+ )? - (.+?) \.? \s* - (?:\*/)? \s* $ - }x) { - warn "$file dependency line: $_" if $debug > 2; - push @deps, $_ for split /,?\s+/, $1; - if ($deps[-2] eq 'and') { - $deps[-2] = $deps[-1]; - pop @deps; - } - } - last if ++$i > 20; - } - warn "$file dependencies: @deps\n" if $debug > 1; - build($file, $cmd, $target, @deps) if $cmd; - warn "$file: no build line found\n" if not $cmd; -} - -# build target according to build line - -sub build { - my ($source, $cmd, $target, @deps) = @_; - my $m = (stat$target)[9]; - - if (! -e $target) { - warn "$source: building because $target does not exist\n" - if $debug; - goto update; - } - - for ($source, @deps) { - if (! -e $_) { - warn "$source: non-existent dependency '$_'\n"; - next; - } - if ($m < (stat$_)[9]) { - warn "$source: building because $_ is modified\n" - if $debug; - goto update; - } - } - warn "$source: $target already up-to-date\n"; - return; - -update: - warn "$source: $cmd > $target\n"; - if ($fork) { - exec('/bin/sh', '-c', "$cmd > $target") if fork() == 0; - } else { - system("$cmd > $target"); - } - return; -} @@ -0,0 +1,125 @@ +#include <ctype.h> +#include <err.h> +#include <stdlib.h> +#include <stdio.h> +#include <string.h> +#include <sys/stat.h> + +#define USAGE "usage: %s file [...]\n", argv[0] + +int +main(int argc, char *argv[]) +{ + char *b, *bb, *bbb, buf[1024], *cmd[32], dep[1024], *t, *tt, tgt[64]; + FILE *fp; + int i, icmd, j, max; + struct stat sb, ssb; + + tgt[0] = dep[0] = 0; + + /* process arguments */ + if (argc-1 == 0) { + fprintf(stderr, USAGE); + return 1; + } + + /* allocate memory */ + for (i = 0; i < 32; i++) { + cmd[i] = malloc(1024*sizeof(char)); + if (!cmd[i]) err(1, "malloc"); + } + + /* process each file */ + for (i = 1, icmd = 0; i < argc; i++, icmd = 0) { + fp = fopen(argv[i], "r"); + if (!fp) err(1, "fopen"); + + /* read line by line */ + while (fgets(buf, sizeof(buf), fp)) { + buf[strcspn(buf, "\n")] = 0; + + /* parse line */ + b = buf; + for (b++; *b; b++) { + if (!(*(b+1) && *(b+2) && *(b+3))) continue; + if (!(*b == ' ' || *b == ' ')) continue; + + /* command line */ + if (*(b+1) == '$' && *(b+2) == ' ') { + strncpy(cmd[icmd++],b+3,sizeof(buf)-1); + + /* find target */ + for (b = b+3; *b; b++) { + if (!(*b+1)) continue; + if (*b != '>') continue; + strncpy(tgt,b+1,sizeof(tgt)-1); + + } + continue; + } + + /* depend line */ + if (*(b+1) == '%' && *(b+2) == ' ') { + strncpy(dep, b+3, sizeof(dep)-1); + continue; + } + } + } + + if (!icmd) { + fprintf(stderr, "%s: no command line found\n", argv[i]); + continue; + } + + if (!*tgt) + goto build; + + /* compare source > target */ + for (t = tgt; *t; t++) + if (!isspace(*t)) break; + for (tt = tgt; *tt; tt++) + if (isspace(*tt)) { *tt = 0; break; } + if (stat(t, &ssb)) + err(1, t); + if (stat(argv[i], &sb)) + err(1, argv[i]); + if (sb.st_mtime > ssb.st_mtime) + goto build; + + /* compare dependencies > target */ + if (*dep) { + for (t = dep; *t; t++) + if (!isspace(*t)) break; + for (j = 0; t[j]; j++) { + if (isspace(t[j]) || t[j+1] == 0) { + tt = strdup(t); + tt[j+(t[j+1]==0)] = 0; + + if (stat(tt, &sb)) + err(1, tt); + free(tt); + if (sb.st_mtime > ssb.st_mtime) + goto build; + + for (; t[j]; j++) + if (!isspace(t[j])) break; + t += j; + j = 0; + } + } + } + + fprintf(stderr, "%s: already up-to-date\n", argv[i]); + goto done; + +build: + /* run commands */ + for (j = 0; j < icmd; j++) { + fprintf(stderr, "%s: %s\n", argv[i], cmd[j]); + system(cmd[j]); + } + +done: + fclose(fp); + } +} |