1
# Copyright 2010-2011 Free Software Foundation, Inc.
3
# This file is part of GNU Radio
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)
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.
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.
20
if(DEFINED __INCLUDED_GR_MISC_UTILS_CMAKE)
23
set(__INCLUDED_GR_MISC_UTILS_CMAKE TRUE)
25
########################################################################
26
# Set global variable macro.
27
# Used for subdirectories to export settings.
28
# Example: include and library paths.
29
########################################################################
30
function(GR_SET_GLOBAL var)
31
set(${var} ${ARGN} CACHE INTERNAL "" FORCE)
32
endfunction(GR_SET_GLOBAL)
34
########################################################################
35
# Set the pre-processor definition if the condition is true.
36
# - def the pre-processor definition to set and condition name
37
########################################################################
38
function(GR_ADD_COND_DEF def)
40
add_definitions(-D${def})
42
endfunction(GR_ADD_COND_DEF)
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
########################################################################
49
function(GR_CHECK_HDR_N_DEF hdr def)
50
include(CheckIncludeFileCXX)
51
CHECK_INCLUDE_FILE_CXX(${hdr} ${def})
52
GR_ADD_COND_DEF(${def})
53
endfunction(GR_CHECK_HDR_N_DEF)
55
########################################################################
56
# Include subdirectory macro.
57
# Sets the CMake directory variables,
58
# includes the subdirectory CMakeLists.txt,
59
# resets the CMake directory variables.
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
########################################################################
66
macro(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})
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})
75
#include the subdirectory CMakeLists to run it
76
file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
77
include(${CMAKE_CURRENT_SOURCE_DIR}/CMakeLists.txt)
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)
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)
86
endmacro(GR_INCLUDE_SUBDIRECTORY)
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
########################################################################
93
macro(GR_ADD_CXX_COMPILER_FLAG_IF_AVAILABLE flag have)
94
include(CheckCXXCompilerFlag)
95
CHECK_CXX_COMPILER_FLAG(${flag} ${have})
97
add_definitions(${flag})
99
endmacro(GR_ADD_CXX_COMPILER_FLAG_IF_AVAILABLE)
101
########################################################################
102
# Generates the .la libtool file
103
# This appears to generate libtool files that cannot be used by auto*.
104
# Usage GR_LIBTOOL(TARGET [target] DESTINATION [dest])
105
# Notice: there is not COMPONENT option, these will not get distributed.
106
########################################################################
108
if(NOT DEFINED GENERATE_LIBTOOL)
109
set(GENERATE_LIBTOOL OFF) #disabled by default
113
include(CMakeParseArgumentsCopy)
114
CMAKE_PARSE_ARGUMENTS(GR_LIBTOOL "" "TARGET;DESTINATION" "" ${ARGN})
116
find_program(LIBTOOL libtool)
118
include(CMakeMacroLibtoolFile)
119
CREATE_LIBTOOL_FILE(${GR_LIBTOOL_TARGET} /${GR_LIBTOOL_DESTINATION})
121
endif(GENERATE_LIBTOOL)
123
endfunction(GR_LIBTOOL)
125
########################################################################
126
# Do standard things to the library target
127
# - set target properties
128
# - make install rules
129
# Also handle gnuradio custom naming conventions w/ extras mode.
130
########################################################################
131
function(GR_LIBRARY_FOO target)
132
#parse the arguments for component names
133
include(CMakeParseArgumentsCopy)
134
CMAKE_PARSE_ARGUMENTS(GR_LIBRARY "" "RUNTIME_COMPONENT;DEVEL_COMPONENT" "" ${ARGN})
136
#set additional target properties
137
set_target_properties(${target} PROPERTIES SOVERSION ${LIBVER})
139
#install the generated files like so...
140
install(TARGETS ${target}
141
LIBRARY DESTINATION ${GR_LIBRARY_DIR} COMPONENT ${GR_LIBRARY_RUNTIME_COMPONENT} # .so/.dylib file
142
ARCHIVE DESTINATION ${GR_LIBRARY_DIR} COMPONENT ${GR_LIBRARY_DEVEL_COMPONENT} # .lib file
143
RUNTIME DESTINATION ${GR_RUNTIME_DIR} COMPONENT ${GR_LIBRARY_RUNTIME_COMPONENT} # .dll file
146
#extras mode enabled automatically on linux
147
if(NOT DEFINED LIBRARY_EXTRAS)
148
set(LIBRARY_EXTRAS ${LINUX})
151
#special extras mode to enable alternative naming conventions
154
#create .la file before changing props
155
GR_LIBTOOL(TARGET ${target} DESTINATION ${GR_LIBRARY_DIR})
157
#give the library a special name with ultra-zero soversion
158
set_target_properties(${target} PROPERTIES LIBRARY_OUTPUT_NAME ${target}-${LIBVER} SOVERSION "0.0.0")
159
set(target_name lib${target}-${LIBVER}.so.0.0.0)
161
#custom command to generate symlinks
165
COMMAND ${CMAKE_COMMAND} -E create_symlink ${target_name} ${CMAKE_CURRENT_BINARY_DIR}/lib${target}.so
166
COMMAND ${CMAKE_COMMAND} -E create_symlink ${target_name} ${CMAKE_CURRENT_BINARY_DIR}/lib${target}-${LIBVER}.so.0
167
COMMAND ${CMAKE_COMMAND} -E touch ${target_name} #so the symlinks point to something valid so cmake 2.6 will install
170
#and install the extra symlinks
173
${CMAKE_CURRENT_BINARY_DIR}/lib${target}.so
174
${CMAKE_CURRENT_BINARY_DIR}/lib${target}-${LIBVER}.so.0
175
DESTINATION ${GR_LIBRARY_DIR} COMPONENT ${GR_LIBRARY_RUNTIME_COMPONENT}
178
endif(LIBRARY_EXTRAS)
179
endfunction(GR_LIBRARY_FOO)
181
########################################################################
182
# Create a dummy custom command that depends on other targets.
184
# GR_GEN_TARGET_DEPS(unique_name target_deps <target1> <target2> ...)
185
# ADD_CUSTOM_COMMAND(<the usual args> ${target_deps})
187
# Custom command cant depend on targets, but can depend on executables,
188
# and executables can depend on targets. So this is the process:
189
########################################################################
190
function(GR_GEN_TARGET_DEPS name var)
192
WRITE ${CMAKE_CURRENT_BINARY_DIR}/${name}.cpp.in
193
"int main(void){return 0;}\n"
196
COMMAND ${CMAKE_COMMAND} -E copy_if_different
197
${CMAKE_CURRENT_BINARY_DIR}/${name}.cpp.in
198
${CMAKE_CURRENT_BINARY_DIR}/${name}.cpp
200
add_executable(${name} ${CMAKE_CURRENT_BINARY_DIR}/${name}.cpp)
202
add_dependencies(${name} ${ARGN})
205
if(CMAKE_CROSSCOMPILING)
206
set(${var} "DEPENDS;${name}" PARENT_SCOPE) #cant call command when cross
208
set(${var} "DEPENDS;${name};COMMAND;${name}" PARENT_SCOPE)
210
endfunction(GR_GEN_TARGET_DEPS)