1 ###| CMAKE Kiibohd Controller |###
3 # Written by Jacob Alexander in 2011 for the Kiibohd Controller
4 # Due to this file's usefulness:
6 # Released into the Public Domain
10 #| Set the Compilers (must be set first)
11 include( CMakeForceCompiler )
12 cmake_force_c_compiler ( avr-gcc AVRCCompiler )
13 cmake_force_cxx_compiler( avr-g++ AVRCxxCompiler )
16 #| Add Dependency Macro
17 include( AddFileDependencies )
25 project( kiibohd_controller )
27 #| Target Name (output name)
31 cmake_minimum_required( VERSION 2.8 )
39 #| Sources (see setup.h for configuring in/away code blocks or other complete modules)
40 #| XXX Not set here in this project, see setup.cmake
43 #| Instead, include the module source selector
44 include( setup.cmake )
56 # Atmel Defines and Linker Options
60 #| You _MUST_ set this to match the board you are using
61 #| type "make clean" after changing this, so all files will be rebuilt
63 #| "at90usb162" # Teensy 1.0
64 #| "atmega32u4" # Teensy 2.0
65 #| "at90usb646" # Teensy++ 1.0
66 #| "at90usb1286" # Teensy++ 2.0
67 set( MCU "atmega32u4" )
70 #| Compiler flag to set the C Standard level.
72 #| gnu89 = c89 plus GCC extensions
73 #| c99 = ISO C99 standard (not yet fully implemented)
74 #| gnu99 = c99 plus GCC extensions
75 set( CSTANDARD "-std=gnu99" )
79 #| -Wall...: warning level
80 set( WARN "-Wall -Wstrict-prototypes" )
84 #| -f...: tuning, see GCC manual and avr-libc documentation
85 set( TUNING "-funsigned-char -funsigned-bitfields -ffunction-sections -fpack-struct -fshort-enums" )
88 #| Optimization level, can be [0, 1, 2, 3, s].
89 #| 0 = turn off optimization. s = optimize for size.
90 #| (Note: 3 is not always the best optimization level. See avr-libc FAQ.)
99 #| Processor frequency.
100 #| Normally the first thing your program should do is set the clock prescaler,
101 #| so your program will run at the correct speed. You should also set this
102 #| variable to same clock speed. The _delay_ms() macro uses this, and many
103 #| examples use this variable to calculate timings. Do not add a "UL" here.
104 set( F_CPU "16000000" )
108 #| Compiler flags to generate dependency files.
109 set( GENDEPFLAGS "-MMD -MP" )
113 set( TARGET_LST ${TARGET}.lst )
117 add_definitions( "-mmcu=${MCU} -DF_CPU=${F_CPU} -O${OPT} ${TUNING} ${WARN} ${CSTANDARD} ${GENDEPFLAGS}" )
121 set( LINKER_FLAGS "-mmcu=${MCU} -Wl,-Map=${TARGET}.map,--cref -Wl,--relax -Wl,--gc-sections" )
124 #| Hex Flags (XXX, CMake seems to have issues if you quote the arguments for the custom commands...)
125 set( HEX_FLAGS -O ${FORMAT} -R .eeprom -R .fuse -R .lock -R .signature )
129 set( EEP_FLAGS -j .eeprom --set-section-flags=.eeprom="alloc,load" --change-section-lma .eeprom=0 --no-change-warnings -O ${FORMAT} )
133 set( LSS_FLAGS -h -S -z )
141 #| Create the .ELF file
142 set( TARGET_ELF ${TARGET}.elf )
143 add_executable( ${TARGET_ELF} ${SRCS} )
147 set_target_properties( ${TARGET_ELF} PROPERTIES
148 LINK_FLAGS ${LINKER_FLAGS}
152 #| Convert the .ELF into a .HEX to load onto the Teensy
153 set( TARGET_HEX ${TARGET}.hex )
154 add_custom_command( TARGET ${TARGET_ELF} POST_BUILD
155 COMMAND avr-objcopy ${HEX_FLAGS} ${TARGET_ELF} ${TARGET_HEX}
156 COMMENT "Creating load file for Flash: ${TARGET_HEX}"
160 #| Convert the .ELF into a .EEP
161 set( TARGET_EEP ${TARGET}.eep )
162 add_custom_command( TARGET ${TARGET_ELF} POST_BUILD
163 COMMAND avr-objcopy ${EEP_FLAGS} ${TARGET_ELF} ${TARGET_EEP}
164 COMMENT "Creating load file for EEPROM: ${TARGET_EEP}"
168 #| Generate the Extended .LSS
169 set( TARGET_LSS ${TARGET}.lss )
170 add_custom_command( TARGET ${TARGET_ELF} POST_BUILD
171 COMMAND avr-objdump ${LSS_FLAGS} ${TARGET_ELF} > ${TARGET_LSS}
172 COMMENT "Creating Extended Listing: ${TARGET_LSS}"
176 #| Generate the Symbol Table .SYM
177 set( TARGET_SYM ${TARGET}.sym )
178 add_custom_command( TARGET ${TARGET_ELF} POST_BUILD
179 COMMAND avr-nm -n ${TARGET_ELF} > ${TARGET_SYM}
180 COMMENT "Creating Symbol Table: ${TARGET_SYM}"
189 #| After Changes Size Information
190 add_custom_target( SizeAfter ALL avr-size --target=${FORMAT} ${TARGET_HEX} ${TARGET_ELF}
191 DEPENDS ${TARGET_ELF}
192 COMMENT "Size after generation:"
198 # Setup Loader Script
201 #| Provides the user with the correct teensy-loader-cli command for the built .HEX file
202 #| teensy-loader-cli must be in the user's path
203 if( ${CMAKE_SYSTEM_NAME} MATCHES "Linux" OR ${CMAKE_SYSTEM_NAME} MATCHES "Darwin" )
204 configure_file( LoadFile/bash load )
205 endif( ${CMAKE_SYSTEM_NAME} MATCHES "Linux" OR ${CMAKE_SYSTEM_NAME} MATCHES "Darwin" )
208 if( ${CMAKE_SYSTEM_NAME} MATCHES "Windows" )
209 message( STATUS "Load Script is on my TODO List for Windows..." )
210 endif( ${CMAKE_SYSTEM_NAME} MATCHES "Windows" )