How To Build On Win32-64bit x86_64
----------------------------------


Contents:
	- Overview
	- Checking out sources to build out-of-the-box
	- Building out of the box
	- Tailoring the VM build
	- Building a Specific Plugin Dll
	- Building the VM Simulator Support Libraries
	- Optimization level and gcc version (please read!)
	- Debugging with gdb
	- Debugging with Visual Studio


Overview
--------
The "Cog" VM comes in a bewildering variety of forms.  The first distinction
is between Squeak/Croquet VMs that run Squeak, Cuis, Croquet images
and their ilk.

Another distinction is between Stack, Cog and Sista VMs.  Stack VMs are those
with context-to-stack mapping that optimise message sending by keeping method
activations on a stack instead of in contexts.  These are pure interpreters but
significantly faster than the standard context-based Interpreter VM.  Cog VMs
add a JIT to the mix, compiling methods used more than once to machine code on
the fly.  Sista VMs, as yet unrealised and in development, add support for
adaptive optimization that does speculative inlining at the bytecode-to-bytecode
level.  These are targeted for release in 2015.

Another distinction is between "v3" VMs and Spur VMs.  "v3" is the original
object representation for Squeak as described in the back-to-the-future paper.
Spur, as described on the www.mirandabanda.org blog, is a faster object
representation which uses generation scavenging, lazy forwarding for fast
become, and a single object header format common to 32 and 64 bit versions.

Another distinction is between normal single-threaded VMs that schedule "green"
Smalltalk processes above a single-threaded VM, and "multi-threaded" VMs that
share the VM between any number of native threads such that only one native
thread owns the VM at any one time, switching between threads on FFI calls and
callbacks or on Smalltalk process switches when Smalltalk processes are owned
by threads.  This multi-threaded support is as yet experimental.

The final distinction is between production, assert and debug VMs.  Production
VMs are fully optimized, although they may include debugging symbols, and as
their name implies are for use in production.  Assert and debug VMs include
many assert checks that are disabled in the production VMs.  These asserts are
very helpful in debugging VM problems but significantly impact performance.
The difference between assert and debug VMs is that assert VMs are compiled
with moderate optimization, which improves the performance of the asserts,
whereas debug VMs are compiled with no optimization at all, providing maximum
debuggability with minimum performance.

This directory tree provides build directories for some of this matrix.  For
example, squeak.stack.spur contains a build directory
for Smalltalk Stack VMs using the Spur object representation.  Build as desired.


Checking out sources to build out-of-the-box
--------------------------------------------
Check-out the repository from github:
	git clone http://www.github.com/OpenSmalltalk/opensmalltalk-vm oscogvm
	cd ./oscogvm
	more README.md


Building out of the box
-----------------------
The standard build configurations are for LLVM/Clang and an MSYS2 shell. You can
either build against the WinSDK directly, which is the recommended way, or
against mingw-w64.

If you do not know about any of these technologies, here are the resources
to get started:
  - LLVM/Clang: https://clang.llvm.org/
  - MSYS2: https://www.msys2.org/
  - Mingw-w64: https://www.mingw-w64.org/
  - Windows SDK: https://developer.microsoft.com/de-de/windows/downloads/windows-sdk/

Option A - WinSDK
  1. Install MSYS2 via "scripts/installMSYS2.cmd base"
  2. Install "Visual Studio Community Edition" (incl. Windows 10 SDK)
       - https://visualstudio.microsoft.com/de/vs/community/
       - Note that recent versions come with own Clang besides MSVCC
       - Note that "C++ ATL Support" is required for the CameraPlugin
  3. [optional] Install another LLVM/Clang that targets *-pc-windows-msvc
       - https://github.com/llvm/llvm-project/releases
  4. Open MSYS2 shell with default MSYS environment
       - Typically "c:/msys64/msys2.exe" or "c:/msys64/msys2_shell.cmd -msys2"
  5. Change to the vm flavor of your choice, e.g. squeak.cog.spur, and 
     execute "./mvm"
       - Use another LLVM/Clang via "./mvm -- LLVM_BIN=c:/llvm/bin/ "
       - Use LLVM/Clang from $PATH via "./mvm -- LLVM_BIN='' "

This builds debug, assert and production versions of the VM in builddbg/vm,
buildast/vm and build/vm.  If the configuration includes multi-threaded builds
it will also create VMs in buildmtdbg/vm, buildmtast/vm and buildmt/vm.  The
system builds two VMs in each build/vm directory, e.g. Squeak.exe and
SqueakConsole.exe.  The latter is to be used with console applications that wish
to access standard i/o.

In order to only build the production vm, you can launch the command:
	./mvm -f


Option B - mingw-w64
  1. Install MSYS2 via "scripts/installMSYS2.cmd x86_64"
  2. Open MSYS2 shell with MINGW64 environment
       - Typically "c:/msys64/mingw64.exe" or "c:/msys64/msys2_shell.cmd -mingw64"
  3. Change to the vm flavor of your choice, e.g. squeak.cog.spur, and 
     execute "./mvm -- TOOLPREFIX='' "

Note that any other version of Clang must be installed only through
MSYS2 and target *-w64-windows-gnu to produce the correct .o files.
  

Option C - WinSDK via x64 Native Tools Command Prompt
  1. Do steps 1 to 3 from option A.
  2. Open an x64 Native Tools Command Prompt
       - Needs "MSVC v143 - VS 2022 C++ x64/86 Buildtool" for "Native Tools Command Prompt"
  3. Change to the vm flavor of your choice, e.g. squeak.cog.spur, and
     execute "..\common\SETPATH_MSYS2.BAT "
  4. Then execute the other batch files to build the VM
  	   ..\common\MAKEDEBUG.BAT
       ..\common\MAKEASSERT.BAT
       ..\common\MAKEFAST.BAT


Option C' - WinSDK via x86 Native Tools Command Prompt substitutes Cygwin for MSYS.
	Follow the instructions for Option C but
	install Cygwin instead of MSYS2, and use "..\common\SETPATH_CYGWIN.BAT"
	instead of "..\common\SETPATH_MSYS2.BAT"



Tailoring the VM build
----------------------
Each build directory contains two files
	plugins.int
	plugins.ext
that determine the set of plugins to be taken from the supplied plugins
directory (which defaults to ../../src/plugins), and which are to be linked
into the VM (plugins.int) or compiled as external dlls to be dynamically
linked at run-time (plugins.ext). Tailor these files as you wish.

Finally, at the building/win64x64 level the makeall script will run all the
mvm scripts it can find.


Building a Specific Plugin Dll
------------------------------
To make only a specific dll one must use the target of the plugin dll (or .lib)
to be made.  e.g.
  $ cd squeak.cog.spur
  $ make build/vm/BochsIA32Plugin.dll


Building the VM Simulator Support Libraries
------------------------------------
If you want to get the Cog VM simulator working you'll need to build one or more
of the processor simulator plugins, each of which has support libraries that
must be built:
  Processor   Plugin           Support Library
  x86         BochsIA32Plugin  building/win64x64/bochsx86
  x86_64/x64  BochsX64Plugin   building/win64x64/bochsx64
  ARMv5       GdbARMPlugin     building/win64x64/gdbarm32
  ARMv8       GdbARMv8Plugin   building/win64x64/gdbarm64
cd to the relevant directories; run conf.COG and then the build script, e.g.
  $ cd building/win64x64/bochsx64
  $ ./conf.COG
  $ ./makeem

Then when Squeak VMs are built they will include the plugin(s) for which support
libraries have been provided.


Optimization level and gcc version
----------------------------------
There used to be issues with gcc version > 4.2.1.  Any of the following flags
may break the build at -O2:
  -ftree-pre
  -fpartial-inlining
  -fcaller-saves

However, with removal of some Undefined Behavior in source code, this list might
well be out of date.

See http://smallissimo.blogspot.fr/2013/02/compiling-squeak-cog-virtual-machine-on.html

Debugging with gdb
------------------

Note that even the stripped production VM can be used for debug using the
unstripped VM to provide symbols, e.g.
	U:\ gdb bin/Squeak.exe
	...
	(no debugging symbols found)
	(gdb) file SqueakUnstripped.exe
	(gdb) run trunk.image

Note that on MSYS2, while cross-compilation works well, cross debugging does
not work at all.  One has to debug 32-bit executables on a 32-bit installation
with a 32-bit gdb, and 64-bit executables on a 64-bit installation with a 64-bit
gdb.

Debugging with Visual Studio
----------------------------

Even though the MSVC build outline above does not use MSVC project files,
debugging is straight-forward.

1. Open Visual Studio
2. use Open Folder... to open {build,buildast,builddbg}/vm as desired
3. use Open File... to open the desired executable, e.g. Squeak.exe
