philburk / pforth

Portable Forth in C
BSD Zero Clause License
588 stars 94 forks source link

Build and test on Windows #148

Open philburk opened 1 year ago

philburk commented 1 year ago

Test CMake build on Windows. Make sure it can generate an MSVC target.

Originally posted by @philburk in https://github.com/philburk/pforth/issues/141#issuecomment-1374943221

philburk commented 1 year ago

I downloaded cmake for windows from cmake.org

I entered: cmake . and it built VC project files and a PForth.sln.

I entered: msbuild PForth.sln and it could not find msbuild.

I then opened PForth.sln in Visual Studio. It looked like it was building but then VS complained about a stale license and I had to exit.

gwankyun commented 1 year ago

It looked like it was building but then VS complained about a stale license and I had to exit.

You can use the Microsoft C++ Build Tools - Visual Studio without license, see Visual Studio Build Tools Licensing Eased for Open Source C++ Projects -- Visual Studio Magazine.

gwankyun commented 1 year ago

cmake . and it built VC project files and a PForth.sln.

I entered: msbuild PForth.sln and it could not find msbuild.

When you have installed Build Tools and CMake.

cmake .
cmake --build .
philburk commented 1 year ago

@gwankyun - Thanks! I installed the build tools from https://visualstudio.microsoft.com/visual-cpp-build-tools/

I then tried: cmake . cmake --build .

I looked for evidence that "cmake --build ." was actually building pForth but I do not see it. So I tried:

msbuild PForth.sln

It still did not recognize "msbuild".

In case it is not obvious, I am not very familiar with Windows.

Is anyone able to build pForth on Windows using CMake? Where does it put the executable?

znmeb commented 1 year ago

I'm trying it with Build Tools for Visual Studio 2022.

(base) PS C:\Users\znmeb\Projects\pforth> cmake .
-- Selecting Windows SDK version 10.0.22621.0 to target Windows 10.0.22623.
Configuring PForth...
Warning: _CRT_SECURE_NO_WARNINGS
-- Configuring done
-- Generating done
-- Build files have been written to: C:/Users/znmeb/Projects/pforth
(base) PS C:\Users\znmeb\Projects\pforth> cmake --build .
MSBuild version 17.4.1+9a89d02ff for .NET Framework
  Checking Build System
  Building Custom Rule C:/Users/znmeb/Projects/pforth/csrc/CMakeLists.txt
  pf_cglue.c
  pf_clib.c
  pf_core.c
C:\Users\znmeb\Projects\pforth\csrc\pf_core.c(475,32): error C2220: the following warning is treated as an error [C:\Users\znmeb\Projects\pforth
\csrc\PForth_lib.vcxproj]
C:\Users\znmeb\Projects\pforth\csrc\pf_core.c(475,32): warning C4127: conditional expression is constant [C:\Users\znmeb\Projects\pforth\csrc\PF
orth_lib.vcxproj]
C:\Users\znmeb\Projects\pforth\csrc\pf_core.c(479,37): warning C4127: conditional expression is constant [C:\Users\znmeb\Projects\pforth\csrc\PF
orth_lib.vcxproj]
  pf_inner.c
C:\Users\znmeb\Projects\pforth\csrc\pf_inner.c(204,69): error C2220: the following warning is treated as an error [C:\Users\znmeb\Projects\pfort
h\csrc\PForth_lib.vcxproj]
C:\Users\znmeb\Projects\pforth\csrc\pf_inner.c(204,69): warning C4293: '>>': shift count negative or too big, undefined behavior [C:\Users\znmeb
\Projects\pforth\csrc\PForth_lib.vcxproj]
C:\Users\znmeb\Projects\pforth\csrc\pf_inner.c(201,49): warning C4100: 'Hi': unreferenced formal parameter [C:\Users\znmeb\Projects\pforth\csrc\
PForth_lib.vcxproj]
C:\Users\znmeb\Projects\pforth\csrc\pf_inner.c(211,32): warning C4100: 'Lo': unreferenced formal parameter [C:\Users\znmeb\Projects\pforth\csrc\
PForth_lib.vcxproj]
  pf_io.c
  pf_io_none.c
  pf_mem.c
  pf_save.c
C:\Users\znmeb\Projects\pforth\csrc\pf_save.c(79,25): error C2220: the following warning is treated as an error [C:\Users\znmeb\Projects\pforth\
csrc\PForth_lib.vcxproj]
C:\Users\znmeb\Projects\pforth\csrc\pf_save.c(79,25): warning C4127: conditional expression is constant [C:\Users\znmeb\Projects\pforth\csrc\PFo
rth_lib.vcxproj]
C:\Users\znmeb\Projects\pforth\csrc\pf_save.c(110,25): warning C4127: conditional expression is constant [C:\Users\znmeb\Projects\pforth\csrc\PF
orth_lib.vcxproj]
C:\Users\znmeb\Projects\pforth\csrc\pf_save.c(221,25): warning C4127: conditional expression is constant [C:\Users\znmeb\Projects\pforth\csrc\PF
orth_lib.vcxproj]
C:\Users\znmeb\Projects\pforth\csrc\pf_save.c(255,25): warning C4127: conditional expression is constant [C:\Users\znmeb\Projects\pforth\csrc\PF
orth_lib.vcxproj]
C:\Users\znmeb\Projects\pforth\csrc\pf_save.c(410,23): warning C4244: '=': conversion from 'cell_t' to 'uint32_t', possible loss of data [C:\Use
rs\znmeb\Projects\pforth\csrc\PForth_lib.vcxproj]
C:\Users\znmeb\Projects\pforth\csrc\pf_save.c(412,42): warning C4267: '=': conversion from 'size_t' to 'int32_t', possible loss of data [C:\User
s\znmeb\Projects\pforth\csrc\PForth_lib.vcxproj]
C:\Users\znmeb\Projects\pforth\csrc\pf_save.c(413,44): warning C4267: '=': conversion from 'size_t' to 'int32_t', possible loss of data [C:\User
s\znmeb\Projects\pforth\csrc\PForth_lib.vcxproj]
C:\Users\znmeb\Projects\pforth\csrc\pf_save.c(414,27): warning C4244: '=': conversion from 'cell_t' to 'int32_t', possible loss of data [C:\User
s\znmeb\Projects\pforth\csrc\PForth_lib.vcxproj]
C:\Users\znmeb\Projects\pforth\csrc\pf_save.c(438,28): warning C4244: '=': conversion from 'ExecToken' to 'int32_t', possible loss of data [C:\U
sers\znmeb\Projects\pforth\csrc\PForth_lib.vcxproj]
C:\Users\znmeb\Projects\pforth\csrc\pf_save.c(456,28): warning C4244: '=': conversion from 'cell_t' to 'int32_t', possible loss of data [C:\User
s\znmeb\Projects\pforth\csrc\PForth_lib.vcxproj]
C:\Users\znmeb\Projects\pforth\csrc\pf_save.c(457,29): warning C4244: '=': conversion from 'cell_t' to 'uint32_t', possible loss of data [C:\Use
rs\znmeb\Projects\pforth\csrc\PForth_lib.vcxproj]
C:\Users\znmeb\Projects\pforth\csrc\pf_save.c(469,26): warning C4244: '=': conversion from 'cell_t' to 'int32_t', possible loss of data [C:\User
s\znmeb\Projects\pforth\csrc\PForth_lib.vcxproj]
C:\Users\znmeb\Projects\pforth\csrc\pf_save.c(476,22): warning C4244: '=': conversion from 'cell_t' to 'int32_t', possible loss of data [C:\User
s\znmeb\Projects\pforth\csrc\PForth_lib.vcxproj]
  pf_text.c
  pf_words.c
  pfcompil.c
  pfcustom.c
  pf_fileio_stdio.c
  pf_io_win32_console.c
C:\Users\znmeb\Projects\pforth\csrc\win32_console\pf_io_win32_console.c(106,17): error C2220: the following warning is treated as an error [C:\U
sers\znmeb\Projects\pforth\csrc\PForth_lib.vcxproj]
C:\Users\znmeb\Projects\pforth\csrc\win32_console\pf_io_win32_console.c(106,17): warning C4244: '-=': conversion from 'int' to 'SHORT', possible
 loss of data [C:\Users\znmeb\Projects\pforth\csrc\PForth_lib.vcxproj]
C:\Users\znmeb\Projects\pforth\csrc\win32_console\pf_io_win32_console.c(120,17): warning C4244: '+=': conversion from 'int' to 'SHORT', possible
 loss of data [C:\Users\znmeb\Projects\pforth\csrc\PForth_lib.vcxproj]
C:\Users\znmeb\Projects\pforth\csrc\win32_console\pf_io_win32_console.c(121,35): warning C4244: '=': conversion from 'int' to 'SHORT', possible
loss of data [C:\Users\znmeb\Projects\pforth\csrc\PForth_lib.vcxproj]
  Generating Code...
(base) PS C:\Users\znmeb\Projects\pforth>
gwankyun commented 1 year ago

It still did not recognize "msbuild".

You do not need to use msbuild, just use CMake can built it.

gwankyun commented 1 year ago

I'm trying it with Build Tools for Visual Studio 2022.

Add followed line in pf_all.h

#ifdef _MSC_VER
#  pragma warning(push)
#  pragma warning(disable: 4127 4293 4100 4244 4267)
#endif
philburk commented 1 year ago

@gwankyun - Thanks. I added the disable to pf_all.h.

Now it gets past the compile and failed with:

 cmake --build .
Microsoft (R) Build Engine version 16.9.0+57a23d249 for .NET Framework
Copyright (C) Microsoft Corporation. All rights reserved.

  pf_cglue.c
[snip]
  pf_io_win32_console.c
  Generating Code...
  PForth_lib.vcxproj -> C:\Users\phil\work\pforth\csrc\Debug\PForth_lib.lib
  Building Custom Rule C:/Users/phil/work/pforth/CMakeLists.txt
  pf_main.c
LINK : fatal error LNK1104: cannot open file 'm.lib' [C:\Users\phil\work\pforth\pforth.vcxproj]

Based on some advice online, I removed the "m" lib from target_link_libraries(). That might break Unix.

I was then able to build an executable but the script failed.

 cmake --build .
Microsoft (R) Build Engine version 16.9.0+57a23d249 for .NET Framework
Copyright (C) Microsoft Corporation. All rights reserved.

  pf_io_win32_console.c
  PForth_lib.vcxproj -> C:\Users\phil\work\pforth\csrc\Debug\PForth_lib.lib
  pforth.vcxproj -> C:\Users\phil\work\pforth\fth\Debug\pforth.exe
  pforth.dic
  '.\pforth' is not recognized as an internal or external command,
  operable program or batch file.
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Microsoft\VC\v160\Microsoft.CppCommon.targets(240,5): error MSB8066: Custom build for 'C:\Users\phil\work\pforth\CMakeFiles\0c94cfde70f26a2635c53cfcf1972020\pforth
.dic.rule;C:\Users\phil\work\pforth\CMakeFiles\08cad30a20c9aa5c3a99d9baae146b17\pforth_dic.rule' exited with code 9009. [C:\Users\phil\work\pforth\pforth_dic.vcxproj]

But pForth seems to be working. This ran fine:

cd fth
Debug\pforth.exe -i system.fth
Debug\pforth.exe 

Progress!

znmeb commented 1 year ago

Incidentally, the reason I even have the build tools on my system is that the Raspberry Pi Pico SDK uses them.

AgitatedAlice commented 1 year ago

$ ninja [17/36] Linking C executable fth\pforth.exe FAILED: fth/pforth.exe cmd.exe /C "cd . && C:\msys64\mingw64\bin\cc.exe CMakeFiles/pforth.dir/csrc/pf_main.c.obj -o fth\p forth.exe -Wl,--out-implib,libpforth.dll.a -Wl,--major-image-version,0,--minor-image-version,0 csrc /libPForth_lib.a -lm -lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -l comdlg32 -ladvapi32 && cd ." C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe : csrc/libPForth_lib.a(pf_inner.c.obj):pf_inner.c:(.text+0x3389): undefined reference to `sdSleepMil lis' collect2.exe: error: ld returned 1 exit status ninja: build stopped: subcommand failed.

Fails to build on windows 10 64 bit with MSYS2, cmake and ninja, I cant get it to not use ninja but that is another issue for another day and I do not think ninja is the cause. What is sdSleepMil? Google comes up with nothing on it, searching my entire file system (took ages) came up with no reference to this. This is the most crpytic error ever.

sohang3112 commented 1 year ago

@AgitatedAlice I also had the same issue with building on Windows due to sdSleepMillis. Finally gave up and built it on WSL.

znmeb commented 1 year ago

@AgitatedAlice I also had the same issue with building on Windows due to sdSleepMillis. Finally gave up and built it on WSL.

Is there a working process for building a Windows 64-bit pforth executable using the cross-build tools on WSL? I'd much rather use that than mess around with MSYS2!!

philburk commented 1 year ago

sdSleepMillis() is a function that was added to pf_io_posix.c and pf_io_win32.c. I forgot to add it to pf_io_win32_console.c.

It is not an IO function so I should move them to a host dependency file.

znmeb commented 1 year ago

Thanks!! As a side effort of one of my projects I want to make Forth a first-class citizen in Quarto Markdown. That means I need a Forth I can shell out to from R on Windows and Linux.

philburk commented 1 year ago

I was able to complete a build on Windows. I solved the problem with the warnings being treated as errors by removing the /WX from the Windows build. I also had to use the executable name "Debug/pforth.exe" instead of "pforth". And I had to use a cross platform way of moving the pfdicdat.h file from the fth to the csrc folder.

Now I need to figure out how to avoid polluting the source tree with build artifacts.

znmeb commented 1 year ago

Now I need to figure out how to avoid polluting the source tree with build artifacts.

Doesn't cmake take care of that?

philburk commented 1 year ago

Doesn't cmake take care of that?

Ideadlly, yes. But when I use the standard techniques to avoid pollution the build fails and I cannot figure out why. But at least it is building. I will merge what I have. It is an improvement. Perhaps someone else will be able to offer advice.

philburk commented 1 year ago

When CMAKE create a bunch of junk in the source tree you can remove it by entering:

git clean -fd        # WARNING deletes all files not already committed