Add script to generate Debian package files for static applications
[oonf.git] / cmake / link_app.cmake
1 # the order of static libraries is important
2 # earlier libraries can use the functions of later, not the
3 # other way around
4
5 function (oonf_create_install_target name)
6     ADD_CUSTOM_TARGET(install_${name}
7                       COMMAND ${CMAKE_COMMAND} 
8                       -DBUILD_TYPE=${CMAKE_BUILD_TYPE}
9                       -DCOMPONENT=component_${name}
10                       -P ${CMAKE_BINARY_DIR}/cmake_install.cmake)
11     ADD_DEPENDENCIES(install_${name}   ${name})
12     
13     get_property(value TARGET ${name} PROPERTY LINK_LIBRARIES)
14     FOREACH(lib ${value})
15         IF (TARGET ${lib})
16             ADD_DEPENDENCIES(install_${name} ${lib})
17             ADD_DEPENDENCIES(install_${name} install_${lib})
18         ENDIF(TARGET ${lib})
19     ENDFOREACH(lib)     
20 endfunction (oonf_create_install_target)
21     
22 function (oonf_create_app executable static_plugins optional_static_plugins)
23     IF(VERBOSE)
24         message (STATUS "Static plugins for ${executable} app:")
25     ENDIF(VERBOSE)
26
27     # standard static linked targets
28     SET(OBJECT_TARGETS )
29     SET(EXTERNAL_LIBRARIES )
30     SET(STATIC_PLUGIN_LIST )
31     
32     # generate configuration file
33     configure_file(${CMAKE_SOURCE_DIR}/src/app_data.c.in ${PROJECT_BINARY_DIR}/${executable}_app_data.c)
34
35     FOREACH(plugin ${optional_static_plugins})
36         IF(TARGET oonf_static_${plugin})
37             list (APPEND static_plugins ${plugin})
38         ELSE(TARGET oonf_static_${plugin})
39             message (STATUS "    Optional plugin ${plugin} is not available for executable ${executable}")
40         ENDIF(TARGET oonf_static_${plugin})
41     ENDFOREACH(plugin)
42
43     # run through list of static plugins
44     FOREACH(plugin ${static_plugins})
45         IF(TARGET oonf_static_${plugin})
46             IF(VERBOSE)
47                 message (STATUS "    Found target: oonf_static_${plugin}")
48             ENDIF(VERBOSE)
49
50             # Remember object targets for static plugin
51             SET(OBJECT_TARGETS ${OBJECT_TARGETS} $<TARGET_OBJECTS:oonf_static_${plugin}>)
52         
53             # extract external libraries of plugin
54             get_property(value TARGET oonf_${plugin} PROPERTY LINK_LIBRARIES)
55             FOREACH(lib ${value})
56                 IF(NOT "${lib}" MATCHES "^oonf_")
57                     IF(VERBOSE)
58                         message (STATUS "        Library: ${lib}")
59                     ENDIF(VERBOSE)
60                     SET(EXTERNAL_LIBRARIES ${EXTERNAL_LIBRARIES} ${lib})
61                 ENDIF()
62             ENDFOREACH(lib)
63         ELSE (TARGET oonf_static_${plugin})
64             ADD_CUSTOM_TARGET(${executable}_dynamic ALL COMMAND false COMMENT "Plugin ${plugin} is not there, maybe a dependency is missing?")
65             ADD_CUSTOM_TARGET(${executable}_static  ALL COMMAND false COMMENT "Plugin ${plugin} is not there, maybe a dependency is missing?")
66             return()
67         ENDIF(TARGET oonf_static_${plugin})
68     ENDFOREACH(plugin)
69
70     # create executables
71     ADD_EXECUTABLE(${executable}_dynamic ${CMAKE_SOURCE_DIR}/src/main.c
72                                          ${PROJECT_BINARY_DIR}/${executable}_app_data.c
73                                          ${OBJECT_TARGETS})
74     ADD_EXECUTABLE(${executable}_static  ${CMAKE_SOURCE_DIR}/src/main.c
75                                          ${PROJECT_BINARY_DIR}/${executable}_app_data.c
76                                          ${OBJECT_TARGETS}
77                                          $<TARGET_OBJECTS:oonf_static_common>
78                                          $<TARGET_OBJECTS:oonf_static_config>
79                                          $<TARGET_OBJECTS:oonf_static_core>)
80
81     # Add executables to static/dynamic target
82     ADD_DEPENDENCIES(dynamic ${executable}_dynamic)
83     ADD_DEPENDENCIES(static  ${executable}_static)
84     
85     # link framework libraries to dynamic executable
86     TARGET_LINK_LIBRARIES(${executable}_dynamic PUBLIC oonf_core
87                                                        oonf_config
88                                                        oonf_common)
89
90     # link external libraries directly to executable
91     TARGET_LINK_LIBRARIES(${executable}_dynamic PUBLIC ${EXTERNAL_LIBRARIES})
92     TARGET_LINK_LIBRARIES(${executable}_static  PUBLIC ${EXTERNAL_LIBRARIES})
93
94     # link dlopen() library
95     TARGET_LINK_LIBRARIES(${executable}_dynamic PUBLIC ${CMAKE_DL_LIBS})
96     TARGET_LINK_LIBRARIES(${executable}_static  PUBLIC ${CMAKE_DL_LIBS})
97     
98     # create install targets
99     INSTALL (TARGETS ${executable}_dynamic RUNTIME 
100                                            DESTINATION bin 
101                                            COMPONENT component_${executable}_dynamic)
102     INSTALL (TARGETS ${executable}_static  RUNTIME
103                                            DESTINATION bin 
104                                            COMPONENT component_${executable}_static)
105
106     # add custom install targets
107     oonf_create_install_target("${executable}_dynamic")
108     oonf_create_install_target("${executable}_static")
109 endfunction(oonf_create_app)