# Makefile for Windows (Win32/64).  Assumes Microsoft compiler.

# Should be invoked as `nmake -f NT_MAKEFILE [<args>]`; the optional arguments
# are:
# - `cpu=AMD64` to target x64;
# - `cpu=i386` to target x86;
# - `enable_static=1` to build it as a static library;
# - `nodebug=1` to produce the release variant of the library;
# - `disable_threads=1` to build the library without threads support.

cc = cl
link = link
rc = rc

!IF !DEFINED(CPU) || "$(CPU)" == ""
CPU = $(PROCESSOR_ARCHITECTURE)
!ENDIF
!IF "$(CPU)" == "I386" || "$(CPU)" == "X86" || "$(CPU)" == "x86"
CPU = i386
!ELSEIF "$(CPU)" == "X64" || "$(CPU)" == "x64" || "$(CPU)" == "amd64"
CPU = AMD64
!ENDIF

!IF !DEFINED(NMAKE_WINVER)
NMAKE_WINVER = 0x0600
!ENDIF

cflags = $(cflags) -c -DCRTAPI1=_cdecl -DCRTAPI2=_cdecl -GS -D_WINNT -W4
!IF "$(CPU)" == "i386"
cflags = $(cflags) -D_X86_=1  -DWIN32 -D_WIN32
!ELSEIF "$(CPU)" == "AMD64"
cflags = $(cflags) -D_AMD64_=1 -DWIN64 -D_WIN64  -DWIN32 -D_WIN32
!ENDIF
cflags = $(cflags) -D_WIN32_WINNT=$(NMAKE_WINVER) -DWINVER=$(NMAKE_WINVER)

!IFDEF NODEBUG
cvarsmt = -D_MT -MT
cdebug = -Ox -DNDEBUG
rcvars = -DWIN32 -D_WIN32 -DWINVER=$(NMAKE_WINVER)
ldebug = /RELEASE
!ELSE
cvarsmt = -D_MT -MTd
cdebug = -Zi -Od -DDEBUG
rcvars = -DWIN32 -D_WIN32 -DWINVER=$(NMAKE_WINVER) -DDEBUG -D_DEBUG
ldebug = /DEBUG /DEBUGTYPE:cv
!ENDIF

!IF "$(CPU)" == "i386"
CVTRES_CPU=X86
!ELSEIF "$(CPU)" == "AMD64"
CVTRES_CPU=X64
!ENDIF

!IFNDEF NODEBUG
CFLAGS_DEBUG=-DGC_ASSERTIONS
!ENDIF

!IFDEF ENABLE_STATIC
CFLAGS_GCDLL=-DGC_NOT_DLL
CORDFLAG=
!ELSE
CFLAGS_GCDLL=-DGC_DLL
# `cord.dll` file and its clients should not link C library statically
# otherwise `FILE`-related functions might not work (because own set of
# opened `FILE` instances is maintained by each copy of the C library thus
# making impossible to pass `FILE` pointer from `.exe` code to `.dll` code).
cvarsmt=
!IFDEF NODEBUG
CORDFLAG=-MD
!ELSE
CORDFLAG=-MDd
!ENDIF
!ENDIF

!IFNDEF DISABLE_THREADS
CFLAGS_MT=$(cvarsmt) -DGC_THREADS -DTHREAD_LOCAL_ALLOC -DPARALLEL_MARK
!ENDIF

# Extra user-defined flags to pass both to C and C++ compilers.
CFLAGS_EXTRA=

CFLAGS_SPECIFIC=$(CFLAGS_DEBUG) $(CFLAGS_GCDLL) $(CFLAGS_MT)

CFLAGS_DEFAULT=-DALL_INTERIOR_POINTERS -DENABLE_DISCLAIM -DGC_ATOMIC_UNCOLLECTABLE -DGC_GCJ_SUPPORT -DJAVA_FINALIZATION -DNO_EXECUTE_PERMISSION -DGC_REQUIRE_WCSDUP -DUSE_MUNMAP

CXXFLAGS_SPECIFIC=/EHsc

# Make sure that `.cc` is not viewed as a suffix.  It is for VC++2005, but
# not earlier versions.  We can deal with either, but not inconsistency.
.SUFFIXES:
.SUFFIXES: .obj .cpp .c

# Atomic_ops installation directory.  For Win32, the source directory
# should do, since we only need the headers.
# We assume this was manually unpacked.
AO_SRC_DIR=libatomic_ops/src
AO_INCLUDE_DIR=$(AO_SRC_DIR)

!IFDEF ENABLE_STATIC
# `pthread_start.obj` file is needed just in case client defines
# `GC_WIN32_PTHREADS` macro.
OBJS= allchblk.obj alloc.obj blacklst.obj dbg_mlc.obj dyn_load.obj finalize.obj fnlz_mlc.obj gcj_mlc.obj headers.obj mach_dep.obj malloc.obj mallocx.obj mark.obj mark_rts.obj misc.obj new_hblk.obj os_dep.obj pthread_start.obj pthread_support.obj ptr_chck.obj reclaim.obj thread_local_alloc.obj typd_mlc.obj win32_threads.obj extra\msvc_dbg.obj
!ELSE
OBJS= extra\gc.obj extra\msvc_dbg.obj
!ENDIF

COBJS= cord\cordbscs.obj cord\cordprnt.obj cord\cordxtra.obj

all: gc.lib cord.lib gccpp.lib gctba.lib

check-deps: gctest.exe cpptest.exe treetest.exe cordtest.exe de.exe

check: check-deps
	gctest.exe
	cordtest.exe
	cpptest.exe
	treetest.exe

.c.obj:
	$(cc) $(cdebug) $(cflags) $(CFLAGS_SPECIFIC) $(CORDFLAG) -Iinclude -I$(AO_INCLUDE_DIR) $(CFLAGS_DEFAULT) -D_CRT_SECURE_NO_DEPRECATE $(CFLAGS_EXTRA) $*.c /Fo$*.obj /wd4127 /wd4701
# Disable `crt` security warnings, since unfortunately they warn about all
# sorts of safe uses of `strncpy`.  It would be nice to leave the rest enabled.

.cpp.obj:
	$(cc) $(cdebug) $(cflags) $(CFLAGS_SPECIFIC) -Iinclude $(CFLAGS_DEFAULT) $(CXXFLAGS_SPECIFIC) -D_CRT_SECURE_NO_DEPRECATE $(CFLAGS_EXTRA) $*.cpp /Fo$*.obj

$(OBJS) tests\gctest.obj: include\private\gc_priv.h include\private\gc_hdrs.h include\gc\gc.h include\private\gcconfig.h include\private\gc_locks.h include\private\gc_pmark.h include\gc\gc_mark.h include\gc\gc_disclaim.h

!IFDEF ENABLE_STATIC

gc.lib: $(OBJS)
	lib /out:gc.lib /MACHINE:$(CPU) $(OBJS)

cord.lib: $(COBJS)
	lib /out:cord.lib /MACHINE:$(CPU) $(COBJS)

gccpp.lib: gc_badalc.obj gc_cpp.obj
	lib /out:gccpp.lib /MACHINE:$(CPU) gc_badalc.obj gc_cpp.obj

# The same as `gccpp.lib` file but contains only `gc_badalc.obj` file.
gctba.lib: gc_badalc.obj
	lib /out:gctba.lib /MACHINE:$(CPU) gc_badalc.obj

!ELSE

gc.lib: $(OBJS)
	$(link) $(ldebug) kernel32.lib user32.lib /subsystem:windows /dll /INCREMENTAL:NO /pdb:"gc.pdb" /out:gc.dll /implib:gc.lib /MACHINE:$(CPU) $(OBJS)

cord.lib: $(COBJS) gc.lib
	$(link) $(ldebug) gc.lib /subsystem:windows /dll /INCREMENTAL:NO /pdb:"cord.pdb" /out:cord.dll /implib:cord.lib /MACHINE:$(CPU) $(COBJS)

gccpp.lib: gc_badalc.obj gc_cpp.obj gc.lib
	$(link) $(ldebug) gc.lib /subsystem:windows /dll /INCREMENTAL:NO /pdb:"gccpp.pdb" /out:gccpp.dll /implib:gccpp.lib /MACHINE:$(CPU) gc_badalc.obj gc_cpp.obj

gctba.lib: gc_badalc.obj gc.lib
	$(link) $(ldebug) gc.lib /subsystem:windows /dll /INCREMENTAL:NO /pdb:"gctba.pdb" /out:gctba.dll /implib:gctba.lib /MACHINE:$(CPU) gc_badalc.obj

!ENDIF

gctest.exe: gc.lib tests\gctest.obj
	$(link) /MACHINE:$(CPU) /INCREMENTAL:NO $(ldebug) $(lflags) user32.lib -out:$*.exe tests\gctest.obj gc.lib
#	`mapsympe -n -o gctest.sym gctest.exe`
# This produces a GUI application that opens no window and writes
# to `gctest.gc.log` file.

cord\tests\de_win.rbj: cord\tests\de_win.res
	cvtres /MACHINE:$(CVTRES_CPU) /OUT:cord\tests\de_win.rbj cord\tests\de_win.res

cord\tests\de.obj cord\tests\de_win.obj: include\gc\cord.h include\gc\cord_pos.h cord\tests\de_win.h cord\tests\de_cmds.h

cord\tests\de_win.res: cord\tests\de_win.rc cord\tests\de_win.h cord\tests\de_cmds.h
	$(rc) $(rcvars) -r -fo cord\tests\de_win.res cord\tests\de_win.rc

# `cord/de` is a real Windows GUI application.
de.exe: cord\tests\de.obj cord\tests\de_win.obj cord\tests\de_win.rbj gc.lib cord.lib
	$(link) /MACHINE:$(CPU) /INCREMENTAL:NO $(ldebug) $(lflags) -out:de.exe cord\tests\de.obj cord\tests\de_win.obj cord\tests\de_win.rbj gc.lib cord.lib gdi32.lib user32.lib

cordtest.exe: cord\tests\cordtest.obj gc.lib cord.lib
	$(link) /subsystem:console /MACHINE:$(CPU) /INCREMENTAL:NO $(ldebug) $(lflags) -out:cordtest.exe cord\tests\cordtest.obj gc.lib cord.lib user32.lib

gc_badalc.obj: gc_badalc.cc include\gc\gc_cpp.h include\gc\gc.h

gc_cpp.obj: gc_cpp.cc include\gc\gc_cpp.h include\gc\gc.h

test_cpp.cpp: tests\cpp.cc
	copy tests\cpp.cc test_cpp.cpp

# This generates the C++ test executable.  The executable expects
# a single numeric argument, which is the number of iterations.
# The output appears in `cpptest.gc.log` file.
cpptest.exe: test_cpp.obj include\gc\gc_cpp.h include\gc\gc_allocator.h include\gc\gc.h gc.lib gccpp.lib
	$(link) /MACHINE:$(CPU) /INCREMENTAL:NO $(ldebug) $(lflags) user32.lib -out:cpptest.exe test_cpp.obj gc.lib gccpp.lib

test_tree.cpp: tests\tree.cc
	copy tests\tree.cc test_tree.cpp

treetest.exe: test_tree.obj include\gc\gc_cpp.h include\gc\gc.h gc.lib gctba.lib
	$(link) /MACHINE:$(CPU) /INCREMENTAL:NO $(ldebug) $(lflags) user32.lib -out:treetest.exe test_tree.obj gc.lib gctba.lib

$(AO_SRC_DIR):
	tar xvfz $(AO_SRC_DIR).tar.gz

clean:
	del *.dll *.exe *.exp *.lib *.log *.obj *.pdb cordtst*.tmp cord\*.obj cord\tests\*.rbj cord\tests\*.res cord\tests\*.obj extra\*.obj test_cpp.cpp test_tree.cpp tests\*.obj 2> nul
