Statistics
| Branch: | Tag: | Revision:

root / gr-howto-write-a-block / cmake / Modules / GrTest.cmake @ 00420d32

History | View | Annotate | Download (5.4 kB)

1
# Copyright 2010-2011 Free Software Foundation, Inc.
2
# 
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
20
if(DEFINED __INCLUDED_GR_TEST_CMAKE)
21
    return()
22
endif()
23
set(__INCLUDED_GR_TEST_CMAKE TRUE)
24
25
########################################################################
26
# Add a unit test and setup the environment for a unit test.
27
# Takes the same arguments as the ADD_TEST function.
28
#
29
# Before calling set the following variables:
30
# GR_TEST_TARGET_DEPS  - built targets for the library path
31
# GR_TEST_LIBRARY_DIRS - directories for the library path
32
# GR_TEST_PYTHON_DIRS  - directories for the python path
33
########################################################################
34
function(GR_ADD_TEST test_name)
35
36
    if(WIN32)
37
        #Ensure that the build exe also appears in the PATH.
38
        list(APPEND GR_TEST_TARGET_DEPS ${ARGN})
39
40
        #In the land of windows, all libraries must be in the PATH.
41
        #Since the dependent libraries are not yet installed,
42
        #we must manually set them in the PATH to run tests.
43
        #The following appends the path of a target dependency.
44
        foreach(target ${GR_TEST_TARGET_DEPS})
45
            get_target_property(location ${target} LOCATION)
46
            if(location)
47
                get_filename_component(path ${location} PATH)
48
                string(REGEX REPLACE "\\$\\(.*\\)" ${CMAKE_BUILD_TYPE} path ${path})
49
                list(APPEND GR_TEST_LIBRARY_DIRS ${path})
50
            endif(location)
51
        endforeach(target)
52
53
        #SWIG generates the python library files into a subdirectory.
54
        #Therefore, we must append this subdirectory into PYTHONPATH.
55
        #Only do this for the python directories matching the following:
56
        foreach(pydir ${GR_TEST_PYTHON_DIRS})
57
            get_filename_component(name ${pydir} NAME)
58
            if(name MATCHES "^(swig|lib|src)$")
59
                list(APPEND GR_TEST_PYTHON_DIRS ${pydir}/${CMAKE_BUILD_TYPE})
60
            endif()
61
        endforeach(pydir)
62
    endif(WIN32)
63
64
    file(TO_NATIVE_PATH ${CMAKE_CURRENT_SOURCE_DIR} srcdir)
65
    file(TO_NATIVE_PATH "${GR_TEST_LIBRARY_DIRS}" libpath) #ok to use on dir list?
66
    file(TO_NATIVE_PATH "${GR_TEST_PYTHON_DIRS}" pypath) #ok to use on dir list?
67
68
    set(environs "GR_DONT_LOAD_PREFS=1" "srcdir=${srcdir}")
69
70
    #http://www.cmake.org/pipermail/cmake/2009-May/029464.html
71
    #Replaced this add test + set environs code with the shell script generation.
72
    #Its nicer to be able to manually run the shell script to diagnose problems.
73
    #ADD_TEST(${ARGV})
74
    #SET_TESTS_PROPERTIES(${test_name} PROPERTIES ENVIRONMENT "${environs}")
75
76
    if(UNIX)
77
        set(binpath "${CMAKE_CURRENT_BINARY_DIR}:$PATH")
78
        #set both LD and DYLD paths to cover multiple UNIX OS library paths
79
        list(APPEND libpath "$LD_LIBRARY_PATH" "$DYLD_LIBRARY_PATH")
80
        list(APPEND pypath "$PYTHONPATH")
81
82
        #replace list separator with the path separator
83
        string(REPLACE ";" ":" libpath "${libpath}")
84
        string(REPLACE ";" ":" pypath "${pypath}")
85
        list(APPEND environs "PATH=${binpath}" "LD_LIBRARY_PATH=${libpath}" "DYLD_LIBRARY_PATH=${libpath}" "PYTHONPATH=${pypath}")
86
87
        #generate a bat file that sets the environment and runs the test
88
        find_program(SHELL sh)
89
        set(sh_file ${CMAKE_CURRENT_BINARY_DIR}/${test_name}_test.sh)
90
        file(WRITE ${sh_file} "#!${SHELL}\n")
91
        #each line sets an environment variable
92
        foreach(environ ${environs})
93
            file(APPEND ${sh_file} "export ${environ}\n")
94
        endforeach(environ)
95
        #load the command to run with its arguments
96
        foreach(arg ${ARGN})
97
            file(APPEND ${sh_file} "${arg} ")
98
        endforeach(arg)
99
        file(APPEND ${sh_file} "\n")
100
101
        #make the shell file executable
102
        execute_process(COMMAND chmod +x ${sh_file})
103
104
        add_test(${test_name} ${SHELL} ${sh_file})
105
106
    endif(UNIX)
107
108
    if(WIN32)
109
        list(APPEND libpath ${DLL_PATHS} "%PATH%")
110
        list(APPEND pypath "%PYTHONPATH%")
111
112
        #replace list separator with the path separator (escaped)
113
        string(REPLACE ";" "\\;" libpath "${libpath}")
114
        string(REPLACE ";" "\\;" pypath "${pypath}")
115
        list(APPEND environs "PATH=${libpath}" "PYTHONPATH=${pypath}")
116
117
        #generate a bat file that sets the environment and runs the test
118
        set(bat_file ${CMAKE_CURRENT_BINARY_DIR}/${test_name}_test.bat)
119
        file(WRITE ${bat_file} "@echo off\n")
120
        #each line sets an environment variable
121
        foreach(environ ${environs})
122
            file(APPEND ${bat_file} "SET ${environ}\n")
123
        endforeach(environ)
124
        #load the command to run with its arguments
125
        foreach(arg ${ARGN})
126
            file(APPEND ${bat_file} "${arg} ")
127
        endforeach(arg)
128
        file(APPEND ${bat_file} "\n")
129
130
        add_test(${test_name} ${bat_file})
131
    endif(WIN32)
132
133
endfunction(GR_ADD_TEST)