build: link time optimization for release builds
[vpp.git] / src / cmake / library.cmake
1 # Copyright (c) 2018 Cisco and/or its affiliates.
2 # Licensed under the Apache License, Version 2.0 (the "License");
3 # you may not use this file except in compliance with the License.
4 # You may obtain a copy of the License at:
5 #
6 #     http://www.apache.org/licenses/LICENSE-2.0
7 #
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS,
10 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 # See the License for the specific language governing permissions and
12 # limitations under the License.
13
14 macro(add_vpp_library lib)
15   cmake_parse_arguments(ARG
16     "LTO"
17     "COMPONENT"
18     "SOURCES;MULTIARCH_SOURCES;API_FILES;LINK_LIBRARIES;INSTALL_HEADERS;DEPENDS"
19     ${ARGN}
20   )
21
22   add_library(${lib} SHARED ${ARG_SOURCES})
23   if(VPP_LIB_VERSION)
24     set_target_properties(${lib} PROPERTIES SOVERSION ${VPP_LIB_VERSION})
25   endif()
26
27   # library deps
28   if(ARG_LINK_LIBRARIES)
29     target_link_libraries(${lib} ${ARG_LINK_LIBRARIES})
30   endif()
31   # install .so
32   if(NOT ARG_COMPONENT)
33     set(ARG_COMPONENT vpp)
34   endif()
35   install(
36     TARGETS ${lib}
37     DESTINATION ${VPP_LIBRARY_DIR}
38     COMPONENT ${ARG_COMPONENT}
39   )
40
41   if (ARG_LTO AND VPP_USE_LTO)
42      set_property(TARGET ${lib} PROPERTY INTERPROCEDURAL_OPTIMIZATION TRUE)
43      target_compile_options (${lib} PRIVATE "-ffunction-sections")
44      target_compile_options (${lib} PRIVATE "-fdata-sections")
45      target_link_libraries (${lib} "-Wl,--gc-sections")
46   endif()
47
48   if(ARG_MULTIARCH_SOURCES)
49     vpp_library_set_multiarch_sources(${lib} "${ARG_DEPENDS}" ${ARG_MULTIARCH_SOURCES})
50   endif()
51
52   if(ARG_API_FILES)
53     vpp_add_api_files(${lib} core vpp ${ARG_API_FILES})
54     foreach(file ${ARG_API_FILES})
55       get_filename_component(dir ${file} DIRECTORY)
56       install(
57         FILES ${file} ${CMAKE_CURRENT_BINARY_DIR}/${file}.h
58         ${CMAKE_CURRENT_BINARY_DIR}/${file}_enum.h
59         ${CMAKE_CURRENT_BINARY_DIR}/${file}_types.h
60         DESTINATION include/${lib}/${dir}
61         COMPONENT vpp-dev
62       )
63     endforeach()
64   endif()
65
66   if(ARG_DEPENDS)
67     add_dependencies(${lib} ${ARG_DEPENDS})
68   endif()
69
70   # install headers
71   if(ARG_INSTALL_HEADERS)
72     foreach(file ${ARG_INSTALL_HEADERS})
73       get_filename_component(dir ${file} DIRECTORY)
74       install(
75         FILES ${file}
76         DESTINATION include/${lib}/${dir}
77         COMPONENT ${ARG_COMPONENT}-dev
78       )
79     endforeach()
80   endif()
81 endmacro()
82
83 ##############################################################################
84 # header files
85 ##############################################################################
86 function (add_vpp_headers path)
87   foreach(file ${ARGN})
88     get_filename_component(dir ${file} DIRECTORY)
89     install(
90       FILES ${file}
91       DESTINATION include/${path}/${dir}
92       COMPONENT vpp-dev
93     )
94   endforeach()
95 endfunction()