CMakeLists.txt 7.62 KB
Newer Older
Erik Jansson's avatar
Erik Jansson committed
1
# Main CMake file for project Firefly.
2
3
4
# Modeline {{{
# vi: foldmarker={{{,}}} foldmethod=marker foldlevel=0
# }}}
Erik Jansson's avatar
Erik Jansson committed
5

6
## General {{{
7
cmake_minimum_required (VERSION 2.8)
Erik Jansson's avatar
Erik Jansson committed
8
project (Firefly C)
9
10
11
if(NOT CMAKE_CROSSCOMPILING)
	enable_testing()
endif(NOT CMAKE_CROSSCOMPILING)
Erik Jansson's avatar
Erik Jansson committed
12
13
14
15
16
17

# Helper function to join a list into a string:
function(JOIN VALUES OUTPUT)
	string(REGEX REPLACE ";" " " _TMP_STR ${VALUES})
	set(${OUTPUT} "${_TMP_STR}" PARENT_SCOPE)
endfunction()
18
## }}}
Erik Jansson's avatar
Erik Jansson committed
19

20
## DIRS {{{
21
# Firefly directories:
Erik Jansson's avatar
Erik Jansson committed
22
23
24
25
set(Firefly_PROJECT_DIR ${Firefly_SOURCE_DIR}/..)
set(GEN_DIR ${Firefly_PROJECT_DIR}/gen)
set(LC_DIR ${Firefly_PROJECT_DIR}/lc)
set(INC_DIR ${Firefly_PROJECT_DIR}/include)
26
set(TEST_FILES_DIR ${Firefly_PROJECT_DIR}/testfiles)
Erik Jansson's avatar
Erik Jansson committed
27

28
29
30
31
32
33
# Documentation directories:
set(DOC_DIR ${Firefly_PROJECT_DIR}/doc)
set(DOC_GEN_DIR ${DOC_DIR}/gen)
set(DOC_GEN_API_DIR ${DOC_GEN_DIR}/api)
Set(DOC_GEN_FULL_DIR ${DOC_GEN_DIR}/full)

Oscar Olsson's avatar
Oscar Olsson committed
34
35
# Labcomm compiler:
set(LABCOMMC ${Firefly_PROJECT_DIR}/tools/labComm.jar)
Erik Jansson's avatar
Erik Jansson committed
36

37
38
# Tell CMake to output library and executables directly in the build
# directory:
Erik Jansson's avatar
Erik Jansson committed
39
40
41
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${Firefly_BINARY_DIR})
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${Firefly_BINARY_DIR})

42
43
# Append our own CMake modules path:
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${Firefly_PROJECT_DIR}/cmake_modules)
Oscar Olsson's avatar
Oscar Olsson committed
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58

# Tell CMake that we prefer static libraries before dynamic ones:
set(CMAKE_FIND_LIBRARY_SUFFIXES ".a;.so")
## }}}

## FIND LIBRARIES {{{
message("-- Looking for Xenomai")
find_package(Xenomai)
message("-- Looking for RTNet")
find_package(RTNet)
message("-- Looking for Labcomm")
find_package(LabComm)

# Add Labcomm lib dir.
link_directories(${LINK_DIRECTORIES} ${LABCOMM_LIBRARIES})
59
## }}}
60

61
## FLAGS {{{
Erik Jansson's avatar
Erik Jansson committed
62
set(C_STD "-std=c99")
Oscar Olsson's avatar
Oscar Olsson committed
63
set(ERR_FLAGS "-Wall -Wextra")
64
65
66
67
68
69

if(DEFINED $ENV{DEBUG})
	set(MACROS_DEF "-DDEBUG -O0")
else()
	set(MACROS_DEF "-DNDEBUG -O3")
endif()
Erik Jansson's avatar
Erik Jansson committed
70

71
if(CMAKE_CROSSCOMPILING)
Erik Jansson's avatar
Erik Jansson committed
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
	if (VXWORKS_COMPILING)
		# CFLAGS when cross compiling to the ARM-card:
		set(CMAKE_C_FLAGS
			"${C_STD} "
			"${ERR_FLAGS} "
			"${MACROS_DEF} "
			"-DLABCOMM_COMPAT=\\\"labcomm_compat_vxworks.h\\\" "
		)
		# Join the list stuff above to a single string:
		JOIN("${CMAKE_C_FLAGS}" CMAKE_C_FLAGS)
	else()
		# CFLAGS when cross compiling to the ARM-card:
		set(CMAKE_C_FLAGS
			"${C_STD} "
			"${ERR_FLAGS} -Wfloat-equal -Werror-implicit-function-declaration "
			"-mthumb -mcpu=cortex-m3 -T../ft-sense/src/adc_freertos_lwip/standalone.ld"
			"-ffunction-sections -fdata-sections "
			"${MACROS_DEF} -DUART_BUFFERED -Dsprintf=usprintf "
			"-Dsnprintf=usnprintf -Dvsnprintf=uvsnprintf -Dprintf=uipprintf "
			"-DFIREFLY_MALLOC=pvPortMalloc -Dcalloc=pvPortCalloc -DFIREFLY_FREE=vPortFree "
			"-DARM_CORTEXM3_CODESOURCERY -DLABCOMM_NO_STDIO "
			"-DLABCOMM_COMPAT=\\\"labcomm_compat_arm_cortexm3.h\\\" "
			"-DGCC_ARMCM3=1"
		)
		# Join the list stuff above to a single string:
		JOIN("${CMAKE_C_FLAGS}" CMAKE_C_FLAGS)

	endif()
Erik Jansson's avatar
Erik Jansson committed
100
101
else()
	# CFLAGS when compiling normally:
Erik Jansson's avatar
Erik Jansson committed
102
	set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${C_STD} ${ERR_FLAGS} ${MACROS_DEF} -g")
103
endif(CMAKE_CROSSCOMPILING)
104
## }}}
105

106
## CUSTOM COMMANDS & TARGETS {{{
107
108
109
# Custom command to create the directory for the generated Labcomm
# files, it depends upon the Labcomm compiler and library to make sure
# they are generated before trying to generate files with Labcomm.
Erik Jansson's avatar
Erik Jansson committed
110
111
112
113
add_custom_command(
	OUTPUT ${GEN_DIR}
	COMMAND mkdir -p ${GEN_DIR}
)
114
# Custom commands to generate files from .lc files:
Erik Jansson's avatar
Erik Jansson committed
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
add_custom_command(
	OUTPUT ${GEN_DIR}/firefly_protocol.c ${GEN_DIR}/firefly_protocol.h
	DEPENDS ${LC_DIR}/firefly_protocol.lc ${GEN_DIR}
	COMMAND java -jar ${LABCOMMC} --c=${GEN_DIR}/firefly_protocol.c --h=${GEN_DIR}/firefly_protocol.h ${LC_DIR}/firefly_protocol.lc
)
add_custom_command(
	OUTPUT ${GEN_DIR}/pingpong.c ${GEN_DIR}/pingpong.h
	DEPENDS ${LC_DIR}/pingpong.lc ${GEN_DIR}
	COMMAND java -jar ${LABCOMMC} --c=${GEN_DIR}/pingpong.c --h=${GEN_DIR}/pingpong.h ${LC_DIR}/pingpong.lc
)
add_custom_command(
	OUTPUT ${GEN_DIR}/test.c ${GEN_DIR}/test.h
	DEPENDS ${LC_DIR}/test.lc ${GEN_DIR}
	COMMAND java -jar ${LABCOMMC} --c=${GEN_DIR}/test.c --h=${GEN_DIR}/test.h ${LC_DIR}/test.lc
)

131
132
# Create a library of the generated Labcomm files to make it easy to
# include in executables:
Erik Jansson's avatar
Erik Jansson committed
133
134
135
add_library(gen-files
	${Firefly_PROJECT_DIR}/gen/test.c
	${Firefly_PROJECT_DIR}/gen/firefly_protocol.c
136
	${Firefly_PROJECT_DIR}/gen/pingpong.c
Erik Jansson's avatar
Erik Jansson committed
137
)
138

139
140
141
# Tell CMake that the files above are generated at compile time,
# otherwise CMake complains about missing files when generating
# build files:
Erik Jansson's avatar
Erik Jansson committed
142
143
set_source_files_properties(${Firefly_PROJECT_DIR}/gen/test.c PROPERTIES GENERATED TRUE)
set_source_files_properties(${Firefly_PROJECT_DIR}/gen/firefly_protocol.c PROPERTIES GENERATED TRUE)
144
145
146
147
148
149
set_source_files_properties(${Firefly_PROJECT_DIR}/gen/pingpong.c PROPERTIES GENERATED TRUE)
# Remove warnings when compiling Labcomm generated code since that's not
# our fault:
set_target_properties(gen-files
	PROPERTIES COMPILE_FLAGS "-Wno-all -Wno-extra"
)
Erik Jansson's avatar
Erik Jansson committed
150

Erik Jansson's avatar
Erik Jansson committed
151
152
153
154
155
156
157
158
159
160
# Custom commands to build tags files for vim/emacs.
add_custom_target(tags
	DEPENDS tags-vim tags-emacs
)
add_custom_target(tags-vim
	COMMAND ctags -R --tag-relative=yes -f ../tags ..
)
add_custom_target(tags-emacs
	COMMAND ctags --recurse --tag-relative=yes -e -f ../TAGS ..
)
Erik Jansson's avatar
Erik Jansson committed
161

Oscar Olsson's avatar
Oscar Olsson committed
162
# Custom command to clean more than clean:
Erik Jansson's avatar
Erik Jansson committed
163
add_custom_target(cleaner
Oscar Olsson's avatar
Oscar Olsson committed
164
	COMMAND rm -fr ${Firefly_PROJECT_DIR}/build/* &&
Erik Jansson's avatar
Erik Jansson committed
165
166
			rm -fr ${TEST_FILES_DIR} &&
)
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194


# Documentation targets {{{
add_custom_target(docs
	DEPENDS docs-full docs-api
)

# Custom target to create full documentation (takes FOREVER to run):
add_custom_target(docs-full
	COMMAND doxygen ${DOC_GEN_DIR}/doxygen_full.cfg
	DEPENDS ${DOC_GEN_DIR}/doxygen_full.cfg
	WORKING_DIRECTORY ${Firefly_PROJECT_DIR}
)

# Custom target to create API documentation:
add_custom_target(docs-api
	COMMAND doxygen ${DOC_GEN_DIR}/doxygen_api.cfg
	DEPENDS ${DOC_GEN_DIR}/doxygen_api.cfg
	WORKING_DIRECTORY ${Firefly_PROJECT_DIR}
)

# Custom target to build the conf file needed by docs-full above:
add_custom_command(
	OUTPUT ${DOC_GEN_DIR}/doxygen_full.cfg
	DEPENDS ${DOC_GEN_DIR}/index.dox
	COMMAND mkdir ${DOC_GEN_FULL_DIR} &&
			cp ${DOC_DIR}/doxygen_template.cfg ${DOC_GEN_DIR}/doxygen_full.cfg &&
			echo \"INPUT = doc/gen/index.dox include src\" >> ${DOC_GEN_DIR}/doxygen_full.cfg &&
195
			echo \"OUTPUT_DIRECTORY = \\"${DOC_GEN_FULL_DIR}\\"\" >> ${DOC_GEN_DIR}/doxygen_full.cfg
196
197
198
199
200
201
202
203
204
)

# Custom target to build the conf file needed by docs-api above:
add_custom_command(
	OUTPUT ${DOC_GEN_DIR}/doxygen_api.cfg
	DEPENDS ${DOC_GEN_DIR}/index.dox
	COMMAND mkdir ${DOC_GEN_API_DIR} &&
			cp ${DOC_DIR}/doxygen_template.cfg ${DOC_GEN_DIR}/doxygen_api.cfg &&
			echo \"INPUT = doc/gen/index.dox include\" >> ${DOC_GEN_DIR}/doxygen_api.cfg &&
205
			echo \"OUTPUT_DIRECTORY = \\"${DOC_GEN_API_DIR}\\"\" >> ${DOC_GEN_DIR}/doxygen_api.cfg
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
)

# Append our README to the index file:
add_custom_command(
	OUTPUT ${DOC_GEN_DIR}/index.dox
	DEPENDS ${DOC_GEN_DIR}
	COMMAND cat ${DOC_DIR}/README.dox >> ${DOC_GEN_DIR}/index.dox
)

# Custom target to create doc/gen:
add_custom_command(
	OUTPUT ${DOC_GEN_DIR}
	COMMAND mkdir -p ${DOC_GEN_DIR}
)
# }}}
221
## }}}
222

223
## MISC {{{
224
# Specify which directories should be searched for header files:
Erik Jansson's avatar
Erik Jansson committed
225
include_directories(
Oscar Olsson's avatar
Oscar Olsson committed
226
	${LABCOMM_INCLUDE_DIRS}
Erik Jansson's avatar
Erik Jansson committed
227
228
229
230
231
	${INC_DIR}
	${Firefly_PROJECT_DIR}
	${Firefly_SOURCE_DIR}
)

Erik Jansson's avatar
Erik Jansson committed
232
233
234
# Make sure object files are named *.o instead of *.c.o:
set(CMAKE_C_OUTPUT_EXTENSION_REPLACE 1)

Erik Jansson's avatar
Erik Jansson committed
235
# Dump variables available to cmake, good for debugging:
Oscar Olsson's avatar
Oscar Olsson committed
236
237
238
239
 #get_cmake_property(_variableNames VARIABLES)
 #foreach (_variableName ${_variableNames})
	 #message(STATUS "${_variableName}=${${_variableName}}")
 #endforeach()
240
## }}}
Erik Jansson's avatar
Erik Jansson committed
241

242
## INCLUDE {{{
243
# Include subdirectories with their own CMake files:
Erik Jansson's avatar
Erik Jansson committed
244
245
246
add_subdirectory(utils)
add_subdirectory(protocol)
add_subdirectory(transport)
247
add_subdirectory(test)
248
## }}}
Erik Jansson's avatar
Erik Jansson committed
249