blob: 04efa9ed92fdfbb850835e2b917737f5646359f8 [file] [log] [blame]
ptrkrysik12fe7a02014-11-20 11:10:20 +01001# Copyright 2010-2011,2014 Free Software Foundation, Inc.
piotr437f5462014-02-04 17:57:25 +01002#
3# This file is part of GNU Radio
4#
5# GNU Radio is free software; you can redistribute it and/or modify
6# it under the terms of the GNU General Public License as published by
7# the Free Software Foundation; either version 3, or (at your option)
8# any later version.
9#
10# GNU Radio is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13# GNU General Public License for more details.
14#
15# You should have received a copy of the GNU General Public License
16# along with GNU Radio; see the file COPYING. If not, write to
17# the Free Software Foundation, Inc., 51 Franklin Street,
18# Boston, MA 02110-1301, USA.
19
20if(DEFINED __INCLUDED_GR_MISC_UTILS_CMAKE)
21 return()
22endif()
23set(__INCLUDED_GR_MISC_UTILS_CMAKE TRUE)
24
25########################################################################
26# Set global variable macro.
27# Used for subdirectories to export settings.
28# Example: include and library paths.
29########################################################################
30function(GR_SET_GLOBAL var)
31 set(${var} ${ARGN} CACHE INTERNAL "" FORCE)
32endfunction(GR_SET_GLOBAL)
33
34########################################################################
35# Set the pre-processor definition if the condition is true.
36# - def the pre-processor definition to set and condition name
37########################################################################
38function(GR_ADD_COND_DEF def)
39 if(${def})
40 add_definitions(-D${def})
41 endif(${def})
42endfunction(GR_ADD_COND_DEF)
43
44########################################################################
45# Check for a header and conditionally set a compile define.
46# - hdr the relative path to the header file
47# - def the pre-processor definition to set
48########################################################################
49function(GR_CHECK_HDR_N_DEF hdr def)
50 include(CheckIncludeFileCXX)
51 CHECK_INCLUDE_FILE_CXX(${hdr} ${def})
52 GR_ADD_COND_DEF(${def})
53endfunction(GR_CHECK_HDR_N_DEF)
54
55########################################################################
56# Include subdirectory macro.
57# Sets the CMake directory variables,
58# includes the subdirectory CMakeLists.txt,
59# resets the CMake directory variables.
60#
61# This macro includes subdirectories rather than adding them
62# so that the subdirectory can affect variables in the level above.
63# This provides a work-around for the lack of convenience libraries.
64# This way a subdirectory can append to the list of library sources.
65########################################################################
66macro(GR_INCLUDE_SUBDIRECTORY subdir)
67 #insert the current directories on the front of the list
68 list(INSERT _cmake_source_dirs 0 ${CMAKE_CURRENT_SOURCE_DIR})
69 list(INSERT _cmake_binary_dirs 0 ${CMAKE_CURRENT_BINARY_DIR})
70
71 #set the current directories to the names of the subdirs
72 set(CMAKE_CURRENT_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/${subdir})
73 set(CMAKE_CURRENT_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/${subdir})
74
75 #include the subdirectory CMakeLists to run it
76 file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
77 include(${CMAKE_CURRENT_SOURCE_DIR}/CMakeLists.txt)
78
79 #reset the value of the current directories
80 list(GET _cmake_source_dirs 0 CMAKE_CURRENT_SOURCE_DIR)
81 list(GET _cmake_binary_dirs 0 CMAKE_CURRENT_BINARY_DIR)
82
83 #pop the subdir names of the front of the list
84 list(REMOVE_AT _cmake_source_dirs 0)
85 list(REMOVE_AT _cmake_binary_dirs 0)
86endmacro(GR_INCLUDE_SUBDIRECTORY)
87
88########################################################################
89# Check if a compiler flag works and conditionally set a compile define.
90# - flag the compiler flag to check for
91# - have the variable to set with result
92########################################################################
93macro(GR_ADD_CXX_COMPILER_FLAG_IF_AVAILABLE flag have)
94 include(CheckCXXCompilerFlag)
95 CHECK_CXX_COMPILER_FLAG(${flag} ${have})
96 if(${have})
ptrkrysik12fe7a02014-11-20 11:10:20 +010097 if(${CMAKE_VERSION} VERSION_GREATER "2.8.4")
98 STRING(FIND "${CMAKE_CXX_FLAGS}" "${flag}" flag_dup)
99 if(${flag_dup} EQUAL -1)
100 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${flag}")
101 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${flag}")
102 endif(${flag_dup} EQUAL -1)
103 endif(${CMAKE_VERSION} VERSION_GREATER "2.8.4")
piotr437f5462014-02-04 17:57:25 +0100104 endif(${have})
105endmacro(GR_ADD_CXX_COMPILER_FLAG_IF_AVAILABLE)
106
107########################################################################
108# Generates the .la libtool file
109# This appears to generate libtool files that cannot be used by auto*.
110# Usage GR_LIBTOOL(TARGET [target] DESTINATION [dest])
111# Notice: there is not COMPONENT option, these will not get distributed.
112########################################################################
113function(GR_LIBTOOL)
114 if(NOT DEFINED GENERATE_LIBTOOL)
115 set(GENERATE_LIBTOOL OFF) #disabled by default
116 endif()
117
118 if(GENERATE_LIBTOOL)
119 include(CMakeParseArgumentsCopy)
120 CMAKE_PARSE_ARGUMENTS(GR_LIBTOOL "" "TARGET;DESTINATION" "" ${ARGN})
121
122 find_program(LIBTOOL libtool)
123 if(LIBTOOL)
124 include(CMakeMacroLibtoolFile)
125 CREATE_LIBTOOL_FILE(${GR_LIBTOOL_TARGET} /${GR_LIBTOOL_DESTINATION})
126 endif(LIBTOOL)
127 endif(GENERATE_LIBTOOL)
128
129endfunction(GR_LIBTOOL)
130
131########################################################################
132# Do standard things to the library target
133# - set target properties
134# - make install rules
135# Also handle gnuradio custom naming conventions w/ extras mode.
136########################################################################
137function(GR_LIBRARY_FOO target)
138 #parse the arguments for component names
139 include(CMakeParseArgumentsCopy)
140 CMAKE_PARSE_ARGUMENTS(GR_LIBRARY "" "RUNTIME_COMPONENT;DEVEL_COMPONENT" "" ${ARGN})
141
142 #set additional target properties
143 set_target_properties(${target} PROPERTIES SOVERSION ${LIBVER})
144
145 #install the generated files like so...
146 install(TARGETS ${target}
147 LIBRARY DESTINATION ${GR_LIBRARY_DIR} COMPONENT ${GR_LIBRARY_RUNTIME_COMPONENT} # .so/.dylib file
148 ARCHIVE DESTINATION ${GR_LIBRARY_DIR} COMPONENT ${GR_LIBRARY_DEVEL_COMPONENT} # .lib file
149 RUNTIME DESTINATION ${GR_RUNTIME_DIR} COMPONENT ${GR_LIBRARY_RUNTIME_COMPONENT} # .dll file
150 )
151
152 #extras mode enabled automatically on linux
153 if(NOT DEFINED LIBRARY_EXTRAS)
154 set(LIBRARY_EXTRAS ${LINUX})
155 endif()
156
157 #special extras mode to enable alternative naming conventions
158 if(LIBRARY_EXTRAS)
159
160 #create .la file before changing props
161 GR_LIBTOOL(TARGET ${target} DESTINATION ${GR_LIBRARY_DIR})
162
163 #give the library a special name with ultra-zero soversion
ptrkrysik12fe7a02014-11-20 11:10:20 +0100164 set_target_properties(${target} PROPERTIES OUTPUT_NAME ${target}-${LIBVER} SOVERSION "0.0.0")
piotr437f5462014-02-04 17:57:25 +0100165 set(target_name lib${target}-${LIBVER}.so.0.0.0)
166
167 #custom command to generate symlinks
168 add_custom_command(
169 TARGET ${target}
170 POST_BUILD
171 COMMAND ${CMAKE_COMMAND} -E create_symlink ${target_name} ${CMAKE_CURRENT_BINARY_DIR}/lib${target}.so
172 COMMAND ${CMAKE_COMMAND} -E create_symlink ${target_name} ${CMAKE_CURRENT_BINARY_DIR}/lib${target}-${LIBVER}.so.0
173 COMMAND ${CMAKE_COMMAND} -E touch ${target_name} #so the symlinks point to something valid so cmake 2.6 will install
174 )
175
176 #and install the extra symlinks
177 install(
178 FILES
179 ${CMAKE_CURRENT_BINARY_DIR}/lib${target}.so
180 ${CMAKE_CURRENT_BINARY_DIR}/lib${target}-${LIBVER}.so.0
181 DESTINATION ${GR_LIBRARY_DIR} COMPONENT ${GR_LIBRARY_RUNTIME_COMPONENT}
182 )
183
184 endif(LIBRARY_EXTRAS)
185endfunction(GR_LIBRARY_FOO)
186
187########################################################################
188# Create a dummy custom command that depends on other targets.
189# Usage:
190# GR_GEN_TARGET_DEPS(unique_name target_deps <target1> <target2> ...)
191# ADD_CUSTOM_COMMAND(<the usual args> ${target_deps})
192#
193# Custom command cant depend on targets, but can depend on executables,
194# and executables can depend on targets. So this is the process:
195########################################################################
196function(GR_GEN_TARGET_DEPS name var)
197 file(
198 WRITE ${CMAKE_CURRENT_BINARY_DIR}/${name}.cpp.in
199 "int main(void){return 0;}\n"
200 )
201 execute_process(
202 COMMAND ${CMAKE_COMMAND} -E copy_if_different
203 ${CMAKE_CURRENT_BINARY_DIR}/${name}.cpp.in
204 ${CMAKE_CURRENT_BINARY_DIR}/${name}.cpp
205 )
206 add_executable(${name} ${CMAKE_CURRENT_BINARY_DIR}/${name}.cpp)
207 if(ARGN)
208 add_dependencies(${name} ${ARGN})
209 endif(ARGN)
210
211 if(CMAKE_CROSSCOMPILING)
212 set(${var} "DEPENDS;${name}" PARENT_SCOPE) #cant call command when cross
213 else()
214 set(${var} "DEPENDS;${name};COMMAND;${name}" PARENT_SCOPE)
215 endif()
216endfunction(GR_GEN_TARGET_DEPS)
ptrkrysik12fe7a02014-11-20 11:10:20 +0100217
218########################################################################
219# Control use of gr_logger
220# Usage:
221# GR_LOGGING()
222#
223# Will set ENABLE_GR_LOG to 1 by default.
224# Can manually set with -DENABLE_GR_LOG=0|1
225########################################################################
226function(GR_LOGGING)
227 find_package(Log4cpp)
228
229 OPTION(ENABLE_GR_LOG "Use gr_logger" ON)
230 if(ENABLE_GR_LOG)
231 # If gr_logger is enabled, make it usable
232 add_definitions( -DENABLE_GR_LOG )
233
234 # also test LOG4CPP; if we have it, use this version of the logger
235 # otherwise, default to the stdout/stderr model.
236 if(LOG4CPP_FOUND)
237 SET(HAVE_LOG4CPP True CACHE INTERNAL "" FORCE)
238 add_definitions( -DHAVE_LOG4CPP )
239 else(not LOG4CPP_FOUND)
240 SET(HAVE_LOG4CPP False CACHE INTERNAL "" FORCE)
241 SET(LOG4CPP_INCLUDE_DIRS "" CACHE INTERNAL "" FORCE)
242 SET(LOG4CPP_LIBRARY_DIRS "" CACHE INTERNAL "" FORCE)
243 SET(LOG4CPP_LIBRARIES "" CACHE INTERNAL "" FORCE)
244 endif(LOG4CPP_FOUND)
245
246 SET(ENABLE_GR_LOG ${ENABLE_GR_LOG} CACHE INTERNAL "" FORCE)
247
248 else(ENABLE_GR_LOG)
249 SET(HAVE_LOG4CPP False CACHE INTERNAL "" FORCE)
250 SET(LOG4CPP_INCLUDE_DIRS "" CACHE INTERNAL "" FORCE)
251 SET(LOG4CPP_LIBRARY_DIRS "" CACHE INTERNAL "" FORCE)
252 SET(LOG4CPP_LIBRARIES "" CACHE INTERNAL "" FORCE)
253 endif(ENABLE_GR_LOG)
254
255 message(STATUS "ENABLE_GR_LOG set to ${ENABLE_GR_LOG}.")
256 message(STATUS "HAVE_LOG4CPP set to ${HAVE_LOG4CPP}.")
257 message(STATUS "LOG4CPP_LIBRARIES set to ${LOG4CPP_LIBRARIES}.")
258
259endfunction(GR_LOGGING)
260
261########################################################################
ptrkrysik12fe7a02014-11-20 11:10:20 +0100262# Check if HAVE_PTHREAD_SETSCHEDPARAM and HAVE_SCHED_SETSCHEDULER
263# should be defined
264########################################################################
265macro(GR_CHECK_LINUX_SCHED_AVAIL)
266set(CMAKE_REQUIRED_LIBRARIES -lpthread)
267 CHECK_CXX_SOURCE_COMPILES("
268 #include <pthread.h>
269 int main(){
270 pthread_t pthread;
271 pthread_setschedparam(pthread, 0, 0);
272 return 0;
273 } " HAVE_PTHREAD_SETSCHEDPARAM
274 )
275 GR_ADD_COND_DEF(HAVE_PTHREAD_SETSCHEDPARAM)
276
277 CHECK_CXX_SOURCE_COMPILES("
278 #include <sched.h>
279 int main(){
280 pid_t pid;
281 sched_setscheduler(pid, 0, 0);
282 return 0;
283 } " HAVE_SCHED_SETSCHEDULER
284 )
285 GR_ADD_COND_DEF(HAVE_SCHED_SETSCHEDULER)
286endmacro(GR_CHECK_LINUX_SCHED_AVAIL)
287
288########################################################################
289# Macros to generate source and header files from template
290########################################################################
291macro(GR_EXPAND_X_H component root)
292
293 include(GrPython)
294
295 file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/generate_helper.py
296"#!${PYTHON_EXECUTABLE}
297
298import sys, os, re
299sys.path.append('${GR_RUNTIME_PYTHONPATH}')
300os.environ['srcdir'] = '${CMAKE_CURRENT_SOURCE_DIR}'
301os.chdir('${CMAKE_CURRENT_BINARY_DIR}')
302
303if __name__ == '__main__':
304 import build_utils
305 root, inp = sys.argv[1:3]
306 for sig in sys.argv[3:]:
307 name = re.sub ('X+', sig, root)
308 d = build_utils.standard_dict2(name, sig, '${component}')
309 build_utils.expand_template(d, inp)
310")
311
312 #make a list of all the generated headers
313 unset(expanded_files_h)
314 foreach(sig ${ARGN})
315 string(REGEX REPLACE "X+" ${sig} name ${root})
316 list(APPEND expanded_files_h ${CMAKE_CURRENT_BINARY_DIR}/${name}.h)
317 endforeach(sig)
318 unset(name)
319
320 #create a command to generate the headers
321 add_custom_command(
322 OUTPUT ${expanded_files_h}
323 DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${root}.h.t
324 COMMAND ${PYTHON_EXECUTABLE} ${PYTHON_DASH_B}
325 ${CMAKE_CURRENT_BINARY_DIR}/generate_helper.py
326 ${root} ${root}.h.t ${ARGN}
327 )
328
329 #install rules for the generated headers
330 list(APPEND generated_includes ${expanded_files_h})
331
332endmacro(GR_EXPAND_X_H)
333
334macro(GR_EXPAND_X_CC_H component root)
335
336 include(GrPython)
337
338 file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/generate_helper.py
339"#!${PYTHON_EXECUTABLE}
340
341import sys, os, re
342sys.path.append('${GR_RUNTIME_PYTHONPATH}')
343os.environ['srcdir'] = '${CMAKE_CURRENT_SOURCE_DIR}'
344os.chdir('${CMAKE_CURRENT_BINARY_DIR}')
345
346if __name__ == '__main__':
347 import build_utils
348 root, inp = sys.argv[1:3]
349 for sig in sys.argv[3:]:
350 name = re.sub ('X+', sig, root)
351 d = build_utils.standard_impl_dict2(name, sig, '${component}')
352 build_utils.expand_template(d, inp)
353")
354
355 #make a list of all the generated files
356 unset(expanded_files_cc)
357 unset(expanded_files_h)
358 foreach(sig ${ARGN})
359 string(REGEX REPLACE "X+" ${sig} name ${root})
360 list(APPEND expanded_files_cc ${CMAKE_CURRENT_BINARY_DIR}/${name}.cc)
361 list(APPEND expanded_files_h ${CMAKE_CURRENT_BINARY_DIR}/${name}.h)
362 endforeach(sig)
363 unset(name)
364
365 #create a command to generate the source files
366 add_custom_command(
367 OUTPUT ${expanded_files_cc}
368 DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${root}.cc.t
369 COMMAND ${PYTHON_EXECUTABLE} ${PYTHON_DASH_B}
370 ${CMAKE_CURRENT_BINARY_DIR}/generate_helper.py
371 ${root} ${root}.cc.t ${ARGN}
372 )
373
374 #create a command to generate the header files
375 add_custom_command(
376 OUTPUT ${expanded_files_h}
377 DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${root}.h.t
378 COMMAND ${PYTHON_EXECUTABLE} ${PYTHON_DASH_B}
379 ${CMAKE_CURRENT_BINARY_DIR}/generate_helper.py
380 ${root} ${root}.h.t ${ARGN}
381 )
382
383 #make source files depends on headers to force generation
384 set_source_files_properties(${expanded_files_cc}
385 PROPERTIES OBJECT_DEPENDS "${expanded_files_h}"
386 )
387
388 #install rules for the generated files
389 list(APPEND generated_sources ${expanded_files_cc})
390 list(APPEND generated_headers ${expanded_files_h})
391
392endmacro(GR_EXPAND_X_CC_H)
393
394macro(GR_EXPAND_X_CC_H_IMPL component root)
395
396 include(GrPython)
397
398 file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/generate_helper.py
399"#!${PYTHON_EXECUTABLE}
400
401import sys, os, re
402sys.path.append('${GR_RUNTIME_PYTHONPATH}')
403os.environ['srcdir'] = '${CMAKE_CURRENT_SOURCE_DIR}'
404os.chdir('${CMAKE_CURRENT_BINARY_DIR}')
405
406if __name__ == '__main__':
407 import build_utils
408 root, inp = sys.argv[1:3]
409 for sig in sys.argv[3:]:
410 name = re.sub ('X+', sig, root)
411 d = build_utils.standard_dict(name, sig, '${component}')
412 build_utils.expand_template(d, inp, '_impl')
413")
414
415 #make a list of all the generated files
416 unset(expanded_files_cc_impl)
417 unset(expanded_files_h_impl)
418 unset(expanded_files_h)
419 foreach(sig ${ARGN})
420 string(REGEX REPLACE "X+" ${sig} name ${root})
421 list(APPEND expanded_files_cc_impl ${CMAKE_CURRENT_BINARY_DIR}/${name}_impl.cc)
422 list(APPEND expanded_files_h_impl ${CMAKE_CURRENT_BINARY_DIR}/${name}_impl.h)
423 list(APPEND expanded_files_h ${CMAKE_CURRENT_BINARY_DIR}/../include/gnuradio/${component}/${name}.h)
424 endforeach(sig)
425 unset(name)
426
427 #create a command to generate the _impl.cc files
428 add_custom_command(
429 OUTPUT ${expanded_files_cc_impl}
430 DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${root}_impl.cc.t
431 COMMAND ${PYTHON_EXECUTABLE} ${PYTHON_DASH_B}
432 ${CMAKE_CURRENT_BINARY_DIR}/generate_helper.py
433 ${root} ${root}_impl.cc.t ${ARGN}
434 )
435
436 #create a command to generate the _impl.h files
437 add_custom_command(
438 OUTPUT ${expanded_files_h_impl}
439 DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${root}_impl.h.t
440 COMMAND ${PYTHON_EXECUTABLE} ${PYTHON_DASH_B}
441 ${CMAKE_CURRENT_BINARY_DIR}/generate_helper.py
442 ${root} ${root}_impl.h.t ${ARGN}
443 )
444
445 #make _impl.cc source files depend on _impl.h to force generation
446 set_source_files_properties(${expanded_files_cc_impl}
447 PROPERTIES OBJECT_DEPENDS "${expanded_files_h_impl}"
448 )
449
450 #make _impl.h source files depend on headers to force generation
451 set_source_files_properties(${expanded_files_h_impl}
452 PROPERTIES OBJECT_DEPENDS "${expanded_files_h}"
453 )
454
455 #install rules for the generated files
456 list(APPEND generated_sources ${expanded_files_cc_impl})
457 list(APPEND generated_headers ${expanded_files_h_impl})
458
459endmacro(GR_EXPAND_X_CC_H_IMPL)