Instructions how to build native sed.exe with Microsoft Visual Studio (or Build Tools for Visual Studio) from CYGWIN shell. While building, it is possible to save build log and create a patch - for compiling sed.exe using only Microsoft Visual Studio tools, without need for CYGWIN. This is how sed-4.9-build-VS22-x64.patch and sed-4.9-build-VS22-x86.patch were created. (sed-4.9-build-VS9-x86.patch was created analogously in Microsoft Visual Studio 2008 environment) From CYGWIN shell: 1) get archive: wget https://ftp.gnu.org/gnu/sed/sed-4.9.tar.xz 2) unpack archive: tar xf sed-4.9.tar.xz 3) go to sed sources: cd sed-4.9 4) patch the sources: patch -Np1 -i sed-4.9-src.patch (patch location: https://raw.githubusercontent.com/mbuilov/sed-windows/master/sed-4.9-src.patch) 5) now start dos prompt: cmd /c "start cmd.exe" 6) setup compiler: "C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Auxiliary\Build\vcvarsall.bat" amd64 (if Microsoft Build Tools for Visual Studio 2022 is installed in "C:\Program Files (x86)") --tip: to build 32-bit version of the Gnu Sed utility, specify "x86" instead of "amd64" --tip: for Visual Studio 2008: "C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\vcvarsall.bat" x86 7) run bash from dos prompt (with environment prepared for compiling with Microsoft Build Tools for Visual Studio 2022): bash 8) check that Gnu Make is installed in CYGWIN and it is working (Gnu Make is required for the build): make --version 9) configure sed: - for 64-bit build: CFLAGS= \ CC="cl /Dssize_t=__int64" \ CPP="cl /Dssize_t=__int64 /E" \ LD=lib ARFLAGS="/OUT:" ./configure --enable-threads=windows --disable-dependency-tracking --disable-silent-rules --disable-nls gl_cv_sys_struct_lconv_ok=yes gt_cv_ssize_t=yes - for 32-bit build: CFLAGS= CC=cl CPP="cl /E" LD=lib ARFLAGS="/OUT:" ./configure --enable-threads=windows --disable-dependency-tracking --disable-silent-rules --disable-nls gl_cv_sys_struct_lconv_ok=yes --tip: for Visual Studio 2008, add "/D_WIN32_WINNT=0x501 /D__bool_true_false_are_defined /Dbool=int /Dtrue=1 /Dfalse=0" compiler switches: CFLAGS= \ CC="cl /D_WIN32_WINNT=0x501 /D__bool_true_false_are_defined /Dbool=int /Dtrue=1 /Dfalse=0" \ CPP="cl /D_WIN32_WINNT=0x501 /D__bool_true_false_are_defined /Dbool=int /Dtrue=1 /Dfalse=0 /E" \ LD=lib ARFLAGS="/OUT:" ./configure --enable-threads=windows --disable-dependency-tracking --disable-silent-rules --disable-nls gl_cv_sys_struct_lconv_ok=yes 10) configure script is not fully functional to create correct Makefiles - they are need to be fixed manually: sed -i '/^SUBDIRS/s/=.*/= ./' Makefile sed -i '/^NEXT/s/=.*/=""/' Makefile sed -i '/lib_libsed_a_AR/s/ lib/lib/' Makefile sed -i '/sed_libver_a_AR/s/ sed/sed/' Makefile sed -i 's@^AR = .*@AR = lib /nologo@' Makefile sed -i 's/-c -o /-c -nologo -Fo/' Makefile sed -i '/RANLIB/d' Makefile sed -i '/^sed_sed_LINK/{s/.*/sed_sed_LINK = $(LINK)/;n;d}' Makefile 11) fix linker command, add support for wildcards in program arguments: sed -i 's%^LINK = .*%LINK = link /nologo /SUBSYSTEM:CONSOLE /OUT:$@ /DEFAULTLIB:Advapi32.lib /DEFAULTLIB:LIBCPMT.lib wsetargv.obj%' ./Makefile --tip: for Visual Studio 2008, specify "/SUBSYSTEM:CONSOLE,5.01" linker switch: sed -i 's%^LINK = .*%LINK = link /nologo /SUBSYSTEM:CONSOLE,5.01 /OUT:$@ /DEFAULTLIB:Advapi32.lib /DEFAULTLIB:LIBCPMT.lib wsetargv.obj%' ./Makefile 12) add gen target: echo 'gen: $(BUILT_SOURCES)' >> Makefile 13) execute Makefile to generate sed headers: make gen 14) some references to system header files are missing in generated headers - they need to be fixed manually. a) set paths to locations of Build Tools for Visual Studio 2022 and WDK10 in sed-4.9-win-headers.patch (assume it was copied to the current directory): sed -i '/Visual Studio/s@.:.*include@'"$(env | sed -n 's@\\@\\\\@g;/^INCLUDE/s@.*[=;]\([^=;]*\\\\VC\\\\[^=;]*[0-9]\\\\include\).*$@\1@p')"'@' ./sed-4.9-win-headers.patch sed -i '/Windows Kits/s@.:.*ucrt@'"$(env | sed -n 's@\\@\\\\@g;/^INCLUDE/s@.*[=;]\([^=;]*\\\\ucrt\).*$@\1@p')"'@' ./sed-4.9-win-headers.patch (patch location: https://raw.githubusercontent.com/mbuilov/sed-windows/master/sed-4.9-win-headers.patch) note: sub-command env | sed -n 's@\\@\\\\@g;/^INCLUDE/s@.*[=;]\([^=;]*\\\\VC\\\\[^=;]*[0-9]\\\\include\).*$@\1@p' should extract path to MSVC headers, like: C:\\Program Files (x86)\\Microsoft Visual Studio\\2022\\BuildTools\\VC\\Tools\\MSVC\\14.30.30705\\include and sub-command env | sed -n 's@\\@\\\\@g;/^INCLUDE/s@.*[=;]\([^=;]*\\\\ucrt\).*$@\1@p' should extract path to WDK headers, like: C:\\Program Files (x86)\\Windows Kits\\10\\include\\10.0.22000.0\\ucrt --tip: for Visual Studio 2008: sed -i '/Visual Studio/s@.:.*include@'"$(env | sed -n 's@\\@\\\\@g;/^INCLUDE/s@.*[=;]\([^=;]*\\\\VC\\\\[iI][nN][cC][lL][uU][dD][eE]\).*$@\1@p')"'@' ./sed-4.9-win-headers.patch sed -i '/Windows Kits/s@.:.*ucrt@'"$(env | sed -n 's@\\@\\\\@g;/^INCLUDE/s@.*[=;]\([^=;]*\\\\VC\\\\[iI][nN][cC][lL][uU][dD][eE]\).*$@\1@p')"'@' ./sed-4.9-win-headers.patch note: sub-command env | sed -n 's@\\@\\\\@g;/^INCLUDE/s@.*[=;]\([^=;]*\\\\VC\\\\[iI][nN][cC][lL][uU][dD][eE]\).*$@\1@p' should extract path to MSVC headers, like: C:\\Program Files (x86)\\Microsoft Visual Studio 9.0\\VC\\INCLUDE b) lib/assert.h header file may not be generated if system has builtin static_assert support, fix the patch to avoid patching missing lib/assert.h test -f ./lib/assert.h || sed -i -n '\@diff -Naur .*/assert.h@{:b;n;/diff -Naur/!b b};p' ./sed-4.9-win-headers.patch c) now patch the previously generated headers with updated patch-file: patch -Np1 -i ./sed-4.9-win-headers.patch 15) add missing getopt sources: patch -Np1 -i sed-4.9-getopt.patch (patch location: https://raw.githubusercontent.com/mbuilov/sed-windows/master/sed-4.9-getopt.patch) 16) add rules to make getopt objects: sed -i 's@^gl_LIBOBJS =.*@& lib/getopt.obj lib/getopt1.obj@' Makefile sed -i '$s@.*@&\nlib/libsed_a-getopt.obj: lib/getopt.c\n\t\$(AM_V_CC)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(lib_libsed_a_CFLAGS) $(CFLAGS) -c -nologo -Folib/libsed_a-getopt.obj `if test -f '"'"'lib/getopt.c'"'"'; then $(CYGPATH_W) '"'"'lib/getopt.c'"'"'; else $(CYGPATH_W) '"'"'$(srcdir)/lib/getopt.c'"'"'; fi`@' Makefile sed -i '$s@.*@&\nlib/libsed_a-getopt1.obj: lib/getopt1.c\n\t\$(AM_V_CC)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(lib_libsed_a_CFLAGS) $(CFLAGS) -c -nologo -Folib/libsed_a-getopt.obj `if test -f '"'"'lib/getopt1.c'"'"'; then $(CYGPATH_W) '"'"'lib/getopt1.c'"'"'; else $(CYGPATH_W) '"'"'$(srcdir)/lib/getopt1.c'"'"'; fi`@' Makefile 17) when building with Visual Studio 2008: a) fix sources for compilation with non-c99 compiler: patch -Np1 -i sed-4.9-src-non-c99.patch (patch location: https://raw.githubusercontent.com/mbuilov/sed-windows/master/sed-4.9-src-non-c99.patch) b) stdbool.h header file is missing, add it: echo > stdbool.h c) fix lib/string.h: sed -i 's@\*restrict @*@g' lib/string.h d) fix lib/ctype.h: sed -i -e '/undef isblank/{:r;n;/^#endif/!b r' -e 's@@&\nstatic __inline int isblank(int c) {return '"'"' '"'"' == c || '"'"'\\t'"'"' == c;}@}' lib/ctype.h 18) do compile sed: make > make.bat 19) check build result: ./sed/sed.exe --version --locale=C (should print sed version, e.g.: (GNU sed) 4.9) compilation should be ok, native (unoptimized) ./sed/sed.exe should be created. Now it is possible to create a patch file - for compiling optimized sed.exe using only Microsoft Visual Studio tools, without need for CYGWIN. 1) create directory for unpatched sed: mkdir ../orig 2) unpack sed: tar xf ../sed-4.9.tar.xz -C ../orig/ 3) diff current directory with original just unpacked sed-4.9.tar.xz in the '../orig' directory diff -rql . ../orig/sed-4.9 4) remove unneeded built files in the current directory (object files, libs, etc...) rm -rv \ ./Makefile \ ./stamp-h1 \ ./sed-4.9-win-headers.patch \ ./lib/*.obj \ ./lib/.dirstamp \ ./lib/.deps \ ./lib/glthread/*.obj \ ./lib/glthread/.dirstamp \ ./lib/glthread/.deps \ ./lib/libsed.a \ ./sed/*.obj \ ./sed/.dirstamp \ ./sed/.deps \ ./lib/malloc/*.obj \ ./lib/malloc/.dirstamp \ ./lib/malloc/.deps \ ./sed/sed.exe \ ./sed/libver.a \ ./po/Makefile \ ./po/Makefile.in \ ./po/POTFILES \ ./config.log \ ./config.status \ ./gnulib-tests/Makefile \ ./sed/sed.exp \ ./sed/sed.lib cp -fv \ ../orig/sed-4.9/doc/sed.1 ./doc/sed.1 5) after this, diff should print: diff -rql . ../orig/sed-4.9 Only in .: config.h Only in ./lib: alloca.h # Only in ./lib: assert.h * Files ./lib/calloc.c and ../orig/sed-4.9/lib/calloc.c differ * Files ./lib/canonicalize-lgpl.c and ../orig/sed-4.9/lib/canonicalize-lgpl.c differ Only in ./lib: ctype.h * Files ./lib/dfa.c and ../orig/sed-4.9/lib/dfa.c differ Only in ./lib: errno.h Only in ./lib: fcntl.h Only in ./lib: getopt-cdefs.h Only in ./lib: getopt-core.h Only in ./lib: getopt-ext.h Only in ./lib: getopt-pfx-core.h Only in ./lib: getopt-pfx-ext.h Only in ./lib: getopt.c Only in ./lib: getopt.h Only in ./lib: getopt1.c Only in ./lib: getopt_int.h Only in ./lib: inttypes.h Only in ./lib: langinfo.h Only in ./lib: limits.h Files ./lib/localcharset.c and ../orig/sed-4.9/lib/localcharset.c differ Only in ./lib: locale.h * Files ./lib/localeinfo.c and ../orig/sed-4.9/lib/localeinfo.c differ Only in ./lib/malloc: dynarray-skeleton.gl.h Only in ./lib/malloc: dynarray.gl.h * Files ./lib/malloc/dynarray_emplace_enlarge.c and ../orig/sed-4.9/lib/malloc/dynarray_emplace_enlarge.c differ * Files ./lib/malloc/dynarray_finalize.c and ../orig/sed-4.9/lib/malloc/dynarray_finalize.c differ * Files ./lib/malloc/dynarray_resize.c and ../orig/sed-4.9/lib/malloc/dynarray_resize.c differ * Files ./lib/malloc/dynarray_resize_clear.c and ../orig/sed-4.9/lib/malloc/dynarray_resize_clear.c differ Only in ./lib/malloc: scratch_buffer.gl.h * Files ./lib/malloc/scratch_buffer_set_array_size.c and ../orig/sed-4.9/lib/malloc/scratch_buffer_set_array_size.c differ * Files ./lib/malloc.c and ../orig/sed-4.9/lib/malloc.c differ * Files ./lib/malloca.c and ../orig/sed-4.9/lib/malloca.c differ Files ./lib/mbtowc-lock.h and ../orig/sed-4.9/lib/mbtowc-lock.h differ Files ./lib/nl_langinfo.c and ../orig/sed-4.9/lib/nl_langinfo.c differ * Files ./lib/rawmemchr.c and ../orig/sed-4.9/lib/rawmemchr.c differ * Files ./lib/realloc.c and ../orig/sed-4.9/lib/realloc.c differ * Files ./lib/regcomp.c and ../orig/sed-4.9/lib/regcomp.c differ * Files ./lib/regexec.c and ../orig/sed-4.9/lib/regexec.c differ Only in ./lib: selinux Only in ./lib: stdalign.h Only in ./lib: stdckdint.h Only in ./lib: stddef.h Only in ./lib: stdint.h Only in ./lib: stdio.h Only in ./lib: stdlib.h Only in ./lib: string.h * Files ./lib/strverscmp.c and ../orig/sed-4.9/lib/strverscmp.c differ Only in ./lib: sys * Files ./lib/tempname.c and ../orig/sed-4.9/lib/tempname.c differ Only in ./lib: time.h Only in ./lib: unistd.h Only in ./lib: wchar.h Only in ./lib: wctype.h * Files ./lib/xmalloc.c and ../orig/sed-4.9/lib/xmalloc.c differ Only in .: make.bat * Files ./sed/debug.c and ../orig/sed-4.9/sed/debug.c differ Files ./sed/execute.c and ../orig/sed-4.9/sed/execute.c differ * Files ./sed/regexp.c and ../orig/sed-4.9/sed/regexp.c differ Files ./sed/sed.c and ../orig/sed-4.9/sed/sed.c differ Files ./sed/utils.c and ../orig/sed-4.9/sed/utils.c differ Only in ./sed: version.c Only in ./sed: version.h * Only in .: stdbool.h # -- if ./lib/assert.h was generated * -- if sed-4.9-src-non-c99.patch was applied 6) edit make.bat cp make.bat make.bat.old sed -i -n 's/.*cl /cl /p;/^lib /p;/^link /p' make.bat sed -i 's@-DLOCALEDIR=."/usr/local/share/locale.".*-c -nologo -F@/DLOCALEDIR="" /c /nologo /F@' make.bat sed -i 's@ -c -nologo -F@/c /nologo /F@' make.bat sed -i '/lc-charset-dispatch.c/s@ /nologo @&/DGNULIB_WCHAR_SINGLE_LOCALE=1 @' make.bat sed -i 's@lib/glthread/@lib\\glthread\\@g' make.bat sed -i 's@/Fosed/@/Fosed\\@;s@/Folib/@/Folib\\@' make.bat sed -i 's@`if test -f .@@;s@.; then.*@@' make.bat sed -i 's@`cygpath -w .@@;s@.`@@' make.bat sed -i 's@ lib/@ lib\\@g;s@ sed/@ sed\\@g' make.bat sed -i 's@\\malloc/@\\malloc\\@g' make.bat sed -i 's@-DHAVE_CONFIG_H -I. -I. -I./lib -I./lib -I./sed@/Ox /GF /Gy /GS- /GL /EHsc /DHAVE_CONFIG_H /I. /I.\\lib /I.\\sed@' make.bat sed -i 's@lib /nologo@lib /LTCG /nologo@' make.bat sed -i 's@/OUT:lib/@/OUT:lib\\@;s@/OUT:sed/@/OUT:sed\\@' make.bat sed -i 's@link /nologo@& /LTCG@' make.bat sed -i 's@$@ || exit /b 1@' make.bat rm make.bat.old 7) finally, create a patch: diff -Naur ../orig/sed-4.9 . > ../sed-4.9-build-VS22-x64.patch --tip: for the 32-bit build, use different patch name: diff -Naur ../orig/sed-4.9 . > ../sed-4.9-build-VS22-x86.patch --tip: for Visual Studio 2008: diff -Naur ../orig/sed-4.9 . > ../sed-4.9-build-VS9-x86.patch