BTSTACK_ROOT = ../../..

prefix  = @prefix@

CC      = @CC@
LDFLAGS = @LDFLAGS@
CFLAGS  = @CFLAGS@ \
    -I ${BTSTACK_ROOT}/3rd-party/bluedroid/decoder/include \
    -I ${BTSTACK_ROOT}/3rd-party/bluedroid/encoder/include \
    -I ${BTSTACK_ROOT}/3rd-party/micro-ecc \
    -I ${BTSTACK_ROOT}/3rd-party/rijndael  \
    -I ${BTSTACK_ROOT}/chipset/intel \
    -I $(BTSTACK_ROOT)/platform/daemon/src \
    -I $(BTSTACK_ROOT)/platform/daemon/src \
    -I $(BTSTACK_ROOT)/platform/posix \
    -I $(BTSTACK_ROOT)/platform/windows \
    -I $(BTSTACK_ROOT)/src \
    -I..
BTSTACK_LIB_LDFLAGS   = @BTSTACK_LIB_LDFLAGS@
BTSTACK_LIB_EXTENSION = @BTSTACK_LIB_EXTENSION@
USB_CFLAGS            = @USB_CFLAGS@
USB_LDFLAGS           = @USB_LDFLAGS@

VPATH += ${BTSTACK_ROOT}/3rd-party/micro-ecc
VPATH += ${BTSTACK_ROOT}/3rd-party/rijndael
VPATH += ${BTSTACK_ROOT}/chipset/intel
VPATH += ${BTSTACK_ROOT}/platform/daemon/src
VPATH += ${BTSTACK_ROOT}/platform/corefoundation
VPATH += ${BTSTACK_ROOT}/platform/libusb
VPATH += ${BTSTACK_ROOT}/platform/posix
VPATH += ${BTSTACK_ROOT}/platform/windows
VPATH += ${BTSTACK_ROOT}/src
VPATH += ${BTSTACK_ROOT}/src/ble
VPATH += ${BTSTACK_ROOT}/src/classic

remote_device_db_sources = @REMOTE_DEVICE_DB_SOURCES@
btstack_run_loop_sources = @btstack_run_loop_SOURCES@
usb_sources = @USB_SOURCES@
uart_sources = @UART_SOURCES@

libBTstack_SOURCES =    \
    btstack.o           \
    socket_connection.o \
    hci_dump.o          \
    hci_cmd.o          \
    daemon_cmds.o       \
    btstack_linked_list.o    \
    btstack_run_loop.o  \
    sdp_util.o          \
    spp_server.o        \
    btstack_util.o             \
    $(btstack_run_loop_sources) \
			  
BTdaemon_SOURCES =      \
    $(libBTstack_SOURCES)       \
    $(usb_sources)              \
    $(uart_sources)             \
    $(remote_device_db_sources) \
    ad_parser.o                 \
    att_db.o                    \
    att_dispatch.o              \
    att_server.o                \
    bnep.o                      \
    btstack_crypto.o            \
    btstack_memory.o            \
    btstack_memory_pool.o       \
    btstack_tlv.o               \
    btstack_tlv_posix.o         \
    btstack_link_key_db_tlv.o   \
    daemon.o                    \
    gatt_client.o               \
    hci.o                       \
    hci_dump.o                  \
    hci_dump_posix_fs.o         \
    hci_dump_posix_stdout.o     \
    hci_transport_h4.o          \
    l2cap.o                     \
    l2cap_signaling.o           \
    le_device_db_tlv.o          \
    rfcomm.o                    \
    rijndael.o                  \
    sdp_client.o                \
    sdp_client_rfcomm.o         \
    sdp_server.o                \
    sm.o                        \
    uECC.o                      \

# use $(CC) for Objective-C files
.m.o:
	$(CC) $(CFLAGS) -c -o $@ $<

all: libBTstack.$(BTSTACK_LIB_EXTENSION) BTdaemon libBTstackServer.$(BTSTACK_LIB_EXTENSION)

# Intel Firmware files
include ${BTSTACK_ROOT}/chipset/intel/Makefile.inc
all: @FIRMWARE_FILES@ 

libBTstack.$(BTSTACK_LIB_EXTENSION): $(libBTstack_SOURCES)
		$(BTSTACK_ROOT)/tool/get_version.sh
		$(CC) $(CFLAGS) $^ $(LDFLAGS) $(BTSTACK_LIB_LDFLAGS) -o $@

# libBTstack.a: $(libBTstack_SOURCES:.c=.o) $(libBTstack_SOURCES:.m=.o)
#		ar cru $@ $(libBTstack_SOURCES:.c=.o) $(libBTstack_SOURCES:.m=.o)
#		ranlib $@

BTdaemon: $(BTdaemon_SOURCES)
		$(CC) $(CFLAGS) $(USB_CFLAGS) $^ $(LDFLAGS) $(USB_LDFLAGS) -o $@

libBTstackServer.$(BTSTACK_LIB_EXTENSION): $(BTdaemon_SOURCES)
		$(BTSTACK_ROOT)/tool/get_version.sh
		$(CC) $(CFLAGS) $(USB_CFLAGS) $^ $(LDFLAGS) $(USB_LDFLAGS) $(BTSTACK_LIB_LDFLAGS) -o $@

clean:
	rm -rf libBTstack* BTdaemon *.o
	
install:    
	echo "Installing BTdaemon in $(prefix)..."
	mkdir -p $(prefix)/bin $(prefix)/lib $(prefix)/include
	# cp libBTstack.a $(prefix)/lib/
	cp libBTstack.dylib $(prefix)/lib/
	cp BTdaemon $(prefix)/bin/
	cp -r $(BTSTACK_ROOT)/include/btstack $(prefix)/include
