[Release] wrt-commons_0.2.133
[framework/web/wrt-commons.git] / tests / CMakeUtils.txt
1 # @file        CMakeUtils.txt
2 # @author      Zbigniew Kostrzewa (z.kostrzewa@samsung.com)
3 # @author      Pawel Sikorski (p.sikorski@samsung.com)
4 # @author      Krzysztof Jackiewicz (k.jackiewicz@samsung.com)
5 # @version     1.0
6 # @brief
7 #
8
9 #
10 # Discovers target's INCLUDE_DIRECTORIES and LINK_DIRECTORIES.
11 # This is done by retrieving the directory target was built in and
12 # fetching appropriate properties of that directory.
13 FUNCTION(WRT_INTROSPECT_TARGET PREFIX TARGET_NAME)
14   GET_TARGET_PROPERTY(LOCATION ${TARGET_NAME} LOCATION)
15   IF(${LOCATION} STREQUAL "LOCATION-NOTFOUND")
16     MESSAGE(FATAL_ERROR "Target '${TARGET_NAME}' introspection failed")
17   ELSE(${LOCATION} STREQUAL "LOCATION-NOTFOUND")
18     STRING(FIND ${LOCATION} "/" LAST_SLASH_POSITION REVERSE)
19     STRING(SUBSTRING ${LOCATION} 0 ${LAST_SLASH_POSITION} LOCATION)
20
21     GET_DIRECTORY_PROPERTY(INCLUDE_DIRS DIRECTORY ${LOCATION} INCLUDE_DIRECTORIES)
22     SET("${PREFIX}_INCLUDE_DIRS" ${INCLUDE_DIRS} PARENT_SCOPE)
23
24     GET_DIRECTORY_PROPERTY(LIBRARY_DIRS DIRECTORY ${LOCATION} LINK_DIRECTORIES)
25     SET("${PREFIX}_LIBRARY_DIRS" ${LIBRARY_DIRS} PARENT_SCOPE)
26   ENDIF(${LOCATION} STREQUAL "LOCATION-NOTFOUND")
27 ENDFUNCTION(WRT_INTROSPECT_TARGET)
28
29 #
30 # Replacement functions for standard (w/o "WRT_" prefix) CMake functions.
31 # They store supplied arguments in global properties to assign them to tests.
32 # Anything added with this functions is used by all targets that are built with
33 # WRT_TEST_BUILD function.
34
35 #
36 # Appends directories to global property TESTS_INCLUDE_DIRS which is
37 # then read by WRT_TEST_BUILD and its content is forwarded to
38 # command INCLUDE_DIRECTORIES() (for all targets).
39 FUNCTION(WRT_INCLUDE_DIRECTORIES)
40   SET_PROPERTY(GLOBAL APPEND PROPERTY TESTS_INCLUDE_DIRS ${ARGV})
41 ENDFUNCTION(WRT_INCLUDE_DIRECTORIES)
42
43 #
44 # Appends directories to global property TESTS_LIBRARY_DIRS which is
45 # then read by WRT_TEST_BUILD and its content is forwarded to
46 # command LINK_DIRECTORIES() (for all targets).
47 FUNCTION(WRT_LINK_DIRECTORIES)
48   SET_PROPERTY(GLOBAL APPEND PROPERTY TESTS_LIBRARY_DIRS ${ARGV})
49 ENDFUNCTION(WRT_LINK_DIRECTORIES)
50
51 #
52 # Appends directories to global property TESTS_LIBRARIES which is
53 # then read by WRT_TEST_BUILD and its content is forwarded to
54 # command TARGET_LINK_LIBRARIES() (for all targets).
55 FUNCTION(WRT_TARGET_LINK_LIBRARIES)
56   SET_PROPERTY(GLOBAL APPEND PROPERTY TESTS_LIBRARIES ${ARGV})
57 ENDFUNCTION(WRT_TARGET_LINK_LIBRARIES)
58
59 #
60 # Convenience method that fills TESTS_INCLUDE_DIRS, TESTS_LIBRARY_DIRS
61 # and TESTS_LIBRARIES with values discovered from introspecting supplied
62 # targets.
63 # Function takes arbitrary number of targets.
64 FUNCTION(WRT_ADD_INTERNAL_DEPENDENCIES)
65   FOREACH(DEPENDENCY ${ARGV})
66     WRT_INTROSPECT_TARGET(prefix ${DEPENDENCY})
67     WRT_INCLUDE_DIRECTORIES(${prefix_INCLUDE_DIRS})
68     WRT_LINK_DIRECTORIES(${prefix_LIBRARY_DIRS})
69     WRT_TARGET_LINK_LIBRARIES(${DEPENDENCY})
70   ENDFOREACH(DEPENDENCY)
71 ENDFUNCTION(WRT_ADD_INTERNAL_DEPENDENCIES)
72
73
74 #
75 # Replacement functions for standard (w/o "WRT_" prefix) CMake functions.
76 # They store supplied arguments in global properties to assign them to specific
77 # tests. Properties names are based on the test target name.
78 # Anything added with this functions is used only by the specified target that
79 # is built with WRT_TEST_BUILD function.
80
81 #
82 # Appends directories to global property ${TARGET_NAME}_INCLUDE_DIRS
83 # which is then read by WRT_TEST_BUILD and its content is forwarded to
84 # command INCLUDE_DIRECTORIES() (for specified target).
85 FUNCTION(WRT_TEST_INCLUDE_DIRECTORIES TARGET_NAME)
86   SET_PROPERTY(GLOBAL APPEND PROPERTY ${TARGET_NAME}_INCLUDE_DIRS ${ARGN})
87 ENDFUNCTION(WRT_TEST_INCLUDE_DIRECTORIES)
88
89 #
90 # Appends directories to global property ${TARGET_NAME}_LIBRARY_DIRS
91 # which is then read by WRT_TEST_BUILD and its content is forwarded to
92 # command LINK_DIRECTORIES() (for specified target).
93 FUNCTION(WRT_TEST_LINK_DIRECTORIES TARGET_NAME)
94   SET_PROPERTY(GLOBAL APPEND PROPERTY ${TARGET_NAME}_LIBRARY_DIRS ${ARGN})
95 ENDFUNCTION(WRT_TEST_LINK_DIRECTORIES)
96
97 #
98 # Appends directories to global property ${TARGET_NAME}_LIBRARIES
99 # which is then read by WRT_TEST_BUILD and its content is forwarded to
100 # command TARGET_LINK_LIBRARIES() (for specified target).
101 FUNCTION(WRT_TEST_TARGET_LINK_LIBRARIES TARGET_NAME)
102   SET_PROPERTY(GLOBAL APPEND PROPERTY ${TARGET_NAME}_LIBRARIES ${ARGN})
103 ENDFUNCTION(WRT_TEST_TARGET_LINK_LIBRARIES)
104
105 #
106 # Convenience method that fills ${TARGET_NAME}_INCLUDE_DIRS,
107 # ${TARGET_NAME}_LIBRARY_DIRS and ${TARGET_NAME}_LIBRARIES with
108 # values discovered from introspecting supplied targets.
109 # Function takes arbitrary number of targets.
110 FUNCTION(WRT_TEST_ADD_INTERNAL_DEPENDENCIES TARGET_NAME)
111   FOREACH(DEPENDENCY ${ARGN})
112     WRT_INTROSPECT_TARGET(prefix ${DEPENDENCY})
113     WRT_TEST_INCLUDE_DIRECTORIES(${TARGET_NAME} ${prefix_INCLUDE_DIRS})
114     WRT_TEST_LINK_DIRECTORIES(${TARGET_NAME} ${prefix_LIBRARY_DIRS})
115     WRT_TEST_TARGET_LINK_LIBRARIES(${TARGET_NAME} ${DEPENDENCY})
116   ENDFOREACH(DEPENDENCY)
117 ENDFUNCTION(WRT_TEST_ADD_INTERNAL_DEPENDENCIES)
118
119 # Functions used to build test targets (proper sources, includes, libs are
120 # added automatically)
121 FUNCTION(WRT_TEST_BUILD TARGET_NAME)
122     SET(SOURCES "${ARGN}")
123     ADD_EXECUTABLE("${TARGET_NAME}" ${SOURCES})
124
125     # get include dirs global property
126     GET_PROPERTY(INCLUDE_DIRS GLOBAL PROPERTY TESTS_INCLUDE_DIRS)
127     GET_PROPERTY(TEST_INCLUDE_DIRS GLOBAL PROPERTY ${TARGET_NAME}_INCLUDE_DIRS)
128     INCLUDE_DIRECTORIES(
129         ${INCLUDE_DIRS}
130         ${TEST_INCLUDE_DIRS}
131     )
132
133     # get library dirs global property
134     GET_PROPERTY(LIBRARY_DIRS GLOBAL PROPERTY TESTS_LIBRARY_DIRS)
135     GET_PROPERTY(TEST_LIBRARY_DIRS GLOBAL PROPERTY ${TARGET_NAME}_LIBRARY_DIRS)
136     LINK_DIRECTORIES(
137         ${LIBRARY_DIRS}
138         ${TEST_LIBRARY_DIRS}
139     )
140
141     # get link libraries global property
142     GET_PROPERTY(LINK_LIBRARIES GLOBAL PROPERTY TESTS_LIBRARIES)
143     GET_PROPERTY(TEST_LIBRARIES GLOBAL PROPERTY ${TARGET_NAME}_LIBRARIES)
144     TARGET_LINK_LIBRARIES("${TARGET_NAME}"
145         ${LINK_LIBRARIES}
146         ${TEST_LIBRARIES}
147     )
148 ENDFUNCTION(WRT_TEST_BUILD)
149
150 FUNCTION(WRT_TEST_INSTALL)
151     SET_TARGET_PROPERTIES(${ARGV} PROPERTIES
152         BUILD_WITH_INSTALL_RPATH ON
153         INSTALL_RPATH_USE_LINK_PATH ON
154     )
155     INSTALL(TARGETS ${ARGV}
156         DESTINATION bin
157         PERMISSIONS OWNER_READ
158                     OWNER_WRITE
159                     OWNER_EXECUTE
160                     GROUP_READ
161                     GROUP_EXECUTE
162                     WORLD_READ
163                     WORLD_EXECUTE
164     )
165 ENDFUNCTION(WRT_TEST_INSTALL)
166
167 # Takes arbitrary number of arguments and concatenates them using ':' character.
168 # Rationale:
169 #   CMake list when converted to a string is joined with ';' character. However,
170 #   GCC takes strings with multiple elements separated with ':' (e.g. list of
171 #   paths). Used typically when generating DB schemas with ORM mechanism.
172 FUNCTION(WRT_CONVERT_TO_GCC_LIST OUTPUT_VARIABLE)
173     FOREACH(ITEM ${ARGN})
174         LIST(APPEND ITEMS ${ITEM})
175     ENDFOREACH(ITEM)
176     STRING(REPLACE ";" ":" OUTPUT "${ITEMS}")
177     SET("${OUTPUT_VARIABLE}" "${OUTPUT}" PARENT_SCOPE)
178 ENDFUNCTION(WRT_CONVERT_TO_GCC_LIST)