# @(#)Makefile	1.23 98/03/29
#
# Makefile for building audio dll
#
#
# NOTE: This file has been modfied to build the audio subsystem
# in two environments: A JDK1.2 tree, and an audio only standalone tree.
# Things that have been added for standalone building have
# been noted in comments. 
# 
# Please setp lightly when modifying the build environment.
# Thank you.
#
#
BUILDDIR = ..\..
LOCATION = sun\audio
!include $(BUILDDIR)\makefiles\defs.nmk

# Needed for moving source.
!ifndef MKSDIR
MKSDIR = c:\mksnt
!endif

MKDIR = $(MKSDIR)\mkdir -p
SED = $(MKSDIR)\sed

# Set up that is dependent on how the engine
# is going to get built.
!if "$(AUDIO_EXTENSION)" == "true"
PKG = com.sun.media.jmf.audio
LIBRARY	= jmfaudio
!else
PKG = sun.audio
LIBRARY = audio
!endif

PKGDIR= $(PKG:.=\)


# Some debug and tracing information
# Seeting these to true will turn debug
# or tracing on in the JSE java code.
!if "$(DEBUG_AUDIO_EXTENSION)" == "true"
AUDIO_DEBUG=true
AUDIO_TRACE=true
AUDIO_DEBUG_FLAGS=-DJNC_TRACE=1 -DJNC_DEBUG=1
!else
AUDIO_DEBUG=false
AUDIO_TRACE=false
!endif

#
# Files to compile.
#
!include $(MINCLUDES)\sun_audio.jmk
!include $(MINCLUDES)\sun_audio.cmk

PLATFORM_FILES_c = \
	$(CTARGDIR)HAE_API_WinOS.c

EXTRA_FILES_c = \
	$(CTARGDIR)GenModFiles.c \
	$(CTARGDIR)MOD_load_m15.c \
	$(CTARGDIR)MOD_load_mod.c \
	$(CTARGDIR)MOD_load_mtm.c \
	$(CTARGDIR)MOD_load_s3m.c \
	$(CTARGDIR)MOD_load_stm.c \
	$(CTARGDIR)MOD_load_ult.c \
	$(CTARGDIR)MOD_load_uni.c \
	$(CTARGDIR)MOD_load_xm.c \
	$(CTARGDIR)MOD_mdriver.c \
	$(CTARGDIR)MOD_memory.c \
	$(CTARGDIR)MOD_mloader.c \
	$(CTARGDIR)MOD_mmio.c \
	$(CTARGDIR)MOD_mplayer.c \
	$(CTARGDIR)MOD_munitrk.c \
	$(CTARGDIR)MOD_virtch.c


!if "$(AUDIO_EXTENSION)" == "true"

FILES_c = $(FILES_c) $(EXTRA_FILES_c)

# No variable substitution here, so we have to do it by hand.
# Here its CTARGDIR is replaced with $(SHARE_SRC)\native\$(PKGDIR)
SRC_FILES_c = $(FILES_c:obj=..\..\..\..\src\share\native\com\sun\media\jmf\audio)
SRC_FILES_platform = $(PLATFORM_FILES_c:obj=..\..\..\..\src\win32\native\com\sun\media\jmf\audio)
SRC_FILES_cpp = $(FILES_cpp:obj=..\..\..\..\src\share\native\com\sun\media\jmf\audio)


#SRC_FILES_java = $(FILES_java:..\..\classes=..\..\..\..\src\share\classes\com\sun\media\jmf\audio)
SRC_FILES_java = $(FILES_java:..\..\classes=..\..\..\..\src\share\classes)
SRC_FILES_java = $(SRC_FILES_java:/=\)

SOURCE_FILES = $(SRC_FILES_java) $(SRC_FILES_c) $(SRC_FILES_platform) $(SRC_FILES_cpp)

SOURCE_DIRS = \
	$(PLATFORM_SRC)\classes\$(PKGDIR) \
	$(PLATFORM_SRC)\native\$(PKGDIR) \
	$(SHARE_SRC)\classes\$(PKGDIR) \
	$(SHARE_SRC)\classes\$(PKGDIR)

!else
FILES_c = $(FILES_c) $(FILES_cpp:.cpp=.c) $(EXTRA_FILES_c) $(PLATFORM_FILES_c)
!endif

#
# Stuff to feed into javah.
#
EXPORTED_CLASSES = \
	$(PKG).HaeMidiSong \
	$(PKG).HaeThread \
	$(PKG).HaeMidiNoise \
	$(PKG).HaeMixer \
	$(PKG).HaeRmfSong \
	$(PKG).HaeWaveStream
#	$(PKG).HaeWaveDirect \
#	$(PKG).HaeWaveMemory 
#  	$(PKG).HaeMidiPlayer \
#	$(PKG).HaeWaveFile \

#
# Tweak flags.
#
EXTRA_LIBS   = winmm.lib user32.lib $(JVMLIB)


EXTRA_CFLAGS = -DX_PLATFORM=X_WIN95 -DJAVA_SOUND  -DJAVA_THREAD -DNO_DIRECT_SOUND $(AUDIO_DEBUG_FLAGS)

#
# Has been converted to the JNI: generate JNI-style header files
#
JAVAHFLAGS = $(JAVAHFLAGS) -jni


#
# This following is for building the Java Sound Engine
# outside of the JDK builds. 
# This build configurations is bracketed by the
# AUDIO_EXTENSION define. 
# 
# To build inside a JDK AUDIO_EXTENSIONS should not be defined
#
# To build standalong AUDIO_EXTENSIONS should be defined to "true"
# and ALT_BOOT_HOME should point to a JDK installation
# to build against.
#
# Please don't make modifications to the configuration information
# in the AUDIO_EXTENSION section  without talking to
# somebody on the sound team: kkytle, jdr, nannette
# Thanks.
#
!if "$(AUDIO_EXTENSION)" == "true"

# Various Configuration that needs to be set.
PLATFORM=win32
INSTALL_DOT_LIB=true

# $(JAVAHOME) is some default (e.g. /usr/local/java/latest)
# unless $(ALT_BOOTDIR) is set in which case $(ALT_JAVAHOME)
# is used.
SOUND_BOOT_DIR=$(ALT_BOOTDIR)
JAVAHOME=$(ALT_BOOTDIR)

# Since we're not building a full JDK build,
# most of the JDK stuff is already built and 
# we need to point to it. 
BINDIR=$(SOUND_BOOT_DIR)\bin
INCLUDESDIR=$(SOUND_BOOT_DIR)\include
INCLUDES = -I$(INCLUDESDIR) -I$(INCLUDESDIR)\$(PLATFORM) -I$(CLASSHEADERS)

# We're building the dll without the any lib reference.
JAVALIB=
JVMLIB=

# Classpath for java compiles and javah
CLASSPATH=$(PLATFORM_SRC)\classes;$(SHARE_SRC)\classes;$(CLASSBINDIR);$(SOUND_BOOT_DIR)\lib\classes.zip

# This is a hack, because I didn't want to find out why 
# and where CLASSPATH is getting changed in the included configuration files.
SOUND_CLASSPATH=$(PLATFORM_SRC)\classes;$(SHARE_SRC)\classes;$(SOUND_BOOT_DIR)\lib\classes.zip;$(CLASSBINDIR)

# Use the INSTALLED JAVAC with the SOUND_CLASSPATH.
JAVAC_CMD = $(BINDIR)\javac$(SUFFIX).exe

# In this case we're adding our own targets for building.
# Using the existing targets doesn't work for a couple
# of reasons that have to do with the way nmake works
# and the way the JDK build is constructed.
# This looks very much like the targets that
# would be obtained from an include of build/win32/makefiles/library.nmk.
#
all: $(SOURCE_DIRS) $(SOURCE_FILES) header_files optimized debug

optimized: optimized.prologue optimized.meat optimized.epilogue
debug: debug.prologue debug.meat debug.epilogue


$(PLATFORM_SRC)\classes\$(PKGDIR) : FORCE
	$(MKDIR) $@

$(PLATFORM_SRC)\native\$(PKGDIR) : FORCE
	$(MKDIR) $@

$(SHARE_SRC)\classes\$(PKGDIR) : FORCE $(SHARE_SRC)\native\$(PKGDIR)
	$(MKDIR) $@

$(SHARE_SRC)\native\$(PKGDIR) : FORCE
	$(MKDIR) $@

OPTIMIZED = $(LIBDIR)\$(LIBRARY).dll
DEBUG	  = $(LIBDIR)\$(LIBRARY)_g.dll

FILES_obj = $(FILES_c:.c=.obj) $(PLATFORM_FILES_c:.c=.obj) $(FILES_cpp:.cpp=.obj)

#
# Include common library utilities.
#
EXTRA_INCLUDES = $(EXTRA_INCLUDES) -I$(SHARE_SRC)\native\common \
		-I$(PLATFORM_SRC)\native\common

!include $(BUILDDIR)\makefiles\rules.nmk

#
# This is a hack in rules.nmk that we have to get around.
_JAVAC_CMD=$(JAVAC_CMD) -classpath $(SOUND_CLASSPATH) -d $(CLASSBINDIR) $(JAVACFLAGS)


#
# Create libdir.
#
optimized.prologue debug.prologue: $(LIBDIR)

#
# Library building rules.
#
$(BINDIR)\$(LIBRARY)$G.dll: $(LIBRARY)$G.dll 
	copy $? $@

$(LIBDIR)\$(LIBRARY)$G.dll: $(LIBRARY)$G.dll
	copy $? $@

# -base: argument is hacked.
# Essentially we are going to adjust the base
# using the cfg file, and the name "audio".
$(LIBRARY)$G.dll: $(OBJDIR) classes classheaders $(LIBRARY)$G.lcf
	$(LINK) -dll -out:$@ $(LFLAGS) @$(LIBRARY)$G.lcf $(OTHER_LCF) \
		-base:@$(BUILDDIR)\makefiles\dllbase.cfg,audio \
		$(JVMLIB) $(JAVALIB) $(LIBS)

$(LIBRARY)$G.lcf: $(FILES_obj)
	@echo Created <<$@
$**
<<KEEP

# Note that the classpath here, has to include the local and the classbindir
# in order to work properly.
classheaders: $(FILES_class)
	$(JAVAH_CMD) $(JAVAHFLAGS) -classpath $(SOUND_CLASSPATH)$(CLASSBINDIR) \
		-d $(CLASSHEADERS) $(EXPORTED_CLASSES)

libstubs.c: $(FILES_class)
	$(JAVAH_CMD) -classpath $(CLASSES) -o $@ -stubs $(EXPORTED_CLASSES)

#
# Install a .lib file if required.
#
!if "$(INSTALL_DOT_LIB)" == "true"
optimized.epilogue: $(LIBDIR)\$(LIBRARY).lib
debug.epilogue: $(LIBDIR)\$(LIBRARY)_g.lib
clean: FORCE
	$(DEL) $(LIBDIR)\$(LIBRARY).lib $(LIBDIR)\$(LIBRARY)_g.lib

$(LIBDIR)\$(LIBRARY).lib: $(LIBRARY).lib
	copy $? $@

$(LIBDIR)\$(LIBRARY)_g.lib: $(LIBRARY)_g.lib
	copy $? $@

!endif

#
# vpaths (works for all libaries except jvm.dll and awt.dll.
#
{$(PLATFORM_SRC)\native\$(PKGDIR)}.c{$(OBJDIR)}.obj :
	@echo Compiling: $<
	$(CC) $(CFLAGS) -I$(PLATFORM_SRC)\native\$(PKGDIR) \
			-I$(SHARE_SRC)\native\$(PKGDIR) -c -Fo$@ $<

{$(SHARE_SRC)\native\$(PKGDIR)}.c{$(OBJDIR)}.obj :
	@echo Compiling: $<
	$(CC) $(CFLAGS) -I$(PLATFORM_SRC)\native\$(PKGDIR) \
			-I$(SHARE_SRC)\native\$(PKGDIR) -c -Fo$@ $<

{$(SHARE_SRC)\native\$(PKGDIR)}.cpp{$(OBJDIR)}.obj :
	@echo Compiling: $<
	$(CC) $(CFLAGS) -I$(PLATFORM_SRC)\native\$(LIBRARY) \
			-I$(SHARE_SRC)\native\$(LIBRARY) -c -Fo$@ $<


# The sed script for the c files.
C_SED_CMD = $(SED) -e s/sun_audio/com_sun_media_jmf_audio/g
JAVA_SED_CMD = $(SED) -e s/sun.audio/com.sun.media.jmf.audio/g
H_SED_CMD = $(SED) -e s:sun/audio:$(PKGDIR:\=/):g

#
# vpaths for moving the source around
#

# We have to do this by brute force, as they aren't listed anywhere.
header_files: FORCE
	-cp $(SHARE_SRC)\native\sun\audio\*.h $(SHARE_SRC)\native\$(PKGDIR)
	-cp $(PLATFORM_SRC)\native\sun\audio\*.h $(PLATFORM_SRC)\native\$(PKGDIR)
	$(H_SED_CMD) $(SHARE_SRC)\native\sun\audio\Hae_Impl.h >$(SHARE_SRC)\native\$(PKGDIR)\Hae_Impl.h


{$(SHARE_SRC)\classes\sun\audio}.java{$(SHARE_SRC)\classes\$(PKGDIR)}.java :
	@echo Moving: $? to $@
	$(JAVA_SED_CMD) $? > $@

{$(SHARE_SRC)\classes\sun\audio}.java{$(PLATFORM_SRC)\classes\$(PKGDIR)}.java :
	@echo Moving: $? to $@
	$(JAVA_SED_CMD) $? > $@

{$(PLATFORM_SRC)\native\sun\audio}.c{$(PLATFORM_SRC)\native\$(PKGDIR)}.c :
	@echo Moving: $? to $@
	$(C_SED_CMD) $? > $@

{$(SHARE_SRC)\native\sun\audio}.c{$(SHARE_SRC)\native\$(PKGDIR)}.c :
	@echo Moving: $? to $@
	$(C_SED_CMD) $? > $@

{$(SHARE_SRC)\native\sun\audio}.cpp{$(SHARE_SRC)\native\$(PKGDIR)}.cpp :
	@echo Moving: $? to $@
	$(C_SED_CMD) $? > $@

#
# Cleanup.
#
clobber: library.clobber source.clobber

clean: library.clean classes.clean classheaders.clean source.clean

# This is the generated source.
source.clobber source.clean: FORCE
	-$(DEL) $(SHARE_SRC)\native\$(PKGDIR) $(SHARE_SRC)\classes\$(PKGDIR)
	-$(DEL) $(PLATFORM_SRC)\native\$(PKGDIR) $(PLATFORM_SRC)\classes\$(PKGDIR)

classheaders.clean: FORCE
	-$(DELTREE) $(CLASSHEADERS)

library.clobber: FORCE
	-$(DEL) $(OPTIMIZED) $(DEBUG) $(LIBRARY).dll $(LIBRARY)_g.dll
	-$(DEL) *.pdb 

library.clean: FORCE
	-$(DEL) libstubs.c 
	-$(DEL) $(LIBRARY).lcf $(LIBRARY)_g.lcf
	-$(DEL) $(LIBRARY).map $(LIBRARY)_g.map
	-$(DEL) $(LIBRARY).lib $(LIBRARY)_g.lib
	-$(DEL) $(LIBRARY).exp $(LIBRARY)_g.exp
	-$(DELTREE) $(OBJROOTS)


# This mess is required to get the name of the
# library we want to build into the class that
# will System.loadLibrary() it.
#
AUDIO_ENV_CLASS= $(SHARE_SRC)/classes/$(PKGDIR)/AudioEnvironment.java
$(SHARE_SRC)/classes/$(PKGDIR)/AudioEnvironment.java : Makefile $(SOURCE_DIRS)
	@echo /* $(HEADER_MESSAGE) */ >$(AUDIO_ENV_CLASS)
	@echo package $(PKG); >>$(AUDIO_ENV_CLASS)
	@echo class AudioEnvironment { >>$(AUDIO_ENV_CLASS)
	@echo static final String libraryName = "$(LIBRARY)"; >>$(AUDIO_ENV_CLASS)
	@echo static boolean debug = $(AUDIO_DEBUG); >>$(AUDIO_ENV_CLASS)
	@echo static boolean trace = $(AUDIO_TRACE); >>$(AUDIO_ENV_CLASS)
	@echo static final String version = "$(FULL_VERSION)"; >>$(AUDIO_ENV_CLASS)
	@echo } >> $(AUDIO_ENV_CLASS)

#
# This ends the AUDIO_EXTENSION portion of the Makefile
# Thank you for you patience.
#
!else

# This is the JDK1.2 way of doing things.

# Rules
!include $(BUILDDIR)\makefiles\library.nmk

# This ends the AUDIO_EXTENSION ifdef
!endif


#
# The General MIDI Soundbank used by the Java Sound Engine to play
# MIDI and RMF formatted audio files.
#
SOUNDBANK_SRC_DIR  = $(SHARE_SRC)\lib\audio
SOUNDBANK_DEST_DIR = $(LIBDIR)\audio

# NOTE: To keep the soundbank from being included in the build,
# comment out the next few lines.
optmized debug: soundbank.install
clean:		soundbank.clean

soundbank.install: $(SOUNDBANK_DEST_DIR)\soundbank.gm

$(SOUNDBANK_DEST_DIR)\soundbank.gm: $(SOUNDBANK_SRC_DIR)\soundbank.gm
	@if not exist $(SOUNDBANK_DEST_DIR) mkdir $(SOUNDBANK_DEST_DIR)
	copy $(SOUNDBANK_SRC_DIR)\soundbank.gm $@

soundbank.clean: FORCE
	-$(DEL) $(SOUNDBANK_DEST_DIR)\soundbank.gm
	-$(DELTREE) $(SOUNDBANK_DEST_DIR)

#
# Find the .cpp files.
#
{$(SHARE_SRC)\native\$(PKG)}.cpp{$(OBJDIR)}.obj:
	$(CC) $(CFLAGS) -c -Fo$@ $<

MF_DEBUG: FORCE
	@echo $(SRC_FILES_c)
