How To Build On Linux
---------------------

Note: These instructions are for aarch64/arm64 CPUs.
See building/linux32ARMv6/HowToBuild
 or building/linux32ARMv7/HowToBuild for 32 bit ARM images.


Contents:
	- Overview
	- Checking out sources to build out-of-the-box
	- Building out of the box
	- Building the VM Simulator Support Libraries
	- How to configure and build a VM on Unix
	- Debugging configuration failures
	- Testing an external plugin has completely linked
	- Optimization level and gcc version (please read!)
	- Installing support libraries
	- Building with MUSL rather than libc


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

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 maxchine 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.

A distinction on linux is between VMs with an itimer hearbeat or a threaded
heartbeat.  VMs with an itimer hearbeat  use setitimer to deliver a SIGALRM
signal at regular intervals to interrupt the VM to check for events.  These
signals can be troublesome, interrupting foreign code that cannot cope with
such signals.  VMs with a threaded heartbeat use a high-priority thread that
loops, blocking on nanosleep and then interrupting the VM, performing the same
function as the itimer heartbeat but without using signals.  These VMs are to
be preferred but suport for multiple thread priorities in user-level processes
has only been available on linux in kernels later than 2.6.12.

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.cog.v3 contains build directories for Smalltalk Cog VMs using
the old object representation, newspeak.stack.spur contains build directories
for Newspeak 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
-----------------------
Install the tools (gcc, X11-devel, etc, e.g. libpng, libuuid libX11 & libxt
source). See "Installing support libraries" below.  If the configure step fails
when "checking for C compiler default output file name", you have yet to install
all the necessary support packages (e.g. libuuid).
Then cd to the build directory of your choice, e.g.
	building/linux64x64/squeak.cog.spur/build
Then execute
	./mvm
answering "y" to perform a clean build or "n" to rebuild without reconfiguring.
Again, if the configure step fails when "checking for C compiler default output
file name", you have yet to install all the necessary support (e.g. lubuuid).

The subdirectories conform to the production/assert/debug x itimer vs threaded
heartbeat x single vs multi-threaded parts of the matrix described above.  For
example, building/linux64x64/squeak.cog.v3 includes

	build
	build.itimerheartbeat
	build.multithreaded

	build.assert
	build.assert.itimerheartbeat
	build.multithreaded.assert

	build.debug
	build.multithreaded.debug
	build.debug.itimerheartbeat

subdirectories. It includes two convenience scripts that will make all
configurations:
	makeallclean
	makealldirty

Each build directory contains three files
	mvm
	plugins.int
	plugins.ext
The mvm script runs ../../platforms/unix/config/configure with the relevant
options, runs make, and then make install to create a VM directory tree in
../../products, ../../products/assert or ../../products/debug as appropriate.
plugins.int and plugins.ext 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 shared
objects to be dynamically linked at run-time (plugins.ext).

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


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/linux64x64/bochsx86
	x86_64/x64  BochsX64Plugin   building/linux64x64/bochsx64
	ARMv5       GdbARMPlugin     building/linux64x64/gdbarm32
cd to the relevant directories; run conf.COG and then the build script, e.g.
  $ cd building/linux64x64/bochsx86
  $ ./conf.COG
  $ ./makeem

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


How to configure and build a VM on Unix
---------------------------------------
The mvm scripts are themselves wrappers around an adaptation of Ian Piumarta's
Squeak build system above autoconf to the Cog sources.  One can choose the vm
source files, plugin source files, and optimization level to compile a VM of
your choice.  To find the full set of options via

	../../platforms/unix/config/configure --help

You can see the use of configure in the various mvm scripts in each build
directory.

e.g.
     ../../../platforms/unix/config/configure --without-npsqueak CFLAGS="-g -O2 -msse2 -D_GNU_SOURCE -DNDEBUG -DITIMER_HEARTBEAT=1 -DCOGMTVM=0 -DDEBUGVM=0" LIBS=-lpthread
     make install prefix=WhereYouWantTheVmToGo

	N.B. If you're on a 64-bit linux read 3e below!!
	N.B. On Ubuntu *do not* supply "LIBS=-lpthread -luuid", i.e. use
     ../../../platforms/unix/config/configure --without-npsqueak CFLAGS="-g -O2 -msse2 -D_GNU_SOURCE -DNDEBUG -DITIMER_HEARTBEAT=1 -DCOGMTVM=0 -DDEBUGVM=0"


N.B. The plugin set is defined by plugins.ext and plugins.int in the build dir.

Be prepared to install libuuid support.  e.g. on CentOS 6.5 use
sudo yum -y install libuuid-devel


Debugging configuration failures
--------------------------------
If your development system does not have the required libraries installed then
the configure step run by the mvm script will fail cryptically.  You will see
something like
	checking sanity of generated src directory... okay
	checking build system type... i686-pc-linux-gnu
	checking host system type... i686-pc-linux-gnu
	checking target system type... i686-pc-linux-gnu

	Configuring Squeak  (.-) for i686-pc-linux-gnu

	checking whether make sets $(MAKE)... yes
	checking for gcc... gcc -m32
	checking for C compiler default output file name... configure: error: C compiler cannot create executables
	See `config.log' for more details.
But config.log is missing the relevant details.  It only contains the failing gcc
invocation, not the output from that failing command.  To diagnose, grep to find
the gcc invocation:
	$ grep gcc config.log
	...
	configure:2269: gcc -m32 -g3 -O0 -fwrapv -DDEBUGVM=1 -DEnforceAccessControl=0 -msse2 -D_GNU_SOURCE -D_FILE_OFFSET_BITS=64  -Wl,-z,now conftest.c -lpthread -luuid >&
and then repeat the compilation supplying your own test file, e.g.
	$ gcc -m32 -g3 -O0 -fwrapv -DDEBUGVM=1 -DEnforceAccessControl=0 -msse2 -D_GNU_SOURCE -D_FILE_OFFSET_BITS=64  -Wl,-z,now helloworld.c -lpthread -luuid
	helloworld.c: In function 'main':
	/usr/bin/ld: cannot find -luuid
	collect2: ld returned 1 exit status
From which one can infer that one needs to install either the 64-bit or the 32-bit
version of libuuid, e.g. sudo yum install libuuid-devel


Testing an external plugin has completely linked
------------------------------------------------
You may find that an external plugin compiles and links but does not load.
This is usually because it contans undefined symbols.  To find undefined
symbols, remake the plugin, capturing the link step and then supply
	-Wl,--warn-unresolved-symbols -Wl,--no-allow-shlib-undefined
when manually repeating the link command


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

So turn them off.  e.g.
     ../../../platforms/unix/config/configure --without-npsqueak CFLAGS="-g -O2 -msse2 -fno-caller-saves -fno-partial-inlining -fno-tree-pre -D_GNU_SOURCE -DNDEBUG -DCOGMTVM=0 -DDEBUGVM=0" LIBS="-lpthread -luuid"
See http://smallissimo.blogspot.fr/2013/02/compiling-squeak-cog-virtual-machine-on.html

People have reported that the OSProcessPlugin is broken on gcc versions > 4.8
on Debian.


Installing support libraries
----------------------------
Different linux distributions have different mechanisms for loading packages.
Here are some examples:

CentOS
	sudo yum install -y cairo-devel
	sudo yum install -y pango-devel
	sudo yum install -y libuuid-devel
	sudo yum install -y libX11-devel
	sudo yum install -y mesa-libGL-devel
	sudo yum install -y openssl-devel
	sudo yum install -y cmake
	sudo yum install -y gcc-c++
N.B. if you get failures due to version conflicts try
	sudo yum update
and then retry.

Ubuntu
	sudo apt install uuid-dev libcairo2-dev libpango1.0-dev libgl1-mesa-dev
	sudo apt install libgl1-mesa-glx libssl-dev libevdev-dev m4 libpulse-dev
	sudo apt install libxrandr-dev

Manjaro
	sudo pacman -Sy gcc clang gdb make
	sudo pacman -S uuid-dev libgl1-mesa-dev libgl1-mesa-glx libssl-dev
	sudo pacman -S xf86-video-fbdeV

Debian (64-bit)
	A complete build configuration (dockerized) can be found here:
	https://github.com/LinqLover/squeak-raspi-docker/tree/master/osvm-mvm

More advice and examples for other distros gratefully received.



Building with MUSL rather than libc
-----------------------------------
There are some differences in include file definitons when
using MUSL in place of libc.  Note: https://musl.libc.org

You should check your Linux distro for libc development support
libraries or include files needed.

The following has been tested on both aarch64 and amd64 Alpine Linux
(MUSL+busybox).

install musl-dev [e.g. sudo apk add musl-dev ].

Due to the continued evolution of the OpenSmalltalk VM
it is not possible to use patch files, but the changes
to be made are simple to describe.

Basically, building works fine but one needs to

[A] add "-DMUSL -D_GNU_SOURCE" to CFLAGS so that include
expansion is not confused between libc and MUSL definitions.

[B] elide (remove) some definitions which use 'FILE'.


A simple way to do [A] is to add the definitions to OPT in the
'mvm' file in the 'build' directory.

For [B] one needs to make small changes to files
    platforms/Cross/vm/sqVirtualMachine.c
and
    platforms/unix/vm/sqUnixMain.c
to skip definitions of pushOutputFile() and popOutputFile().


[B.1] platforms/Cross/vm/sqVirtualMachine.c
...
#ifndef MUSL
 #define STDOUT_STACK_SZ 5
 static int stdoutStackIdx = -1;
 static FILE stdoutStack[STDOUT_STACK_SZ];
#endif
...
#ifndef MUSL
 void
 pushOutputFile(char *filenameOrStdioIndex)
 {
...
 }

 void
 popOutputFile()
 {
 ...
 }
#endif


[B.2] platforms/unix/vm/sqUnixMain.c
...
#ifdef MUSL
void pushOutputFile(char *fileNameOrStdioIndex) {;}
void popOutputFile() {;}
#endif
...


That's it.  You should be able to invoke 'mvm' in the 'build'
directory, answer 'y' to the 'clean?' prompt, and get a
proper build.

If running the resultant squeak vm gives an error something like
    mprotect(x,y,PROT_READ|PROT_EXEC)
or
    memory_alias_map: shm_open: Permission denied
you need to enable shared memory for the COG JIT.

As root:
   chmod 777 /dev/shm
   echo 'none /dev/shm tmpfs rw,nosuid,nodev 0 0' >> /etc/fstab
   mount /dev/shm

The squeak vm should now work.
