###############################################################################
# BRLTTY - A background process providing access to the console screen (when in
#          text mode) for a blind person using a refreshable braille display.
#
# Copyright (C) 1995-2012 by The BRLTTY Developers.
#
# BRLTTY comes with ABSOLUTELY NO WARRANTY.
#
# This is free software, placed under the terms of the
# GNU General Public License, as published by the Free Software
# Foundation; either version 2 of the License, or (at your option) any
# later version. Please see the file LICENSE-GPL for details.
#
# Web Page: http://mielke.cc/brltty/
#
# This software is maintained by Dave Mielke <dave@mielke.cc>.
###############################################################################

all: all-brltty brltty-trtxt$X brltty-ttb$X brltty-ctb$X $(ALL_XBRLAPI) $(ALL_API_BINDINGS)
everything: all all-brltest all-scrtest all-spktest all-ktbtest tunetest$X $(ALL_API)
all-brltty: brltty$X $(BRAILLE_DRIVERS) $(SPEECH_DRIVERS) $(SCREEN_DRIVERS)
all-brltest: brltest$X $(BRAILLE_DRIVERS)
all-spktest: spktest$X $(SPEECH_DRIVERS)
all-scrtest: scrtest$X $(SCREEN_DRIVERS)
all-ktbtest: ktbtest$X $(BRAILLE_DRIVERS)
all-api: apitest$X $(ALL_XBRLAPI) $(ALL_API_BINDINGS)
all-xbrlapi: xbrlapi$X

###############################################################################

BASE_OBJECTS = log.$O file.$O device.$O parse.$O timing.$O io_misc.$O

log.$O:
	$(CC) $(LIBCFLAGS) -c $(SRC_DIR)/log.c

file.$O:
	$(CC) $(LIBCFLAGS) -c $(SRC_DIR)/file.c

device.$O:
	$(CC) $(LIBCFLAGS) -c $(SRC_DIR)/device.c

parse.$O:
	$(CC) $(LIBCFLAGS) -c $(SRC_DIR)/parse.c

timing.$O:
	$(CC) $(LIBCFLAGS) -c $(SRC_DIR)/timing.c

io_misc.$O:
	$(CC) $(LIBCFLAGS) -c $(SRC_DIR)/io_misc.c

###############################################################################

PROGRAM_OBJECTS = program.$O pid.$O options.$O $(BASE_OBJECTS)

program.$O:
	$(CC) $(LIBCFLAGS) -c $(SRC_DIR)/program.c

pid.$O:
	$(CC) $(LIBCFLAGS) -c $(SRC_DIR)/pid.c

options.$O:
	$(CC) $(LIBCFLAGS) -c $(SRC_DIR)/options.c

###############################################################################

PROGRAM_OBJECTS_FOR_HOST = $(PROGRAM_OBJECTS) $(SYSTEM_OBJECT)

sys_skeleton.$O:
	$(CC) $(LIBCFLAGS) -c $(SRC_DIR)/sys_skeleton.c

sys_darwin.$O:
	$(CC) $(LIBCFLAGS) -c $(SRC_DIR)/sys_darwin.c

sys_freebsd.$O:
	$(CC) $(LIBCFLAGS) -c $(SRC_DIR)/sys_freebsd.c

sys_grub.$O:
	$(CC) $(LIBCFLAGS) -c $(SRC_DIR)/sys_grub.c

sys_hpux.$O:
	$(CC) $(LIBCFLAGS) -I/opt/audio/include -c $(SRC_DIR)/sys_hpux.c

sys_hurd.$O:
	$(CC) $(LIBCFLAGS) -c $(SRC_DIR)/sys_hurd.c

sys_kfreebsd.$O:
	$(CC) $(LIBCFLAGS) -c $(SRC_DIR)/sys_kfreebsd.c

sys_linux.$O:
	$(CC) $(LIBCFLAGS) -c $(SRC_DIR)/sys_linux.c

sys_msdos.$O:
	$(CC) $(LIBCFLAGS) -c $(SRC_DIR)/sys_msdos.c

sys_netbsd.$O:
	$(CC) $(LIBCFLAGS) -c $(SRC_DIR)/sys_netbsd.c

sys_openbsd.$O:
	$(CC) $(LIBCFLAGS) -c $(SRC_DIR)/sys_openbsd.c

sys_osf.$O:
	$(CC) $(LIBCFLAGS) -c $(SRC_DIR)/sys_osf.c

sys_qnx.$O:
	$(CC) $(LIBCFLAGS) -c $(SRC_DIR)/sys_qnx.c

sys_solaris.$O:
	$(CC) $(LIBCFLAGS) -c $(SRC_DIR)/sys_solaris.c

sys_windows.$O:
	$(CC) $(LIBCFLAGS) -c $(SRC_DIR)/sys_windows.c

###############################################################################

kbd.$O:
	$(CC) $(LIBCFLAGS) -c $(SRC_DIR)/kbd.c

kbd_none.$O:
	$(CC) $(LIBCFLAGS) -c $(SRC_DIR)/kbd_none.c

kbd_linux.$O:
	$(CC) $(LIBCFLAGS) -c $(SRC_DIR)/kbd_linux.c

###############################################################################

async.$O:
	$(CC) $(LIBCFLAGS) -c $(SRC_DIR)/async.c

auth.$O:
	$(CC) $(LIBCFLAGS) -c $(SRC_DIR)/auth.c

dataarea.$O:
	$(CC) $(LIBCFLAGS) -c $(SRC_DIR)/dataarea.c

datafile.$O:
	$(CC) $(LIBCFLAGS) -c $(SRC_DIR)/datafile.c

lock.$O:
	$(CC) $(LIBCFLAGS) -c $(SRC_DIR)/lock.c

queue.$O:
	$(CC) $(LIBCFLAGS) -c $(SRC_DIR)/queue.c

unicode.$O:
	$(CC) $(LIBCFLAGS) -c $(SRC_DIR)/unicode.c

###############################################################################

PREFS_OBJECTS = prefs.$O prefs_table.$O

prefs.$O:
	$(CC) $(LIBCFLAGS) -c $(SRC_DIR)/prefs.c

prefs_table.$O:
	$(CC) $(LIBCFLAGS) -c $(SRC_DIR)/prefs_table.c

###############################################################################

io_generic.$O:
	$(CC) $(LIBCFLAGS) -c $(SRC_DIR)/io_generic.c

###############################################################################

serial.$O:
	$(CC) $(LIBCFLAGS) -c $(SRC_DIR)/serial.c

serial_none.$O:
	$(CC) $(LIBCFLAGS) -c $(SRC_DIR)/serial_none.c

serial_grub.$O:
	$(CC) $(LIBCFLAGS) -c $(SRC_DIR)/serial_grub.c

serial_msdos.$O:
	$(CC) $(LIBCFLAGS) -c $(SRC_DIR)/serial_msdos.c

serial_termios.$O:
	$(CC) $(LIBCFLAGS) -c $(SRC_DIR)/serial_termios.c

serial_windows.$O:
	$(CC) $(LIBCFLAGS) -c $(SRC_DIR)/serial_windows.c

###############################################################################

usb.$O:
	$(CC) $(LIBCFLAGS) -c $(SRC_DIR)/usb.c

usb_hid.$O:
	$(CC) $(LIBCFLAGS) -c $(SRC_DIR)/usb_hid.c

usb_serial.$O:
	$(CC) $(LIBCFLAGS) -c $(SRC_DIR)/usb_serial.c

usb_none.$O:
	$(CC) $(LIBCFLAGS) $(USB_CFLAGS) -c $(SRC_DIR)/usb_none.c

usb_darwin.$O:
	$(CC) $(LIBCFLAGS) $(USB_CFLAGS) -c $(SRC_DIR)/usb_darwin.c

usb_freebsd.$O:
	$(CC) $(LIBCFLAGS) $(USB_CFLAGS) -c $(SRC_DIR)/usb_freebsd.c

usb_grub.$O:
	$(CC) $(LIBCFLAGS) $(USB_CFLAGS) -c $(SRC_DIR)/usb_grub.c

usb_kfreebsd.$O:
	$(CC) $(LIBCFLAGS) $(USB_CFLAGS) -c $(SRC_DIR)/usb_kfreebsd.c

usb_libusb.$O:
	$(CC) $(LIBCFLAGS) $(USB_CFLAGS) -c $(SRC_DIR)/usb_libusb.c

usb_libusb-1.0.$O:
	$(CC) $(LIBCFLAGS) $(USB_CFLAGS) -c $(SRC_DIR)/usb_libusb-1.0.c

usb_linux.$O:
	$(CC) $(LIBCFLAGS) $(USB_CFLAGS) -c $(SRC_DIR)/usb_linux.c

usb_netbsd.$O:
	$(CC) $(LIBCFLAGS) $(USB_CFLAGS) -c $(SRC_DIR)/usb_netbsd.c

usb_openbsd.$O:
	$(CC) $(LIBCFLAGS) $(USB_CFLAGS) -c $(SRC_DIR)/usb_openbsd.c

usb_solaris.$O:
	$(CC) $(LIBCFLAGS) $(USB_CFLAGS) -c $(SRC_DIR)/usb_solaris.c

###############################################################################

bluetooth.$O:
	$(CC) $(LIBCFLAGS) -c $(SRC_DIR)/bluetooth.c

bluetooth_none.$O:
	$(CC) $(LIBCFLAGS) $(BLUETOOTH_CFLAGS) -c $(SRC_DIR)/bluetooth_none.c

bluetooth_linux.$O:
	$(CC) $(LIBCFLAGS) $(BLUETOOTH_CFLAGS) -c $(SRC_DIR)/bluetooth_linux.c

bluetooth_windows.$O:
	$(CC) $(LIBCFLAGS) $(BLUETOOTH_CFLAGS) -D__USE_W32_SOCKETS -c $(SRC_DIR)/bluetooth_windows.c

###############################################################################

charset.$O:
	$(CC) $(LIBCFLAGS) -c $(SRC_DIR)/charset.c

charset_none.$O:
	$(CC) $(LIBCFLAGS) -c $(SRC_DIR)/charset_none.c

charset_grub.$O:
	$(CC) $(LIBCFLAGS) -c $(SRC_DIR)/charset_grub.c

charset_iconv.$O:
	$(CC) $(LIBCFLAGS) -c $(SRC_DIR)/charset_iconv.c

charset_msdos.$O:
	$(CC) $(LIBCFLAGS) -c $(SRC_DIR)/charset_msdos.c

charset_windows.$O:
	$(CC) $(LIBCFLAGS) -c $(SRC_DIR)/charset_windows.c

###############################################################################

hostcmd.$O:
	$(CC) $(LIBCFLAGS) -c $(SRC_DIR)/hostcmd.c

hostcmd_none.$O:
	$(CC) $(LIBCFLAGS) -c $(SRC_DIR)/hostcmd_none.c

hostcmd_unix.$O:
	$(CC) $(LIBCFLAGS) -c $(SRC_DIR)/hostcmd_unix.c

###############################################################################

mount.$O:
	$(CC) $(LIBCFLAGS) -c $(SRC_DIR)/mount.c

mount_none.$O:
	$(CC) $(LIBCFLAGS) -c $(SRC_DIR)/mount_none.c

mount_mntent.$O:
	$(CC) $(LIBCFLAGS) -c $(SRC_DIR)/mount_mntent.c

mount_mnttab.$O:
	$(CC) $(LIBCFLAGS) -c $(SRC_DIR)/mount_mnttab.c

###############################################################################

IO_OBJECTS = io_generic.$O $(SERIAL_OBJECTS) $(USB_OBJECTS) $(BLUETOOTH_OBJECTS)
BRAILLE_OBJECTS = brl.$O $(BRAILLE_DRIVER_OBJECTS) $(IO_OBJECTS)

brl.$O:
	$(CC) $(LIBCFLAGS) -c $(SRC_DIR)/brl.c

###############################################################################

SPEECH_OBJECTS = $(SPEECH_OBJECT) $(SPEECH_DRIVER_OBJECTS)

spk.$O:
	$(CC) $(LIBCFLAGS) -c $(SRC_DIR)/spk.c

###############################################################################

SCREEN_OBJECTS = scr.$O scr_base.$O scr_help.$O scr_frozen.$O scr_menu.$O menu_prefs.$O scr_main.$O scr_real.$O routing.$O $(SCREEN_DRIVER_OBJECTS)

scr.$O:
	$(CC) $(LIBCFLAGS) -c $(SRC_DIR)/scr.c

scr_base.$O:
	$(CC) $(LIBCFLAGS) -c $(SRC_DIR)/scr_base.c

scr_help.$O:
	$(CC) $(LIBCFLAGS) -c $(SRC_DIR)/scr_help.c

scr_frozen.$O:
	$(CC) $(LIBCFLAGS) -c $(SRC_DIR)/scr_frozen.c

scr_menu.$O:
	$(CC) $(LIBCFLAGS) -c $(SRC_DIR)/scr_menu.c

menu_prefs.$O:
	$(CC) $(LIBCFLAGS) -c $(SRC_DIR)/menu_prefs.c

scr_main.$O:
	$(CC) $(LIBCFLAGS) -c $(SRC_DIR)/scr_main.c

scr_real.$O: scr.auto.h
	$(CC) $(LIBCFLAGS) -c $(SRC_DIR)/scr_real.c

routing.$O:
	$(CC) $(LIBCFLAGS) -c $(SRC_DIR)/routing.c

###############################################################################

TUNE_OBJECTS = tunes.$O notes.$O $(BEEPER_OBJECTS) $(PCM_OBJECTS) $(MIDI_OBJECTS) $(FM_OBJECTS)

tunes.$O:
	$(CC) $(LIBCFLAGS) -c $(SRC_DIR)/tunes.c

notes.$O:
	$(CC) $(LIBCFLAGS) -c $(SRC_DIR)/notes.c

beeper.$O:
	$(CC) $(LIBCFLAGS) -c $(SRC_DIR)/beeper.c

pcm.$O:
	$(CC) $(LIBCFLAGS) -c $(SRC_DIR)/pcm.c

midi.$O:
	$(CC) $(LIBCFLAGS) -c $(SRC_DIR)/midi.c

fm.$O:
	$(CC) $(LIBCFLAGS) -c $(SRC_DIR)/fm.c

adlib.$O:
	$(CC) $(LIBCFLAGS) -c $(SRC_DIR)/adlib.c

###############################################################################

API_OBJS = brlapi_client.$O
API_NAME = brlapi
API_ARC = $(ARC_PFX)$(API_NAME).$(ARC_EXT)
API_LIB = $(LIB_PFX)$(API_NAME).$(LIB_EXT)
API_DLL = $(LIB_PFX)$(API_NAME)-$(API_VERSION).$(LIB_EXT)
API_IMPLIB = $(ARC_PFX)$(API_NAME).$(LIB_EXT).$(ARC_EXT)
API_IMPLIB_VERSIONED = $(ARC_PFX)$(API_NAME)-$(API_RELEASE).$(LIB_EXT).$(ARC_EXT)
API_DEF = $(API_NAME).def

api: $(API_DYNAMIC_LIBRARY) $(API_ARC)

api-dynamic-library: $(API_LIB)
$(API_LIB): $(API_OBJS)
	$(MKLIB:<name>=${API_LIB}.${API_VERSION}) $@ $(API_OBJS) $(API_LIBRARIES)

api-dynamic-library-windows: $(API_DLL)
$(API_DLL): $(API_OBJS)
	-rm -f implib.a lib.def
	$(MKLIB:<name>=${API_LIB}.${API_VERSION}) $@ $(API_OBJS) $(API_LIBRARIES)
	[ ! -f implib.a ] || mv implib.a $(API_IMPLIB)
	[ ! -f lib.def ] || mv lib.def $(API_DEF)

$(API_ARC): $(API_OBJS)
	ar rc $@ $(API_OBJS)
	$(RANLIB) $@

brlapi_client.$O:
	$(CC) $(LIBCFLAGS) -c $(SRC_DIR)/brlapi_client.c

brlapi_constants.h: $(SRC_DIR)/brldefs.h $(SRC_DIR)/brldefs.awk $(SRC_DIR)/brlapi_constants.awk
	$(AWK) -f $(SRC_DIR)/brldefs.awk -f $(SRC_DIR)/brlapi_constants.awk $(SRC_DIR)/brldefs.h >$@

brlapi_keytab.auto.h: $(SRC_DIR)/brldefs.h $(SRC_DIR)/brldefs.awk $(SRC_DIR)/brlapi_keytab.awk
	$(AWK) -f $(SRC_DIR)/brldefs.awk -f $(SRC_DIR)/brlapi_keytab.awk $(SRC_DIR)/brldefs.h >$@

brlapi_brldefs.auto.h: $(SRC_DIR)/brldefs.h $(SRC_DIR)/brldefs.awk $(SRC_DIR)/brlapi_brldefs.awk
	$(AWK) -f $(SRC_DIR)/brldefs.awk -f $(SRC_DIR)/brlapi_brldefs.awk $(SRC_DIR)/brldefs.h >$@

brlapi_server.$O:
	$(CC) $(LIBCFLAGS) -c $(SRC_DIR)/brlapi_server.c

brlapi_keyranges.$O:
	$(CC) $(LIBCFLAGS) -c $(SRC_DIR)/brlapi_keyranges.c

all-api-bindings: api
	set -- $(API_BINDINGS); \
	for language \
	do (cd $(BLD_TOP)$(BND_DIR)/$$language && $(MAKE) all) || exit 1; \
	done

install-api-bindings: all-api-bindings
	set -- $(API_BINDINGS); \
	for language \
	do (cd $(BLD_TOP)$(BND_DIR)/$$language && $(MAKE) install) || exit 1; \
	done

uninstall-api-bindings:
	set -- $(API_BINDINGS); \
	for language \
	do (cd $(BLD_TOP)$(BND_DIR)/$$language && $(MAKE) uninstall); \
	done

###############################################################################

CORE_OBJECTS = brltty.$O $(PROGRAM_OBJECTS_FOR_HOST) config.$O $(PREFS_OBJECTS) menu.$O ses.$O status.$O clipboard.$O touch.$O async.$O $(CHARSET_OBJECTS) dataarea.$O datafile.$O $(HOSTCMD_OBJECTS) lock.$O $(MOUNT_OBJECTS) queue.$O unicode.$O cmd.$O scancodes.$O ttb_compile.$O ttb_native.$O ttb_translate.$O atb_compile.$O atb_translate.$O $(CTB_OBJECTS) ktb_compile.$O ktb_translate.$O ktb_list.$O ktb_keyboard.$O $(KEYBOARD_OBJECTS) $(TUNE_OBJECTS) hidkeys.$O drivers.$O driver.$O $(SCREEN_OBJECTS) $(BRAILLE_OBJECTS) $(SPEECH_OBJECTS) $(API_OBJECTS)
CORE_NAME = brltty

brltty-core: $(CORE_OBJECTS)

brltty.$O:
	$(CC) $(LIBCFLAGS) $(ICU_INCLUDES) -c $(SRC_DIR)/brltty.c

config.$O:
	$(CC) $(LIBCFLAGS) -c $(SRC_DIR)/config.c

menu.$O:
	$(CC) $(LIBCFLAGS) -c $(SRC_DIR)/menu.c

ses.$O:
	$(CC) $(LIBCFLAGS) -c $(SRC_DIR)/ses.c

status.$O:
	$(CC) $(LIBCFLAGS) -c $(SRC_DIR)/status.c

clipboard.$O:
	$(CC) $(LIBCFLAGS) -c $(SRC_DIR)/clipboard.c

touch.$O:
	$(CC) $(LIBCFLAGS) -c $(SRC_DIR)/touch.c

cmd.$O:
	$(CC) $(LIBCFLAGS) -c $(SRC_DIR)/cmd.c

hidkeys.$O:
	$(CC) $(LIBCFLAGS) -c $(SRC_DIR)/hidkeys.c

scancodes.$O:
	$(CC) $(LIBCFLAGS) -c $(SRC_DIR)/scancodes.c

drivers.$O:
	$(CC) $(LIBCFLAGS) -c $(SRC_DIR)/drivers.c

driver.$O:
	$(CC) $(LIBCFLAGS) -c $(SRC_DIR)/driver.c

###############################################################################

BRLTTY_OBJECTS = main.$O $(CORE_OBJECTS)
BRLTTY_LIBRARIES = $(BRAILLE_DRIVER_LIBRARIES) $(SPEECH_DRIVER_LIBRARIES) $(SCREEN_DRIVER_LIBRARIES) $(USB_LIBS) $(BLUETOOTH_LIBS) $(ICU_LIBRARIES) $(LDLIBS)

brltty$X: $(BRLTTY_OBJECTS)
	$(CC) $(LDFLAGS) -o $@ $(BRLTTY_OBJECTS) $(BRLTTY_LIBRARIES)

main.$O:
	$(CC) $(CFLAGS) -c $(SRC_DIR)/main.c

###############################################################################

BRLTTY_CORE_LIB = $(LIB_PFX)$(CORE_NAME)_core.$(LIB_EXT)

core-library: $(BRLTTY_CORE_LIB)

$(BRLTTY_CORE_LIB): $(CORE_OBJECTS)
	$(MKLIB:<name>=$@) $@ $(CORE_OBJECTS) $(BRLTTY_LIBRARIES)

###############################################################################

BRLTTY_JNI_JAR = $(CORE_NAME).jar
BRLTTY_JNI_LIB = $(LIB_PFX)$(CORE_NAME)_jni.$(LIB_EXT)

brltty-jni: $(BRLTTY_JNI_JAR) $(BRLTTY_JNI_LIB)

$(BRLTTY_JNI_JAR): brltty.class
	jar cf $@ brltty.class

brltty.class: $(SRC_DIR)/brltty.java
	javac $(SRC_DIR)/brltty.java

$(BRLTTY_JNI_LIB): brltty_jni.$O $(BRLTTY_CORE_LIB)
	$(MKLIB:<name>=$@) $@ brltty_jni.$O

brltty_jni.$O:
	$(CC) $(LIBCFLAGS) -c $(SRC_DIR)/brltty_jni.c

###############################################################################

BRLTTY_TRTXT_OBJECTS = brltty-trtxt.$O $(PROGRAM_OBJECTS_FOR_HOST) ttb_translate.$O ttb_compile.$O ttb_native.$O unicode.$O $(CHARSET_OBJECTS) datafile.$O lock.$O queue.$O dataarea.$O

brltty-trtxt$X: $(BRLTTY_TRTXT_OBJECTS)
	$(CC) $(LDFLAGS) -o $@ $(BRLTTY_TRTXT_OBJECTS) $(ICU_LIBRARIES) $(LDLIBS)

brltty-trtxt.$O:
	$(CC) $(CFLAGS) -c $(SRC_DIR)/brltty-trtxt.c

###############################################################################

BRLTTY_TTB_OBJECTS = brltty-ttb.$O $(PROGRAM_OBJECTS_FOR_HOST) lock.$O $(CHARSET_OBJECTS) datafile.$O queue.$O dataarea.$O ttb_compile.$O ttb_native.$O ttb_gnome.$O ttb_louis.$O

brltty-ttb$X: $(BRLTTY_TTB_OBJECTS) $(BUILD_API)
	$(CC) $(LDFLAGS) -o $@ $(BRLTTY_TTB_OBJECTS) $(API_REF) $(CLIBS) $(ICU_LIBRARIES) $(LDLIBS)

brltty-ttb.$O:
	$(CC) $(CFLAGS) $(ICU_INCLUDES) -c $(SRC_DIR)/brltty-ttb.c

ttb_compile.$O:
	$(CC) $(LIBCFLAGS) -c $(SRC_DIR)/ttb_compile.c

ttb_native.$O:
	$(CC) $(LIBCFLAGS) -c $(SRC_DIR)/ttb_native.c

ttb_gnome.$O:
	$(CC) $(LIBCFLAGS) -c $(SRC_DIR)/ttb_gnome.c

ttb_louis.$O:
	$(CC) $(LIBCFLAGS) -c $(SRC_DIR)/ttb_louis.c

ttb_translate.$O:
	$(CC) $(LIBCFLAGS) -c $(SRC_DIR)/ttb_translate.c

check-text-tables: brltty-ttb$X
	for file in $(SRC_TOP)$(TBL_DIR)/*.ttb; do LD_LIBRARY_PATH=$(BLD_DIR) ./brltty-ttb$X -T$(SRC_TOP)$(TBL_DIR) $$file; done

###############################################################################

BRLTTY_CTB_OBJECTS = brltty-ctb.$O $(PROGRAM_OBJECTS_FOR_HOST) $(PREFS_OBJECTS) datafile.$O queue.$O dataarea.$O ttb_compile.$O ttb_native.$O ttb_translate.$O ctb_compile.$O ctb_translate.$O $(CHARSET_OBJECTS) $(HOSTCMD_OBJECTS) lock.$O unicode.$O

brltty-ctb$X: $(BRLTTY_CTB_OBJECTS)
	$(CC) $(LDFLAGS) -o $@ $(BRLTTY_CTB_OBJECTS) $(ICU_LIBRARIES) $(LDLIBS)

brltty-ctb.$O:
	$(CC) $(CFLAGS) -c $(SRC_DIR)/brltty-ctb.c

ctb_compile.$O:
	$(CC) $(LIBCFLAGS) $(ICU_INCLUDES) -c $(SRC_DIR)/ctb_compile.c

ctb_translate.$O:
	$(CC) $(LIBCFLAGS) $(ICU_INCLUDES) -c $(SRC_DIR)/ctb_translate.c

check-contraction-tables: brltty-ctb$X
	for file in $(SRC_TOP)$(TBL_DIR)/*.ctb; do ./brltty-ctb$X -T$(SRC_TOP)$(TBL_DIR) -c$$file </dev/null; done

###############################################################################

BRLTEST_OBJECTS = brltest.$O $(PROGRAM_OBJECTS_FOR_HOST) $(MOUNT_OBJECTS) ttb_translate.$O cmd.$O $(CHARSET_OBJECTS) lock.$O hidkeys.$O drivers.$O driver.$O $(BRAILLE_OBJECTS) touch.$O

brltest$X: $(BRLTEST_OBJECTS)
	$(CC) $(LDFLAGS) -o $@ $(BRLTEST_OBJECTS) $(BRAILLE_DRIVER_LIBRARIES) $(USB_LIBS) $(BLUETOOTH_LIBS) $(LDLIBS)

brltest.$O:
	$(CC) $(CFLAGS) -c $(SRC_DIR)/brltest.c

###############################################################################

SPKTEST_OBJECTS = spktest.$O $(PROGRAM_OBJECTS_FOR_HOST) lock.$O $(CHARSET_OBJECTS) drivers.$O driver.$O $(SPEECH_OBJECTS)

spktest$X: $(SPKTEST_OBJECTS)
	$(CC) $(LDFLAGS) -o $@ $(SPKTEST_OBJECTS) $(SPEECH_DRIVER_LIBRARIES) $(LDLIBS)

spktest.$O:
	$(CC) $(CFLAGS) -c $(SRC_DIR)/spktest.c

###############################################################################

SCRTEST_OBJECTS = scrtest.$O $(PROGRAM_OBJECTS_FOR_HOST) drivers.$O driver.$O $(SCREEN_OBJECTS) async.$O $(CHARSET_OBJECTS) $(HOSTCMD_OBJECTS) lock.$O queue.$O unicode.$O scancodes.$O

scrtest$X: $(SCRTEST_OBJECTS)
	$(CC) $(LDFLAGS) -o $@ $(SCRTEST_OBJECTS) $(SCREEN_DRIVER_LIBRARIES) $(ICU_LIBRARIES) $(LDLIBS)

scrtest.$O:
	$(CC) $(CFLAGS) -c $(SRC_DIR)/scrtest.c

###############################################################################

atb_compile.$O:
	$(CC) $(LIBCFLAGS) -c $(SRC_DIR)/atb_compile.c

atb_translate.$O:
	$(CC) $(LIBCFLAGS) -c $(SRC_DIR)/atb_translate.c

###############################################################################

KTBTEST_OBJECTS = ktbtest.$O $(PROGRAM_OBJECTS_FOR_HOST) ktb_compile.$O ktb_list.$O ktb_keyboard.$O datafile.$O queue.$O unicode.$O $(CHARSET_OBJECTS) lock.$O cmd.$O async.$O scancodes.$O hidkeys.$O drivers.$O driver.$O $(BRAILLE_OBJECTS) $(MOUNT_OBJECTS) touch.$O ttb_translate.$O ttb_compile.$O ttb_native.$O dataarea.$O

ktbtest$X: $(KTBTEST_OBJECTS)
	$(CC) $(LDFLAGS) -o $@ $(KTBTEST_OBJECTS) $(BRAILLE_DRIVER_LIBRARIES) $(USB_LIBS) $(BLUETOOTH_LIBS) $(ICU_LIBRARIES) $(LDLIBS)

ktbtest.$O:
	$(CC) $(CFLAGS) -c $(SRC_DIR)/ktbtest.c

ktb_compile.$O:
	$(CC) $(LIBCFLAGS) -c $(SRC_DIR)/ktb_compile.c

ktb_translate.$O:
	$(CC) $(LIBCFLAGS) -c $(SRC_DIR)/ktb_translate.c

ktb_list.$O:
	$(CC) $(LIBCFLAGS) -c $(SRC_DIR)/ktb_list.c

ktb_keyboard.$O:
	$(CC) $(LIBCFLAGS) -c $(SRC_DIR)/ktb_keyboard.c

check-key-tables: all-ktbtest
	for file in $(SRC_TOP)$(TBL_DIR)/*.ktb; do ./ktbtest$X -L$(BLD_TOP)$(DRV_DIR) -T$(SRC_TOP)$(TBL_DIR) $$file; done

###############################################################################

TUNETEST_OBJECTS = tunetest.$O $(PROGRAM_OBJECTS_FOR_HOST) $(PREFS_OBJECTS) $(TUNE_OBJECTS)

tunetest$X: $(TUNETEST_OBJECTS)
	$(CC) $(LDFLAGS) -o $@ $(TUNETEST_OBJECTS) $(LDLIBS)

tunetest.$O:
	$(CC) $(CFLAGS) -c $(SRC_DIR)/tunetest.c

###############################################################################

APITEST_OBJECTS = apitest.$O $(PROGRAM_OBJECTS_FOR_HOST) cmd.$O ttb_translate.$O ttb_compile.$O ttb_native.$O unicode.$O $(CHARSET_OBJECTS) lock.$O datafile.$O dataarea.$O queue.$O

apitest$X: $(APITEST_OBJECTS) api
	$(CC) $(LDFLAGS) -o $@ $(APITEST_OBJECTS) $(API_LIBS) $(ICU_LIBRARIES) $(LDLIBS)

apitest.$O:
	$(CC) $(CFLAGS) -c $(SRC_DIR)/apitest.c

###############################################################################

braille-drivers: $(BUILD_API)
	for driver in $(BRAILLE_EXTERNAL_DRIVER_NAMES); \
	do (cd $(BLD_TOP)$(BRL_DIR)/$$driver && $(MAKE) braille-driver) || exit 1; \
	done

###############################################################################

speech-drivers:
	for driver in $(SPEECH_EXTERNAL_DRIVER_NAMES); \
	do (cd $(BLD_TOP)$(SPK_DIR)/$$driver && $(MAKE) speech-driver) || exit 1; \
	done

###############################################################################

screen-drivers:
	for driver in $(SCREEN_EXTERNAL_DRIVER_NAMES); \
	do (cd $(BLD_TOP)$(SCR_DIR)/$$driver && $(MAKE) screen-driver) || exit 1; \
	done

###############################################################################

TEXT_TABLE_FILE = $(SRC_TOP)$(TBL_DIR)/$(TEXT_TABLE).ttb
text.auto.h: $(TEXT_TABLE_FILE) tbl2hex$(X_FOR_BUILD)
	./tbl2hex$(X_FOR_BUILD) -- $(TEXT_TABLE_FILE) >$@

ATTRIBUTES_TABLE_FILE = $(SRC_TOP)$(TBL_DIR)/$(ATTRIBUTES_TABLE).atb
attr.auto.h: $(ATTRIBUTES_TABLE_FILE) tbl2hex$(X_FOR_BUILD)
	./tbl2hex$(X_FOR_BUILD) -- $(ATTRIBUTES_TABLE_FILE) >$@

cmds.auto.h: $(SRC_DIR)/brldefs.h $(SRC_DIR)/brldefs.awk $(SRC_DIR)/cmds.awk
	$(AWK) -f $(SRC_DIR)/brldefs.awk -f $(SRC_DIR)/cmds.awk $(SRC_DIR)/brldefs.h >$@

brl.auto.h: $(SRC_DIR)/mkdrvtab
	$(SRC_DIR)/mkdrvtab BrailleDriver brl_driver_ $(BRAILLE_INTERNAL_DRIVER_CODES) >$@

spk.auto.h: $(SRC_DIR)/mkdrvtab
	$(SRC_DIR)/mkdrvtab SpeechDriver spk_driver_ $(SPEECH_INTERNAL_DRIVER_CODES) >$@

scr.auto.h: $(SRC_DIR)/mkdrvtab
	$(SRC_DIR)/mkdrvtab ScreenDriver scr_driver_ $(SCREEN_INTERNAL_DRIVER_CODES) >$@

###############################################################################

XBRLAPI_OBJECTS = xbrlapi.$O $(PROGRAM_OBJECTS_FOR_HOST)

xbrlapi$X: $(XBRLAPI_OBJECTS) api
	$(CC) $(LDFLAGS) -o $@ $(XBRLAPI_OBJECTS) $(API_LIBS) $(XKB_LIBS) $(X_LIBS) $(LDLIBS)

xbrlapi.$O:
	$(CC) $(CFLAGS) $(X_CFLAGS) -c $(SRC_DIR)/xbrlapi.c

###############################################################################

PROGRAM_OBJECTS_FOR_BUILD = $(PROGRAM_OBJECTS:.$O=.$B) $(SYSTEM_OBJECT_FOR_BUILD:.$O=.$B)

TBL2HEX_OBJECTS = tbl2hex.$B $(PROGRAM_OBJECTS_FOR_BUILD) charset.$B charset_none.$B dataarea.$B datafile.$B hostcmd.$B hostcmd_none.$B lock.$B queue.$B ttb_compile.$B ttb_native.$B atb_compile.$B ctb_compile.$B

tbl2hex$(X_FOR_BUILD): $(TBL2HEX_OBJECTS)
	$(CC_FOR_BUILD) -o $@ $(TBL2HEX_OBJECTS) $(ICU_LIBRARIES_FOR_BUILD) $(LDLIBS_FOR_BUILD)

###############################################################################

check-braille: brltty$X braille-drivers
	for code in $(BRAILLE_DRIVER_CODES); \
	do ls -l $(BLD_TOP)$(DRV_DIR)/libbrlttyb$$code.$(MOD_EXT) || exit 10; \
	   ./brltty -v -q -N -e -f /dev/null -b $$code -s no -D "$(BLD_TOP)$(DRV_DIR)" -T "$(BLD_TOP)$(TBL_DIR)" 2>&1 || exit 11; \
	done

check-speech: brltty speech-drivers
	for code in $(SPEECH_DRIVER_CODES); \
	do ls -l $(BLD_TOP)$(DRV_DIR)/libbrlttys$$code.$(MOD_EXT) || exit 10; \
	   ./brltty -v -q -N -e -f /dev/null -b no -s $$code -D "$(BLD_TOP)$(DRV_DIR)" -T "$(BLD_TOP)$(TBL_DIR)" 2>&1 || exit 11; \
	done

###############################################################################

install:: install-programs install-tables $(INSTALL_DRIVERS) install-manpages $(INSTALL_API)

install-programs: brltty$X brltty-trtxt$X brltty-ttb$X brltty-ctb$X install-program-directory install-writable-directory
	$(INSTALL_PROGRAM) brltty$X $(INSTALL_PROGRAM_DIRECTORY) 
	$(INSTALL_PROGRAM) brltty-trtxt$X $(INSTALL_PROGRAM_DIRECTORY) 
	$(INSTALL_PROGRAM) brltty-ttb$X $(INSTALL_PROGRAM_DIRECTORY) 
	$(INSTALL_PROGRAM) brltty-ctb$X $(INSTALL_PROGRAM_DIRECTORY) 
	$(INSTALL_DATA) brltty-config $(INSTALL_PROGRAM_DIRECTORY) 
	$(INSTALL_SCRIPT) $(SRC_DIR)/brltty-install $(INSTALL_PROGRAM_DIRECTORY) 

install-xbrlapi: xbrlapi$X install-program-directory
	$(INSTALL_PROGRAM) xbrlapi$X $(INSTALL_PROGRAM_DIRECTORY) 

install-tables: $(INSTALL_TEXT_TABLES) $(INSTALL_ATTRIBUTES_TABLES) $(INSTALL_CONTRACTION_TABLES) install-key-tables install-help-files

install-text-tables: install-tables-directory
	$(INSTALL_DATA) $(SRC_TOP)$(TBL_DIR)/*.tt[bi] $(INSTALL_TABLES_DIRECTORY)

install-attributes-tables: install-tables-directory
	$(INSTALL_DATA) $(SRC_TOP)$(TBL_DIR)/*.at[bi] $(INSTALL_TABLES_DIRECTORY)

install-contraction-tables: install-tables-directory
	$(INSTALL_DATA) $(SRC_TOP)$(TBL_DIR)/*.ct[bi] $(INSTALL_TABLES_DIRECTORY)

install-key-tables: install-tables-directory
	$(INSTALL_DATA) $(SRC_TOP)$(TBL_DIR)/*.kt[bi] $(INSTALL_TABLES_DIRECTORY)

install-help-files: install-tables-directory
	$(INSTALL_DATA) $(SRC_TOP)$(TBL_DIR)/*.txt $(INSTALL_TABLES_DIRECTORY)

install-drivers: $(BRAILLE_DRIVERS) $(SPEECH_DRIVERS) $(SCREEN_DRIVERS) install-drivers-directory
	$(INSTALL_PROGRAM) $(BLD_TOP)$(DRV_DIR)/*.$(MOD_EXT) $(INSTALL_DRIVERS_DIRECTORY)

install-manpages: install-man1-directory
	$(INSTALL_DATA) $(BLD_TOP)$(DOC_DIR)/*.1 $(INSTALL_MAN1_DIRECTORY)

install-documents: install-document-directory ktbtest$X $(BRAILLE_DRIVERS)
	cd $(BLD_TOP)$(DOC_DIR) && $(MAKE) all
	$(SRC_TOP)mkdocs -o $(INSTALL_DOCUMENT_DIRECTORY) -s $(SRC_TOP) -b $(BLD_TOP) -d $(DOC_DIR) -t $(TBL_DIR)

install-api: $(INSTALL_API_LIBRARIES) install-api-headers install-api-manpages install-api-key $(INSTALL_XBRLAPI) $(INSTALL_API_BINDINGS)

install-api-libraries: $(API_LIB) $(API_ARC) install-apilib-directory install-apisoc-directory
	$(INSTALL_PROGRAM) $(API_LIB) $(INSTALL_APILIB_DIRECTORY)/$(API_LIB).$(API_RELEASE)
	$(SYMLINK) -f $(API_LIB).$(API_RELEASE) $(INSTALL_APILIB_DIRECTORY)/$(API_LIB)
	$(SYMLINK) -f $(API_LIB).$(API_RELEASE) $(INSTALL_APILIB_DIRECTORY)/$(API_LIB).$(API_VERSION)
	$(CONFLIBDIR) $(INSTALL_APILIB_DIRECTORY)
	$(INSTALL_DATA) $(API_ARC) $(INSTALL_APILIB_DIRECTORY)

install-api-libraries-windows: $(API_DLL) $(API_ARC) install-program-directory install-apilib-directory
	$(INSTALL_PROGRAM) $(API_DLL) $(INSTALL_PROGRAM_DIRECTORY)
	$(INSTALL_DATA) $(API_IMPLIB) $(INSTALL_APILIB_DIRECTORY)/$(API_IMPLIB_VERSIONED)
	cd $(INSTALL_APILIB_DIRECTORY) && $(SYMLINK) -f $(API_IMPLIB_VERSIONED) $(API_IMPLIB)
	$(INSTALL_DATA) $(API_ARC) $(INSTALL_APILIB_DIRECTORY)
	$(INSTALL_DATA) $(API_DEF) $(INSTALL_APILIB_DIRECTORY)

install-api-headers: brlapi_constants.h brlapi_brldefs.auto.h install-apihdr-directory install-include-directory
	$(INSTALL_DATA) brlapi.h $(INSTALL_APIHDR_DIRECTORY)
	$(INSTALL_DATA) brlapi_constants.h $(INSTALL_APIHDR_DIRECTORY)
	$(INSTALL_DATA) $(SRC_DIR)/brlapi_keycodes.h $(INSTALL_APIHDR_DIRECTORY)
	$(INSTALL_DATA) $(SRC_DIR)/brlapi_protocol.h $(INSTALL_APIHDR_DIRECTORY)
	$(INSTALL_DATA) brlapi_brldefs.auto.h $(INSTALL_INCLUDE_DIRECTORY)/brldefs.h
	$(INSTALL_DATA) $(SRC_DIR)/api.h $(INSTALL_INCLUDE_DIRECTORY)

install-api-manpages: install-man3-directory
	cd $(BLD_TOP)$(DOC_DIR) && $(MAKE) man3
	set -- $(BLD_TOP)$(DOC_DIR)/BrlAPIref/man/man3/brlapi_*.3 && [ -f $$1 ] || exit 0 && \
	$(INSTALL_DATA) $$* $(INSTALL_MAN3_DIRECTORY)

install-api-key:
	file=$(sysconfdir)/$(API_AUTHFILE) && \
	if test ! -f $$file -a -w $(sysconfdir) -a -z "$(INSTALL_ROOT)"; \
	then umask 077 && $(SRC_DIR)/brltty-genkey $$file; fi

###############################################################################

uninstall:: uninstall-programs uninstall-tables uninstall-drivers uninstall-manpages uninstall-documents uninstall-headers uninstall-api-bindings

uninstall-programs:
	-rm -f $(INSTALL_PROGRAM_DIRECTORY)/brltty$X
	-rm -f $(INSTALL_PROGRAM_DIRECTORY)/brltty-*
	-rm -f $(INSTALL_PROGRAM_DIRECTORY)/brltty-install
	-rm -f $(INSTALL_PROGRAM_DIRECTORY)/xbrlapi$X

uninstall-tables:
	-rm -f $(INSTALL_TABLES_DIRECTORY)/*.[tack]t[bi]
	-rm -f $(INSTALL_TABLES_DIRECTORY)/brl-*.txt
	-[ ! -d $(INSTALL_WRITABLE_DIRECTORY) ] || rmdir $(INSTALL_WRITABLE_DIRECTORY)
	-[ ! -d $(INSTALL_TABLES_DIRECTORY) ] || rmdir $(INSTALL_TABLES_DIRECTORY)

uninstall-drivers: $(UNINSTALL_API_LIBRARIES)
	-rm -f $(INSTALL_DRIVERS_DIRECTORY)/$(LIB_NAME)*
	-[ ! -d $(INSTALL_DRIVERS_DIRECTORY) ] || rmdir $(INSTALL_DRIVERS_DIRECTORY)

uninstall-api-libraries:
	-rm -f $(INSTALL_APILIB_DIRECTORY)/$(API_LIB)
	-rm -f $(INSTALL_APILIB_DIRECTORY)/$(API_LIB).$(API_VERSION)
	-rm -f $(INSTALL_APILIB_DIRECTORY)/$(API_LIB).$(API_RELEASE)
	$(CONFLIBDIR) $(INSTALL_APILIB_DIRECTORY)
	-rm -f $(INSTALL_APILIB_DIRECTORY)/$(API_ARC)
	-rm -f -r $(API_SOCKET_DIRECTORY)

uninstall-api-libraries-windows:
	-rm -f $(INSTALL_APILIB_DIRECTORY)/$(API_ARC)
	-rm -f $(INSTALL_APILIB_DIRECTORY)/$(API_IMPLIB)
	-rm -f $(INSTALL_APILIB_DIRECTORY)/$(API_IMPLIB_VERSIONED)
	-rm -f $(INSTALL_APILIB_DIRECTORY)/$(API_DEF)
	-rm -f $(INSTALL_PROGRAM_DIRECTORY)/$(API_DLL)

uninstall-manpages:
	-rm -f $(INSTALL_ROOT)$(MANPAGE_DIRECTORY)/man1/brltty.1
	-rm -f $(INSTALL_MAN3_DIRECTORY)/brlapi_*.3

uninstall-documents:
	-rm -r -f $(INSTALL_DOCUMENT_DIRECTORY)

uninstall-headers:
	-rm -f $(INSTALL_APIHDR_DIRECTORY)/brlapi.h
	-rm -f $(INSTALL_APIHDR_DIRECTORY)/brlapi_*.h
	-rm -f $(INSTALL_INCLUDE_DIRECTORY)/api.h
	-rm -f $(INSTALL_INCLUDE_DIRECTORY)/brldefs.h
	-rm -f $(INSTALL_INCLUDE_DIRECTORY)/brldefs-*.h
	-[ ! -d $(INSTALL_INCLUDE_DIRECTORY) ] || rmdir $(INSTALL_INCLUDE_DIRECTORY)

###############################################################################

install uninstall::
	-set -- $(BRAILLE_EXTERNAL_DRIVER_NAMES); \
	for driver do (cd $(BLD_TOP)$(BRL_DIR)/$$driver && $(MAKE) $@); done
	-set -- $(SPEECH_EXTERNAL_DRIVER_NAMES); \
	for driver do (cd $(BLD_TOP)$(SPK_DIR)/$$driver && $(MAKE) $@); done
	-set -- $(SCREEN_EXTERNAL_DRIVER_NAMES); \
	for driver do (cd $(BLD_TOP)$(SCR_DIR)/$$driver && $(MAKE) $@); done

distclean clean::
	-set -- $(BRAILLE_EXTERNAL_DRIVER_NAMES) $(BRAILLE_INTERNAL_DRIVER_NAMES); \
	for driver do (cd $(BLD_TOP)$(BRL_DIR)/$$driver && $(MAKE) $@); done
	-set -- $(SPEECH_EXTERNAL_DRIVER_NAMES) $(SPEECH_INTERNAL_DRIVER_NAMES); \
	for driver do (cd $(BLD_TOP)$(SPK_DIR)/$$driver && $(MAKE) $@); done
	-set -- $(SCREEN_EXTERNAL_DRIVER_NAMES) $(SCREEN_INTERNAL_DRIVER_NAMES); \
	for driver do (cd $(BLD_TOP)$(SCR_DIR)/$$driver && $(MAKE) $@); done
	-set -- $(API_BINDINGS); \
	for language do (cd $(BLD_TOP)$(BND_DIR)/$$language && $(MAKE) $@); done

clean::
	-rm -f brltty$X brltty-ctb$X brltty-ttb$X brltty-trtxt$X xbrlapi$X
	-rm -f tbl2hex$(X_FOR_BUILD) *test$X *-static$X
	-rm -f brlapi_constants.h *.$(LIB_EXT) *.$(ARC_EXT) *.def *.class *.jar
	-rm -f $(BLD_TOP)$(DRV_DIR)/*

distclean::
	-rm -f brlapi.h
	-rm -f brltty-config
