import os
from doit.tools import create_folder
from build_sys.doit_helpers import file_utils
from build_sys.doit_helpers import gcc_utils

DOIT_CONFIG = {'default_tasks': ['print_size']}


# ---------------------------------------------------------------------
# Build settings

PROJECT_NAME = 'goggles'

BUILD_DIR = 'build'

ARDUINO_DIR = os.path.join('programs_win', 'arduino-1.0.5')

C_COMPILER = os.path.join(ARDUINO_DIR, 'hardware', 'tools', 'avr', 'bin', 'avr-gcc')

C_INCLUDE_FLAGS = [
    '-I' + os.path.join(ARDUINO_DIR, 'hardware', 'arduino', 'cores', 'arduino'),
    '-I' + os.path.join(ARDUINO_DIR, 'hardware', 'arduino', 'variants', 'standard'),
]

C_FLAGS = [
    '-c',
    '-g',
    '-Os',
    '-Wall',
    '-ffunction-sections',
    '-fdata-sections',
    '-mmcu=atmega328p',
    '-DF_CPU=16000000L',
    '-MMD',
    '-DUSB_VID=null',
    '-DUSB_PID=null',
    '-DARDUINO=105',
]

C_COMPILER = os.path.join(ARDUINO_DIR, 'hardware', 'tools', 'avr', 'bin', 'avr-g++')

CPP_INCLUDE_FLAGS = C_INCLUDE_FLAGS

CPP_FLAGS = C_FLAGS + ['-fno-exceptions']

LINKER = os.path.join(ARDUINO_DIR, 'hardware', 'tools', 'avr', 'bin', 'avr-gcc')

LINKER_FLAGS = [
    '-Os',
    '-Wl,--gc-sections',
    '-mmcu=atmega328p',
]

LINKER_LIB_FLAGS = [
    '-lm'
]

LINKER_TARGET = os.path.join(BUILD_DIR, PROJECT_NAME + '.elf')

LINKER = os.path.join(ARDUINO_DIR, 'hardware', 'tools', 'avr', 'bin', 'avr-objcopy')

OBJCOPY_EEPROM_FLAGS = [
    '-O',
    'ihex',
    '-j',
    '.eeprom',
    '--set-section-flags=.eeprom=alloc,load',
    '--no-change-warnings',
    '--change-section-lma',
    '.eeprom=0',
]

EEPROM_TARGET = os.path.join(BUILD_DIR, PROJECT_NAME + '.eep')

OBJCOPY_FLASH_FLAGS = [
    '-O',
    'ihex',
    '-R',
    '.eeprom',
]

FLASH_TARGET = os.path.join(BUILD_DIR, PROJECT_NAME + '.hex')

AVRDUDE_PATH = os.path.join(ARDUINO_DIR, 'hardware', 'tools', 'avr', 'bin', 'avrdude')

AVRDUDE_CONF_FILE = os.path.join(ARDUINO_DIR, 'hardware', 'tools', 'avr', 'etc' 'avrdude.conf')

AVRDUDE_FLAGS = [
    '-C' + AVRDUDE_CONF_FILE,
    '-patmega328p',
    '-carduino',
    '-PCOM5',
    '-b115200',
    '-D'