From d3f577b5837199b32be1ee5bb2a0f8c6140cbca7 Mon Sep 17 00:00:00 2001 From: "Avi Halachmi (:avih)" Date: Wed, 4 Oct 2023 23:04:37 +0300 Subject: [PATCH 1/2] windows: msvc: Makefile.wnm: allow generating funcs.h, help.c Previously the mscv Makefile.wnm didn't have rules to generate funcs.h and help.c, meaning that it could only be used to build release tarballs (which already have these files), but not git snapshots. Now we have a new independent rule "generated" (not part of "all"), which generates them. Because typical msvc setups do't have the tools which are used elsewhere to generate these files, this commit adds buildgen.c which, when compiled, can be used instead. It's not part of "all" because when cross compiling, e.g. an arm binary on Windows 64, buildgen.exe should be native and not arm, so this allows making "generated" with a native toolchain, and then "all" using the arm toolchain. --- Makefile.wnm | 19 +++++++++++++++++++ buildgen.c | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 buildgen.c diff --git a/Makefile.wnm b/Makefile.wnm index c8bcce3c..4c7a0cb8 100644 --- a/Makefile.wnm +++ b/Makefile.wnm @@ -1,5 +1,17 @@ # Makefile for less. # Windows 32/64 Visual C++ version +# +# To build in msvc env (VCVARS32.BAT, VCVARS64.BAT, ...) +# +# A release tarball: +# nmake -f Makefile.wnm +# +# A git snapshot: +# nmake -f Makefile.wnm generated +# nmake -f Makefile.wnm +# +# If cross compiling (e.g. arm): "generated" should be made in a native env. +# Else (native build) this works too: nmake -f Makefile.wnm generated less.exe #### Start of system configuration section. #### @@ -45,6 +57,13 @@ defines.h: defines.wn $(OBJ): less.h defines.h funcs.h cmd.h +# generate help.c and funcs.h. We use *.c because we don't have a sources list. +# intentionally detached from $(OBJ) in case of cross compile - needs native cc +generated: + $(CC) buildgen.c + buildgen.exe help < less.hlp > help.c + type *.c 2>NUL | buildgen.exe funcs > funcs.h + clean: -del *.obj -del less.exe diff --git a/buildgen.c b/buildgen.c new file mode 100644 index 00000000..a1755f0b --- /dev/null +++ b/buildgen.c @@ -0,0 +1,36 @@ +#include +#include + +int main(int argc, char **argv) +{ + char *p, buf[1024]; /* enough for the longest prototype at funcs.h */ + int cmd = 0, ok = 0; /* fail if stdin was empty, pipefail-ish */ + + if (argc == 2 && !strcmp(argv[1], "funcs")) + { + for (cmd = 1; fgets(buf, sizeof buf, stdin); ok = 1) + if (!strncmp(buf, "public ", 7) && !strchr(buf, ';')) + printf("%s;", buf); /* "\n;" is ok */ + } + + if (argc == 2 && !strcmp(argv[1], "help")) + { + printf("/* generated by '%s help' from less.hlp */\n", *argv); + puts("#include \"less.h\"\n"); + puts("constant char helpdata[] = {"); + + for (cmd = 1; fgets(buf, sizeof buf, stdin); puts(""), ok = 1) + for (p = buf; *p; ++p) + if (p[0] != '\r' || p[1] != '\n') + printf("%d,", (int)(unsigned char)*p); + + puts("0 };\n"); + puts("constant int size_helpdata = sizeof(helpdata) - 1;"); + } + + if (cmd && ok) + return 0; + fprintf(stderr, !cmd ? "stdin -> stdout: %s MODE (funcs or help)\n" + : "%s: error: empty input\n", *argv); + return 1; +} From 7c53519a4251c18ed82e79de6d1681ebc89ef551 Mon Sep 17 00:00:00 2001 From: "Avi Halachmi (:avih)" Date: Fri, 6 Oct 2023 09:27:17 +0300 Subject: [PATCH 2/2] Windows: mingw: Makefile.wng: allow native funcs.h, help.c We already have buildgen.c which can be used to generate funcs.h and help.c without grep/sed/perl/sh/etc, which can be useful in mingw setups on Windows which don't bring the whole posix arsenal with them. The default is still the posix tools, but now adding WINGEN=1 will use buildgen.c instead of the posix tools. The compiler which is used with buildgen.c is the same compiler used at the rest of Makefile.wng, so it should be native (not cross build). --- Makefile.wng | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/Makefile.wng b/Makefile.wng index c5faa0ce..7d034f8b 100644 --- a/Makefile.wng +++ b/Makefile.wng @@ -25,6 +25,14 @@ # REGEX_PACKAGE == none # This choice disables regex, and instead uses smart plain text search. # +# By default, the files help.c and funcs.h are generated using few posix +# utilities (grep, sed, etc). On Windows, if the tools are missing, add: +# WINGEN=1 +# to compile a native C program which will be used instead of the posix tools. +# NOTE: to cross compile with WINGEN=1, e.g. to arm, first run with native CC: +# make -f Makefile.wng CC=gcc buildgen.exe +# and then compile `less` with the arm CC: +# make -f Makefile.wng WINGEN=1 CC=armv7... #### Start of system configuration section. #### @@ -136,13 +144,25 @@ lessecho.o version.o: less.h defines.h funcs.h defines.h: defines.wn ${CP} $< $@ -funcs.h: ${LESS_SRC} +ifeq (${WINGEN},1) + BUILDGEN = buildgen.exe + MKHELP = buildgen.exe help + MKFUNCS = type 2>NUL ${LESS_SRC} | buildgen.exe funcs +else + MKHELP = (perl mkhelp.pl || sh mkhelp.sh) + MKFUNCS = grep -h "^public [^;]*$$" ${LESS_SRC} | sed "s/$$/;/" +endif + +funcs.h: ${LESS_SRC} ${BUILDGEN} -${CP} funcs.h funcs.h.old - grep -h "^public [^;]*$$" ${LESS_SRC} | sed "s/$$/;/" >funcs.h.tmp + ${MKFUNCS} >funcs.h.tmp ${DIFF} funcs.h.tmp funcs.h || ${MV} funcs.h.tmp funcs.h -help.c: less.hlp - (perl mkhelp.pl || sh mkhelp.sh) < $< > $@ +help.c: less.hlp ${BUILDGEN} + ${MKHELP} < less.hlp > $@ + +buildgen.exe: buildgen.c + ${CC} $< -o $@ ${OBJ}: less.h defines.h funcs.h