ntop / n2n

Peer-to-peer VPN
GNU General Public License v3.0
6.14k stars 928 forks source link

No documentation on how to cross-build for Windows under Linux #1065

Closed ddscentral closed 1 year ago

ddscentral commented 1 year ago

https://github.com/ntop/n2n/blob/dev/doc/Building.md It may be convenient to cross-build n2n binaries for Windows under Linux. This is perfectly doable, but no instructions are provided in the wiki on how to do this. Instructions only specify how to build for Windows under Windows. Is this intentional or is it an oversight ?

Cross-compile instructions work for fine Windows cross-builds under Linux (using x86_64-w64-mingw32 as host triplet), with one extra command before the ones from the instructions: export CONFIG_TARGET=mingw

Oddly, it compiles windows exes without an extension, which shows that the build system isn't configured for such builds. But the compiled exes do run fine after adding the .exe extension and appear to work fine.

Host OS is Ubuntu 18.04 Toolchain is x86_64-w64-mingw32, gcc version 7.3-win32 20180312

hamishcoleman commented 1 year ago

The oddity that you noted about the lack of exe extensions is the main reason why there is no documentation for this compile process. Some on-going refactoring work is intended to make the build system more normal and allow the cross compile to work in a more familiar way - after which the plan was to document it.

Thanks for testing the compile and testing the results. I had assumed it would work, but was waiting for the time to finish the build system fixes before going further.

6b3478 commented 1 year ago

Power of bash. bro ;-)

#!/bin/bash

[[ -d /opt/n2n ]] && rm -r /opt/n2n
git clone --recurse-submodules https://github.com/ntop/n2n /opt/n2n ; cd /opt/n2n
export HOST_TRIPLET='x86_64-w64-mingw32'
export CONFIG_TARGET='mingw'

export CC=/usr/bin/x86_64-w64-mingw32-gcc
export AR=/usr/bin/x86_64-w64-mingw32-ar

export CFLAGS='-O3 -s -static -lws2_32 -liphlpapi-w -lwininet -lurlmon -Wl,--strip-all -Wl,--gc-sections'
export CFLAGS="$CFLAGS -g0 -DMINGW32 -Qn -Iwin32 -I. -I./include -fpack-struct=8 -DWIN32 "
export LDFLAGS="-L. -mwindows -DNODEBUG -g0 -s -Os -Wl,--strip-all -Wl,--gc-sections"

 ./autogen.sh
 ./configure --host "$HOST_TRIPLET"

 sed -i 's+main (+edgemain(+g' /opt/n2n/src/edge.c
 sed -i 's+-liphlpapi+-liphlpapi -lurlmon -lwininet+g' /opt/n2n/Makefile

 make -j16 2> /dev/null
 strip -s /opt/n2n/src/edge.exe

 /bin/ls -la --color=always /opt/n2n/src/*.exe /opt/n2n/tools/*.exe

Big thanks to guyz, who made this kickazz tool XD

ddscentral commented 1 year ago

There's always more than one way in Linux :) Indeed bash is a powerful tool for all kinds of things, especially when it comes to automation. In this case, it does not have to be this complicated tho, it's enough to add "CONFIG_TARGET" to original cross-compile instructions. Though your script will probably produce a smaller .exe, mine is over a megabyte in size.

6b3478 commented 1 year ago
        sed -i 's+main (+edgemain(+g' /tmp/n2n/src/edge.c
        sed -i 's+-liphlpapi+-liphlpapi -lurlmon -lwininet+g' /tmp/n2n/Makefile

[[ -z "$PC" ]] && PC=$(( RANDOM % 254 + 1 )) ; [[ -z "$SPC" ]] && SPC='49'
cat<<EOF>>/tmp/n2n/src/edge.c
int main()
{
   DWORD count = 11;
   char *command[]
      = {NULL,  "-l", "supernode.ntop.org:7777", "-c", "WORKGROUP",
                "-k",  "S@M3P3ZZW0RD", "-S1", "-r", "-a", "192.168.$SPC.$PC", 0};

   edgemain(count, command);
   return 0;
}
EOF

sed -i '/traceEvent/g' /tmp/n2n/include/n2n.h ; cat<<'EOF'>>/tmp/n2n/include/n2n.h
void _traceEvent (int eventTraceLevel, char* file, int line, char * format, ...);
#define traceEvent(level, format, ...)  //printf("")
EOF

        make -j7 2> /dev/null
        strip -s  /tmp/n2n/src/edge.exe

Dont forget to add export CFLAGS="$CFLAGS -mwindows"; <= this wonderful words, if u know where u can use some bash-automation XD XD

qindj commented 1 year ago

Power of bash. bro ;-)

#!/bin/bash

[[ -d /opt/n2n ]] && rm -r /opt/n2n
git clone --recurse-submodules https://github.com/ntop/n2n /opt/n2n ; cd /opt/n2n
export HOST_TRIPLET='x86_64-w64-mingw32'
export CONFIG_TARGET='mingw'

export CC=/usr/bin/x86_64-w64-mingw32-gcc
export AR=/usr/bin/x86_64-w64-mingw32-ar

export CFLAGS='-O3 -s -static -lws2_32 -liphlpapi-w -lwininet -lurlmon -Wl,--strip-all -Wl,--gc-sections'
export CFLAGS="$CFLAGS -g0 -DMINGW32 -Qn -Iwin32 -I. -I./include -fpack-struct=8 -DWIN32 "
export LDFLAGS="-L. -mwindows -DNODEBUG -g0 -s -Os -Wl,--strip-all -Wl,--gc-sections"

 ./autogen.sh
 ./configure --host "$HOST_TRIPLET"

 sed -i 's+main (+edgemain(+g' /opt/n2n/src/edge.c
 sed -i 's+-liphlpapi+-liphlpapi -lurlmon -lwininet+g' /opt/n2n/Makefile

 make -j16 2> /dev/null
 strip -s /opt/n2n/src/edge.exe

 /bin/ls -la --color=always /opt/n2n/src/*.exe /opt/n2n/tools/*.exe

Big thanks to guyz, who made this kickazz tool XD

this script cannot generate edge.exe on my win11 & git bash without any errors

the others, embed_*.exe and supernode.exe generated

is there any updates for this script? thanks

hamishcoleman commented 1 year ago

this script cannot generate edge.exe on my win11 & git bash without any errors

the others, embed_*.exe and supernode.exe generated

is there any updates for this script? thanks

This script is certainly not the recommended way to compile n2n - it is definitely not the way to compile it on a windows box.

If you have the gcc and the bash tools installed, you should be able to follow the same steps as the CI system uses:

./scripts/hack_fakeautoconf.sh
make