blob: f36fac2d4e3efebfc2cc821ba05858d2c4249308 [file] [log] [blame]
Mohsin Kazmia33db252018-09-13 09:59:50 +02001# 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
14include (UseJava)
15
16find_path(VNET_INCLUDE_DIR NAMES vnet/api_errno.h)
17find_library(VPPINFRA_DIR NAMES vppinfra REQUIRED)
18find_library(VLIBMEMORYCLIENT_DIR NAMES vlibmemoryclient REQUIRED)
19find_library(SVM_DIR NAMES svm REQUIRED)
20
21include_directories(${VNET_INCLUDE_DIR}
22 ${VNET_INCLUDE_DIR}/vpp_plugins
23 ${JNI_INCLUDE_DIRS}
24 ${CMAKE_CURRENT_SOURCE_DIR}
25 ${CMAKE_BINARY_DIR/../vpp/plugins})
26add_compile_options(-Wall)
27############# Common package ##################
28add_library(jvpp_common SHARED jvpp-common/jvpp_common.c)
29set_target_properties(jvpp_common PROPERTIES SOVERSION ${PROJECT_VERSION})
30target_link_libraries(jvpp_common ${VPPINFRA_DIR})
31install(TARGETS jvpp_common DESTINATION lib COMPONENT libjvpp_common)
32install(FILES jvpp-common/jvpp_common.h DESTINATION include/japi/)
33
34set(JVPP_LIBS jvpp_common ${VPPINFRA_DIR} ${VLIBMEMORYCLIENT_DIR} ${SVM_DIR}
35 Threads::Threads m rt)
36
37############# Registry package ##################
38set(PACKAGE_DIR_JVPP_REGISTRY io/fd/vpp/jvpp)
39unset(files)
40FILE(GLOB files RELATIVE
41 ${CMAKE_CURRENT_SOURCE_DIR}
42 ${CMAKE_CURRENT_SOURCE_DIR}/jvpp-registry/${PACKAGE_DIR_JVPP_REGISTRY}/*.java
43 ${CMAKE_CURRENT_SOURCE_DIR}/jvpp-registry/${PACKAGE_DIR_JVPP_REGISTRY}/*/*.java
44)
45
46add_custom_target (jvpp-registry-classes)
47add_custom_command (TARGET jvpp-registry-classes
48 PRE_BUILD
49 COMMAND mkdir -p ${CMAKE_CURRENT_BINARY_DIR}/jvpp-registry
50 COMMAND ${Java_JAVAC_EXECUTABLE}
51 ARGS -d ${CMAKE_CURRENT_BINARY_DIR}/jvpp-registry -h jvpp-registry ${files}
52 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
53)
54
55add_library(jvpp_registry SHARED jvpp-registry/jvpp_registry.c)
56target_link_libraries(jvpp_registry ${JVPP_LIBS})
57include_directories(jvpp-registry)
58add_dependencies(jvpp_registry jvpp_common jvpp-registry-classes)
59add_jar(jvpp-registry ${files} VERSION ${PROJECT_VERSION})
60install_jar(jvpp-registry DESTINATION share/java/)
61
62############## Functions #########################
63function(japigen name)
64 if(NOT VPP_JAVA_APIGEN)
65 set(VPP_JAVA_APIGEN ${CMAKE_CURRENT_SOURCE_DIR}/jvpp/gen/jvpp_gen.py)
66 endif()
67 add_custom_target(japigen-${name} DEPENDS jvpp-registry)
68 add_custom_command(TARGET japigen-${name}
69 POST_BUILD
70 COMMAND mkdir -p jvpp-${name}/target
71 COMMAND mkdir -p ${CMAKE_CURRENT_BINARY_DIR}/jvpp-${name}
72 COMMAND ${VPP_JAVA_APIGEN}
73 ARGS --plugin_name ${name} --root_dir jvpp-${name} -i ${ARGN}
74 COMMAND find jvpp-${name} -name \*.java > jvpp-${name}/jvpp-${name}.files
75 COMMAND ${Java_JAVAC_EXECUTABLE}
76 ARGS -cp ${CMAKE_CURRENT_BINARY_DIR}/jvpp-registry-${PROJECT_VERSION}.jar -d
77 ${CMAKE_CURRENT_BINARY_DIR}/jvpp-${name} -h jvpp-${name}
78 @jvpp-${name}/jvpp-${name}.files
79 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
80 COMMENT "JAVA_API_GEN ${name}"
81 )
82endfunction()
83
84function(jargen name)
85 add_custom_command(TARGET jvpp_${name}
86 POST_BUILD
87 COMMAND cp ${CMAKE_BINARY_DIR}/lib/libjvpp_${name}.so jvpp-${name}/target
88 COMMAND ${Java_JAR_EXECUTABLE} ARGS cf
89 ${CMAKE_CURRENT_BINARY_DIR}/jvpp-${name}-${PROJECT_VERSION}.jar
90 @jvpp-${name}/jvpp-${name}.files
91 COMMAND rm ARGS -rf jvpp-${name}/target jvpp-${name}/jvpp-${name}.files
92 jvpp-${name}/jvpp_${name}_gen.h jvpp-${name}/io_fd_vpp_jvpp_*.h
93 jvpp-registry/io_fd_vpp_jvpp_*.h
94 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
95 COMMENT "JAR_GEN ${name}"
96 )
97 install(
98 FILES ${CMAKE_CURRENT_BINARY_DIR}/jvpp-${name}-${PROJECT_VERSION}.jar
99 DESTINATION share/java
100 )
101endfunction()
102
103function(java_api_binding name src_file)
104 japigen (${name} ${ARGN})
105 add_library(jvpp_${name} SHARED jvpp-${name}/jvpp_${src_file}.c)
106 target_link_libraries(jvpp_${name} ${JVPP_LIBS})
107 include_directories(jvpp-${name})
108 add_dependencies(jvpp_${name} jvpp_common jvpp_registry japigen-${name})
109 jargen (${name})
110endfunction()
111
112############ Core Package #######################
113unset (files)
114unset (JSON_API_PATH)
115set (JSON_API_PATH ${CMAKE_BINARY_DIR}/../vpp)
116
117FILE(GLOB_RECURSE files RELATIVE
118 ${CMAKE_CURRENT_SOURCE_DIR}
119 ${JSON_API_PATH}/vnet/*.api.json
120 ${JSON_API_PATH}/vpp/*.api.json
121)
122
123java_api_binding (core core ${files})
124
125############ Plugin Packages #######################
126unset (ACL_JSON_FILE)
127unset (NAT_JSON_FILE)
128unset (NSH_JSON_FILE)
129unset (GTPU_JSON_FILE)
130unset (PPPOE_JSON_FILE)
131unset (IOAM_TRACE_JSON_FILE)
132unset (IOAM_POT_JSON_FILE)
133unset (IOAM_EXPORT_JSON_FILE)
134
135set (plugin_path ${CMAKE_BINARY_DIR}/../vpp/plugins)
136
137find_file(ACL_JSON_FILE NAMES acl.api.json HINTS ${plugin_path} PATH_SUFFIXES acl)
138find_file(NAT_JSON_FILE NAMES nat.api.json HINTS ${plugin_path} PATH_SUFFIXES nat)
139find_file(NSH_JSON_FILE NAMES nsh.api.json HINTS ${plugin_path} PATH_SUFFIXES nsh)
140find_file(GTPU_JSON_FILE NAMES gtpu.api.json HINTS ${plugin_path} PATH_SUFFIXES gtpu)
141find_file(PPPOE_JSON_FILE NAMES pppoe.api.json HINTS ${plugin_path} PATH_SUFFIXES pppoe)
142find_file(IOAM_TRACE_JSON_FILE NAMES trace.api.json HINTS ${plugin_path} PATH_SUFFIXES ioam/lib-trace)
143find_file(IOAM_POT_JSON_FILE NAMES pot.api.json HINTS ${plugin_path} PATH_SUFFIXES ioam/lib-pot)
144find_file(IOAM_EXPORT_JSON_FILE NAMES ioam_export.api.json HINTS ${plugin_path} PATH_SUFFIXES ioam/export)
145
146if(ACL_JSON_FILE)
147 java_api_binding (acl acl ${ACL_JSON_FILE})
148endif()
149
150if(NAT_JSON_FILE)
151 java_api_binding (nat nat ${NAT_JSON_FILE})
152endif()
153
154if(NSH_JSON_FILE)
155 java_api_binding (nsh nsh ${NSH_JSON_FILE})
156endif()
157
158if(GTPU_JSON_FILE)
159 java_api_binding (gtpu gtpu ${GTPU_JSON_FILE})
160endif()
161
162if(PPPOE_JSON_FILE)
163 java_api_binding (pppoe pppoe ${PPPOE_JSON_FILE})
164endif()
165
166if(IOAM_TRACE_JSON_FILE)
167 java_api_binding (ioamtrace ioam_trace ${IOAM_TRACE_JSON_FILE})
168endif()
169
170if(IOAM_POT_JSON_FILE)
171 java_api_binding (ioampot ioam_pot ${IOAM_POT_JSON_FILE})
172endif()
173
174if(IOAM_EXPORT_JSON_FILE)
175 java_api_binding (ioamexport ioam_export ${IOAM_EXPORT_JSON_FILE})
176endif()